stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<iostream>

#include<stdlib.h>

using namespace std;

int main()

{

    int n;

    cin>>n;

    int ar[n+2];

    long long sum=0;

    ar[0]=0;

    ar[n+1]=0;

    for(int i=1;i<=n;i++)

    {

        cin>>ar[i];

        sum+=abs(ar[i]-ar[i-1]);

    }

    sum+=abs(0-ar[n]);

    //cout<<sum<<endl;

    long long res=0;

    long long res1=0;

    long long tem=sum;

    for(int j=1;j<=n;j++)

    {

        sum=tem;

        res+=abs(ar[j]-ar[j-1])+abs(ar[j+1]-ar[j]);

        //cout<<"abs(ar[j]-ar[j-1])="<<abs(ar[j]-ar[j-1])<<"   abs(ar[j+1]-ar[j])="<<abs(ar[j+1]-ar[j])<<endl;

        res1+=abs(ar[j+1]-ar[j-1]);

        //cout<<"res="<<res<<" res1="<<res1<<endl;

        sum-=res;

        sum+=res1;



        cout<<sum<<endl;

        sum=0;

        res=0;

        res1=0;

    }







    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

#define MAXN int(1e5)

int n,tot;

int poi[MAXN+5];

int main()

{

    //freopen("in.txt","r",stdin);

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&poi[i]),tot+=abs(poi[i-1]-poi[i]);

    tot+=abs(poi[n]);

    for(int i=1;i<=n;i++)

    {

        int ans=tot;

        if(i==n)

            ans=ans-abs(poi[n])-abs(poi[n-1]-poi[n])+abs(poi[n-1]);

        else

            ans=ans-abs(poi[i-1]-poi[i])-abs(poi[i]-poi[i+1])+abs(poi[i-1]-poi[i+1]);

        printf("%d\n",ans);

    }

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):


#include<bits/stdc++.h>

#define optimize  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define ll long long int

#define pb push_back



using namespace std;



int main()

{

    optimize;



   ll s=0;

   int n;



   cin>>n;



   int a[n];

   cin>>a[0];

   s = abs(0-a[0]);

   for(int i = 1;i<n;i++){

      cin>>a[i];



      s = s + abs(a[i-1]-a[i]);



   }



   s = s + abs(0-a[n-1]);

   ll ss = s;

   for(int i = 0;i<n;i++){

    ss  = s;

    if(i==0){

        ss = ss - abs(0-a[i]) - abs(a[i]-a[i+1]);

        ss = ss + abs(0 - a[i+1]);

        cout<<ss<<endl;



    }

    else if(i==n-1){

        ss = ss - abs(a[i-1]-a[i]) - abs(a[i]-0);

        ss = ss + abs(a[i-1]-0);

        cout<<ss<<endl;

    }

    else{

        ss = ss - abs(a[i-1]-a[i]) - abs(a[i]-a[i+1]);

        ss = ss+abs(a[i-1]-a[i+1]);

        cout<<ss<<endl;

    }

   }



}




// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

#define MAXN int(1e5)

int n,tot;

int poi[MAXN+5];

int main()

{

    //freopen("in.txt","r",stdin);

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&poi[i]),tot+=abs(poi[i-1]-poi[i]);

    tot+=abs(poi[n]);

    for(int i=1;i<=n;i++)

    {

        int ans=tot;

        if(i==n)

            ans=ans-abs(poi[n])-abs(poi[n-1]-poi[n])+abs(poi[n-1]);

        else

            ans=ans-abs(poi[i-1]-poi[i])-abs(poi[i]-poi[i+1])+abs(poi[i-1]-poi[i+1]);

        printf("%d\n",ans);

    }

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define ll long long

using namespace std;



const int M = 1e5 + 7;

int a[M];



int main()

{

    int n;

    scanf("%d", &n);

    for(int i = 1; i <= n; i++){

        scanf("%d",a + i);

    }



    ll s = 0;

    for(int i = 0; i < n; i++){

        s += abs(a[i + 1] - a[i]);

        //cout << s << endl;

    }



    s += abs(a[n]);



    //cout << s << endl;



    for(int i = 2; i <= n; i++){

        ll sub = 0, add = 0;

        sub += abs(a[i - 1] - a[i - 2]);

        sub += abs(a[i] - a[i - 1]);

        add += abs(a[i - 2] - a[i]);

        printf("%lld\n", s - sub + add);

    }

    //cout << " s = "<< s << endl;

    ll sub = 0, add = 0;

    sub += abs(a[n - 1] - a[n]);

    sub += abs(a[n]);

    add += abs(a[n - 1]);



    //cout << sub << ' ' << add << endl;



    printf("%lld\n",s - sub + add);



    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

#define MAXN int(1e5)

int n,tot;

int poi[MAXN+5];

int main()

{

    //freopen("in.txt","r",stdin);

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&poi[i]),tot+=abs(poi[i-1]-poi[i]);

    tot+=abs(poi[n]);

    for(int i=1;i<=n;i++)

    {

        int ans=tot;

        if(i==n)

            ans=ans-abs(poi[n])-abs(poi[n-1]-poi[n])+abs(poi[n-1]);

        else

            ans=ans-abs(poi[i-1]-poi[i])-abs(poi[i]-poi[i+1])+abs(poi[i-1]-poi[i+1]);

        printf("%d\n",ans);

    }

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

using namespace std;



int main() {

	int n;

	int v[100010] = { 0 };

	cin >> n;

	int s[100010];

	int t[100010] = { 0 };

	int sum = 0;

	for (int i = 1;i <= n;i++) {

		cin >> v[i];

		s[i - 1] = abs(v[i] - v[i - 1]);

		if (i > 1)t[i - 1] = abs(v[i] - v[i - 2]);

		sum += s[i - 1];

	}

	s[n] = abs(0 - v[n]);

	sum += s[n];

	t[n] = abs(0 - v[n - 1]);

	for (int i = 0;i < n;i++) {

		cout << sum - (s[i] + s[i + 1]) + t[i + 1] << endl;

	}

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int mod = 1e9 + 7;

const int inf = (1 << 30) - 1;

const ll infll = (1LL << 61) - 1;



#include <bits/stdc++.h>

using namespace std;



string to_string(string s)

{

    return '"' + s + '"';

}



string to_string(const char *s)

{

    return to_string((string)s);

}



string to_string(bool b)

{

    return (b ? "true" : "false");

}



template <typename A, typename B>

string to_string(pair<A, B> p)

{

    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";

}



template <typename A>

string to_string(A v)

{

    bool first = true;

    string res = "{";

    for (const auto &x : v)

    {

        if (!first)

        {

            res += ", ";

        }

        first = false;

        res += to_string(x);

    }

    res += "}";

    return res;

}



void debug_out() { cerr << endl; }



template <typename Head, typename... Tail>

void debug_out(Head H, Tail... T)

{

    cerr << " " << to_string(H);

    debug_out(T...);

}



ll N, ans = infll, sum;



int main()

{

    cin >> N;

    vector<ll> A(N + 2), cnt;

    A[0] = 0;

    for (int i = 1; i <= N; i++)

    {

        cin >> A[i];

        sum += abs(A[i] - A[i -1]);

    }

    sum += abs(A[N + 1] - A[N]);

    A[N + 1] = 0;

    cnt.push_back(A[0]);

    for (int i = 1; i <= N; i++)

    {

        cout << sum - abs(A[i - 1] - A[i]) - abs(A[i + 1] - A[i]) + abs(A[i - 1] - A[i + 1]) << endl;

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;



#define justinlaiorz ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

const int N = 1e5 + 2;

int n, a[N], pre[N], suf[N];



inline int abs(int a){

	return a < 0 ? -a : a;

}

int main(){

	justinlaiorz

	cin >> n;

	for(int i = 1; i <= n; ++i) cin >> a[i];

	for(int i = 1; i <= n; ++i) pre[i] = pre[i-1] + abs(a[i] - a[i-1]);

	for(int i = n; i >= 1; --i) suf[i] = suf[i+1] + abs(a[i] - a[i+1]);

	for(int i = 1; i <= n; ++i){

		cout << pre[i-1] + suf[i+1] + abs(a[i-1] - a[i+1]) << '\n';

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

int n, a[100002], pre[100002], suf[100002];

int abs(int a){ return a < 0 ? -a : a;}

int main(){

	scanf("%d", &n);

	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), pre[i] = pre[i-1] + abs(a[i] - a[i-1]);

	for(int i = n; i >= 1; --i) suf[i] = suf[i+1] + abs(a[i] - a[i+1]);

	for(int i = 1; i <= n; ++i) printf("%d\n", pre[i-1] + suf[i+1] + abs(a[i-1] - a[i+1]));

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 2e5 + 10;

int n, c[N], cnt[N], siz[N];

ll cal(int x) {

	return 1ll * x*(x - 1) / 2 + x;

}

ll ans[N];

vector<int>G[N];

	void dfs(int v, int fa) {

		int tmp = cnt[c[v]];

		siz[v] = 1;

		for(int u:G[v]) {

			if(u==fa) continue;

			cnt[c[v]] = 0;

			dfs(u, v);

			siz[v] += siz[u];

			ans[c[v]]+=cal(siz[u]-cnt[c[v]]);

		}

		cnt[c[v]] = tmp + siz[v];

	}

int main() {

	scanf("%d", &n);

	for (int i = 1; i <= n; i++)scanf("%d", &c[i]);

	for (int i = 1; i < n; i++) {

		int u, v;

		scanf("%d%d", &u, &v);

		G[u].push_back(v);

		G[v].push_back(u);

	}

	dfs(1, 0);

	for (int i = 1; i <= n; i++) {

		printf("%lld\n", cal(n) - ans[i] - cal(n - cnt[i]));

	}

	return 0;

}


// Optimized solution to the same problem (faster):


#include<iostream>

#include<vector>

using namespace std;

#define LL	long long

const int MAXN = 2e5+10;



int N;

int c[MAXN];

//vector<int> G[MAXN];

int tot = 0;

int head[MAXN], Next[2*MAXN], to[2*MAXN];



// sub[i]: 顶点i下子树顶点的个数

int sub[MAXN];

// cnt[i]: 颜色为i的顶点下子树顶点的个数

int cnt[MAXN]; 

// ans[i]: 颜色为i的顶点下不经过颜色i的顶点数

LL ans[MAXN];



// 一开始起名count结果和c++ count()冲突，调试了半天, md

inline LL cal(int n) {

	// C(n,2) + n

	return 1LL*n*(n+1)/2;

}



void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int i=head[v]; i; i=Next[i]) {

		int u = to[i];

		if(u == fa) continue;

		cnt[c[v]] = 0;	// 置0重新计数

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]] += cal(sub[u] - cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

inline void add(int a, int b) {

	++tot;

	Next[tot] = head[a];

	to[tot] = b;

	head[a] = tot;

}



/*

void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int u:G[v]) {

		if(u==fa) continue;

		cnt[c[v]] = 0;

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]]+=cal(sub[u]-cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

* */



int main() {

	std::ios::sync_with_stdio(false);

	cin>>N;

	for(int i=1; i<=N; i++) {

		cin >> c[i];

	}

	int a, b;

	for(int i=1; i<N; i++) {

		cin>>a>>b;

	

		add(a, b);

		add(b, a);

		

		//G[a].push_back(b);

		//G[b].push_back(a);

	}



	//dfs1(1, 0);

	dfs(1, 0);

	for(int i=1; i<=N; i++) {

		cout << cal(N) - ans[i] - cal(N-cnt[i]) << endl;

	}

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 2e5 + 10;

int n, c[N], cnt[N], siz[N];

ll cal(int x) {

	return 1ll * x*(x+1)/2;

}

ll ans[N];

vector<int>G[N];

	void dfs(int v, int fa) {

		int tmp = cnt[c[v]];

		siz[v] = 1;

		for(int u:G[v]) {

			if(u==fa) continue;

			cnt[c[v]] = 0;

			dfs(u, v);

			siz[v] += siz[u];

			ans[c[v]]+=cal(siz[u]-cnt[c[v]]);

		}

		cnt[c[v]] = tmp + siz[v];

	}

int main() {

	scanf("%d", &n);

	for (int i = 1; i <= n; i++)scanf("%d", &c[i]);

	for (int i = 1; i < n; i++) {

		int u, v;

		scanf("%d%d", &u, &v);

		G[u].push_back(v);

		G[v].push_back(u);

	}

	dfs(1, 0);

	for (int i = 1; i <= n; i++) {

		printf("%lld\n", cal(n) - ans[i] - cal(n - cnt[i]));

	}

	return 0;

}


// Optimized solution to the same problem (faster):


#include<iostream>

#include<vector>

using namespace std;

#define LL	long long

const int MAXN = 2e5+10;



int N;

int c[MAXN];

//vector<int> G[MAXN];

int tot = 0;

int head[MAXN], Next[2*MAXN], to[2*MAXN];



// sub[i]: 顶点i下子树顶点的个数

int sub[MAXN];

// cnt[i]: 颜色为i的顶点下子树顶点的个数

int cnt[MAXN]; 

// ans[i]: 颜色为i的顶点下不经过颜色i的顶点数

LL ans[MAXN];



// 一开始起名count结果和c++ count()冲突，调试了半天, md

inline LL cal(int n) {

	// C(n,2) + n

	return 1LL*n*(n+1)/2;

}



void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int i=head[v]; i; i=Next[i]) {

		int u = to[i];

		if(u == fa) continue;

		cnt[c[v]] = 0;	// 置0重新计数

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]] += cal(sub[u] - cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

inline void add(int a, int b) {

	++tot;

	Next[tot] = head[a];

	to[tot] = b;

	head[a] = tot;

}



/*

void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int u:G[v]) {

		if(u==fa) continue;

		cnt[c[v]] = 0;

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]]+=cal(sub[u]-cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

* */



int main() {

	std::ios::sync_with_stdio(false);

	cin>>N;

	for(int i=1; i<=N; i++) {

		cin >> c[i];

	}

	int a, b;

	for(int i=1; i<N; i++) {

		cin>>a>>b;

	

		add(a, b);

		add(b, a);

		

		//G[a].push_back(b);

		//G[b].push_back(a);

	}



	//dfs1(1, 0);

	dfs(1, 0);

	for(int i=1; i<=N; i++) {

		cout << cal(N) - ans[i] - cal(N-cnt[i]) << endl;

	}

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 2e5 + 10;

int n, c[N], cnt[N], siz[N];

inline ll cal(int x) {

	return 1ll * x*(x+1)/2;

}

ll ans[N];

vector<int>G[N];

	void dfs(int v, int fa) {

		int tmp = cnt[c[v]];

		siz[v] = 1;

		for(int u:G[v]) {

			if(u==fa) continue;

			cnt[c[v]] = 0;

			dfs(u, v);

			siz[v] += siz[u];

			ans[c[v]]+=cal(siz[u]-cnt[c[v]]);

		}

		cnt[c[v]] = tmp + siz[v];

	}

int main() {

	scanf("%d", &n);

	for (int i = 1; i <= n; i++)scanf("%d", &c[i]);

	for (int i = 1; i < n; i++) {

		int u, v;

		scanf("%d%d", &u, &v);

		G[u].push_back(v);

		G[v].push_back(u);

	}

	dfs(1, 0);

	for (int i = 1; i <= n; i++) {

		printf("%lld\n", cal(n) - ans[i] - cal(n - cnt[i]));

	}

	return 0;

}


// Optimized solution to the same problem (faster):


#include<iostream>

#include<vector>

using namespace std;

#define LL	long long

const int MAXN = 2e5+10;



int N;

int c[MAXN];

//vector<int> G[MAXN];

int tot = 0;

int head[MAXN], Next[2*MAXN], to[2*MAXN];



// sub[i]: 顶点i下子树顶点的个数

int sub[MAXN];

// cnt[i]: 颜色为i的顶点下子树顶点的个数

int cnt[MAXN]; 

// ans[i]: 颜色为i的顶点下不经过颜色i的顶点数

LL ans[MAXN];



// 一开始起名count结果和c++ count()冲突，调试了半天, md

inline LL cal(int n) {

	// C(n,2) + n

	return 1LL*n*(n+1)/2;

}



void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int i=head[v]; i; i=Next[i]) {

		int u = to[i];

		if(u == fa) continue;

		cnt[c[v]] = 0;	// 置0重新计数

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]] += cal(sub[u] - cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

inline void add(int a, int b) {

	++tot;

	Next[tot] = head[a];

	to[tot] = b;

	head[a] = tot;

}



/*

void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int u:G[v]) {

		if(u==fa) continue;

		cnt[c[v]] = 0;

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]]+=cal(sub[u]-cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

* */



int main() {

	std::ios::sync_with_stdio(false);

	cin>>N;

	for(int i=1; i<=N; i++) {

		cin >> c[i];

	}

	int a, b;

	for(int i=1; i<N; i++) {

		cin>>a>>b;

	

		add(a, b);

		add(b, a);

		

		//G[a].push_back(b);

		//G[b].push_back(a);

	}



	//dfs1(1, 0);

	dfs(1, 0);

	for(int i=1; i<=N; i++) {

		cout << cal(N) - ans[i] - cal(N-cnt[i]) << endl;

	}

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define LL long long

const int N = 2e5 + 10;

int n, c[N], cnt[N], siz[N];

	inline LL cal(int n) {

		// C(n,2) + n

		return 1LL*n*(n+1)/2;

	}

LL ans[N];

vector<int>G[N];

	void dfs(int v, int fa) {

		int tmp = cnt[c[v]];

		siz[v] = 1;

		for(int u:G[v]) {

			if(u==fa) continue;

			cnt[c[v]] = 0;

			dfs(u, v);

			siz[v] += siz[u];

			ans[c[v]]+=cal(siz[u]-cnt[c[v]]);

		}

		cnt[c[v]] = tmp + siz[v];

	}

int main() {

	//scanf("%d", &n);

	std::ios::sync_with_stdio(false);

	cin>>n;

	for (int i = 1; i <= n; i++)

		//scanf("%d", &c[i]);

		cin>>c[i];

	for (int i = 1; i < n; i++) {

		int u, v;

		//scanf("%d%d", &u, &v);

		cin>>u>>v;

		G[u].push_back(v);

		G[v].push_back(u);

	}

	dfs(1, 0);

	for (int i = 1; i <= n; i++) {

		cout<< cal(n) - ans[i] - cal(n - cnt[i])<<endl;

	}

	return 0;

}


// Optimized solution to the same problem (faster):


#include<iostream>

#include<vector>

using namespace std;

#define LL	long long

const int MAXN = 2e5+10;



int N;

int c[MAXN];

//vector<int> G[MAXN];

int tot = 0;

int head[MAXN], Next[2*MAXN], to[2*MAXN];



// sub[i]: 顶点i下子树顶点的个数

int sub[MAXN];

// cnt[i]: 颜色为i的顶点下子树顶点的个数

int cnt[MAXN]; 

// ans[i]: 颜色为i的顶点下不经过颜色i的顶点数

LL ans[MAXN];



// 一开始起名count结果和c++ count()冲突，调试了半天, md

inline LL cal(int n) {

	// C(n,2) + n

	return 1LL*n*(n+1)/2;

}



void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int i=head[v]; i; i=Next[i]) {

		int u = to[i];

		if(u == fa) continue;

		cnt[c[v]] = 0;	// 置0重新计数

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]] += cal(sub[u] - cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

inline void add(int a, int b) {

	++tot;

	Next[tot] = head[a];

	to[tot] = b;

	head[a] = tot;

}



/*

void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int u:G[v]) {

		if(u==fa) continue;

		cnt[c[v]] = 0;

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]]+=cal(sub[u]-cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

* */



int main() {

	std::ios::sync_with_stdio(false);

	cin>>N;

	for(int i=1; i<=N; i++) {

		cin >> c[i];

	}

	int a, b;

	for(int i=1; i<N; i++) {

		cin>>a>>b;

	

		add(a, b);

		add(b, a);

		

		//G[a].push_back(b);

		//G[b].push_back(a);

	}



	//dfs1(1, 0);

	dfs(1, 0);

	for(int i=1; i<=N; i++) {

		cout << cal(N) - ans[i] - cal(N-cnt[i]) << endl;

	}

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
	#include<iostream>

	#include<vector>

	using namespace std;

	#define LL	long long

	const int MAXN = 2e5+10;



	int N;

	int c[MAXN];

	vector<int> G[MAXN];

//	int tot = 0;

//	int head[MAXN], Next[MAXN], to[MAXN];



	// sub[i]: 顶点i下子树顶点的个数

	int sub[MAXN];

	// cnt[i]: 颜色为i的顶点下子树顶点的个数

	int cnt[MAXN]; 

	// ans[i]: 颜色为i的顶点下不经过颜色i的顶点数

	LL ans[MAXN];

	

	// 一开始起名count结果和c++ count()冲突，调试了半天, md

	inline LL cal(int n) {

		// C(n,2) + n

		return 1LL*n*(n+1)/2;

	}

	/*

	void dfs(int v, int fa) {

		int tmp = cnt[c[v]];

		sub[v] = 1;

		for(int i=head[v]; i; i=Next[i]) {

			int u = to[i];

			if(u == fa) continue;

			cnt[c[v]] = 0;	// 置0重新计数

			dfs(u, v);

			sub[v] += sub[u];

			ans[c[v]] += count(sub[u] - cnt[c[v]]);

		}

		cnt[c[v]] = tmp + sub[v];

	}

	void add(int a, int b) {

		++tot;

		Next[tot] = head[a];

		to[tot] = b;

		head[a] = tot;

	}

	*/

	void dfs(int v, int fa) {

		int tmp = cnt[c[v]];

		sub[v] = 1;

		for(int u:G[v]) {

			if(u==fa) continue;

			cnt[c[v]] = 0;

			dfs(u, v);

			sub[v] += sub[u];

			ans[c[v]]+=cal(sub[u]-cnt[c[v]]);

		}

		cnt[c[v]] = tmp + sub[v];

	}

	

	int main() {

		std::ios::sync_with_stdio(false);

		cin>>N;

		for(int i=1; i<=N; i++) {

			cin >> c[i];

		}

		int a, b;

		for(int i=1; i<N; i++) {

			cin>>a>>b;

			G[a].push_back(b);

			G[b].push_back(a);

		}

		//dfs1(1, 0);

		dfs(1, 0);

		for(int i=1; i<=N; i++) {

			cout << cal(N) - ans[i] - cal(N-cnt[i]) << endl;

		}

		return 0;

	}


// Optimized solution to the same problem (faster):


#include<iostream>

#include<vector>

using namespace std;

#define LL	long long

const int MAXN = 2e5+10;



int N;

int c[MAXN];

//vector<int> G[MAXN];

int tot = 0;

int head[MAXN], Next[2*MAXN], to[2*MAXN];



// sub[i]: 顶点i下子树顶点的个数

int sub[MAXN];

// cnt[i]: 颜色为i的顶点下子树顶点的个数

int cnt[MAXN]; 

// ans[i]: 颜色为i的顶点下不经过颜色i的顶点数

LL ans[MAXN];



// 一开始起名count结果和c++ count()冲突，调试了半天, md

inline LL cal(int n) {

	// C(n,2) + n

	return 1LL*n*(n+1)/2;

}



void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int i=head[v]; i; i=Next[i]) {

		int u = to[i];

		if(u == fa) continue;

		cnt[c[v]] = 0;	// 置0重新计数

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]] += cal(sub[u] - cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

inline void add(int a, int b) {

	++tot;

	Next[tot] = head[a];

	to[tot] = b;

	head[a] = tot;

}



/*

void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int u:G[v]) {

		if(u==fa) continue;

		cnt[c[v]] = 0;

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]]+=cal(sub[u]-cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

* */



int main() {

	std::ios::sync_with_stdio(false);

	cin>>N;

	for(int i=1; i<=N; i++) {

		cin >> c[i];

	}

	int a, b;

	for(int i=1; i<N; i++) {

		cin>>a>>b;

	

		add(a, b);

		add(b, a);

		

		//G[a].push_back(b);

		//G[b].push_back(a);

	}



	//dfs1(1, 0);

	dfs(1, 0);

	for(int i=1; i<=N; i++) {

		cout << cal(N) - ans[i] - cal(N-cnt[i]) << endl;

	}

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define LL long long

const int N = 2e5 + 10;

int n, c[N], cnt[N], siz[N];

	inline LL cal(int n) {

		// C(n,2) + n

		return 1LL*n*(n+1)/2;

	}

LL ans[N];

vector<int>G[N];

	void dfs(int v, int fa) {

		int tmp = cnt[c[v]];

		siz[v] = 1;

		for(int u:G[v]) {

			if(u==fa) continue;

			cnt[c[v]] = 0;

			dfs(u, v);

			siz[v] += siz[u];

			ans[c[v]]+=cal(siz[u]-cnt[c[v]]);

		}

		cnt[c[v]] = tmp + siz[v];

	}

	

	inline void add(int a, int b) {

		G[a].push_back(b);

		G[b].push_back(a);

	}

	

int main() {

	//scanf("%d", &n);

	std::ios::sync_with_stdio(false);

	cin>>n;

	for (int i = 1; i <= n; i++)

		//scanf("%d", &c[i]);

		cin>>c[i];

	for (int i = 1; i < n; i++) {

		int u, v;

		//scanf("%d%d", &u, &v);

		cin>>u>>v;

		G[u].push_back(v);

		G[v].push_back(u);

		//add(u, v); add(v, u);

	}

	dfs(1, 0);

	for (int i = 1; i <= n; i++) {

		cout<< cal(n) - ans[i] - cal(n - cnt[i])<<endl;

	}

	return 0;

}


// Optimized solution to the same problem (faster):


#include<iostream>

#include<vector>

using namespace std;

#define LL	long long

const int MAXN = 2e5+10;



int N;

int c[MAXN];

//vector<int> G[MAXN];

int tot = 0;

int head[MAXN], Next[2*MAXN], to[2*MAXN];



// sub[i]: 顶点i下子树顶点的个数

int sub[MAXN];

// cnt[i]: 颜色为i的顶点下子树顶点的个数

int cnt[MAXN]; 

// ans[i]: 颜色为i的顶点下不经过颜色i的顶点数

LL ans[MAXN];



// 一开始起名count结果和c++ count()冲突，调试了半天, md

inline LL cal(int n) {

	// C(n,2) + n

	return 1LL*n*(n+1)/2;

}



void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int i=head[v]; i; i=Next[i]) {

		int u = to[i];

		if(u == fa) continue;

		cnt[c[v]] = 0;	// 置0重新计数

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]] += cal(sub[u] - cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

inline void add(int a, int b) {

	++tot;

	Next[tot] = head[a];

	to[tot] = b;

	head[a] = tot;

}



/*

void dfs(int v, int fa) {

	int tmp = cnt[c[v]];

	sub[v] = 1;

	for(int u:G[v]) {

		if(u==fa) continue;

		cnt[c[v]] = 0;

		dfs(u, v);

		sub[v] += sub[u];

		ans[c[v]]+=cal(sub[u]-cnt[c[v]]);

	}

	cnt[c[v]] = tmp + sub[v];

}

* */



int main() {

	std::ios::sync_with_stdio(false);

	cin>>N;

	for(int i=1; i<=N; i++) {

		cin >> c[i];

	}

	int a, b;

	for(int i=1; i<N; i++) {

		cin>>a>>b;

	

		add(a, b);

		add(b, a);

		

		//G[a].push_back(b);

		//G[b].push_back(a);

	}



	//dfs1(1, 0);

	dfs(1, 0);

	for(int i=1; i<=N; i++) {

		cout << cal(N) - ans[i] - cal(N-cnt[i]) << endl;

	}

	return 0;

}


// Fastest known solution for this problem: