// https://atcoder.jp/contests/abc163/tasks/abc163_f
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===


type Graph = Vec<Vec<usize>>;

fn dfs_cpo(g: &Graph, root: usize) -> (Vec<usize>, Vec<Option<usize>>, Vec<usize>) {
    let n = g.len();
    struct Cpo {
        count: Vec<usize>,
        parent: Vec<Option<usize>>,
        order: Vec<usize>,
        oidx: usize
    };
    let mut cpo = Cpo {
        count: vec![0; n],
        parent: vec![None; n],
        order: vec![0; n],
        oidx: 0
    };

    fn dfs(g: &Graph, now: usize, par: Option<usize>, cpo: &mut Cpo) {
        for &to in &g[now] {
            if Some(to) != par {
                dfs(g, to, Some(now), cpo);
            }
        }
        cpo.count[now] = 1;
        for &to in &g[now] {
            if Some(to) != par {
                cpo.count[now] += cpo.count[to];
            }
        }
        cpo.parent[now] = par;
        cpo.order[cpo.oidx] = now;
        cpo.oidx += 1;

    }
    dfs(&g, root, None, &mut cpo);
    (cpo.count, cpo.parent, cpo.order)
}
//===


fn dfs(g: &Graph, now: usize, par: Option<usize>, color: &Vec<usize>, color_parent: &mut Vec<(usize,usize)>, last_color: &mut Vec<usize>, last_color_idx: &mut Vec<usize>) {
    let n = g.len();
    let c = color[now];
    if last_color[c] == n {
        color_parent[now] = (now, 0);
    } else {
        color_parent[now] = (last_color[c], last_color_idx[c]);
    }

    for (idx, &to) in g[now].iter().enumerate() {
        if Some(to) == par {
            continue;
        }
        let prev = last_color[c];
        let prev2 = last_color_idx[c];
        last_color[c] = now;
        last_color_idx[c] = idx;

        dfs(g, to, Some(now), color, color_parent, last_color, last_color_idx);

        last_color[c] = prev;
        last_color_idx[c] = prev2;
    }
}

fn main() {
    input! {
        n: usize,
        c: [usize1; n],
        edges: [(usize1, usize1); n-1]
    };

    let mut g = vec![vec![]; n];

    for (u, v) in edges {
        g[u].push(v);
        g[v].push(u);
    }

    let mut nw = n as i64;

    let mut component_list = vec![vec![]; n];
    let mut answer = vec![(nw*(nw+1))/2; n];
    let mut leftover = vec![0; n];

    let mut total_removed = vec![];
    for i in 0..n {
        total_removed.push(vec![0; g[i].len()]);
    }
    let (children, parent, order) = dfs_cpo(&g, 0);
    let mut color_parent = vec![(0,0); n];
    let mut last_color = vec![n; n];
    let mut last_color_idx = vec![0; n];

    dfs(&g, 0, None, &c, &mut color_parent, &mut last_color, &mut last_color_idx);

    for i in 0..n {
        let v = order[i];
        let col = c[v];

        for (idx, &to) in g[v].iter().enumerate() {
            if Some(to) == parent[v] {
                continue;
            }
            if children[to] > total_removed[v][idx] {
                component_list[col].push(children[to]-total_removed[v][idx]);
                let sz = (children[to]-total_removed[v][idx]) as i64;
                answer[col] -= sz*(sz+1)/2;
            }
        }

        let (cp, idx) = color_parent[v];
        if cp == v {
            leftover[col] += children[v];
        } else {
            total_removed[cp][idx] += children[v];
        }
    }

    for i in 0..n {
        let lo = (n-leftover[i]) as i64;
        answer[i] -= lo * (lo+1)/2;
        println!("{}", answer[i]);
    }

    // debug!(component_list);
}


Measured memory_bytes: 78796