Pagini recente » Cod sursa (job #2584850) | Cod sursa (job #1875868)
#include <fstream>
#include <vector>
using namespace std;
struct Node
{
int best_sum;
bool visited = false;
vector<int> edges;
};
typedef vector<Node> Tree;
void Dfs(Tree &t, int node)
{
t[node].visited = true;
for (int neighbour : t[node].edges) {
if (!t[neighbour].visited) {
Dfs(t, neighbour);
t[node].best_sum += max(t[neighbour].best_sum, 0);
}
}
}
int FindRes(Tree &t, int root)
{
Dfs(t, root);
int res = -(1 << 30);
for (const auto &node : t) {
res = max(res, node.best_sum);
}
return res;
}
int main()
{
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n;
fin >> n;
Tree tree(n);
for (int i = 0; i < n; ++i) {
fin >> tree[i].best_sum;
}
for (int i = 1; i < n; ++i) {
int x, y;
fin >> x >> y;
tree[x - 1].edges.push_back(y - 1);
tree[y - 1].edges.push_back(x - 1);
}
fout << FindRes(tree, 0) << "\n";
return 0;
}