Pagini recente » Cod sursa (job #1042291) | cevaproblemeoji | Cod sursa (job #1179678) | Cod sursa (job #2147264) | Cod sursa (job #2830732)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
vector<vector<int>> a;
int n,x,y,maxim;
vector<int> c;
vector<bool> viz;
int dfs(int n){
viz[n]= true;
int suma=c[n];
for(int x : a[n]){
if(!viz[x]){
suma=max(suma,suma+dfs(x));
}
}
if(suma>maxim)
maxim=suma;
return suma;
}
int main() {
f>>n;
a.resize(n+1);
c.resize(n+1);
viz.resize(n+1,false);
for(int i = 1 ; i <=n ;++i){
f>>c[i];
}
for(int i=0 ; i<n-1;++i){
f>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
maxim=-10001;
dfs(1);
g<<maxim;
return 0;
}