Pagini recente » hk | Cod sursa (job #2856986) | Cod sursa (job #940926) | Cod sursa (job #3169525) | Cod sursa (job #933496)
Cod sursa(job #933496)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int n, v[16005];
bool vis[16005];
vector<int> muchii[16005];
int vmax = -16000005;
int go(int nod)
{
vis[nod] = true;
int s = v[nod];
for(unsigned i = 0; i < muchii[nod].size(); ++i)
{
if(!vis[muchii[nod][i]])
{
int ret = go(muchii[nod][i]);
if(ret > 0)
{
vmax = max(vmax, ret);
s += ret;
}
}
}
vmax = max(vmax, s);
return s;
}
int main()
{
ifstream in("asmax.in");
ofstream out("asmax.out");
in >> n;
for(int i = 0; i < n; ++i)
in >> v[i];
for(int i = 0; i < n-1; ++i)
{
int a, b;
in >> a >> b;
--a, --b;
muchii[a].push_back(b);
muchii[b].push_back(a);
}
int root = 0;
go(root);
out << vmax;
return 0;
}