Pagini recente » Cod sursa (job #2666236) | Cod sursa (job #2534300) | Cod sursa (job #2621408) | Cod sursa (job #327360) | Cod sursa (job #1803770)
#include <bits/stdc++.h>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
typedef long long int lld;
const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
const int NMAX = 16e3;
int N;
int val[NMAX + 5];
vector<int> V[NMAX + 5];
bitset < NMAX + 5 > viz;
int dp[NMAX + 5];
void dfs(int x) {
viz[x] = 1;
dp[x] = val[x];
for (int y : V[x]) {
if (!viz[y]) {
dfs(y);
dp[x] += max(0, dp[y]);
}
}
}
int main() {
cin.sync_with_stdio(false);
freopen("asmax.in", "r", stdin);
freopen("asmax.out", "w", stdout);
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> val[i];
}
for (int i = 1, x, y; i <= N - 1; ++i) {
cin >> x >> y;
V[x].push_back(y);
V[y].push_back(x);
}
dfs(1);
cout << *max_element(dp + 1, dp + N + 1);
return 0;
}