Pagini recente » Cod sursa (job #1776373) | Cod sursa (job #1046375) | Cod sursa (job #2910618) | Cod sursa (job #2885335) | Cod sursa (job #2055141)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mp make_pair
#define CHECK(x) if(!(x)) return false;
#ifdef INFOARENA
#define ProblemName "asmax"
#endif
#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "fis.in"
#define OuFile "fis.out"
#endif
const int MAXN = 16010;
vector<int> G[MAXN];
bool viz[MAXN];
int v[MAXN];
int best = INT_MIN;
int DFS(int x) {
viz[x] = true;
int cand = v[x];
for (const auto &it : G[x]) {
if (viz[it])
continue;
int t = DFS(it);
if (t > 0)
cand += t;
}
best = max(best, cand);
return cand;
}
int main() {
assert(freopen(InFile, "r", stdin));
assert(freopen(OuFile, "w", stdout));
int N;
scanf("%d", &N);
for (int i = 0; i < N; ++i)
scanf("%d", &v[i]);
for (int i = 0; i < N - 1; ++i) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
G[a].push_back(b);
G[b].push_back(a);
}
DFS(0);
printf("%d\n", best);
return 0;
}