Pagini recente » Cod sursa (job #1812989) | Cod sursa (job #30942) | Cod sursa (job #988559) | Cod sursa (job #500683) | Cod sursa (job #2531677)
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
#ifdef DEBUG
string name = "data";
#else
string name = "asmax";
#endif
ifstream fin(name + ".in");
ofstream fout(name + ".out");
#define MAXN 16001
int n;
int best[MAXN];
int cost[MAXN];
vector<int> g[MAXN];
int solve(int x) {
if (best[x] != -INF) {
return best[x];
}
best[x] = cost[x];
for (auto y: g[x]) {
int child = solve(y);
if (child > 0) {
best[x] += child;
}
}
return best[x];
}
int main() {
fin >> n;
for (int i = 1; i <= n; ++i) {
int x;
fin >> x;
cost[i] = x;
best[i] = -INF;
}
for (int i = 0; i < n - 1; ++i) {
int x,y;
fin >> x >> y;
g[x].push_back(y);
}
int sol = -INF;
for (int i = 1; i <= n; ++i) {
sol = max(sol, solve(i));
}
fout << sol;
return 0;
}