Input:
import sys

import numba as nb

import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8,i8,i8[:],i8[:])")

def solve(N, W, w, v):

    dp = np.full(shape=(N + 1, np.sum(v) + 1), fill_value=(N * W), dtype=np.int64)

    dp[:, 0] = 0

    for i in range(N):

        # TODO: use out keyward

        dp[i + 1][:v[i]] = np.minimum(w[i], dp[i][:v[i]])

        dp[i + 1][v[i]:] = np.minimum(w[i] + dp[i][: - v[i]], dp[i][v[i]:])

    return np.where(dp[-1] <= W)[0][-1]

def main():

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

    w = np.empty(shape=N, dtype=np.int64)

    v = np.empty(shape=N, dtype=np.int64)

    for i in range(N):

        w[i], v[i] = list(map(int, input().split()))

    ans = solve(N, W, w, v)

    print(ans)

if __name__ == "__main__":

    main()

import sys

import numba as nb

import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8,i8,i8[:],i8[:])")

def solve(N, W, w, v):

    dp = np.full(shape=(N + 1, np.sum(v) + 1), fill_value=(N * W), dtype=np.int64)

    dp[:, 0] = 0

    for i in range(N):

        np.minimum(w[i], dp[i][:v[i]], dp[i + 1][:v[i]])

        np.minimum(w[i] + dp[i][: - v[i]], dp[i][v[i]:], dp[i + 1][v[i]:])

    return np.where(dp[-1] <= W)[0][-1]

def main():

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

    w = np.empty(shape=N, dtype=np.int64)

    v = np.empty(shape=N, dtype=np.int64)

    for i in range(N):

        w[i], v[i] = list(map(int, input().split()))

    ans = solve(N, W, w, v)

    print(ans)

if __name__ == "__main__":

    main()

import sys
import numba as nb
import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8,i8,i8[:],i8[:])")
def solve(N, W, w, v):
    dp = np.full(shape=(N + 1, np.sum(v) + 1), fill_value=(N * W), dtype=np.int64)
    dp[:, 0] = 0
    for i in range(N):
        # TODO: use out keyward
        dp[i + 1][: v[i]] = np.minimum(w[i], dp[i][: v[i]])
        dp[i + 1][v[i] :] = np.minimum(w[i] + dp[i][: -v[i]], dp[i][v[i] :])
    return np.where(dp[-1] <= W)[0][-1]

def main():
    N, W = list(map(int, input().split()))
    w = np.empty(shape=N, dtype=np.int64)
    v = np.empty(shape=N, dtype=np.int64)
    for i in range(N):
        w[i], v[i] = list(map(int, input().split()))
    ans = solve(N, W, w, v)
    print(ans)

if __name__ == "__main__":
    main()

import sys
import numba as nb
import numpy as np

input = sys.stdin.readline

@nb.njit("i8(i8,i8,i8[:],i8[:])")
def solve(N, W, w, v):
    dp = np.full(shape=(N + 1, np.sum(v) + 1), fill_value=(N * W), dtype=np.int64)
    dp[:, 0] = 0
    for i in range(N):
        np.minimum(w[i], dp[i][: v[i]], dp[i + 1][: v[i]])
        np.minimum(w[i] + dp[i][: -v[i]], dp[i][v[i] :], dp[i + 1][v[i] :])
    return np.where(dp[-1] <= W)[0][-1]

def main():
    N, W = list(map(int, input().split()))
    w = np.empty(shape=N, dtype=np.int64)
    v = np.empty(shape=N, dtype=np.int64)
    for i in range(N):
        w[i], v[i] = list(map(int, input().split()))
    ans = solve(N, W, w, v)
    print(ans)

if __name__ == "__main__":
    main()


Input:
from fractions import gcd

import sys

input = sys.stdin.readline

N = int(eval(input()))

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

l_gcd_list = [0]*(N+1)

r_gcd_list = [0]*(N+1)

for i in range(N):

    l_gcd_list[i+1] = (gcd(l_gcd_list[i], A[i]))

    r_gcd_list[-(i+2)] = (gcd(r_gcd_list[-(i+1)], A[-(i+1)]))

# print(l_gcd_list)

# print(r_gcd_list)

results = []

for i in range(N):

    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print((max(results)))

import sys

input = sys.stdin.readline

from fractions import gcd

N = int(eval(input()))

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

l_gcd_list = [0]

r_gcd_list = [0]

for i in range(N):

    l_gcd_list.append(gcd(l_gcd_list[i], A[i]))

    r_gcd_list.append(gcd(r_gcd_list[i], A[-(i+1)]))

r_gcd_list = r_gcd_list[::-1]

# print(l_gcd_list)

# print(r_gcd_list)

results = []

for i in range(N):

    results.append(gcd(l_gcd_list[i], r_gcd_list[i+1]))

print((max(results)))

from fractions import gcd
import sys

input = sys.stdin.readline
N = int(eval(input()))
A = list(map(int, input().split()))
l_gcd_list = [0] * (N + 1)
r_gcd_list = [0] * (N + 1)
for i in range(N):
    l_gcd_list[i + 1] = gcd(l_gcd_list[i], A[i])
    r_gcd_list[-(i + 2)] = gcd(r_gcd_list[-(i + 1)], A[-(i + 1)])
# print(l_gcd_list)
# print(r_gcd_list)
results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i + 1]))
print((max(results)))

import sys

input = sys.stdin.readline
from fractions import gcd

N = int(eval(input()))
A = list(map(int, input().split()))
l_gcd_list = [0]
r_gcd_list = [0]
for i in range(N):
    l_gcd_list.append(gcd(l_gcd_list[i], A[i]))
    r_gcd_list.append(gcd(r_gcd_list[i], A[-(i + 1)]))
r_gcd_list = r_gcd_list[::-1]
# print(l_gcd_list)
# print(r_gcd_list)
results = []
for i in range(N):
    results.append(gcd(l_gcd_list[i], r_gcd_list[i + 1]))
print((max(results)))

-    l_gcd_list[i + 1] = gcd(l_gcd_list[i], A[i])

+    l_gcd_list.append(gcd(l_gcd_list[i], A[i]))

+    r_gcd_list.append(gcd(r_gcd_list[i], A[-(i + 1)]))


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

d=[list(eval(input())) for i in range(n)]

count=0

ans=0

for i in range(n):

  d[i].sort()

d.sort()

for i in range(n-1):

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

    count+=1

  elif d[i]!=d[i+1]:

    x=count+1

    count=0

    ans+=(x*(x-1))/2

  if i==(n-2):

    x=count+1

    count=0

    ans+=(x*(x-1))/2

print((int(ans)))

n=int(eval(input()))

d=[]

for i in range(n):

  s=list(eval(input()))

  s.sort()

  s="".join(s)

  d.append(s)

d.sort()

x=[]

k=1

for i in range(n-1):

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

    k+=1

  else:

    x.append(k)

    k=1

x.append(k)

ans=[]

for i in range(len(x)):

  if x[i]>1:

    ans.append((x[i]*(x[i]-1))/2)

  else:

    ans.append(0)

print((int(sum(ans))))

n = int(eval(input()))
d = [list(eval(input())) for i in range(n)]
count = 0
ans = 0
for i in range(n):
    d[i].sort()
d.sort()
for i in range(n - 1):
    if d[i] == d[i + 1]:
        count += 1
    elif d[i] != d[i + 1]:
        x = count + 1
        count = 0
        ans += (x * (x - 1)) / 2
    if i == (n - 2):
        x = count + 1
        count = 0
        ans += (x * (x - 1)) / 2
print((int(ans)))

n = int(eval(input()))
d = []
for i in range(n):
    s = list(eval(input()))
    s.sort()
    s = "".join(s)
    d.append(s)
d.sort()
x = []
k = 1
for i in range(n - 1):
    if d[i] == d[i + 1]:
        k += 1
    else:
        x.append(k)
        k = 1
x.append(k)
ans = []
for i in range(len(x)):
    if x[i] > 1:
        ans.append((x[i] * (x[i] - 1)) / 2)
    else:
        ans.append(0)
print((int(sum(ans))))

-d = [list(eval(input())) for i in range(n)]


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

if x+r > W or y+r > H:

    print("No")

elif  x-r < 0 or y-r < 0:

    print("No")

else:

    print("Yes")

list=input().split()

w=int(list[0])

h=int(list[1])

x=int(list[2])

y=int(list[3])

r=int(list[4])

if w>=x+r and x-r>=0:

  if h>=y+r and y-r>=0:

    print("Yes")

  else:

    print("No")

else:

  print("No")

W, H, x, y, r = list(map(int, input().split()))
if x + r > W or y + r > H:
    print("No")
elif x - r < 0 or y - r < 0:
    print("No")
else:
    print("Yes")

list = input().split()
w = int(list[0])
h = int(list[1])
x = int(list[2])
y = int(list[3])
r = int(list[4])
if w >= x + r and x - r >= 0:
    if h >= y + r and y - r >= 0:
        print("Yes")
    else:
        print("No")
else:
    print("No")

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


Input:
N, X = [int(x) for x in input().split()]

list01=[]

for i in range(N):

  j = int(eval(input()))

  list01.append(j)

j = 0

mass = sum(list01)

while mass <= X:

  j += 1

  mass = sum(list01) + min(list01) * j

print((N + j -1))

N, X = [int(x) for x in input().split()]

list01 = []

for i in range(N):

  i = int(eval(input()))

  list01.append(i)

min_01 = 0

for i in range(len(list01)):

  if min_01 == 0:

    min_01 = list01[i]

  elif list01[i] < min_01:

    min_01 = list01[i]

for i in range(len(list01)):

  X -= list01[i]

print((X // min_01 + N))

N, X = [int(x) for x in input().split()]
list01 = []
for i in range(N):
    j = int(eval(input()))
    list01.append(j)
j = 0
mass = sum(list01)
while mass <= X:
    j += 1
    mass = sum(list01) + min(list01) * j
print((N + j - 1))

-    mass = sum(list01) + min(list01) * j


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

a=[]

while True:

  try:

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

  except:

    break;

t=[0]*n

for i in a:

  t[i-1]+=1

for i in t:

  if i >= q - k + 1:

    print("Yes")

  else:

    print("No")

#C

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

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

people = [0] * n

for i in a:

    people[i-1] += 1

# print(people)

for i in people:

    if k <= q - i:

        print("No")

    else:

        print("Yes")

n, k, q = list(map(int, input().split()))
a = []
while True:
    try:
        a.append(int(eval(input())))
    except:
        break
t = [0] * n
for i in a:
    t[i - 1] += 1
for i in t:
    if i >= q - k + 1:
        print("Yes")
    else:
        print("No")

# C
n, k, q = list(map(int, input().split()))
a = [int(eval(input())) for _ in range(q)]
people = [0] * n
for i in a:
    people[i - 1] += 1
# print(people)
for i in people:
    if k <= q - i:
        print("No")
    else:
        print("Yes")

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


Input:
import sys

sys.setrecursionlimit(10 ** 5)

def dfs(v):

    if v > 3234566667:

        return

    ans.append(v)

    d = v % 10

    if d - 1 >= 0:

        dfs(v * 10 + d - 1)

    dfs(v * 10 + d)

    if d + 1 < 10:

        dfs(v * 10 + d + 1)

K = int(eval(input()))

ans = []

[dfs(i) for i in range(1, 10)]

print((sorted(ans)[K - 1]))

def is_good(mid, key):

    S = list(map(int, str(mid)))

    N = len(S)

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

    dp[1][1][10] = 1

    for k in range(1, S[0]):

        dp[1][1][k] = 1

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

    for i in range(1, N):

        for k in range(1, 11):

            dp[i + 1][1][k] += dp[i][0][10] + dp[i][1][10]

        for is_less in range(2):

            for k in range(10):

                for l in range(k - 1, k + 2):

                    if not 0 <= l <= 9 or (not is_less and l > S[i]):

                        continue

                    dp[i + 1][is_less or l < S[i]][l] += dp[i][is_less][k]

    return sum(dp[N][0][k] + dp[N][1][k] for k in range(10)) >= key

def binary_search(bad, good, key):

    while good - bad > 1:

        mid = (bad + good) // 2

        if is_good(mid, key):

            good = mid

        else:

            bad = mid

    return good

K = int(eval(input()))

print((binary_search(0, 3234566667, K)))

import sys

sys.setrecursionlimit(10**5)

def dfs(v):
    if v > 3234566667:
        return
    ans.append(v)
    d = v % 10
    if d - 1 >= 0:
        dfs(v * 10 + d - 1)
    dfs(v * 10 + d)
    if d + 1 < 10:
        dfs(v * 10 + d + 1)

K = int(eval(input()))
ans = []
[dfs(i) for i in range(1, 10)]
print((sorted(ans)[K - 1]))

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

+                for l in range(k - 1, k + 2):

+                    if not 0 <= l <= 9 or (not is_less and l > S[i]):

+                    dp[i + 1][is_less or l < S[i]][l] += dp[i][is_less][k]

+    return sum(dp[N][0][k] + dp[N][1][k] for k in range(10)) >= key


Input:
#!python3

import sys

iim = lambda: list(map(int, sys.stdin.readline().rstrip().split()))

def get_factor(N):

    N1 = N + 2

    A = [2, 0] * (N1//2)

    for i in range(3, N1, 2):

        if A[i]:

            continue

        for j in range(i, N1, 2*i):

            A[j] = i

    def factor(n):

        i = 0

        ans = []

        while n & 1 == 0:

            i += 1

            n >>= 1

        if i:

            ans.append((2, i))

        while n > 1:

            a = A[n]

            i = 1

            while True:

                n //= a

                b = A[n]

                if a != b:

                    ans.append((a, i))

                    b = a

                    break

                i += 1

        return ans

    return factor

def resolve():

    N = int(sys.stdin.readline())

    f = get_factor(N)

    ans = 0

    for i in range(1, N):

        num = 1

        #print(i, f(i))

        for a, b in f(i):

            num *= b + 1

        ans += num

    print(ans)

if __name__ == "__main__":

    resolve()

#!python3

import sys

iim = lambda: list(map(int, sys.stdin.readline().rstrip().split()))

def resolve():

    N = int(sys.stdin.readline())

    N1 = N - 1

    ans = 0

    for i in range(1, N):

        ans += N1//i

    print(ans)

if __name__ == "__main__":

    resolve()

#!python3
import sys

iim = lambda: list(map(int, sys.stdin.readline().rstrip().split()))

def get_factor(N):
    N1 = N + 2
    A = [2, 0] * (N1 // 2)
    for i in range(3, N1, 2):
        if A[i]:
            continue
        for j in range(i, N1, 2 * i):
            A[j] = i

    def factor(n):
        i = 0
        ans = []
        while n & 1 == 0:
            i += 1
            n >>= 1
        if i:
            ans.append((2, i))
        while n > 1:
            a = A[n]
            i = 1
            while True:
                n //= a
                b = A[n]
                if a != b:
                    ans.append((a, i))
                    b = a
                    break
                i += 1
        return ans

    return factor

def resolve():
    N = int(sys.stdin.readline())
    f = get_factor(N)
    ans = 0
    for i in range(1, N):
        num = 1
        # print(i, f(i))
        for a, b in f(i):
            num *= b + 1
        ans += num
    print(ans)

if __name__ == "__main__":
    resolve()

#!python3
import sys

iim = lambda: list(map(int, sys.stdin.readline().rstrip().split()))

def resolve():
    N = int(sys.stdin.readline())
    N1 = N - 1
    ans = 0
    for i in range(1, N):
        ans += N1 // i
    print(ans)

if __name__ == "__main__":
    resolve()

-def get_factor(N):
-    N1 = N + 2
-    A = [2, 0] * (N1 // 2)
-    for i in range(3, N1, 2):
-        if A[i]:
-            continue
-        for j in range(i, N1, 2 * i):
-            A[j] = i
-    def factor(n):
-        i = 0
-        ans = []
-        while n & 1 == 0:
-            i += 1
-            n >>= 1
-        if i:
-            ans.append((2, i))
-        while n > 1:
-            a = A[n]
-            i = 1
-            while True:
-                n //= a
-                b = A[n]
-                if a != b:
-                    ans.append((a, i))
-                    b = a
-                    break
-                i += 1
-        return ans
-    return factor
-    f = get_factor(N)
+    N1 = N - 1
-        num = 1
-        # print(i, f(i))
-        for a, b in f(i):
-            num *= b + 1
-        ans += num
+        ans += N1 // i


Input:
b = eval(input())

if b == "T":

  print ("A")

elif b == "A":

  print ("T")

elif b == ("G"):

  print ("C")

else:

  print ("G")

array = ["A","T","G","C"]

b = eval(input())

if b == array[0]:

  print((array[1]))

elif b == array[1]:

  print((array[0]))

elif b == array[2]:

  print((array[3]))

else:

  print((array[2]))

b = eval(input())
if b == "T":
    print("A")
elif b == "A":
    print("T")
elif b == ("G"):
    print("C")
else:
    print("G")

array = ["A", "T", "G", "C"]
b = eval(input())
if b == array[0]:
    print((array[1]))
elif b == array[1]:
    print((array[0]))
elif b == array[2]:
    print((array[3]))
else:
    print((array[2]))

+array = ["A", "T", "G", "C"]
-if b == "T":
-    print("A")
-elif b == "A":
-    print("T")
-elif b == ("G"):
-    print("C")
+if b == array[0]:
+    print((array[1]))
+elif b == array[1]:
+    print((array[0]))
+elif b == array[2]:
+    print((array[3]))
-    print("G")
+    print((array[2]))


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

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

i = A.index(1)

j = K-1

l = i

r = N - l - 1

ans, e = divmod(l, j)

if e:

    ans += 1

    r -= K - e - 1

ans += (r+j-1)//j

print(ans)

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

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

print((((N-1)+(K-1)-1)//(K-1)))

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
i = A.index(1)
j = K - 1
l = i
r = N - l - 1
ans, e = divmod(l, j)
if e:
    ans += 1
    r -= K - e - 1
ans += (r + j - 1) // j
print(ans)

N, K = list(map(int, input().split()))
(*A,) = list(map(int, input().split()))
print((((N - 1) + (K - 1) - 1) // (K - 1)))

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


Input:
class UnionFind:

    def __init__(self, n):

        # 親要素のノード番号を格納　par[x] == x の時そのノードは根

        self.par = [i for i in range(n+1)]

        # 木の高さを格納する（初期状態では0)

        self.rank = [0] * (n+1)

        # 各々の集合の要素数（根が代表して値を持っておく）

        self.count = [1] * (n+1)

    # 検索

    def find(self, x):

        # 根ならその番号を返す

        root = x

        queue = [x]

        data = []

        while queue:

            now = queue.pop()

            if self.par[now] == now:

                root = now

            else:

                # 根でないなら親の要素で再検索

                data.append(now)

                queue.append(self.par[now])

        # 検索する過程で親を更新

        for y in data:

            self.par[y] = root

        return root

    # 併合

    def union(self, x, y):

        x = self.find(x)

        y = self.find(y)

        t = (x == y)

        if UnionFind.same_check(self,x,y):

            return t

        if self.rank[x] < self.rank[y]:

            self.par[x] = y

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

            self.count[x] = 0

        else:

            self.par[y] = x

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

            self.count[y] = 0

            if self.rank[x] == self.rank[y]:

                self.rank[x] += 1

        return t

    # 同じ集合に属しているか判定

    def same_check(self, x, y):

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

    # 属している集合の要素数

    def size(self,x):

        q = UnionFind.find(self,x)

        return self.count[q]

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

u = UnionFind(n)

friend = [1 for i in range(n)]

for i in range(m):

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

    a -= 1

    b -= 1

    friend[a] += 1

    friend[b] += 1

    u.union(a,b)

ans = [u.size(i)-friend[i] for i in range(n)]

#print(*ans)

for i in range(k):

    c,d = list(map(int,input().split()))

    c -= 1

    d -= 1

    if u.same_check(c,d):

        ans[c] -= 1

        ans[d] -= 1

print((*ans))

class UnionFind:

    def __init__(self, n):

        # 親要素のノード番号を格納　par[x] == x の時そのノードは根

        self.par = [i for i in range(n+1)]

        # 木の高さを格納する（初期状態では0)

        self.rank = [0] * (n+1)

        # 各々の集合の要素数（根が代表して値を持っておく）

        self.count = [1] * (n+1)

    # 検索

    def find(self, x):

        # 根ならその番号を返す

        root = x

        queue = [x]

        data = []

        while queue:

            now = queue.pop()

            if self.par[now] == now:

                root = now

            else:

                # 根でないなら親の要素で再検索

                data.append(now)

                queue.append(self.par[now])

        # 検索する過程で親を更新

        for y in data:

            self.par[y] = root

        return root

    # 併合

    def union(self, x, y):

        x = self.find(x)

        y = self.find(y)

        t = (x == y)

        if UnionFind.same_check(self,x,y):

            return t

        if self.rank[x] < self.rank[y]:

            self.par[x] = y

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

            self.count[x] = 0

        else:

            self.par[y] = x

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

            self.count[y] = 0

            if self.rank[x] == self.rank[y]:

                self.rank[x] += 1

        return t

    # 同じ集合に属しているか判定

    def same_check(self, x, y):

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

    # 属している集合の要素数

    def size(self,x):

        q = UnionFind.find(self,x)

        return self.count[q]

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

u = UnionFind(n)

ans = [-1 for i in range(n)]

for i in range(m):

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

    a -= 1

    b -= 1

    ans[a] -= 1

    ans[b] -= 1

    u.union(a,b)

#print(*ans)

for i in range(k):

    c,d = list(map(int,input().split()))

    c -= 1

    d -= 1

    if u.same_check(c,d):

        ans[c] -= 1

        ans[d] -= 1

print((*[u.size(i)+x for i,x in enumerate(ans)]))

class UnionFind:
    def __init__(self, n):
        # 親要素のノード番号を格納　par[x] == x の時そのノードは根
        self.par = [i for i in range(n + 1)]
        # 木の高さを格納する（初期状態では0)
        self.rank = [0] * (n + 1)
        # 各々の集合の要素数（根が代表して値を持っておく）
        self.count = [1] * (n + 1)

    # 検索
    def find(self, x):
        # 根ならその番号を返す
        root = x
        queue = [x]
        data = []
        while queue:
            now = queue.pop()
            if self.par[now] == now:
                root = now
            else:
                # 根でないなら親の要素で再検索
                data.append(now)
                queue.append(self.par[now])
        # 検索する過程で親を更新
        for y in data:
            self.par[y] = root
        return root

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        t = x == y
        if UnionFind.same_check(self, x, y):
            return t
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.count[y] += self.count[x]
            self.count[x] = 0
        else:
            self.par[y] = x
            self.count[x] += self.count[y]
            self.count[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return t

    # 同じ集合に属しているか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    # 属している集合の要素数
    def size(self, x):
        q = UnionFind.find(self, x)
        return self.count[q]

n, m, k = list(map(int, input().split()))
u = UnionFind(n)
friend = [1 for i in range(n)]
for i in range(m):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    friend[a] += 1
    friend[b] += 1
    u.union(a, b)
ans = [u.size(i) - friend[i] for i in range(n)]
# print(*ans)
for i in range(k):
    c, d = list(map(int, input().split()))
    c -= 1
    d -= 1
    if u.same_check(c, d):
        ans[c] -= 1
        ans[d] -= 1
print((*ans))

class UnionFind:
    def __init__(self, n):
        # 親要素のノード番号を格納　par[x] == x の時そのノードは根
        self.par = [i for i in range(n + 1)]
        # 木の高さを格納する（初期状態では0)
        self.rank = [0] * (n + 1)
        # 各々の集合の要素数（根が代表して値を持っておく）
        self.count = [1] * (n + 1)

    # 検索
    def find(self, x):
        # 根ならその番号を返す
        root = x
        queue = [x]
        data = []
        while queue:
            now = queue.pop()
            if self.par[now] == now:
                root = now
            else:
                # 根でないなら親の要素で再検索
                data.append(now)
                queue.append(self.par[now])
        # 検索する過程で親を更新
        for y in data:
            self.par[y] = root
        return root

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        t = x == y
        if UnionFind.same_check(self, x, y):
            return t
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.count[y] += self.count[x]
            self.count[x] = 0
        else:
            self.par[y] = x
            self.count[x] += self.count[y]
            self.count[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return t

    # 同じ集合に属しているか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    # 属している集合の要素数
    def size(self, x):
        q = UnionFind.find(self, x)
        return self.count[q]

n, m, k = list(map(int, input().split()))
u = UnionFind(n)
ans = [-1 for i in range(n)]
for i in range(m):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    ans[a] -= 1
    ans[b] -= 1
    u.union(a, b)
# print(*ans)
for i in range(k):
    c, d = list(map(int, input().split()))
    c -= 1
    d -= 1
    if u.same_check(c, d):
        ans[c] -= 1
        ans[d] -= 1
print((*[u.size(i) + x for i, x in enumerate(ans)]))

-ans = [u.size(i) - friend[i] for i in range(n)]

+print((*[u.size(i) + x for i, x in enumerate(ans)]))


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

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

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

v.append(0)

# forward

now = 0

for i in range(n):

    if now + t[i] < v[i]:

        v[i] = now + t[i]

    if v[i] <= v[i+1]:

        now = v[i]

    elif t[i] + v[i+1] + now < 2 * v[i]:

        v[i] = (t[i] + v[i+1] + now) / 2

        now = v[i+1]

    else:

        now = v[i+1]

# back

now = 0

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

    if now + t[i] < v[i]:

        v[i] = now + t[i]

    if v[i] <= v[i-1]:

        now = v[i]

    elif t[i] + v[i-1] + now < 2 * v[i]:

        v[i] = (t[i] + v[i-1] + now) / 2

        now = v[i-1]

    else:

        now = v[i-1]

ans = 0

now = 0

for i in range(n):

    t1 = v[i] - now

    t3 = v[i] - v[i+1]

    t2 = t[i] - t1 - t3

    ans += now * t1 + t1 * t1 / 2

    now = v[i] # now += t1

    ans += now * t2

    # now = now

    ans += now * t3 - t3 * t3 / 2

    now -= t3

print(ans)

n = int(eval(input()))

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

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

v.append(0)

# forward

now = 0

for i in range(n):

    if now + t[i] < v[i]:

        v[i] = now + t[i]

    if v[i] <= v[i+1]:

        now = v[i]

    elif t[i] + v[i+1] + now < 2 * v[i]:

        v[i] = (t[i] + v[i+1] + now) / 2

        now = v[i+1]

    else:

        now = v[i+1]

# back

now = 0

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

    if now + t[i] < v[i]:

        v[i] = now + t[i]

    if v[i] <= v[i-1]:

        now = v[i]

    elif t[i] + v[i-1] + now < 2 * v[i]:

        v[i] = (t[i] + v[i-1] + now) / 2

        now = v[i-1]

    else:

        now = v[i-1]

ans = 0

now = 0

for i in range(n):

    t1 = v[i] - now

    t3 = v[i] - v[i+1]

    # t2 = t[i] - t1 - t3

    ans += now * t1 + t1 * t1 / 2

    now = v[i] # now += t1

    ans += now * (t[i] - t1 - t3) # ans += now * t2

    # now = now

    ans += now * t3 - t3 * t3 / 2

    now -= t3

print(ans)

n = int(eval(input()))
t = list(map(int, input().split()))
v = list(map(int, input().split()))
v.append(0)
# forward
now = 0
for i in range(n):
    if now + t[i] < v[i]:
        v[i] = now + t[i]
    if v[i] <= v[i + 1]:
        now = v[i]
    elif t[i] + v[i + 1] + now < 2 * v[i]:
        v[i] = (t[i] + v[i + 1] + now) / 2
        now = v[i + 1]
    else:
        now = v[i + 1]
# back
now = 0
for i in range(n - 1, -1, -1):
    if now + t[i] < v[i]:
        v[i] = now + t[i]
    if v[i] <= v[i - 1]:
        now = v[i]
    elif t[i] + v[i - 1] + now < 2 * v[i]:
        v[i] = (t[i] + v[i - 1] + now) / 2
        now = v[i - 1]
    else:
        now = v[i - 1]
ans = 0
now = 0
for i in range(n):
    t1 = v[i] - now
    t3 = v[i] - v[i + 1]
    t2 = t[i] - t1 - t3
    ans += now * t1 + t1 * t1 / 2
    now = v[i]  # now += t1
    ans += now * t2
    # now = now
    ans += now * t3 - t3 * t3 / 2
    now -= t3
print(ans)

n = int(eval(input()))
t = list(map(int, input().split()))
v = list(map(int, input().split()))
v.append(0)
# forward
now = 0
for i in range(n):
    if now + t[i] < v[i]:
        v[i] = now + t[i]
    if v[i] <= v[i + 1]:
        now = v[i]
    elif t[i] + v[i + 1] + now < 2 * v[i]:
        v[i] = (t[i] + v[i + 1] + now) / 2
        now = v[i + 1]
    else:
        now = v[i + 1]
# back
now = 0
for i in range(n - 1, -1, -1):
    if now + t[i] < v[i]:
        v[i] = now + t[i]
    if v[i] <= v[i - 1]:
        now = v[i]
    elif t[i] + v[i - 1] + now < 2 * v[i]:
        v[i] = (t[i] + v[i - 1] + now) / 2
        now = v[i - 1]
    else:
        now = v[i - 1]
ans = 0
now = 0
for i in range(n):
    t1 = v[i] - now
    t3 = v[i] - v[i + 1]
    # t2 = t[i] - t1 - t3
    ans += now * t1 + t1 * t1 / 2
    now = v[i]  # now += t1
    ans += now * (t[i] - t1 - t3)  # ans += now * t2
    # now = now
    ans += now * t3 - t3 * t3 / 2
    now -= t3
print(ans)

+    ans += now * (t[i] - t1 - t3)  # ans += now * t2


Input:
# coding: utf-8

# Your code here!

SIZE=300000; MOD=10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD

fac = [0]*(SIZE+1)# fac[j] = j! mod MOD

finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD

inv[1] = 1

fac[0] = fac[1] = 1

finv[0] = finv[1] = 1

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

    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD

    fac[i] = fac[i-1]*i%MOD

    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算

    if 0 <= r <= n:

        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD

    else:

        return 0

h,w,n = [int(i) for i in input().split()]

xy = [[1,1]]+[[int(i) for i in input().split()] for _ in range(n)]

dp = [0]*(n+1)

dp[0] = 1

xy.sort(key=lambda x: x[1])

xy.sort(key=lambda x: x[0])

#print(xy)

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

    x,y = xy[i]

    for j in range(i):

        xj,yj = xy[j]

        dp[i] -= choose(x - xj + y-yj, y-yj)*dp[j]

        dp[i] %= MOD

ans = 0

#print(dp)

for i,dpi in enumerate(dp):

    x,y=xy[i]

    ans += choose(h-x + w-y, w-y) * dpi

    ans %= MOD

print(ans)

# coding: utf-8

# Your code here!

SIZE=300000; MOD=10**9+7 #ここを変更する

inv = [0]*(SIZE+1)# inv[j] = j^{-1} mod MOD

fac = [0]*(SIZE+1)# fac[j] = j! mod MOD

finv = [0]*(SIZE+1)# finv[j] = (j!)^{-1} mod MOD

inv[1] = 1

fac[0] = fac[1] = 1

finv[0] = finv[1] = 1

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

    inv[i] = MOD -(MOD//i)*inv[MOD%i]%MOD

    fac[i] = fac[i-1]*i%MOD

    finv[i]= finv[i-1]*inv[i]%MOD

def choose(n,r): # nCk mod MOD の計算

    if 0 <= r <= n:

        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD

    else:

        return 0

h,w,n = [int(i) for i in input().split()]

xy = [[1,1]]+[[int(i) for i in input().split()] for _ in range(n)]

dp = [0]*(n+1)

dp[0] = 1

xy.sort()

#print(xy)

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

    x,y = xy[i]

    for j in range(i):

        xj,yj = xy[j]

        dp[i] -= choose(x - xj + y-yj, y-yj)*dp[j]

        dp[i] %= MOD

ans = 0

#print(dp)

for i,dpi in enumerate(dp):

    x,y=xy[i]

    ans += choose(h-x + w-y, w-y) * dpi

    ans %= MOD

print(ans)

# coding: utf-8
# Your code here!
SIZE = 300000
MOD = 10**9 + 7  # ここを変更する
inv = [0] * (SIZE + 1)  # inv[j] = j^{-1} mod MOD
fac = [0] * (SIZE + 1)  # fac[j] = j! mod MOD
finv = [0] * (SIZE + 1)  # finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2, SIZE + 1):
    inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
    fac[i] = fac[i - 1] * i % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD

def choose(n, r):  # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n] * finv[r] % MOD) * finv[n - r] % MOD
    else:
        return 0

h, w, n = [int(i) for i in input().split()]
xy = [[1, 1]] + [[int(i) for i in input().split()] for _ in range(n)]
dp = [0] * (n + 1)
dp[0] = 1
xy.sort(key=lambda x: x[1])
xy.sort(key=lambda x: x[0])
# print(xy)
for i in range(1, n + 1):
    x, y = xy[i]
    for j in range(i):
        xj, yj = xy[j]
        dp[i] -= choose(x - xj + y - yj, y - yj) * dp[j]
        dp[i] %= MOD
ans = 0
# print(dp)
for i, dpi in enumerate(dp):
    x, y = xy[i]
    ans += choose(h - x + w - y, w - y) * dpi
    ans %= MOD
print(ans)

# coding: utf-8
# Your code here!
SIZE = 300000
MOD = 10**9 + 7  # ここを変更する
inv = [0] * (SIZE + 1)  # inv[j] = j^{-1} mod MOD
fac = [0] * (SIZE + 1)  # fac[j] = j! mod MOD
finv = [0] * (SIZE + 1)  # finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2, SIZE + 1):
    inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
    fac[i] = fac[i - 1] * i % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD

def choose(n, r):  # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n] * finv[r] % MOD) * finv[n - r] % MOD
    else:
        return 0

h, w, n = [int(i) for i in input().split()]
xy = [[1, 1]] + [[int(i) for i in input().split()] for _ in range(n)]
dp = [0] * (n + 1)
dp[0] = 1
xy.sort()
# print(xy)
for i in range(1, n + 1):
    x, y = xy[i]
    for j in range(i):
        xj, yj = xy[j]
        dp[i] -= choose(x - xj + y - yj, y - yj) * dp[j]
        dp[i] %= MOD
ans = 0
# print(dp)
for i, dpi in enumerate(dp):
    x, y = xy[i]
    ans += choose(h - x + w - y, w - y) * dpi
    ans %= MOD
print(ans)

-xy.sort(key=lambda x: x[1])
-xy.sort(key=lambda x: x[0])
+xy.sort()


Input:
import itertools

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

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

cnt = [i for i in range(n)]

ans = 10**12

ok = False

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

    for conb in itertools.combinations(cnt, i):

        z = [0]*(m+1)

        for j in conb:

            for k in range(m+1):

                z[k] = z[k] + ca[j][k]

            if sum(l >= x for l in z[1:]) == m:

                ans = min(ans, z[0])

                ok = True

if ok:

    print(ans)

else:

    print((-1))

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

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

ans = 10**111

ok = False

for i in range(2**n):

    y = [0]*(m+1)

    for j in range(n):

        if i & (1 << j):

            for k in range(m+1):

                y[k] = y[k] + ca[j][k]

    if sum(z >= x for z in y[1:]) == m:

        ans = min(ans, y[0])

        ok = True

if ok:

    print(ans)

else:

    print((-1))

import itertools

n, m, x = list(map(int, input().split()))
ca = [list(map(int, input().split())) for _ in range(n)]
cnt = [i for i in range(n)]
ans = 10**12
ok = False
for i in range(1, n + 1):
    for conb in itertools.combinations(cnt, i):
        z = [0] * (m + 1)
        for j in conb:
            for k in range(m + 1):
                z[k] = z[k] + ca[j][k]
            if sum(l >= x for l in z[1:]) == m:
                ans = min(ans, z[0])
                ok = True
if ok:
    print(ans)
else:
    print((-1))

n, m, x = list(map(int, input().split()))
ca = [list(map(int, input().split())) for _ in range(n)]
ans = 10**111
ok = False
for i in range(2**n):
    y = [0] * (m + 1)
    for j in range(n):
        if i & (1 << j):
            for k in range(m + 1):
                y[k] = y[k] + ca[j][k]
    if sum(z >= x for z in y[1:]) == m:
        ans = min(ans, y[0])
        ok = True
if ok:
    print(ans)
else:
    print((-1))

-    for conb in itertools.combinations(cnt, i):

-            if sum(l >= x for l in z[1:]) == m:

+    if sum(z >= x for z in y[1:]) == m:


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

s = [(eval(input())) for i in range(n)]

m = int(eval(input()))

t = [(eval(input())) for i in range(m)]

print((max([0]+[s.count(s[i])-t.count(s[i]) for i in range(n)])))

n=int(eval(input()))

s=[eval(input()) for i in range(n)]

m=int(eval(input()))

t=[eval(input()) for i in range(m)]

u=set(s)

ans=0

for i in u:

    cnt=s.count(i)-t.count(i)

    ans=max(ans,cnt)

print(ans)

n = int(eval(input()))
s = [(eval(input())) for i in range(n)]
m = int(eval(input()))
t = [(eval(input())) for i in range(m)]
print((max([0] + [s.count(s[i]) - t.count(s[i]) for i in range(n)])))

n = int(eval(input()))
s = [eval(input()) for i in range(n)]
m = int(eval(input()))
t = [eval(input()) for i in range(m)]
u = set(s)
ans = 0
for i in u:
    cnt = s.count(i) - t.count(i)
    ans = max(ans, cnt)
print(ans)

-s = [(eval(input())) for i in range(n)]

-t = [(eval(input())) for i in range(m)]

-print((max([0] + [s.count(s[i]) - t.count(s[i]) for i in range(n)])))


Input:
import numpy as np

import sys

def main():

  input = sys.stdin.readline

  n = int(eval(input()))

  a = np.array([int(x) for x in input().split()])

  inf = pow(10, 9)+7

  max_a = np.max(a)

  bit = 1

  ans = 0

  num = 0

  while max_a:

    num += 1

    max_a //= 2

  for _ in range(num):

    bit_count = np.sum(a%2)

    ans += ((((bit_count%inf)*((n-bit_count)%inf))%inf)*(bit%inf))%inf

    ans %= inf

    a //= 2

    bit *= 2

  print(ans)

if __name__ == "__main__":

  main()

import numpy as np

n = int(eval(input()))

a = np.array([int(x) for x in input().split()])

ans = 0

mod = pow(10, 9)+7

for i in range(100):

  c = np.sum((a>>i)&1)

  ans += (c*(n-c)%mod)*pow(2, i, mod)%mod

  ans %= mod

print(ans)

import numpy as np
import sys

def main():
    input = sys.stdin.readline
    n = int(eval(input()))
    a = np.array([int(x) for x in input().split()])
    inf = pow(10, 9) + 7
    max_a = np.max(a)
    bit = 1
    ans = 0
    num = 0
    while max_a:
        num += 1
        max_a //= 2
    for _ in range(num):
        bit_count = np.sum(a % 2)
        ans += (
            (((bit_count % inf) * ((n - bit_count) % inf)) % inf) * (bit % inf)
        ) % inf
        ans %= inf
        a //= 2
        bit *= 2
    print(ans)

if __name__ == "__main__":
    main()

import numpy as np

n = int(eval(input()))
a = np.array([int(x) for x in input().split()])
ans = 0
mod = pow(10, 9) + 7
for i in range(100):
    c = np.sum((a >> i) & 1)
    ans += (c * (n - c) % mod) * pow(2, i, mod) % mod
    ans %= mod
print(ans)

-    a = np.array([int(x) for x in input().split()])

-            (((bit_count % inf) * ((n - bit_count) % inf)) % inf) * (bit % inf)

+a = np.array([int(x) for x in input().split()])

+    ans += (c * (n - c) % mod) * pow(2, i, mod) % mod


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

ans = 0

for x in range(k+1):

  for y in range(k+1):

    if x+y > s: break

    elif s-x-y <= k: ans += 1

print(ans)

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

ans = 0

for x in range(k+1):

  for y in range(k+1):

    if 0 <= s - (x+y) <= k:

      ans += 1

print(ans)

k, s = list(map(int, input().split()))
ans = 0
for x in range(k + 1):
    for y in range(k + 1):
        if x + y > s:
            break
        elif s - x - y <= k:
            ans += 1
print(ans)

k, s = list(map(int, input().split()))
ans = 0
for x in range(k + 1):
    for y in range(k + 1):
        if 0 <= s - (x + y) <= k:
            ans += 1
print(ans)

-        if x + y > s:
-            break
-        elif s - x - y <= k:
+        if 0 <= s - (x + y) <= k:


Input:
#!/usr/bin/env python3

import sys

from pprint import pprint

def solve(H: int, W: int, A: "List[str]"):

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

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

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

    s = set()

    for i in range(H):

        for j in range(W):

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

                s.add((i, j))

                used[i][j] = True

    ret = 0

    while True:

        nex = set()

        for x, y in s:

            for j in range(4):

                tx = x + dx[j]

                ty = y + dy[j]

                if 0 <= tx < H and 0 <= ty < W and not used[tx][ty]:

                    nex.add((tx, ty))

                    used[tx][ty] = True

        if len(nex) < 1:

            break

        s = nex

        ret += 1

    print(ret)

    return

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    H = int(next(tokens))  # type: int

    W = int(next(tokens))  # type: int

    A = [next(tokens) for _ in range(H) ]  # type: "List[str]"

    solve(H, W, A)

if __name__ == '__main__':

    main()

#!/usr/bin/env python3

import sys

from pprint import pprint

def solve(H: int, W: int, A: "List[str]"):

    count = [[H + W] * (W + 2) for _ in range(H + 2)]

    for i in range(H):

        for j in range(W):

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

                count[i + 1][j + 1] = 0

            else:

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

    #pprint(count)

    ret = 0

    for i in range(H, 0, -1):

        for j in range(W, 0, -1):

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

            ret = max(ret, count[i][j])

    #pprint(count)

    print(ret)

    return

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    H = int(next(tokens))  # type: int

    W = int(next(tokens))  # type: int

    A = [next(tokens) for _ in range(H) ]  # type: "List[str]"

    solve(H, W, A)

if __name__ == '__main__':

    main()

#!/usr/bin/env python3
import sys
from pprint import pprint

def solve(H: int, W: int, A: "List[str]"):
    used = [[False] * W for _ in range(H)]
    dx = [1, 0, -1, 0]
    dy = [0, -1, 0, 1]
    s = set()
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                s.add((i, j))
                used[i][j] = True
    ret = 0
    while True:
        nex = set()
        for x, y in s:
            for j in range(4):
                tx = x + dx[j]
                ty = y + dy[j]
                if 0 <= tx < H and 0 <= ty < W and not used[tx][ty]:
                    nex.add((tx, ty))
                    used[tx][ty] = True
        if len(nex) < 1:
            break
        s = nex
        ret += 1
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [next(tokens) for _ in range(H)]  # type: "List[str]"
    solve(H, W, A)

if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys
from pprint import pprint

def solve(H: int, W: int, A: "List[str]"):
    count = [[H + W] * (W + 2) for _ in range(H + 2)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                count[i + 1][j + 1] = 0
            else:
                count[i + 1][j + 1] = min(count[i][j + 1], count[i + 1][j]) + 1
    # pprint(count)
    ret = 0
    for i in range(H, 0, -1):
        for j in range(W, 0, -1):
            count[i][j] = min(count[i][j], min(count[i][j + 1], count[i + 1][j]) + 1)
            ret = max(ret, count[i][j])
    # pprint(count)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [next(tokens) for _ in range(H)]  # type: "List[str]"
    solve(H, W, A)

if __name__ == "__main__":
    main()

-    used = [[False] * W for _ in range(H)]

+    count = [[H + W] * (W + 2) for _ in range(H + 2)]

-                if 0 <= tx < H and 0 <= ty < W and not used[tx][ty]:

-                    used[tx][ty] = True

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

+            ret = max(ret, count[i][j])


Input:
from collections import defaultdict

import bisect

S = eval(input())

T = eval(input())

if set(T) - set(S):

    print((-1))

    exit()

dic = defaultdict(list)

leng_dic = defaultdict(int)

for pos, s in enumerate(S):

    dic[s].append(pos)

    leng_dic[s] += 1

length = len(S)

now = -1

count = 0

for t in T:

    if dic[t][-1] < now+1:

        now = -1

        count += 1

    idx = bisect.bisect_left(dic[t], now+1)

    now = dic[t][idx]

ans = count*length + now + 1

print(ans)

from collections import defaultdict

import bisect

S = eval(input())

T = eval(input())

if set(T) - set(S):

    print((-1))

    exit()

dic = defaultdict(list)

for pos, s in enumerate(S):

    dic[s].append(pos)

now = -1

count = 0

for t in T:

    if dic[t][-1] < now+1:

        now = -1

        count += 1

    idx = bisect.bisect_left(dic[t], now+1)

    now = dic[t][idx]

ans = count*len(S) + now + 1

print(ans)

from collections import defaultdict
import bisect

S = eval(input())
T = eval(input())
if set(T) - set(S):
    print((-1))
    exit()
dic = defaultdict(list)
leng_dic = defaultdict(int)
for pos, s in enumerate(S):
    dic[s].append(pos)
    leng_dic[s] += 1
length = len(S)
now = -1
count = 0
for t in T:
    if dic[t][-1] < now + 1:
        now = -1
        count += 1
    idx = bisect.bisect_left(dic[t], now + 1)
    now = dic[t][idx]
ans = count * length + now + 1
print(ans)

from collections import defaultdict
import bisect

S = eval(input())
T = eval(input())
if set(T) - set(S):
    print((-1))
    exit()
dic = defaultdict(list)
for pos, s in enumerate(S):
    dic[s].append(pos)
now = -1
count = 0
for t in T:
    if dic[t][-1] < now + 1:
        now = -1
        count += 1
    idx = bisect.bisect_left(dic[t], now + 1)
    now = dic[t][idx]
ans = count * len(S) + now + 1
print(ans)

-leng_dic = defaultdict(int)
-    leng_dic[s] += 1
-length = len(S)
-ans = count * length + now + 1
+ans = count * len(S) + now + 1


Input:
import sys

from collections import deque

read = sys.stdin.read

readline = sys.stdin.readline

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

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

def bfs(grid, start):

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

    queue = deque([start])

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

    visited[start[0]][start[1]] = True

    while queue:

        x, y = queue.popleft()

        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):

            new_y, new_x = y + j, x + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if grid[new_x][new_y] == '.' and not visited[new_x][new_y]:

                    distance[new_x][new_y] = distance[x][y] + 1

                    visited[new_x][new_y] = True

                    queue.append((new_x, new_y))

    return distance

answer = 0

limit = H - 1 + W - 1

for i in range(H):

    for j in range(W):

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

            continue

        cnt = 0

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

            new_y, new_x = dy + j, dx + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if S[new_x][new_y] == '.':

                    cnt += 1

        if cnt > 2:

            continue

        tmp = max(list(map(max, bfs(S, (i, j)))))

        if tmp > answer:

            answer = tmp

        if answer == limit:

            print(answer)

            exit()

print(answer)

import sys

from collections import deque

read = sys.stdin.read

readline = sys.stdin.readline

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

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

def bfs(grid, start):

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

    queue = deque([start])

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

    visited[start[0]][start[1]] = True

    while queue:

        x, y = queue.popleft()

        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):

            new_y, new_x = y + j, x + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if grid[new_x][new_y] == '.' and not visited[new_x][new_y]:

                    distance[new_x][new_y] = distance[x][y] + 1

                    visited[new_x][new_y] = True

                    queue.append((new_x, new_y))

    return distance

answer = 0

for i in range(H):

    for j in range(W):

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

            continue

        cnt = 0

        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):

            new_y, new_x = dy + j, dx + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if S[new_x][new_y] == '.':

                    cnt += 1

        if cnt > 2:

            continue

        tmp = max(list(map(max, bfs(S, (i, j)))))

        if tmp > answer:

            answer = tmp

print(answer)

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
H, W = list(map(int, readline().split()))
S = [list(eval(input())) for i in range(H)]

def bfs(grid, start):
    distance = [[0] * W for _ in range(H)]
    queue = deque([start])
    visited = [[False] * W for _ in range(H)]
    visited[start[0]][start[1]] = True
    while queue:
        x, y = queue.popleft()
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == "." and not visited[new_x][new_y]:
                    distance[new_x][new_y] = distance[x][y] + 1
                    visited[new_x][new_y] = True
                    queue.append((new_x, new_y))
    return distance

answer = 0
limit = H - 1 + W - 1
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        cnt = 0
        for dx, dy in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = dy + j, dx + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if S[new_x][new_y] == ".":
                    cnt += 1
        if cnt > 2:
            continue
        tmp = max(list(map(max, bfs(S, (i, j)))))
        if tmp > answer:
            answer = tmp
        if answer == limit:
            print(answer)
            exit()
print(answer)

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
H, W = list(map(int, readline().split()))
S = [list(eval(input())) for i in range(H)]

def bfs(grid, start):
    distance = [[0] * W for _ in range(H)]
    queue = deque([start])
    visited = [[False] * W for _ in range(H)]
    visited[start[0]][start[1]] = True
    while queue:
        x, y = queue.popleft()
        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == "." and not visited[new_x][new_y]:
                    distance[new_x][new_y] = distance[x][y] + 1
                    visited[new_x][new_y] = True
                    queue.append((new_x, new_y))
    return distance

answer = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        cnt = 0
        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = dy + j, dx + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if S[new_x][new_y] == ".":
                    cnt += 1
        if cnt > 2:
            continue
        tmp = max(list(map(max, bfs(S, (i, j)))))
        if tmp > answer:
            answer = tmp
print(answer)


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

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

n_list = list(str(n))

n_list = [int(num) for num in n_list]

num = {1,2,3,4,5,6,7,8,9,0}

d_set = set(d)

empty = set()

#print(set(n_list))

#print(d_set)

#print(set(n_list) & d_set)

while(1):

  n_list = list(str(n))

  n_list = [int(num) for num in n_list]

  if(set(n_list) & d_set == empty):

    print(n)

    break

  n += 1

import sys

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

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

d_set = set(d)

num_set = set([1,2,3,4,5,6,7,8,9,0])

enable = num_set - d_set

#print(type(enable))

#print(enable)

def dfs(s):

  #print(s)

  global ans

  # すでにN以上の場合はこの先を探索しない

  if s != "":

    if int(s) >= N:

      #print(s)

      ans.append(int(s))

      return

  for num in enable:

    if len(s) == 0 and num == 0:

      continue

    else:

      (dfs(s+str(num)))

  return

ans = []

dfs("")

#print(ans)

print((min(ans)))

n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
n_list = list(str(n))
n_list = [int(num) for num in n_list]
num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
d_set = set(d)
empty = set()
# print(set(n_list))
# print(d_set)
# print(set(n_list) & d_set)
while 1:
    n_list = list(str(n))
    n_list = [int(num) for num in n_list]
    if set(n_list) & d_set == empty:
        print(n)
        break
    n += 1

import sys

N, K = list(map(int, input().split()))
d = list(map(int, input().split()))
d_set = set(d)
num_set = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
enable = num_set - d_set
# print(type(enable))
# print(enable)
def dfs(s):
    # print(s)
    global ans
    # すでにN以上の場合はこの先を探索しない
    if s != "":
        if int(s) >= N:
            # print(s)
            ans.append(int(s))
            return
    for num in enable:
        if len(s) == 0 and num == 0:
            continue
        else:
            (dfs(s + str(num)))
    return

ans = []
dfs("")
# print(ans)
print((min(ans)))

-    n_list = [int(num) for num in n_list]

+    # すでにN以上の場合はこの先を探索しない


Input:
#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""

input:

6

1 2 2 3

2 2 3 4

3 1 5

4 1 6

5 1 6

6 0

output:

1 1 12

2 2 11

3 3 8

4 9 10

5 4 7

6 5 6

"""

import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2

def generate_adj_matrix(v_info):

    for v_detail in v_info:

        v_index, adj_num, *adj_list = list(map(int, v_detail))

        # assert len(adj_list) == adj_num

        init_adj_table[v_index].extend(sorted(adj_list, reverse=True))

    return init_adj_table

def graph_dfs(v_init):

    global time

    # init the first node of overall graph iterations(times >= 1)

    stack.append(v_init)

    d_time[v_init] += time

    # init end

    while stack:

        current = stack[-1]

        v_table = adj_table[current]

        color[current]=VISITED_IN_STACK

        # if adj is None, current's adj(s) have been all visited

        adj = v_table.pop() if v_table else None

        if adj:

            if color[adj] is UNVISITED:

                color[adj] = VISITED_IN_STACK

                time += 1

                d_time[adj] += time

                stack.append(adj)

        else:

            stack.pop()

            color[current] = POPPED_OUT

            time += 1

            f_time[current] += time

    return None

def dfs_start():

    global time

    for i in range(vertices):

        if color[i + 1] == UNVISITED:

            # print('round: ', i + 1)

            graph_dfs(i + 1)

            time += 1

if __name__ == '__main__':

    _input = sys.stdin.readlines()

    vertices = int(_input[0])

    v_info = [x.split() for x in _input[1:]]

    init_adj_table = tuple([] for _ in range(vertices + 1))

    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)

    stack = []

    color = [UNVISITED] * (vertices + 1)

    d_time, f_time = ([0] * (vertices + 1) for _ in range(2))

    adj_table = generate_adj_matrix(v_info)

    # timing start from 1

    time = 1

    dfs_start()

    for index, v in enumerate(zip(d_time[1:], f_time[1:]), 1):

        print((index, *v))

#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""

input:

6

1 2 2 3

2 2 3 4

3 1 5

4 1 6

5 1 6

6 0

output:

1 1 12

2 2 11

3 3 8

4 9 10

5 4 7

6 5 6

"""

import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2

def dfs(v_init):

    global timer

    # init the first node of overall graph iterations(times >= 1)

    stack = list()

    stack.append(v_init)

    d_time[v_init] += timer

    # init end

    while stack:

        current = stack[-1]

        v_table = adj_table[current]

        visited[current] = VISITED_IN_STACK

        # if adj is None, current's adj(s) have been all visited

        adj = v_table.pop() if v_table else None

        if adj:

            if visited[adj] is UNVISITED:

                visited[adj] = VISITED_IN_STACK

                timer += 1

                d_time[adj] += timer

                stack.append(adj)

        else:

            stack.pop()

            visited[current] = POPPED_OUT

            timer += 1

            f_time[current] += timer

    return None

def dfs_init():

    global timer

    for v in range(v_num):

        if visited[v + 1] == UNVISITED:

            dfs(v + 1)

            timer += 1

if __name__ == '__main__':

    _input = sys.stdin.readlines()

    v_num = int(_input[0])

    vertices = [x.split() for x in _input[1:]]

    # config length = (v_num + 1)

    # stack = []

    visited = [UNVISITED] * (v_num + 1)

    d_time, f_time = ([0] * (v_num + 1) for _ in range(2))

    adj_table = tuple([] for _ in range(v_num + 1))

    for v_info in vertices:

        v_index, adj_num, *adj_list = list(map(int, v_info))

        # assert len(adj_list) == adj_num

        adj_table[v_index].extend(sorted(adj_list, reverse=True))

    # timing start from 1

    timer = 1

    dfs_init()

    for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):

        print((index, *time_info))

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0
output:
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6
"""
import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2

def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_index, adj_num, *adj_list = list(map(int, v_detail))
        # assert len(adj_list) == adj_num
        init_adj_table[v_index].extend(sorted(adj_list, reverse=True))
    return init_adj_table

def graph_dfs(v_init):
    global time
    # init the first node of overall graph iterations(times >= 1)
    stack.append(v_init)
    d_time[v_init] += time
    # init end
    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        color[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None
        if adj:
            if color[adj] is UNVISITED:
                color[adj] = VISITED_IN_STACK
                time += 1
                d_time[adj] += time
                stack.append(adj)
        else:
            stack.pop()
            color[current] = POPPED_OUT
            time += 1
            f_time[current] += time
    return None

def dfs_start():
    global time
    for i in range(vertices):
        if color[i + 1] == UNVISITED:
            # print('round: ', i + 1)
            graph_dfs(i + 1)
            time += 1

if __name__ == "__main__":
    _input = sys.stdin.readlines()
    vertices = int(_input[0])
    v_info = [x.split() for x in _input[1:]]
    init_adj_table = tuple([] for _ in range(vertices + 1))
    # assert len(vertex_info) == vertex_num
    # config length = (vertex_num + 1)
    stack = []
    color = [UNVISITED] * (vertices + 1)
    d_time, f_time = ([0] * (vertices + 1) for _ in range(2))
    adj_table = generate_adj_matrix(v_info)
    # timing start from 1
    time = 1
    dfs_start()
    for index, v in enumerate(zip(d_time[1:], f_time[1:]), 1):
        print((index, *v))

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0
output:
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 6
"""
import sys

UNVISITED, VISITED_IN_STACK, POPPED_OUT = 0, 1, 2

def dfs(v_init):
    global timer
    # init the first node of overall graph iterations(times >= 1)
    stack = list()
    stack.append(v_init)
    d_time[v_init] += timer
    # init end
    while stack:
        current = stack[-1]
        v_table = adj_table[current]
        visited[current] = VISITED_IN_STACK
        # if adj is None, current's adj(s) have been all visited
        adj = v_table.pop() if v_table else None
        if adj:
            if visited[adj] is UNVISITED:
                visited[adj] = VISITED_IN_STACK
                timer += 1
                d_time[adj] += timer
                stack.append(adj)
        else:
            stack.pop()
            visited[current] = POPPED_OUT
            timer += 1
            f_time[current] += timer
    return None

def dfs_init():
    global timer
    for v in range(v_num):
        if visited[v + 1] == UNVISITED:
            dfs(v + 1)
            timer += 1

if __name__ == "__main__":
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = [x.split() for x in _input[1:]]
    # config length = (v_num + 1)
    # stack = []
    visited = [UNVISITED] * (v_num + 1)
    d_time, f_time = ([0] * (v_num + 1) for _ in range(2))
    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = list(map(int, v_info))
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(sorted(adj_list, reverse=True))
    # timing start from 1
    timer = 1
    dfs_init()
    for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):
        print((index, *time_info))

-        v_index, adj_num, *adj_list = list(map(int, v_detail))

-        # assert len(adj_list) == adj_num

-        init_adj_table[v_index].extend(sorted(adj_list, reverse=True))

-        color[current] = VISITED_IN_STACK

+        visited[current] = VISITED_IN_STACK

-            if color[adj] is UNVISITED:

-                color[adj] = VISITED_IN_STACK

+            if visited[adj] is UNVISITED:

+                visited[adj] = VISITED_IN_STACK

-            color[current] = POPPED_OUT

+            visited[current] = POPPED_OUT

+        if visited[v + 1] == UNVISITED:

-    v_info = [x.split() for x in _input[1:]]

-    init_adj_table = tuple([] for _ in range(vertices + 1))

-    # assert len(vertex_info) == vertex_num

-    color = [UNVISITED] * (vertices + 1)

-    d_time, f_time = ([0] * (vertices + 1) for _ in range(2))

-    adj_table = generate_adj_matrix(v_info)

+    vertices = [x.split() for x in _input[1:]]

+    visited = [UNVISITED] * (v_num + 1)

+    d_time, f_time = ([0] * (v_num + 1) for _ in range(2))

+    adj_table = tuple([] for _ in range(v_num + 1))

+        v_index, adj_num, *adj_list = list(map(int, v_info))

+        # assert len(adj_list) == adj_num

+        adj_table[v_index].extend(sorted(adj_list, reverse=True))

-    for index, v in enumerate(zip(d_time[1:], f_time[1:]), 1):

+    for index, time_info in enumerate(zip(d_time[1:], f_time[1:]), 1):


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

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

ans = 'No'

flag = False

for i in range(N-2):

  if D[i][0] == D[i][1] and D[i+1][0] == D[i+1][1] and D[i+2][0] == D[i+2][1]: flag = True

  if flag:

    ans = 'Yes'

    break

print(ans)

N = int(eval(input()))

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

ans = 'No'

for i in range(N-2):

  if D[i][0] == D[i][1] and D[i+1][0] == D[i+1][1] and D[i+2][0] == D[i+2][1]:

    ans = 'Yes'

    break

print(ans)

N = int(eval(input()))
D = [list(map(int, input().split())) for _ in range(N)]
ans = "No"
flag = False
for i in range(N - 2):
    if D[i][0] == D[i][1] and D[i + 1][0] == D[i + 1][1] and D[i + 2][0] == D[i + 2][1]:
        flag = True
    if flag:
        ans = "Yes"
        break
print(ans)

N = int(eval(input()))
D = [list(map(int, input().split())) for _ in range(N)]
ans = "No"
for i in range(N - 2):
    if D[i][0] == D[i][1] and D[i + 1][0] == D[i + 1][1] and D[i + 2][0] == D[i + 2][1]:
        ans = "Yes"
        break
print(ans)

-flag = False
-        flag = True
-    if flag:


Input:
import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

a=[[0],[0],[0]]

n = int(eval(input()))

for i in range(n):

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

    a[0].append(A)

    a[1].append(B)

    a[2].append(C)

dp = []

for j in range(n+1):

    dp.append([0,0,0])

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

dp[1][1] = a[1][1]

dp[1][2] = a[2][1]

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

    for j in range(3):

        x = dp[i-1][(j+1)%3]+a[j][i]

        y = dp[i-1][(j+2)%3]+a[j][i]

        dp[i][j] = max(x,y)

print((max(dp[n][0],dp[n][1],dp[n][2])))

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

a,b,c = 0,0,0

n = int(eval(input()))

for i in range(n):

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

    a,b,c = max(b,c)+x,max(a,c)+y,max(a,b)+z

print((max(a,b,c)))

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
a = [[0], [0], [0]]
n = int(eval(input()))
for i in range(n):
    A, B, C = list(map(int, input().split()))
    a[0].append(A)
    a[1].append(B)
    a[2].append(C)
dp = []
for j in range(n + 1):
    dp.append([0, 0, 0])
dp[1][0] = a[0][1]
dp[1][1] = a[1][1]
dp[1][2] = a[2][1]
for i in range(2, n + 1):
    for j in range(3):
        x = dp[i - 1][(j + 1) % 3] + a[j][i]
        y = dp[i - 1][(j + 2) % 3] + a[j][i]
        dp[i][j] = max(x, y)
print((max(dp[n][0], dp[n][1], dp[n][2])))

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
a, b, c = 0, 0, 0
n = int(eval(input()))
for i in range(n):
    x, y, z = list(map(int, input().split()))
    a, b, c = max(b, c) + x, max(a, c) + y, max(a, b) + z
print((max(a, b, c)))

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

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

+    a, b, c = max(b, c) + x, max(a, c) + y, max(a, b) + z


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

weight = []

value = []

for i in range(n):

    wi, vi = list(map(int, input().split()))

    weight.append(wi)

    value.append(vi)

v_sum = sum(value)

dp = [[float('inf') for _ in range(v_sum+1)] for i in range(n)]

for i in range(n):

    if i == 0:

        dp[i][0] = 0

        dp[i][value[0]] = weight[0]

    else:

        for k in range(v_sum+1):

            if k < value[i]:

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

            else:

                dp[i][k] = min(dp[i-1][k], dp[i-1][k-value[i]]+weight[i])

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

    if dp[n-1][i] <= w:

        print(i)

        exit()

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

weight = []

value = []

for i in range(n):

    wi, vi = list(map(int, input().split()))

    weight.append(wi)

    value.append(vi)

v_sum = sum(value)

dp = [[10**12 for _ in range(v_sum+1)] for i in range(n)]

for i in range(n):

    if i == 0:

        dp[i][0] = 0

        dp[i][value[0]] = weight[0]

    else:

        for k in range(v_sum+1):

            if k < value[i]:

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

            else:

                dp[i][k] = min(dp[i-1][k], dp[i-1][k-value[i]]+weight[i])

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

    if dp[n-1][i] <= w:

        print(i)

        exit()

n, w = list(map(int, input().split()))
weight = []
value = []
for i in range(n):
    wi, vi = list(map(int, input().split()))
    weight.append(wi)
    value.append(vi)
v_sum = sum(value)
dp = [[float("inf") for _ in range(v_sum + 1)] for i in range(n)]
for i in range(n):
    if i == 0:
        dp[i][0] = 0
        dp[i][value[0]] = weight[0]
    else:
        for k in range(v_sum + 1):
            if k < value[i]:
                dp[i][k] = dp[i - 1][k]
            else:
                dp[i][k] = min(dp[i - 1][k], dp[i - 1][k - value[i]] + weight[i])
for i in range(v_sum, -1, -1):
    if dp[n - 1][i] <= w:
        print(i)
        exit()

n, w = list(map(int, input().split()))
weight = []
value = []
for i in range(n):
    wi, vi = list(map(int, input().split()))
    weight.append(wi)
    value.append(vi)
v_sum = sum(value)
dp = [[10**12 for _ in range(v_sum + 1)] for i in range(n)]
for i in range(n):
    if i == 0:
        dp[i][0] = 0
        dp[i][value[0]] = weight[0]
    else:
        for k in range(v_sum + 1):
            if k < value[i]:
                dp[i][k] = dp[i - 1][k]
            else:
                dp[i][k] = min(dp[i - 1][k], dp[i - 1][k - value[i]] + weight[i])
for i in range(v_sum, -1, -1):
    if dp[n - 1][i] <= w:
        print(i)
        exit()

-dp = [[float("inf") for _ in range(v_sum + 1)] for i in range(n)]

+dp = [[10**12 for _ in range(v_sum + 1)] for i in range(n)]


Input:
P=10**9+7

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

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

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

A=[i for i in X]

B=[i for i in Y]

if len(set(A))!=N or len(set(B))!=M:

    print((0))

    exit()

L=[]

x=0

y=0

D=N*M

while(len(A)>0 and len(B)>0):

    if max(A[-1],B[-1])==D:

        if A[-1]>B[-1]:

            A.pop()

            L.append(y)

            x+=1

        elif A[-1]<B[-1]:

            B.pop()

            L.append(x)

            y+=1

        else:

            A.pop()

            B.pop()

            x+=1

            y+=1

        D-=1

    else:

        C=max(A[-1],B[-1])+1

        tmp=x*y-(N*M-D)

        for i in range(C,D+1)[::-1]:

            L.append(tmp)

            tmp-=1

        D=max(A[-1],B[-1])

if len(A)>0:

    D=min(Y)-1

    while(len(A)>0):

        C=A[-1]+1

        tmp=x*y-(N*M-D)

        for i in range(C,D+1)[::-1]:

            L.append(tmp)

            tmp-=1

        D=A.pop()-1

        x+=1

        L.append(M)

if len(B)>0:

    D=min(X)-1

    while(len(B)>0):

        C=B[-1]+1

        tmp=x*y-(N*M-D)

        for i in range(C,D+1)[::-1]:

            L.append(tmp)

            tmp-=1

        D=B.pop()-1

        y+=1

        L.append(N)

m=min(min(X),min(Y))-1

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

    L.append(i)

ans=1

for i in L:

    ans*=i

    ans%=P

print(ans)

P=10**9+7

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

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

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

mx=A[0]-1

my=B[0]-1

m=min(mx,my)

ans=1

if len(set(A))!=N or len(set(B))!=M:

    print((0))

    exit()

x=0

y=0

D=N*M

while(len(A)>0 and len(B)>0):

    if max(A[-1],B[-1])==D:

        if A[-1]>B[-1]:

            A.pop()

            ans*=y

            ans%=P

            x+=1

        elif A[-1]<B[-1]:

            B.pop()

            ans*=x

            ans%=P

            y+=1

        else:

            A.pop()

            B.pop()

            x+=1

            y+=1

        D-=1

    else:

        C=max(A[-1],B[-1])+1

        for i in range(C,D+1):

            ans*=x*y-N*M+i

            ans%=P

        D=max(A[-1],B[-1])

if len(A)>0:

    D=my

    while(len(A)>0):

        C=A[-1]+1

        for i in range(C,D+1)[::-1]:

            ans*=x*y-N*M+i

            ans%=P

        D=A.pop()-1

        x+=1

        ans*=M

        ans%=P

if len(B)>0:

    D=mx

    while(len(B)>0):

        C=B[-1]+1

        for i in range(C,D+1):

            ans*=x*y-N*M+i

            ans%=P

        D=B.pop()-1

        y+=1

        ans*=N

        ans%=P

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

    ans*=i

    ans%=P

print(ans)

P = 10**9 + 7
N, M = list(map(int, input().split()))
X = sorted([int(i) for i in input().split()])
Y = sorted([int(i) for i in input().split()])
A = [i for i in X]
B = [i for i in Y]
if len(set(A)) != N or len(set(B)) != M:
    print((0))
    exit()
L = []
x = 0
y = 0
D = N * M
while len(A) > 0 and len(B) > 0:
    if max(A[-1], B[-1]) == D:
        if A[-1] > B[-1]:
            A.pop()
            L.append(y)
            x += 1
        elif A[-1] < B[-1]:
            B.pop()
            L.append(x)
            y += 1
        else:
            A.pop()
            B.pop()
            x += 1
            y += 1
        D -= 1
    else:
        C = max(A[-1], B[-1]) + 1
        tmp = x * y - (N * M - D)
        for i in range(C, D + 1)[::-1]:
            L.append(tmp)
            tmp -= 1
        D = max(A[-1], B[-1])
if len(A) > 0:
    D = min(Y) - 1
    while len(A) > 0:
        C = A[-1] + 1
        tmp = x * y - (N * M - D)
        for i in range(C, D + 1)[::-1]:
            L.append(tmp)
            tmp -= 1
        D = A.pop() - 1
        x += 1
        L.append(M)
if len(B) > 0:
    D = min(X) - 1
    while len(B) > 0:
        C = B[-1] + 1
        tmp = x * y - (N * M - D)
        for i in range(C, D + 1)[::-1]:
            L.append(tmp)
            tmp -= 1
        D = B.pop() - 1
        y += 1
        L.append(N)
m = min(min(X), min(Y)) - 1
for i in range(1, m + 1):
    L.append(i)
ans = 1
for i in L:
    ans *= i
    ans %= P
print(ans)

P = 10**9 + 7
N, M = list(map(int, input().split()))
A = sorted([int(i) for i in input().split()])
B = sorted([int(i) for i in input().split()])
mx = A[0] - 1
my = B[0] - 1
m = min(mx, my)
ans = 1
if len(set(A)) != N or len(set(B)) != M:
    print((0))
    exit()
x = 0
y = 0
D = N * M
while len(A) > 0 and len(B) > 0:
    if max(A[-1], B[-1]) == D:
        if A[-1] > B[-1]:
            A.pop()
            ans *= y
            ans %= P
            x += 1
        elif A[-1] < B[-1]:
            B.pop()
            ans *= x
            ans %= P
            y += 1
        else:
            A.pop()
            B.pop()
            x += 1
            y += 1
        D -= 1
    else:
        C = max(A[-1], B[-1]) + 1
        for i in range(C, D + 1):
            ans *= x * y - N * M + i
            ans %= P
        D = max(A[-1], B[-1])
if len(A) > 0:
    D = my
    while len(A) > 0:
        C = A[-1] + 1
        for i in range(C, D + 1)[::-1]:
            ans *= x * y - N * M + i
            ans %= P
        D = A.pop() - 1
        x += 1
        ans *= M
        ans %= P
if len(B) > 0:
    D = mx
    while len(B) > 0:
        C = B[-1] + 1
        for i in range(C, D + 1):
            ans *= x * y - N * M + i
            ans %= P
        D = B.pop() - 1
        y += 1
        ans *= N
        ans %= P
for i in range(1, m + 1):
    ans *= i
    ans %= P
print(ans)

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

-Y = sorted([int(i) for i in input().split()])

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

+B = sorted([int(i) for i in input().split()])

-        for i in range(C, D + 1)[::-1]:

-        for i in range(C, D + 1)[::-1]:


Input:
import bisect

import sys

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

s = tuple([-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0]*A))) + [10**18])

t = tuple([-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0]*B))) + [10**18])

def solve(s,t,x):

    ans = 10**18

    for i in range(2):

        a, b = (s, t) if i==0 else (t, s)

        posX1 = bisect.bisect_left(a,x)

        for j in range(2):

            ax = a[posX1 -j]

            posX2 = bisect.bisect_left(b,ax)

            for k in range(2):

                bx = b[posX2-k]

                ans = min(ans, abs(x-ax)+abs(bx-ax))

    return ans

for _ in range(Q):

    x = int(eval(input()))

    print((solve(s,t,x)))

import bisect

import sys

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

s = tuple([-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0]*A))) + [10**18])

t = tuple([-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0]*B))) + [10**18])

def solve(s,t,x):

    ans = 10**18

    for i in range(2):

        a, b = (s, t) if i==0 else (t, s)

        posX1 = bisect.bisect_left(a,x)

        for j in range(2):

            ax = a[posX1 -j]

            posX2 = bisect.bisect_left(b,ax)

            for k in range(2):

                bx = b[posX2-k]

                ans = min(ans, abs(x-ax)+abs(bx-ax))

    return ans

ans = [solve(s,t,x) for x in map(int,sys.stdin)]

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

import bisect
import sys

A, B, Q = list(map(int, input().split()))
s = tuple(
    [-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0] * A))) + [10**18]
)
t = tuple(
    [-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0] * B))) + [10**18]
)

def solve(s, t, x):
    ans = 10**18
    for i in range(2):
        a, b = (s, t) if i == 0 else (t, s)
        posX1 = bisect.bisect_left(a, x)
        for j in range(2):
            ax = a[posX1 - j]
            posX2 = bisect.bisect_left(b, ax)
            for k in range(2):
                bx = b[posX2 - k]
                ans = min(ans, abs(x - ax) + abs(bx - ax))
    return ans

for _ in range(Q):
    x = int(eval(input()))
    print((solve(s, t, x)))

import bisect
import sys

A, B, Q = map(int, input().split())
s = tuple(
    [-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0] * A))) + [10**18]
)
t = tuple(
    [-(10**18)] + list(map(int, (sys.stdin.readline() for _ in [0] * B))) + [10**18]
)

def solve(s, t, x):
    ans = 10**18
    for i in range(2):
        a, b = (s, t) if i == 0 else (t, s)
        posX1 = bisect.bisect_left(a, x)
        for j in range(2):
            ax = a[posX1 - j]
            posX2 = bisect.bisect_left(b, ax)
            for k in range(2):
                bx = b[posX2 - k]
                ans = min(ans, abs(x - ax) + abs(bx - ax))
    return ans

ans = [solve(s, t, x) for x in map(int, sys.stdin)]
print(*ans, sep="\n")

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

+ans = [solve(s, t, x) for x in map(int, sys.stdin)]


Input:
import collections

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

temp = [k-q for _ in range(n)]

for _ in range(q):

  a = int(eval(input()))

  temp[a-1] += 1

for i in temp:

  print(('Yes' if i > 0 else 'No'))

import collections

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

temp = [k-q for _ in range(n)]

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

for A in a:

  temp[A-1] += 1

for i in temp:

  print(('Yes' if i > 0 else 'No'))

import collections

n, k, q = list(map(int, input().split()))
temp = [k - q for _ in range(n)]
for _ in range(q):
    a = int(eval(input()))
    temp[a - 1] += 1
for i in temp:
    print(("Yes" if i > 0 else "No"))

import collections

n, k, q = list(map(int, input().split()))
temp = [k - q for _ in range(n)]
a = [int(eval(input())) for _ in range(q)]
for A in a:
    temp[A - 1] += 1
for i in temp:
    print(("Yes" if i > 0 else "No"))

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


Input:
import bisect

n = int(eval(input()))

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

l.sort()

ans = 0

# a < b < c とすると、2式は必ず成立し、c < a + b のみ考慮すれば良い

# つまり　b < c < a + b

# c < a + b の境界部の発見に2分探索を利用する

for i in range(n - 1):

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

        k = bisect.bisect_left(l, l[i] + l[j])

        ans += k - (j + 1)

print(ans)

def main():

    import bisect

    n = int(eval(input()))

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

    l.sort()

    ans = 0

    # a < b < c とすると、2式は必ず成立し、c < a + b のみ考慮すれば良い

    # つまり　b < c < a + b

    # c < a + b の境界部の発見に2分探索を利用する

    for i in range(n - 1):

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

            k = bisect.bisect_left(l, l[i] + l[j])

            ans += k - (j + 1)

    print(ans)

if __name__ == '__main__':

    main()

def main():
    import bisect

    n = int(eval(input()))
    l = list(map(int, input().split()))
    l.sort()
    ans = 0
    # a < b < c とすると、2式は必ず成立し、c < a + b のみ考慮すれば良い
    # つまり　b < c < a + b
    # c < a + b の境界部の発見に2分探索を利用する
    for i in range(n - 1):
        for j in range(i + 1, n):
            k = bisect.bisect_left(l, l[i] + l[j])
            ans += k - (j + 1)
    print(ans)

if __name__ == "__main__":
    main()

-# a < b < c とすると、2式は必ず成立し、c < a + b のみ考慮すれば良い

-# c < a + b の境界部の発見に2分探索を利用する

-        k = bisect.bisect_left(l, l[i] + l[j])

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

+    # a < b < c とすると、2式は必ず成立し、c < a + b のみ考慮すれば良い

+    # c < a + b の境界部の発見に2分探索を利用する

+            k = bisect.bisect_left(l, l[i] + l[j])


Input:
S = eval(input())

Q = int(eval(input()))

Query = [input().split() for _ in range(Q)]

rev = False

S1 = ''

S2 = ''

for TFC in Query:

    if TFC[0] == '1':

        rev = not rev

    else:

        if TFC[1] == '1':

            if rev:

                S2 += TFC[2]

            else:

                S1 = TFC[2] + S1

        else:

            if rev:

                S1 = TFC[2] + S1

            else:

                S2 += TFC[2]

if rev:

    print((S2[::-1] + S[::-1] + S1[::-1]))

else:

    print((S1 + S + S2))

from collections import deque

S = eval(input())

Q = int(eval(input()))

Query = [input().split() for _ in range(Q)]

S = deque(S)

rev = False

for TFC in Query:

    if TFC[0] == '1':

        rev = not rev

    else:

        if TFC[1] == '1':

            if rev:

                S.append(TFC[2])

            else:

                S.appendleft(TFC[2])

        else:

            if rev:

                S.appendleft(TFC[2])

            else:

                S.append(TFC[2])

if rev:

    S.reverse()

print((''.join(S)))

S = eval(input())
Q = int(eval(input()))
Query = [input().split() for _ in range(Q)]
rev = False
S1 = ""
S2 = ""
for TFC in Query:
    if TFC[0] == "1":
        rev = not rev
    else:
        if TFC[1] == "1":
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]
if rev:
    print((S2[::-1] + S[::-1] + S1[::-1]))
else:
    print((S1 + S + S2))

from collections import deque

S = eval(input())
Q = int(eval(input()))
Query = [input().split() for _ in range(Q)]
S = deque(S)
rev = False
for TFC in Query:
    if TFC[0] == "1":
        rev = not rev
    else:
        if TFC[1] == "1":
            if rev:
                S.append(TFC[2])
            else:
                S.appendleft(TFC[2])
        else:
            if rev:
                S.appendleft(TFC[2])
            else:
                S.append(TFC[2])
if rev:
    S.reverse()
print(("".join(S)))


Input:
import sys

import math

from collections import defaultdict

sys.setrecursionlimit(10**7)

def input():

    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())

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

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

def Line(N,num):

    if N<=0:

        return [[]]*num

    elif num==1:

        return [I() for _ in range(N)]

    else:

        read_all = [tuple(II()) for _ in range(N)]

        return map(list, zip(*read_all))

#################

from bisect import bisect_left

#Binary Indexed Tree（区間加算）

#1-indexed

class Range_BIT():

    def __init__(self, N):

        self.size = N

        self.data0 = [0]*(N+1)

        self.data1 = [0]*(N+1)

    def _add(self, data, k, x):

        while k <= self.size:

            data[k] += x

            k += k & -k

    # 区間[l,r)にxを加算

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

        self._add(self.data0, l, -x*(l-1))

        self._add(self.data0, r, x*(r-1))

        self._add(self.data1, l, x)

        self._add(self.data1, r, -x)

    def _get(self, data, k):

        s = 0

        while k:

            s += data[k]

            k -= k & -k

        return s

    # 区間[l,r)の和を求める

    def query(self, l, r):

        return self._get(self.data1, r-1)*(r-1)+self._get(self.data0, r-1)\

                -self._get(self.data1, l-1)*(l-1)-self._get(self.data0, l-1)

N,M = II()

A = III()

A.sort()

def is_ok(x):

    num = 0

    for a in A:

        p = bisect_left(A,x-a)

        num += N-p

    if num>=M:

        return True

    else:

        return False

#l：その値以上の幸福度の握手がM通り以上ある最大値

l = 0

r = 2*10**5 + 1

while abs(r-l)>1:

    mid = (l+r)//2

    if is_ok(mid):

        l = mid

    else:

        r = mid

#値l以上の握手で用いるA[i]の個数を調べる

bit = Range_BIT(N)

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

    p = bisect_left(A,l-A[i-1])

    if p!=N:

        bit.add(p+1,N+1,1)

        bit.add(i,i+1,N-p)

x = [0]*N

for i in range(N):

    x[i] = bit.query(i+1,i+2)

ans = 0

for i in range(N):

    ans += A[i]*x[i]

#握手の回数がM回になるように調整

ans -= l*((sum(x)-2*M)//2)

print(ans)

import sys

import math

from collections import defaultdict

sys.setrecursionlimit(10**7)

def input():

    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

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

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

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

def Line(N,num):

    if N<=0:

        return [[] for _ in range(num)]

    elif num==1:

        return [I() for _ in range(N)]

    else:

        read_all = [tuple(II()) for _ in range(N)]

        return list(map(list, list(zip(*read_all))))

#################

# FFT

# use python3

# a = np.array([a1,a2,a3]), b= np.array([b1,b2,b3])

# c = np.array([a1b1,a1b2+a2b1,a1a3+a2b2+a3b1,a2b3+a3b2,a3b3])

import numpy as np

def FFT(a,b):

    bit = (len(a)+len(b)).bit_length()+1

    L = 2**bit

    fa,fb = np.fft.rfft(a,L), np.fft.rfft(b,L)

    c = np.rint(np.fft.irfft(fa*fb,L)).astype(np.int64)

    return c[:len(a)+len(b)-1]

N,M = II()

A = III()

h = [0]*(max(A))

for a in A:

    h[a-1] += 1

conv = np.append([0,0],FFT(h,h))

ans = 0

count = 0

for k in range(2,2*max(A)+1)[::-1]:

    if conv[k]:

        num = min(M-count,conv[k])

        count += num

        ans += k*num

    if count==M:

        break

print(ans)

import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I():
    return int(input())

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

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

def Line(N, num):
    if N <= 0:
        return [[]] * num
    elif num == 1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################
from bisect import bisect_left

# Binary Indexed Tree（区間加算）
# 1-indexed
class Range_BIT:
    def __init__(self, N):
        self.size = N
        self.data0 = [0] * (N + 1)
        self.data1 = [0] * (N + 1)

    def _add(self, data, k, x):
        while k <= self.size:
            data[k] += x
            k += k & -k

    # 区間[l,r)にxを加算
    def add(self, l, r, x):
        self._add(self.data0, l, -x * (l - 1))
        self._add(self.data0, r, x * (r - 1))
        self._add(self.data1, l, x)
        self._add(self.data1, r, -x)

    def _get(self, data, k):
        s = 0
        while k:
            s += data[k]
            k -= k & -k
        return s

    # 区間[l,r)の和を求める
    def query(self, l, r):
        return (
            self._get(self.data1, r - 1) * (r - 1)
            + self._get(self.data0, r - 1)
            - self._get(self.data1, l - 1) * (l - 1)
            - self._get(self.data0, l - 1)
        )

N, M = II()
A = III()
A.sort()

def is_ok(x):
    num = 0
    for a in A:
        p = bisect_left(A, x - a)
        num += N - p
    if num >= M:
        return True
    else:
        return False

# l：その値以上の幸福度の握手がM通り以上ある最大値
l = 0
r = 2 * 10**5 + 1
while abs(r - l) > 1:
    mid = (l + r) // 2
    if is_ok(mid):
        l = mid
    else:
        r = mid
# 値l以上の握手で用いるA[i]の個数を調べる
bit = Range_BIT(N)
for i in range(1, N + 1):
    p = bisect_left(A, l - A[i - 1])
    if p != N:
        bit.add(p + 1, N + 1, 1)
        bit.add(i, i + 1, N - p)
x = [0] * N
for i in range(N):
    x[i] = bit.query(i + 1, i + 2)
ans = 0
for i in range(N):
    ans += A[i] * x[i]
# 握手の回数がM回になるように調整
ans -= l * ((sum(x) - 2 * M) // 2)
print(ans)

import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

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

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

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

def Line(N, num):
    if N <= 0:
        return [[] for _ in range(num)]
    elif num == 1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return list(map(list, list(zip(*read_all))))

#################
# FFT
# use python3
# a = np.array([a1,a2,a3]), b= np.array([b1,b2,b3])
# c = np.array([a1b1,a1b2+a2b1,a1a3+a2b2+a3b1,a2b3+a3b2,a3b3])
import numpy as np

def FFT(a, b):
    bit = (len(a) + len(b)).bit_length() + 1
    L = 2**bit
    fa, fb = np.fft.rfft(a, L), np.fft.rfft(b, L)
    c = np.rint(np.fft.irfft(fa * fb, L)).astype(np.int64)
    return c[: len(a) + len(b) - 1]

N, M = II()
A = III()
h = [0] * (max(A))
for a in A:
    h[a - 1] += 1
conv = np.append([0, 0], FFT(h, h))
ans = 0
count = 0
for k in range(2, 2 * max(A) + 1)[::-1]:
    if conv[k]:
        num = min(M - count, conv[k])
        count += num
        ans += k * num
    if count == M:
        break
print(ans)

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

+        return [[] for _ in range(num)]

-        return map(list, zip(*read_all))

+        return list(map(list, list(zip(*read_all))))

-        self._add(self.data0, l, -x * (l - 1))

-        self._add(self.data0, r, x * (r - 1))

-    # 区間[l,r)の和を求める

-            self._get(self.data1, r - 1) * (r - 1)

-            + self._get(self.data0, r - 1)

-            - self._get(self.data1, l - 1) * (l - 1)

-            - self._get(self.data0, l - 1)

+    bit = (len(a) + len(b)).bit_length() + 1

+    fa, fb = np.fft.rfft(a, L), np.fft.rfft(b, L)

+    c = np.rint(np.fft.irfft(fa * fb, L)).astype(np.int64)

-# l：その値以上の幸福度の握手がM通り以上ある最大値

-# 値l以上の握手で用いるA[i]の個数を調べる

-# 握手の回数がM回になるように調整


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

p = [int(x) for x in input().split()]

ans = 0

for i in range(N):

  if i < N - 1 and p[i] == i + 1:

    p[i] = p[i+1]

    p[i+1] = i+1

    ans += 1

  elif i == N -1 and p[i] == i+1:

    p[i] = p[i-1]

    p[i-1] = i + 1

    ans += 1

print(ans)

N = int(eval(input()))

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

ans = 0

for i in range(N):

    if p[i]-1 == i:

        if i + 1 <= N-1:

            p[i] = p[i+1]

            p[i+1] = i + 1

            ans += 1

        elif i - 1 >= 0:

            p[i] = p[i-1]

            p[i-1] = i + 1

            ans += 1

print(ans)

N = int(eval(input()))
p = [int(x) for x in input().split()]
ans = 0
for i in range(N):
    if i < N - 1 and p[i] == i + 1:
        p[i] = p[i + 1]
        p[i + 1] = i + 1
        ans += 1
    elif i == N - 1 and p[i] == i + 1:
        p[i] = p[i - 1]
        p[i - 1] = i + 1
        ans += 1
print(ans)

N = int(eval(input()))
p = list(map(int, input().split()))
ans = 0
for i in range(N):
    if p[i] - 1 == i:
        if i + 1 <= N - 1:
            p[i] = p[i + 1]
            p[i + 1] = i + 1
            ans += 1
        elif i - 1 >= 0:
            p[i] = p[i - 1]
            p[i - 1] = i + 1
            ans += 1
print(ans)


Input:
import math

def S(a,b,c):

	s = (a+b+c)/2

	return math.sqrt(s*(s-a)*(s-b)*(s-c))

while True:

	try:

		x1,y1,x2,y2,x3,y3,xp,yp = list(map(float, input().split()))

		a = math.sqrt((x1-x2)**2+(y1-y2)**2)

		b = math.sqrt((x1-x3)**2+(y1-y3)**2)

		c = math.sqrt((x2-x3)**2+(y2-y3)**2)

		xa = math.sqrt((x3-xp)**2+(y3-yp)**2)

		xb = math.sqrt((x2-xp)**2+(y2-yp)**2)

		xc = math.sqrt((x1-xp)**2+(y1-yp)**2)

		if S(a,xb,xc)+S(b,xc,xa)+S(c,xa,xb)-S(a,b,c) > 0.0000001:

			print("NO")

		else:

			print("YES")

	except:

		break

import math

def L(x1,y1,x2,y2):

	return math.sqrt((x1-x2)**2+(y1-y2)**2)

def S(a,b,c):

	s=(a+b+c)/2

	return math.sqrt(s*(s-a)*(s-b)*(s-c))

while True:

    try:

        x1,y1,x2,y2,x3,y3,xp,yp = list(map(float, input().split()))

        a  = L(x1,y1,x2,y2)

        b  = L(x1,y1,x3,y3)

        c  = L(x2,y2,x3,y3)

        pa = L(x3,y3,xp,yp)

        pb = L(x2,y2,xp,yp)

        pc = L(x1,y1,xp,yp)

        if S(a,pb,pc)+S(b,pc,pa)+S(c,pa,pb)-S(a,b,c) > 0.0000001:

            print("NO")

        else:

            print("YES")

    except:

        break

import math

def S(a, b, c):
    s = (a + b + c) / 2
    return math.sqrt(s * (s - a) * (s - b) * (s - c))

while True:
    try:
        x1, y1, x2, y2, x3, y3, xp, yp = list(map(float, input().split()))
        a = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
        b = math.sqrt((x1 - x3) ** 2 + (y1 - y3) ** 2)
        c = math.sqrt((x2 - x3) ** 2 + (y2 - y3) ** 2)
        xa = math.sqrt((x3 - xp) ** 2 + (y3 - yp) ** 2)
        xb = math.sqrt((x2 - xp) ** 2 + (y2 - yp) ** 2)
        xc = math.sqrt((x1 - xp) ** 2 + (y1 - yp) ** 2)
        if S(a, xb, xc) + S(b, xc, xa) + S(c, xa, xb) - S(a, b, c) > 0.0000001:
            print("NO")
        else:
            print("YES")
    except:
        break

import math

def L(x1, y1, x2, y2):
    return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

def S(a, b, c):
    s = (a + b + c) / 2
    return math.sqrt(s * (s - a) * (s - b) * (s - c))

while True:
    try:
        x1, y1, x2, y2, x3, y3, xp, yp = list(map(float, input().split()))
        a = L(x1, y1, x2, y2)
        b = L(x1, y1, x3, y3)
        c = L(x2, y2, x3, y3)
        pa = L(x3, y3, xp, yp)
        pb = L(x2, y2, xp, yp)
        pc = L(x1, y1, xp, yp)
        if S(a, pb, pc) + S(b, pc, pa) + S(c, pa, pb) - S(a, b, c) > 0.0000001:
            print("NO")
        else:
            print("YES")
    except:
        break


Input:
n, k, *a = list(map(int, open(0).read().split()))

a = [0] + a

p = 1

while k:

    if k % 2:

        p = a[p]

    a = [a[b] for b in a]

    k //= 2

print(p)

n,k,*a=list(map(int,open(0).read().split()));a=[0]+a;p=1

while k:p=k%2and a[p]or p;a=[a[b]for b in a];k//=2

print(p)

n, k, *a = list(map(int, open(0).read().split()))
a = [0] + a
p = 1
while k:
    p = k % 2 and a[p] or p
    a = [a[b] for b in a]
    k //= 2
print(p)

-    if k % 2:
-        p = a[p]
+    p = k % 2 and a[p] or p


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

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

a.sort()

ans = 1

for i in a:

    if i == 0:

        ans = 0

        break

    ans *= i

    if ans > 10**18:

        ans = -1

        break

print(ans)

num = int(eval(input()))

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

nums.sort()

ans = 1

for i in nums:

    if i == 0:

        ans = 0

        break

    ans *= i

    if ans > 10 **18:

        ans = -1

        break

print(ans)

num = int(eval(input()))
nums = list(map(int, input().split()))
nums.sort()
ans = 1
for i in nums:
    if i == 0:
        ans = 0
        break
    ans *= i
    if ans > 10**18:
        ans = -1
        break
print(ans)

-n = int(eval(input()))
-a = [int(_) for _ in input().split()]
-a.sort()
+num = int(eval(input()))
+nums = list(map(int, input().split()))
+nums.sort()
-for i in a:
+for i in nums:


Input:
import sys

sys.setrecursionlimit(10**7)

def I(): return int(sys.stdin.readline().rstrip())

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

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし

def S(): return sys.stdin.readline().rstrip()

def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり

def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N = I()

mod = 10**9+7

print(((pow(10,N,mod)-2*pow(9,N,mod)+pow(8,N,mod)) % mod))

import sys

def I(): return int(sys.stdin.readline().rstrip())

N = I()

mod = 10**9+7

print(((pow(10,N,mod)-2*pow(9,N,mod)+pow(8,N,mod)) % mod))

import sys

sys.setrecursionlimit(10**7)

def I():
    return int(sys.stdin.readline().rstrip())

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

def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))  # 空白あり

def LI2():
    return list(map(int, sys.stdin.readline().rstrip()))  # 空白なし

def S():
    return sys.stdin.readline().rstrip()

def LS():
    return list(sys.stdin.readline().rstrip().split())  # 空白あり

def LS2():
    return list(sys.stdin.readline().rstrip())  # 空白なし

N = I()
mod = 10**9 + 7
print(((pow(10, N, mod) - 2 * pow(9, N, mod) + pow(8, N, mod)) % mod))

import sys

def I():
    return int(sys.stdin.readline().rstrip())

N = I()
mod = 10**9 + 7
print(((pow(10, N, mod) - 2 * pow(9, N, mod) + pow(8, N, mod)) % mod))

-    return list(map(int, sys.stdin.readline().rstrip().split()))  # 空白あり

-    return list(map(int, sys.stdin.readline().rstrip()))  # 空白なし

-    return sys.stdin.readline().rstrip()

-    return list(sys.stdin.readline().rstrip().split())  # 空白あり

-    return list(sys.stdin.readline().rstrip())  # 空白なし


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

n, p, score = N, 2, 0

while p ** 2 <= N:

  e = 1

  while n >= (p ** e) and n % (p ** e) == 0:

    n //= (p ** e)

    score += 1

    e += 1

  else:

    while n >= p and n % p == 0:

      n //= p

    p += 1

else:

  if n != 1:

    score += 1

print(score)

N = int(eval(input()))

n, p, score = N, 2, 0

while p ** 2 <= N:

  e = 1

  while n % (p ** e) == 0:

    n //= (p ** e)

    score += 1

    e += 1

  else:

    while n % p == 0:

      n //= p

    p += 1

else:

  if n != 1:

    score += 1

print(score)

N = int(eval(input()))
n, p, score = N, 2, 0
while p**2 <= N:
    e = 1
    while n >= (p**e) and n % (p**e) == 0:
        n //= p**e
        score += 1
        e += 1
    else:
        while n >= p and n % p == 0:
            n //= p
        p += 1
else:
    if n != 1:
        score += 1
print(score)

N = int(eval(input()))
n, p, score = N, 2, 0
while p**2 <= N:
    e = 1
    while n % (p**e) == 0:
        n //= p**e
        score += 1
        e += 1
    else:
        while n % p == 0:
            n //= p
        p += 1
else:
    if n != 1:
        score += 1
print(score)