Pagini recente » Cod sursa (job #2139983) | Cod sursa (job #2570488) | Cod sursa (job #2126676) | Cod sursa (job #893554) | Cod sursa (job #3198534)
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
int n;
const int nmax = 16005;
int maxdp[nmax], val[nmax];
vector<int> a[nmax];
void dfs(int node, int tactu_mare){
maxdp[node] = val[node];
for(int vec : a[node]){
if(vec != tactu_mare){
dfs(vec, node);
if(maxdp[vec] > 0){
maxdp[node] += maxdp[vec];
}
}
}
}
int main(){
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;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1, 0);
int sol = INT_MIN;
for(int i = 1; i <= n; i++){
sol = max(sol, maxdp[i]);
}
g << sol << '\n';
}