Pagini recente » Cod sursa (job #2195450) | Cod sursa (job #1242409) | Cod sursa (job #68402) | Cod sursa (job #2851450) | Cod sursa (job #3004152)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n, val[16005], dp[16005];
vector <int> a[16005];
bitset <16005> viz;
void Dfs(int x, int last)
{
viz[x] = 1;
dp[x] = val[x];
for (auto w : a[x])
if (viz[w] == 0)
{
Dfs(w, x);
dp[x] = max(dp[x], dp[x] + dp[w]);
}
}
int32_t main()
{
int i, x, y, sol = -2e9;
fin >> n;
for (i = 1; i <= n; i++)
fin >> val[i];
for (i = 1; i < n; i++)
{
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
Dfs(1, 0);
for (i = 1; i <= n; i++)
sol = max(sol, dp[i]);
fout << sol << "\n";
return 0;
}