Pagini recente » Cod sursa (job #3130723) | Cod sursa (job #970363) | Cod sursa (job #2385572) | Cod sursa (job #2806141) | Cod sursa (job #1908288)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
using VI = vector<int>;
using VB = vector<bool>;
using VVI = vector<VI>;
VVI G;
vector<bool> s;
VI smax; // smax[x] = suma maxima a unui subarbore care contine si nodul x, continut in subarborele cu rad in x
VI v; // valorile asociate nodurilor
int n;
void ReadArb();
void Df(int x);
int main()
{
ReadArb();
Df(1);
int Smax = 0;
for (int i = 1; i <= n; ++i)
Smax = max(Smax, smax[i]);
fout << Smax;
fin.close();
fout.close();
}
void Df(int x)
{
s[x] = true;
smax[x] = v[x];
for (const int& y : G[x])
{
if (s[y]) continue;
Df(y);
if (smax[y] > 0 )
smax[x] += smax[y];
}
}
void ReadArb()
{
fin >> n;
v = smax = VI(n + 1);
G = VVI(n + 1);
s = VB(n + 1);
for (int i = 1; i <= n; ++i)
fin >> v[i];
int x, y;
for (int i = 1; i < n; ++i)
{
fin >> x >> y;
G[x].push_back(y),
G[y].push_back(x);
}
}