Pagini recente » Cod sursa (job #357924) | Cod sursa (job #2029579) | Cod sursa (job #421181) | Cod sursa (job #2210995) | Cod sursa (job #1892664)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
using VI = vector<int>;
using VVI = vector<VI>;
int n;
VVI G;
VI smax;
VI v;
VI viz;
void Read();
void Df(int x);
int main()
{
Read();
Df(1);
int Smax = 0;
for (const int& val : smax)
Smax = max(Smax, val);
fout << Smax;
fin.close();
fout.close();
}
void Df(int x)
{
viz[x] = true;
smax[x] = v[x];
for (const int& y : G[x])
{
if (viz[y]) continue;
Df(y);
if (smax[y] > 0)
smax[x] += smax[y];
}
}
void Read()
{
int x, y;
fin >> n;
G = VVI(n + 1);
smax = v = viz = VI(n + 1);
for (int i = 1; i <= n; ++i)
fin >> v[i];
while (fin >> x >> y)
G[x].push_back(y),
G[y].push_back(x);
}