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 JavaScript (Node.js 12.16.1) (judge verdict: Wrong Answer)

function Scanner(input) {
	var self = this;
	self.itemNo = 0;
	self.orgStr = input;
	self.splitedStrs = (function(target) {
		var result = [];
		var target = target.replace(/[\n \t]+/g, " ");
		var strs = target.split(" ");
		for (var i = 0, len = strs.length; i < len; i++) {
			result.push(strs[i]);
		}
		return result;
	}(input));

	self.next = function() {
		var result = self.splitedStrs[self.itemNo];
		self.itemNo++;
		return (result != null) ? result : null;
	}

	self.nextInt = function() {
		var result = self.splitedStrs[self.itemNo];
		self.itemNo++;
		return (result != null) ? parseInt(result) : null;
	}

	self.nextBigDecimal = function() {
		var result = self.splitedStrs[self.itemNo];
		self.itemNo++;
		return (result != null) ? parseFloat(result) : null;
	}
	
	self.nextByte = function() {
		var result = self.splitedStrs[self.itemNo];
		self.itemNo++;
		return (result != null) ? new Int8Array(result) : null;
	}

}

function Main(sc) {

	var N = sc.nextInt();
	var anss = [];
	
	wrtnObj = {};
	for (var i = 0; i < N; i++) {
		wrtnObj[i + 1] = sc.nextInt();
	}
	
	orderedSubTree = {};
	for (var i = 0; i < N - 1; i++) {
		var n1 = sc.nextInt();
		var n2 = sc.nextInt();
		if (!orderedSubTree[n1]) orderedSubTree[n1] = [n2];
		else orderedSubTree[n1].push(n2);
		if (!orderedSubTree[n2]) orderedSubTree[n2] = [n1];
		else orderedSubTree[n2].push(n1);
	}
	
	// 木をたどって要素を順に取得していく
	wrtnNoLists = {};
	nexts = [];
	crawlNode = function (tgtNo, fromNo) {
//		wrtnNos = (wrtnNoLists[fromNo]) ? wrtnNoLists[fromNo].concat() : [];
//		// 自分の数字を詰め込んで、確定
//		wrtnNos.push(wrtnObj[tgtNo]);
//		wrtnNoLists[tgtNo] = wrtnNos;
		wrtnNoLists[tgtNo] = 1;
		var directionObj = orderedSubTree[tgtNo];
		if (directionObj) {
			for (var i = 0; i < directionObj.length; i++) {
				var nextNo = directionObj[i];
				if (!wrtnNoLists[nextNo]) {
					nexts.push([nextNo, tgtNo]);
				}
			}
		}
	}
	
	nexts.push([1, -1]);
	for (var YAKUYOKE = 0; YAKUYOKE < 10000000; YAKUYOKE++) {
		var next = nexts.shift();
		crawlNode(next[0], next[1]);
		if (nexts.length == 0) break;
	}
	
	for (var i = 1; i <= N; i++) {

		var list = wrtnNoLists[i];
		var list2 = [];

		for (var j = 0; j < list.length; j++) {
			var wk = list[j];
			if (list2.length == 0) {
				list2.push(wk);
			}
			// 最終より大きい場合push
			else if (wk > list2[list2.length - 1]) {
				list2.push(wk);
			}
			// 最初より同じか小さい場合
			else if (wk <= list2[0]) {
				list2[0] = wk;
			}
			// 最初より大きくて最終より小さい場合、l[i-1]<wk<l[i]	となるl[i]とwkを置換する
			else {
				for (var k = 1; k < list2.length; k++) {
					if (list2[k-1] < wk && wk <= list2[k]) {
						list2[k] = wk;
						break;
					}
				}
			}
		}
		anss.push(list2.length);
	}
	
	console.log(anss.join("\n"));
}
Main(new Scanner(require("fs").readFileSync("/dev/stdin", "utf8")));
// Main(new Scanner("33"));

// var str1 = sc.next(); // Stringで取得
// var int1 = sc.nextInt(); // intで取得
// var bd1 = sc.nextBigDecimal(); // BigDecimalで取得
// var byte1 = sc.nextByte(); // byteで取得
// for (var i = 0; i < 1000000000; i++) var toDelay = "aaa"; // 1000msくらいの遅延
// console.log(wrtnNos); return;