peterverebics/CodeNet_Python_428 huggingface dataset:
# Judge status: Wrong Answer
from itertools import accumulate
from bisect import bisect_left


def main():
    children, cookies = map(int, input().split())
    target = sorted(list(map(int, input().split())))
    sum_target = sum(target)
    if sum_target < cookies:
        print(children - 1)
    elif cookies < target[0]:
        print(0)
    else:
        accumulate_target = list(accumulate(target))
        print(bisect_left(accumulate_target, cookies) + 1)


if __name__ == '__main__':
    main()
# Judge status: Wrong Answer
n = int(input())
a = [list(map(int,input().split())) for i in range(2)]
ans = 0
for i in range(n):
  ans = max(ans , sum(a[0][0:i]) + sum(a[0][i:n-1]))
print(ans)
# Judge status: Wrong Answer
n=int(input())
a=list(map(int,input().split()))
b=sorted(a)
alice=sum(b[::2])
Bob=sum(b[1::2])
print(alice-Bob)
# Judge status: Wrong Answer
s=input()
a=['Sunny','Cloudy','Rainy']

for i in range(2):
  if s==a[i]:
    print(a[i+1])
# Judge status: Accepted
import sys
input = sys.stdin.readline

x, y, a, b, c = map(int, input().split())
p = sorted(map(int, input().split()), reverse=True)
q = sorted(map(int, input().split()), reverse=True)
r = list(map(int, input().split()))

C = p[:x] + q[:y] + r
C.sort(reverse=True)

print(sum(C[:x + y]))
# Judge status: Accepted
from itertools import combinations
_, l = input(), sorted(int(i) for i in input().split())
if len(l) < 3:
    print(0)
else:
    ans = 0
    for v in combinations(l, 3):
        if v[0] + v[1] > v[2] and len(set(v)) == 3:
            ans += 1
    print(ans)
# Judge status: Wrong Answer
n=int(input())
if 100<=n>=111: print(111)
elif 112<=n>=222:print(222)
elif 223<=n>=333:print(333)
elif 334<=n>=444:print(444)
elif 445<=n>=555:print(555)
elif 556<=n>=666:print(666)
elif 667<=n>=777:print(777)
elif 778<=n>=888:print(888)
else:print(999)
# Judge status: Accepted
N = int(input())

def solve_function(N):
    if N % 2 == 1:
        return 0

    it_can_be_divisible = 10
    ans = 0
    while True:
        if it_can_be_divisible > N:
            break
        ans += N//it_can_be_divisible
        it_can_be_divisible *= 5

    return ans

if solve_function(N):
    print(solve_function(N))
else:
    print(0)
# Judge status: Wrong Answer
N = int(input())
S = str(input())

west = 0
for i in range(N):
  if S[i] == "W":
    west += 1

ans_list = [0] * N
for i in range(N):
  if S[i] == "W":
    west -= 1
  if i != 0:
    if S[i - 1] == "E":
      west += 1
  #ans_list[i] = min(west, N - west)
  ans_list[i] = west
#print(ans_list)
print(min(ans_list))
# Judge status: Accepted
def main():
    N = int(input())
    H = list(map(int, input().split()))
    dp = [float('inf')] * (N + 5)
    dp[0] = 0
    for i in range(N):
        if i + 1 <= N - 1:
            dp[i+1] = min(dp[i+1], dp[i] + abs(H[i+1] - H[i]))
        if i + 2 <= N - 1:
            dp[i+2] = min(dp[i+2], dp[i] + abs(H[i+2] - H[i]))
    
    print(dp[N-1])

    

if __name__ == "__main__":
    main()
# Judge status: Wrong Answer
s = input()

if s == "keyence":
    print("YES")
    exit()

for i in range(len(s)+1):
    for j in range(i,len(s)+1):
        if s[:i] + s[i + j:] == "keyence":
            print("YES")
            exit()

print("NO")
# Judge status: Accepted
x,N = list(map(int,input().split(" ")))
if N != 0:
  p = list(map(int,input().split(" ")))
  sa = 0
  ans = -100
else:
  ans = x

while True:
  if ans != -100:
    break
  for i in range(N):
    if x-sa == p[i]:
      break
    if i == N-1:
      if x-sa != p[i]:
        ans = x-sa
  if ans != -100:
    break
  for i in range(N):
    if x+sa == p[i]:
      break
    if i == N-1:
      if x+sa != p[i]:
        ans = x+sa
  if ans != -100:
    break
  sa += 1
print(ans)
# Judge status: Wrong Answer
n,m = map(int,input().split())

for i in range(m):
    print(i+1,n-i)
# Judge status: Accepted
A, x, C = map(str, input().split())

print("A" + x[0] + "C")
# Judge status: Accepted
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(input())

N, W = geta(int)
cur = [0] * (W+1) 

for i in range(N):
    w, v = geta(int)
    update = cur[:]
    for j in range(w,W+1):
        tmp = cur[j-w] + v
        if tmp > cur[j]: update[j] = tmp
    
    cur = update

print(cur[W])
# Judge status: Accepted
s = input()

now = ''
pre = ''
ans = 0
for i in s:
    now += i
    if pre != now:
        ans += 1
        pre = now
        now = ''
print(ans)
# Judge status: Accepted
N=int(input())
result=0
for A in range(1,N):
  result+=int((N-1)/A)
print(result)
# Judge status: Accepted
H, N = input().split()
A = list(input().split())
H = int(H)
N = int(N)
A = [int(a) for a in A]
if H > sum(A):
    print('No')
else:
    print('Yes')
# Judge status: Wrong Answer

import sys
def II(): return int(sys.stdin.readline())

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

N = II()
A = LI()

def comb(k):
	if k == 1:
		return 1
	else:
		return int(k*(k-1)/2)

result = 0
kind = list(set(A))
for k in kind:
	result += comb(A.count(k))

for a in A:
	n = A.count(a)
	print(result-n+1)
# Judge status: Accepted
n,m,k,j = list(map(int,input().split()))

if (abs(m-n)<=j and abs(k-m)<=j) or abs(k-n)<=j:
  print("Yes")
else:
  print("No")
# Judge status: Accepted
def discount(x):
  return (x // 15) * 200

def cost(x):
  return x * 800

N = int(input())
Sum = cost(N) - discount(N)

print(Sum)
# Judge status: Accepted
S = input()

D =[]
for i in range(3):
    if S[i] == S[i+1]:
        D.append(1)
        break


if 1 in D:
    print("Bad")

else:
    print("Good")
# Judge status: Accepted
from collections import deque
k = int(input())
l = deque([i for i in range(1, 10)])
# print(l)

for i in range(k):
    x = l.popleft()
    mod = x % 10
    if mod != 0:
        l.append(x*10 + mod - 1)
    l.append(x*10 + mod)
    if mod != 9:
        l.append(x*10 + mod + 1)

print(x)
# Judge status: Accepted
s = input()
n = len(s)


def dfs(i, s_str, sum):
    if i == n - 1:
        if sum == 7:
            print(s_str + '=7')
            exit()

    else:
        dfs(i+1, s_str + '+' + s[i+1], sum + int(s[i+1]))
        dfs(i+1, s_str + '-' + s[i+1], sum - int(s[i+1]))


dfs(0, s[0], int(s[0]))
# Judge status: Accepted
n = int(input())
a = list(map(int, input().split()))

mod = 10**9+7

ans = 0
p = 1
for i in range(60):
    s = sum(1 for b in a if b&p)
    ans += s*(n-s)*p
    ans %= mod
    p <<= 1
print(ans)
# Judge status: Accepted
o = input()
e = input()
ans = ""
for i in range(len(e)):
    ans += o[i] + e[i]
if len(o) > len(e):
    ans += o[-1]
print(ans)
# Judge status: Wrong Answer
n,m=list(map(int,input().split()))
d={}
for _ in range(n):
    a, b = list(map(int, input().split()))
    d[a]=b

sd=dict(sorted(d.items()))

result=0
ans=0
for i in sd:
	result+=d[i]
	ans+=i*d[i]
	if result>m:
		ans-=i*(result-m)
		break
print(ans)
# Judge status: Accepted
A, B = map(int, input().split())
if 0 in [A%3, B%3, (A+B)%3]:
  print('Possible')
else:
  print('Impossible')
# Judge status: Accepted
x = int(input())
if(x>=30):
    print("Yes")
else:
    print("No")
# Judge status: Accepted
# https://atcoder.jp/contests/agc019/tasks/agc019_a

q, h, s, d = map(int, input().split())
n = int(input())
ans = (n // 2) * min([q * 8, h * 4, s * 2, d]) + (n % 2) * min([q * 4, h * 2, s])
print(ans)
# Judge status: Accepted
import sys
input = sys.stdin.readline
x,y= [int(i) for i in input().split()]
a1 = [1,3,5,7,8,10,12]
a2 = [4,6,9,11]
a3 = [2]
if x in a1 :
    if y in a1 :
        print("Yes")
    else :
        print("No")
elif x in a2 :
    if y in a2 :
        print("Yes")
    else :
        print("No")
elif x == 2 :
    if y == 2:
        print("Yes")
    else :
        print("No")
# Judge status: Accepted
a,b,c=map(int,input().split())
print(b//a if b//a<c else c)
# Judge status: Accepted
def IsPalindrome(S):
	l = len(S)
	for i in range(l >> 1):
		if S[i] != S[-(i + 1)]:
			return False
	return True

S = input()

m = len(S) >> 1
L, R = [""] * (m), [""] * (m)
for i in range(m):
	L[i] = S[i]
	R[i] = S[i + m + 1]

if not (IsPalindrome(L) and IsPalindrome(S) and IsPalindrome(R)):
	print("No")
else:
	print("Yes")
# Judge status: Wrong Answer
from collections import Counter


MOD = 998244353
N = int(input())
D = list(map(int, input().split()))
cnt_D = Counter(D)
if D[0] != 0:
    print(0)
else:
    prev = 1
    ans = 1
    for i in range(1, max(D) + 1):
        v = cnt_D.get(i, 0)
        ans *= pow(prev, v, MOD)
        ans %= MOD
        prev = v
    print(ans)
# Judge status: Wrong Answer
import math
n = int(input())
a = list(map(int,input().split()))

a = sorted(a, reverse = True)
mid = a[0]//2
diff = abs(mid - a[0])

if n == 2:
    print(max(a[0],a[1]),min(a[0],a[1]))
else:
    for i in range(n):
        if abs(a[i] - mid) <= diff:
            diff = abs(a[i] - mid)
            ans = i
    print(a[0], a[ans])
# Judge status: Wrong Answer
n,k = map(int,input().split())
p = 10 ** 9 + 7

ans = 0
max=0
min=0

a=[0]*(n+1)
b=[0]*(n+2)

for i in range(n+1):
    a[i]=i
    b[i+1]=a[i]+b[i]

if n>k:
    for i in range(k,n+2):
        min=b[i]
        max=b[n+1]-b[n+1-i]
        ans=ans+max-min+1
else:
    ans=1

ans = ans % p

print(ans)
# Judge status: Wrong Answer
from collections import deque

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

graph = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
print(graph)
ans = [-1] * (n - 1)
used = [0]*n
used[0] = 1
que = deque([0])
while que:
    posi = que.popleft()
    for p in graph[posi]:
        if not used[p]:
            que.append(p)
            ans[p-1] = posi+1
            used[p] = 1
print('Yes')
print(*ans, sep='\n')
# Judge status: Accepted
Num = int(input())
bossNumsList =  list(map(int, input().split()))

bossNums=[]

for i in range(Num):
  bossNums.append(0)


for i in range(Num-1):
  bossNums[bossNumsList[i]-1] += 1

for i in range(Num):
  print(bossNums[i])
# Judge status: Wrong Answer
n, k = [int(i) for i in input().split()]
h = [int(j) for j in input().split()]

h = sorted(h)
del h[n-k:n]

print(sum(h))
# Judge status: Accepted
n=int(input())
A=[int(input()) for _ in range(n)]+[0]

cnt=0
for i in range(n):
    cnt +=A[i]//2
    if A[i+1]!=0:
        cnt +=A[i]%2
        A[i+1] -=A[i]%2
print(cnt)
# Judge status: Accepted
import numpy as np
N = int(input())
a = list(map(int, input().split()))

a = np.array(a)
cum = np.cumsum(a)[:-1]
rcum = np.cumsum(a[::-1])[:-1]
ans = abs(cum - rcum[::-1])

print(np.min(ans))
# Judge status: Accepted
n = list(str(input()))
maxi = 0
#print(n)
for i in range(len(n)):
  #print(int(n[i])-1, int(n[i])-1 + (len(n)-i-1)*9)
  maxi = max(maxi, int(n[i])-1 + (len(n)-i-1)*9)
print(max(sum(map(int, n)), maxi))
# Judge status: Wrong Answer
A,B = map(int,input().split())
ans = 0
for i in range(A,B+1):
    tmp = str(i)
    L = len(str(i))
    l = L//2
    if L%2 == 0:
        if tmp[:l] == tmp[l:]:
            ans += 1
    else:
        if tmp[:l] == tmp[l+1:]:
            ans += 1
print(ans)
# Judge status: Accepted
n,m = map(int, input().split())
ls = []
rs = []
for _ in range(m):
    l,r = map(int, input().split())
    ls.append(l)
    rs.append(r)

ls.sort()
rs.sort()

ans = rs[0] - ls[-1] + 1
ans = max(0,ans)

print(ans)
# Judge status: Accepted
import fractions

def lcm(x, y):
  return x * y // fractions.gcd(x, y)

N, M = [int(x) for x in input().split()]
l = [int(x) // 2 for x in input().split()]
ans = l[0]
for i in reversed(range(30)):
  if ans % 2 ** i == 0:
    numof2 = 2 ** i
    break
for i in range(1, N):
  if l[i] % numof2 == 0 and (l[i] // numof2) % 2 == 1: 
    ans = lcm(ans, l[i])
  else:
    ans = M + 1
    break
ans2 = M // ans
print(ans2 // 2 + ans2 % 2)
# Judge status: Accepted
N = int(input())

ans = 0

for i in range(2, 10**6+1):
    if N % i != 0: continue
    ans += 1
    cnta = 0
    cntb = 2
    while N % i == 0:
        N //= i
        cnta += 1
        if cnta > cntb:
            cnta -= cntb
            cntb += 1
            ans += 1
    if N == 1:
        break
if N != 1:
    ans += 1


print(ans)
# Judge status: Accepted
N,M=map(int,input().split())
A=list(map(int,input().split()))

ans=N-sum(A)

if ans<0: print(-1)
else: print(ans)
# Judge status: Accepted
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 28 22:05:17 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

#N = int(input())
K, N = map(int,input().split())
A = list(map(int,input().split()))
#A.append()
B = []
for i in range(N-1):
    B.append(A[i+1] - A[i])
B.append(A[0] + (K - A[-1]))

ans = K - max(B)

print(ans)
# Judge status: Accepted
n, k = map(int, input().split())
nums = [list(map(int, input().split())) for _ in range(n)]
nums.sort(key=lambda x: x[0])
cnt = 0
for a, b in nums:
    cnt += b
    if cnt >= k:
        print(a)
        break
# Judge status: Accepted
from sys import stdin 
input=stdin.readline
def main():
  n=int(input())
  l=tuple(tuple(map(int,input().split())) for i in range(n-1))
  for i in range(n):
    now=0
    for a,s,d in l[i:]:
      if now<=s:now=s+a
      else:now=(now+d-1)//d*d+a
    print(now)
if __name__=="__main__":
  main()
# Judge status: Accepted
import math
x,k,d=map(int,input().split())
count=int(math.sqrt(x*x)/d)
if k<=count:
    count=k
    k=2
else:
    k=k-count
ans=int(math.sqrt(x*x)-d*count)
if k%2==1:
    ans=math.sqrt((d-ans)**2)
print(int(ans))
# Judge status: Accepted
N = int(input())
L = [2, 1]

for i in range(1, N):
    L += [L[-1] + L[-2]]
print(L[N])
# Judge status: Accepted
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    a = list(map(int, readline().split()))

    odd = a[0::2]
    even = a[1::2]

    if n % 2 == 0:
        res = even[::-1] + odd
        print(*res)
    else:
        res = odd[::-1] + even
        print(*res)


if __name__ == '__main__':
    main()
# Judge status: Wrong Answer
N, K = map(int, input().split())
 
tmp = set()
 
for i in range(K):
  input()
  tmp |= set(input())
 
print(N - len(tmp) + 1)
# Judge status: Accepted
n = int(input())
w = [input() for i in range(n)]

if len(w) != len(set(w)):
    print("No")
    exit()

h, b = [], []
for i in range(n):
    if i != 0:
        h.append(w[i][0])
    if i != n-1:
        b.append(w[i][-1])

for i, j in zip(h, b):
    if i != j:
        print("No")
        exit()

print("Yes")
# Judge status: Accepted
n = int(input())
if n==3 or n==5 or n==7:
  print("YES")
else:
  print("NO")
# Judge status: Accepted
a,b=map(int,input().split())
if a >= 10 or b>=10:
    print(-1)
else:
    print(a*b)
# Judge status: Accepted
S = sorted(list(input()))
if S[0] == S[1] and S[1] != S[2] and S[2] == S[3]:
    print('Yes')
else:
    print('No')
# Judge status: Accepted
n=input()
i=list(input())
Wn=i.count("W")
print(Wn-i[(len(i)-Wn):].count("W"))
# Judge status: Wrong Answer
N,M = [int(i) for i in input().split()]
count = 0
if 2 * N <= M:
    count = N
    M -= 2 * N
    if M >= 4:
        count += M // 4
        print(count)
    else:
        print(count)
else:
    if N != 0:
        while N == 0 or M <= 1:
            count += 1
            N -= 1
            M -= 2
        print(count)
    else:
        print(count)
# Judge status: Accepted
N, M = map(int, input().split())

if N == 1 and M == 1:
  print(1)
elif N == 1 or M == 1:
  print(N*M-2)
else:
  print((N-2) * (M-2))
# Judge status: Accepted
K,X=map(int,input().split())
if K*500>=X:
    print('Yes')
else:
    print('No')
# Judge status: Accepted
s = input()
t = input()
counter = 0

for i in range(len(s)):
  if not s[i] == t[i]:
    counter += 1

print(counter)
# Judge status: Accepted
A=int(input())
B=int(input())
C=int(input())
D=int(input())
E=int(input())
count=0
lis=[[A,((A%10)-1)%10],[B,((B%10)-1)%10],[C,((C%10)-1)%10],[D,((D%10)-1)%10],[E,((E%10)-1)%10]]
lis.sort(key=lambda x:x[1], reverse=True)
#print(lis)

for i in range(len(lis)):
    if not(i==len(lis)-1):
        count=count+lis[i][0]+(10-(lis[i][0])%10)%10
    else:
        count=count+lis[i][0]
print(count)
# Judge status: Accepted
a, b, c = map(int, input().split())

if b-a == c-b:
    print("YES")
else:
    print("NO")
# Judge status: Accepted
s, w = map(int, input().split())
if s <= w:
  print('unsafe')
else:
  print('safe')
# Judge status: Wrong Answer
R = list()
L = list()
N = int(input())
R.append(0)
for i in range(N):
    k = int(input())
    L.append(k)
    if k == 0:
        R.append(len(L))
S = sum(L)
R.append(S)
for i in range(len(R) - 1):
    if sum(L[R[i] + 1 : R[i + 1]]) % 2 == 1:
        S = S - 1
print(S // 2)
# Judge status: Accepted
n,k,*a=map(int,open(0).read().split());print(min(r-l+min(abs(r),abs(l))for l,r in zip(a,a[k-1:])))
# Judge status: Wrong Answer
import bisect
n = int(input())
x = sorted(list(map(int,input().split())), reverse=True)
y = sorted(list(map(int,input().split())), reverse=True)
z = sorted(list(map(int,input().split())), reverse=True)
count = 0
for a in range(n):
  j = bisect.bisect_right(y,z[a])
  for b in range(j,n-1):
    k = bisect.bisect_right(x,y[b])
    count+=(n-k)
print(count)
# Judge status: Accepted
n, k = map(int, input().split())
s = input()

score = 0
paired = 0
for i in range(1, n):
    if s[i - 1] == s[i]:
        score += 1
    if s[i - 1] == s[0] and s[i] != s[0]:
        paired += 1

if k < paired:
    print(score + 2 * k)
else:
    print(n - 1)
# Judge status: Accepted
n = int(input())
B = list(map(int, input().split()))

ans = 0
for i in range(n):
    if i == 0:
        ans += B[0]
    elif i == n - 1:
        ans += B[n - 2]
    else:
        ans += min(B[i - 1], B[i])

print(ans)
# Judge status: Accepted
N=int(input())
A=[]
B=[]
for i in range(N):
  a,b=map(int,input().split())
  A.append(a)
  B.append(b)
  
A.sort()
B.sort()


if N%2==0:
  al=A[(N-1)//2]
  ar=A[N//2]
  bl=B[(N-1)//2]
  br=B[N//2]
  print(int(((br+bl)/2-(ar+al)/2)*2+1))
else:
  mini=A[(N-1)//2]
  maxi=B[(N-1)//2]
  print(maxi-mini+1)
# Judge status: Accepted
n = int(input())
h = list(map(int, input().split()))

dp = [0 for _ in range(n)]
dp[1] = abs(h[0] - h[1])
for i in range(2, n):
    dp[i] = min(
        dp[i - 2] + abs(h[i] - h[i - 2]),
        dp[i - 1] + abs(h[i] - h[i - 1]))
print(dp[-1])
# Judge status: Wrong Answer
s=input()
ans='AC'
if s[0]!='A':
  ans='WA'
elif s[2:-1].count('C')!=1:
  ans='WA'
if ans=='AC':
  s=s[1:]
  s=s[1:-1].replace('C','')+s[-1]
  if s.islower==False:
    ans='WA'
print(ans)
# Judge status: Wrong Answer
S = input()
result = 0
for i in range(int(len(S) / 2)):
    if S[i] != S[-i]:
        result += 1
print(result)
# Judge status: Wrong Answer
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
if max(L) > (sum(L)-max(L)):
    print('Yes')
else:
    print('No')
# Judge status: Wrong Answer
import math


# For taking integer inputs.
def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


N, x = invr()

a = inlist()
a = sorted(a)
c = 0
for i in a:
    if i <= x:
        c += 1
        x -= i
if x > 0 and c > 0:
    c -= 1
print(c)
# Judge status: Accepted
S=input()
if S=='ARC':
  print('ABC')
else:
  print('ARC')
# Judge status: Accepted
def main(X):
    cntS = 0; cntT = 0
    for x in X:
        if x == 'T' and cntS == 0:
            cntT += 1
        elif x == 'S':
            cntS += 1
        elif x == 'T' and cntS > 0:
            cntS -= 1
        else:
            pass
    return cntS + cntT
    
if __name__ == "__main__":
    X = list(input())
    ans = main(X)
    print(ans)
# Judge status: Accepted
n = int(input())
flag = 0
flagg = 1
for _ in range(n):
    a, b = map(int, input().split())
    if flagg:
        if a==b:
            flag+=1
        else:
            flag = 0
        if flag==3:
            print('Yes')
            flagg = 0
if flag<3:
    print('No')
# Judge status: Wrong Answer
N = int(input())

words = [chr(ord('a')+i) for i in range(10)]
dp = ['']
for i in range(1, N+1):
    ndp = []
    for w in dp:
        for k in range(i):
            ndp.append(w+words[k])
    dp = ndp
for l in dp:
    print(l)
# Judge status: Accepted
n,m=map(int,input().split())
l=list(map(int,input().split()))
d=dict()
for i in range(m):
  a,b=map(int,input().split())
  if b in d:
    d[b]+=a
  else:
    d[b]=a
x=sorted(d.keys())[::-1]
now=0
for y in x:
  now+=d[y]
  l+=[y]*min(n,d[y])
  if now>n:
    break
print(sum(sorted(l)[::-1][:n]))
# Judge status: Wrong Answer
o = input()
e = input()
p = ""
for o, e in zip(o, e):
  p = p + o + e
  
print(p if len(o) == len(e) else p + o[-1])
# Judge status: Wrong Answer
import heapq
n,k = map(int,input().split())
a = list(map(int,input().split()))
r = [0 for _ in range(max(a)+1)]
if len(set(a)) <= 2:
    print(0)
    exit()
for i in range(n):
    r[a[i]] += 1
r = list(map(lambda x:-x,r))
heapq.heapify(r)
print(n+(r[0]+r[1]))
# Judge status: Wrong Answer
X = int(input())
if X == 3 or X == 5 or X == 7:
    print("Yes")
else:
    print("No")
# Judge status: Wrong Answer
import bisect
N = int(input())
A = [0]
A += list(map(int, input().split()))
cnt = 0
L = [- 10 ** 6]
R = [- 10 ** 6]

for i in range(1, N + 1):
    L.append(i + A[i])
    R.append(i - A[i])
L.sort()
print(L)
print(R)
for j in range(1, N + 1):
    x = bisect.bisect_left(L, R[j])
    while R[j] == L[x]:
        cnt += 1
        x += 1
print(cnt)
# Judge status: Accepted
x,y=map(int,input().split())
if x%y==0:
    print(-1)
else:
    print(x*(y-1))
# Judge status: Accepted
N,K=map(int,input().split())
l=[list(map(int,input().split()))for i in range(N)]
l.sort(key=lambda x: x[0])
ans=0
for i in l:
    if K==0:
        break
    K-=min(K,i[1])
    ans=i[0]
print(ans)
# Judge status: Accepted
A, B, C = map(int, input().split())
if A == B == C:
    print(0 if A%2 else -1)
else:
    cnt = 0
    while A%2==0 and B%2==0 and C%2==0:
        A, B, C = (B+C)//2, (C+A)//2, (A+B)//2
        cnt += 1
    print(cnt)
# Judge status: Accepted
n=int(input())
a=[int(i) for i in input().split()]
a.sort(reverse=True)
#rint(sum(a[:-1]))

c=0
for i in range(n):
    if i == 0:
        c+=0
    elif i == 1:
        c+=a[0]
    else:
        c+=a[i//2]
print(c)
# Judge status: Accepted
n = int(input())
s = input()
work = s[1:].count("E")
temp = [work]
leader = s[0]
for c in s[1:]:
    if leader == "W":
        work += 1
    if c == "E":
        work -= 1
    temp.append(work)
    leader = c
print(min(temp))
# Judge status: Accepted
n=int(input())
a=list(input().split())
if n%2==0:  
  b=a[::2]
  a.pop(0)
  c=a[::2]
  c.reverse()
  print(' '.join(c+b))
else:
  b=a[::2]
  a.pop(0)
  c=a[::2]
  b.reverse()
  print(' '.join(b+c))
# Judge status: Accepted
A, B, C = map(int, input().split())

x = A * 10 + B + C
y = A + 10 * B + C
z = A + B + 10 * C
print(max(x, y, z))
# Judge status: Accepted
#!/usr/bin/env python

n = int(input())
x_plus_y = [0 for _ in range(n)]
x_minus_y = [0 for _ in range(n)]
for i in range(n):
    x, y = map(int, input().split())
    x_plus_y[i] = x+y 
    x_minus_y[i] = x-y 

ans = max(max(x_plus_y)-min(x_plus_y), max(x_minus_y)-min(x_minus_y))
print(ans)
# Judge status: Wrong Answer
n,w=map(int,input().split())
dp=[-1]*(w+1)
dp[0]=0
for _ in range(n):
  a,d=map(int,input().split())
  for b in range(w,a-1,-1):
    if dp[b-a]!=-1:
    	dp[b]=max(dp[b],d+dp[b-a])
print(dp[w])
# Judge status: Accepted
K = int(input())

print(K//2*(K-K//2))
# Judge status: Accepted
w,a,b = map(int, input().split())

if abs(a - b) <= w:
	print(0)
else:
	print(abs(a - b) - w)
# Judge status: Accepted
w,h,n=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(n)]
a,b,c,d=0,w,0,h
for x,y,A in l:
    if A==1:
        a=max(a,x)
    elif A==2:
        b=min(b,x)
    elif A==3:
        c=max(c,y)
    else:
        d=min(d,y)
if b-a>=0 and d-c>=0:
    print((b-a)*(d-c))
else:
    print(0)
# Judge status: Accepted
H, N= map(int, input().split())
A = list(map(int, input().split()))

if sum(A) >= H:
    print("Yes")
else :
    print("No")