Pagini recente » Cod sursa (job #461936) | template/ixia | Istoria paginii autumn-warmup-2007/clasament/runda-2 | Cod sursa (job #165058) | Cod sursa (job #2012311)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
using VI = vector<int>;
vector<bool> v;
VI smax, val;
vector<VI> G;
int n;
void ReadGraph();
void Df(int x);
int main()
{
ReadGraph();
Df(1);
int res = -1001;
for (int i = 1; i <= n; ++i)
res = max(res, smax[i]);
fout << res;
fin.close();
fout.close();
}
void Df(int x)
{
v[x] = true;
smax[x] = val[x];
for (const int& y : G[x])
{
if (v[y]) continue;
Df(y);
if (smax[y] > 0)
smax[x] += smax[y];
}
}
void ReadGraph()
{
fin >> n;
v = vector<bool>(n + 1);
val = VI(n + 1);
smax = VI(n + 1);
G = vector<VI>(n + 1);
for (int i = 1; i <= n; ++i)
fin >> val[i];
int x, y;
for (int i = 1; i < n; ++i)
{
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}