#[allow(unused_imports)]
use std::cmp::Ordering;

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get {
    ([$t: ty]) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

const INF: usize = 1_000_000_000_000_000;

fn lower_bound<F>(l: isize, r: isize, criterion: F) -> isize
where
    F: Fn(isize) -> bool,
{
    let mut l = l;
    let mut r = r;
    while r - l > 1 {
        let mid = l + (r - l) / 2;
        if criterion(mid) {
            r = mid;
        } else {
            l = mid;
        }
    }
    r
}

fn dfs(
    node: usize,
    parent: usize,
    nodes: &Vec<Vec<usize>>,
    dp: &mut Vec<usize>,
    res: &mut Vec<usize>,
    a: &Vec<usize>,
    mut val: usize,
) {
    let crit = |i: isize| {
        let i = i as usize;
        a[node - 1] <= dp[i]
    };
    let i = lower_bound(-1, a.len() as isize, crit) as usize;
    if dp[i] == INF {
        val += 1;
    }
    let before = dp[i];
    dp[i] = a[node - 1];
    for &next in &nodes[node] {
        if next == parent {
            continue;
        }
        dfs(next, node, nodes, dp, res, a, val);
    }
    dp[i] = before;
    res[node - 1] = val;
}

fn solve() {
    let n = get!(usize);
    let a = get!([usize]);
    let uv = get!(usize, usize; n-1);

    let mut nodes = vec![Vec::new(); n + 1];
    for (u, v) in uv {
        nodes[u].push(v);
        nodes[v].push(u);
    }

    let mut res = vec![0; n];
    dfs(1, 0, &nodes, &mut vec![INF; n], &mut res, &a, 0);
    for i in 0..n {
        println!("{}", res[i]);
    }
}


Measured memory_bytes: 39880