Pagini recente » Cod sursa (job #2130884) | Cod sursa (job #3287246) | Cod sursa (job #1340560) | Cod sursa (job #122270) | Cod sursa (job #2135955)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int N = 16001;
int n, m, cost[N];
vector <int> a[N];
bool viz[N];
int dfs(int x)
{
viz[x] = true;
int y, i;
for(i = 0; i < a[x].size(); i++)
{
y = a[x][i];
if(!viz[y])
{
dfs(y);
if(cost[y] > 0) cost[x] += cost[y];
}
}
}
void read()
{
fin >> n;
int i, x, y;
for(i = 1; i <= n; i++)
fin >> cost[i];
m = n - 1;
for(i = 0; i < m; i++)
{
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
}
int main() {
read();
int i, total = 0;
dfs(1);
for(i = 1; i <= n; i++)
{
total = max(total, cost[i]);
}
fout <<'\n' << total;
fin.close();
return 0;
}