Pagini recente » Cod sursa (job #1522818) | Cod sursa (job #2052291) | Cod sursa (job #1962824) | Cod sursa (job #3204688) | Cod sursa (job #2135945)
#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;
int sum = cost[x];
for(i = 0; i < a[x].size(); i++)
{
y = a[x][i];
if(!viz[y]) sum += dfs(y);
}
cost[x] = sum;
if(cost[x] < 0) return 0;
else return 1;
}
void read()
{
fin >> n;
int i, x, y;
for(i = 0; 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 << total;
fin.close();
return 0;
}