Pagini recente » Cod sursa (job #1862757) | Cod sursa (job #639654) | Cod sursa (job #2726021) | Cod sursa (job #1422755) | Cod sursa (job #1616986)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int NMAX = 16000;
const int INF = 0x7fffffff;
int n;
int cost[NMAX + 1];
int best[NMAX + 1]; //best[i] = cel mai bun cost care se poate forma cu un subarbore cu radacina in i, care il cuprinde pe i
//considerand ca radacina este in nodul 1
vector<int> g[NMAX + 1];
int smax = -INF;
void read() {
fin >> n;
for(int i = 1; i <= n ; ++i)
fin >> cost[i];
for(int i = 1; i < n ; ++i) {
int x; int y;
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
}
void dfs(int node, int t) {
for(int x : g[node])
if(x != t)
dfs(x, node);
best[node] = cost[node];
for(int x : g[node])
if(x != t && best[x] > 0)
best[node] += best[x];
if(best[node] > smax) smax = best[node];
}
void solve() {
dfs(1, 0);
fout << smax << '\n';
}
int main() {
read();
solve();
return 0;
}