Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

d = {}
N = int(input())

for _ in range(N):
    s = str(input())
    if s in d.keys():
        d[s] += 1
    else:
        d[s] = 1

ans = []

for key in d.keys():
    if d[key] == max(d.values()):
        ans.append(key)

ans = sorted(ans)

for _ in range(len(ans)):
    print(ans[_])

### FIXED CODE ###

N = int(input())
l = []

for _ in range(N):
    s = str(input())
    l.append(s)

import collections
c = collections.Counter(l)
c = c.most_common()

most_count = c[0][1]
c.sort()

for i in range(len(c)):
    if c[i][1] == most_count:
        print(c[i][0])
### BUGGY CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sat Jun 29 22:14:02 2019

@author: Yamazaki Kenichi
"""

N, K = map(int,input().split())
mod = 10**9+7

r = int(pow(N,1/2))

const = []
if N // r == r:
    s = r - 1
else:
    s = r

for i in range(1,r+1):
    const.append(N//i - N//(i+1))
#if r == int(pow(N,1/2)) :
#    const += [1]
const.reverse()
const = [1 for i in range(s)] + const

dp = [[]]
for c in const:
    try:
        dp[0].append(dp[0][-1] +c)
    except:
        dp[0].append(c)
dp += [[0 for i in range(len(const))] for k in range(K-1)]

for k in range(1,K):
    for i in range(len(const)):
        dp[k][i] = (dp[k][i-1] + dp[k-1][len(const)-i-1]%mod
                      * const[i])%mod
print(dp[-1][-1]%mod)


### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sat Jun 29 22:14:02 2019

@author: Yamazaki Kenichi
"""

N, K = map(int,input().split())
mod = 10**9+7

r = int(pow(N,1/2))

const = []
if N // r == r:
    s = r - 1
else:
    s = r

for i in range(1,r+1):
    const.append(N//i - N//(i+1))
#if r == int(pow(N,1/2)) :
#    const += [1]
const.reverse()
const = [1 for i in range(s)] + const

dp = [[]]
for c in const:
    try:
        dp[0].append(dp[0][-1] +c)
    except:
        dp[0].append(c)
dp += [[0 for i in range(len(const))] for k in range(K-1)]

for k in range(1,K):
    for i in range(len(const)):
        dp[k][i] = (dp[k][i-1] + dp[k-1][len(const)-i-1]%mod
                      * const[i])%mod
print(dp[-1][-1]%mod)
### BUGGY CODE ###

N = int(input())
num = []
for i in range(N+1):
  if i % 3 == 0 and i % 5 == 0 or i % 3 == 0 or i % 5 == 0:
    num.sort()
  else:
    num.append(i)
print(sum(num))

### FIXED CODE ###

N = int(input())
num = []
num2= []
for i in range(N+1):
  if i % 3 == 0 and i % 5 == 0:
    num.append(i)
  if i % 5 == 0:
    num.append(i)
  elif i % 3 == 0:
    num.append(i)
  else:
    num2.append(i)

print(sum(num2))
### BUGGY CODE ###

import sys
#import time
#import copy
from collections import deque, Counter, defaultdict
#from fractions import gcd
#import bisect
import heapq
import time

start = time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
q = ri()
lr = [rl() for _ in range(q)]

def Is_prime(n):
    if n<2:return False
    elif n==2:return True
    elif n>3 and n%2==0:return False
    for i in range(3, n):
        if i*i>n:
            break
        if n%i==0:return False
    return True

for j in lr:
    ans=0
    for i in range(j[0], j[1]+1):
        if i%2==0:continue
        if Is_prime(i) and Is_prime((i+1)//2):ans+=1
    print(ans)

### FIXED CODE ###

import sys
#import time
#import copy
from collections import deque, Counter, defaultdict
#from fractions import gcd
#import bisect
import heapq
import time

start = time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
q = ri()
lr = [rl() for _ in range(q)]

def Is_prime(n):
    if n<2:return False
    elif n==2:return True
    elif n>3 and n%2==0:return False
    for i in range(3, n):
        if i*i>n:
            break
        if n%i==0:return False
    return True

dp=[0]*(10**5+1)
for i in range(1,10**5+1):
    if i%2==0:
        dp[i]=dp[i-1]
    if Is_prime(i) and Is_prime((i+1)//2):
        dp[i]=dp[i-1]+1
    else:
        dp[i]=dp[i-1]

for j in lr:
    print(dp[j[1]]-dp[j[0]-1])
### BUGGY CODE ###

# import bisect
# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np
# import math

# n, k = map(int, input().split())
# a = list(map(int, input().split()))
s = input()
q = int(input())
rev = False
for i in range(q):
    que = input()
    if que[0] == '1':
        rev = not rev
    else:
        que = list(que.split())
        if (int(que[1])==1 and not rev) or (int(que[1])==2 and rev):
            s = que[2] + s
        else:
            s = s + que[2]

if rev: ans = s[::-1]
else: ans = s
print(ans)


### FIXED CODE ###

# import bisect
# import heapq
# from copy import deepcopy
from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np
# import math

# n, k = map(int, input().split())
# a = list(map(int, input().split()))
s = deque(input())
q = int(input())
rev = False
for i in range(q):
    que = input()
    if que[0] == '1':
        rev = not rev
    else:
        que = list(que.split())
        if (int(que[1])==1 and not rev) or (int(que[1])==2 and rev):
            s.appendleft(que[2])
        else:
            s.append(que[2])

if rev: ans = ''.join(reversed(s))
else: ans = ''.join(s)
print(ans)
### BUGGY CODE ###

N, M = map(int, raw_input().split(' '))
P = 1000000007

skip = []
for i in range(M):
    skip.append(int(raw_input()))


steps = [0] * (N + 2)
steps[0] = 1

for i in range(0, N):
    if i+1 not in skip:
        steps[i+1] += steps[i]
        steps[i+1] %= P

    if i+2 not in skip:
        steps[i+2] += steps[i]
        steps[i+1] %= P

print steps[N]


### FIXED CODE ###

N, M = map(int, raw_input().split(' '))
P = 1000000007

skip = []
for i in range(M):
    skip.append(int(raw_input()))

nums = set(range(N)) - set(skip)

steps = [0] * (N + 2)
steps[0] = 1

for i in nums:
    steps[i+1] = (steps[i+1] + steps[i]) % P
    steps[i+2] = (steps[i+2] + steps[i]) % P

print steps[N]
### BUGGY CODE ###

s = input()
t = input()
dp = [[0] * (len(t)+1) for _ in range(len(s)+1)]

for s_i in range(len(s)):
    for t_i in range(len(t)):
        if s[s_i] == t[t_i]:
            dp[s_i+1][t_i+1] = dp[s_i][t_i] + 1
        else:
            dp[s_i+1][t_i+1] = max(dp[s_i][t_i+1], dp[s_i+1][t_i])

ans = []
s_i = len(s)
t_i = len(t)
while s_i > 0 and t_i > 0:
    if dp[s_i][t_i] == dp[s_i][t_i-1] and dp[s_i-1][t_i-1] == dp[s_i][t_i]:
        s_i -= 1
        t_i -= 1
    elif dp[s_i][t_i] - dp[s_i-1][t_i-1] == 1:
        s_i -= 1
        t_i -= 1
        if s_i >= 0:
            ans.append(s[s_i])
    else: #dp[s_i][t_i] - dp[s_i-1][t_i] == 1:
        t_i -= 1
        if t_i >= 0:
            ans.append(t[t_i])

print("".join(ans[::-1]))

### FIXED CODE ###

s = input()
t = input()
dp = [[0] * (len(t)+1) for _ in range(len(s)+1)]

for s_i in range(len(s)):
    for t_i in range(len(t)):
        if s[s_i] == t[t_i]:
            dp[s_i+1][t_i+1] = dp[s_i][t_i] + 1
        else:
            dp[s_i+1][t_i+1] = max(dp[s_i][t_i+1], dp[s_i+1][t_i])

ans = []
s_i = len(s)
t_i = len(t)
while s_i > 0 and t_i > 0:
    if dp[s_i][t_i] == dp[s_i][t_i-1]:
        t_i -= 1
    elif dp[s_i][t_i] == dp[s_i-1][t_i]:
        s_i -= 1
    else: #dp[s_i][t_i]  dp[s_i-1][t_i] == 1:
        s_i -= 1
        t_i -= 1
        if t_i >= 0:
            ans.append(t[t_i])


print("".join(ans[::-1]))
### BUGGY CODE ###

# D - Sum of Divisors
N = int(input())
count = [1] * (N + 1)

for i in range(1, N + 1):
  for j in range(2, i + 1):
    if i % j == 0:
      count[i] += 1

ans = 0

for i in range(1, N + 1):
  ans += i * count[i]

print(ans)

### FIXED CODE ###

# D - Sum of Divisors
N = int(input())
ans = (1 + N) * N

for i in range(2, N//2 + 1):
	j = 2
	
	while True:
		if i * j <= N:
			
			ans += i * j
			j += 1
		else:
			break

print(ans - 1)
### BUGGY CODE ###


def func():
    N, K = [int(i) for i in input().split()]
    H = [int(i) for i in input().split()]
    
    dp = [0]*N
    for i in range(1, N):
        L = []
        for j in range(max(0, i-K), i):
            L.append(dp[j] + abs(H[i] - H[j]))
        dp[i] = min(L)

    print(dp[-1])
    
if __name__=="__main__":
    func()

### FIXED CODE ###

def func():
    N, K = [int(i) for i in input().split()]
    H = [int(i) for i in input().split()]
    
    dp = [0]*N
    for i in range(1, N):
        L = []
        for j in range(max(0, i-K), i):
            L.append(dp[j] + abs(H[i] - H[j]))
        dp[i] = min(L)

    print(dp[-1])
    
if __name__=="__main__":
    func()
### BUGGY CODE ###

n,q=map(int,input().split())
par=[-1 for i in range(n+1)]    #par<0なら自身が親で要素の個数、>0なら子で親の位置示す
p=[-1 for i in range(n+1)]
pp=[[] for i in range(n+1)]
p[1]=0
def root(a):
    if par[a]<0:
        return a
    else:
        return root(par[a])

def size(a):
    return -par[root(a)]

def connect(a,b):
    x,y=a,b
    a=root(a)
    b=root(b)
    if a==b:
        return False
    if size(a)<size(b):
        a,b=b,a
        x,y=y,x
    par[a]+=par[b]
    par[b]=a
    p[b]=x
    pp[x].append(b)
    
    return True
ll=[]
for i in range(n-1):
    a=[int(j) for j in input().split()]
    ll.append(a)
    if root(a[0])!=root(a[1]):
        connect(a[0],a[1])

l=[0 for i in range(n+1)]
for i in range(q):
    r=[int(j) for j in input().split()]
    l[r[0]]+=r[1]

from collections import deque
qu=deque([1])

i=0
while qu:
    qq=qu.popleft()
    l[qq]+=l[p[qq]]
    for j in pp[qq]:
        qu.append(j)


print(*l[1:])

### FIXED CODE ###

from collections import deque
n,q=map(int, input().split())
l=[[] for i in range(n+1)]
ans=[0]*(n+1)

for i in range(n-1):
    a,b=[int(j) for j in input().split()]
    l[a].append(b)
    l[b].append(a)
for i in range(q):
    p,x=[int(j) for j in input().split()]
    ans[p]+=x

que=deque()
que.append(1)
ll=[0]*(n+1)
while que:
    y=que.popleft()
    ll[y]=1
    for i in l[y]:
        if ll[i]==1:
            continue
        ans[i]+=ans[y]
        que.append(i)
print(*ans[1:])
### BUGGY CODE ###

N,X,Y = map(int,input().split())
cc = []
for i in range(1,N+1):
  for j in range(i+1,N+1):
    cc.append(min(j-i,abs(X-i)+1+abs(Y-j)))
for i in range(1,N):
  print(cc.count(i))

### FIXED CODE ###

N,X,Y = map(int,input().split())
cc = [0]*(N-1)
for i in range(1,N+1):
  for j in range(i+1,N+1):
    cc[min(j-i,abs(X-i)+1+abs(Y-j))-1] += 1
for i in cc:
  print(i)
### BUGGY CODE ###

import numpy as np

num = input()
num = int(num)

str = input()
list_in = list(map(int, str.split()))

count_arr = np.zeros((num+1), dtype=np.int32)

for item in list_in:
    count_arr[item] += 1

sum = 0
for i in range(num+1):
    tmp = count_arr[i]
    tmp = (tmp * (tmp -1))//2
    sum += tmp

for item in list_in:
    tmp = count_arr[item]
    result = sum - (tmp - 1)
    print(result)


### FIXED CODE ###

import numpy as np

num = input()
num = int(num)

str = input()
list_in = list(map(int, str.split()))

count_arr = [0]*num

for item in list_in:
    count_arr[item-1] += 1

sum = 0
for i in range(num):
    tmp = count_arr[i-1]
    tmp = (tmp * (tmp -1))//2
    sum += tmp

for item in list_in:
    tmp = count_arr[item-1]
    result = sum - (tmp - 1)
    print(result)
### BUGGY CODE ###

N = int(input())
cnt = 0
while N > 0:
    cnt += N % 10
    cnt %= 9
    N //= 10
ans = 'Yes' if cnt % 9 == 0 else 'No'
print(ans)


### FIXED CODE ###

N = input()
cnt = 0
for s in N:
    cnt += int(s)
    cnt %= 9
ans = 'Yes' if cnt % 9 == 0 else 'No'
print(ans)
### BUGGY CODE ###

from math import gcd
from functools import reduce
N = int(input())
*A, = map(int, input().split())

n = 1000_000

is_prime = [True]*(n+1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(n**0.5)+1):
    if not is_prime[i]:
        continue
    for j in range(i*2, n+1, i):
        is_prime[j] = False
primes = [i for i in range(n+1) if is_prime[i]]

for p in primes:
    flag = 0
    for i in range(N):
        if A[i] % p == 0:
            if flag:
                break
            flag = 1
    else:
        continue

    # not pairwise coprime
    gcd_set = reduce(gcd, A)
    if gcd_set == 1:
        print("setwise coprime")
    else:
        print('not coprime')
    exit()
print('pairwise coprime')

    


### FIXED CODE ###

from math import gcd
from functools import reduce
N = int(input())
*A, = map(int, input().split())

MAX_N = max(A)+5
primes = list(range(MAX_N+1))
for i in range(2, int(MAX_N**0.5)+1):
    if primes[i] < i:
        continue
    for j in range(2*i, MAX_N+1, i):
        primes[j] = i


def check():
    S = set()
    for a in A:
        T = set()
        while True:
            p = primes[a]
            if p != 1 and p in S:
                return False
            T.add(p)
            if p == a:
                break
            a //= p
        S |= T
    return True


if check() or all(a == 1 for a in A):
    print('pairwise coprime')
elif reduce(gcd, A) == 1:
    print('setwise coprime')
else:
    print('not coprime')
### BUGGY CODE ###

import bisect

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

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

BC = []

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])

BC.reverse()

for i in range(M):
    B, C = BC[i]
    in_idx = bisect.bisect_right(A,C)

    if B >= in_idx:
        A = [C]*in_idx + A[in_idx:]
        break
    else:
        A = A[B:in_idx] + [C]*B + A[in_idx:]

print(sum(A))

### FIXED CODE ###

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)
### BUGGY CODE ###

import math
import calendar
import fractions
import itertools

n = int(input())
a = list(map(int, input().split()))
b = sorted(set(a))[::-1]
ans = 1
cnt = 0
for i in b:
    if cnt == 2:
        break
    if a.count(i) < 2:
        continue
    if cnt == 1:
        ans *= i
        cnt += 1
        break
    if a.count(i) >= 4:
        ans = i ** 2
        cnt = 2
        break
    ans *= i
    cnt += 1

if cnt < 2:
    ans = 0
print(ans)

### FIXED CODE ###

import math
import calendar
import fractions
import itertools

n = int(input())
a = sorted(map(int, input().split()), reverse=True)
h = 0
w = 0
index = -1
for i in range(n - 3):
    if a[i] == a[i + 1]:
        h = a[i]
        index = i + 2
        break

for i in range(index, n - 1):
    if a[i] == a[i + 1]:
        w = a[i]
        break

print(h * w)
### BUGGY CODE ###

n = int(input())
s = list(input())
q = int(input())
for i in range(q):
    a,b,c = input().split()
    if a == '1':
        s[int(b)-1] = c
    else:
        b = int(b)
        c = int(c)
        print(len(set(s[b-1:c])))

### FIXED CODE ###

def update(i,x):
    i += d-1
    bit = ord(x)-97
    seg[i] = 0 | (1<<bit)
    while i > 0:
        i = (i-1)//2
        seg[i] = seg[i*2+1]|seg[i*2+2]
def find(a,b,k,l,r):
    if r <= a or b <= l:
        return 0
    if a <= l and r <= b:
        return seg[k]
    c1 = find(a,b,2*k+1,l,(l+r)//2)
    c2 = find(a,b,2*k+2,(l+r)//2,r)
    return c1|c2
n = int(input())
s = input()
q = int(input())
d = 1
while d < n:
    d *= 2
seg = [0]*(2*d-1)
for i in range(n):
    update(i,s[i])
for i in range(q):
    type,a,b = input().split()
    if type == "1":
        update(int(a)-1,b)
    else:
        part = find(int(a)-1,int(b),0,0,d)
        print(bin(part).count("1"))
### BUGGY CODE ###

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

count = 1
for i in range(1, N):
  Pi = sorted(P[:i+1])
  if Pi[0] >= P[i]:
    count += 1

print(count)


### FIXED CODE ###

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

minimum = N + 1
ans = 0

for i in range(N):
  if minimum >= P[i]:
    ans += 1
    minimum = P[i]

print(ans)
### BUGGY CODE ###

import sys

import numpy as np

readline = sys.stdin.buffer.readline


def main():
    N, K = map(int, readline().split())
    p = np.array(readline().split(), dtype=np.int32)
    v = np.ones(K)
    q = np.convolve(p, v, mode='vaild')

    print((np.max(q) + K) / 2)


main()


### FIXED CODE ###

import sys

import numpy as np

readline = sys.stdin.buffer.readline


def moving_average(a, n=3) :
    ret = np.cumsum(a, dtype=float)
    ret[n:] = ret[n:] - ret[:-n]
    return ret[n - 1:]

def main():
    N, K = map(int, readline().split())
    p = np.array(readline().split(), dtype=np.int32)
    q = moving_average(p, K)

    print((np.max(q) + K) / 2)


main()
### BUGGY CODE ###

import numpy as np

def LCS():
  X = np.array(list(input()),dtype = str)
  Y = np.array(list(input()),dtype = str)
  
  dp = np.zeros([len(Y) + 1,len(X) + 1],dtype = int)
  for y in range(len(Y)):
    for x in range(len(X)):
      if Y[y] == X[x]:
        #Xのi文字目とYのj文字目が一致した時のみ値が更新される
        #文字列が一致する直線、i - 1,j - 1目までの最大一致数に、1を足す
        dp[y + 1][x + 1] = dp[y][x] + 1
        
      else:
        dp[y + 1][x + 1] = max(dp[y][x + 1],dp[y + 1][x])
        
    
  x = len(X)
  y = len(Y)
  ans = ""
  
  while x > 0 and y > 0:
    if dp[y][x] == dp[y - 1][x]:
      y -= 1
    elif dp[y][x] == dp[y][x - 1]:
      x -= 1
    else:
      ans = Y[y - 1] + ans
      y,x = y - 1,x - 1
  
  print(ans)
  
LCS()


### FIXED CODE ###

import numpy as np
#文字列をASCII codeのリストで管理
S = np.array([ord(i) - 97 for i in input()], dtype=int)
T = np.array([ord(i) - 97 for i in input()], dtype=int)
ls = len(S)
lt = len(T)
#S[i] == T[j]であれば、i * j行列の[i,j]成分が1となる行列
equal = np.equal.outer(S,T).astype(int)
#print(equal)
 
dp = [np.zeros(lt + 1,dtype = int)]
#print(dp)
for i in range(ls):
  #追加された最後のdp列をx,newDPとする
  x = dp[-1].copy()
  newDP = x.copy()
  #一致しているところで1を足し、そうでないところはそのまま
  newDP[1:] = np.maximum(x[:-1] + equal[i],newDP[1:])
  #dp[i] = i以前のdpの最大値に更新
  np.maximum.accumulate(newDP, out = newDP)
  #最大共通部分を計算するだけなら一列のdp列を使い回せば良いが
  #文字列の復元のために、dp列を追加する
  dp.append(newDP)
  #print(dp)
 
 
ans = ""
i, j = ls, lt
#数列の復元と文字列への復元
while i > 0 and j > 0:
  if S[i - 1] == T[j - 1]:
    ans += chr(S[i - 1] + 97)
    i -= 1
    j -= 1
    continue
  if dp[i][j] == dp[i - 1][j]:
    i -= 1
  else:
    j -= 1

#リバース
print(ans[::-1])
### BUGGY CODE ###

import math
mod = 10**9+7
n,m = map(int,raw_input().split())

if abs(n-m) > 1:
	print 0
	quit()

if max(n,m) > min(n,m):
	print math.factorial(max(n,m)) % mod * math.factorial(min(n,m)) % mod

else :
	print (((math.factorial(n))%mod)**2 * 2)%mod

### FIXED CODE ###


mod = 10**9+7
n,m = map(int,raw_input().split())
def factorial(x):
	retval = 1
	for i in range(2,x+1):
		retval = (retval * i)% mod
	return retval

if abs(n-m) > 1:
	print 0
	quit()

if max(n,m) > min(n,m):
	print (factorial(max(n,m)) * factorial(min(n,m))) % mod

else :
	print ((factorial(n)**2)%mod * 2)%mod
### BUGGY CODE ###

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

minimum_cost = [float('inf') for i in range(N)]
for i in range(min(K+1, N)):
    minimum_cost[i] = abs(H[i] - H[0])

for i in range(2, N):
    for j in range(K+1):
        if i-j < 0:
            break
        temp = minimum_cost[i-j] + abs(H[i]-H[i-j])
        minimum_cost[i] = min(temp, minimum_cost[i])

print(minimum_cost[N-1])


### FIXED CODE ###

import numpy as np

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

minimum_cost = np.zeros(N, dtype=int)
H = np.array(H)

for i in range(1, N):
    start = max(0, i-K)
    minimum_cost[i] = min(minimum_cost[start:i]
                          + np.abs(H[i] - H[start:i]))

print(minimum_cost[-1])
### BUGGY CODE ###

import sys
readline = sys.stdin.readline

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

a = [0]
b = [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])

max = 0
for i in range(N+1):
  t = a[i]
  if t > K:
    break
  for j in range(M+1):
    if t + b[j] > K:
      break
    if i + j > max:
      max = i + j
print(max)

### FIXED CODE ###

import sys
readline = sys.stdin.readline

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

a = [0]
b = [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])

max = 0
j = M
for i in range(N+1):
  t = K - a[i]
  if t < 0:
    break
  while b[j] > t:
    j -= 1
  if i + j > max:
    max = i + j
print(max)
### BUGGY CODE ###

import sys
n = int(sys.stdin.readline().strip())
ar = [int(x) for x in sys.stdin.readline().split()]
stop = False

def ok(x, y):
	if x == -1: return True
	return (x*y)%4 == 0

def find(ctr, prev, mask, app):
	global stop
	if ctr == n:
		stop = True
	if stop == True: return
	for i in range(n):
		if mask[i] and ok(prev, ar[i]):
			mask[i] = False
			find(ctr+1, ar[i], mask, app + [ar[i]])
			mask[i] = True

find(0, -1, [True]*n, [])
print "Yes" if stop else "No"

### FIXED CODE ###

import sys
n = int(sys.stdin.readline().strip())
ar = [int(x) for x in sys.stdin.readline().split()]
f4, odd, even = 0, 0, 0
for num in ar:
	if num%4==0:
		f4 +=1
	elif num%2==0:
		even+=1
	else:
		odd+=1
if odd-f4 == 0:
	print "Yes"
elif odd-f4 <= 1 and even == 0:
	print "Yes"
else:
	print "No"
### BUGGY CODE ###

import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

n,q = m()

a = l()
a.sort()
d = []
for i in range(q):
	b,c = m()
	if c > a[0]:
		d.append([b,c])
hq.heapify(a)


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

for i in range(len(d)):
	kk = bs.bisect_left(a,d[i][1])
	if kk == 0:
		break
	else:
		kkk = min(d[i][0],kk)
		for j in range(kkk):
			hq.heappushpop(a,d[i][1]);
	a.sort()

print(sum(a))






### FIXED CODE ###

import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

n,q = m()

a = l()
a.sort()
d = []
su = 0
co = 0
for i in range(q):
	b,c = m()
	if c > a[0]:
		d.append([b,c])
hq.heapify(a)
d.sort(reverse = True,key = lambda x:x[1])

for i in range(len(d)):
	kk = bs.bisect_left(a,d[i][1])
	if kk == 0:
		break
	else:
		if co + d[i][0] >= kk+1:
			su += d[i][1] * (kk-co)
			co += kk-co
			break
		else:
			su += d[i][0] * d[i][1]
			co += d[i][0]

			
su +=sum(a[co:])
print(su)
### BUGGY CODE ###

from collections import deque
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
sharps = []
for h, line in enumerate(A):
    for w, p in enumerate(line):
        if p == '#':
            A[h][w] = 1
            sharps.append([h, w, 0])
        else:
            A[h][w] = 0

q = deque(sharps)
while q:
    i, j, score = q.popleft()
    for mi, mj in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        ni = i + mi
        nj = j + mj
        if ni < 0 or H <= ni or nj < 0 or W <= nj or A[ni][nj] == 1:
            continue
        A[ni][nj] = 1
        q.append([ni, nj, score+1])
print(score)

### FIXED CODE ###

from collections import deque
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
sharps = []
for h, line in enumerate(A):
    sharps += [[h, w, 0] for w, p in enumerate(line) if p == '#']

q = deque(sharps)
while q:
    i, j, score = q.popleft()
    for mi, mj in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
        ni = i + mi
        nj = j + mj
        if ni < 0 or H <= ni or nj < 0 or W <= nj or A[ni][nj] == '#':
            continue
        A[ni][nj] = '#'
        q.append([ni, nj, score+1])
print(score)