Pagini recente » Cod sursa (job #2708587) | Cod sursa (job #3140067) | Cod sursa (job #300442) | Cod sursa (job #1665515) | Cod sursa (job #578523)
Cod sursa(job #578523)
#include <fstream>
#include <vector>
#define input "asmax.in"
#define output "asmax.out"
using namespace std;
const int N=16001;
const int s=1;
int val[N],cost[N],smax=-(1<<30),n;
vector<int> g[N];
bool iw[N];
void citire()
{
int x,y,i;
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&val[i]);
for (i=1;i<=n-1;i++)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
}
void dfs(int x)
{
if (iw[x])
return;
iw[x]=true;
cost[x]=val[x];
for (int i=0;i<g[x].size();i++)
{
int y=g[x][i];
if (!iw[y])
{
df(y);
if (cost[y]>0)
cost[x]+=cost[y];
}
}
if (cost[x]>smax)
smax=cost[x];
}
void afisare()
{
printf("%d",smax);
}
int main()
{
freopen(input,"r",stdin);
freopen(output,"w",stdout);
citire();
dfs(s);
afisare();
return 0;
}