Pagini recente » Cod sursa (job #328390) | Cod sursa (job #584290) | Cod sursa (job #1999234) | Cod sursa (job #731320) | Cod sursa (job #2780752)
#include<bits/stdc++.h>
using namespace std;
int n, rs, s, a[16050];
int dp[16050];
vector<int> V[16050];
void DFS(int x, int pr) {
for (auto it: V[x]) {
if (it != pr) {
DFS(it, x);
if (dp[it] >= 0)
dp[x] += dp[it];
}
}
dp[x] += a[x];
rs = max(rs, dp[x]);
}
int main() {
// ifstream cin("asmax.in");
// ofstream cout("asmax.out");
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i], s += a[i];
for (int i = 1; i < n; ++i) {
int x, y;
cin >> x >> y;
V[x].push_back(y);
V[y].push_back(x);
}
rs = s;
DFS(1, -1);
cout << rs;
return 0;
}