Pagini recente » Cod sursa (job #1097034) | Cod sursa (job #165605) | Cod sursa (job #2493486) | Cod sursa (job #317100) | Cod sursa (job #3209878)
#include <iostream>
#include <fstream>
#include <vector>
#include <stdint.h>
#include <limits.h>
const int32_t MAX_N = 16000;
int32_t vals[MAX_N];
std::vector<int32_t> adj[MAX_N];
int32_t max[MAX_N];
void DFS(int32_t node, int32_t prev) {
int32_t maxSum = vals[node];
for(int32_t next : adj[node]) {
if(next == prev)
continue;
DFS(next, node);
if(max[next] > 0)
maxSum += max[next];
}
max[node] = maxSum;
}
int main() {
std::ifstream fin("asmax.in");
std::ofstream fout("asmax.out");
int32_t n;
fin >> n;
for(int32_t i = 0; i != n; ++i)
fin >> vals[i];
for(int32_t i = 0; i != n - 1; ++i) {
int32_t x, y;
fin >> x >> y;
--x; --y;
adj[x].push_back(y);
adj[y].push_back(x);
}
DFS(0, -1);
int32_t maxSum = INT_MIN;
for(int32_t i = 0; i != n; ++i) {
if(max[i] > maxSum)
maxSum = max[i];
}
fout << maxSum;
fin.close();
fout.close();
return 0;
}