Pagini recente » Cod sursa (job #487654) | Cod sursa (job #1430325) | Cod sursa (job #2357951) | Cod sursa (job #2839896) | Cod sursa (job #2203007)
#include <fstream>
#include <vector>
using namespace std;
const int N = 16001;
vector <int> a[N];
int pct[N], s[N], mx = -10000000, n;
bool viz[N];
ifstream f("asmax.in");
ofstream g("asmax.out");
void citire()
{
int x, y;
f >> n;
for (int i = 1; i <= n; i++)
f >> pct[i];
for (int i = 1; i < n; i++)
{
f >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
viz[x] = true;
for (int i = 0; i < a[x].size(); i++)
{
int y = a[x][i];
if (viz[y] == false)
{
dfs(y);
if (pct[y] > 0)
pct[x] += pct[y];
}
}
if (mx < pct[x])
mx = pct[x];
}
int main()
{
citire();
for (int j = 1; j <= n; j++)
{
if (viz[j] == false)
dfs(j);
}
g << mx;
f.close();
g.close();
return 0;
}