Pagini recente » Cod sursa (job #1649465) | Cod sursa (job #1452966) | Cod sursa (job #1782963) | Cod sursa (job #1487106) | Cod sursa (job #2824778)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n;
vector <int> graph[16005];
int val[16005];
bool visited[16005];
int bkt(int node, int oldNode)
{
if (visited[node] == true)
{
return 0;
}
int sum = val[node];
visited[node] = true;
for (int x : graph[node])
{
if (x == oldNode)
{
continue;
}
int newVal = bkt(x, node);
if (newVal > 0)
{
sum += newVal;
}
}
return sum;
}
int main()
{
fin >> n;
for (int i = 1; i <= n; i++)
{
fin >> val[i];
}
for (int i = 0; i < n - 1; i++)
{
int a, b;
fin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
fout << bkt(1, 0) << '\n';
}