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 Java (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;

public class Main {
	static int[] a, lis, b;
	static ArrayList<Integer>[] adjList;

	static Stack<Solve> st ;

	public static int bins(int key) {
		int lo = 0;
		int hi = b.length - 1;
		int ans = -1;
		while (lo <= hi) {
			int mid = lo + hi >> 1;
			if (b[mid] >= key) {
				ans = mid;
				hi = mid - 1;
			} else {
				lo = mid + 1;
			}
		}
		return ans;
	}

	public static void solve(int u, int p) {
		st.add(new Solve(0, -1, true));
		while (!st.isEmpty()) {
			st.pop().ex();
		}
	}

	static class Solve {
		int u;
		int p;
		boolean s;
		int old;
		int changedidx;

		public Solve(int u, int p, boolean s) {
			this.u = u;
			this.p = p;
			this.s = s;
		}

		public Solve(int old, int changedidx) {
			this.old = old;
			this.changedidx = changedidx;
			s = false;
		}

		public void ex() {
			if (s) {
				int changedidx = bins(a[u]);
				lis[u] = changedidx + 1;
				int old = b[changedidx];
				b[changedidx] = a[u];
				st.add(new Solve(old, changedidx));
				for (int x : adjList[u]) {
					if (x != p) {
						st.add(new Solve(x, u, true));
					}
				}
			} else {
				b[changedidx] = old;
			}
		}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int n = sc.nextInt();
		a = sc.nextIntArr(n);
		b = new int[n + 1];
		Arrays.fill(b, (int) 1e9);
		adjList = new ArrayList[n];
		for (int i = 0; i < adjList.length; i++) {
			adjList[i] = new ArrayList<Integer>();
		}
		for (int i = 0; i < n - 1; i++) {
			int u = sc.nextInt() - 1;
			int v = sc.nextInt() - 1;
			adjList[u].add(v);
			adjList[v].add(u);
		}
		TreeSet<Integer> ts = new TreeSet<Integer>();
		for (int i = 0; i < a.length; i++) {
			ts.add(a[i]);
		}
		int m = ts.size();
		int N = 1;

		while (N < m)
			N <<= 1;

		lis = new int[n];

		TreeMap<Integer, Integer> tm = new TreeMap<>();
		while (!ts.isEmpty()) {
			tm.put(ts.pollFirst(), tm.size() + 1);
		}
		for (int i = 0; i < a.length; i++) {
			a[i] = tm.get(a[i]);
		}
		st = new Stack<Solve>();
		solve(0, -1);
		for (int i = 0; i < lis.length; i++) {
			pw.println(lis[i]);
		}
		pw.close();

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}