using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CompLib.Collections;
using CompLib.Util;

public class Program
{
    int N;
    int[] C;
    List<int>[] edges;

    // 含まないグループのサイズ
    List<int>[] Cnt;


    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        C = new int[N];
        for (int i = 0; i < N; i++)
        {
            C[i] = sc.NextInt() - 1;
        }

        edges = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            edges[i] = new List<int>();
        }

        for (int i = 0; i < N - 1; i++)
        {
            int a = sc.NextInt() - 1;
            int b = sc.NextInt() - 1;
            edges[a].Add(b);
            edges[b].Add(a);
        }

        Size = new int[N];
        map = new HashMap<int, int>[N];
        Last = new int[N];
        Cnt = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            Cnt[i] = new List<int>();
            map[i] = new HashMap<int, int>();
        }

        DFS(0, -1);

        for (int i = 0; i < N; i++)
        {
            if (C[0] == i) continue;
            Cnt[i].Add(N - map[i][0]);
        }

        long all = (long)(N + 1) * N / 2;
        var sb = new StringBuilder();
        for (int i = 0; i < N; i++)
        {
            long t = 0;
            foreach (var j in Cnt[i])
            {
                t += (long)(j + 1) * j / 2;
            }
            sb.AppendLine((all - t).ToString());
        }
        Console.Write(sb);

    }

    // 部分木iの大きさ
    int[] Size;

    // 色i, 直近の親が色iの祖先j 部分木サイズ
    HashMap<int, int>[] map;

    // 直近の親が色iの祖先
    int[] Last;

    void DFS(int i, int p)
    {
        Size[i] = 1;
        int t = -1;
        if (p != -1)
        {
            t = Last[C[p]];
            Last[C[p]] = i;
        }
        foreach (var to in edges[i])
        {
            if (p == to) continue;
            DFS(to, i);
            Size[i] += Size[to];
        }

        // 現在地の色i
        map[C[i]][Last[C[i]]] += Size[i];

        if (p != -1)
        {
            Cnt[C[p]].Add(Size[i] - map[C[p]][i]);
        }


        if (p != -1)
        {
            Last[C[p]] = t;
        }
    }

    public static void Main(string[] args) => new Program().Solve();
}


namespace CompLib.Collections
{
    using System.Collections.Generic;
    public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get
            {
                TValue o;
                return TryGetValue(key, out o) ? o : default(TValue);
            }
            set { base[key] = value; }
        }
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


Measured memory_bytes: 129900