-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathP1122.cc
39 lines (36 loc) · 797 Bytes
/
P1122.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdio>
#include <cstdlib>
#include <vector>
using std::vector;
#define maxn 16000
int dp[maxn];
int v[maxn];
vector<int> tree[maxn];
void DP(int u, int parent) {
dp[u] = v[u];
for (auto it = tree[u].begin(); it != tree[u].end(); it++)
if (*it != parent) {
DP(*it, u);
if (dp[*it] > 0) dp[u] += dp[*it];
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &v[i]);
int a, b;
for (int i = 0; i < n - 1; i++) {
scanf("%d %d", &a, &b);
a--; b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
DP(0, -1);
int ans = dp[0];
for (int i = 1; i < n; i++) {
if (dp[i] > ans) ans = dp[i];
}
printf("%d\n", ans);
return 0;
}