import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*

fun main() {
    val jin = BufferedReader(InputStreamReader(System.`in`))
    val n = jin.readLine().toInt()
    var tokenizer = StringTokenizer(jin.readLine())
    val colors = intArrayOf(0) + IntArray(n) { tokenizer.nextToken().toInt() }
    val adj = Array(n + 1) { mutableListOf<Int>() }
    for (j in 2..n) {
        tokenizer = StringTokenizer(jin.readLine())
        val a = tokenizer.nextToken().toInt()
        val b = tokenizer.nextToken().toInt()
        adj[a].add(b)
        adj[b].add(a)
    }
    val parent = IntArray(n + 1)
    val answer = LongArray(n + 1) { (n.toLong() * (n + 1).toLong()) / 2L }
    val subTree = IntArray(n + 1) { 1 }
    subTree[0] = Int.MIN_VALUE
    val DUMMY_MAP = mutableMapOf<Int, Int>()
    val specific = Array(n + 1) { DUMMY_MAP }
    val stack = Stack<Int>()
    stack.push(-1)
    while (stack.isNotEmpty()) {
        var a = stack.pop()
        if (a < 0) {
            a *= -1
            stack.push(a)
            for (b in adj[a]) {
                if (b != parent[a]) {
                    parent[b] = a
                    stack.push(-b)
                }
            }
        } else {
            var bestChild = 0
            for (b in adj[a]) {
                if (b != parent[a]) {
                    subTree[a] += subTree[b]
                    val amt = subTree[b] + (specific[b][colors[a]] ?: 0)
                    answer[colors[a]] -= (amt.toLong() * (amt + 1).toLong()) / 2L
                    if (subTree[b] > subTree[bestChild]) {
                        bestChild = b
                    }
                }
            }
            if (bestChild == 0) {
                specific[a] = mutableMapOf<Int, Int>()
            } else {
                specific[a] = specific[bestChild]
                for (b in adj[a]) {
                    if (b != parent[a] && b != bestChild) {
                        for ((color, amt) in specific[b]) {
                            specific[a][color] = (specific[a][color] ?: 0) + amt
                        }
                    }
                }
            }
            specific[a][colors[a]] = -subTree[a]
        }
    }
    for (color in 1..n) {
        val amt = subTree[1] + (specific[1][color] ?: 0)
        answer[color] -= (amt.toLong() * (amt + 1).toLong()) / 2L
    }
    answer[0] = 0
    println(answer.joinToString("\n").substring(2))
}