Pagini recente » Cod sursa (job #1459831) | Cod sursa (job #1618634) | Cod sursa (job #1648923) | Cod sursa (job #1064492) | Cod sursa (job #3214420)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n;
vector<int> val(vec_len);
vector<vector<int>> graph(vec_len);
const int vec_len = 200001;
int res = INT_MIN;
bitset<vec_len> vis;
void dfs(int node){
vis[node] = true;
for (int next : graph[node])
if (!vis[next]){
dfs(next);
if (val[next] > 0)
val[node] += val[next];
}
res = max(res, val[node]);
}
int main(){
fin >> n;
for (int i = 1; i <= n; i++)
fin >> val[i];
int x, y;
for (int i = 1; i < n; i++){
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs(1);
fout << res;
return 0;
}