Pagini recente » Cod sursa (job #242747) | Cod sursa (job #657349) | Cod sursa (job #2446630) | Cod sursa (job #1213906) | Cod sursa (job #899425)
Cod sursa(job #899425)
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int n, cost[16001], i, j, x, y, max_cost = -1<<30;
vector<int> l[16001];
bool viz[16001];
void dfs(int x)
{
viz[x] = true;
for(int i=0; i<l[x].size(); i++)
if(viz[l[x][i]] == false)
{
dfs(l[x][i]);
if(cost[l[x][i]] > 0)
cost[x] += cost[l[x][i]];
}
if(cost[x] > max_cost) max_cost = cost[x];
}
int main()
{
ifstream fin("asmax.in");
ofstream fout("asmax.out");
//Read
fin>>n;
for(i=1; i<=n; i++)
fin>>cost[i];
for(i=1; i<n; i++)
{
fin>>x>>y;
l[x].push_back(y);
l[y].push_back(x);
}
//Compute
dfs(1);
//Print
fout<<max_cost;
for(i=1; i<=n; i++)
cout<<cost[i]<<" ";
}