Pagini recente » Cod sursa (job #307195) | Cod sursa (job #41394) | Cod sursa (job #2838647) | Cod sursa (job #1698861) | Cod sursa (job #2347130)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
const int N = 16001;
int n, x, y, v[N];
bool viz[N];
vector <int> a[N];
void dfs(int x)
{
viz[x] = true;
for(auto y: a[x])
{
if(!viz[y])
{
dfs(y);
if(v[y] > 0)
v[x] += v[y];
}
}
}
int main()
{
in>>n;
for(int i=1; i<=n; i++)
in>>v[i];
for(int i=1; i<n; i++)
{
in>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
out<<v[1];
return 0;
}