Pagini recente » Istoria paginii utilizator/eduardturtoi | Monitorul de evaluare | Clasament erase_att | Monitorul de evaluare | Cod sursa (job #2250930)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
const int MaxN = 16001;
const int Inf = 0x3f3f3f3f;
int n, x ,y, maxim_s;
int val[MaxN], smax[MaxN];
VVI G;
VB V;
void ReadGraph();
void DFS_And_Calculate(int N);
void WriteSolution();
int main()
{
ReadGraph();
DFS_And_Calculate(1);
WriteSolution();
return 0;
}
void ReadGraph()
{
fin >> n;
for(int i = 1; i <= n; ++i)
fin >> val[i];
G = VVI(n + 1);
V = VB(n + 1);
while(fin >> x >> y)
{
G[x].push_back(y);
G[y].push_back(x);
}
}
void DFS_And_Calculate(int N)
{
smax[N] = val[N];
V[N] = true;
for (const int& t : G[N])
{
if (V[t]) continue;
DFS_And_Calculate(t);
if (smax[t] > 0) smax[N] += smax[t];
}
}
void WriteSolution()
{
maxim_s = -Inf;
for(int i = 1; i <= n; ++i)
if (smax[i] > maxim_s) maxim_s = smax[i];
fout << maxim_s;
}