Pagini recente » Cod sursa (job #1050523) | Borderou de evaluare (job #1837983) | Cod sursa (job #529505) | Cod sursa (job #2651967) | Cod sursa (job #569999)
Cod sursa(job #569999)
#include <cstdio>
#include <list>
#define Nmax 16001
using namespace std;
list<int> G[Nmax];
int C[Nmax], N, rez;
int uz[Nmax];
int df(int x) {
uz[x] = 1;
if (G[x].size() == 1 && x != 1) {
if (C[x] > rez) rez = C[x];
return C[x];
}
int temp;
temp = C[x];
for (list<int>::iterator it = G[x].begin(); it != G[x].end(); ++it) {
if (!uz[*it]) {
int aux = df(*it);
if (aux > 0) temp += aux;
}
}
if (temp > rez) rez = temp;
return temp;
}
int main() {
freopen("asmax.in", "r", stdin);
freopen("asmax.out", "w", stdout);
scanf("%d\n", &N);
for (int i = 1; i <= N; ++i ) scanf("%d ", C + i);
for (int i = 0; i < N; ++i) {
int x, y;
scanf("%d %d\n", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
int temp = df(1);
if (temp > rez) rez = temp;
printf("%d\n", rez);
return 0;
}