//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "DFS序+分治": {
        "prefix": "acm-4.3Lomsat gelral",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3;",
            "int o,n,m,x,y,t,tmp,dfn_O,A[N],Q[N],cnt[N],dfn[N],idx[N],size[N],head[N];LL ans,Ans[N];",
            "struct QAQ{int to,next;}a[N<<1];",
            "inline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "inline void dfs(Re x,Re fa){",
            "    idx[dfn[x]=++dfn_O]=x,size[x]=1;",
            "    for(Re i=head[x],to;i;i=a[i].next)",
            "        if((to=a[i].to)!=fa)dfs(to,x),size[x]+=size[to];",
            "}",
            "inline void CL(){",
            "    while(t)cnt[Q[t--]]=0;ans=tmp=0;",
            "}",
            "inline void insert(Re x){",
            "    ++cnt[Q[++t]=A[x]];",
            "    if(cnt[A[x]]>tmp)tmp=cnt[ans=A[x]];",
            "    else if(cnt[A[x]]==tmp)ans+=A[x];",
            "}",
            "inline void sakura(Re L,Re R){",
            "    if(L==R){if(size[idx[L]]==1)Ans[idx[L]]=A[idx[L]];return;}",
            "    Re mid=L+R>>1;",
            "    sakura(L,mid),sakura(mid+1,R);",
            "    Re p=mid;CL();",
            "    for(Re i=mid,j;i>=L&&(j=i+size[idx[i]]-1)<=R;--i){",
            "        insert(idx[i]);",
            "        if(j<=mid)continue;",
            "        while(p<j)insert(idx[++p]);",
            "        Ans[idx[i]]=ans;",
            "    }",
            "}",
            "int main(){",
            "//  freopen(\"123.txt\",\"r\",stdin);",
            "    in(n),m=n-1;",
            "    for(Re i=1;i<=n;++i)in(A[i]);",
            "    while(m--)in(x),in(y),add(x,y),add(y,x);",
            "    dfs(1,0),sakura(1,n);",
            "    for(Re i=1;i<=n;++i)printf(\"%lld \",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 DFS序+分治"
    }
}