Pagini recente » Cod sursa (job #2292670) | Cod sursa (job #1545371) | Cod sursa (job #1758016) | Cod sursa (job #1453642) | Cod sursa (job #2591302)
#include <bits/stdc++.h>
#define newline '\n'
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
///***********************
const int NMAX = (1 << 4) * 1e3 + 5;
int n, a[NMAX], ans = -1e3 + 5;
vector<int> graph[NMAX];
void read()
{
fin >> n;
for (int i = 1; i <= n; i++)
fin >> a[i];
for (int i = 1; i < n; i++)
{
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
void dfs(int node, int father)
{
for (auto it : graph[node])
{
if (it != father)
{
dfs(it, node);
if (a[it] > 0)
a[node] += a[it];
}
}
ans = max(ans, a[node]);
}
int main()
{
read();
dfs(1, 0);
fout << ans << newline;
fout.close();
return 0;
}