Pagini recente » Cod sursa (job #2483121) | Cod sursa (job #2536830) | Cod sursa (job #1630039) | Cod sursa (job #1769590) | Cod sursa (job #3246029)
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
void dfs(vector<vector<int>> &graph, vector<int> &asmax, vector<int> v,
vector<int> &mark, int node){
asmax[node] = v[node];
mark[node] = 1;
for(int son : graph[node]){
if(mark[son] == 0){
dfs(graph, asmax, v, mark, son);
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), mark(n+1);
mark[0] = 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, mark, 1);
int sol = INT_MIN;
for(int i = 1; i <= n; i ++){
sol = max(sol, asmax[i]);
}
fout << sol;
return 0;
}