Pagini recente » Cod sursa (job #3350980) | Cod sursa (job #3341292) | Cod sursa (job #2812265) | Cod sursa (job #2127156) | Cod sursa (job #3354051)
/*
https://infoarena.ro/problema/asmax
*/
#include <fstream>
#include <vector>
using namespace std;
void dfs(int x, int t, vector <vector <int>> &a, vector <int> &val)
{
for (auto y: a[x])
{
if (y != t)
{
dfs(y, x, a, val);
if (val[y] > 0)
{
val[x] += val[y];
}
}
}
}
int main()
{
ifstream in("asmax.in");
ofstream out("asmax.out");
int n;
in >> n;
vector <int> val(n + 1);
for (int i = 1; i <= n; i++)
{
in >> val[i];
}
vector <vector <int>> a(n + 1);
for (int i = 0; i < n - 1; i++)
{
int x, y;
in >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
in.close();
dfs(1, 0, a, val);
int v_max = val[1];
for (int i = 2; i <= n; i++)
{
v_max = max(v_max, val[i]);
}
out << v_max << "\n";
out.close();
return 0;
}