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: Runtime Error)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
  private static final int INF = 1_000_100_001;
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    int N = scanner.nextInt();
    Node[] nodes = new Node[N];
    for (int i = 0; i < N; i++) nodes[i] = new Node(N, scanner.nextInt());
    for (int i = 0; i < N - 1; i++) {
      int u = scanner.nextInt() - 1;
      int v = scanner.nextInt() - 1;
      nodes[u].adjacent.add(nodes[v]);
      nodes[v].adjacent.add(nodes[u]);
    }
    dfs(N, nodes[0], null, 0);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < N; i++) {
      int j = upperBound(nodes[i].lis, INF);
      sb.append(j + 1).append('\n');
    }
    System.out.print(sb.toString());
  }

  private static void dfs(int N, Node node, Node from, int depth) {
    if (from != null) {
      System.arraycopy(from.lis, 0, node.lis, 0, N);
    }
    int i = upperBound(node.lis, node.a);
    node.lis[i + 1] = Math.min(node.lis[i + 1], node.a);
    for (Node a : node.adjacent) {
      if (a != from) {
        dfs(N, a, node, depth + 1);
      }
    }
  }

  private static class Node {
    private final List<Node> adjacent = new ArrayList<>();
    private final int[] lis;
    private final int a;

    public Node(int N, int a) {
      lis = new int[N + 1];
      Arrays.fill(lis, INF);
      this.a = a;
    }
  }

  public static int upperBound(int[] a, int k) {
    int left = -1;
    int right = a.length;
    while (right - left > 1) {
      int mid = (left + right) / 2;
      if (a[mid] < k) {
        left = mid;
      } else {
        right = mid;
      }
    }
    return left;
  }
}