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)

fn lower_bound<T: std::cmp::Ord>(arr: &Vec<T>, b: T) -> usize {
    let (mut lt, mut ge) = (0, arr.len());
    // lt=-1にすると型強制しなきゃなのでここで確かめる
    if arr[lt] >= b {
        return 0;
    }
    while lt + 1 < ge {
        let m = (lt + ge) / 2;
        if arr[m] < b {
            lt = m;
        } else if arr[m] > b {
            ge = m;
        } else {
            return m;
        }
    }
    ge
}

fn bfs(
    v: usize,
    adjl: &Vec<Vec<usize>>,
    dp: &mut Vec<usize>,
    used: &mut Vec<usize>,
    stack: &mut Vec<(usize, usize)>,
    a: &Vec<usize>,
) {
    if used[v] != std::usize::MAX {
        return;
    } else {
        // 更新
        let b = lower_bound(&dp, a[v]);
        stack.push((b, dp[b]));
        dp[b] = a[v];
        used[v] = b + 1;
        for u in &adjl[v] {
            bfs(*u, adjl, dp, used, stack, a);
        }
        if let Some((position, o_val)) = stack.pop() {
            dp[position] = o_val;
        }
    }
}

fn main() {
    let n = read();
    let a: Vec<usize> = (0..n).map(|_| read::<usize>()).collect();
    let mut adjl = vec![vec![]; n];
    for _ in 0..n - 1 {
        let (u, v): (usize, usize) = (read::<usize>() - 1, read::<usize>() - 1);
        adjl[u].push(v);
    }
    let mut dp = vec![std::usize::MAX; n];
    let mut stack: Vec<(usize, usize)> = vec![];
    let mut used = vec![std::usize::MAX; n];
    bfs(0, &adjl, &mut dp, &mut used, &mut stack, &a);
    for i in 0..n {
        println!("{}", used[i]);
    }
}

// =========
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::process::exit;

#[allow(dead_code)]
const MOD: usize = 1000000007;

fn read<T: std::str::FromStr>() -> T {
    use std::io::Read;
    let stdin = std::io::stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

// =========