Cod sursa(job #3375518)
| Utilizator | Data | 12 noiembrie 2026 10:48:20 | |
|---|---|---|---|
| Problema | Asmax | Scor | 90 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva de probleme | Marime | 0.66 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int N = 16002;
int n, cost[N], fr[N], suma[N], ans;
vector<int> g[N];
void dfs(int nod){
fr[nod] = 1;
suma[nod] = cost[nod];
for(int e : g[nod])
if(!fr[e]){
dfs(e);
suma[nod] += max(0, suma[e]);
}
ans = max(ans, suma[nod]);
}
int32_t main(){
int i, x, y;
fin >> n;
for(i=1;i<=n;i++)
fin >> cost[i];
for(i=1;i<n;i++){
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
fout << ans;
}
