Pagini recente » Monitorul de evaluare | Profil Nereid | Cod sursa (job #2300914) | Diferente pentru utilizator/stay_awake77 intre reviziile 49 si 48 | Cod sursa (job #2990840)
#include <fstream>
#include <vector>
using namespace std;
const int N = 16001;
ifstream in("asmax.in");
ofstream out("asmax.out");
bool fq[N];
int scor[N], val[N], n;
vector <int> a[N];
void dfs(int x)
{
fq[x] = true;
scor[x] = val[x];
for (auto i : a[x])
{
if (!fq[i])
{
dfs(i);
if (scor[i] > 0)
scor[x] += scor[i];
}
}
}
int main()
{
in >> n;
for (int i = 1; i <= n; i++)
in >> val[i];
for (int i = 1; i <= n; i++)
{
int ns, nd;
in >> ns >> nd;
a[ns].push_back(nd);
a[nd].push_back(ns);
}
dfs(1);
int maxi = scor[1];
for (int i = 1; i <= n; i++)
maxi = max(maxi, scor[i]);
out << maxi;
return 0;
}