Pagini recente » Cod sursa (job #2723221) | Cod sursa (job #676756) | Cod sursa (job #639842) | Cod sursa (job #3188292) | Cod sursa (job #2490443)
#include <bits/stdc++.h>
#define NMAX 16001
using namespace std;
ifstream fin{"asmax.in"};
ofstream fout{"asmax.out"};
vector<vector<int>> arbore(NMAX, vector<int>());
int N, V[NMAX], ans = -2000000000;
int dpDFS(int nod, int tata)
{
int s_max_nod = V[nod];
for(auto& fiu : arbore[nod])
{
if(fiu == tata) continue;
int s_max_fiu = dpDFS(fiu, nod);
s_max_nod += max(0, s_max_fiu);
}
ans = max(ans, s_max_nod);
return s_max_nod;
}
int main()
{
fin >> N;
for(int i = 1; i <= N; ++i)
{
fin >> V[i];
}
for(int i = 1; i < N; ++i)
{
int x, y;
fin >> x >> y;
arbore[x].push_back(y);
arbore[y].push_back(x);
}
dpDFS(1, -1);
fout << ans;
}