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 11.0.6) (judge verdict: Time Limit Exceeded)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Sparsh Sanchorawala
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        FLISOnTree solver = new FLISOnTree();
        solver.solve(1, in, out);
        out.close();
    }

    static class FLISOnTree {
        ArrayList<Integer>[] adj;
        int[] map;
        int[] left;
        int[] right;
        int time;
        int[] tree;
        int[] lazy;
        int[] res;

        void dfs(int x, int p) {
            time++;
            left[x] = time;
            map[time] = x;
            for (int y : adj[x]) {
                if (y != p) {
                    dfs(y, x);
                }
            }
            right[x] = time;
            //System.out.println(x + ": "  + left[x] + " " + right[x]);
        }

        void pushLazy(int n, int nL, int nR) {
            tree[n] = Math.max(lazy[n], tree[n]);
            if (nL != nR) {
                lazy[2 * n + 1] = Math.max(lazy[n], lazy[2 * n + 1]);
                lazy[2 * n + 2] = Math.max(lazy[n], lazy[2 * n + 2]);
            }
            lazy[n] = 0;
        }

        void update(int n, int nL, int nR, int l, int r, int x) {
            pushLazy(n, nL, nR);
            if (nR < l || r < nL)
                return;
            if (l <= nL && nR <= r) {
                lazy[n] = Math.max(x, lazy[n]);
                pushLazy(n, nL, nR);
                return;
            }
            update(2 * n + 1, nL, (nL + nR) / 2, l, r, x);
            update(2 * n + 2, (nL + nR) / 2 + 1, nR, l, r, x);
            tree[n] = Math.max(tree[2 * n + 1], tree[2 * n + 2]);
        }

        int query(int n, int nL, int nR, int idx) {
            pushLazy(n, nL, nR);
            if (nR < idx || idx < nL)
                return 0;
            if (nL == idx && nR == idx)
                return tree[n];
            int ret = Math.max(query(2 * n + 1, nL, (nL + nR) / 2, idx), query(2 * n + 2, (nL + nR) / 2 + 1, nR, idx));
            tree[n] = Math.max(tree[2 * n + 1], tree[2 * n + 2]);
            return ret;
        }

        void dfs2(int x, int p) {
            if (p != -1)
                res[x] = Math.max(res[x], res[p]);
            for (int y : adj[x]) {
                if (y != p)
                    dfs2(y, x);
            }
        }

        public void solve(int testNumber, InputReader s, PrintWriter w) {
            int n = s.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = s.nextInt();
            Pair[] p = new Pair[n];
            for (int i = 0; i < n; i++)
                p[i] = new Pair(i, a[i]);
            Arrays.sort(p, new Comparator<Pair>() {

                public int compare(Pair p1, Pair p2) {
                    if (p1.w < p2.w)
                        return -1;
                    if (p1.w > p2.w)
                        return 1;
                    return 0;
                }
            });
            adj = new ArrayList[n];
            for (int i = 0; i < n; i++)
                adj[i] = new ArrayList<>();
            for (int i = 0; i < n - 1; i++) {
                int u = s.nextInt() - 1, v = s.nextInt() - 1;
                adj[u].add(v);
                adj[v].add(u);
            }
            map = new int[n];
            left = new int[n];
            right = new int[n];
            time = -1;
            dfs(0, -1);
            int sz = 1 << (int) Math.ceil(Math.log(n) / Math.log(2) + 1);
            tree = new int[sz];
            lazy = new int[sz];
            res = new int[n];
            int i = 0;
            while (i < n) {
                int j = i;
                while (j < n && p[i].w == p[j].w) {
                    int x = p[j].i;
                    res[x] = query(0, 0, n - 1, left[x]) + 1;
                    j++;
                }
                j = i;
                while (j < n && p[i].w == p[j].w) {
                    int x = p[j].i;
                    update(0, 0, n - 1, left[x], right[x], res[x]);
                    j++;
                }
                i = j;
            }
            dfs2(0, -1);
            for (i = 0; i < n; i++)
                w.println(res[i]);

        }

        class Pair {
            int i;
            int w;

            Pair(int i, int w) {
                this.i = i;
                this.w = w;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}