Pagini recente » Cod sursa (job #3202398) | Cod sursa (job #2420945) | Cod sursa (job #2029495) | Cod sursa (job #3177979) | Cod sursa (job #3281031)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n, val[16002], s;
vector<int> G[16002];
int cnt, t1[16002], t2[16002], smax = -1e9;
bitset<16002> viz;
void DFS(int x)
{
viz[x] = true;
s += val[x];
smax = max(smax, s);
for(int w : G[x])
if(!viz[w]) DFS(w);
}
void F(int x)
{
sp[x] = x;
viz[x] = true;
for(int w : G[x])
if(!viz[w])
{
F(w);
sp[x] += sp[w];
}
}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i, j;
fin >> n;
for(i = 1; i <= n; i++)
fin >> val[i];
for(int t = 1; t < n; t++)
{
fin >> i >> j;
G[i].push_back(j);
G[j].push_back(i);
}
F(1);
for(i = 1; i <= n; i++)
cout << t1[i] << " " << t2[i] << "\n";
for(i = 1; i <= n; i++)
{
viz.reset();
s = 0;
DFS(i);
}
fout << smax;
return 0;
}