Pagini recente » Cod sursa (job #2172704) | Cod sursa (job #2465555) | Cod sursa (job #1975228) | Cod sursa (job #694215) | Cod sursa (job #3280511)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int solution = -1e9;
int Sum_Max(int node, int parent, vector<int> &arr, vector<vector<int>> &graph) {
int val = arr[node];
for(auto neighbor : graph[node]) {
if(neighbor != parent) {
int newSum = Sum_Max(neighbor, node, arr, graph);
if(newSum > 0)
val+= newSum;
}
}
solution = max(solution, val);
return val;
}
int main() {
int N;
fin >> N;
vector<int> arr(N+1);
for(int i=1; i<=N; i++)
fin >> arr[i];
vector<vector<int>> graph(N+1, vector<int>());
for(int i=1; i<N; i++) {
int x,y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
Sum_Max(1, -1, arr, graph);
fout << solution;
return 0;
}