Pagini recente » Istoria paginii utilizator/victor10 | Siruri3 | Diferente pentru problema/politic intre reviziile 5 si 12 | Istoria paginii utilizator/bubi | Cod sursa (job #1443670)
#include <iostream>
#include <vector>
#include <fstream>
#define INF 2000000000
using namespace std;
int n, sol;
int cost[16010], best[16010];
vector <int> G[16010];
bool viz[16010];
/// best[i] = costul maxim al unui subarbore cu radacina in nodul i
/// consider ca arborele are radacina in nodul 1
inline void Read()
{
ifstream f ("asmax.in");
f>>n;
int i, x, y;
for (i=1; i<=n; i++)
f>>cost[i];
for (i=1; i<n; i++)
{
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
f.close();
}
inline void DFS(int nod)
{
best[nod] = cost[nod];
viz[nod] = true;
vector <int>::iterator it;
for (it = G[nod].begin(); it!=G[nod].end(); it++)
{
if (viz[*it] == false)
{
DFS(*it);
best[nod] = max(best[nod], best[nod] + best[*it]);
}
}
}
inline void Solve()
{
DFS(1);
sol = -INF;
for (int i = 1; i<=n; i++)
sol = max (sol, best[i]);
}
inline void Write()
{
ofstream g("asmax.out");
g<<sol<<"\n";
g.close();
}
int main()
{
Read();
Solve();
Write();
return 0;
}