Pagini recente » Cod sursa (job #2559642) | Cod sursa (job #1231890) | Cod sursa (job #2111860) | Cod sursa (job #564266) | Cod sursa (job #1345316)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int NMax = 16005;
const int oo = -(1<<30);
vector < int > V[NMax];
int N, Values[NMax], Sum[NMax], Result;
bool beenThere[NMax];
void DFS(int node)
{
beenThere[node] = true;
Sum[node] = Values[node];
for(unsigned int i = 0; i < V[node].size(); i++)
{
int Neighbor = V[node][i];
if(!beenThere[Neighbor])
{
DFS(Neighbor);
Sum[node] = max(Sum[node], Sum[node] + Sum[Neighbor]);
}
}
Result = max(Result, Sum[node]);
}
void Read()
{
int N;
fin >> N;
for(int i = 1; i <= N; i++)
fin >> Values[i];
for(int i = 1; i < N; i++)
{
int x, y;
fin >> x >> y;
V[x].push_back(y);
V[y].push_back(x);
}
DFS(1);
fout << Result;
}
int main()
{
Read();
return 0;
}