Pagini recente » Monitorul de evaluare | Statistici Andon Tepavicharov (DonchoBonboncho) | Monitorul de evaluare | Cod sursa (job #672564) | Cod sursa (job #570003)
Cod sursa(job #570003)
#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 i, max, ok = 0;
for (i = 1; i <= N; ++i)
if (ok == 0 || C[i] > max) {
max = C[i];
ok = 1;
}
if (max < 0) {
printf("%d\n", max);
return 0;
}
int temp = df(1);
if (temp > rez) rez = temp;
printf("%d\n", rez);
return 0;
}