Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote a_{i}. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).

Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.

The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ a_{u}.

Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that v controls u.


-----Input-----

The first line contains single integer n (1 ≤ n ≤ 2·10^5).

The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the integers written in the vertices.

The next (n - 1) lines contain two integers each. The i-th of these lines contains integers p_{i} and w_{i} (1 ≤ p_{i} ≤ n, 1 ≤ w_{i} ≤ 10^9) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between p_{i} and (i + 1).

It is guaranteed that the given graph is a tree.


-----Output-----

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.


-----Examples-----
Input
5
2 5 1 4 6
1 7
1 1
3 5
3 6

Output
1 0 1 0 0

Input
5
9 7 8 6 5
1 1
2 1
3 1
4 1

Output
4 3 2 1 0



-----Note-----

In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1 controls the vertex 5).

Source: codeforces | Difficulty: HARD
Tags: Graph algorithms

Graph traversal

Sorting

Tree algorithms

Data structures


Solutions:
from itertools import accumulate
from math import *
import os, sys
import threading
from io import BytesIO
from bisect import bisect_left
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
e = {}
g = [[] for i in range(n)]
d = [0] * (n + 5)
ans = [0] * n
p = [0] * (n + 5)
for i in range(n - 1):
	(c, w) = map(int, input().split())
	c -= 1
	g[c].append(i + 1)
	e[i + 1] = w

def dfs(i, h):
	global ans, a, e, g, d, p
	p[h] = 0
	for j in g[i]:
		d[h + 1] = d[h] + e[j]
		dfs(j, h + 1)
	x = bisect_left(d, d[h] - a[i], 0, h + 1)
	if x >= 0:
		p[x - 1] -= 1
	p[h - 1] += p[h] + 1
	ans[i] = p[h]

def solve():
	global ans
	dfs(0, 0)
	print(*ans)
sys.setrecursionlimit(10 ** 5 * 2 + 1000)
threading.stack_size((10 ** 5 * 2 + 1000) * 500)
thread = threading.Thread(target=solve)
thread.start()


import sys
import threading
from bisect import *

def dfs(v):
	global d
	d += 1
	dist[d] = w[v] + dist[d - 1]
	add[d] = 0
	for i in c[v]:
		dfs(i)
	x = bisect_left(dist, dist[d] - a[v], 0, d)
	add[x - 1] -= 1
	add[d - 1] += 1
	add[d - 1] += add[d]
	ans[v] = add[d]
	d -= 1
d = 0
dist = [0 for i in range(2 * 10 ** 5 + 3)]
add = [0 for i in range(2 * 10 ** 5 + 3)]
n = int(input())
a = [0 for i in range(n + 1)]
c = [[] for i in range(n + 1)]
w = [0 for i in range(n + 1)]
ans = [0 for i in range(n + 1)]
for (i, j) in enumerate(map(int, input().split())):
	a[i + 1] = j
for i in range(2, n + 1):
	(t1, t2) = map(int, input().split())
	c[t1].append(i)
	w[i] = t2
sys.setrecursionlimit(10 ** 6)
threading.stack_size(10 ** 7 * 8)
thread = threading.Thread(target=dfs, args=(1,))
thread.start()
if thread.is_alive():
	thread.join()
print(' '.join((str(ans[i]) for i in range(1, n + 1))))


import sys
import threading
from bisect import *
from itertools import *
from collections import defaultdict
from math import log2
from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10 ** 5)

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

@bootstrap
def dfs(v):
	global d
	d += 1
	dist[d] = w[v] + dist[d - 1]
	add[d] = 0
	for i in c[v]:
		yield dfs(i)
	x = bisect_left(dist, dist[d] - a[v], 0, d)
	add[x - 1] -= 1
	add[d - 1] += 1
	add[d - 1] += add[d]
	ans[v] = add[d]
	d -= 1
	yield
d = 0
dist = [0]
dist = [0 for i in range(2 * 10 ** 5 + 3)]
add = [0 for i in range(2 * 10 ** 5 + 3)]
ans = [0 for i in range(2 * 10 ** 5 + 3)]
n = int(input())
a = [0]
c = [[] for i in range(n + 1)]
w = [0 for i in range(n + 1)]
for i in map(int, input().split()):
	a.append(i)
for i in range(2, n + 1):
	(t1, t2) = map(int, input().split())
	c[t1].append(i)
	w[i] = t2
dfs(1)
print(' '.join((str(ans[i]) for i in range(1, n + 1))))


import sys
import threading
from bisect import *

def ri():
	return map(int, input().split())

def dfs(v, d, c, w):
	d += 1
	dist[d] = w[v] + dist[d - 1]
	add[d] = 0
	for i in c[v]:
		dfs(i, d, c, w)
	x = bisect_left(dist, dist[d] - a[v], 0, d)
	add[x - 1] -= 1
	add[d - 1] += 1 + add[d]
	ans[v] = str(add[d])
	d -= 1
n = int(input())
dist = [0] * (2 * 10 ** 5 + 3)
add = [0] * (2 * 10 ** 5 + 3)
ans = [''] * (n + 1)
a = [0] * (n + 1)

def solve():
	c = [[] for i in range(n + 1)]
	w = [0] * (n + 1)
	for (i, j) in enumerate(ri()):
		a[i + 1] = j
	for i in range(2, n + 1):
		(t1, t2) = ri()
		c[t1].append(i)
		w[i] = t2
	dfs(1, 0, c, w)
	print(' '.join(ans))
sys.setrecursionlimit(10 ** 5 * 3 + 100)
threading.stack_size(10 ** 8 + 10 ** 7)
thread = threading.Thread(target=solve)
thread.start()


import sys
import threading
from bisect import bisect_left
n = int(input())
a = list(map(int, input().split()))
e = {}
g = [[] for i in range(n)]
d = [0] * (n + 5)
ans = [0] * n
p = [0] * (n + 5)
for i in range(n - 1):
	(c, w) = map(int, input().split())
	c -= 1
	g[c].append(i + 1)
	e[i + 1] = w

def dfs(i, h):
	nonlocal ans, a, e, g, d, p
	p[h] = 0
	for j in g[i]:
		d[h + 1] = d[h] + e[j]
		dfs(j, h + 1)
	x = bisect_left(d, d[h] - a[i], 0, h + 1)
	if x >= 0:
		p[x - 1] -= 1
	p[h - 1] += p[h] + 1
	ans[i] = p[h]

def solve():
	nonlocal ans
	dfs(0, 0)
	print(' '.join(map(str, ans)))
max_recur_size = 10 ** 5 * 2 + 1000
max_stack_size = max_recur_size * 500
sys.setrecursionlimit(max_recur_size)
threading.stack_size(max_stack_size)
thread = threading.Thread(target=solve)
thread.start()