Pagini recente » Cod sursa (job #2677654) | Cod sursa (job #1029266) | Cod sursa (job #2852281) | Cod sursa (job #419947) | Cod sursa (job #2830730)
#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);
}
dfs(1);
g<<maxim;
return 0;
}