module IntSet = Set.Make (struct
  type t = int
  let compare = compare
end)

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let es = Array.make n [] in
  for i = 1 to n - 1 do
    Scanf.scanf "%d %d\n" @@ fun u v ->
      es.(u - 1) <- v - 1 :: es.(u - 1);
      es.(v - 1) <- u - 1 :: es.(v - 1)
  done;
  let ans = Array.make n 0 in
  let visited = Array.make n false in
  let rec visit l v =
    if not visited.(v) then begin
      visited.(v) <- true;
      let l' =
        match IntSet.split as_.(v) l with
        | _, true, _ -> l
        | _, false, lr ->
            if IntSet.is_empty lr
            then IntSet.add as_.(v) l
            else IntSet.add as_.(v) (IntSet.remove (IntSet.min_elt lr) l) in
      ans.(v) <- IntSet.cardinal l';
      List.iter (visit l') es.(v)
    end in
  visit IntSet.empty 0;
  Array.iter (Printf.printf "%d\n") ans
