Pagini recente » simulareoji2020 | Cod sursa (job #1510113) | Cod sursa (job #1556496) | Cod sursa (job #588837) | Cod sursa (job #2823105)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int nmax = 20000;
vector<int> adj[nmax];
int maxSum[nmax];
bool vis[nmax];
void dfs(int crt) {
vis[crt] = true;
for(auto ngb: adj[crt]) {
if(vis[ngb] == false) {
dfs(ngb);
if(maxSum[ngb] > 0) {
maxSum[crt] += maxSum[ngb];
}
}
}
}
int main() {
int N;
fin >> N;
for(int i = 1; i <= N; ++i) {
int x; fin >> x;
maxSum[i] = x;
}
for(int i = 1; i <= N - 1; ++i) {
int x, y;
fin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
int maxx = INT_MIN;
for(int i = 1; i <= N; ++i) {
maxx = max(maxx, maxSum[i]);
}
fout << maxx << '\n';
return 0;
}