Pagini recente » Cod sursa (job #2646129) | Cod sursa (job #212034) | Cod sursa (job #2561947) | Cod sursa (job #1316912) | Cod sursa (job #3281005)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
/**
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
*/
int n, v[16003];
vector <int> g[16003];
bitset <16003> viz;
long long cnt, sum, maxx, summax;
void DFS(int x)
{
viz[x] = 1;
maxx = max(maxx, cnt + v[x]);
cnt += v[x];
for (int i : g[x])
if (viz[i] == 0)
DFS(i);
}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i, j, x, y;
fin >> n;
for (i = 1; i <= n; i++)
fin >> v[i];
for (i = 1; i < n; i++)
{
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (j = 1; j <= n; j++)
{
sum = 0;
viz[j] = 1;
for (int i : g[j])
{
cnt = maxx = 0;
DFS(i);
//fout << maxx << " ";
sum += maxx;
}
summax = max(summax, v[j] + sum);
for (i = 1; i <= n; i++)
viz[i] = 0;
}
fout << summax << "\n";
return 0;
}