Input:
n, k = list(map(int, input().split()))

a = list(map(int, input().split()))

d = {}

for i in a:

    if i not in d:

        d[i] = 1

    else:

        d[i] += 1

d = sorted(list(d.items()), key=lambda x:x[1], reverse=True)

ans = 0

if len(d) <= k:

    print((0))

else:

    for i in range(len(d) - k):

        ans += d[-i-1][1]

    print(ans)

import collections

n, k = list(map(int,input().split()))

a = input().split()

a = sorted(list(collections.Counter(a).values()), reverse=True)

s = sum(a[:k])

print((0 if s == n else n - s))

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for i in a:
    if i not in d:
        d[i] = 1
    else:
        d[i] += 1
d = sorted(list(d.items()), key=lambda x: x[1], reverse=True)
ans = 0
if len(d) <= k:
    print((0))
else:
    for i in range(len(d) - k):
        ans += d[-i - 1][1]
    print(ans)

import collections

n, k = list(map(int, input().split()))
a = input().split()
a = sorted(list(collections.Counter(a).values()), reverse=True)
s = sum(a[:k])
print((0 if s == n else n - s))

-d = sorted(list(d.items()), key=lambda x: x[1], reverse=True)

+a = sorted(list(collections.Counter(a).values()), reverse=True)


Input:
# AOJ 1501: Grid

# Python3 2018.7.13 bal4u

MOD = 100000007

def dp(n, k):

	if tbl[n][k]: return tbl[n][k]

	if (k << 1) > n: k = n-k

	if k == 0: ans = 1

	elif k == 1: ans = n

	else: ans = dp(n-1, k) + dp(n-1, k-1)

	tbl[n][k] = ans % MOD

	return tbl[n][k]

tbl = [[0 for j in range(1001)] for i in range(1001)]

k = 0

r, c, a1, a2, b1, b2 = list(map(int, input().split()))

dr = abs(a1-b1)

if dr > r-dr: dr = r-dr

if (dr << 1) == r: k += 1

dc = abs(a2-b2)

if dc > c-dc: dc = c-dc

if (dc << 1) == c: k += 1

print(((dp(dr+dc, min(dr, dc)) << k) % MOD))

# AOJ 1501: Grid

# Python3 2018.7.13 bal4u

from math import factorial

def comb (n, k):

	return factorial(n)//factorial(n-k)//factorial(k)

k = 0

r, c, a1, a2, b1, b2 = list(map(int, input().split()))

dr = abs(a1-b1)

if dr > r-dr: dr = r-dr

if (dr << 1) == r: k += 1

dc = abs(a2-b2)

if dc > c-dc: dc = c-dc

if (dc << 1) == c: k += 1

print(((comb(dr+dc, min(dr, dc)) << k) % 100000007))

# AOJ 1501: Grid
# Python3 2018.7.13 bal4u
MOD = 100000007

def dp(n, k):
    if tbl[n][k]:
        return tbl[n][k]
    if (k << 1) > n:
        k = n - k
    if k == 0:
        ans = 1
    elif k == 1:
        ans = n
    else:
        ans = dp(n - 1, k) + dp(n - 1, k - 1)
    tbl[n][k] = ans % MOD
    return tbl[n][k]

tbl = [[0 for j in range(1001)] for i in range(1001)]
k = 0
r, c, a1, a2, b1, b2 = list(map(int, input().split()))
dr = abs(a1 - b1)
if dr > r - dr:
    dr = r - dr
if (dr << 1) == r:
    k += 1
dc = abs(a2 - b2)
if dc > c - dc:
    dc = c - dc
if (dc << 1) == c:
    k += 1
print(((dp(dr + dc, min(dr, dc)) << k) % MOD))

# AOJ 1501: Grid
# Python3 2018.7.13 bal4u
from math import factorial

def comb(n, k):
    return factorial(n) // factorial(n - k) // factorial(k)

k = 0
r, c, a1, a2, b1, b2 = list(map(int, input().split()))
dr = abs(a1 - b1)
if dr > r - dr:
    dr = r - dr
if (dr << 1) == r:
    k += 1
dc = abs(a2 - b2)
if dc > c - dc:
    dc = c - dc
if (dc << 1) == c:
    k += 1
print(((comb(dr + dc, min(dr, dc)) << k) % 100000007))

+    return factorial(n) // factorial(n - k) // factorial(k)

-tbl = [[0 for j in range(1001)] for i in range(1001)]

-print(((dp(dr + dc, min(dr, dc)) << k) % MOD))


Input:
def slove():

    import sys

    input = sys.stdin.readline

    a, b = list(map(int, input().rstrip('\n').split()))

    s = str(input().rstrip('\n'))

    ls = list("1234567890")

    for i in range(len(s)):

        if i < a:

            if s[i] not in ls:

                print("No")

                exit()

        elif i == a:

            if s[i] != "-":

                print("No")

                exit()

        elif b <= i:

            if s[i] not in ls:

                print("No")

                exit()

    print("Yes")

if __name__ == '__main__':

    slove()

def slove():

    import sys

    import collections

    input = sys.stdin.readline

    a, b = list(map(int, input().rstrip('\n').split()))

    s = str(input().rstrip('\n'))

    d = collections.defaultdict(int)

    for i in range(10):

        d[str(i)]

    b = True

    for i in range(a + b + 1):

        if i < a:

            if s[i] not in d:

                b = False

                break

        elif i == a:

            if s[i] != "-":

                b = False

                break

        else:

            if s[i] not in d:

                b = False

                break

    if b:

        print("Yes")

    else:

        print("No")

if __name__ == '__main__':

    slove()

def slove():
    import sys

    input = sys.stdin.readline
    a, b = list(map(int, input().rstrip("\n").split()))
    s = str(input().rstrip("\n"))
    ls = list("1234567890")
    for i in range(len(s)):
        if i < a:
            if s[i] not in ls:
                print("No")
                exit()
        elif i == a:
            if s[i] != "-":
                print("No")
                exit()
        elif b <= i:
            if s[i] not in ls:
                print("No")
                exit()
    print("Yes")

if __name__ == "__main__":
    slove()

def slove():
    import sys
    import collections

    input = sys.stdin.readline
    a, b = list(map(int, input().rstrip("\n").split()))
    s = str(input().rstrip("\n"))
    d = collections.defaultdict(int)
    for i in range(10):
        d[str(i)]
    b = True
    for i in range(a + b + 1):
        if i < a:
            if s[i] not in d:
                b = False
                break
        elif i == a:
            if s[i] != "-":
                b = False
                break
        else:
            if s[i] not in d:
                b = False
                break
    if b:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    slove()

+    import collections
-    ls = list("1234567890")
-    for i in range(len(s)):
+    d = collections.defaultdict(int)
+    for i in range(10):
+        d[str(i)]
+    b = True
+    for i in range(a + b + 1):
-            if s[i] not in ls:
-                print("No")
-                exit()
+            if s[i] not in d:
+                b = False
+                break
-                print("No")
-                exit()
-        elif b <= i:
-            if s[i] not in ls:
-                print("No")
-                exit()
-    print("Yes")
+                b = False
+                break
+        else:
+            if s[i] not in d:
+                b = False
+                break
+    if b:
+        print("Yes")
+    else:
+        print("No")


Input:
S = eval(input())

tmp = []

count = 1

for i, x in enumerate(S):

    if x == 'B':

        tmp.append([i, count])

        count += 1

tmp.sort(key=lambda x: x[1], reverse=True)

ans = 0

for i in range(len(tmp)):

    x = tmp[i][0]

    ans += len(S)-1-i - x

print(ans)

S = eval(input())

count = 0

ans = 0

for x in S:

    if x == "B":

        count += 1

    else:

        ans += count

print(ans)

S = eval(input())
tmp = []
count = 1
for i, x in enumerate(S):
    if x == "B":
        tmp.append([i, count])
        count += 1
tmp.sort(key=lambda x: x[1], reverse=True)
ans = 0
for i in range(len(tmp)):
    x = tmp[i][0]
    ans += len(S) - 1 - i - x
print(ans)

-tmp.sort(key=lambda x: x[1], reverse=True)


Input:
h,w = list(map(int,input().split()))

c = [[int(i) for i in input().split()] for _ in range(10)]

a = [[int(i) for i in input().split()] for _ in range(h)]

for k in range(10):

    for i in range(10):

        for j in range(10):

            c[i][j] = min(c[i][j],c[i][k]+c[k][j])

ans = 0

for i in range(h):

    for j in range(w):

        if abs(a[i][j]) != 1:

            ans += c[a[i][j]][1]

print(ans)

h,w = list(map(int,input().split()))

c = [[int(i) for i in input().split()] for _ in range(10)]

a = [[int(i) for i in input().split()] for _ in range(h)]

for k in range(10):

    for i in range(10):

        for j in range(10):

            c[i][j] = min(c[i][j], c[i][k] + c[k][j])

ans = 0

mp = [c[i][1] for i in range(10)]

mp.append(0)

for i in range(h):

    for j in range(w): ans += mp[a[i][j]]

print(ans)

h, w = list(map(int, input().split()))
c = [[int(i) for i in input().split()] for _ in range(10)]
a = [[int(i) for i in input().split()] for _ in range(h)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
for i in range(h):
    for j in range(w):
        if abs(a[i][j]) != 1:
            ans += c[a[i][j]][1]
print(ans)

h, w = list(map(int, input().split()))
c = [[int(i) for i in input().split()] for _ in range(10)]
a = [[int(i) for i in input().split()] for _ in range(h)]
for k in range(10):
    for i in range(10):
        for j in range(10):
            c[i][j] = min(c[i][j], c[i][k] + c[k][j])
ans = 0
mp = [c[i][1] for i in range(10)]
mp.append(0)
for i in range(h):
    for j in range(w):
        ans += mp[a[i][j]]
print(ans)


Input:
def solve():

    import sys

    sys.setrecursionlimit(10**6)

    def dfs(tree, n, ans):

        for i in tree[n]:

            ans[i] += ans[n]

            tree[i].remove(n)

            dfs(tree, i, ans) #再帰関数になっている。次の木の中を探索

    n, q = list(map(int,input().split()))

    ab = [list(map(int,input().split())) for _ in range(n-1)]

    px = [list(map(int,input().split())) for _ in range(q)]

    g = [[] for _ in range(n)]

    for i in range(n-1):

        g[ab[i][0]-1].append(ab[i][1]-1)

        g[ab[i][1]-1].append(ab[i][0]-1)

    ans = [0]*n

    for i in range(q):

        ans[px[i-1][0]-1] += px[i-1][1]

    dfs(g, 0, ans)

    print((*ans))

if __name__ == "__main__":

    solve()

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

n,q = list(map(int,readline().split()))

ABPX = list(map(int,read().split()))

AB = iter(ABPX[:n+n-2])

PX = iter(ABPX[n+n-2:])

graph = [[] for _ in range(n+1)]

for a, b in zip(AB, AB):

    graph[a].append(b)

    graph[b].append(a)

val = [0] * (n+1)

for p, x in zip(PX, PX):

    val[p] += x

stack = [1] #queue的なやつ

parent = [0] * (n+1)

while stack:

    x = stack.pop() #根を引っこ抜く

    for y in graph[x]: #木を見る

        if y == parent[x]:

            continue

        parent[y] = x

        stack.append(y)

        val[y] += val[x]

print((' '.join(map(str, val[1:]))))

def solve():
    import sys

    sys.setrecursionlimit(10**6)

    def dfs(tree, n, ans):
        for i in tree[n]:
            ans[i] += ans[n]
            tree[i].remove(n)
            dfs(tree, i, ans)  # 再帰関数になっている。次の木の中を探索

    n, q = list(map(int, input().split()))
    ab = [list(map(int, input().split())) for _ in range(n - 1)]
    px = [list(map(int, input().split())) for _ in range(q)]
    g = [[] for _ in range(n)]
    for i in range(n - 1):
        g[ab[i][0] - 1].append(ab[i][1] - 1)
        g[ab[i][1] - 1].append(ab[i][0] - 1)
    ans = [0] * n
    for i in range(q):
        ans[px[i - 1][0] - 1] += px[i - 1][1]
    dfs(g, 0, ans)
    print((*ans))

if __name__ == "__main__":
    solve()

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n, q = list(map(int, readline().split()))
ABPX = list(map(int, read().split()))
AB = iter(ABPX[: n + n - 2])
PX = iter(ABPX[n + n - 2 :])
graph = [[] for _ in range(n + 1)]
for a, b in zip(AB, AB):
    graph[a].append(b)
    graph[b].append(a)
val = [0] * (n + 1)
for p, x in zip(PX, PX):
    val[p] += x
stack = [1]  # queue的なやつ
parent = [0] * (n + 1)
while stack:
    x = stack.pop()  # 根を引っこ抜く
    for y in graph[x]:  # 木を見る
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
        val[y] += val[x]
print((" ".join(map(str, val[1:]))))

-            dfs(tree, i, ans)  # 再帰関数になっている。次の木の中を探索

-    n, q = list(map(int, input().split()))

-    ab = [list(map(int, input().split())) for _ in range(n - 1)]

-    px = [list(map(int, input().split())) for _ in range(q)]

+n, q = list(map(int, readline().split()))


Input:
N = int(eval(input()))

if N % 1000 != 0:

    print((1000 - N % 1000))

else:

    print((0))


Input:
import sys, collections, heapq

F = sys.stdin

def single_input(): return F.readline().strip("\n")

def line_input(): return F.readline().strip("\n").split()

def solve():

    N, Q = list(map(int, line_input()))

    event = []

    for i in range(N):

        s, t, x = list(map(int, line_input()))

        event.append((s-x, 1, x))

        event.append((t-x,-1, x))

    for i in range(Q):

        d = int(single_input())

        event.append((d, 2, d))

    event.sort()

    candidate = []

    heapq.heapify(candidate)

    stop = collections.defaultdict(int)

    ans = ""

    for time, parameter, co in event:

        if parameter == 1:

            heapq.heappush(candidate, co)

            stop[co] += 1

        elif parameter == -1:

            stop[co] -= 1

        else:

            while candidate:

                x = heapq.heappop(candidate)

                if stop[x] > 0:

                    heapq.heappush(candidate, x)

                    ans += str(x) + "\n"

                    break

            else: ans += "-1\n"

    print(ans)

    return 0

if __name__ == "__main__":

    solve()

import sys, collections, heapq

def single_input(F): return F.readline().strip("\n")

def line_input(F): return F.readline().strip("\n").split()

def solve():

    F = sys.stdin

    N, Q = list(map(int, line_input(F)))

    event = []

    for i in range(N):

        s, t, x = list(map(int, line_input(F)))

        event.append((s-x, 1, x))

        event.append((t-x,-1, x))

    for i in range(Q):

        d = int(single_input(F))

        event.append((d, 2, d))

    event.sort()

    candidate = []

    heapq.heapify(candidate)

    stop = collections.defaultdict(int)

    ans = ""

    for time, parameter, co in event:

        if parameter == 1:

            heapq.heappush(candidate, co)

            stop[co] += 1

        elif parameter == -1:

            stop[co] -= 1

        else:

            while candidate:

                x = heapq.heappop(candidate)

                if stop[x] > 0:

                    heapq.heappush(candidate, x)

                    ans += str(x) + "\n"

                    break

            else: ans += "-1\n"

    print(ans)

    return 0

if __name__ == "__main__":

    solve()

import sys, collections, heapq

F = sys.stdin

def single_input():
    return F.readline().strip("\n")

def line_input():
    return F.readline().strip("\n").split()

def solve():
    N, Q = list(map(int, line_input()))
    event = []
    for i in range(N):
        s, t, x = list(map(int, line_input()))
        event.append((s - x, 1, x))
        event.append((t - x, -1, x))
    for i in range(Q):
        d = int(single_input())
        event.append((d, 2, d))
    event.sort()
    candidate = []
    heapq.heapify(candidate)
    stop = collections.defaultdict(int)
    ans = ""
    for time, parameter, co in event:
        if parameter == 1:
            heapq.heappush(candidate, co)
            stop[co] += 1
        elif parameter == -1:
            stop[co] -= 1
        else:
            while candidate:
                x = heapq.heappop(candidate)
                if stop[x] > 0:
                    heapq.heappush(candidate, x)
                    ans += str(x) + "\n"
                    break
            else:
                ans += "-1\n"
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

import sys, collections, heapq

def single_input(F):
    return F.readline().strip("\n")

def line_input(F):
    return F.readline().strip("\n").split()

def solve():
    F = sys.stdin
    N, Q = list(map(int, line_input(F)))
    event = []
    for i in range(N):
        s, t, x = list(map(int, line_input(F)))
        event.append((s - x, 1, x))
        event.append((t - x, -1, x))
    for i in range(Q):
        d = int(single_input(F))
        event.append((d, 2, d))
    event.sort()
    candidate = []
    heapq.heapify(candidate)
    stop = collections.defaultdict(int)
    ans = ""
    for time, parameter, co in event:
        if parameter == 1:
            heapq.heappush(candidate, co)
            stop[co] += 1
        elif parameter == -1:
            stop[co] -= 1
        else:
            while candidate:
                x = heapq.heappop(candidate)
                if stop[x] > 0:
                    heapq.heappush(candidate, x)
                    ans += str(x) + "\n"
                    break
            else:
                ans += "-1\n"
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

-    N, Q = list(map(int, line_input()))

+    N, Q = list(map(int, line_input(F)))

-        s, t, x = list(map(int, line_input()))

+        s, t, x = list(map(int, line_input(F)))


Input:
x = int(eval(input()))

t = 100

count = 0

while True:

  if t >= x:

    print(count)

    exit()

  t = int(t + t*0.01)

  count += 1

x = int(eval(input()))

s = 100

ans = 0

while s < x:

  t = int(s*0.01)

  s = s+t

  ans += 1

print(ans)

x = int(eval(input()))
t = 100
count = 0
while True:
    if t >= x:
        print(count)
        exit()
    t = int(t + t * 0.01)
    count += 1

x = int(eval(input()))
s = 100
ans = 0
while s < x:
    t = int(s * 0.01)
    s = s + t
    ans += 1
print(ans)

-t = 100
-count = 0
-while True:
-    if t >= x:
-        print(count)
-        exit()
-    t = int(t + t * 0.01)
-    count += 1
+s = 100
+ans = 0
+while s < x:
+    t = int(s * 0.01)
+    s = s + t
+    ans += 1
+print(ans)


Input:
N, M, L = list(map(int, input().split()))

edges = [list(map(int, input().split())) for _ in range(M)]

Q = int(eval(input()))

X = [list(map(int, input().split())) for _ in range(Q)]

INF = 10 ** 9 + 6

graph = [[INF] * N for _ in range(N)]

for i in range(N):

    graph[i][i] = 0

for u, v, l in edges:

    graph[u - 1][v - 1] = l

    graph[v - 1][u - 1] = l

for k in range(N):

    for i in range(N):

        if graph[i][k] == INF:

            continue

        for j in range(N):

            if graph[k][j] == INF:

                continue

            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

cnt = [[INF] * N for _ in range(N)]

for i in range(N):

    cnt[i][i] = 0

for i in range(N):

    for j in range(N):

        if i == j:

            continue

        if graph[i][j] <= L:

            cnt[i][j] = 1

            cnt[j][i] = 1

for k in range(N):

    for i in range(N):

        if cnt[i][k] == INF:

            continue

        for j in range(N):

            if cnt[k][j] == INF:

                continue

            cnt[i][j] = min(cnt[i][j], cnt[i][k] + cnt[k][j])

for s, t in X:

    if cnt[s - 1][t - 1] == INF:

        print((-1))

    else:

        print((cnt[s - 1][t - 1] - 1))

N, M, L = list(map(int, input().split()))

X = [list(map(int, input().split())) for _ in range(M)]

Q = int(eval(input()))

Y = [list(map(int, input().split())) for _ in range(Q)]

inf = 10 ** 9 + 7

graph = [[inf] * N for _ in range(N)]

count = [[inf] * N for _ in range(N)]

for i in range(N):

    graph[i][i] = 0

    count[i][i] = 0

for a, b, c in X:

    graph[a - 1][b - 1] = c

    graph[b - 1][a - 1] = c

for k in range(N):

    for i in range(N):

        for j in range(N):

            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

for i in range(N):

    for j in range(N):

        if i != j and graph[i][j] <= L:

            count[i][j] = 1

for k in range(N):

    for i in range(N):

        for j in range(N):

            count[i][j] = min(count[i][j], count[i][k] + count[k][j])

for s, t in Y:

    if count[s - 1][t - 1] == inf:

        print((-1))

    else:

        print((count[s - 1][t - 1] - 1))

N, M, L = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(M)]
Q = int(eval(input()))
X = [list(map(int, input().split())) for _ in range(Q)]
INF = 10**9 + 6
graph = [[INF] * N for _ in range(N)]
for i in range(N):
    graph[i][i] = 0
for u, v, l in edges:
    graph[u - 1][v - 1] = l
    graph[v - 1][u - 1] = l
for k in range(N):
    for i in range(N):
        if graph[i][k] == INF:
            continue
        for j in range(N):
            if graph[k][j] == INF:
                continue
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
cnt = [[INF] * N for _ in range(N)]
for i in range(N):
    cnt[i][i] = 0
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        if graph[i][j] <= L:
            cnt[i][j] = 1
            cnt[j][i] = 1
for k in range(N):
    for i in range(N):
        if cnt[i][k] == INF:
            continue
        for j in range(N):
            if cnt[k][j] == INF:
                continue
            cnt[i][j] = min(cnt[i][j], cnt[i][k] + cnt[k][j])
for s, t in X:
    if cnt[s - 1][t - 1] == INF:
        print((-1))
    else:
        print((cnt[s - 1][t - 1] - 1))

N, M, L = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(M)]
Q = int(eval(input()))
Y = [list(map(int, input().split())) for _ in range(Q)]
inf = 10**9 + 7
graph = [[inf] * N for _ in range(N)]
count = [[inf] * N for _ in range(N)]
for i in range(N):
    graph[i][i] = 0
    count[i][i] = 0
for a, b, c in X:
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c
for k in range(N):
    for i in range(N):
        for j in range(N):
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
for i in range(N):
    for j in range(N):
        if i != j and graph[i][j] <= L:
            count[i][j] = 1
for k in range(N):
    for i in range(N):
        for j in range(N):
            count[i][j] = min(count[i][j], count[i][k] + count[k][j])
for s, t in Y:
    if count[s - 1][t - 1] == inf:
        print((-1))
    else:
        print((count[s - 1][t - 1] - 1))

-edges = [list(map(int, input().split())) for _ in range(M)]

+X = [list(map(int, input().split())) for _ in range(M)]

-X = [list(map(int, input().split())) for _ in range(Q)]

+Y = [list(map(int, input().split())) for _ in range(Q)]

+        if i != j and graph[i][j] <= L:

-            cnt[i][j] = min(cnt[i][j], cnt[i][k] + cnt[k][j])

+            count[i][j] = min(count[i][j], count[i][k] + count[k][j])


Input:
n = int(eval(input()))

tp = hp = 0

for nc in range(n):

    (tc, hc) = input().split()

    if tc == hc:

        tp += 1

        hp += 1

    elif tc > hc:

        tp += 3

    elif tc < hc:

        hp += 3

print((tp, hp))

n = int(eval(input()))

taro = 0

hanako = 0

for ni in range(n):

    t, h = input().split()

    if t < h:

        hanako += 3

    elif t > h:

        taro += 3

    else:

        hanako += 1

        taro += 1

print((taro, hanako))

-tp = hp = 0
-for nc in range(n):
-    (tc, hc) = input().split()
-    if tc == hc:
-        tp += 1
-        hp += 1
-    elif tc > hc:
-        tp += 3
-    elif tc < hc:
-        hp += 3
-print((tp, hp))
+taro = 0
+hanako = 0
+for ni in range(n):
+    t, h = input().split()
+    if t < h:
+        hanako += 3
+    elif t > h:
+        taro += 3
+    else:
+        hanako += 1
+        taro += 1
+print((taro, hanako))


Input:
def fib(n):

    a, b = 0, 1

    if n == 1:

        return a

    elif n == 2:

        return b

    else:

        for i in range(n-2):

            a, b = b, a + b

        return b

N, M = list(map(int, input().split()))

a = [-1]

for i in range(M):

    a.append(int(eval(input())))

for i in range(M-1):

    if a[i+1] == (a[i] + 1):

        print((0))

        exit()

combi = 1

for i in range(len(a)-1):

    dis = a[i+1] - a[i]

    #print(dis)

    combi *= fib(dis)

    #print(a[i])

dis = N+1 - a[-1]

#print(dis)

combi *= fib(dis)

print((combi % 1000000007))

N, M = list(map(int, input().split()))

# 壊れている床

a = [-1]

for i in range(M):

  a.append(int(eval(input())))

  # 連続して壊れていたら辿り着けない

  if a[i + 1] - a[i] == 1:

    print((0))

    exit()

# フィボナッチ数列

def fib(n):

  a, b = 0, 1

  if n == 1:

    return a

  elif n == 2:

    return b

  else:

    for i in range(n-2):

      a, b = b, a + b

    return b

# 階段を登る組み合わせ

combi = 1

for i in range(len(a) - 1):

  # 次の穴との距離

  dis = a[i+1] - a[i]

  #print(dis)

  combi *= fib(dis)

  #print(a[i])

dis = N+1 - a[-1]

#print(dis)

combi *= fib(dis)

print((combi % 1000000007))

def fib(n):
    a, b = 0, 1
    if n == 1:
        return a
    elif n == 2:
        return b
    else:
        for i in range(n - 2):
            a, b = b, a + b
        return b

N, M = list(map(int, input().split()))
a = [-1]
for i in range(M):
    a.append(int(eval(input())))
for i in range(M - 1):
    if a[i + 1] == (a[i] + 1):
        print((0))
        exit()
combi = 1
for i in range(len(a) - 1):
    dis = a[i + 1] - a[i]
    # print(dis)
    combi *= fib(dis)
    # print(a[i])
dis = N + 1 - a[-1]
# print(dis)
combi *= fib(dis)
print((combi % 1000000007))

N, M = list(map(int, input().split()))
# 壊れている床
a = [-1]
for i in range(M):
    a.append(int(eval(input())))
    # 連続して壊れていたら辿り着けない
    if a[i + 1] - a[i] == 1:
        print((0))
        exit()
# フィボナッチ数列
def fib(n):
    a, b = 0, 1
    if n == 1:
        return a
    elif n == 2:
        return b
    else:
        for i in range(n - 2):
            a, b = b, a + b
        return b

# 階段を登る組み合わせ
combi = 1
for i in range(len(a) - 1):
    # 次の穴との距離
    dis = a[i + 1] - a[i]
    # print(dis)
    combi *= fib(dis)
    # print(a[i])
dis = N + 1 - a[-1]
# print(dis)
combi *= fib(dis)
print((combi % 1000000007))

+N, M = list(map(int, input().split()))
+# 壊れている床
+a = [-1]
+for i in range(M):
+    a.append(int(eval(input())))
+    # 連続して壊れていたら辿り着けない
+    if a[i + 1] - a[i] == 1:
+        print((0))
+        exit()
+# フィボナッチ数列
-N, M = list(map(int, input().split()))
-a = [-1]
-for i in range(M):
-    a.append(int(eval(input())))
-for i in range(M - 1):
-    if a[i + 1] == (a[i] + 1):
-        print((0))
-        exit()
+# 階段を登る組み合わせ
+    # 次の穴との距離


Input:
#!/usr/bin/env python3

import sys

from collections import Counter

sys.setrecursionlimit(10**8)

INF = float("inf")

def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))

    Q = int(eval(input()))

    B, C = [0]*Q, [0]*Q

    for i in range(Q):

        B[i], C[i] = list(map(int, input().split()))

    # 個数を持つ

    # Biの個数 O(1),C[i]の個数 O(1) 更新 O(1) 総和O(1)

    count = Counter(A)

    tot = sum(A)

    for i in range(Q):

        b = count[B[i]]

        c = count[C[i]]

        count[C[i]] += b

        count[B[i]] = 0

        tot = tot - B[i]*b + C[i]*b

        print(tot)

    return

if __name__ == '__main__':

    main()

#!/usr/bin/env python3

import sys

from collections import Counter

sys.setrecursionlimit(10**8)

INF = float("inf")

class UnionFind():

    def __init__(self, n):

        self.n = n

        self.parents = [-1] * n

    def find(self, x):

        if self.parents[x] < 0:

            return x

        else:

            self.parents[x] = self.find(self.parents[x])

            return self.parents[x]

    def union(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

        if self.parents[x] > self.parents[y]:

            x, y = y, x

        self.parents[x] += self.parents[y]

        self.parents[y] = x

    def size(self, x):

        return -self.parents[self.find(x)]

    def same(self, x, y):

        return self.find(x) == self.find(y)

    def members(self, x):

        root = self.find(x)

        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):

        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):

        return len(self.roots())

    def all_group_members(self):

        return {r: self.members(r) for r in self.roots()}

    def __str__(self):

        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))

    Q = int(eval(input()))

    B, C = [0]*Q, [0]*Q

    for i in range(Q):

        B[i], C[i] = list(map(int, input().split()))

    # 個数を持つ

    # Biの個数 O(1),C[i]の個数 O(1) 更新 O(1) 総和O(1)

    group = [None]*(10**5+1)

    uf = UnionFind(N)

    # union_find

    # N個の数に対して、i番目の数がどのグループに属するのかを示す

    # group

    # j (< 10**5)に対して、jがつけられているグループの根の番号を示す

    for i, a in enumerate(A):

        # グループにつけられている番号がaであるようなグループはあるか？

        # なければ自信のグループにつけられている番号として保持

        # あればそこに合流

        if group[a] is None:

            group[a] = i

        else:

            uf.union(i, group[a])

            group[a] = uf.find(i)

    # print(A)

    # print(group)

    # print(uf.parents)

    tot = sum(A)

    for i in range(Q):

        if group[B[i]] is None:

            b = 0

        elif group[C[i]] is None:

            b = uf.size(group[B[i]])

            group[C[i]] = uf.find(group[B[i]])

            group[B[i]] = None

        else:

            b = uf.size(group[B[i]])

            uf.union(group[B[i]], group[C[i]])

            group[C[i]] = uf.find(group[B[i]])

            group[B[i]] = None

        tot = tot - B[i]*b + C[i]*b

        print(tot)

        # print(group)

        # print(uf.parents)

    return

if __name__ == '__main__':

    main()

#!/usr/bin/env python3
import sys
from collections import Counter

sys.setrecursionlimit(10**8)
INF = float("inf")

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    B, C = [0] * Q, [0] * Q
    for i in range(Q):
        B[i], C[i] = list(map(int, input().split()))
    # 個数を持つ
    # Biの個数 O(1),C[i]の個数 O(1) 更新 O(1) 総和O(1)
    count = Counter(A)
    tot = sum(A)
    for i in range(Q):
        b = count[B[i]]
        c = count[C[i]]
        count[C[i]] += b
        count[B[i]] = 0
        tot = tot - B[i] * b + C[i] * b
        print(tot)
    return

if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys
from collections import Counter

sys.setrecursionlimit(10**8)
INF = float("inf")

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    B, C = [0] * Q, [0] * Q
    for i in range(Q):
        B[i], C[i] = list(map(int, input().split()))
    # 個数を持つ
    # Biの個数 O(1),C[i]の個数 O(1) 更新 O(1) 総和O(1)
    group = [None] * (10**5 + 1)
    uf = UnionFind(N)
    # union_find
    # N個の数に対して、i番目の数がどのグループに属するのかを示す
    # group
    # j (< 10**5)に対して、jがつけられているグループの根の番号を示す
    for i, a in enumerate(A):
        # グループにつけられている番号がaであるようなグループはあるか？
        # なければ自信のグループにつけられている番号として保持
        # あればそこに合流
        if group[a] is None:
            group[a] = i
        else:
            uf.union(i, group[a])
            group[a] = uf.find(i)
    # print(A)
    # print(group)
    # print(uf.parents)
    tot = sum(A)
    for i in range(Q):
        if group[B[i]] is None:
            b = 0
        elif group[C[i]] is None:
            b = uf.size(group[B[i]])
            group[C[i]] = uf.find(group[B[i]])
            group[B[i]] = None
        else:
            b = uf.size(group[B[i]])
            uf.union(group[B[i]], group[C[i]])
            group[C[i]] = uf.find(group[B[i]])
            group[B[i]] = None
        tot = tot - B[i] * b + C[i] * b
        print(tot)
        # print(group)
        # print(uf.parents)
    return

if __name__ == "__main__":
    main()

+            self.parents[x] = self.find(self.parents[x])

+        if self.parents[x] > self.parents[y]:

+        self.parents[x] += self.parents[y]

+        return -self.parents[self.find(x)]

+        return self.find(x) == self.find(y)

+        return [i for i in range(self.n) if self.find(i) == root]

+        return [i for i, x in enumerate(self.parents) if x < 0]

+        return {r: self.members(r) for r in self.roots()}

+        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())

+    # N個の数に対して、i番目の数がどのグループに属するのかを示す

+    # j (< 10**5)に対して、jがつけられているグループの根の番号を示す

+        # グループにつけられている番号がaであるようなグループはあるか？

+        # なければ自信のグループにつけられている番号として保持

+        # あればそこに合流

+            group[C[i]] = uf.find(group[B[i]])

+            uf.union(group[B[i]], group[C[i]])

+            group[C[i]] = uf.find(group[B[i]])


Input:
def euclid(a, b):

    global l

    if b == 0:

        return a

    else:

        ch = l[a][b]

        if ch == 0:

            return euclid(b, a%b)

        else:

            return ch

def gcd(a, b, c):

    global l

    d = l[c][b]

    if d == 0:

        d = euclid(c, b)

        l[c][b] = d

    e = l[max(d, a)][min(d, a)]

    if e == 0:

        e = euclid(max(d, a), min(d, a))

        l[max(d, a)][min(d, a)] = e

    return e

K = int(eval(input()))

ans = 0

l = []

for i in range(K+1):

    l.append([0]*(K+1))

for i in range(1, K+1):

    for j in range(i, K+1):

        for k in range(j, K+1):

            num = gcd(i, j, k)

            if i == j:

                if j == k:

                    ans += num

                else:

                    ans += 3*num

            else:

                if j == k:

                    ans += 3*num

                else:

                    ans += 6*num

print(ans)

import math

K = int(eval(input()))

ans = 0

l = []

for i in range(K+1):

    l.append([0]*(K+1))

for i in range(1, K+1):

    for j in range(i, K+1):

        for k in range(j, K+1):

            num = math.gcd(math.gcd(i, j), k)

            if i == j:

                if j == k:

                    ans += num

                else:

                    ans += 3*num

            else:

                if j == k:

                    ans += 3*num

                else:

                    ans += 6*num

print(ans)

def euclid(a, b):
    global l
    if b == 0:
        return a
    else:
        ch = l[a][b]
        if ch == 0:
            return euclid(b, a % b)
        else:
            return ch

def gcd(a, b, c):
    global l
    d = l[c][b]
    if d == 0:
        d = euclid(c, b)
        l[c][b] = d
    e = l[max(d, a)][min(d, a)]
    if e == 0:
        e = euclid(max(d, a), min(d, a))
        l[max(d, a)][min(d, a)] = e
    return e

K = int(eval(input()))
ans = 0
l = []
for i in range(K + 1):
    l.append([0] * (K + 1))
for i in range(1, K + 1):
    for j in range(i, K + 1):
        for k in range(j, K + 1):
            num = gcd(i, j, k)
            if i == j:
                if j == k:
                    ans += num
                else:
                    ans += 3 * num
            else:
                if j == k:
                    ans += 3 * num
                else:
                    ans += 6 * num
print(ans)

import math

K = int(eval(input()))
ans = 0
l = []
for i in range(K + 1):
    l.append([0] * (K + 1))
for i in range(1, K + 1):
    for j in range(i, K + 1):
        for k in range(j, K + 1):
            num = math.gcd(math.gcd(i, j), k)
            if i == j:
                if j == k:
                    ans += num
                else:
                    ans += 3 * num
            else:
                if j == k:
                    ans += 3 * num
                else:
                    ans += 6 * num
print(ans)

-        e = euclid(max(d, a), min(d, a))

+            num = math.gcd(math.gcd(i, j), k)


Input:
colors = input().split()

print((len(set(colors))))

abc = input().split()

print((len(set(abc))))

-colors = input().split()
-print((len(set(colors))))
+abc = input().split()
+print((len(set(abc))))


Input:
N,K = list(map(int,input().split()))

A = list([int(x)-1 for x in input().split()])

o = [0] * 63

def bi(x):

    i = 0

    while x != 0:

        o[i] = x%2

        x //= 2

        i += 1

bi(K)

n = 0

for i in range(63):

    if o[i]:

        n = A[n]

    A = [A[A[x]] for x in range(N)]

print((n+1))

N,K = list(map(int,input().split()))

A = list([int(x)-1 for x in input().split()])

B = [0]*N

o = [0] * 63

def bi(x):

    i = 0

    while x != 0:

        o[i] = x%2

        x //= 2

        i += 1

bi(K)

n = 0

for i in range(63):

    if o[i]:

        n = A[n]

    for j in range(N):

        B[j] = A[A[j]]

    for j in range(N):

        A[j] = B[j]

print((n+1))

N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
o = [0] * 63

def bi(x):
    i = 0
    while x != 0:
        o[i] = x % 2
        x //= 2
        i += 1

bi(K)
n = 0
for i in range(63):
    if o[i]:
        n = A[n]
    A = [A[A[x]] for x in range(N)]
print((n + 1))

N, K = list(map(int, input().split()))
A = list([int(x) - 1 for x in input().split()])
B = [0] * N
o = [0] * 63

def bi(x):
    i = 0
    while x != 0:
        o[i] = x % 2
        x //= 2
        i += 1

bi(K)
n = 0
for i in range(63):
    if o[i]:
        n = A[n]
    for j in range(N):
        B[j] = A[A[j]]
    for j in range(N):
        A[j] = B[j]
print((n + 1))

+B = [0] * N
-    A = [A[A[x]] for x in range(N)]
+    for j in range(N):
+        B[j] = A[A[j]]
+    for j in range(N):
+        A[j] = B[j]


Input:
n,m = list(map(int, input().split()))

ab = [list(map(int, input().split())) for _ in range(n)]

ab.sort()

money = 0

drink = 0

for a,b in ab:

  money += a*b

  drink += b

  if drink >= m:

    money -= (drink-m) * a

    break

print(money)

n,m = list(map(int, input().split()))

ab = [list(map(int, input().split())) for _ in range(n)]

ab.sort()

money = 0

drink = 0

for a,b in ab:

    if drink+b <= m:

        drink += b

        money += a*b

    else:

        money += (m-drink) * a

        drink = m

    if drink == m: break

print(money)

n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
money = 0
drink = 0
for a, b in ab:
    money += a * b
    drink += b
    if drink >= m:
        money -= (drink - m) * a
        break
print(money)

n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort()
money = 0
drink = 0
for a, b in ab:
    if drink + b <= m:
        drink += b
        money += a * b
    else:
        money += (m - drink) * a
        drink = m
    if drink == m:
        break
print(money)

-    money += a * b
-    drink += b
-    if drink >= m:
-        money -= (drink - m) * a
+    if drink + b <= m:
+        drink += b
+        money += a * b
+    else:
+        money += (m - drink) * a
+        drink = m
+    if drink == m:


Input:
K = int(eval(input()))

lunlun_number_list = []

def dfs(N):

    lunlun_number_list.append(N)

    if len(str(N)) > 10:

        return

    now_int = int(str(N)[-1])

    if now_int == 0:

        for i in range(2):

            dfs(N*10 + i)

    elif now_int == 9:

        for i in range(8, 10, 1):

            dfs(N*10 + i)

    else:

        for i in range(now_int-1, now_int+2, 1):

            dfs(N*10 + i)

for i in range(1, 10, 1):

    dfs(i)

lunlun_number_list.sort()

print((lunlun_number_list[K-1]))

import collections

K = int(eval(input()))

que = collections.deque()

for i in range(1, 10, 1):

    que.append(i)

cnt = 0

for i in range(K):

    x = que.popleft()

    cnt += 1

    if cnt == K:

        print(x)

        exit()

    if x%10 == 0:

        que.append(10*x+0)

        que.append(10*x+1)

    elif x%10 == 9:

        que.append(10*x+8)

        que.append(10*x+9)

    else:

        que.append(10*x+(x%10)-1)

        que.append(10*x+(x%10)+0)

        que.append(10*x+(x%10)+1)

K = int(eval(input()))
lunlun_number_list = []

def dfs(N):
    lunlun_number_list.append(N)
    if len(str(N)) > 10:
        return
    now_int = int(str(N)[-1])
    if now_int == 0:
        for i in range(2):
            dfs(N * 10 + i)
    elif now_int == 9:
        for i in range(8, 10, 1):
            dfs(N * 10 + i)
    else:
        for i in range(now_int - 1, now_int + 2, 1):
            dfs(N * 10 + i)

for i in range(1, 10, 1):
    dfs(i)
lunlun_number_list.sort()
print((lunlun_number_list[K - 1]))

import collections

K = int(eval(input()))
que = collections.deque()
for i in range(1, 10, 1):
    que.append(i)
cnt = 0
for i in range(K):
    x = que.popleft()
    cnt += 1
    if cnt == K:
        print(x)
        exit()
    if x % 10 == 0:
        que.append(10 * x + 0)
        que.append(10 * x + 1)
    elif x % 10 == 9:
        que.append(10 * x + 8)
        que.append(10 * x + 9)
    else:
        que.append(10 * x + (x % 10) - 1)
        que.append(10 * x + (x % 10) + 0)
        que.append(10 * x + (x % 10) + 1)

-        for i in range(now_int - 1, now_int + 2, 1):


Input:
n, h = list(map(int, input().split()))

a, b = [], []

for _ in range(n):

    ai, bi = list(map(int, input().split()))

    a.append(ai), b.append(bi)

a.sort(), b.sort()

ans, amax = 0, a[-1]

for bi in b[::-1]:

    if bi <= amax or h<=0:

        break

    h -= bi

    ans += 1

print((ans + ((h + amax - 1) // amax) * (h > 0)))

n, h = list(map(int, input().split()))

a, b = [], []

for _ in range(n):

    ai, bi = list(map(int, input().split()))

    a.append(ai), b.append(bi)

a.sort(), b.sort()

ans, amax = 0, a[-1]

for bi in b[::-1]:

    if bi <= amax or h <= 0:

        break

    h -= bi

    ans += 1

print((ans + (h + amax - 1) // amax if h > 0 else ans))

n, h = list(map(int, input().split()))
a, b = [], []
for _ in range(n):
    ai, bi = list(map(int, input().split()))
    a.append(ai), b.append(bi)
a.sort(), b.sort()
ans, amax = 0, a[-1]
for bi in b[::-1]:
    if bi <= amax or h <= 0:
        break
    h -= bi
    ans += 1
print((ans + ((h + amax - 1) // amax) * (h > 0)))

+print((ans + (h + amax - 1) // amax if h > 0 else ans))


Input:
def judge(a):

    global flg

    if a % 4 == 0 or a % 7 == 0:

        flg = 1

N = int(eval(input()))

flg = 0

judge(N)

while N > 7:

    N -= 7

    judge(N)

if flg == 1:

    print("Yes")

else:

    print("No")

N = int(eval(input()))

f = 0

if N % 4 == 0 or N % 7 == 0:

    f = 1

else:

    while N > 4:

        N -= 4

        if N % 7 == 0:

            f = 1

            break

if f == 0:

    print("No")

else:

    print("Yes")

-def judge(a):
-    global flg
-    if a % 4 == 0 or a % 7 == 0:
-        flg = 1
-flg = 0
-judge(N)
-while N > 7:
-    N -= 7
-    judge(N)
-if flg == 1:
+f = 0
+if N % 4 == 0 or N % 7 == 0:
+    f = 1
+else:
+    while N > 4:
+        N -= 4
+        if N % 7 == 0:
+            f = 1
+            break
+if f == 0:
+    print("No")
+else:
-else:
-    print("No")


Input:
from  collections import Counter

def main():

    num, avg = list(map(int, input().split()))

    data = list(map(int, input().split()))

    dp = [[[0 for i in range(6000)] for j in range(num + 1)] for k in range(num + 1)]

    dp[0][0][0] = 1

    for i in range(1, num + 1):

        now_card = data[i - 1]

        for j in range(num + 1):

            for k in range(6000):

                # print(i, j, k)

                dp[i][j][k] += dp[i - 1][j][k]

                if k - now_card >= 0 and j - 1 >= 0:

                    dp[i][j][k] += dp[i - 1][j - 1][k - now_card]

        # print(dp[i][j])

    ans = 0

    for i in range(1, num + 1):

        ans += dp[num][i][i * avg]

    print(ans)

if __name__ == '__main__':

    main()

from  collections import Counter

def main():

    num, avg = list(map(int, input().split()))

    data = list(map(int, input().split()))

    max_num = max(num, avg, max(data))

    dp = [[[0 for i in range(max_num ** 2 + 1)] for j in range(num + 1)] for k in range(num + 1)]

    dp[0][0][0] = 1

    for i in range(1, num + 1):

        now_card = data[i - 1]

        for j in range(num + 1):

            for k in range(max_num ** 2 + 1):

                # print(i, j, k)

                dp[i][j][k] += dp[i - 1][j][k]

                if k - now_card >= 0 and j - 1 >= 0:

                    dp[i][j][k] += dp[i - 1][j - 1][k - now_card]

        # print(dp[i][j])

    ans = 0

    for i in range(1, num + 1):

        ans += dp[num][i][i * avg]

    print(ans)

if __name__ == '__main__':

    main()

from collections import Counter

def main():
    num, avg = list(map(int, input().split()))
    data = list(map(int, input().split()))
    dp = [[[0 for i in range(6000)] for j in range(num + 1)] for k in range(num + 1)]
    dp[0][0][0] = 1
    for i in range(1, num + 1):
        now_card = data[i - 1]
        for j in range(num + 1):
            for k in range(6000):
                # print(i, j, k)
                dp[i][j][k] += dp[i - 1][j][k]
                if k - now_card >= 0 and j - 1 >= 0:
                    dp[i][j][k] += dp[i - 1][j - 1][k - now_card]
        # print(dp[i][j])
    ans = 0
    for i in range(1, num + 1):
        ans += dp[num][i][i * avg]
    print(ans)

if __name__ == "__main__":
    main()

from collections import Counter

def main():
    num, avg = list(map(int, input().split()))
    data = list(map(int, input().split()))
    max_num = max(num, avg, max(data))
    dp = [
        [[0 for i in range(max_num**2 + 1)] for j in range(num + 1)]
        for k in range(num + 1)
    ]
    dp[0][0][0] = 1
    for i in range(1, num + 1):
        now_card = data[i - 1]
        for j in range(num + 1):
            for k in range(max_num**2 + 1):
                # print(i, j, k)
                dp[i][j][k] += dp[i - 1][j][k]
                if k - now_card >= 0 and j - 1 >= 0:
                    dp[i][j][k] += dp[i - 1][j - 1][k - now_card]
        # print(dp[i][j])
    ans = 0
    for i in range(1, num + 1):
        ans += dp[num][i][i * avg]
    print(ans)

if __name__ == "__main__":
    main()

-    dp = [[[0 for i in range(6000)] for j in range(num + 1)] for k in range(num + 1)]

+        [[0 for i in range(max_num**2 + 1)] for j in range(num + 1)]

+            for k in range(max_num**2 + 1):


Input:
from collections import deque

from heapq import heappush,heappop

import re

def int_raw():

    return int(eval(input()))

def ss_raw():

    return input().split()

def ints_raw():

    return list(map(int, ss_raw()))

INF = 1<<29

DIV = 10**9+7

def mod_inv(a,mod = DIV):

    return pow(a,mod-2,mod)

def ncr(n,r, mod =DIV):

	r = min(r,n-r)

	ret = 1

	for i in range(1,r+1):

		ret = ret * (n-i+1) % mod

		ret = ret * pow(i, mod-2, mod) % mod

	return ret

def main():

    N, M,K = ints_raw()

    ans = 0

    for d in range(1,N):

        ans =(ans+d*(N-d)*M*M)%DIV

    for d in range(1,M):

        ans =(ans+d*(M-d)*N*N)%DIV

    return (ans*ncr(N*M-2,K-2))%DIV

print((main()))

from collections import deque

from heapq import heappush,heappop

import re

def int_raw():

    return int(eval(input()))

def ss_raw():

    return input().split()

def ints_raw():

    return list(map(int, ss_raw()))

INF = 1<<29

DIV = 10**9+7

def mod_inv_prime(a,mod = DIV):

    return pow(a,mod-2,mod)

def mod_inv(a,b):

    r = a

    w = b

    u = 1

    v = 0

    while w!=0:

        t = r//w

        r -=t*w

        r,w = w,r

        u -= t*v

        u,v = v,u

    return (u%b+b)%b

def ncr(n,r, mod =DIV):

	r = min(r,n-r)

	ret = 1

	for i in range(1,r+1):

		ret = ret * (n-i+1) % mod

		ret = ret * mod_inv(i, mod) % mod

	return ret

def main():

    N, M,K = ints_raw()

    ans = 0

    for d in range(1,N):

        ans =(ans+d*(N-d)*M*M)%DIV

    for d in range(1,M):

        ans =(ans+d*(M-d)*N*N)%DIV

    return (ans*ncr(N*M-2,K-2))%DIV

if __name__ =="__main__":

    print((main()))

from collections import deque
from heapq import heappush, heappop
import re

def int_raw():
    return int(eval(input()))

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, ss_raw()))

INF = 1 << 29
DIV = 10**9 + 7

def mod_inv(a, mod=DIV):
    return pow(a, mod - 2, mod)

def ncr(n, r, mod=DIV):
    r = min(r, n - r)
    ret = 1
    for i in range(1, r + 1):
        ret = ret * (n - i + 1) % mod
        ret = ret * pow(i, mod - 2, mod) % mod
    return ret

def main():
    N, M, K = ints_raw()
    ans = 0
    for d in range(1, N):
        ans = (ans + d * (N - d) * M * M) % DIV
    for d in range(1, M):
        ans = (ans + d * (M - d) * N * N) % DIV
    return (ans * ncr(N * M - 2, K - 2)) % DIV

print((main()))

from collections import deque
from heapq import heappush, heappop
import re

def int_raw():
    return int(eval(input()))

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, ss_raw()))

INF = 1 << 29
DIV = 10**9 + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod - 2, mod)

def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r // w
        r -= t * w
        r, w = w, r
        u -= t * v
        u, v = v, u
    return (u % b + b) % b

def ncr(n, r, mod=DIV):
    r = min(r, n - r)
    ret = 1
    for i in range(1, r + 1):
        ret = ret * (n - i + 1) % mod
        ret = ret * mod_inv(i, mod) % mod
    return ret

def main():
    N, M, K = ints_raw()
    ans = 0
    for d in range(1, N):
        ans = (ans + d * (N - d) * M * M) % DIV
    for d in range(1, M):
        ans = (ans + d * (M - d) * N * N) % DIV
    return (ans * ncr(N * M - 2, K - 2)) % DIV

if __name__ == "__main__":
    print((main()))

-        ret = ret * pow(i, mod - 2, mod) % mod

+        ret = ret * mod_inv(i, mod) % mod


Input:
# https://atcoder.jp/contests/abc084/tasks/abc084_d

# D - 2017-like Number

import sys

sys.setrecursionlimit(10 ** 7)

f_inf = float('inf')

mod = 10 ** 9 + 7

# 素数判定

def is_prime(n):

    if n == 1:

        return False

    for k in range(2, int(pow(n, 0.5)) + 1):

        if n % k == 0:

            return False

    return True

def resolve():

    q = int(eval(input()))

    LR = [list(map(int, input().split())) for _ in range(q)]

    # 1であれば2017に似た整数、0であれば2017に似ていない数字

    Lile_2017 = [0] * (10 ** 5 + 1)

    # 素数判定済み配列

    prime = set()

    not_prime = set()

    # 1~100000が2017に似た数かどうか判定する

    for n in range(1, 10 ** 5 + 1):

        flg1, flg2 = False, False

        if n % 2 != 0:

            if n in prime:

                flg1 = True

            elif n in not_prime:

                flg1 = False

            else:

                flg1 = is_prime(n)

                prime.add(n) if flg1 else not_prime.add(n)

            if (n + 1) // 2 in prime:

                flg2 = True

            elif (n + 1) // 2 in not_prime:

                flg2 = False

            else:

                flg2 = is_prime((n + 1) // 2)

                prime.add((n + 1) // 2) if flg1 else not_prime.add((n + 1) // 2)

        Lile_2017[n - 1] += 1 if flg1 and flg2 else 0

    # 2017に似た数の累積和配列を作る

    Ruiseki = [0]

    for i in range(1, 10 ** 5 + 1):

        Ruiseki.append(Ruiseki[-1] + Lile_2017[i - 1])

    # クエリに答える

    for l, r in LR:

        print((Ruiseki[r] - Ruiseki[l - 1]))

if __name__ == '__main__':

    resolve()

import sys

sys.setrecursionlimit(10 ** 7)

f_inf = float('inf')

mod = 10 ** 9 + 7

def resolve():

    def is_prime(n):

        if n == 1:

            return False

        for k in range(2, int(pow(n, 0.5)) + 1):

            if n % k == 0:

                return False

        return True

    q = int(eval(input()))

    primes = set()

    nums = set()

    for i in range(2, 10 ** 5 + 1):

        if is_prime(i):

            primes.add(i)

            if (i + 1) // 2 in primes:

                nums.add(i)

    cnt = [0]

    for i in range(1, 10 ** 5 + 1):

        op = 1 if i in nums else 0

        cnt.append(cnt[-1] + op)

    for _ in range(q):

        l, r = list(map(int, input().split()))

        res = cnt[r] - cnt[l - 1]

        print(res)

if __name__ == '__main__':

    resolve()

# https://atcoder.jp/contests/abc084/tasks/abc084_d
# D - 2017-like Number
import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7
# 素数判定
def is_prime(n):
    if n == 1:
        return False
    for k in range(2, int(pow(n, 0.5)) + 1):
        if n % k == 0:
            return False
    return True

def resolve():
    q = int(eval(input()))
    LR = [list(map(int, input().split())) for _ in range(q)]
    # 1であれば2017に似た整数、0であれば2017に似ていない数字
    Lile_2017 = [0] * (10**5 + 1)
    # 素数判定済み配列
    prime = set()
    not_prime = set()
    # 1~100000が2017に似た数かどうか判定する
    for n in range(1, 10**5 + 1):
        flg1, flg2 = False, False
        if n % 2 != 0:
            if n in prime:
                flg1 = True
            elif n in not_prime:
                flg1 = False
            else:
                flg1 = is_prime(n)
                prime.add(n) if flg1 else not_prime.add(n)
            if (n + 1) // 2 in prime:
                flg2 = True
            elif (n + 1) // 2 in not_prime:
                flg2 = False
            else:
                flg2 = is_prime((n + 1) // 2)
                prime.add((n + 1) // 2) if flg1 else not_prime.add((n + 1) // 2)
        Lile_2017[n - 1] += 1 if flg1 and flg2 else 0
    # 2017に似た数の累積和配列を作る
    Ruiseki = [0]
    for i in range(1, 10**5 + 1):
        Ruiseki.append(Ruiseki[-1] + Lile_2017[i - 1])
    # クエリに答える
    for l, r in LR:
        print((Ruiseki[r] - Ruiseki[l - 1]))

if __name__ == "__main__":
    resolve()

import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7

def resolve():
    def is_prime(n):
        if n == 1:
            return False
        for k in range(2, int(pow(n, 0.5)) + 1):
            if n % k == 0:
                return False
        return True

    q = int(eval(input()))
    primes = set()
    nums = set()
    for i in range(2, 10**5 + 1):
        if is_prime(i):
            primes.add(i)
            if (i + 1) // 2 in primes:
                nums.add(i)
    cnt = [0]
    for i in range(1, 10**5 + 1):
        op = 1 if i in nums else 0
        cnt.append(cnt[-1] + op)
    for _ in range(q):
        l, r = list(map(int, input().split()))
        res = cnt[r] - cnt[l - 1]
        print(res)

if __name__ == "__main__":
    resolve()

-    for k in range(2, int(pow(n, 0.5)) + 1):

+        for k in range(2, int(pow(n, 0.5)) + 1):

-    LR = [list(map(int, input().split())) for _ in range(q)]

-    # 1であれば2017に似た整数、0であれば2017に似ていない数字

-    # 素数判定済み配列

-                prime.add(n) if flg1 else not_prime.add(n)

-            elif (n + 1) // 2 in not_prime:

-                prime.add((n + 1) // 2) if flg1 else not_prime.add((n + 1) // 2)

-        Lile_2017[n - 1] += 1 if flg1 and flg2 else 0

-    # 2017に似た数の累積和配列を作る

-        Ruiseki.append(Ruiseki[-1] + Lile_2017[i - 1])

-    # クエリに答える

-        print((Ruiseki[r] - Ruiseki[l - 1]))

+        l, r = list(map(int, input().split()))


Input:
def gcd(x, y):

    if y > x:

        x, y = y, x

    while y:

        x, y = y, x % y

    return x

N=int(eval(input()))

a=list(map(int,input().split()))

l=[0]*N

l[0]=a[0]

for i in range(1,N):

    l[i]=gcd(l[i-1],a[i])

r=[0]*N

r[N-1]=a[N-1]

for i in range(N-2,-1,-1):

    r[i]=gcd(r[i+1],a[i])

ans=max(r[1],l[N-2])

for i in range(1,N-1):

    tmp=gcd(l[i-1],r[i+1])

    ans=max(tmp,ans)

print(ans)

def gcd(x, y):

    if y > x:

        x, y = y, x

    while y:

        x, y = y, x % y

    return x

N=int(eval(input()))

a=list(map(int,input().split()))

l=[0]*N

l[0]=a[0]

for i in range(1,N):

    l[i]=gcd(l[i-1],a[i])

r=[0]*N

r[N-1]=a[N-1]

for i in range(N-2,-1,-1):

    r[i]=gcd(r[i+1],a[i])

ans=max(r[1],l[N-2])

for i in range(1,N-1):

    tmp=gcd(l[i-1],r[i+1])

    if tmp>ans: ans=tmp

print(ans)

def gcd(x, y):
    if y > x:
        x, y = y, x
    while y:
        x, y = y, x % y
    return x

N = int(eval(input()))
a = list(map(int, input().split()))
l = [0] * N
l[0] = a[0]
for i in range(1, N):
    l[i] = gcd(l[i - 1], a[i])
r = [0] * N
r[N - 1] = a[N - 1]
for i in range(N - 2, -1, -1):
    r[i] = gcd(r[i + 1], a[i])
ans = max(r[1], l[N - 2])
for i in range(1, N - 1):
    tmp = gcd(l[i - 1], r[i + 1])
    ans = max(tmp, ans)
print(ans)

def gcd(x, y):
    if y > x:
        x, y = y, x
    while y:
        x, y = y, x % y
    return x

N = int(eval(input()))
a = list(map(int, input().split()))
l = [0] * N
l[0] = a[0]
for i in range(1, N):
    l[i] = gcd(l[i - 1], a[i])
r = [0] * N
r[N - 1] = a[N - 1]
for i in range(N - 2, -1, -1):
    r[i] = gcd(r[i + 1], a[i])
ans = max(r[1], l[N - 2])
for i in range(1, N - 1):
    tmp = gcd(l[i - 1], r[i + 1])
    if tmp > ans:
        ans = tmp
print(ans)

-    ans = max(tmp, ans)
+    if tmp > ans:
+        ans = tmp


Input:
import sys

sys.setrecursionlimit(10**9)

MOD=10**9+7

n,k=list(map(int,input().split()))

if k>=n-1:

  k=n-1

inv_table = [0]+[1]

for i in range(2,k+1):

  inv_table+=[inv_table[MOD%i]*(MOD-int(MOD/i))%MOD]

comb_sum=1

fact=1

comb1=1

comb2=1

for i in range(1,k+1):

  comb1=(comb1*(n-i+1)*inv_table[i])%MOD

  comb2=(comb2*(n-i)*inv_table[i])%MOD

  comb=(comb1*comb2)%MOD

  comb_sum+=comb

print((comb_sum%MOD))

import sys

sys.setrecursionlimit(10**9)

MOD=10**9+7

n,k=list(map(int,input().split()))

if k>=n-1:

  k=n-1

inv_table = [0]+[1]

for i in range(2,k+1):

  inv_table.append(-1*(MOD//i)*inv_table[MOD%i] %MOD)

comb_sum=1

fact=1

comb1=1

comb2=1

for i in range(1,k+1):

  comb1=(comb1*(n-i+1)*inv_table[i])%MOD

  comb2=(comb2*(n-i)*inv_table[i])%MOD

  comb=(comb1*comb2)%MOD

  comb_sum+=comb

print((comb_sum%MOD))

import sys

sys.setrecursionlimit(10**9)
MOD = 10**9 + 7
n, k = list(map(int, input().split()))
if k >= n - 1:
    k = n - 1
inv_table = [0] + [1]
for i in range(2, k + 1):
    inv_table += [inv_table[MOD % i] * (MOD - int(MOD / i)) % MOD]
comb_sum = 1
fact = 1
comb1 = 1
comb2 = 1
for i in range(1, k + 1):
    comb1 = (comb1 * (n - i + 1) * inv_table[i]) % MOD
    comb2 = (comb2 * (n - i) * inv_table[i]) % MOD
    comb = (comb1 * comb2) % MOD
    comb_sum += comb
print((comb_sum % MOD))

import sys

sys.setrecursionlimit(10**9)
MOD = 10**9 + 7
n, k = list(map(int, input().split()))
if k >= n - 1:
    k = n - 1
inv_table = [0] + [1]
for i in range(2, k + 1):
    inv_table.append(-1 * (MOD // i) * inv_table[MOD % i] % MOD)
comb_sum = 1
fact = 1
comb1 = 1
comb2 = 1
for i in range(1, k + 1):
    comb1 = (comb1 * (n - i + 1) * inv_table[i]) % MOD
    comb2 = (comb2 * (n - i) * inv_table[i]) % MOD
    comb = (comb1 * comb2) % MOD
    comb_sum += comb
print((comb_sum % MOD))

-    inv_table += [inv_table[MOD % i] * (MOD - int(MOD / i)) % MOD]

+    inv_table.append(-1 * (MOD // i) * inv_table[MOD % i] % MOD)


Input:
import sys

sys.setrecursionlimit(10**6)

def bps(gr,p,s,num):

    if gr[s]!=-1:

        return

    gr[s]=num

    for x in p[s]:

        bps(gr,p,x,num)

N,K,L=list(map(int,input().split()))

dic={}

road=[[] for _ in range(N)]

rail=[[] for _ in range(N)]

for i in range(K):

    p,q=list(map(int,input().split()))

    road[p-1].append(q-1)

    road[q-1].append(p-1)

for i in range(L):

    r,s=list(map(int,input().split()))

    rail[r-1].append(s-1)

    rail[s-1].append(r-1)

roadG=[-1 for _ in range(N)]

num=0

for i in range(N):

    if roadG[i]==-1:

        bps(roadG,road,i,num)

        num+=1

railG=[-1 for _ in range(N)]

num=0

for i in range(N):

    if railG[i]==-1:

        bps(railG,rail,i,num)

        num+=1

dic={}

arr=[]

for i in range(N):

    key=roadG[i]*N+railG[i]

    arr.append(key)

    dic[key]=dic.setdefault(key,0)+1

print(dic[arr[0]],end='')

for x in arr[1:]:

    print(' {}'.format(dic[x]),end='')

print('')

import sys

sys.setrecursionlimit(10**6)

def dps(gr,p,s,num):

    if gr[s]!=-1:

        return

    gr[s]=num

    for x in p[s]:

        dps(gr,p,x,num)

N,K,L=list(map(int,input().split()))

dic={}

road=[[] for _ in range(N)]

rail=[[] for _ in range(N)]

for i in range(K):

    p,q=list(map(int,input().split()))

    road[p-1].append(q-1)

    road[q-1].append(p-1)

for i in range(L):

    r,s=list(map(int,input().split()))

    rail[r-1].append(s-1)

    rail[s-1].append(r-1)

roadG=[-1 for _ in range(N)]

num=0

for i in range(N):

    if roadG[i]==-1:

        dps(roadG,road,i,num)

        num+=1

railG=[-1 for _ in range(N)]

num=0

for i in range(N):

    if railG[i]==-1:

        dps(railG,rail,i,num)

        num+=1

dic={}

arr=[]

for i in range(N):

    key=(roadG[i],railG[i])

    arr.append(key)

    dic[key]=dic.setdefault(key,0)+1

print((' '.join([str(dic[x]) for x in arr])))

import sys

sys.setrecursionlimit(10**6)

def bps(gr, p, s, num):
    if gr[s] != -1:
        return
    gr[s] = num
    for x in p[s]:
        bps(gr, p, x, num)

N, K, L = list(map(int, input().split()))
dic = {}
road = [[] for _ in range(N)]
rail = [[] for _ in range(N)]
for i in range(K):
    p, q = list(map(int, input().split()))
    road[p - 1].append(q - 1)
    road[q - 1].append(p - 1)
for i in range(L):
    r, s = list(map(int, input().split()))
    rail[r - 1].append(s - 1)
    rail[s - 1].append(r - 1)
roadG = [-1 for _ in range(N)]
num = 0
for i in range(N):
    if roadG[i] == -1:
        bps(roadG, road, i, num)
        num += 1
railG = [-1 for _ in range(N)]
num = 0
for i in range(N):
    if railG[i] == -1:
        bps(railG, rail, i, num)
        num += 1
dic = {}
arr = []
for i in range(N):
    key = roadG[i] * N + railG[i]
    arr.append(key)
    dic[key] = dic.setdefault(key, 0) + 1
print(dic[arr[0]], end="")
for x in arr[1:]:
    print(" {}".format(dic[x]), end="")
print("")

import sys

sys.setrecursionlimit(10**6)

def dps(gr, p, s, num):
    if gr[s] != -1:
        return
    gr[s] = num
    for x in p[s]:
        dps(gr, p, x, num)

N, K, L = list(map(int, input().split()))
dic = {}
road = [[] for _ in range(N)]
rail = [[] for _ in range(N)]
for i in range(K):
    p, q = list(map(int, input().split()))
    road[p - 1].append(q - 1)
    road[q - 1].append(p - 1)
for i in range(L):
    r, s = list(map(int, input().split()))
    rail[r - 1].append(s - 1)
    rail[s - 1].append(r - 1)
roadG = [-1 for _ in range(N)]
num = 0
for i in range(N):
    if roadG[i] == -1:
        dps(roadG, road, i, num)
        num += 1
railG = [-1 for _ in range(N)]
num = 0
for i in range(N):
    if railG[i] == -1:
        dps(railG, rail, i, num)
        num += 1
dic = {}
arr = []
for i in range(N):
    key = (roadG[i], railG[i])
    arr.append(key)
    dic[key] = dic.setdefault(key, 0) + 1
print((" ".join([str(dic[x]) for x in arr])))

-    print(" {}".format(dic[x]), end="")

+print((" ".join([str(dic[x]) for x in arr])))


Input:
from pprint import pprint

from collections import deque, defaultdict

import sys

# n, q = map(int, input().strip().split(" "))

n, q = list(map(int, sys.stdin.readline().strip().split(" ")))

edges = [[] for _ in range(n)]

for _ in range(n-1):

    # a_i, b_i = map(int, input().strip().split(" "))

    a_i, b_i = list(map(int, sys.stdin.readline().strip().split(" ")))

    edges[a_i-1].append(b_i-1)

    edges[b_i-1].append(a_i-1)

counter = [0] * n

p = [0] * n

for _ in range(q):

    # p_i, x_i = map(int, input().strip().split(" "))

    p_i, x_i = list(map(int, sys.stdin.readline().strip().split(" ")))

    p[p_i-1] += x_i

    # counter[p_i-1] += x_i

parents = deque()

parents.append(0)

visited = set()

while parents:

    parent = parents.popleft()

    if parent in visited:

        continue

    counter[parent] += p[parent]

    visited.add(parent)

    for child in edges[parent]:

        if child in visited:

            continue

        counter[child] += counter[parent]

        # print("child")

        # print(child, counter[child])

        parents.append(child)

print((" ".join(list(map(str, counter)))))

import sys

from collections import deque

N, Q = list(map(int, sys.stdin.readline().split(" ")))

edges = [[] for _ in range(N)]

for _ in range(N-1):

    a, b = list(map(int, sys.stdin.readline().split(" ")))

    # print(a, b)

    edges[a-1].append(b-1)

    edges[b-1].append(a-1)

# print(edges)

# sys.exit()

points = [0] * N

for _ in range(Q):

    p, x = list(map(int, sys.stdin.readline().split(" ")))

    points[p-1] += x

visited = set()

counted = [0] * N

q = deque([(0, points[0])])

while q:

    node, point = q.popleft()

    # print(node, point)

    if node in visited:

        continue

    counted[node] = str(point)

    visited.add(node)

    for edge in edges[node]:

        q.appendleft((edge, point + points[edge]))

print((" ".join(counted)))

from pprint import pprint
from collections import deque, defaultdict
import sys

# n, q = map(int, input().strip().split(" "))
n, q = list(map(int, sys.stdin.readline().strip().split(" ")))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    # a_i, b_i = map(int, input().strip().split(" "))
    a_i, b_i = list(map(int, sys.stdin.readline().strip().split(" ")))
    edges[a_i - 1].append(b_i - 1)
    edges[b_i - 1].append(a_i - 1)
counter = [0] * n
p = [0] * n
for _ in range(q):
    # p_i, x_i = map(int, input().strip().split(" "))
    p_i, x_i = list(map(int, sys.stdin.readline().strip().split(" ")))
    p[p_i - 1] += x_i
    # counter[p_i-1] += x_i
parents = deque()
parents.append(0)
visited = set()
while parents:
    parent = parents.popleft()
    if parent in visited:
        continue
    counter[parent] += p[parent]
    visited.add(parent)
    for child in edges[parent]:
        if child in visited:
            continue
        counter[child] += counter[parent]
        # print("child")
        # print(child, counter[child])
        parents.append(child)
print((" ".join(list(map(str, counter)))))

import sys
from collections import deque

N, Q = list(map(int, sys.stdin.readline().split(" ")))
edges = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = list(map(int, sys.stdin.readline().split(" ")))
    # print(a, b)
    edges[a - 1].append(b - 1)
    edges[b - 1].append(a - 1)
# print(edges)
# sys.exit()
points = [0] * N
for _ in range(Q):
    p, x = list(map(int, sys.stdin.readline().split(" ")))
    points[p - 1] += x
visited = set()
counted = [0] * N
q = deque([(0, points[0])])
while q:
    node, point = q.popleft()
    # print(node, point)
    if node in visited:
        continue
    counted[node] = str(point)
    visited.add(node)
    for edge in edges[node]:
        q.appendleft((edge, point + points[edge]))
print((" ".join(counted)))

-from collections import deque, defaultdict

-# n, q = map(int, input().strip().split(" "))

-n, q = list(map(int, sys.stdin.readline().strip().split(" ")))

-    # a_i, b_i = map(int, input().strip().split(" "))

-    a_i, b_i = list(map(int, sys.stdin.readline().strip().split(" ")))

-    # p_i, x_i = map(int, input().strip().split(" "))

-    p_i, x_i = list(map(int, sys.stdin.readline().strip().split(" ")))

+N, Q = list(map(int, sys.stdin.readline().split(" ")))

+    a, b = list(map(int, sys.stdin.readline().split(" ")))

+    p, x = list(map(int, sys.stdin.readline().split(" ")))

-        counter[child] += counter[parent]

+        q.appendleft((edge, point + points[edge]))


Input:
N = int(eval(input()))

ans = 10 ** 20

for i in range(1, 10 ** 6 + 1):

    if i ** 2 > N: break

    if N % i == 0:

        ans = min(ans, i + N // i - 2)

print(ans)

import sys

def solve():

    N = int(eval(input()))

    moveN = 10000000000000000000

    for i in range(1, N + 1):

        if i ** 2 > N: break

        if N % i == 0:

            moveN = min(i + (N // i) - 2, moveN)

    print(moveN)

    return 0

if __name__ == "__main__":

    solve()

N = int(eval(input()))
ans = 10**20
for i in range(1, 10**6 + 1):
    if i**2 > N:
        break
    if N % i == 0:
        ans = min(ans, i + N // i - 2)
print(ans)

import sys

def solve():
    N = int(eval(input()))
    moveN = 10000000000000000000
    for i in range(1, N + 1):
        if i**2 > N:
            break
        if N % i == 0:
            moveN = min(i + (N // i) - 2, moveN)
    print(moveN)
    return 0

if __name__ == "__main__":
    solve()

+            moveN = min(i + (N // i) - 2, moveN)


Input:
import sys, re

from collections import deque, defaultdict, Counter

from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians

from itertools import accumulate, permutations, combinations, product

from operator import itemgetter, mul

from copy import deepcopy

from string import ascii_lowercase, ascii_uppercase, digits

from bisect import bisect, bisect_left

from fractions import gcd

from heapq import heappush, heappop

from functools import reduce

def input(): return sys.stdin.readline().strip()

def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(): return list(map(int, input().split()))

sys.setrecursionlimit(10 ** 9)

INF = float('inf')

mod = 10 ** 9 + 7

def comb_count(n, r):

    return factorial(n) // (factorial(n - r) * factorial(r))

N = INT()

S = [eval(input()) for _ in range(N)]

for i in range(N):

	S[i] = sorted(S[i])

S = sorted(S)

ku = []

kugiri = 0

for i in range(N - 1):

	if S[i] == S[i + 1]:

		kugiri += 1

		if i == N - 2:

			ku.append(kugiri + 1)

	else:

		ku.append(kugiri + 1)

		kugiri = 0

wa = 0

for i in range(len(ku)):

	if ku[i] == 1:

		continue

	wa += comb_count(ku[i], 2)

print(wa)

import sys, re

from collections import deque, defaultdict, Counter

from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians

from itertools import accumulate, permutations, combinations, product

from operator import itemgetter, mul

from copy import deepcopy

from string import ascii_lowercase, ascii_uppercase, digits

from bisect import bisect, bisect_left

from fractions import gcd

from heapq import heappush, heappop

from functools import reduce

def input(): return sys.stdin.readline().strip()

def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(): return list(map(int, input().split()))

sys.setrecursionlimit(10 ** 9)

INF = float('inf')

mod = 10 ** 9 + 7

def comb_count(n, r):

    return factorial(n) // (factorial(n - r) * factorial(r))

N = INT()

S = ["".join(sorted(eval(input()))) for _ in range(N)]

dic = defaultdict(int)

for x in S:

	dic[x] += 1

wa = 0

for n in list(dic.values()):

	wa += int(n*(n - 1)/2)

print(wa)

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce

def input():
    return sys.stdin.readline().strip()

def INT():
    return int(eval(input()))

def MAP():
    return list(map(int, input().split()))

def LIST():
    return list(map(int, input().split()))

sys.setrecursionlimit(10**9)
INF = float("inf")
mod = 10**9 + 7

def comb_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

N = INT()
S = [eval(input()) for _ in range(N)]
for i in range(N):
    S[i] = sorted(S[i])
S = sorted(S)
ku = []
kugiri = 0
for i in range(N - 1):
    if S[i] == S[i + 1]:
        kugiri += 1
        if i == N - 2:
            ku.append(kugiri + 1)
    else:
        ku.append(kugiri + 1)
        kugiri = 0
wa = 0
for i in range(len(ku)):
    if ku[i] == 1:
        continue
    wa += comb_count(ku[i], 2)
print(wa)

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce

def input():
    return sys.stdin.readline().strip()

def INT():
    return int(eval(input()))

def MAP():
    return list(map(int, input().split()))

def LIST():
    return list(map(int, input().split()))

sys.setrecursionlimit(10**9)
INF = float("inf")
mod = 10**9 + 7

def comb_count(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

N = INT()
S = ["".join(sorted(eval(input()))) for _ in range(N)]
dic = defaultdict(int)
for x in S:
    dic[x] += 1
wa = 0
for n in list(dic.values()):
    wa += int(n * (n - 1) / 2)
print(wa)

+S = ["".join(sorted(eval(input()))) for _ in range(N)]


Input:
def main():

    from collections import deque

    s = deque(eval(input()))

    q = int(eval(input()))

    flag = 0

    query = []

    for _ in range(q):

        temp = input().split()

        if len(temp) == 1:

            flag += 1

        else:

            query.append(temp + [flag%2])

    for q in query:

        if q[-1] == 0:

            if q[1] == "1":

                s.appendleft(q[2])

            else:

                s.append(q[2])

        else:

            if q[1] == "1":

                s.append(q[2])

            else:

                s.appendleft(q[2])

    if flag % 2 == 1:

        print(("".join(list(s)[::-1])))

    else:

        print(("".join(s)))

main()

from collections import deque

s = deque(eval(input()))

q = int(eval(input()))

flag = True

for _ in range(q):

    temp = input().split()

    if len(temp) == 1:

        flag = not flag

    else:

        if flag:

            if temp[1] == "1":

                s.appendleft(temp[2])

            else:

                s.append(temp[2])

        else:

            if temp[1] == "1":

                s.append(temp[2])

            else:

                s.appendleft(temp[2])

if flag:

    print(("".join(s)))

else:

    print(("".join(list(s)[::-1])))

def main():
    from collections import deque

    s = deque(eval(input()))
    q = int(eval(input()))
    flag = 0
    query = []
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag % 2])
    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])
    if flag % 2 == 1:
        print(("".join(list(s)[::-1])))
    else:
        print(("".join(s)))

main()

-            query.append(temp + [flag % 2])


Input:
from bisect import bisect_right

N = int(eval(input()))

unit = [1]

for b in [6,9]:

    u = b

    while u <= N:

        unit.append(u)

        u *= b

unit.sort()

Nu = len(unit)

ans = N

state_pool = [(0, 0, Nu-1)]

while state_pool:

    n, i, pk = state_pool.pop()

    if N - n >= (ans-i)*unit[pk]:

        continue

    sk = bisect_right(unit, N-n, 0, pk+1)

    for k in range(sk):

        u = unit[k]

        c = n + u

        if c == N:

            if i + 1 < ans:

                ans = i + 1

        else:

            state_pool.append((c, i+1, k))

print(ans)

from bisect import bisect_right

N = int(eval(input()))

unit = [1]

for b in [6,9]:

    u = b

    while u <= N:

        unit.append(u)

        u *= b

unit.sort()

Nu = len(unit)

ans = N

state_pool = [(0, 0, 0, Nu-1)]

while state_pool:

    n, i, parent, pk = state_pool.pop()

    if N - n >= (ans-i)*unit[pk]:

        continue

    elif pk > 0 and i > 0:

        state_pool.append((parent+unit[pk-1], i, parent, pk-1))

    k = bisect_right(unit, N-n, 0, pk+1) - 1

    c = n + unit[k]

    if c == N:

        if i + 1 < ans:

            ans = i + 1

    else:

        state_pool.append((c, i+1, n, k))

print(ans)

from bisect import bisect_right

N = int(eval(input()))
unit = [1]
for b in [6, 9]:
    u = b
    while u <= N:
        unit.append(u)
        u *= b
unit.sort()
Nu = len(unit)
ans = N
state_pool = [(0, 0, Nu - 1)]
while state_pool:
    n, i, pk = state_pool.pop()
    if N - n >= (ans - i) * unit[pk]:
        continue
    sk = bisect_right(unit, N - n, 0, pk + 1)
    for k in range(sk):
        u = unit[k]
        c = n + u
        if c == N:
            if i + 1 < ans:
                ans = i + 1
        else:
            state_pool.append((c, i + 1, k))
print(ans)

from bisect import bisect_right

N = int(eval(input()))
unit = [1]
for b in [6, 9]:
    u = b
    while u <= N:
        unit.append(u)
        u *= b
unit.sort()
Nu = len(unit)
ans = N
state_pool = [(0, 0, 0, Nu - 1)]
while state_pool:
    n, i, parent, pk = state_pool.pop()
    if N - n >= (ans - i) * unit[pk]:
        continue
    elif pk > 0 and i > 0:
        state_pool.append((parent + unit[pk - 1], i, parent, pk - 1))
    k = bisect_right(unit, N - n, 0, pk + 1) - 1
    c = n + unit[k]
    if c == N:
        if i + 1 < ans:
            ans = i + 1
    else:
        state_pool.append((c, i + 1, n, k))
print(ans)

+    n, i, parent, pk = state_pool.pop()

-    sk = bisect_right(unit, N - n, 0, pk + 1)

-            state_pool.append((c, i + 1, k))

+        state_pool.append((parent + unit[pk - 1], i, parent, pk - 1))

+    k = bisect_right(unit, N - n, 0, pk + 1) - 1

+        state_pool.append((c, i + 1, n, k))


Input:
def main():

    n, m = list(map(int, input().split()))

    p = 2**n

    dp = [float('inf')]*p

    dp[0] = 0

    for _ in range(m):

        a, _ = list(map(int, input().split()))

        c = sum([2**(int(i)-1) for i in input().split()])

        for s in range(p):

                t = s|c

                if dp[t] > dp[s] + a:

                    dp[t] = dp[s] + a

    ans = dp[-1]

    if ans == float('inf'):

        ans = -1

    print(ans)

if __name__ == '__main__':

    main()

def main():

    n, m = list(map(int, input().split()))

    p = 2**n

    dp = [10**7]*p

    dp[0] = 0

    for _ in range(m):

        a, _ = list(map(int, input().split()))

        c = sum([2**(int(i)-1) for i in input().split()])

        for s in range(p):

                t = s|c

                if dp[t] > dp[s] + a:

                    dp[t] = dp[s] + a

    ans = dp[-1]

    if ans == 10**7:

        ans = -1

    print(ans)

if __name__ == '__main__':

    main()

def main():
    n, m = list(map(int, input().split()))
    p = 2**n
    dp = [float("inf")] * p
    dp[0] = 0
    for _ in range(m):
        a, _ = list(map(int, input().split()))
        c = sum([2 ** (int(i) - 1) for i in input().split()])
        for s in range(p):
            t = s | c
            if dp[t] > dp[s] + a:
                dp[t] = dp[s] + a
    ans = dp[-1]
    if ans == float("inf"):
        ans = -1
    print(ans)

if __name__ == "__main__":
    main()

def main():
    n, m = list(map(int, input().split()))
    p = 2**n
    dp = [10**7] * p
    dp[0] = 0
    for _ in range(m):
        a, _ = list(map(int, input().split()))
        c = sum([2 ** (int(i) - 1) for i in input().split()])
        for s in range(p):
            t = s | c
            if dp[t] > dp[s] + a:
                dp[t] = dp[s] + a
    ans = dp[-1]
    if ans == 10**7:
        ans = -1
    print(ans)

if __name__ == "__main__":
    main()


Input:
H, W = list(map(int, input().split()))

c = [eval(input()) for _ in range(H)]

moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def bfs():

    visited = [[False for _ in range(W)] for _ in range(H)]

    dist = [[[] for _ in range(W)] for _ in range(H)]

    routes = [(0, 0)]

    visited[0][0] = True

    hop_count = 0

    while len(routes) > 0:

        x, y = routes.pop(0)

        for move in moves:

            cx, cy = x + move[0], y + move[1]

            if cx < 0 or cy < 0 or cx >= W or cy >= H or c[cy][cx] == '#' or visited[cy][cx]:

                continue

            if cx == W-1 and cy == H-1:

                return sum([line.count('.')for line in c]) - len(dist[y][x]) - 2

            visited[cy][cx] = True

            routes.append((cx, cy))

            dist[cy][cx] += dist[y][x] + [(x, y)]

    return -1

print((bfs()))

H, W = list(map(int, input().split()))

c = [eval(input()) for _ in range(H)]

moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def bfs():

    visited = [[False for _ in range(W)] for _ in range(H)]

    dist = [[0 for _ in range(W)] for _ in range(H)]

    routes = [(0, 0)]

    visited[0][0] = True

    while len(routes) > 0:

        x, y = routes.pop(0)

        for move in moves:

            cx, cy = x + move[0], y + move[1]

            if cx < 0 or cy < 0 or cx >= W or cy >= H or c[cy][cx] == '#' or visited[cy][cx]:

                continue

            if cx == W-1 and cy == H-1:

                return sum([line.count('.')for line in c]) - dist[y][x] - 2

            visited[cy][cx] = True

            routes.append((cx, cy))

            dist[cy][cx] = dist[y][x] + 1

    return -1

print((bfs()))

H, W = list(map(int, input().split()))
c = [eval(input()) for _ in range(H)]
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def bfs():
    visited = [[False for _ in range(W)] for _ in range(H)]
    dist = [[[] for _ in range(W)] for _ in range(H)]
    routes = [(0, 0)]
    visited[0][0] = True
    hop_count = 0
    while len(routes) > 0:
        x, y = routes.pop(0)
        for move in moves:
            cx, cy = x + move[0], y + move[1]
            if (
                cx < 0
                or cy < 0
                or cx >= W
                or cy >= H
                or c[cy][cx] == "#"
                or visited[cy][cx]
            ):
                continue
            if cx == W - 1 and cy == H - 1:
                return sum([line.count(".") for line in c]) - len(dist[y][x]) - 2
            visited[cy][cx] = True
            routes.append((cx, cy))
            dist[cy][cx] += dist[y][x] + [(x, y)]
    return -1

print((bfs()))

H, W = list(map(int, input().split()))
c = [eval(input()) for _ in range(H)]
moves = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def bfs():
    visited = [[False for _ in range(W)] for _ in range(H)]
    dist = [[0 for _ in range(W)] for _ in range(H)]
    routes = [(0, 0)]
    visited[0][0] = True
    while len(routes) > 0:
        x, y = routes.pop(0)
        for move in moves:
            cx, cy = x + move[0], y + move[1]
            if (
                cx < 0
                or cy < 0
                or cx >= W
                or cy >= H
                or c[cy][cx] == "#"
                or visited[cy][cx]
            ):
                continue
            if cx == W - 1 and cy == H - 1:
                return sum([line.count(".") for line in c]) - dist[y][x] - 2
            visited[cy][cx] = True
            routes.append((cx, cy))
            dist[cy][cx] = dist[y][x] + 1
    return -1

print((bfs()))

-    dist = [[[] for _ in range(W)] for _ in range(H)]

+    dist = [[0 for _ in range(W)] for _ in range(H)]

-                return sum([line.count(".") for line in c]) - len(dist[y][x]) - 2

+                return sum([line.count(".") for line in c]) - dist[y][x] - 2

+            dist[cy][cx] = dist[y][x] + 1


Input:
import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60

MOD = 1000000007

def main():

    N = int(readline())

    S = readline().strip()

    dp = [[0] * (N + 1) for _ in range(N + 1)]

    for i in range(N - 1, -1, -1):

        for j in range(N - 1, i, -1):

            if S[i] == S[j]:

                dp[i][j] = dp[i + 1][j + 1] + 1

    ans = 0

    for i in range(N):

        for j in range(i + 1, N):

            if ans < min(dp[i][j], j - i):

                ans = min(dp[i][j], j - i)

    print(ans)

    return

if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60

MOD = 1000000007

def main():

    N = int(readline())

    S = readline().strip()

    dp = [[0] * (N + 1) for _ in range(N + 1)]

    ans = 0

    for i in range(N - 1, -1, -1):

        for j in range(N - 1, i, -1):

            if S[i] == S[j]:

                dp[i][j] = dp[i + 1][j + 1] + 1

            if ans < min(dp[i][j], j - i):

                ans = min(dp[i][j], j - i)

    print(ans)

    return

if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007

def main():
    N = int(readline())
    S = readline().strip()
    dp = [[0] * (N + 1) for _ in range(N + 1)]
    for i in range(N - 1, -1, -1):
        for j in range(N - 1, i, -1):
            if S[i] == S[j]:
                dp[i][j] = dp[i + 1][j + 1] + 1
    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            if ans < min(dp[i][j], j - i):
                ans = min(dp[i][j], j - i)
    print(ans)
    return

if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007

def main():
    N = int(readline())
    S = readline().strip()
    dp = [[0] * (N + 1) for _ in range(N + 1)]
    ans = 0
    for i in range(N - 1, -1, -1):
        for j in range(N - 1, i, -1):
            if S[i] == S[j]:
                dp[i][j] = dp[i + 1][j + 1] + 1
            if ans < min(dp[i][j], j - i):
                ans = min(dp[i][j], j - i)
    print(ans)
    return

if __name__ == "__main__":
    main()

+    ans = 0
-    ans = 0
-    for i in range(N):
-        for j in range(i + 1, N):


Input:
a=int(eval(input())) # 500

b=int(eval(input())) # 100

c=int(eval(input())) # 50

x=int(eval(input()))

sum = []

count = 0

for i in range(0, a+1):

    asum = 500 * i

    for i in range(0, b+1):

        bsum = 100 * i

        for i in range(0, c+1):

            csum = 50 * i

            sum.append(asum + bsum + csum)

for i in sum:

    if i == x:

        count += 1

print(count)

a=int(eval(input())) # 500

b=int(eval(input())) # 100

c=int(eval(input())) # 50

x=int(eval(input()))

count = 0

for i in range(0, a+1):

    for j in range(0, b+1):

        for k in range(0, c+1):

            if 500 * i + 100 * j + 50 * k == x:

                count += 1

print(count)

a = int(eval(input()))  # 500
b = int(eval(input()))  # 100
c = int(eval(input()))  # 50
x = int(eval(input()))
sum = []
count = 0
for i in range(0, a + 1):
    asum = 500 * i
    for i in range(0, b + 1):
        bsum = 100 * i
        for i in range(0, c + 1):
            csum = 50 * i
            sum.append(asum + bsum + csum)
for i in sum:
    if i == x:
        count += 1
print(count)

a = int(eval(input()))  # 500
b = int(eval(input()))  # 100
c = int(eval(input()))  # 50
x = int(eval(input()))
count = 0
for i in range(0, a + 1):
    for j in range(0, b + 1):
        for k in range(0, c + 1):
            if 500 * i + 100 * j + 50 * k == x:
                count += 1
print(count)

-            sum.append(asum + bsum + csum)


Input:
n=int(eval(input()))

a=[list(map(int,input().split()))for _ in range(n)]

m=0

for i in range(n):

  s=(a[-1-i][0]+m)%a[-1-i][1]

  if s!=0:m+=a[-1-i][1]-s

print(m)

n=int(eval(input()))

a=[]

b=[]

for i in range(n):

  aa,bb=list(map(int,input().split()))

  a.append(aa)

  b.append(bb)

m=0

for i in range(n-1,-1,-1):

  a[i]+=m

  if a[i]%b[i]:m+=b[i]-a[i]%b[i]

print(m)

n = int(eval(input()))
a = [list(map(int, input().split())) for _ in range(n)]
m = 0
for i in range(n):
    s = (a[-1 - i][0] + m) % a[-1 - i][1]
    if s != 0:
        m += a[-1 - i][1] - s
print(m)

n = int(eval(input()))
a = []
b = []
for i in range(n):
    aa, bb = list(map(int, input().split()))
    a.append(aa)
    b.append(bb)
m = 0
for i in range(n - 1, -1, -1):
    a[i] += m
    if a[i] % b[i]:
        m += b[i] - a[i] % b[i]
print(m)

-a = [list(map(int, input().split())) for _ in range(n)]

+    aa, bb = list(map(int, input().split()))


Input:
N = int(eval(input()))

A = [int(i) for i in input().split()]

absA = []

for i in range(N):

    absA.append(abs(A[i]))

cnt = 0

for i in range(N):

    if A[i] < 0:

        cnt += 1

flag = 0

for i in range(N):

    if A[i] != 0:

        flag = 1

    if flag == 1 and A[i] == 0:

        cnt += 1

if cnt % 2 == 0:

    ans = sum(absA)

else:

    ans = sum(absA) - min(absA) * 2

print(ans)

N = int(eval(input()))

A = [int(i) for i in input().split()]

absA = []

for i in range(N):

    absA.append(abs(A[i]))

cnt = 0

for i in range(N):

    if A[i] <= 0:

        cnt += 1

if cnt % 2 == 0:

    ans = sum(absA)

else:

    ans = sum(absA) - min(absA) * 2

print(ans)

-    if A[i] < 0:
-        cnt += 1
-flag = 0
-for i in range(N):
-    if A[i] != 0:
-        flag = 1
-    if flag == 1 and A[i] == 0:
+    if A[i] <= 0:


Input:
import numpy as np

N = int(eval(input()))

a = sorted(list(map(int,input().split())),reverse=True)

print((sum(a[::2]) - sum(a[1::2])))

N = int(eval(input()))

value = sorted(list(map(int,input().split())))[::-1]

print((sum(value[::2]) - sum(value[1::2])))

import numpy as np

N = int(eval(input()))
a = sorted(list(map(int, input().split())), reverse=True)
print((sum(a[::2]) - sum(a[1::2])))

N = int(eval(input()))
value = sorted(list(map(int, input().split())))[::-1]
print((sum(value[::2]) - sum(value[1::2])))

-a = sorted(list(map(int, input().split())), reverse=True)

+value = sorted(list(map(int, input().split())))[::-1]


Input:
N, Q = list(map(int, input().split()))

S = list(eval(input()))

mark = [0]*(N+1)

for i in range(2, N+1):

    if S[i-2] == 'A' and S[i-1] == 'C':

        mark[i] = mark[i-1] + 1

    else:

        mark[i] = mark[i-1]

for i in range(Q):

    l,r = list(map(int,input().split()))

    l = l - 1

    r = r - 1

    print((mark[r+1]-mark[l+1]))

N, Q = list(map(int, input().split()))

S = list(eval(input()))

lr = [list(map(int, input().split())) for i in range(Q)]

mark = [0]*(N+1)

for i in range(2, N+1):

    if S[i-2] == 'A' and S[i-1] == 'C':

        mark[i] = mark[i-1] + 1

    else:

        mark[i] = mark[i-1]

for i in lr:

    print((mark[i[1]]-mark[i[0]]))

N, Q = list(map(int, input().split()))
S = list(eval(input()))
mark = [0] * (N + 1)
for i in range(2, N + 1):
    if S[i - 2] == "A" and S[i - 1] == "C":
        mark[i] = mark[i - 1] + 1
    else:
        mark[i] = mark[i - 1]
for i in range(Q):
    l, r = list(map(int, input().split()))
    l = l - 1
    r = r - 1
    print((mark[r + 1] - mark[l + 1]))

N, Q = list(map(int, input().split()))
S = list(eval(input()))
lr = [list(map(int, input().split())) for i in range(Q)]
mark = [0] * (N + 1)
for i in range(2, N + 1):
    if S[i - 2] == "A" and S[i - 1] == "C":
        mark[i] = mark[i - 1] + 1
    else:
        mark[i] = mark[i - 1]
for i in lr:
    print((mark[i[1]] - mark[i[0]]))

+lr = [list(map(int, input().split())) for i in range(Q)]

-    l, r = list(map(int, input().split()))


Input:
e=enumerate

n,a=open(0)

n=int(n)

d=[0]+[1e18]*n

for j,(a,i)in e(sorted((-int(a),i)for i,a in e(a.split()))):d=[min(t+a*abs(~i-j+k+n),d[k-1]+a*abs(~i+k))for k,t in e(d)]

print((-min(d)))

n=int(eval(input()))

d=[0]+[-10**18]*n

for j,(a,i)in enumerate(sorted((-a,i)for i,a in enumerate(map(int,input().split())))):

    d=[max(t-a*abs(~i-j+k+n),d[k-1]-a*abs(~i+k))for k,t in enumerate(d)]

print((max(d)))

e = enumerate
n, a = open(0)
n = int(n)
d = [0] + [1e18] * n
for j, (a, i) in e(sorted((-int(a), i) for i, a in e(a.split()))):
    d = [min(t + a * abs(~i - j + k + n), d[k - 1] + a * abs(~i + k)) for k, t in e(d)]
print((-min(d)))

n = int(eval(input()))
d = [0] + [-(10**18)] * n
for j, (a, i) in enumerate(
    sorted((-a, i) for i, a in enumerate(map(int, input().split())))
):
    d = [
        max(t - a * abs(~i - j + k + n), d[k - 1] - a * abs(~i + k))
        for k, t in enumerate(d)
    ]
print((max(d)))

-for j, (a, i) in e(sorted((-int(a), i) for i, a in e(a.split()))):

-    d = [min(t + a * abs(~i - j + k + n), d[k - 1] + a * abs(~i + k)) for k, t in e(d)]

+    sorted((-a, i) for i, a in enumerate(map(int, input().split())))


Input:
N = int(eval(input()))

A = list(map(int, input().split()))

A.sort()

for i in range(N//2):

    if A[i*2] + A[i*2+1] < 0:

        A[i*2] = -1 * A[i*2]

        A[i*2+1] = -1 * A[i*2+1]

    else:

        break

print((sum(A)))

N = int(eval(input()))

A = list(map(int, input().split()))

num_of_minus = sum([a < 0 for a in A])

abs_A = [abs(a) for a in A]

ans = sum(abs_A)

if num_of_minus % 2 == 0:

    print(ans)

else:

    print((ans - 2 * min(abs_A)))

N = int(eval(input()))
A = list(map(int, input().split()))
A.sort()
for i in range(N // 2):
    if A[i * 2] + A[i * 2 + 1] < 0:
        A[i * 2] = -1 * A[i * 2]
        A[i * 2 + 1] = -1 * A[i * 2 + 1]
    else:
        break
print((sum(A)))


Input:
import numpy as np

from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import floyd_warshall as f

def main():

  t=np.fromfile(open(0,'rb'),'i',sep=' ')

  n,m,l=t[:3]

  m=m*3+3

  print(('\n'.join((f(f(csr_matrix((t[5:m:3],(t[3:m:3],t[4:m:3])),[n+1]*2),0)<=l)[t[m+1::2],t[m+2::2]].clip(0,n).astype(int)%n-1).astype(str).tolist())))

main()

import sys

from numpy import fromstring

from scipy.sparse import csr_matrix

from scipy.sparse.csgraph import floyd_warshall as f

def main():

  t=fromstring(sys.stdin.buffer.read(),'i',sep=' ')

  n,m,l=t[:3]

  m=m*3+3

  print(('\n'.join((f(f(csr_matrix((t[5:m:3],(t[3:m:3],t[4:m:3])),[n+1]*2),0)<=l)[t[m+1::2],t[m+2::2]].clip(0,n).astype(int)%n-1).astype(str).tolist())))

main()

import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall as f

def main():
    t = np.fromfile(open(0, "rb"), "i", sep=" ")
    n, m, l = t[:3]
    m = m * 3 + 3
    print(
        (
            "\n".join(
                (
                    f(
                        f(csr_matrix((t[5:m:3], (t[3:m:3], t[4:m:3])), [n + 1] * 2), 0)
                        <= l
                    )[t[m + 1 :: 2], t[m + 2 :: 2]]
                    .clip(0, n)
                    .astype(int)
                    % n
                    - 1
                )
                .astype(str)
                .tolist()
            )
        )
    )

main()

import sys
from numpy import fromstring
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall as f

def main():
    t = fromstring(sys.stdin.buffer.read(), "i", sep=" ")
    n, m, l = t[:3]
    m = m * 3 + 3
    print(
        (
            "\n".join(
                (
                    f(
                        f(csr_matrix((t[5:m:3], (t[3:m:3], t[4:m:3])), [n + 1] * 2), 0)
                        <= l
                    )[t[m + 1 :: 2], t[m + 2 :: 2]]
                    .clip(0, n)
                    .astype(int)
                    % n
                    - 1
                )
                .astype(str)
                .tolist()
            )
        )
    )

main()

-    t = np.fromfile(open(0, "rb"), "i", sep=" ")

+    t = fromstring(sys.stdin.buffer.read(), "i", sep=" ")