Pagini recente » Atasamentele paginii Profil YO128813 | Cod sursa (job #3153414) | Borderou de evaluare (job #2324843) | Borderou de evaluare (job #2915659) | Cod sursa (job #3212752)
#include <fstream>
#include <climits>
#include <vector>
using namespace std;
const int nmax = 16005;
vector<int> v[nmax];
int val[nmax], dp[nmax];
void dfs(int node, int tactu_mare){
dp[node] = val[node];
for(int vec: v[node]){
if(vec != tactu_mare){
dfs(vec, node);
if(dp[vec] > 0){
dp[node] += dp[vec];
}
}
}
}
int main(){
ifstream f("asmax.in");
ofstream g("asmax.out");
int n;
f >> n;
for(int i = 1; i <= n; i++){
f >> val[i];
}
for(int i = 1; i <= n; i++){
int x, y;
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1, 0);
int sol = INT_MIN;
for(int i = 1; i <= n; i++){
sol = max(sol, dp[i]);
}
g << sol << '\n';
}