Pagini recente » Cod sursa (job #1849389) | Cod sursa (job #3223148) | Cod sursa (job #303991) | Cod sursa (job #1743113) | Cod sursa (job #3246030)
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
void dfs(vector<vector<int>> &graph, vector<int> &asmax, vector<int> v,
int node, int father){
asmax[node] = v[node];
for(int son : graph[node]){
if(son != father){
dfs(graph, asmax, v, son, node);
if(v[son] >= 0){
asmax[node] += v[son];
}
}
}
}
int main(){
int n;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
fin >> n;
vector<int> v, asmax(n+1);
vector<vector<int>> graph(n+1, vector<int>());
v.push_back(0);
for(int i = 1; i <= n; i++){
int x;
fin >> x;
v.push_back(x);
}
for(int i = 1; i <= n-1; i++){
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs(graph, asmax, v, 1, 0);
int sol = INT_MIN;
for(int i = 1; i <= n; i ++){
sol = max(sol, asmax[i]);
}
fout << sol;
return 0;
}