Pagini recente » Cod sursa (job #2920714) | Cod sursa (job #2268928) | Cod sursa (job #2756418) | Cod sursa (job #155238) | Cod sursa (job #2998536)
#include <fstream>
#include <vector>
using namespace std;
const int N = 16000;
int val[N+1], scor[N+1];
bool viz[N+1];
vector <int> a[N+1];
void dfs(int x)
{
viz[x] = true;
scor[x] = val[x];
for (auto y: a[x])
{
if (!viz[y])///y este fiu al 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 >> val[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;
in.close();
out.close();
return 0;
}