Pagini recente » Cod sursa (job #1772778) | Cod sursa (job #2246437) | Cod sursa (job #2087347) | Cod sursa (job #2069143) | Cod sursa (job #2827235)
#include <bits/stdc++.h>
#define NMax 16001
#define inf -16000001
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int N;
vector <int> gr[NMax];
vector <int> v; // valorile initiale ale nodurilor
vector <int> v_max; // v_max[i] = valoarea maxima a subarborelui cu radacina in nodul i
bool viz[NMax];
void Read()
{
fin >> N;
v.resize(N+1);
v_max.resize(N+1);
for(int i = 1; i <= N; ++i)
fin >> v[i];
for(int i = 1; i < N; ++i)
{
int x, y;
fin >> x >> y;
gr[x].push_back(y);
gr[y].push_back(x);
}
}
void DFS(int s)
{
viz[s] = 1;
v_max[s] = v[s];
for(auto i : gr[s])
{
if(!viz[i])
{
DFS(i);
if(v_max[i] > 0)
v_max[s] += v_max[i];
}
}
}
void Solve()
{
int val_max = inf;
for(int i = 1; i <= N; ++i)
val_max = max(val_max, v_max[i]);
fout << val_max;
}
int main()
{
Read();
DFS(1); // conex, aciclic
Solve();
return 0;
}