Pagini recente » Cod sursa (job #3347895) | Cod sursa (job #3323581) | Cod sursa (job #1834513) | Cod sursa (job #107616) | Cod sursa (job #3336981)
#include <bits/stdc++.h>
const int NMAX = 16005;
// Suma maxima daca incepem
// subgraful din nodul [i]
std::vector<int> dp;
std::vector<int> graph[NMAX];
void DFS(int node, int parent) {
for (int neighbour : graph[node]) {
if (neighbour != parent) {
DFS(neighbour, node);
if (dp[neighbour] > 0) {
dp[node] += dp[neighbour];
}
}
}
}
int main() {
std::ifstream fin("asmax.in");
std::ofstream fout("asmax.out");
int n;
fin >> n;
dp.resize(n + 1, 0);
for (int i = 1; i <= n; ++i) {
fin >> dp[i];
}
for (int i = 1; i <= n; ++i) {
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
DFS(1, 0);
fout << *std::max_element(dp.begin() + 1, dp.end()) << '\n';
return 0;
}