Pagini recente » Cod sursa (job #1862762) | Cod sursa (job #1966282) | Cod sursa (job #2419017) | Cod sursa (job #760583) | Cod sursa (job #583720)
Cod sursa(job #583720)
#include <stdio.h>
#define NMAX 16000
char viz[NMAX];
long long best[NMAX];
short val[NMAX];
struct nod {
short info;
nod *next;
}*v[NMAX];
void df(short);
int main() {
int n,i,x,y;
nod *c;
long long max;
freopen("asmax.in", "r", stdin);
freopen("asmax.out", "w", stdout);
scanf("%d", &n);
for(i=1;i<=n;i++) {
scanf("%d", &val[i]);
best[i] = val[i];
}
for(i=0;i<n-1;i++) {
scanf("%d", &x);
scanf("%d", &y);
c = new nod;
c->info = y;
c->next = v[x];
v[x] = c;
c = new nod;
c->info = x;
c->next = v[y];
v[y] = c;
}
df(1);
max = 0;
for(i=1;i<=n;i++) {
if(max < best[i]) {
max = best[i];
}
}
printf("%d", max);
return 0;
}
void df(short x) {
nod *c;
short curent;
long long rez;
viz[x] = 1;
for(c = v[x]; c; c=c->next) {
if(c && !viz[c->info]) {
curent = c->info;
df(curent);
//rez = best[curent];
//if(rez == -1001) {
// rez = val[curent];
//}
if(best[curent] > 0)
best[x] += best[curent];
}
}
}