Pagini recente » Cod sursa (job #464813) | Cod sursa (job #1810042) | Cod sursa (job #230887) | Cod sursa (job #128713) | Cod sursa (job #2939059)
#include <fstream>
#include <vector>
#include <algorithm>
#pragma GCC operator ("O3")
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
struct nnod
{
int nod, lvl;
bool operator <(const nnod& other) const
{
return (lvl>=other.lvl);
}
}nd[16005];
vector <int> v[16005];
int n, tata[16005], dp[16005], vv[16005];
int maxim=-(2e9);
void dfs(int, int, int);
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(0);
int a, b, i;
fin>>n;
for(i=1; i<=n; i++) fin>>vv[i];
for(i=1; i<n; i++)
{
fin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1, 1, 0);
sort(nd+1, nd+n+1);
for(i=1; i<=n; i++)
{
dp[nd[i].nod]+=vv[nd[i].nod];
if(dp[nd[i].nod]>0) dp[tata[nd[i].nod]]+=dp[nd[i].nod];
maxim=max(maxim, dp[nd[i].nod]);
}
fout<<maxim<<'\n';
}
void dfs(int nod, int lvl, int tr)
{
if(nd[nod].lvl==0)
{
tata[nod]=tr;
nd[nod].lvl=lvl;
nd[nod].nod=nod;
for(auto i:v[nod]) dfs(i, lvl+1, nod);
}
}