Pagini recente » Cod sursa (job #2383627) | Cod sursa (job #3289511) | Cod sursa (job #3229357)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
const int N = 16000;
int val[N + 1];
int nrd[N + 1]; //nr de descendenti
bool viz[N + 1];
vector<int>a[N + 1];
int maxx = -1001;
void dfs(int x){
viz[x] = 1;
nrd[x] = 1;
for(auto y : a[x]){
if(!viz[y]){
dfs(y);
nrd[x] += nrd[y];
val[x] = max(val[x], val[x] + val[y]);
}
}
maxx = max(maxx, val[x]);
}
int main(){
int n, x, y;
in >> n;
for(int i = 1; i <= n; i++) in >> val[i];
for(int i = 1; i <= n - 1; i++){
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
out << maxx;
return 0;
}