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)

use std::io::{self, BufRead, Write};

struct IO<'a> {
    reader: io::BufReader<io::StdinLock<'a>>,
    writer: io::BufWriter<io::StdoutLock<'a>>,
}

impl<'a> IO<'a> {
    fn new(stdin: &'a io::Stdin, stdout: &'a io::Stdout) -> IO<'a> {
        let reader = io::BufReader::new(stdin.lock());
        let writer = io::BufWriter::new(stdout.lock());
        IO { reader, writer }
    }

    fn read_line(&mut self) -> String {
        let mut buf = String::new();
        self.reader.read_line(&mut buf).unwrap();
        buf
    }

    fn in_1<T: std::str::FromStr>(&mut self) -> T {
        let buf = self.read_line();
        buf.trim_end().parse().ok().unwrap()
    }

    fn in_2<T, U>(&mut self) -> (T, U)
        where T: std::str::FromStr, U: std::str::FromStr
    {
        let buf = self.read_line();
        let mut iter = buf.split_whitespace();
        (
            iter.next().unwrap().parse().ok().unwrap(),
            iter.next().unwrap().parse().ok().unwrap(),
        )
    }

    fn in_v<T: std::str::FromStr>(&mut self) -> Vec<T> {
        let buf = self.read_line();
        let iter = buf.split_whitespace();
        iter.map(|x| x.parse().ok().unwrap()).collect()
    }

    fn out<T: std::fmt::Display>(&mut self, ans: T) {
        writeln!(self.writer, "{}", ans).unwrap();
    }
}

const INF: i32 = 1 << 30;

struct DFS {
    n: usize,
    a: Vec<i32>,
    g: Vec<Vec<usize>>,
    seen: Vec<bool>,
    dp: Vec<i32>,
    ans: Vec<String>,
}

impl DFS {
    fn new(n: usize, a: Vec<i32>, g: Vec<Vec<usize>>) -> DFS {
        DFS {
            n, a, g,
            seen: vec![false; n],
            dp: vec![INF; n],
            ans: vec![String::new(); n],
        }
    }

    fn search(&mut self, from: usize) {
        self.seen[from] = true;
        let i = {
            let (mut left, mut right) = (0, self.n);
            while left < right {
                let mid = (left + right) / 2;
                if self.dp[mid] <= self.a[from] {
                    left = mid + 1;
                } else {
                    right = mid;
                }
            }
            left
        };
        let tmp = self.dp[i];
        self.dp[i] = self.a[from];
        let j = {
            let (mut left, mut right) = (0, self.n);
            while left < right {
                let mid = (left + right) / 2;
                if self.dp[mid] < INF {
                    left = mid + 1;
                } else {
                    right = mid;
                }
            }
            left
        };
        self.ans[from] = j.to_string();
        for to in self.g[from].clone() {
            if self.seen[to] { continue; }
            self.search(to);
        }
        self.dp[i] = tmp;
    }
}

fn main() {
    let stdin = io::stdin();
    let stdout = io::stdout();
    let mut io = IO::new(&stdin, &stdout);

    let n: usize = io.in_1();
    let a: Vec<i32> = io.in_v();
    let mut g: Vec<Vec<usize>> = vec![vec![]; n];
    for _ in 0..(n - 1) {
        let (u, v): (usize, usize) = {
            let (u, v): (usize, usize) = io.in_2();
            (u - 1, v - 1)
        };
        g[u].push(v);
        g[v].push(u);
    }

    let mut dfs = DFS::new(n, a, g);
    dfs.search(0);
    io.out(dfs.ans.join("\n"));
}