Pagini recente » Cod sursa (job #639594) | Cod sursa (job #2798886) | Cod sursa (job #3178922) | Cod sursa (job #1095658) | Cod sursa (job #3216055)
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
const int nmax = 16005;
int n;
vector<int> l[nmax];
int dp[nmax], val[nmax];
void dfs(int node, int father){
dp[node] += val[node];
for(auto son: l[node]){
if(son != father){
dfs(son, node);
if(dp[son] > 0){
dp[node] += dp[son];
}
}
}
}
int main(){
ifstream f("asmax.in");
ofstream g("asmax.out");
f >> n;
for(int i = 1; i <= n; i++){
f >> val[i];
}
for(int i = 1; i <= n - 1; i++){
int x, y;
f >> x >> y;
l[x].push_back(y);
l[y].push_back(x);
}
dfs(1, 0);
int maxim = INT_MIN;
for(int i = 1; i <= n; i++){
maxim = max(maxim, dp[i]);
}
g << maxim << '\n';
}