Pagini recente » Cod sursa (job #643434) | Cod sursa (job #2336858) | Cod sursa (job #852086) | Cod sursa (job #327546) | Cod sursa (job #1851451)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
vector <int> G[16002];
vector <int> Viz, V, Sum;
int n;
void DFS(int current_node)
{
Viz[current_node] = 1;
Sum[current_node] = V[current_node];
for (const int & x : G[current_node])
if (Viz[x] == 0)
{
DFS(x);
if (Sum[x] > 0)
Sum[current_node] += Sum[x];
}
}
int main()
{
f >> n;
Viz = V = vector<int> (n + 1);
Sum = vector<int>(n + 1, -1e9);
for (int i = 1; i <= n; ++i)
f >> V[i];
for (int i = 1; i < n; ++i)
{
int x, y;
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
DFS(1);
int sum_max = -1e9;
for (int i = 1; i <= n; ++i)
if (Sum[i] > sum_max)
sum_max = Sum[i];
g << sum_max;
f.close();
g.close();
return 0;
}