Pagini recente » Cod sursa (job #2791309) | Cod sursa (job #1473529) | Cod sursa (job #1296041) | Cod sursa (job #1421880) | Cod sursa (job #3339101)
#include <fstream>
#include <vector>
using namespace std;
const int N = 16000;
vector <int> a[N+1];
int v[N+1], scor_max[N+1];
bool viz[N+1];
void dfs(int x)
{
viz[x] = true;
scor_max[x] = v[x];
for (auto y: a[x])
{
if (!viz[y])
{
dfs(y);
if (scor_max[y] > 0)
{
scor_max[x] += scor_max[y];
}
}
}
}
int main()
{
ifstream in("asmax.in");
ofstream out("asmax.out");
int n;
in >> n;
for (int i = 1; i <= n; i++)
{
in >> v[i];
}
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);
int maxim = scor_max[1];
for (int i = 2; i <= n; i++)
{
maxim = max(maxim, scor_max[i]);
}
out << maxim << "\n";
out.close();
return 0;
}