Pagini recente » Statistici Motiu Radu (kerry6205) | Cod sursa (job #405956) | Cod sursa (job #159214) | B.M.D. | Cod sursa (job #1278047)
#include <cstdio>
#include <vector>
using namespace std;
vector<int> g[16001];
bool vis[16001];
bool root[16001];
int v[16001];
int sol = -0x3f3f3f3f;
int dfs(int node)
{
vis[node] = 1;
int s = v[node];
for (unsigned i = 0; i < g[node].size(); i++)
if (!vis[g[node][i]]) {
s += dfs(g[node][i]);
}
if (s < 0)
s = 0;
if (s > sol)
sol = s;
return s;
}
int main()
{
FILE *in = fopen("asmax.in", "r");
FILE *out = fopen("asmax.out", "w");
int n;
fscanf(in, "%d", &n);
int nmax = -2000, nstart = 1;
for (int i = 1; i <= n; ++i)
{
fscanf(in, "%d", &v[i]);
if (v[i] > nmax)
nmax = v[i], nstart = i;
}
for (int x, y, i = 1; i < n; ++i)
{
fscanf(in, "%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
fprintf(out, "%d\n", sol);
return 0;
}