Pagini recente » Cod sursa (job #1355864) | Cod sursa (job #1253870) | Cod sursa (job #2375122) | Cod sursa (job #1753788) | Cod sursa (job #1426834)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define maxn 16006
vector<int> g[maxn];
int n, m;
int c[maxn], viz[maxn];
void dfs(int x)
{
viz[x] = 1;
for (int i = 0; i < g[x].size(); i++)
if (!viz[g[x][i]])
{
dfs(g[x][i]);
if (c[x] + c[g[x][i]] > c[x]) c[x] +=c[g[x][i]];
}
}
int main()
{
ifstream f("asmax.in");
ofstream e("asmax.out");
f>>n;
int maxim = -1001, imax;
for (int i = 1; i <= n; i++)
{
f>>c[i];
if (c[i] > maxim)
{
maxim = c[i];
imax = i;
}
}
int x, y;
for (int i = 0; i < n; i++)
{
f>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
if (maxim < 0) e<<maxim;
else {
dfs(imax);
e<<c[imax];
}
}