Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::marker::Usize1;

fn main() {
    proconio::input! {
        n: usize,
        a: [u32; n],
        edge: [(Usize1, Usize1); n-1]
    }
    let mut adj = vec![Vec::new(); n];
    for (u, v) in edge {
        adj[u].push(v);
        adj[v].push(u);
    }
    let mut dp = vec![10u32.pow(9) + 1; n];
    let mut sn = vec![false; n];
    let mut ans = vec![0; n];
    ans[0] = 1;
    sn[0] = true;
    dp[0] = a[0];
    dfs(0, (0, a[0]), &a, &adj, &mut dp, &mut ans, &mut sn, n);
    for k in 0..n {
        println!("{}", ans[k])
    }
}

fn dfs(
    v: usize, re: (usize, u32), a: &Vec<u32>, adj: &[Vec<usize>],
    dp: &mut Vec<u32>, ans: &mut Vec<usize>, sn: &mut Vec<bool>, n: usize,
)
{
    for &w in &adj[v] {
        if sn[w] {
            continue;
        }
        sn[w] = true;
        let mut i = 0;
        let mut j = n;
        while i + 1 != j {
            let m = (i + j) / 2;
            if dp[m] < a[w] {
                i = m
            } else {
                j = m
            }
        }
        let re = if a[w] < dp[0] {
            (0, dp[0])
        } else {
            (i + 1, dp[i + 1])
        };
        dp[re.0] = a[w];
        let mut i = 0;
        let mut j = n;
        while i + 1 != j {
            let m = (i + j) / 2;
            if dp[m] <= 10u32.pow(9) {
                i = m
            } else {
                j = m
            }
        }
        ans[w] = i + 1;
        dfs(w, re, a, adj, dp, ans, sn, n)
    }
    dp[re.0] = re.1;
}
*/

fn main() {
    let exe = "/tmp/bin4F316FA9";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";