Pagini recente » Cod sursa (job #803323) | Cod sursa (job #2453605) | Cod sursa (job #2752124) | Profil anna_bozianu | Cod sursa (job #2764960)
#include <fstream>
#include <vector>
using namespace std;
int n;
int Max;
int cost[16001];
int dp[16001];
vector<int> a[16001];
void read() {
int i, x, y;
ifstream f("asmax.in");
f >> n;
for (i = 1; i <= n; i++)
f >> cost[i];
for (i = 1; i < n; i++) {
f >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
f.close();
}
void dfs(int x, int px) {
for (int y : a[x])
if (y != px) {
dfs(y, x);
dp[x] += dp[y];
}
if (dp[x] + cost[x] > Max)
Max = dp[x] + cost[x];
dp[x] = max(0, dp[x] + cost[x]);
}
void solve() {
int i;
Max = -16000 * 1000 - 1;
for (i = 1; i <= n; i++)
Max = max(Max, cost[i]);
dfs(1, 0);
}
void output() {
ofstream g("asmax.out");
g << Max;
g.close();
}
int main() {
read();
solve();
output();
return 0;
}