Pagini recente » Cod sursa (job #3228489) | Cod sursa (job #523839) | Cod sursa (job #1094423) | Cod sursa (job #2680892) | Cod sursa (job #3281000)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
/**
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
*/
int n, sum, cnt, maxx, v[16003];
vector <int> g[16003];
bitset <16003> viz;
void DFS(int x)
{
viz[x] = 1;
maxx = max(cnt, cnt + v[x]);
cnt += v[x];
for (int i : g[x])
if (viz[i] == 0)
DFS(i);
}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i, j, x, y;
fin >> n;
for (i = 1; i <= n; i++)
fin >> v[i];
for (i = 1; i < n; i++)
{
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
viz[1] = 1;
for (int i : g[1])
{
cnt = maxx = 0;
DFS(i);
//fout << maxx << " ";
sum += maxx;
}
fout << v[1] + sum << "\n";
return 0;
}