Pagini recente » Cod sursa (job #2037925) | Cod sursa (job #31276) | Cod sursa (job #1426540)
#include <fstream>
#include <array>
#include <vector>
#include <limits>
using namespace std;
std::ifstream in("asmax.in");
std::ofstream out("asmax.out");
const int nmax = 16005;
vector<int> graph[nmax];
array<int, nmax> weight;
array<bool, nmax> visited;
void dfs(int u) {
visited[u] = true;
for (auto v : graph[u])
if (!visited[v]) {
dfs(v);
if (weight[v] > 0)
weight[u] += weight[v];
}
}
int main() {
int n;
in >> n;
for (int i = 1; i <= n; ++i)
in >> weight[i];
for (int i = 1; i < n; ++i) {
int u, v;
in >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(1);
int result = numeric_limits<int>::min();
for (int i = 1; i <= n; ++i)
result = max(result, weight[i]);
out << result << '\n';
return 0;
}