Pagini recente » Cod sursa (job #2252521) | Cod sursa (job #874598) | Cod sursa (job #2173631) | Cod sursa (job #1550632) | Cod sursa (job #2824494)
#include <iostream>
#include <vector>
#include <fstream>
#define N 16003
using namespace std;
vector <int> vizitat(N, 0);
vector <vector <int>> listaAd;
int suma[N], cost[N];
void dfs(int nodStart)
{
vizitat[nodStart] = 1;
suma[nodStart] = cost[nodStart];
for (int i = 0; i < listaAd[nodStart].size(); i++)
{
int nodCurent = listaAd[nodStart][i];
if (vizitat[nodCurent] == 0)
{
dfs(nodCurent);
if (suma[nodCurent] + suma[nodStart] > suma[nodStart])
suma[nodStart] = suma[nodStart] + suma[nodCurent];
}
}
}
int main()
{
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n, rez, st, dr;
fin >> n;
listaAd = vector <vector <int>> (n + 1);
for (int i = 1; i <= n; i++)
fin >> cost[i];
for (int i = 1; i <= n - 1; i++) ///arborele are n-1 muchii
{
fin >> st >> dr;
listaAd[st].push_back(dr);
listaAd[dr].push_back(st);
}
dfs(1);
rez = suma[1];
for (int i = 2; i <= n; i++)
if (suma[i] > rez)
rez = suma[i];
fout << rez;
fin.close();
fout.close();
return 0;
}