Pagini recente » Cod sursa (job #319036) | Cod sursa (job #3029918) | Cod sursa (job #181572) | Cod sursa (job #914743) | Cod sursa (job #2952587)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
#define ll long long
const int maxn = 16e3;
const int maxg = -1e7;
vector<int> a[maxn + 2];
bitset<maxn + 2> visited;
int cost[maxn + 2];
int n;
int ans = maxg;
int dp[maxn + 2];
void read(){
fin >> n;
for(int i = 1; i <= n; ++i) fin >> cost[i];
for(int i = 1; i < n; ++i){
int x, y; fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int i){
visited[i] = 1;
int m1 = cost[i];
for(auto x : a[i]){
if(!visited[x]){
dfs(x);
m1 = max(m1, m1 + dp[x]);
}
}
dp[i] = m1;
ans = max(ans, m1);
}
int main(){
read();
ans = cost[1];
dfs(1);
fout << ans;
return 0;
}
/*
dp[i] suma maxima pe care o pot obtine dintr-un subgraf ce contine nodul i
*/