Pagini recente » Cod sursa (job #626651) | Cod sursa (job #1980292) | Cod sursa (job #3144019) | Cod sursa (job #58388) | Cod sursa (job #833908)
Cod sursa(job #833908)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
using namespace std;
const int MAXN = 16001;
int n;
vector<int> G[MAXN];
int cost[MAXN];
bool viz[MAXN];
int indeg[MAXN];
int max_sum(int node)
{
bool found = false;
viz[node] = true;
int ssum = 0;
vector<int>::iterator neigh;
for (neigh = G[node].begin(); neigh != G[node].end(); ++neigh) {
if (viz[*neigh]) continue;
found = true;
ssum += max(0, max_sum(*neigh));
}
if (!found) {
return cost[node];
} else {
return ssum + cost[node];
}
}
int main()
{
freopen("asmax.in", "r", stdin);
freopen("asmax.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &cost[i]);
int max_in = 0;
vector<int> poss;
for (int i = 0; i < n - 1; ++i) {
int x, y; scanf("%d %d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
indeg[x]++, indeg[y]++;
if (max_in == indeg[x]) poss.push_back(x);
if (max_in == indeg[y]) poss.push_back(y);
if (max_in < indeg[x]) {
max_in = indeg[x];
poss.clear(); poss.push_back(x);
}
if (max_in < indeg[y]) {
max_in = indeg[y];
poss.clear(); poss.push_back(y);
}
}
int result = cost[1];
for (size_t i = 0; i < poss.size(); ++i) {
memset(viz, false, sizeof(viz));
result = max(result, max_sum(poss[i]));
}
printf("%d\n", result);
return 0;
}