import Control.Monad
import Data.Maybe
import Data.List
import Data.Array
import qualified Data.IntMap.Strict as M
import qualified Data.ByteString.Char8 as C

parseInt :: C.ByteString -> Int
parseInt = fst . fromJust . C.readInt

data Tree = Tree Int Int [Tree]

createTree :: Array Int Int -> M.IntMap [Int] -> Int -> Int -> Tree
createTree vals graph u par =
  let val = vals ! u
      vs = fromJust $ M.lookup u graph
      childs = map (\v -> createTree vals graph v u) $ filter (/= par) vs
  in Tree u val childs

dfs :: Tree -> M.IntMap Int -> [(Int, Int)] -> [(Int, Int)]
dfs (Tree u val childs) mp results =
  (u, M.size mp') : foldl' (\results' child -> dfs child mp' results') results childs
  where
  mp' = case M.lookupGE val mp of
    Nothing -> M.insert val (M.size mp + 1) mp
    Just (val', len) -> M.insert val len (M.delete val' mp)

main :: IO ()
main = do
  n <- parseInt <$> C.getLine
  as <- map parseInt . C.words <$> C.getLine
  es <- replicateM (n - 1) $ do
    [u, v] <- map parseInt . C.words <$> C.getLine
    return (u, v)
  let update x Nothing = Just [x]
      update x (Just xs) = Just (x : xs)
      vals = array (1, n) $ zip [1..n] as
      graph = foldl' (\mp (u, v) -> M.alter (update v) u $ M.alter (update u) v mp) M.empty es
      tree = createTree vals graph 1 0
      ans = array (1, n) $ dfs tree M.empty []
  C.putStr $ C.unlines $ map (C.pack . show) [ans ! i | i <- [1..n]]