Pagini recente » Cod sursa (job #2361180) | Cod sursa (job #561619) | Cod sursa (job #1968856) | Cod sursa (job #2990948) | Cod sursa (job #759870)
Cod sursa(job #759870)
#include <fstream>
#include <vector>
using namespace std;
int n, sol = -2000000000;
int v[16100], ap[16100], cost[16100];
vector <int> A[16100];
void DFs(int nod)
{
ap[nod] = 1;
cost[nod] = v[nod];
for (vector <int> :: iterator it = A[nod].begin(); it != A[nod].end(); ++it)
{
if (!ap[*it])
{
DFs(*it);
if (cost[*it] > 0) cost[nod] += cost[*it];
}
}
}
int main()
{
ifstream f("asmax.in");
ofstream g("asmax.out");
f >> n;
for (int i = 1; i <= n; ++i) f >> v[i];
for (int i = 1; i < n; ++i)
{
int x, y;
f >> x >> y;
A[x].push_back(y);
A[y].push_back(x);
}
DFs(1);
for (int i = 1; i <= n; ++i) sol = max(sol, cost[i]);
g << sol << '\n';
g.close();
return 0;
}