# noi.ac #525 神树的权值

mcfx神仙的题qwq

### 题目链接：戳我

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define MAXN 100010
using namespace std;
int n,t;
struct Edge{int nxt,to;}edge[MAXN<<1];
{
}
{
int ans;
int kkk[MAXN];
inline void search(int x,int pre,int maxx)
{
{
int v=edge[i].to;
if(v==pre) continue;
if(a[v]>maxx) kkk[v]=1;
search(v,x,max(maxx,a[v]));
}
}
inline void solve()
{
for(int p=1;p<=n;p++)
{
ans=0;
for(int i=1;i<=n;i++) kkk[i]=0;
kkk[p]=1;
search(p,0,a[p]);
for(int i=1;i<=n;i++)
if(kkk[i])
ans+=i;
printf("%d ",ans);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
}
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define MAXN 300010
using namespace std;
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48); ch=getchar();}
return x*f;
}
int n,t;
long long ans[MAXN],val[MAXN];
vector<pair<int,int> >G[MAXN];
struct Edge{int nxt,to;}edge[MAXN<<1];
{
}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void dfs(int x,int pre)
{
ans[x]=ans[pre]+val[x];
{
int v=edge[i].to;
dfs(v,x);
}
}
inline void solve()
{
for(int i=1;i<n;i++)
{
if(a[x]<a[y]) swap(x,y);
G[a[x]].push_back(make_pair(x,y));
}
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=n;i++)
{
for(int j=0;j<G[i].size();j++)
{
int x=G[i][j].first;
int y=G[i][j].second;
if(a[x]==a[y]) continue;
if(a[x]<a[y]) swap(x,y);
val[find(y)]+=x;
}
for(int j=0;j<G[i].size();j++)
{
int x=G[i][j].first;
int y=G[i][j].second;
if(a[x]<a[y]) swap(x,y);
int xx=find(x),yy=find(y);
fa[yy]=xx;
}
}
int root=find(1);
dfs(root,0);
for(int i=1;i<=n;i++) printf("%lld ",ans[i]+i);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif