Pagini recente » Cod sursa (job #2909976) | Cod sursa (job #1190114) | Cod sursa (job #698909) | Cod sursa (job #1225814) | Cod sursa (job #595598)
Cod sursa(job #595598)
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
//#define DEBUG
#ifdef DEBUG
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define dprintf(...) do{}while(0)
#endif
using namespace std;
#define N_MAX 16001
int n;
long long v[N_MAX];
int grad[N_MAX];
int *vecini[N_MAX];
int vecini_data[2 * N_MAX];
int muchie_a[N_MAX];
int muchie_b[N_MAX];
int viz[N_MAX];
static inline void readData() {
scanf("%d", &n);
for(int i = 0; i < n; ++i)
grad[i] = 0;
for(int i = 0; i < n; ++i)
scanf("%lld", v + i);
int a, b;
for(int i = 0; i < n - 1; ++i) {
scanf("%d %d", &a, &b);
a--;
b--;
grad[a]++;
grad[b]++;
muchie_a[i] = a;
muchie_b[i] = b;
}
for(int i = 0; i < n; ++i)
vecini[i] = (i == 0 ? vecini_data : (vecini[i - 1] + grad[i - 1]));
for(int i = 0; i < n - 1; ++i) {
a = muchie_a[i];
b = muchie_b[i];
vecini[a][0] = b;
vecini[b][0] = a;
vecini[a]++;
vecini[b]++;
}
for(int i = 0; i < n; ++i)
vecini[i] = (i == 0 ? vecini_data : (vecini[i - 1] + grad[i - 1]));
}
int main() {
freopen("asmax.in", "r", stdin);
freopen("asmax.out", "w", stdout);
readData();
int *s = muchie_a;
int *fii = muchie_b;
int vf = -1;
int x, p;
long long max = v[0];
for(int i = 0; i < n; ++i) {
max = max > v[i] ? max : v[i];
viz[i] = 0;
fii[i] = grad[i];
if(grad[i] == 1)
s[++vf] = i;
}
while(vf >= 0) {
x = s[vf--];
viz[x] = 1;
p = 0;
for(int i = 0; i < grad[x]; ++i)
if(viz[vecini[x][i]] == 0) {
p = vecini[x][0];
break;
}
//contribui
if(v[x] > 0)
v[p] += v[x];
fii[p]--;
if(fii[p] == 1)
s[++vf] = p;
if(fii[p] == 0) {
if(v[p] >= max)
max = v[p];
dprintf("%lld\n", max);
printf("%lld\n", max);
break;
}
}
char *s = NULL;
s[1] = 2;
fclose(stdout);
return 0;
}