Pagini recente » Cod sursa (job #3155671) | Cod sursa (job #1691013) | Cod sursa (job #1689682) | Cod sursa (job #980081) | Cod sursa (job #3230486)
#include <fstream>
#include <vector>
using namespace std;
const int N = 16000;
int v[N+1], scor[N+1];
bool viz[N+1];
vector <int> a[N+1];
void dfs(int x)
{
viz[x] = true;
scor[x] = v[x];
for (auto y: a[x])///y va fi pe rand fiecare vecin al lui x
{
if (!viz[y])///y nu este tatal lui x in arborele DFS
{
dfs(y);
if (scor[y] > 0)
{
scor[x] += scor[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);
}
dfs(1);
int scor_max = scor[1];
for (int i = 2; i <= n; i++)
{
scor_max = max(scor_max, scor[i]);
}
out << scor_max << "\n";
in.close();
out.close();
return 0;
}