Input:
def CNT(A):

    tmp, Min = 0, 0

    for a in A:

        if a == '(': tmp += 1

        else: tmp -= 1

        Min = min(Min, tmp)

    return (-Min, tmp-Min)

N = int(eval(input()))

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

T = [CNT(s) for s in S]

pls = []

mis = []

for l, r in T:

    if l <= r: pls.append((l, r))

    else: mis.append((l, r))

pls.sort(key=lambda a: a[0])

mis.sort(key=lambda a: a[1], reverse=True)

totl= pls + mis

levl = 0

for l, r in totl:

    levl -= l

    if levl < 0:

        print('No')

        exit()

    levl += r

print(('Yes' if levl==0 else 'No'))

def main():

  ## IMPORT MODULE

  #import sys

  #sys.setrecursionlimit(100000)

  #input=lambda :sys.stdin.readline().rstrip()

  #f_inf=float("inf")

  #MOD=10**9+7

  if 'get_ipython' in globals():

    ## SAMPLE INPUT

    n = 4

    S = ['((()))', '((((((', '))))))', '()()()']

  else:

    ## INPUT

    n = int(eval(input()))

    #a, b = map(int, input().split())

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

  ## SUBMITION CODES HERE

  def CNT(A):

    tmp, Min = 0, 0

    for a in A:

        if a == '(': tmp += 1

        else: tmp -= 1

        Min = min(Min, tmp)

    return (-Min, tmp-Min)

  T = [CNT(s) for s in S]

  pls = []

  mis = []

  for l, r in T:

    if l <= r: pls.append((l, r))

    else: mis.append((l, r))

  pls.sort(key=lambda a: a[0])

  mis.sort(key=lambda a: a[1], reverse=True)

  total = pls + mis

  levl = 0

  for l, r in total:

    levl -= l

    if levl < 0:

        print('No')

        exit()

    levl += r

  print(('Yes' if levl == 0 else 'No'))

main()

def CNT(A):
    tmp, Min = 0, 0
    for a in A:
        if a == "(":
            tmp += 1
        else:
            tmp -= 1
        Min = min(Min, tmp)
    return (-Min, tmp - Min)

N = int(eval(input()))
S = [eval(input()) for _ in range(N)]
T = [CNT(s) for s in S]
pls = []
mis = []
for l, r in T:
    if l <= r:
        pls.append((l, r))
    else:
        mis.append((l, r))
pls.sort(key=lambda a: a[0])
mis.sort(key=lambda a: a[1], reverse=True)
totl = pls + mis
levl = 0
for l, r in totl:
    levl -= l
    if levl < 0:
        print("No")
        exit()
    levl += r
print(("Yes" if levl == 0 else "No"))

def main():
    ## IMPORT MODULE
    # import sys
    # sys.setrecursionlimit(100000)
    # input=lambda :sys.stdin.readline().rstrip()
    # f_inf=float("inf")
    # MOD=10**9+7
    if "get_ipython" in globals():
        ## SAMPLE INPUT
        n = 4
        S = ["((()))", "((((((", "))))))", "()()()"]
    else:
        ## INPUT
        n = int(eval(input()))
        # a, b = map(int, input().split())
        S = [eval(input()) for _ in range(n)]
    ## SUBMITION CODES HERE
    def CNT(A):
        tmp, Min = 0, 0
        for a in A:
            if a == "(":
                tmp += 1
            else:
                tmp -= 1
            Min = min(Min, tmp)
        return (-Min, tmp - Min)

    T = [CNT(s) for s in S]
    pls = []
    mis = []
    for l, r in T:
        if l <= r:
            pls.append((l, r))
        else:
            mis.append((l, r))
    pls.sort(key=lambda a: a[0])
    mis.sort(key=lambda a: a[1], reverse=True)
    total = pls + mis
    levl = 0
    for l, r in total:
        levl -= l
        if levl < 0:
            print("No")
            exit()
        levl += r
    print(("Yes" if levl == 0 else "No"))

main()

+    # input=lambda :sys.stdin.readline().rstrip()

+        # a, b = map(int, input().split())

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

+    mis.sort(key=lambda a: a[1], reverse=True)

+    print(("Yes" if levl == 0 else "No"))

-mis.sort(key=lambda a: a[1], reverse=True)


Input:
# jsc2019-qualB - Kleene Inversion

from collections import Counter

def main():

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

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

    MOD = 10 ** 9 + 7

    C = Counter(A)

    x = sum(sum(j > i for j in A) * c for i, c in list(C.items()))

    y = sum(sum(b > a for b in A[i + 1 :]) for i, a in enumerate(A[:-1]))

    ans = (x * K * (1 + K) // 2) % MOD

    ans = (ans - y * K) % MOD

    print(ans)

if __name__ == "__main__":

    main()

# jsc2019-qualB - Kleene Inversion

from collections import Counter

from itertools import accumulate

def main():

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

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

    MOD = 10 ** 9 + 7

    C = list(Counter(A).values())

    ac = list(accumulate(C[:-1]))

    x = sum(i * j for i, j in zip(C[1:], ac)) % MOD

    y = 0

    for i, a in enumerate(A[:-1]):

        y += sum(b > a for b in A[i + 1 :])

    ans = (x * (1 + K) * K // 2) % MOD

    ans = (ans - y * K) % MOD

    print(ans)

if __name__ == "__main__":

    main()

# jsc2019-qualB - Kleene Inversion
from collections import Counter

def main():
    N, K = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    MOD = 10**9 + 7
    C = Counter(A)
    x = sum(sum(j > i for j in A) * c for i, c in list(C.items()))
    y = sum(sum(b > a for b in A[i + 1 :]) for i, a in enumerate(A[:-1]))
    ans = (x * K * (1 + K) // 2) % MOD
    ans = (ans - y * K) % MOD
    print(ans)

if __name__ == "__main__":
    main()

# jsc2019-qualB - Kleene Inversion
from collections import Counter
from itertools import accumulate

def main():
    N, K = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    MOD = 10**9 + 7
    C = list(Counter(A).values())
    ac = list(accumulate(C[:-1]))
    x = sum(i * j for i, j in zip(C[1:], ac)) % MOD
    y = 0
    for i, a in enumerate(A[:-1]):
        y += sum(b > a for b in A[i + 1 :])
    ans = (x * (1 + K) * K // 2) % MOD
    ans = (ans - y * K) % MOD
    print(ans)

if __name__ == "__main__":
    main()

-    x = sum(sum(j > i for j in A) * c for i, c in list(C.items()))

-    y = sum(sum(b > a for b in A[i + 1 :]) for i, a in enumerate(A[:-1]))

+    x = sum(i * j for i, j in zip(C[1:], ac)) % MOD

+        y += sum(b > a for b in A[i + 1 :])


Input:
import sys

import numpy as np

input = sys.stdin.readline

def main():

    n = int(eval(input()))

    v = np.array(list(map(int, input().split())))

    len1 = n//2 + n&1

    len2 = n//2

    d1 = {0:0}

    d2 = {0:0}

    for i in range(0,n,2):

        if v[i] in d1:

            d1[v[i]] += 1

        else:

            d1[v[i]] = 1

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

        if v[i] in d2:

            d2[v[i]] += 1

        else:

            d2[v[i]] = 1

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

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

    a1_key, a1_val = a[0]

    a2_key, a2_val = a[1]

    b1_key, b1_val = b[0]

    b2_key, b2_val = b[1]

    if a1_key != b1_key:

        print((n - a1_val - b1_val))

    else:

        print((min(n - a1_val - b2_val, n - a2_val - b1_val)))

if __name__ == "__main__":

    main()

def main():

    n = int(eval(input()))

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

    dic1 = {0:0}

    for i in range(0, n, 2):

        if v[i] in dic1:

            dic1[v[i]] += 1

        else:

            dic1[v[i]] = 1

    dic2 = {0:0}

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

        if v[i] in dic2:

            dic2[v[i]] += 1

        else:

            dic2[v[i]] = 1

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

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

    # print(dic1)

    # print(dic2)

    if dic1[0][0] != dic2[0][0]:

        print((n-dic1[0][1]-dic2[0][1]))

    else:

        print((min(n-dic1[0][1]-dic2[1][1], n-dic1[1][1]-dic2[0][1])))

if __name__ == "__main__":

    main()

import sys
import numpy as np

input = sys.stdin.readline

def main():
    n = int(eval(input()))
    v = np.array(list(map(int, input().split())))
    len1 = n // 2 + n & 1
    len2 = n // 2
    d1 = {0: 0}
    d2 = {0: 0}
    for i in range(0, n, 2):
        if v[i] in d1:
            d1[v[i]] += 1
        else:
            d1[v[i]] = 1
    for i in range(1, n, 2):
        if v[i] in d2:
            d2[v[i]] += 1
        else:
            d2[v[i]] = 1
    a = sorted(list(d1.items()), key=lambda x: (x[1], x[0]), reverse=True)
    b = sorted(list(d2.items()), key=lambda x: (x[1], -x[0]), reverse=True)
    a1_key, a1_val = a[0]
    a2_key, a2_val = a[1]
    b1_key, b1_val = b[0]
    b2_key, b2_val = b[1]
    if a1_key != b1_key:
        print((n - a1_val - b1_val))
    else:
        print((min(n - a1_val - b2_val, n - a2_val - b1_val)))

if __name__ == "__main__":
    main()

def main():
    n = int(eval(input()))
    v = list(map(int, input().split()))
    dic1 = {0: 0}
    for i in range(0, n, 2):
        if v[i] in dic1:
            dic1[v[i]] += 1
        else:
            dic1[v[i]] = 1
    dic2 = {0: 0}
    for i in range(1, n, 2):
        if v[i] in dic2:
            dic2[v[i]] += 1
        else:
            dic2[v[i]] = 1
    dic1 = sorted(list(dic1.items()), key=lambda x: -x[1])
    dic2 = sorted(list(dic2.items()), key=lambda x: -x[1])
    # print(dic1)
    # print(dic2)
    if dic1[0][0] != dic2[0][0]:
        print((n - dic1[0][1] - dic2[0][1]))
    else:
        print((min(n - dic1[0][1] - dic2[1][1], n - dic1[1][1] - dic2[0][1])))

if __name__ == "__main__":
    main()

-    v = np.array(list(map(int, input().split())))

+    v = list(map(int, input().split()))

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

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

+    dic1 = sorted(list(dic1.items()), key=lambda x: -x[1])

+    dic2 = sorted(list(dic2.items()), key=lambda x: -x[1])

-        print((min(n - a1_val - b2_val, n - a2_val - b1_val)))


Input:
from collections import Counter,defaultdict,deque

from heapq import heappop,heappush,heapify

import sys,bisect,math,itertools,fractions,pprint

sys.setrecursionlimit(10**8)

mod = 10**9+7

INF = float('inf')

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()

wv = [inpl() for _ in range(n)]

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

res = 0

tmp = min(n,5)

for i in range(tmp):

    dp = [[0] * (t+5) for _ in range(n+5)]

    for j in range(n):

        w,v = wv[j]

        for k in range(t):

            if k < w or i == j:

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

            else:

                dp[j+1][k] = max(dp[j][k], dp[j][k-w] + v)

    # print(dp[n][t-1])

    res = max(dp[n][t-1] + wv[i][1], res)

print(res)

from collections import Counter,defaultdict,deque

from heapq import heappop,heappush,heapify

import sys,bisect,math,itertools,fractions,pprint

sys.setrecursionlimit(10**8)

mod = 10**9+7

INF = float('inf')

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()

wv = [inpl() for _ in range(n)]

rwv = wv[::-1]

udp = [[0] * t for _ in range(n)]

ddp = [[0] * t for _ in range(n)]

for i in range(n-1):

    w,v = wv[i]

    for j in range(t):

        if j < w:

            udp[i+1][j] = udp[i][j]

        else:

            udp[i+1][j] = max(udp[i][j], udp[i][j-w] + v)

res = udp[n-1][t-1] + wv[n-1][1]

for i in range(n-1):

    w,v = rwv[i]

    for j in range(t):

        if j < w:

            ddp[i+1][j] = ddp[i][j]

        else:

            ddp[i+1][j] = max(ddp[i][j], ddp[i][j-w] + v)

    res = max(res,ddp[n-1][t-1] + wv[0][1])

# print(res)

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

    u = i; d = n-i-1

    mx = 0

    for j in range(t):

        tmp = udp[u][j] + ddp[d][t-1-j]

        mx = max(mx, tmp)

    res = max(res, mx + wv[i][1])

print(res)

from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions, pprint

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

def inp():
    return int(sys.stdin.readline())

def inpl():
    return list(map(int, sys.stdin.readline().split()))

n, t = inpl()
wv = [inpl() for _ in range(n)]
wv.sort(key=lambda x: x[1], reverse=True)
res = 0
tmp = min(n, 5)
for i in range(tmp):
    dp = [[0] * (t + 5) for _ in range(n + 5)]
    for j in range(n):
        w, v = wv[j]
        for k in range(t):
            if k < w or i == j:
                dp[j + 1][k] = dp[j][k]
            else:
                dp[j + 1][k] = max(dp[j][k], dp[j][k - w] + v)
    # print(dp[n][t-1])
    res = max(dp[n][t - 1] + wv[i][1], res)
print(res)

from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions, pprint

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

def inp():
    return int(sys.stdin.readline())

def inpl():
    return list(map(int, sys.stdin.readline().split()))

n, t = inpl()
wv = [inpl() for _ in range(n)]
rwv = wv[::-1]
udp = [[0] * t for _ in range(n)]
ddp = [[0] * t for _ in range(n)]
for i in range(n - 1):
    w, v = wv[i]
    for j in range(t):
        if j < w:
            udp[i + 1][j] = udp[i][j]
        else:
            udp[i + 1][j] = max(udp[i][j], udp[i][j - w] + v)
res = udp[n - 1][t - 1] + wv[n - 1][1]
for i in range(n - 1):
    w, v = rwv[i]
    for j in range(t):
        if j < w:
            ddp[i + 1][j] = ddp[i][j]
        else:
            ddp[i + 1][j] = max(ddp[i][j], ddp[i][j - w] + v)
    res = max(res, ddp[n - 1][t - 1] + wv[0][1])
# print(res)
for i in range(1, n - 1):
    u = i
    d = n - i - 1
    mx = 0
    for j in range(t):
        tmp = udp[u][j] + ddp[d][t - 1 - j]
        mx = max(mx, tmp)
    res = max(res, mx + wv[i][1])
print(res)

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

+        tmp = udp[u][j] + ddp[d][t - 1 - j]


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

s=[]

for _ in range(n):

	s.append(eval(input()))

s.sort()

print((''.join(s)))

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

li = []

for i in range(n):

    s = input()

    li.append(s)

li.sort()

for i in li:

    print(i, end="")

n, l = list(map(int, input().split()))
s = []
for _ in range(n):
    s.append(eval(input()))
s.sort()
print(("".join(s)))

-n, l = list(map(int, input().split()))
-s = []
-for _ in range(n):
-    s.append(eval(input()))
-s.sort()
-print(("".join(s)))
+n, m = map(int, input().split())
+li = []
+for i in range(n):
+    s = input()
+    li.append(s)
+li.sort()
+for i in li:
+    print(i, end="")


Input:
import sys

input = sys.stdin.buffer.readline

def main():

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

    XH = []

    for i in range(n):

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

        XH.append((x, h))

    XH.sort()

    from collections import deque

    q = deque()

    ans = 0

    t = 0

    import copy

    for x, h in XH:

        while q:

            r, s = q.popleft()

            if r+d < x:

                t -= s

            else:

                q.appendleft((r, s))

                break

        h -= t

        if h > 0:

            if h%a == 0:

                b = h//a

                ans += b

                t += a*b

                q.append((x+d, a*b))

            else:

                b = h//a+1

                ans += b

                t += a*b

                q.append((x+d, a*b))

    print(ans)

if __name__ == '__main__':

    main()

class BIT:

    def __init__(self, n):

        self.n = n

        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):

        for i, v in enumerate(init_val):

            self.add(i, v)

    def add(self, i, x):

        # i: 0-indexed

        i += 1 # to 1-indexed

        while i <= self.n:

            self.bit[i] += x

            i += (i & -i)

    def sum(self, i, j):

        # return sum of [i, j)

        # i, j: 0-indexed

        return self._sum(j) - self._sum(i)

    def _sum(self, i):

        # return sum of [0, i)

        # i: 0-indexed

        res = 0

        while i > 0:

            res += self.bit[i]

            i -= i & (-i)

        return res

class RangeAddBIT:

    def __init__(self, n):

        self.n = n

        self.bit1 = BIT(n)

        self.bit2 = BIT(n)

    def init(self, init_val):

        self.bit2.init(init_val)

    def add(self, l, r, x):

        # add x to [l, r)

        # l, r: 0-indexed

        self.bit1.add(l, x)

        self.bit1.add(r, -x)

        self.bit2.add(l, -x*l)

        self.bit2.add(r, x*r)

    def sum(self, l, r):

        # return sum of [l, r)

        # l, r: 0-indexed

        return self._sum(r) - self._sum(l)

    def _sum(self, i):

        # return sum of [0, i)

        # i: 0-indexed

        return self.bit1._sum(i)*i + self.bit2._sum(i)

import sys

import io, os

#input = sys.stdin.buffer.readline

input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

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

XH = []

for i in range(n):

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

    XH.append((x-d, h))

XH.sort()

X = []

H = []

for x, h in XH:

    X.append(x)

    H.append(h)

bit = RangeAddBIT(n+1)

bit.init(H)

import bisect

ans = 0

for i in range(n):

    h = bit.sum(i, i+1)

    if h > 0:

        q = (h+a-1)//a

        ans += q

        j = bisect.bisect_right(X, X[i]+2*d)

        bit.add(i, j, -q*a)

print(ans)

import sys

input = sys.stdin.buffer.readline

def main():
    n, d, a = list(map(int, input().split()))
    XH = []
    for i in range(n):
        x, h = list(map(int, input().split()))
        XH.append((x, h))
    XH.sort()
    from collections import deque

    q = deque()
    ans = 0
    t = 0
    import copy

    for x, h in XH:
        while q:
            r, s = q.popleft()
            if r + d < x:
                t -= s
            else:
                q.appendleft((r, s))
                break
        h -= t
        if h > 0:
            if h % a == 0:
                b = h // a
                ans += b
                t += a * b
                q.append((x + d, a * b))
            else:
                b = h // a + 1
                ans += b
                t += a * b
                q.append((x + d, a * b))
    print(ans)

if __name__ == "__main__":
    main()

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (self.n + 1)  # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1  # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += i & -i

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

class RangeAddBIT:
    def __init__(self, n):
        self.n = n
        self.bit1 = BIT(n)
        self.bit2 = BIT(n)

    def init(self, init_val):
        self.bit2.init(init_val)

    def add(self, l, r, x):
        # add x to [l, r)
        # l, r: 0-indexed
        self.bit1.add(l, x)
        self.bit1.add(r, -x)
        self.bit2.add(l, -x * l)
        self.bit2.add(r, x * r)

    def sum(self, l, r):
        # return sum of [l, r)
        # l, r: 0-indexed
        return self._sum(r) - self._sum(l)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        return self.bit1._sum(i) * i + self.bit2._sum(i)

import sys
import io, os

# input = sys.stdin.buffer.readline
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n, d, a = list(map(int, input().split()))
XH = []
for i in range(n):
    x, h = list(map(int, input().split()))
    XH.append((x - d, h))
XH.sort()
X = []
H = []
for x, h in XH:
    X.append(x)
    H.append(h)
bit = RangeAddBIT(n + 1)
bit.init(H)
import bisect

ans = 0
for i in range(n):
    h = bit.sum(i, i + 1)
    if h > 0:
        q = (h + a - 1) // a
        ans += q
        j = bisect.bisect_right(X, X[i] + 2 * d)
        bit.add(i, j, -q * a)
print(ans)

+        self.bit = [0] * (self.n + 1)  # 1-indexed

+        for i, v in enumerate(init_val):

+        return self._sum(j) - self._sum(i)

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

-        x, h = list(map(int, input().split()))

-                q.append((x + d, a * b))

-                q.append((x + d, a * b))

+        return self._sum(r) - self._sum(l)

+        return self.bit1._sum(i) * i + self.bit2._sum(i)

+input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

+n, d, a = list(map(int, input().split()))

+    x, h = list(map(int, input().split()))

+        j = bisect.bisect_right(X, X[i] + 2 * d)


Input:
import sys

from bisect import bisect_left as bl

input = sys.stdin.readline

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

ys = [0] * (H + 1)

xs = [0] * (W + 1)

a = []

base = 10 ** 6

for i in range(N):

  y, x = list(map(int, input().split()))

  ys[y] += 1

  xs[x] += 1

  a.append((y, x))

sy = sorted(ys[1: ])

sx = sorted(xs[1: ])

#print(sy, sx)

def check(k):

  res = 0

  for y, x in a:

    res -= (ys[y] + xs[x]) == k

    res += (ys[y] + xs[x]) > k

  #print(res, k)

  for y in sy:

    i = bl(sx, k - y)

    res += W - i

    #print(W - i, y, k, res)

  return res > 0

ok = 0

ng = N + 1

while ng - ok > 1:

  m = (ok + ng) // 2

  if check(m): ok = m

  else: ng = m

print(ok)

import sys

input = sys.stdin.readline

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

s = set()

ys = [0] * (H + 1)

xs = [0] * (W + 1)

for _ in range(M):

  y, x = list(map(int, input().split()))

  s.add((y, x))

  ys[y] += 1

  xs[x] += 1

res = 0

mxy = []

mx = max(ys)

for i in range(H + 1):

  if ys[i] == mx: mxy.append(i)

res += mx

mxx = []

mx = max(xs)

for i in range(W + 1):

  if xs[i] == mx: mxx.append(i)

res += mx - 1

for x in mxx:

  for y in mxy:

    if (y, x) in s: continue

    res += 1

    break

  else: continue

  break

print(res)

import sys
from bisect import bisect_left as bl

input = sys.stdin.readline
H, W, N = list(map(int, input().split()))
ys = [0] * (H + 1)
xs = [0] * (W + 1)
a = []
base = 10**6
for i in range(N):
    y, x = list(map(int, input().split()))
    ys[y] += 1
    xs[x] += 1
    a.append((y, x))
sy = sorted(ys[1:])
sx = sorted(xs[1:])
# print(sy, sx)
def check(k):
    res = 0
    for y, x in a:
        res -= (ys[y] + xs[x]) == k
        res += (ys[y] + xs[x]) > k
    # print(res, k)
    for y in sy:
        i = bl(sx, k - y)
        res += W - i
        # print(W - i, y, k, res)
    return res > 0

ok = 0
ng = N + 1
while ng - ok > 1:
    m = (ok + ng) // 2
    if check(m):
        ok = m
    else:
        ng = m
print(ok)

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

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


Input:
while True:

    n = int(eval(input()))

    if n == 0:

        break

    d = {}

    for _ in range(n):

        a, b = input().split()

        d[a] = b

    m = int(eval(input()))

    ans = ""

    for _ in range(m):

        a = input().strip()

        ans += d.get(a, a)

    print(ans)

while True:

    n = int(eval(input()))

    if n == 0:

        break

    d = {}

    for _ in range(n):

        a, b = input().split()

        d[a] = b

    m = int(eval(input()))

    ans = []

    for _ in range(m):

        a = input().strip()

        ans.append(d.get(a, a))

    print(("".join(ans)))

-    ans = ""
+    ans = []
-        ans += d.get(a, a)
-    print(ans)
+        ans.append(d.get(a, a))
+    print(("".join(ans)))


Input:
def main():

    from math import factorial

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

    p = 1000000007

    # 1:花束の組み合わせ総数の(mod p)

    n_all = pow(2, n, p) -1

    # 2:aとbの組み合わせ数の(mod p)を出す。

    a_comb = 0      # aの組み合わせ数

    a_comb_x = 1    # aの組み合わせ数計算の分子。n*(n-1)*…*(n-r+1)のmod p

    a_comb_y = 1    # aの組み合わせ数計算の分母。r!のmod pにおける逆元

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

        a_comb_x *= i

        a_comb_x %= p

    a_comb_y = factorial(a)

    a_comb_y = pow(a_comb_y, p-2,p)

    a_comb = (a_comb_x * a_comb_y) % p

    b_comb = 0

    b_comb_x = 1

    b_comb_y = 1

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

        b_comb_x *= i

        b_comb_x %= p

    b_comb_y = factorial(b)

    b_comb_y = pow(b_comb_y, p-2,p)

    b_comb = (b_comb_x * b_comb_y) % p

    # 3: 1の結果から２のそれをマイナスする。結果が負になったら+p

    r = n_all - a_comb

    if r < 0:

        r += p

    r -= b_comb

    if r < 0:

        r += p

    print(r)

main()

def main():

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

    p = 10 ** 9 + 7

    r = pow(2, n, p) - 1

    if (n - a) < a:

        a = n - a

    if (n - b) < b:

        b = n - b

    fac_na = 1

    for i1 in range(n, n - a, -1):

        fac_na = (fac_na * i1) % p

    fac_nb = 1

    for i1 in range(n, n - b, -1):

        fac_nb = (fac_nb * i1) % p

    fac_a = 1

    for i1 in range(1, a+1):

        fac_a = (fac_a * i1) % p

    fac_b = 1

    for i1 in range(1, b+1):

        fac_b = (fac_b * i1) % p

    an = fac_na * pow(fac_a, p-2, p)

    bn = fac_nb * pow(fac_b, p-2, p)

    an = an % p

    bn = bn % p

    r -= an

    if r < 0:

        r += p

    r -= bn

    if r < 0:

        r += p

    if r < 0:

        r += p

    print((int(r)))

if __name__ == '__main__':

    main()

def main():
    from math import factorial

    n, a, b = list(map(int, input().split()))
    p = 1000000007
    # 1:花束の組み合わせ総数の(mod p)
    n_all = pow(2, n, p) - 1
    # 2:aとbの組み合わせ数の(mod p)を出す。
    a_comb = 0  # aの組み合わせ数
    a_comb_x = 1  # aの組み合わせ数計算の分子。n*(n-1)*…*(n-r+1)のmod p
    a_comb_y = 1  # aの組み合わせ数計算の分母。r!のmod pにおける逆元
    for i in range(n, n - a, -1):
        a_comb_x *= i
        a_comb_x %= p
    a_comb_y = factorial(a)
    a_comb_y = pow(a_comb_y, p - 2, p)
    a_comb = (a_comb_x * a_comb_y) % p
    b_comb = 0
    b_comb_x = 1
    b_comb_y = 1
    for i in range(n, n - b, -1):
        b_comb_x *= i
        b_comb_x %= p
    b_comb_y = factorial(b)
    b_comb_y = pow(b_comb_y, p - 2, p)
    b_comb = (b_comb_x * b_comb_y) % p
    # 3: 1の結果から２のそれをマイナスする。結果が負になったら+p
    r = n_all - a_comb
    if r < 0:
        r += p
    r -= b_comb
    if r < 0:
        r += p
    print(r)

main()

def main():
    n, a, b = list(map(int, input().split()))
    p = 10**9 + 7
    r = pow(2, n, p) - 1
    if (n - a) < a:
        a = n - a
    if (n - b) < b:
        b = n - b
    fac_na = 1
    for i1 in range(n, n - a, -1):
        fac_na = (fac_na * i1) % p
    fac_nb = 1
    for i1 in range(n, n - b, -1):
        fac_nb = (fac_nb * i1) % p
    fac_a = 1
    for i1 in range(1, a + 1):
        fac_a = (fac_a * i1) % p
    fac_b = 1
    for i1 in range(1, b + 1):
        fac_b = (fac_b * i1) % p
    an = fac_na * pow(fac_a, p - 2, p)
    bn = fac_nb * pow(fac_b, p - 2, p)
    an = an % p
    bn = bn % p
    r -= an
    if r < 0:
        r += p
    r -= bn
    if r < 0:
        r += p
    if r < 0:
        r += p
    print((int(r)))

if __name__ == "__main__":
    main()

-    # 1:花束の組み合わせ総数の(mod p)

-    # 2:aとbの組み合わせ数の(mod p)を出す。

-    a_comb_x = 1  # aの組み合わせ数計算の分子。n*(n-1)*…*(n-r+1)のmod p

-    a_comb_y = 1  # aの組み合わせ数計算の分母。r!のmod pにおける逆元

-    # 3: 1の結果から２のそれをマイナスする。結果が負になったら+p


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

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

S = sorted(S)

tri_num=0

for i in range(N-2):

    k = i+2

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

        while k < N:

            if S[i] + S[j] <= S[k]:

                tri_num = tri_num + k-j-1

                if k == i+2:

                    k+=1

                break

            if k == N-1:

                tri_num = tri_num + k-j

                break

            k+=1

print(tri_num)

N=int(eval(input()))

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

S = sorted(S,reverse=True)

tri_num=0

for i in range(N-2):

    j = i+1

    k = N-1

    while k > j:

        if S[i] < S[j] + S[k]:

            tri_num += k-j

            j+=1

        else:

            k-=1

print(tri_num)

N = int(eval(input()))
S = list(map(int, input().split()))
S = sorted(S)
tri_num = 0
for i in range(N - 2):
    k = i + 2
    for j in range(i + 1, N - 1):
        while k < N:
            if S[i] + S[j] <= S[k]:
                tri_num = tri_num + k - j - 1
                if k == i + 2:
                    k += 1
                break
            if k == N - 1:
                tri_num = tri_num + k - j
                break
            k += 1
print(tri_num)

N = int(eval(input()))
S = list(map(int, input().split()))
S = sorted(S, reverse=True)
tri_num = 0
for i in range(N - 2):
    j = i + 1
    k = N - 1
    while k > j:
        if S[i] < S[j] + S[k]:
            tri_num += k - j
            j += 1
        else:
            k -= 1
print(tri_num)

-                tri_num = tri_num + k - j - 1

-                tri_num = tri_num + k - j


Input:
import collections, itertools

H, W, ch, cw, dh, dw, *S = open(0).read().split()

H, W, ch, cw, dh, dw = [int(_) + 1 for _ in [H, W, ch, cw, dh, dw]]

H += 3

W += 3

S = ['#' * W] + ['#' * W] + ['##' + s + '##'

                             for s in S] + ['#' * W] + ['#' * W]

Dist = [[None] * W for _ in range(H)]

Q = collections.deque([(0, ch, cw)])

while Q:

    d, ox, oy = Q.popleft()

    if Dist[ox][oy] is not None:

        continue

    stack = [[ox, oy]]

    warp = set()

    while stack:

        x, y = stack.pop()

        if (x, y) == (dh, dw):

            print(d)

            exit()

        Dist[x][y] = d

        for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]:

            nx, ny = x + dx, y + dy

            if Dist[nx][ny] is None:

                if S[nx][ny] == '.':

                    stack += [[nx, ny]]

                else:

                    warp.add((x, y))

    for x, y in warp:

        for dx, dy in itertools.product(list(range(-2, 3)), repeat=2):

            nx, ny = x + dx, y + dy

            if Dist[nx][ny] is None and S[nx][ny] == '.':

                Q += [(d + 1, nx, ny)]

print((-1))

H, W, ch, cw, dh, dw, *S = open(0).read().split()

H, W, ch, cw, dh, dw = [int(_) for _ in [H, W, ch, cw, dh, dw]]

ch -= 1

cw -= 1

dh -= 1

dw -= 1

stack = [[ch, cw]]

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

d = 0

while True:

    stack2 = []

    warp = set()

    while stack:

        x, y = stack.pop()

        if (x, y) == (dh, dw):

            print(d)

            exit()

        if dist[x][y] is not None:

            continue

        dist[x][y] = d

        for dx, dy in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

            nx, ny = x + dx, y + dy

            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None:

                if S[nx][ny] == '.':

                    stack += [[nx, ny]]

                else:

                    warp.add((x, y))

    for x, y in warp:

        for dx in range(-2, 3):

            for dy in range(-2, 3):

                nx, ny = x + dx, y + dy

                if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None and S[

                        nx][ny] == '.':

                    stack2 += [[nx, ny]]

    if stack2 == []:

        break

    else:

        d += 1

        stack = stack2

print((-1))

import collections, itertools

H, W, ch, cw, dh, dw, *S = open(0).read().split()
H, W, ch, cw, dh, dw = [int(_) + 1 for _ in [H, W, ch, cw, dh, dw]]
H += 3
W += 3
S = ["#" * W] + ["#" * W] + ["##" + s + "##" for s in S] + ["#" * W] + ["#" * W]
Dist = [[None] * W for _ in range(H)]
Q = collections.deque([(0, ch, cw)])
while Q:
    d, ox, oy = Q.popleft()
    if Dist[ox][oy] is not None:
        continue
    stack = [[ox, oy]]
    warp = set()
    while stack:
        x, y = stack.pop()
        if (x, y) == (dh, dw):
            print(d)
            exit()
        Dist[x][y] = d
        for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
            nx, ny = x + dx, y + dy
            if Dist[nx][ny] is None:
                if S[nx][ny] == ".":
                    stack += [[nx, ny]]
                else:
                    warp.add((x, y))
    for x, y in warp:
        for dx, dy in itertools.product(list(range(-2, 3)), repeat=2):
            nx, ny = x + dx, y + dy
            if Dist[nx][ny] is None and S[nx][ny] == ".":
                Q += [(d + 1, nx, ny)]
print((-1))

H, W, ch, cw, dh, dw, *S = open(0).read().split()
H, W, ch, cw, dh, dw = [int(_) for _ in [H, W, ch, cw, dh, dw]]
ch -= 1
cw -= 1
dh -= 1
dw -= 1
stack = [[ch, cw]]
dist = [[None] * W for _ in range(H)]
d = 0
while True:
    stack2 = []
    warp = set()
    while stack:
        x, y = stack.pop()
        if (x, y) == (dh, dw):
            print(d)
            exit()
        if dist[x][y] is not None:
            continue
        dist[x][y] = d
        for dx, dy in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None:
                if S[nx][ny] == ".":
                    stack += [[nx, ny]]
                else:
                    warp.add((x, y))
    for x, y in warp:
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                nx, ny = x + dx, y + dy
                if (
                    0 <= nx < H
                    and 0 <= ny < W
                    and dist[nx][ny] is None
                    and S[nx][ny] == "."
                ):
                    stack2 += [[nx, ny]]
    if stack2 == []:
        break
    else:
        d += 1
        stack = stack2
print((-1))

-H, W, ch, cw, dh, dw = [int(_) + 1 for _ in [H, W, ch, cw, dh, dw]]

+H, W, ch, cw, dh, dw = [int(_) for _ in [H, W, ch, cw, dh, dw]]

+            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None:

-        for dx, dy in itertools.product(list(range(-2, 3)), repeat=2):

-            if Dist[nx][ny] is None and S[nx][ny] == ".":

+                nx, ny = x + dx, y + dy

+                    and dist[nx][ny] is None

+                    stack2 += [[nx, ny]]


Input:
n=eval(input())

num=[]

for i in range(4):

    num.append(int(n[i]))

o=["+","+","+"]

op=[1,1,1]

def ans():

    return num[0]+op[0]*num[1]+op[1]*num[2]+op[2]*num[3]

if ans()!=7:

    for i in range(3):

        op[i]=-1

        if ans()==7:

            break

        op[i]=1

if ans()!=7:

    for i in range(-1,2):

        op[i]=-1

        op[i+1]=-1

        if ans()==7:

            break

        op[i]=1

        op[i+1]=1

if ans()!=7:

    for i in range(3):

        op[i]=-1

for i in range(3):

    if op[i]==-1:

        o[i]="-"

print((n[0]+o[0]+n[1]+o[1]+n[2]+o[2]+n[3]+"=7"))


Input:
from collections import defaultdict

def main():

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

    d = defaultdict(int)

    for _ in range(N):

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

        for y in range(-2, 1):

            for x in range(-2, 1):

                n_y, n_x = y + a, x + b

                if 0 < n_y <= H - 2 and 0 < n_x <= W - 2:

                    d[(n_y, n_x)] += 1

    ans = [0] * 10

    for k, v in d.items():

        ans[v] += 1

    ans[0] = (H - 2) * (W - 2) - sum(ans)

    print(*ans, sep="\n")

if __name__ == '__main__':

    main()

from collections import defaultdict

def main():

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

    d = defaultdict(int)

    for _ in range(N):

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

        a, b = a - 1, b - 1

        for y in range(-2, 1):

            for x in range(-2, 1):

                ny, nx = a + y, b + x

                if 0 <= ny < H - 2 and 0 <= nx < W - 2:

                    d[(ny, nx)] += 1

    ans = [0] * 10

    for v in d.values():

        ans[v] += 1

    ans[0] = (H - 2) * (W - 2) - sum(ans)

    print(*ans, sep="\n")

if __name__ == '__main__':

    main()

from collections import defaultdict

def main():
    H, W, N = map(int, input().split())
    d = defaultdict(int)
    for _ in range(N):
        a, b = map(int, input().split())
        for y in range(-2, 1):
            for x in range(-2, 1):
                n_y, n_x = y + a, x + b
                if 0 < n_y <= H - 2 and 0 < n_x <= W - 2:
                    d[(n_y, n_x)] += 1
    ans = [0] * 10
    for k, v in d.items():
        ans[v] += 1
    ans[0] = (H - 2) * (W - 2) - sum(ans)
    print(*ans, sep="\n")

if __name__ == "__main__":
    main()

from collections import defaultdict

def main():
    H, W, N = map(int, input().split())
    d = defaultdict(int)
    for _ in range(N):
        a, b = map(int, input().split())
        a, b = a - 1, b - 1
        for y in range(-2, 1):
            for x in range(-2, 1):
                ny, nx = a + y, b + x
                if 0 <= ny < H - 2 and 0 <= nx < W - 2:
                    d[(ny, nx)] += 1
    ans = [0] * 10
    for v in d.values():
        ans[v] += 1
    ans[0] = (H - 2) * (W - 2) - sum(ans)
    print(*ans, sep="\n")

if __name__ == "__main__":
    main()

-                n_y, n_x = y + a, x + b


Input:
n = int(input())

c = 2 * (n / 11)

h = {0:0}

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

	h[i] = 1

for i in range(7, 12):

	h[i] = 2

if n in h:

	print(h[n])

else:

	print(h[n % 11] + c)

n = int(input())
c = 2 * (n / 11)
h = {0: 0}
for i in range(1, 6 + 1):
    h[i] = 1
for i in range(7, 12):
    h[i] = 2
if n in h:
    print(h[n])
else:
    print(h[n % 11] + c)


Input:
# 2019-11-24 20:59:47(JST)

import sys

def main():

    n = int(sys.stdin.readline().rstrip())

    m = list(map(int, sys.stdin.read().split()))

    ab = list(zip(m, m))

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

    for a, b in ab:

        graph[a].append(b)

        graph[b].append(a)

    root = 1

    parent = [0] * (n + 1)

    order = []

    stack = [root]

    while stack:

        x = stack.pop()

        order.append(x)

        for y in graph[x]:

            if y == parent[x]:

                continue

            parent[y] = x

            stack.append(y)

    color = [-1] * (n + 1)

    for x in order:

        ng = color[x]

        c = 1

        for y in graph[x]:

            if y == parent[x]:

                continue

            if c == ng:

                c += 1

            color[y] = c

            c += 1

    res = []

    for a, b in ab:

        if parent[a] == b:

            res.append(color[a])

        else:

            res.append(color[b])

    print((max(res)))

    print(('\n'.join(map(str, res))))

if __name__ == '__main__':

    main()

# 2019-11-24 20:59:47(JST)

import sys

def main():

    n = int(sys.stdin.readline().rstrip())

    m = list(map(int, sys.stdin.read().split()))

    ab = list(zip(m, m)) # 2回以上使えるようにlist化

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

    for a, b in ab:

        graph[a].append(b)

        graph[b].append(a)

    root = 1

    parent = [None] * (n + 1)

    # treeなのでループがないため、parentが定義できる

    checked = []

    to_check = [root]

    # dfs: depth first search では stack

    # bfs: breadth first search では queueを使う

    # 今回は stack

    while to_check:

        x = to_check.pop()

        # stackから要素を取り除き、xにset

        for y in graph[x]: # xから直接つながっているverticesについて

            if y == parent[x]:

                continue

            # yがxの親でなければ

            parent[y] = x # xを親にして

            to_check.append(y) # 子であるyをto_checkにstackしていく

        checked.append(x) # 探索が終わったらvertexを記録

    color = [-1] * (n + 1) # 初期は0でもいいが、あえてngであることがわかりやすいように-1にしている

    for x in checked: # checkされた順になっている

        ng = color[x] # 最初は-1, colorは1からなので最初はngにはならない

        c = 1

        for y in graph[x]:

            if y == parent[x]:

                continue

            if c == ng:

                c += 1

            color[y] = c

            c += 1

    res = []

    for a, b in ab:

        if a == parent[b]:

            res.append(color[b])

        else: # 木なので parent[a] == b

            res.append(color[a])

    print((max(res)))

    print(('\n'.join(map(str, res))))

if __name__ == '__main__':

    main()

# 2019-11-24 20:59:47(JST)
import sys

def main():
    n = int(sys.stdin.readline().rstrip())
    m = list(map(int, sys.stdin.read().split()))
    ab = list(zip(m, m))
    graph = [[] for _ in range(n + 1)]
    for a, b in ab:
        graph[a].append(b)
        graph[b].append(a)
    root = 1
    parent = [0] * (n + 1)
    order = []
    stack = [root]
    while stack:
        x = stack.pop()
        order.append(x)
        for y in graph[x]:
            if y == parent[x]:
                continue
            parent[y] = x
            stack.append(y)
    color = [-1] * (n + 1)
    for x in order:
        ng = color[x]
        c = 1
        for y in graph[x]:
            if y == parent[x]:
                continue
            if c == ng:
                c += 1
            color[y] = c
            c += 1
    res = []
    for a, b in ab:
        if parent[a] == b:
            res.append(color[a])
        else:
            res.append(color[b])
    print((max(res)))
    print(("\n".join(map(str, res))))

if __name__ == "__main__":
    main()

# 2019-11-24 20:59:47(JST)
import sys

def main():
    n = int(sys.stdin.readline().rstrip())
    m = list(map(int, sys.stdin.read().split()))
    ab = list(zip(m, m))  # 2回以上使えるようにlist化
    graph = [[] for _ in range(n + 1)]
    for a, b in ab:
        graph[a].append(b)
        graph[b].append(a)
    root = 1
    parent = [None] * (n + 1)
    # treeなのでループがないため、parentが定義できる
    checked = []
    to_check = [root]
    # dfs: depth first search では stack
    # bfs: breadth first search では queueを使う
    # 今回は stack
    while to_check:
        x = to_check.pop()
        # stackから要素を取り除き、xにset
        for y in graph[x]:  # xから直接つながっているverticesについて
            if y == parent[x]:
                continue
            # yがxの親でなければ
            parent[y] = x  # xを親にして
            to_check.append(y)  # 子であるyをto_checkにstackしていく
        checked.append(x)  # 探索が終わったらvertexを記録
    color = [-1] * (n + 1)  # 初期は0でもいいが、あえてngであることがわかりやすいように-1にしている
    for x in checked:  # checkされた順になっている
        ng = color[x]  # 最初は-1, colorは1からなので最初はngにはならない
        c = 1
        for y in graph[x]:
            if y == parent[x]:
                continue
            if c == ng:
                c += 1
            color[y] = c
            c += 1
    res = []
    for a, b in ab:
        if a == parent[b]:
            res.append(color[b])
        else:  # 木なので parent[a] == b
            res.append(color[a])
    print((max(res)))
    print(("\n".join(map(str, res))))

if __name__ == "__main__":
    main()

+    ab = list(zip(m, m))  # 2回以上使えるようにlist化

+    # treeなのでループがないため、parentが定義できる

+    # bfs: breadth first search では queueを使う

+        # stackから要素を取り除き、xにset

+        for y in graph[x]:  # xから直接つながっているverticesについて

+            # yがxの親でなければ

+            to_check.append(y)  # 子であるyをto_checkにstackしていく

+        checked.append(x)  # 探索が終わったらvertexを記録

+    color = [-1] * (n + 1)  # 初期は0でもいいが、あえてngであることがわかりやすいように-1にしている

+    for x in checked:  # checkされた順になっている

+        ng = color[x]  # 最初は-1, colorは1からなので最初はngにはならない


Input:
import itertools

import math

import string

import collections

from collections import Counter

from collections import deque

from operator import itemgetter

import sys

sys.setrecursionlimit(2*10**5)

INF = 2**60

def readints():

    return list(map(int, input().split()))

def nCr(n, r):

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

def has_duplicates2(seq):

    seen = []

    for item in seq:

        if not(item in seen):

            seen.append(item)

    return len(seq) != len(seen)

def divisor(n):

    divisor = []

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

        if n % i == 0:

            divisor.append(i)

    return divisor

# coordinates

dx = [-1, -1, -1, 0, 0, 1, 1, 1]

dy = [-1, 0, 1, -1, 1, -1, 0, 1]

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

ab = [readints() for _ in range(n)]

# print(ab)

abab = sorted(ab)

# print(abab)

tmp = 0

for i in range(n):

    tmp += abab[i][1]

    if tmp >= k:

        print((abab[i][0]))

        break

import math

import string

import collections

from collections import Counter

from collections import deque

from decimal import Decimal

import sys

import fractions

def readints():

    return list(map(int, input().split()))

def nCr(n, r):

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

def has_duplicates2(seq):

    seen = []

    for item in seq:

        if not(item in seen):

            seen.append(item)

    return len(seq) != len(seen)

def divisor(n):

    divisor = []

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

        if n % i == 0:

            divisor.append(i)

    return divisor

# coordinates

dx = [-1, -1, -1, 0, 0, 1, 1, 1]

dy = [-1, 0, 1, -1, 1, -1, 0, 1]

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

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

for i in range(n):

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

    x[a] += b

y = 0

for i in range(len(x)):

    y += x[i]

    if y >= k:

        print(i)

        exit()

import itertools
import math
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys

sys.setrecursionlimit(2 * 10**5)
INF = 2**60

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

def nCr(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not (item in seen):
            seen.append(item)
    return len(seq) != len(seen)

def divisor(n):
    divisor = []
    for i in range(1, n + 1):
        if n % i == 0:
            divisor.append(i)
    return divisor

# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n, k = list(map(int, input().split()))
ab = [readints() for _ in range(n)]
# print(ab)
abab = sorted(ab)
# print(abab)
tmp = 0
for i in range(n):
    tmp += abab[i][1]
    if tmp >= k:
        print((abab[i][0]))
        break

import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions

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

def nCr(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not (item in seen):
            seen.append(item)
    return len(seq) != len(seen)

def divisor(n):
    divisor = []
    for i in range(1, n + 1):
        if n % i == 0:
            divisor.append(i)
    return divisor

# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n, k = list(map(int, input().split()))
x = [0] * ((10**5) + 1)
for i in range(n):
    a, b = list(map(int, input().split()))
    x[a] += b
y = 0
for i in range(len(x)):
    y += x[i]
    if y >= k:
        print(i)
        exit()

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


Input:
def main():

  n=list(eval(input()))

  n=[int(i) for i in n]

  l=len(n)

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

  if n[0]>=5:

    dp[0][1]=1

  for i in range(l):

      dp[i+1][0]=min(dp[i][0]+n[i],dp[i][1]+10-n[i])

      dp[i+1][1]=min(dp[i][0]+n[i]+1,dp[i][1]+9-n[i])

  #print(dp)

  print((dp[l][0]))

if __name__=='__main__':

    main()

def main():

  n=list(eval(input()))

  n=[int(i) for i in reversed(n)]

  l=len(n)

  n.append(0)

  ans=0

  for i in range(l):

    if n[i]<=4:

      a=n[i]

    elif n[i]==5:

      if n[i+1]<=4:

        a=n[i]

      else:

        a=10-n[i]

        n[i+1]+=1

    else:

      n[i+1]+=1

      a=10-n[i]

    ans+=a

  ans+=n[l]

  print(ans)

if __name__=='__main__':

    main()

def main():
    n = list(eval(input()))
    n = [int(i) for i in reversed(n)]
    l = len(n)
    n.append(0)
    ans = 0
    for i in range(l):
        if n[i] <= 4:
            a = n[i]
        elif n[i] == 5:
            if n[i + 1] <= 4:
                a = n[i]
            else:
                a = 10 - n[i]
                n[i + 1] += 1
        else:
            n[i + 1] += 1
            a = 10 - n[i]
        ans += a
    ans += n[l]
    print(ans)

if __name__ == "__main__":
    main()


Input:
import numpy as np

n=int(eval(input()))

abc=[]

for _ in range(n):

      abc.append(list(map(int,input().split())))

dp=np.zeros((n+1,3),np.int64)

# dp[i][0]:i日目でaをとった時の最大。dp[0]=[0,0,0]。求めるのはmax(dp[n])

dp[1]=np.array(abc[0])

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

      dp[i][0]=abc[i-1][0]+max(dp[i-1][1],dp[i-1][2])

      dp[i][1]=abc[i-1][1]+max(dp[i-1][0],dp[i-1][2])

      dp[i][2]=abc[i-1][2]+max(dp[i-1][0],dp[i-1][1])

print((int(np.max(dp[-1]))))

import numpy as np

n=int(eval(input()))

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

dp=np.zeros((n+1,3),np.int64)

# dp[i+1][j]:i日目にjで遊んだ時の最大の幸福度

# dp[0][j]=0,dp[1][j]=abc[0][j]

dp[1]=abc[0]

for i in range(1,n):

      dp[i+1][0]=abc[i][0]+max(dp[i][1],dp[i][2])

      dp[i+1][1]=abc[i][1]+max(dp[i][0],dp[i][2])

      dp[i+1][2]=abc[i][2]+max(dp[i][0],dp[i][1])

print((np.max(dp[-1])))

import numpy as np

n = int(eval(input()))
abc = []
for _ in range(n):
    abc.append(list(map(int, input().split())))
dp = np.zeros((n + 1, 3), np.int64)
# dp[i][0]:i日目でaをとった時の最大。dp[0]=[0,0,0]。求めるのはmax(dp[n])
dp[1] = np.array(abc[0])
for i in range(2, n + 1):
    dp[i][0] = abc[i - 1][0] + max(dp[i - 1][1], dp[i - 1][2])
    dp[i][1] = abc[i - 1][1] + max(dp[i - 1][0], dp[i - 1][2])
    dp[i][2] = abc[i - 1][2] + max(dp[i - 1][0], dp[i - 1][1])
print((int(np.max(dp[-1]))))

import numpy as np

n = int(eval(input()))
abc = np.array([list(map(int, input().split())) for _ in range(n)])
dp = np.zeros((n + 1, 3), np.int64)
# dp[i+1][j]:i日目にjで遊んだ時の最大の幸福度
# dp[0][j]=0,dp[1][j]=abc[0][j]
dp[1] = abc[0]
for i in range(1, n):
    dp[i + 1][0] = abc[i][0] + max(dp[i][1], dp[i][2])
    dp[i + 1][1] = abc[i][1] + max(dp[i][0], dp[i][2])
    dp[i + 1][2] = abc[i][2] + max(dp[i][0], dp[i][1])
print((np.max(dp[-1])))

-    abc.append(list(map(int, input().split())))

+abc = np.array([list(map(int, input().split())) for _ in range(n)])

-# dp[i][0]:i日目でaをとった時の最大。dp[0]=[0,0,0]。求めるのはmax(dp[n])

+# dp[i+1][j]:i日目にjで遊んだ時の最大の幸福度


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

p = 1

i = 1

while h > 1:

    h = h // 2

    p += 2 ** i

    i += 1

print(p)

h = int(eval(input()))

ans = 1

i = 1

while h > 1:

    h = h // 2

    ans += 2 ** i

    i += 1

print(ans)

h = int(eval(input()))
p = 1
i = 1
while h > 1:
    h = h // 2
    p += 2**i
    i += 1
print(p)

h = int(eval(input()))
ans = 1
i = 1
while h > 1:
    h = h // 2
    ans += 2**i
    i += 1
print(ans)

-p = 1
+ans = 1
-    p += 2**i
+    ans += 2**i
-print(p)
+print(ans)


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

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

temp=x

ans=0

a.sort()

i=-1

while True:

    i+=1

    x-=a[i]

    if x<0:

        break

    ans+=1

    if ans==N:

        break

if sum(a)<temp:

    ans-=1

print(ans)

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

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

man=[0]*N

a.sort()

ans=0

for i in range(N):

    if a[i]<=x:

        x-=a[i]

        a[i]=0

        ans+=1

    else:

        break

if ans==N:

    if x!=0:

        ans-=1

if ans<0:

    ans=0

print(ans)

N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
temp = x
ans = 0
a.sort()
i = -1
while True:
    i += 1
    x -= a[i]
    if x < 0:
        break
    ans += 1
    if ans == N:
        break
if sum(a) < temp:
    ans -= 1
print(ans)

N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
man = [0] * N
a.sort()
ans = 0
for i in range(N):
    if a[i] <= x:
        x -= a[i]
        a[i] = 0
        ans += 1
    else:
        break
if ans == N:
    if x != 0:
        ans -= 1
if ans < 0:
    ans = 0
print(ans)

-temp = x
+man = [0] * N
+a.sort()
-a.sort()
-i = -1
-while True:
-    i += 1
-    x -= a[i]
-    if x < 0:
+for i in range(N):
+    if a[i] <= x:
+        x -= a[i]
+        a[i] = 0
+        ans += 1
+    else:
-    ans += 1
-    if ans == N:
-        break
-if sum(a) < temp:
-    ans -= 1
+if ans == N:
+    if x != 0:
+        ans -= 1
+if ans < 0:
+    ans = 0


Input:
c = [list(map(int, input().split())) for i in range(3)]

res = 0

ans = 0

for i in range(3):

  res += sum(c[i])

  ans += c[i][i]*3

if res == ans:

  print('Yes')

else:

  print('No')

c = [list(map(int, input().split())) for i in range(3)]
res = 0
ans = 0
for i in range(3):
    res += sum(c[i])
    ans += c[i][i] * 3
if res == ans:
    print("Yes")
else:
    print("No")


Input:
def main():

    from heapq import heappush, heappop

    import sys

    sys.setrecursionlimit(10 ** 7)

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

    event=[]

    for _ in range(n):

        s,t,x=list(map(int,input().split()))

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

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

    for i in range(q):

        event.append((int(eval(input())),2,i))

    event.sort()

    ans=[0]*q

    stop=set()

    hq=[]

    for t,op,x in event:

        if op==1:

            heappush(hq,x)

            stop.add(x)

        elif op==-1:

            stop.remove(x)

        else:

            while hq and hq[0] not in stop:

                heappop(hq)

            ans[x]=hq[0] if hq else -1

    for x in ans:

        print(x)

main()

from heapq import heappush, heappop

import sys

sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

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

STX = [[int(x) for x in readline().split()] for _ in range(N)]

EVENT = []

for s, t, x in STX:

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

    EVENT.append((t-x, 0, x))

for i in range(Q):

    d = int(readline())

    EVENT.append((d, 2, i))

ans = [-1] * Q

EVENT.sort()

STOP = set()

h = []

for t, op, x in EVENT:

    if op == 2 and STOP:

        while h and h[0] not in STOP:

            heappop(h)

        ans[x] = h[0]

    elif op == 1:

        STOP.add(x)

        heappush(h, x)

    elif op == 0:

        STOP.remove(x)

print(("\n".join(map(str, ans))))

def main():
    from heapq import heappush, heappop
    import sys

    sys.setrecursionlimit(10**7)
    n, q = list(map(int, input().split()))
    event = []
    for _ in range(n):
        s, t, x = list(map(int, input().split()))
        event.append((s - x, 1, x))
        event.append((t - x, -1, x))
    for i in range(q):
        event.append((int(eval(input())), 2, i))
    event.sort()
    ans = [0] * q
    stop = set()
    hq = []
    for t, op, x in event:
        if op == 1:
            heappush(hq, x)
            stop.add(x)
        elif op == -1:
            stop.remove(x)
        else:
            while hq and hq[0] not in stop:
                heappop(hq)
            ans[x] = hq[0] if hq else -1
    for x in ans:
        print(x)

main()

from heapq import heappush, heappop
import sys

sys.setrecursionlimit(10**7)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, Q = list(map(int, readline().split()))
STX = [[int(x) for x in readline().split()] for _ in range(N)]
EVENT = []
for s, t, x in STX:
    EVENT.append((s - x, 1, x))
    EVENT.append((t - x, 0, x))
for i in range(Q):
    d = int(readline())
    EVENT.append((d, 2, i))
ans = [-1] * Q
EVENT.sort()
STOP = set()
h = []
for t, op, x in EVENT:
    if op == 2 and STOP:
        while h and h[0] not in STOP:
            heappop(h)
        ans[x] = h[0]
    elif op == 1:
        STOP.add(x)
        heappush(h, x)
    elif op == 0:
        STOP.remove(x)
print(("\n".join(map(str, ans))))

-    from heapq import heappush, heappop

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

-        s, t, x = list(map(int, input().split()))

-        event.append((int(eval(input())), 2, i))

-            while hq and hq[0] not in stop:

-            ans[x] = hq[0] if hq else -1

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

+STX = [[int(x) for x in readline().split()] for _ in range(N)]


Input:
from collections import defaultdict

def main():

    eval(input())

    array = sorted(int(x) for x in input().split())

    maximum = 1000003

    counter = defaultdict(int)

    for x in array:

        counter[x] += 1

    maximum = array[-1] + 1

    candidates = [True] * maximum

    for key, value in list(counter.items()):

        if value > 1:  # duplicate

            candidates[key] = False

        for i in range(key * 2, maximum, key):

            candidates[i] = False

    ans = sum(candidates[x] for x in array)

    print(ans)

if __name__ == '__main__':

    main()

def main():

    eval(input())

    array = sorted(int(x) for x in input().split())

    maximum = array[-1] + 1

    counter = [0] * maximum

    for x in array:

        if counter[x] != 0:

            counter[x] = 2

            continue

        else:

            for multiple in range(x, maximum, x):

                counter[multiple] += 1

    ans = 0

    for x in array:

        if counter[x] == 1:

            ans += 1

    print(ans)

if __name__ == '__main__':

    main()

from collections import defaultdict

def main():
    eval(input())
    array = sorted(int(x) for x in input().split())
    maximum = 1000003
    counter = defaultdict(int)
    for x in array:
        counter[x] += 1
    maximum = array[-1] + 1
    candidates = [True] * maximum
    for key, value in list(counter.items()):
        if value > 1:  # duplicate
            candidates[key] = False
        for i in range(key * 2, maximum, key):
            candidates[i] = False
    ans = sum(candidates[x] for x in array)
    print(ans)

if __name__ == "__main__":
    main()

def main():
    eval(input())
    array = sorted(int(x) for x in input().split())
    maximum = array[-1] + 1
    counter = [0] * maximum
    for x in array:
        if counter[x] != 0:
            counter[x] = 2
            continue
        else:
            for multiple in range(x, maximum, x):
                counter[multiple] += 1
    ans = 0
    for x in array:
        if counter[x] == 1:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

-    for key, value in list(counter.items()):

-        for i in range(key * 2, maximum, key):

-    ans = sum(candidates[x] for x in array)

+            for multiple in range(x, maximum, x):


Input:
s = eval(input())

t = eval(input())

from collections import defaultdict

td = defaultdict(set)

sd = defaultdict(set)

for i in range(len(s)):

        td[t[i]].add(s[i])

        sd[s[i]].add(t[i])

for tv, sv in zip(list(td.values()), list(sd.values())):

    if (len(tv) > 1) or (len(sv) > 1):

        print("No")

        break

else:

    print('Yes')

s = eval(input())

t = eval(input())

sl = []

tl = []

for c in "abcdefghijklmnopqrstuvwxyz":

    sl.append(s.count(c))

    tl.append(t.count(c))

if sorted(sl)==sorted(tl):

    print("Yes")

else:

    print("No")

s = eval(input())
t = eval(input())
from collections import defaultdict

td = defaultdict(set)
sd = defaultdict(set)
for i in range(len(s)):
    td[t[i]].add(s[i])
    sd[s[i]].add(t[i])
for tv, sv in zip(list(td.values()), list(sd.values())):
    if (len(tv) > 1) or (len(sv) > 1):
        print("No")
        break
else:
    print("Yes")

s = eval(input())
t = eval(input())
sl = []
tl = []
for c in "abcdefghijklmnopqrstuvwxyz":
    sl.append(s.count(c))
    tl.append(t.count(c))
if sorted(sl) == sorted(tl):
    print("Yes")
else:
    print("No")

-for tv, sv in zip(list(td.values()), list(sd.values())):


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

a = list(int(eval(input())) for _ in range(n))

d = {}

for i in range(n):

    d[a[i]] = d.get(a[i], 0) + 1

ans = 0

for v in list(d.values()):

    if v % 2 == 1:

        ans += 1

print(ans)

def main():

    from collections import Counter

    import sys

    input = sys.stdin.readline

    N = int(eval(input()))  # <= 10**5

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

    ctr = Counter(a)

    ans = 0

    for cnt in list(ctr.values()):

        ans += cnt % 2

    print(ans)

if __name__ == '__main__':

    main()

# import sys

#

# sys.setrecursionlimit(10 ** 7)

#

# input = sys.stdin.readline

# rstrip()

# int(input())

# map(int, input().split())

def main():
    from collections import Counter
    import sys

    input = sys.stdin.readline
    N = int(eval(input()))  # <= 10**5
    a = [int(eval(input())) for _ in range(N)]
    ctr = Counter(a)
    ans = 0
    for cnt in list(ctr.values()):
        ans += cnt % 2
    print(ans)

if __name__ == "__main__":
    main()
# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

-a = list(int(eval(input())) for _ in range(n))

+    a = [int(eval(input())) for _ in range(N)]


Input:
def gcd(a, b):

    while b:

        a, b = b, a % b

    return a

def gcd_n(numbers):

    ret = numbers[0]

    for i in range(len(numbers)):

        ret = gcd(ret, numbers[i])

    return ret

def main():

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

    x = [abs(X - int(i)) for i in input().split()]

    print((gcd_n(x)))

    return

main()

def gcd(a, b):

    while b:

        a, b = b, a % b

    return a

def main():

    N, X, *xn = list(map(int, open(0).read().split()))

    ans = None

    for x in xn:

        if ans is not None:

            ans = gcd(ans, abs(X - x))

        else:

            ans = abs(X - xn[0])

    print(ans)

    return

main()

-    N, X = list(map(int, input().split()))

-    x = [abs(X - int(i)) for i in input().split()]

+    N, X, *xn = list(map(int, open(0).read().split()))


Input:
from itertools import accumulate

N, K, *A = list(map(int, open(0).read().split()))

def check(d):

    r = sorted([v % d for v in A])

    r_inv = [d - v for v in r]

    r_cs = [0] + list(accumulate(r))

    r_inv_cs = list(accumulate(r_inv[::-1]))[::-1] + [0]

    ret = False

    for i in range(N + 1):

        flg1 = r_cs[i] <= K

        flg2 = r_cs[i] == r_inv_cs[i]

        ret = (flg1 and flg2) or ret

    return ret

M = sum(A)

ans = 0

for i in reversed(list(range(1, int(M ** 0.5) + 1))):

    if M % i == 0 and check(i):

        ans = max(ans, i)

    if M % (M // i) == 0 and check(M // i):

        ans = max(ans, M // i)

print(ans)

from itertools import accumulate

N, K, *A = list(map(int, open(0).read().split()))

s = sum(A)

divisor = []

for i in range(1, int(s ** 0.5) + 1):

    if s % i == 0:

        divisor.append(i)

        if i != s // i:

            divisor.append(s // i)

ans = 0

for d in divisor:

    x = sorted(v % d for v in A)

    y = [d - r for r in x]

    x_s = [0] + list(accumulate(x))

    y_s = list(accumulate(y[::-1]))[::-1] + [0]

    for i in range(N + 1):

        if x_s[i] <= K and x_s[i] == y_s[i]:

            ans = max(ans, d)

            break

print(ans)

from itertools import accumulate

N, K, *A = list(map(int, open(0).read().split()))

def check(d):
    r = sorted([v % d for v in A])
    r_inv = [d - v for v in r]
    r_cs = [0] + list(accumulate(r))
    r_inv_cs = list(accumulate(r_inv[::-1]))[::-1] + [0]
    ret = False
    for i in range(N + 1):
        flg1 = r_cs[i] <= K
        flg2 = r_cs[i] == r_inv_cs[i]
        ret = (flg1 and flg2) or ret
    return ret

M = sum(A)
ans = 0
for i in reversed(list(range(1, int(M**0.5) + 1))):
    if M % i == 0 and check(i):
        ans = max(ans, i)
    if M % (M // i) == 0 and check(M // i):
        ans = max(ans, M // i)
print(ans)

from itertools import accumulate

N, K, *A = list(map(int, open(0).read().split()))
s = sum(A)
divisor = []
for i in range(1, int(s**0.5) + 1):
    if s % i == 0:
        divisor.append(i)
        if i != s // i:
            divisor.append(s // i)
ans = 0
for d in divisor:
    x = sorted(v % d for v in A)
    y = [d - r for r in x]
    x_s = [0] + list(accumulate(x))
    y_s = list(accumulate(y[::-1]))[::-1] + [0]
    for i in range(N + 1):
        if x_s[i] <= K and x_s[i] == y_s[i]:
            ans = max(ans, d)
            break
print(ans)

-for i in reversed(list(range(1, int(M**0.5) + 1))):

-    if M % (M // i) == 0 and check(M // i):

+        if x_s[i] <= K and x_s[i] == y_s[i]:


Input:
s=eval(input())

k=int(eval(input()))

t="abcdefghijklmnopqrstuvwxyz"

a=[t.index(i)for i in s]

i=0

while k:

  if i==len(s)-1:a[i]+=k;a[i]%=26;k=0

  if a[i]==0 or 26-a[i]>k:i+=1

  else:a[i]+=1;a[i]%=26;k-=1

print(("".join([t[i]for i in a])))

t="abcdefghijklmnopqrstuvwxyz"

s=eval(input())

k=int(eval(input()))

a=[t.index(i)for i in s]

ans=""

for i in a:

  j=i

  if i==0:

    ans+="a"

    continue

  if k>=26-i:

    k-=26-i

    j=0

  ans+=t[j]

j+=k

j%=26

ans=ans[:-1]+t[j]

print(ans)

s = eval(input())
k = int(eval(input()))
t = "abcdefghijklmnopqrstuvwxyz"
a = [t.index(i) for i in s]
i = 0
while k:
    if i == len(s) - 1:
        a[i] += k
        a[i] %= 26
        k = 0
    if a[i] == 0 or 26 - a[i] > k:
        i += 1
    else:
        a[i] += 1
        a[i] %= 26
        k -= 1
print(("".join([t[i] for i in a])))

t = "abcdefghijklmnopqrstuvwxyz"
s = eval(input())
k = int(eval(input()))
a = [t.index(i) for i in s]
ans = ""
for i in a:
    j = i
    if i == 0:
        ans += "a"
        continue
    if k >= 26 - i:
        k -= 26 - i
        j = 0
    ans += t[j]
j += k
j %= 26
ans = ans[:-1] + t[j]
print(ans)

+t = "abcdefghijklmnopqrstuvwxyz"
-t = "abcdefghijklmnopqrstuvwxyz"
-i = 0
-while k:
-    if i == len(s) - 1:
-        a[i] += k
-        a[i] %= 26
-        k = 0
-    if a[i] == 0 or 26 - a[i] > k:
-        i += 1
-    else:
-        a[i] += 1
-        a[i] %= 26
-        k -= 1
-print(("".join([t[i] for i in a])))
+ans = ""
+for i in a:
+    j = i
+    if i == 0:
+        ans += "a"
+        continue
+    if k >= 26 - i:
+        k -= 26 - i
+        j = 0
+    ans += t[j]
+j += k
+j %= 26
+ans = ans[:-1] + t[j]
+print(ans)


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

if N == 2:

    print((1))

    exit()

if N == 3:

    print((2))

    exit()

ans = []

for n in range(1, int(N ** 0.5) + 1):

    if (N - 1) % n == 0:

        ans.append(n)

        ans.append((N - 1) // n)

for k in range(2, int(N ** 0.5) + 1):

    x = N

    while x % k == 0:

        x //= k

    if 1 in (x % k - k, x % k):

        ans.append(k)

print((len(set(ans))))

N = int(eval(input()))

ans = []

for n in range(1, int(N ** 0.5) + 1):

    if (N - 1) % n == 0:

        ans.append(n)

        ans.append((N - 1) // n)

for k in range(2, int(N ** 0.5) + 1):

    if N % k != 0:

        continue

    x = N

    while x % k == 0:

        x //= k

    if x % k == 1:

        ans.append(k)

print((len(set(ans))))

N = int(eval(input()))
if N == 2:
    print((1))
    exit()
if N == 3:
    print((2))
    exit()
ans = []
for n in range(1, int(N**0.5) + 1):
    if (N - 1) % n == 0:
        ans.append(n)
        ans.append((N - 1) // n)
for k in range(2, int(N**0.5) + 1):
    x = N
    while x % k == 0:
        x //= k
    if 1 in (x % k - k, x % k):
        ans.append(k)
print((len(set(ans))))

N = int(eval(input()))
ans = []
for n in range(1, int(N**0.5) + 1):
    if (N - 1) % n == 0:
        ans.append(n)
        ans.append((N - 1) // n)
for k in range(2, int(N**0.5) + 1):
    if N % k != 0:
        continue
    x = N
    while x % k == 0:
        x //= k
    if x % k == 1:
        ans.append(k)
print((len(set(ans))))


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

l=[[0,0]]+[[]for _ in range(h*w)]

for i in range(h):

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

        l[a]=[i,j]

for i in range(h*w+1):

    if i<d:

        l[i].append(0)

    else:

        a,b=l[i]

        x,y,v=l[i-d]

        l[i].append(abs(x-a)+abs(y-b)+v)

for _ in range(int(eval(input()))):

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

    print((l[b][2]-l[a][2]))

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

l=[[0,0]]+[[]for _ in range(h*w)]

for i in range(h):

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

        l[a]=[i,j]

for i in range(h*w+1):

    if i<d:

        l[i].append(0)

    else:

        a,b=l[i]

        x,y,v=l[i-d]

        l[i].append(abs(x-a)+abs(y-b)+v)

l=[a[2]for a in l]

for _ in range(int(eval(input()))):

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

    print((l[b]-l[a]))

h, w, d = list(map(int, input().split()))
l = [[0, 0]] + [[] for _ in range(h * w)]
for i in range(h):
    for j, a in enumerate(map(int, input().split())):
        l[a] = [i, j]
for i in range(h * w + 1):
    if i < d:
        l[i].append(0)
    else:
        a, b = l[i]
        x, y, v = l[i - d]
        l[i].append(abs(x - a) + abs(y - b) + v)
for _ in range(int(eval(input()))):
    a, b = list(map(int, input().split()))
    print((l[b][2] - l[a][2]))

h, w, d = list(map(int, input().split()))
l = [[0, 0]] + [[] for _ in range(h * w)]
for i in range(h):
    for j, a in enumerate(map(int, input().split())):
        l[a] = [i, j]
for i in range(h * w + 1):
    if i < d:
        l[i].append(0)
    else:
        a, b = l[i]
        x, y, v = l[i - d]
        l[i].append(abs(x - a) + abs(y - b) + v)
l = [a[2] for a in l]
for _ in range(int(eval(input()))):
    a, b = list(map(int, input().split()))
    print((l[b] - l[a]))


Input:
S = eval(input())

MOD = 10**9 + 7

p = [ (10**k)%13 for k in range(6) ]

mod_tb = [ [i*p[k]%13 for i in range(10)] for k in range(6) ]

n = [0]*6

base = 0

for k in range(len(S)):

    s = S[-(k+1)]

    if s == '?':

        n[k%6] += 1

    else:

        base += p[k%6]*int(s)

        base %= 13

DP = [ [0] * 13 for _ in range(sum(n)+1) ]

DP[0][base] = 1

i = 1

for k in range(6):

    for _ in range(n[k]):

        for m in range(13):

            for d in range(10):

                DP[i][(m + mod_tb[k][d])%13] += DP[i-1][m]

        for m in range(13):

            DP[i][m] %= MOD

        i += 1

print((DP[-1][5]))

MOD = 10**9 + 7

S = eval(input())

cur = [0] * 13

cur[0] = 1

r = 1

for i in range(len(S)):

    s = S[-(i+1)]

    if s == '?':

        digits = list(range(10))

    else:

        digits = [int(s)]

    prev = cur

    cur = [0] * 13

    for m in range(13):

        for d in digits:

            cur[(m + r*d) % 13] += prev[m]

    for m in range(13):

        cur[m] %= MOD

    r = (10 * r) % 13

print((cur[5]))

S = eval(input())
MOD = 10**9 + 7
p = [(10**k) % 13 for k in range(6)]
mod_tb = [[i * p[k] % 13 for i in range(10)] for k in range(6)]
n = [0] * 6
base = 0
for k in range(len(S)):
    s = S[-(k + 1)]
    if s == "?":
        n[k % 6] += 1
    else:
        base += p[k % 6] * int(s)
        base %= 13
DP = [[0] * 13 for _ in range(sum(n) + 1)]
DP[0][base] = 1
i = 1
for k in range(6):
    for _ in range(n[k]):
        for m in range(13):
            for d in range(10):
                DP[i][(m + mod_tb[k][d]) % 13] += DP[i - 1][m]
        for m in range(13):
            DP[i][m] %= MOD
        i += 1
print((DP[-1][5]))

MOD = 10**9 + 7
S = eval(input())
cur = [0] * 13
cur[0] = 1
r = 1
for i in range(len(S)):
    s = S[-(i + 1)]
    if s == "?":
        digits = list(range(10))
    else:
        digits = [int(s)]
    prev = cur
    cur = [0] * 13
    for m in range(13):
        for d in digits:
            cur[(m + r * d) % 13] += prev[m]
    for m in range(13):
        cur[m] %= MOD
    r = (10 * r) % 13
print((cur[5]))

-mod_tb = [[i * p[k] % 13 for i in range(10)] for k in range(6)]


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

mod = 10**9+7

def factorial_mod(x, y, mod):

    factorial = 1

    for i in range(x,y+1):

        factorial *= i

        factorial %= mod

    return factorial

#conbinationを何回も求める場合はfactorial_k,nkをあらかじめ計算し, リストに入れておけば良い

def n_C_k(n,k,mod):

    factorial_nk = factorial_mod(n-k+1, n, mod)

    factorial_k = factorial_mod(1,k,mod)

    conbi=factorial_nk*pow(factorial_k, mod-2, mod)

    return conbi%mod

ans = (pow(2, n, mod)-1-n_C_k(n, a, mod)-n_C_k(n, b, mod))%mod

print(ans)

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

mod = 10**9+7

"""

def factorial_mod(x, y, mod):

    factorial = 1

    for i in range(x,y+1):

        factorial *= i

        factorial %= mod

    return factorial

#conbinationを何回も求める場合はfactorial_k,nkをあらかじめ計算し, リストに入れておけば良い

def n_C_k(n,k,mod):

    factorial_nk = factorial_mod(n-k+1, n, mod)

    factorial_k = factorial_mod(1,k,mod)

    conbi=factorial_nk*pow(factorial_k, mod-2, mod)

    return conbi%mod

ans = (pow(2, n, mod)-1-n_C_k(n, a, mod)-n_C_k(n, b, mod))%mod

print(ans)

"""

#n!を求める

def factorial_mod(n, mod):

    x = 1

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

        x *= i

        x %= mod

    return x

#n_P_kを求める

def permutation_mod(n, k, mod):

    x = 1

    for i in range(n-k+1, n+1): #k回for文を回す

        x *= i

        x %= mod

    return x

#conbinationを何回も求める場合はfactorial_k,nkをあらかじめ計算し, リストに入れておけば良い

def n_C_k(n, k, mod): #O(min(k, n-k))

    k = min(k, n-k)

    n_P_k = permutation_mod(n, k, mod)

    factorial_k = factorial_mod(k, mod)

    conbi=n_P_k * pow(factorial_k, mod-2, mod)

    return conbi % mod

ans = (pow(2, n, mod)-1-n_C_k(n, a, mod)-n_C_k(n, b, mod))%mod

print(ans)

n, a, b = list(map(int, input().split()))
mod = 10**9 + 7

def factorial_mod(x, y, mod):
    factorial = 1
    for i in range(x, y + 1):
        factorial *= i
        factorial %= mod
    return factorial

# conbinationを何回も求める場合はfactorial_k,nkをあらかじめ計算し, リストに入れておけば良い
def n_C_k(n, k, mod):
    factorial_nk = factorial_mod(n - k + 1, n, mod)
    factorial_k = factorial_mod(1, k, mod)
    conbi = factorial_nk * pow(factorial_k, mod - 2, mod)
    return conbi % mod

ans = (pow(2, n, mod) - 1 - n_C_k(n, a, mod) - n_C_k(n, b, mod)) % mod
print(ans)

n, a, b = list(map(int, input().split()))
mod = 10**9 + 7
"""
def factorial_mod(x, y, mod):
    factorial = 1
    for i in range(x,y+1):
        factorial *= i
        factorial %= mod
    return factorial
#conbinationを何回も求める場合はfactorial_k,nkをあらかじめ計算し, リストに入れておけば良い
def n_C_k(n,k,mod):
    factorial_nk = factorial_mod(n-k+1, n, mod)
    factorial_k = factorial_mod(1,k,mod)
    conbi=factorial_nk*pow(factorial_k, mod-2, mod)
    return conbi%mod
ans = (pow(2, n, mod)-1-n_C_k(n, a, mod)-n_C_k(n, b, mod))%mod
print(ans)
"""
# n!を求める
def factorial_mod(n, mod):
    x = 1
    for i in range(1, n + 1):
        x *= i
        x %= mod
    return x

# n_P_kを求める
def permutation_mod(n, k, mod):
    x = 1
    for i in range(n - k + 1, n + 1):  # k回for文を回す
        x *= i
        x %= mod
    return x

# conbinationを何回も求める場合はfactorial_k,nkをあらかじめ計算し, リストに入れておけば良い
def n_C_k(n, k, mod):  # O(min(k, n-k))
    k = min(k, n - k)
    n_P_k = permutation_mod(n, k, mod)
    factorial_k = factorial_mod(k, mod)
    conbi = n_P_k * pow(factorial_k, mod - 2, mod)
    return conbi % mod

ans = (pow(2, n, mod) - 1 - n_C_k(n, a, mod) - n_C_k(n, b, mod)) % mod
print(ans)

+#conbinationを何回も求める場合はfactorial_k,nkをあらかじめ計算し, リストに入れておけば良い

+    factorial_nk = factorial_mod(n-k+1, n, mod)

+    factorial_k = factorial_mod(1,k,mod)

+    conbi=factorial_nk*pow(factorial_k, mod-2, mod)

+ans = (pow(2, n, mod)-1-n_C_k(n, a, mod)-n_C_k(n, b, mod))%mod

+    for i in range(n - k + 1, n + 1):  # k回for文を回す

-    factorial_nk = factorial_mod(n - k + 1, n, mod)

-    factorial_k = factorial_mod(1, k, mod)

-    conbi = factorial_nk * pow(factorial_k, mod - 2, mod)

+def n_C_k(n, k, mod):  # O(min(k, n-k))

+    factorial_k = factorial_mod(k, mod)

+    conbi = n_P_k * pow(factorial_k, mod - 2, mod)


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

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

for i in range(A,B+1):

    if i%K==0:

        print("OK")

        exit()

print("NG")

K=int(eval(input()))

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

for n in range(A,B+1):

    if n%K==0:

        print("OK")

        exit()

print("NG")

K = int(eval(input()))
A, B = list(map(int, input().split()))
for i in range(A, B + 1):
    if i % K == 0:
        print("OK")
        exit()
print("NG")

K = int(eval(input()))
A, B = list(map(int, input().split()))
for n in range(A, B + 1):
    if n % K == 0:
        print("OK")
        exit()
print("NG")

-for i in range(A, B + 1):
-    if i % K == 0:
+for n in range(A, B + 1):
+    if n % K == 0:


Input:
import sys

from math import factorial

N = int(eval(input()))

S = eval(input())

mod = 10**9+7

if S[0] == 'W' or S[-1] == 'W':

    print((0))

    sys.exit()

op = "L"

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

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

        op += 'R' if op[i-1] == 'L' else 'L'

    else:

        op += 'R' if op[i-1] == 'R' else 'L'

if op.count('L') != N:

    print((0))

    sys.exit()

ct_L, ans = 0, 1

for i in op:

    if i == 'L':

        ct_L += 1

    else:

        ans *= ct_L

        ct_L -= 1

    ans %= mod

print((factorial(N) * ans % mod))

import sys

def factorial(N: int, mod: int) -> int:

    res = 1

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

        res *= i

        res %= mod

    return res

N = int(eval(input()))

S = eval(input())

mod = 10**9+7

if S[0] == 'W' or S[-1] == 'W':

    print((0))

    sys.exit()

op = [0]*(2*N)

op[0] = False

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

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

        op[i] = True if op[i-1] is False else False

    else:

        op[i] = True if op[i-1] is True else False

if op.count(True) != N:

    print((0))

    sys.exit()

ans = 1

L_ct = 0

R_ct = 0

for i in op:

    if i:

        ans *= L_ct

        L_ct -= 1

    else:

        L_ct += 1

    ans %= mod

print((factorial(N, mod) * ans % mod))

import sys
from math import factorial

N = int(eval(input()))
S = eval(input())
mod = 10**9 + 7
if S[0] == "W" or S[-1] == "W":
    print((0))
    sys.exit()
op = "L"
for i in range(1, 2 * N):
    if S[i] == S[i - 1]:
        op += "R" if op[i - 1] == "L" else "L"
    else:
        op += "R" if op[i - 1] == "R" else "L"
if op.count("L") != N:
    print((0))
    sys.exit()
ct_L, ans = 0, 1
for i in op:
    if i == "L":
        ct_L += 1
    else:
        ans *= ct_L
        ct_L -= 1
    ans %= mod
print((factorial(N) * ans % mod))

import sys

def factorial(N: int, mod: int) -> int:
    res = 1
    for i in range(1, N + 1):
        res *= i
        res %= mod
    return res

N = int(eval(input()))
S = eval(input())
mod = 10**9 + 7
if S[0] == "W" or S[-1] == "W":
    print((0))
    sys.exit()
op = [0] * (2 * N)
op[0] = False
for i in range(1, 2 * N):
    if S[i] == S[i - 1]:
        op[i] = True if op[i - 1] is False else False
    else:
        op[i] = True if op[i - 1] is True else False
if op.count(True) != N:
    print((0))
    sys.exit()
ans = 1
L_ct = 0
R_ct = 0
for i in op:
    if i:
        ans *= L_ct
        L_ct -= 1
    else:
        L_ct += 1
    ans %= mod
print((factorial(N, mod) * ans % mod))

+def factorial(N: int, mod: int) -> int:

+        op[i] = True if op[i - 1] is False else False

+        op[i] = True if op[i - 1] is True else False


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

min_num = 100

ls = [0] * 201

for i in range(0, 201):

    ls[i] = i

ls_in = [0] * n

if n > 0:

    ls_in = list(map(int, input().split(' ')))

    for i in ls_in:

        ls.remove(i)

    ls.reverse()

    for i in ls:

        min_num = min(abs(x-i), min_num)

        if abs(x-i) == min_num:

            ans = i

    print(ans)

else:

    print(x)

x, n = list(map(int, input().split(' ')))

diff = x

result = 0

if n != 0:

    ls = list(map(int, input().split(' ')))

    ls_remaind = []

    for i in range(1, 102):

        if i not in ls:

            ls_remaind.append(i)

    for j in ls_remaind:

        if abs(x-j) == diff:

            result = min(result, j)

        elif abs(x-j) < diff:

            diff = abs(x-j)

            result = j

else:

    result = x

print(result)

x, n = list(map(int, input().split(" ")))
min_num = 100
ls = [0] * 201
for i in range(0, 201):
    ls[i] = i
ls_in = [0] * n
if n > 0:
    ls_in = list(map(int, input().split(" ")))
    for i in ls_in:
        ls.remove(i)
    ls.reverse()
    for i in ls:
        min_num = min(abs(x - i), min_num)
        if abs(x - i) == min_num:
            ans = i
    print(ans)
else:
    print(x)

x, n = list(map(int, input().split(" ")))
diff = x
result = 0
if n != 0:
    ls = list(map(int, input().split(" ")))
    ls_remaind = []
    for i in range(1, 102):
        if i not in ls:
            ls_remaind.append(i)
    for j in ls_remaind:
        if abs(x - j) == diff:
            result = min(result, j)
        elif abs(x - j) < diff:
            diff = abs(x - j)
            result = j
else:
    result = x
print(result)

-    ls_in = list(map(int, input().split(" ")))

-        min_num = min(abs(x - i), min_num)

+    ls = list(map(int, input().split(" ")))


Input:
def main():

    from collections import namedtuple

    from itertools import combinations

    City = namedtuple('City', 'x y')

    def dist(a: City, b: City) -> float:

        dx = a.x - b.x

        dy = a.y - b.y

        return (dx * dx + dy * dy) ** 0.5

    N = int(eval(input()))

    cities = []

    for _ in range(N):

        x, y = list(map(int, input().split()))

        c = City(x=x, y=y)

        cities.append(c)

    total = 0

    for comb in combinations(cities, r=2):

        total += dist(*comb)

    ans = total * 2 / N

    print(ans)

if __name__ == '__main__':

    main()

# 特定の2点を固定し、それ以外を並べる + 固定順を逆にする

# [oo]ooo... これを4個の要素の並び替えを見なす

# (n-1)! * 2!

# この2点間の移動が含まれる並び順は、(n-1)! * 2!通り

# 各2点間の距離の総和*(n-1)! * 2!通り / n!

# 各2点間の距離の総和*2/n

def main():

    from itertools import permutations

    from math import hypot

    N = int(eval(input()))

    cities = [tuple(map(int, input().split())) for _ in range(N)]

    def dist(c1, c2):

        return hypot(c1[0] - c2[0], c1[1] - c2[1])

    ans = 0

    for perm in permutations(cities, r=2):

        ans += dist(*perm)

    ans /= N

    print(ans)

if __name__ == '__main__':

    main()

def main():
    from collections import namedtuple
    from itertools import combinations

    City = namedtuple("City", "x y")

    def dist(a: City, b: City) -> float:
        dx = a.x - b.x
        dy = a.y - b.y
        return (dx * dx + dy * dy) ** 0.5

    N = int(eval(input()))
    cities = []
    for _ in range(N):
        x, y = list(map(int, input().split()))
        c = City(x=x, y=y)
        cities.append(c)
    total = 0
    for comb in combinations(cities, r=2):
        total += dist(*comb)
    ans = total * 2 / N
    print(ans)

if __name__ == "__main__":
    main()
# 特定の2点を固定し、それ以外を並べる + 固定順を逆にする
# [oo]ooo... これを4個の要素の並び替えを見なす
# (n-1)! * 2!
# この2点間の移動が含まれる並び順は、(n-1)! * 2!通り
# 各2点間の距離の総和*(n-1)! * 2!通り / n!
# 各2点間の距離の総和*2/n

def main():
    from itertools import permutations
    from math import hypot

    N = int(eval(input()))
    cities = [tuple(map(int, input().split())) for _ in range(N)]

    def dist(c1, c2):
        return hypot(c1[0] - c2[0], c1[1] - c2[1])

    ans = 0
    for perm in permutations(cities, r=2):
        ans += dist(*perm)
    ans /= N
    print(ans)

if __name__ == "__main__":
    main()

-    def dist(a: City, b: City) -> float:

-        return (dx * dx + dy * dy) ** 0.5

-        x, y = list(map(int, input().split()))

-    for comb in combinations(cities, r=2):

+    cities = [tuple(map(int, input().split())) for _ in range(N)]

+    for perm in permutations(cities, r=2):

-# 特定の2点を固定し、それ以外を並べる + 固定順を逆にする

-# [oo]ooo... これを4個の要素の並び替えを見なす

-# この2点間の移動が含まれる並び順は、(n-1)! * 2!通り

-# 各2点間の距離の総和*2/n


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

def gcd(x, y):

    while y:

        x, y = y, x % y

    return x

ans = 0

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

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

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

            ans += gcd(gcd(a, b), c)

print(ans)

K = int(eval(input()))

from math import gcd

ans = 0

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

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

		t = gcd(a, b)

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

			ans += gcd(t, c)

print(ans)

-def gcd(x, y):
-    while y:
-        x, y = y, x % y
-    return x
+from math import gcd
+        t = gcd(a, b)
-            ans += gcd(gcd(a, b), c)
+            ans += gcd(t, c)


Input:
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))

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

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))

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()

+            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)]

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

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


Input:
import math

N=int(eval(input()))

li=[]

l=0

m=0

for i in range(math.floor(math.sqrt(N))):

  if N%(i+1)==0:

    l=i

    m=N/(i+1)-1

    li.append(l+m)

  else:

    pass

print((int(min(li))))

N=int(eval(input()))

answer=0

for i in range(int(pow(N,1/2)),0,-1):

    if N%i==0:

        answer=(i-1)+(N//i-1)

        break

print(answer)

import math

N = int(eval(input()))
li = []
l = 0
m = 0
for i in range(math.floor(math.sqrt(N))):
    if N % (i + 1) == 0:
        l = i
        m = N / (i + 1) - 1
        li.append(l + m)
    else:
        pass
print((int(min(li))))

N = int(eval(input()))
answer = 0
for i in range(int(pow(N, 1 / 2)), 0, -1):
    if N % i == 0:
        answer = (i - 1) + (N // i - 1)
        break
print(answer)

-for i in range(math.floor(math.sqrt(N))):

+for i in range(int(pow(N, 1 / 2)), 0, -1):


Input:
from functools import lru_cache

def popcnt(x):

    return bin(x).count("1")

@lru_cache(maxsize=None)

def rec(n):

    if n == 0:

        return 0

    else:

        return rec(n % popcnt(n)) + 1

ret = [rec(x) for x in range(2 * 10**5 + 1)]

n = int(input())

arr = input()

ALL_ARR = int(arr, 2)

cnt = popcnt(int(arr, 2))

init_big = ALL_ARR % (cnt + 1)

if cnt == 1:

    init_small = 0

else:

    init_small = ALL_ARR % (cnt - 1)

li = [0] * n

for i in range(n):

    if arr[i] == "0":

        li[i] = (init_big + pow(2, n - i - 1, cnt + 1)) % (cnt + 1)

    elif ALL_ARR - (1 << (n - i - 1)) == 0 or cnt - 1 == 0:

        li[i] = "flg"

    else:

        li[i] = (init_small - pow(2, n - i - 1, cnt - 1)) % (cnt - 1)

ans = []

for x in li:

    if x == "flg":

        ans.append(0)

    else:

        ans.append(ret[x] + 1)

print(*ans, sep="\n")

from functools import lru_cache

def popcnt(x):

    return bin(x).count("1")

@lru_cache(maxsize=None)

def rec(n):

    if n == 0:

        return 0

    else:

        return rec(n % popcnt(n)) + 1

n = int(input())

arr = input()

ALL_ARR = int(arr, 2)

cnt = popcnt(int(arr, 2))

init_big = ALL_ARR % (cnt + 1)

if cnt == 1:

    init_small = 0

else:

    init_small = ALL_ARR % (cnt - 1)

li = [0] * n

for i in range(n):

    if arr[i] == "0":

        li[i] = (init_big + pow(2, n - i - 1, cnt + 1)) % (cnt + 1)

    elif ALL_ARR - (1 << (n - i - 1)) == 0 or cnt - 1 == 0:

        li[i] = "flg"

    else:

        li[i] = (init_small - pow(2, n - i - 1, cnt - 1)) % (cnt - 1)

ans = []

for x in li:

    if x == "flg":

        ans.append(0)

    else:

        ans.append(rec(x) + 1)

print(*ans, sep="\n")

-ret = [rec(x) for x in range(2 * 10**5 + 1)]


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

S=[list(eval(input())) for i in range(H)]

table=[[0]*(H+W-1) for i in range(H+W-1)]

for j in range(H):

    for i in range(W):

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

            table[i+j][i-j+H-1]=1

yoko=[[0]*(H+W) for i in range(H+W-1)]

for j in range(H+W-1):

    for i in range(1,H+W):

            yoko[j][i]=yoko[j][i-1]+table[j][i-1]

tate=[[0]*(H+W-1) for i in range(H+W)]

for j in range(1,H+W):

    for i in range(H+W-1):

        tate[j][i]=tate[j-1][i]+table[j-1][i]

ans=0

for y in range(H+W-1):

    for x in range((y+H-1)%2,H+W-1,2):

        if table[y][x]!=1:

            continue

        for z in range(x+2,H+W-1,2):

            if table[y][z]==1:

                d=z-x

                if y+d<H+W-1:

                    ans+=yoko[y+d][z+1]-yoko[y+d][x]

                    #print(1,'.',ans,':',x,y,z)

                if y-d>=0:

                    ans+=yoko[y-d][z+1]-yoko[y-d][x]

                    #print(2,'.',ans,':',x,y,z)

        for w in range(y+2,H+W-1,2):

            if table[w][x]==1:

                e=w-y

                if x+e<H+W-1:

                    ans+=tate[w][x+e]-tate[y+1][x+e]

                    #print(3,'.',ans,':',x,y,w)

                if x-e>=0:

                    ans+=tate[w][x-e]-tate[y+1][x-e]

                    #print(4,'.',ans,':',x,y,w)

print(ans)

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

S=[eval(input()) for i in range(H)]

table=[[0]*(H+W-1) for i in range(H+W-1)]

for j in range(H):

    for i in range(W):

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

            table[i+j][i-j+H-1]=1

yoko=[[0]*(H+W) for i in range(H+W-1)]

for j in range(H+W-1):

    for i in range(1,H+W):

            yoko[j][i]=yoko[j][i-1]+table[j][i-1]

tate=[[0]*(H+W-1) for i in range(H+W)]

for j in range(1,H+W):

    for i in range(H+W-1):

        tate[j][i]=tate[j-1][i]+table[j-1][i]

ans=0

for y in range(H+W-1):

    for x in range((y+H-1)%2,H+W-1,2):

        if table[y][x]!=1:

            continue

        for z in range(x+2,H+W-1,2):

            if table[y][z]==1:

                d=z-x

                if y+d<H+W-1:

                    ans+=yoko[y+d][z+1]-yoko[y+d][x]

                    #print(1,'.',ans,':',x,y,z)

                if y-d>=0:

                    ans+=yoko[y-d][z+1]-yoko[y-d][x]

                    #print(2,'.',ans,':',x,y,z)

        for w in range(y+2,H+W-1,2):

            if table[w][x]==1:

                e=w-y

                if x+e<H+W-1:

                    ans+=tate[w][x+e]-tate[y+1][x+e]

                    #print(3,'.',ans,':',x,y,w)

                if x-e>=0:

                    ans+=tate[w][x-e]-tate[y+1][x-e]

                    #print(4,'.',ans,':',x,y,w)

print(ans)

H, W = list(map(int, input().split()))
S = [list(eval(input())) for i in range(H)]
table = [[0] * (H + W - 1) for i in range(H + W - 1)]
for j in range(H):
    for i in range(W):
        if S[j][i] == "#":
            table[i + j][i - j + H - 1] = 1
yoko = [[0] * (H + W) for i in range(H + W - 1)]
for j in range(H + W - 1):
    for i in range(1, H + W):
        yoko[j][i] = yoko[j][i - 1] + table[j][i - 1]
tate = [[0] * (H + W - 1) for i in range(H + W)]
for j in range(1, H + W):
    for i in range(H + W - 1):
        tate[j][i] = tate[j - 1][i] + table[j - 1][i]
ans = 0
for y in range(H + W - 1):
    for x in range((y + H - 1) % 2, H + W - 1, 2):
        if table[y][x] != 1:
            continue
        for z in range(x + 2, H + W - 1, 2):
            if table[y][z] == 1:
                d = z - x
                if y + d < H + W - 1:
                    ans += yoko[y + d][z + 1] - yoko[y + d][x]
                    # print(1,'.',ans,':',x,y,z)
                if y - d >= 0:
                    ans += yoko[y - d][z + 1] - yoko[y - d][x]
                    # print(2,'.',ans,':',x,y,z)
        for w in range(y + 2, H + W - 1, 2):
            if table[w][x] == 1:
                e = w - y
                if x + e < H + W - 1:
                    ans += tate[w][x + e] - tate[y + 1][x + e]
                    # print(3,'.',ans,':',x,y,w)
                if x - e >= 0:
                    ans += tate[w][x - e] - tate[y + 1][x - e]
                    # print(4,'.',ans,':',x,y,w)
print(ans)

H, W = list(map(int, input().split()))
S = [eval(input()) for i in range(H)]
table = [[0] * (H + W - 1) for i in range(H + W - 1)]
for j in range(H):
    for i in range(W):
        if S[j][i] == "#":
            table[i + j][i - j + H - 1] = 1
yoko = [[0] * (H + W) for i in range(H + W - 1)]
for j in range(H + W - 1):
    for i in range(1, H + W):
        yoko[j][i] = yoko[j][i - 1] + table[j][i - 1]
tate = [[0] * (H + W - 1) for i in range(H + W)]
for j in range(1, H + W):
    for i in range(H + W - 1):
        tate[j][i] = tate[j - 1][i] + table[j - 1][i]
ans = 0
for y in range(H + W - 1):
    for x in range((y + H - 1) % 2, H + W - 1, 2):
        if table[y][x] != 1:
            continue
        for z in range(x + 2, H + W - 1, 2):
            if table[y][z] == 1:
                d = z - x
                if y + d < H + W - 1:
                    ans += yoko[y + d][z + 1] - yoko[y + d][x]
                    # print(1,'.',ans,':',x,y,z)
                if y - d >= 0:
                    ans += yoko[y - d][z + 1] - yoko[y - d][x]
                    # print(2,'.',ans,':',x,y,z)
        for w in range(y + 2, H + W - 1, 2):
            if table[w][x] == 1:
                e = w - y
                if x + e < H + W - 1:
                    ans += tate[w][x + e] - tate[y + 1][x + e]
                    # print(3,'.',ans,':',x,y,w)
                if x - e >= 0:
                    ans += tate[w][x - e] - tate[y + 1][x - e]
                    # print(4,'.',ans,':',x,y,w)
print(ans)

-S = [list(eval(input())) for i in range(H)]


Input:
x,y,a,b,c=list(map(int,input().split()))

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

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

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

l=sorted(p,reverse=True)[:x]+sorted(q,reverse=True)[:y]+r

l.sort(reverse=True)

print((sum(l[:x+y])))

x, y, a, b, c = list(map(int,input().split()))

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

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

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

p.sort(reverse=True)

q.sort(reverse=True)

cand = p[:x] + q[:y] + r

cand.sort(reverse=True)

print((sum(cand[:x + y])))

x, y, a, b, c = list(map(int, input().split()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
l = sorted(p, reverse=True)[:x] + sorted(q, reverse=True)[:y] + r
l.sort(reverse=True)
print((sum(l[: x + y])))

x, y, a, b, c = list(map(int, input().split()))
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
cand = p[:x] + q[:y] + r
cand.sort(reverse=True)
print((sum(cand[: x + y])))

-l = sorted(p, reverse=True)[:x] + sorted(q, reverse=True)[:y] + r


Input:
def main():

    from sys import stdin

    readline = stdin.readline

    H, N = list(map(int, readline().split()))

    AB = [list(map(int, readline().split())) for _ in range(N)]

    dp = [100000000] * (H + 10 ** 4 + 1)

    dp[0] = 0

    for i in range(H):

        if dp[i] == 100000000:

            continue

        for a, b in AB:

            t = dp[i] + b

            if t < dp[i + a]:

                dp[i + a] = t

    print((min(dp[H:])))

main()

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

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

A_max = 10 ** 4

INF = float('inf')

dp = [INF] * (H + A_max + 1)

dp[0] = 0

for i in range(H):

    if dp[i] == INF:

        continue

    for a, b in AB:

        if dp[i] + b < dp[i + a]:

            dp[i + a] = dp[i] + b

print((min(dp[H:])))

def main():
    from sys import stdin

    readline = stdin.readline
    H, N = list(map(int, readline().split()))
    AB = [list(map(int, readline().split())) for _ in range(N)]
    dp = [100000000] * (H + 10**4 + 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for a, b in AB:
            t = dp[i] + b
            if t < dp[i + a]:
                dp[i + a] = t
    print((min(dp[H:])))

main()

H, N = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]
A_max = 10**4
INF = float("inf")
dp = [INF] * (H + A_max + 1)
dp[0] = 0
for i in range(H):
    if dp[i] == INF:
        continue
    for a, b in AB:
        if dp[i] + b < dp[i + a]:
            dp[i + a] = dp[i] + b
print((min(dp[H:])))

-    H, N = list(map(int, readline().split()))

-    AB = [list(map(int, readline().split())) for _ in range(N)]

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


Input:
def ex(x, y):

    if x > y:

        tmp = x

        x = y

        y = tmp

    return x, y

def ex2num():

    r1, r2 = input().split(" ")

    r1 = int(r1)

    r2 = int(r2)

    while (0 != r1) or (0 != r2):

        r1, r2 = ex(r1, r2)

        print((r1, r2))

        r1, r2 = input().split(" ")

        r1 = int(r1)

        r2 = int(r2)

    return 0

if __name__ == "__main__":

    ret = ex2num()

def ex2num():

    while (True):

        r1, r2 = input().split(" ")

        r1 = int(r1)

        r2 = int(r2)

        if (0 == r1) and (0 == r2):

            break

        if r1 < r2:

            print((r1, r2))

        else:

            print((r2, r1))

    return 0

if __name__ == "__main__":

    ret = ex2num()

def ex2num():
    while True:
        r1, r2 = input().split(" ")
        r1 = int(r1)
        r2 = int(r2)
        if (0 == r1) and (0 == r2):
            break
        if r1 < r2:
            print((r1, r2))
        else:
            print((r2, r1))
    return 0

if __name__ == "__main__":
    ret = ex2num()

-def ex(x, y):
-    if x > y:
-        tmp = x
-        x = y
-        y = tmp
-    return x, y
-    r1, r2 = input().split(" ")
-    r1 = int(r1)
-    r2 = int(r2)
-    while (0 != r1) or (0 != r2):
-        r1, r2 = ex(r1, r2)
-        print((r1, r2))
+    while True:
+        if (0 == r1) and (0 == r2):
+            break
+        if r1 < r2:
+            print((r1, r2))
+        else:
+            print((r2, r1))