Pagini recente » Cod sursa (job #1868139) | Cod sursa (job #2830088) | Cod sursa (job #159560) | Cod sursa (job #2154251) | Cod sursa (job #2828205)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int nMax = 16001;
int n;
int cost[nMax];
int maxCost[nMax];
vector<int> ad[nMax];
void DFS(int current, int parent) {
maxCost[current] = cost[current];
for (auto &w : ad[current]) {
if (w != parent) {
DFS(w, current);
if (maxCost[w] > 0) {
maxCost[current] += maxCost[w];
}
}
}
}
int main() {
int x, y;
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> cost[i];
}
for (int i = 1; i <= n; ++i) {
fin >> x >> y;
ad[x].push_back(y);
ad[y].push_back(x);
}
fin.close();
DFS(1, 0);
int ans = -1000000000;
for (int i = 1; i <= n; ++i) {
ans = max(ans, maxCost[i]);
}
fout << ans << '\n';
fout.close();
return 0;
}