Pagini recente » Cod sursa (job #2115160) | Cod sursa (job #2434257) | Cod sursa (job #2838389) | Cod sursa (job #1105420) | Cod sursa (job #897304)
Cod sursa(job #897304)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
const int N=50001;
vector<int> a[N];
int n,c[N],p[N];
bool viz[N];
void citire()
{
int x,y;
f>>n;
for(int i=1; i<=n; ++i)
f>>c[i];
for(int i=1; i<=n-1; ++i)
{
f>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
int y;
viz[x] = true;
p[x]=c[x];
for(size_t i=0; i<a[x].size(); ++i)
{
y=a[x][i];
if (!viz[y])
{
dfs(y);
if(p[y]>0)
p[x]+=p[y];
}
}
}
int main()
{
citire();
int max=-1001;
dfs(1);
for(int i=1; i<=n; ++i)
if (p[i]>max) max = p[i];
g<<max;
return 0;
}