Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

C = list(set(A))

B = len(C)

x = B - K #種類の差

ans = 0

figure_list = [0] * B

if x<=0:
    print('0')
else:
    for i in range(B):
        stock = int(C[i])
        counter = 0
        for ii in range(N):
            if stock == A[ii]:
                counter += 1
        figure_list[i] = counter

    figure_list = sorted(figure_list)

    for i in range(B-K):
        ans += figure_list[i]

    print(ans)    





Fixed submission:

#【註】N 個のボールと K 種類の整数
N, K = map(int, input().split())

#【註】ボールに書かれている数 A_1...A_N
A = list(map(int, input().split()))

#【註】何種類あるか？を set で取得
C = list(set(A))

# 数字の種類数
B = len(C) 
# 元々の種類と目的の種類の差
x = B - K

#-----
#【註】「文字：回数」という辞書を使って、ループを最小限にします。
d = {}

#【解説】A のそれぞれの要素について
for i in A:
    #【解説】もし辞書に既に登録されていれば、その値（出現回数）に 1 を足す
    if i in d:
        d[i] += 1
    #【解説】そうでなければ、辞書にキーを登録して値（出現回数）を 1 にする
    else:
        d[i] = 1

#【解説】辞書を値でソートすると、出現回数が小さい順に並ぶ
d = sorted(d.items(), key=lambda x:x[1])

ans = 0
count = 0

if x<=0:
    print('0')
else:
    #【解説】辞書からキーと値を取り出し
    for key, value in d:
        #【解説】取り出した玉の数に value を足す
        ans = ans + value
        #【解説】何種類取り出したか、の値に 1 を足す
        count = count + 1

        #【解説】もし x 種類取り出しが完了したら ans を出力
        if count == x:
            print(ans)
            break
Buggy python submission (verdict: Runtime Error):
def solve(): 

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

    i = 0
    result = 0

    while i < N:
        while i+1 < n and A[i] == A[i+1]:
            i += 1
        if i+1 < N and A[i] < A[i+1]:
            while i+1 < n and A[i] <= A[i+1]:
                i += 1
        elif i+1 < N and A[i] > A[i+1]:
            while i+1 < n and A[i] >= A[i+1]:
                i += 1
        
        result += 1
        i += 1

    print(result)    

if __name__ == "__main__":
    solve()


Fixed submission:
def solve(): 

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

    i = 0
    result = 0

    while i < N:
        while i+1 < N and A[i] == A[i+1]:
            i += 1
        if i+1 < N and A[i] < A[i+1]:
            while i+1 < N and A[i] <= A[i+1]:
                i += 1
        elif i+1 < N and A[i] > A[i+1]:
            while i+1 < N and A[i] >= A[i+1]:
                i += 1
        
        result += 1
        i += 1

    print(result)    

if __name__ == "__main__":
    solve()

Buggy python submission (verdict: Runtime Error):
def solve(): 

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

    i = 0
    result = 0

    while i < n:
        while i+1 < n and A[i] == A[i+1]:
            i += 1
        if i+1 < n and A[i] < A[i+1]:
            while i+1 < n and A[i] <= A[i+1]:
                i += 1
        elif i+1 < n and A[i] > A[i+1]:
            while i+1 < n and A[i] >= A[i+1]:
                i += 1
        
        result += 1
        i += 1

    print(result)    

if __name__ == "__main__":
    solve()


Fixed submission:
def solve(): 

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

    i = 0
    result = 0

    while i < N:
        while i+1 < N and A[i] == A[i+1]:
            i += 1
        if i+1 < N and A[i] < A[i+1]:
            while i+1 < N and A[i] <= A[i+1]:
                i += 1
        elif i+1 < N and A[i] > A[i+1]:
            while i+1 < N and A[i] >= A[i+1]:
                i += 1
        
        result += 1
        i += 1

    print(result)    

if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

ans1 = 0

A = '1'
B = str(N)

if len(A)>=len(B):
    ans = len(A)
else:
    ans = len(B)
    
for i in range(2,N+1):
    A = i
    if N%A==0:
        B = int(N / A)
        A = str(A)
        B = str(B)
#        print(A, B)
        if len(A)>=len(B):
            ans1 = len(A)
        else:
            ans1 = len(B)
        if ans1 <= ans:
            ans = ans1
    if len(str(A))==len(str(B)):
        break
print(ans)


Fixed submission:
import math

N = int(input())

ans1 = 0

A = '1'
B = str(N)

if len(A)>=len(B):
    ans = len(A)
else:
    ans = len(B)
    
for i in range(2,int(math.sqrt(N)+1)):
    A = i
    if N%A==0:
        B = int(N / A)
        A = str(A)
        B = str(B)
#        print(A, B)
        if len(A)>=len(B):
            ans1 = len(A)
        else:
            ans1 = len(B)
        if ans1 <= ans:
            ans = ans1
    if len(str(A))==len(str(B)):
        break
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
K, S = map(int, input().split())

counter = 0

for i in range(K+1):
    x = i
    for ii in range(K+1):
        y = ii
        z = S - i - ii
        if 0<=z and z<=K and (x+y+z)==S:
            counter += 1

print(counter)            


Fixed submission:
K, S = map(int, input().split())

counter = 0

for i in range(K+1):
    x = i
    for ii in range(K+1):
        y = ii
        z = S - i - ii
        if 0<=z and z<=K:
            counter += 1

print(counter)

Buggy python submission (verdict: Time Limit Exceeded):
K, S = map(int, input().split())

counter = 0

for i in range(K+1):
    x = i
    for ii in range(K+1):
        y = ii
        z = S - i - ii
        if 0<=z and z<=K and (x+y+z)==S:
            counter += 1

print(counter)            


Fixed submission:
K, S = map(int, input().split())

counter = 0

for i in range(K+1):
    x = i
    for ii in range(K+1):
        y = ii
        z = S - i - ii
        if 0<=z and z<=K:
            counter += 1

print(counter)
Buggy python submission (verdict: Runtime Error):
weat= input()
seaS = 'S'
seaR = 'R'
a = 0
b = 0
c = 0
imput = 0
for alp in wea:
  if a == 0:
    if alp == seaS:
      a = 1
    if alp == seaR:
      a = 2
  if b == 0 and a != 0:
    if alp == seaS:
      b = 1
    if alp == seaR:
      b = 2
  if c == 0 and a != 0 and b != 0:
    if alp == seaS:
      c = 1
    if alp == seaR:
      c = 2
if a or b or c == 2:
  imput = 1
if a and b == 2 or b and c == 2:
  imput = 2
if a and b and c == 3:
  imput = 3

print(imput)


Fixed submission:
weat= input()
imput = 0
if weat == 'SSS':
  imput = 0
if weat == 'SSR' or weat == 'RSS' or weat == 'SRS' or weat == 'RSR':
  imput = 1
if weat == 'SRR' or weat == 'RRS':
  imput = 2
if weat == 'RRR':
  imput = 3

print(imput)
Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
mo = 0
for i in range(1, k + 1):
    mo = mo * 10 + 7
    l = mo % k
    if l == 0:
        print(i)
        break
else:
    print(-1)

Fixed submission:
k = int(input())
mo = 7
mo = mo % k
for i in range(1 , k + 1):
  if mo == 0:
    print(i)
    break
  if mo != 0:
    mo = (mo * 10 + 7) % k
else:
  print(-1)

Buggy python submission (verdict: Runtime Error):
k = int(input())
mo = 0
for i in range(1, k + 1):
    mo = mo * 10 + 7
    k = mo % k
    if mod == 0:
        print(i)
        break
else:
    print(-1)

Fixed submission:
k = int(input())
mo = 7
mo = mo % k
for i in range(1 , k + 1):
  if mo == 0:
    print(i)
    break
  if mo != 0:
    mo = (mo * 10 + 7) % k
else:
  print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A1 = {}
A2 = []
for i in range(N):
  s = input()
  if s not in A2:
    A1[s] = 1
    A2.append(s)
  else:
    A1[s] += 1
sai = max(A1.values())
A2.sort()
for j in A2:
  if A1[j] == sai:
    print(j)
  

  

Fixed submission:
N = int(input())
A1 = {}
for i in range(N):
  s = input()
  if s in A1:
    A1[s] += 1
  else:
    A1[s] = 1
sai = max(A1.values())
for j in sorted(k for k in A1 if A1[k] == sai):
  print(j)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
con = 0
con2 = 0
c = 1
A1 = []
win = []
for a in range(1,N+1):
  b = str(input())
  A1.append(b)
A2 = list(set(A1))
A1.sort()
A2.sort()
for i in range(len(A2)):
  for j in range(len(A1)):
    if A2[i] == A1[j]:
      con += 1
  if con2 == con:
    win.append(A2[i])
  if con2 < con:
    con2 = con
    win = []
    c = A2[i]
    win.append(c)
  con = 0
win.sort()
for k in range(len(win)):
  print(win[k])
  
      

Fixed submission:
N = int(input())
A1 = {}
for i in range(N):
  s = input()
  if s in A1:
    A1[s] += 1
  else:
    A1[s] = 1
sai = max(A1.values())
for j in sorted(k for k in A1 if A1[k] == sai):
  print(j)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
P = list(map(int,input().split()))
L = [N + 1]
con = 0
for i in range(N):
  if min(L) > P[i]:
    con += 1
  L.append(P[i])
print(con)

Fixed submission:
N = int(input())
P = list(map(int, input().split()))
c = 10 ** 20
con = 0
for i in range(N):
  if c > P[i]:
    con += 1
    c = P[i]
print(con)
Buggy python submission (verdict: Runtime Error):
s = str(input())
K = int(input())
L = []
M = []
con = 0
for i in s:
  L.append(i)
L = list(set(L))
L.sort()
for j in range(len(s)):
    if s[j] == L[0]:
      for k in range(1,6):
        if j + k >=len(s):
          M.append(s[j:])
          break
        else:
          M.append(s[j:j+k])

for l in range(len(s)):
  if s[l] == L[1]:
    for m in range(1,6):
      if l + m >= len(s):
        M.append(s[l:])
        break
      else:
        M.append(s[l:l+m]) 
if len(M) < K:
  for n in range(len(s)):
    if s[n] == L[2]:
      for o in range(1,6):
        if n + o >=len(s):
          M.append(s[n:])
          break
        else:
          M.append(s[n:n+o])
M = list(set(M))
M.sort()
print(M)
print(M[K - 1])  
          


Fixed submission:
s = str(input())
K = int(input())
L = []
M = []
con = 0
for i in s:
  L.append(i)
L = list(set(L))
L.sort()
for j in range(len(s)):
    if s[j] == L[0]:
      for k in range(1,6):
        if j + k >=len(s):
          M.append(s[j:])
          break
        else:
          M.append(s[j:j+k])
if len(L) != 1:
  for l in range(len(s)):
    if s[l] == L[1]:
      for m in range(1,6):
        if l + m >= len(s):
          M.append(s[l:])
          break
        else:
          M.append(s[l:l+m]) 
if len(M) < K:
  for n in range(len(s)):
    if s[n] == L[2]:
      for o in range(1,6):
        if n + o >=len(s):
          M.append(s[n:])
          break
        else:
          M.append(s[n:n+o])
M = list(set(M))
M.sort()
print(M[K - 1])
Buggy python submission (verdict: Runtime Error):
A,B,K = map(int,input().split())
L = list(range(A,B + 1))
for i in range(len(L)):
  if i < K or len(L) - K <= i:
    print(L[i])


Fixed submission:
A,B,K = map(int,input().split())
if B - A >= 2 * K :
  for i in range(K):
    print(A + i)
  for j in reversed(range(K)):
    print(B - j)
else:
  for _ in range(A,B + 1):
    print(_)
Buggy python submission (verdict: Runtime Error):
A,B = map(int, input().split())
if A > B:
  print('GREATER')
if A < B:
  print('LESS')
if A == B:
  print('EQUAL')

Fixed submission:
A = int(input())
B = int(input())
if A > B:
  print('GREATER')
if A < B:
  print('LESS')
if A == B:
  print('EQUAL')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = list(map(int ,input().split()))
con = 0
for i in range(N):
  for j in range(i + 1 , N):
    if s[i] == j + 1 and s[j] == i + 1:
      con += 1
print(con)

Fixed submission:
N = int(input())
s = list(map(int ,input().split()))
con = 0
for i in range(N):
  if s[s[i] - 1] == i + 1:
    con += 1
print(con // 2)
Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
cnt = 0
if k % 2 == 0:
    print("-1")
else:
    n = ""
    while True:
        cnt += 1
        n = n + "7"
        if int(n) % k == 0:
            break
        else:
            pass
    print(cnt)

Fixed submission:
K = int(input())
a = [7%K]
for i in range(1, K):
  a.append((a[i-1]*10+7)%K)

isCorrect = False
for i in range(K):
  if a[i] == 0:
    print(i+1)
    isCorrect = True
    break
if not isCorrect:
  print("-1")
Buggy python submission (verdict: Runtime Error):
S=input()
if S=="AAA"or"BBB":
    print("No")
  else:
    print("Yes")
        



Fixed submission:
S=input()
if S=="AAA" or S=="BBB":
    print("No")
else:
    print("Yes")
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=int(input())
B=int(input())
a=int(A+B)
b=int(N/a)
c=int(N%a)
if c<A:
  print(int(b*A+c))
else:
  print(int(b*A+A))

  

Fixed submission:
N,A,B=(int(x) for x in input().split())
a=int(A+B)
b=int(N/a)
c=int(N%a)
if c<A:
  print(int(b*A+c))
else:
  print(int(b*A+A))

Buggy python submission (verdict: Runtime Error):
N=input()
A=input()
B=input()
a=int(A+B)
b=int(N/a)
c=int(N%a)
if c<A:
  print(int(b*A+c))
else:
  print(int(b*A+A))


Fixed submission:
N,A,B=(int(x) for x in input().split())
a=int(A+B)
b=int(N/a)
c=int(N%a)
if c<A:
  print(int(b*A+c))
else:
  print(int(b*A+A))

Buggy python submission (verdict: Runtime Error):
N=input()
A=input()
B=input()
a=str(A+B)
b=str(N/a)
c=str(N%a)
if c<A:
  print(str(b*A+c))
else:
  print(str(b*A+A))


Fixed submission:
N,A,B=(int(x) for x in input().split())
a=int(A+B)
b=int(N/a)
c=int(N%a)
if c<A:
  print(int(b*A+c))
else:
  print(int(b*A+A))

Buggy python submission (verdict: Runtime Error):
N=input()
A=input()
B=input()
a=A+B
b=str(N/a)
c=str(N%a)
if c<A:
  print(b*A+c)
else:
  print(b*A+A)


Fixed submission:
N,A,B=(int(x) for x in input().split())
a=int(A+B)
b=int(N/a)
c=int(N%a)
if c<A:
  print(int(b*A+c))
else:
  print(int(b*A+A))

Buggy python submission (verdict: Runtime Error):
N=input()
A=input()
B=input()
a=A+B
b=N/a
c=N%a
if c<A:
  print(b*A+c)
else:
  print(b*A+A)

Fixed submission:
N,A,B=(int(x) for x in input().split())
a=int(A+B)
b=int(N/a)
c=int(N%a)
if c<A:
  print(int(b*A+c))
else:
  print(int(b*A+A))
Buggy python submission (verdict: Runtime Error):
k=input()
for i in range(k):
  print("ACL",end="")

Fixed submission:
k = int(input())
for i in range(k):
  print("ACL",end="")
Buggy python submission (verdict: Runtime Error):
print([1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51][int(input()-1)]

Fixed submission:
print([1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51][int(input())-1])
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for c in range(2,N):
    cc=L[c]
    for b in range(1,c):
        bb=L[b]
        if cc>=bb+L[b-1]: continue
        l=-1
        u=b-1
        while u-l>1:
            a=(l+u+1)//2
            aa=L[a]
            if aa+bb>cc: u=a
            else: l=a
        ans+=(b-u)
print(ans)

Fixed submission:
N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for c in range(2,N):
    cc=L[c]
    for b in range(1,c):
        bb=L[b]
        if cc>=bb+L[b-1]: continue
        l=-1
        u=b-1
        while u-l>1:
            a=(l+u+1)//2
            aa=L[a]
            if aa+bb>cc: u=a
            else: l=a
        ans+=(b-u)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n=int(input())
dic=defaultdict(int)
for _ in range(n):
	s=list(input())
	s.sort()
	s=''.join(s)
	dic[s]+=1
	ans=0
	for (k,v) in dic.items():
		ans += v*(v-1)//2
print(ans)

Fixed submission:
from collections import defaultdict
n=int(input())
dic=defaultdict(int)
for _ in range(n):
	s=list(input())
	s.sort()
	s=''.join(s)
	dic[s]+=1
ans=0
for (k,v) in dic.items():
	ans += v*(v-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n=int(input())
dic=defaultdict(int)
for _ in range(n):
	s=list(input())
	s.sort()
	s=''.join(s)
	if s not in dic.keys():
		dic[s]=1
	else:
		dic[s]+=1
	ans=0
	for (k,v) in dic.items():
		ans += v*(v-1)//2
print(ans)

Fixed submission:
from collections import defaultdict
n=int(input())
dic=defaultdict(int)
for _ in range(n):
	s=list(input())
	s.sort()
	s=''.join(s)
	dic[s]+=1
ans=0
for (k,v) in dic.items():
	ans += v*(v-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
dic={}
for _ in range(n):
	s=list(input())
	s.sort()
	s=''.join(s)
	if s not in dic.keys():
		dic[s]=1
	else:
		dic[s]+=1
	ans=0
	for (k,v) in dic.items():
		ans += v*(v-1)//2
print(ans)

Fixed submission:
from collections import defaultdict
n=int(input())
dic=defaultdict(int)
for _ in range(n):
	s=list(input())
	s.sort()
	s=''.join(s)
	dic[s]+=1
ans=0
for (k,v) in dic.items():
	ans += v*(v-1)//2
print(ans)
Buggy python submission (verdict: Runtime Error):
from math import gcd, sqrt
from sys import exit
N=int(input())
A=list(map(int,input().split()))

if len(A)==2:
    print(max(A))
    exit()

g=gcd(A[0],A[-1])

p=[]
for i in range(1,int(sqrt(g)+1)):
    if g%i==0:
        if i*i==g: p.append(i)
        else: p+=[i,g//i]
        
def ok(n):
    f=False
    for a in A[1:-1]:
        if a%n:
            if f: return False
            f=True
    return True
    
def GCD(L):
    if len(L)==1: return L[0]
    ans=gcd(L[0],L[1])
    if len(L)>2:
        for l in L[2:]:
            ans=gcd(ans,l)
    return ans

p.sort(reverse=True)
for num in p:
    if ok(num):
        ans=num
        break
p1=GCD(A[1:-1])
print(max([ans,gcd(p1,A[0]),gcd(p1,A[-1])]))


Fixed submission:
from math import sqrt
from sys import exit
N=int(input())
A=list(map(int,input().split()))

if len(A)==2:
    print(max(A))
    exit()

def gcd(a,b):
    if a<b: a,b=b,a
    if a%b==0: return b
    return gcd(b,a%b)
    
g=gcd(A[0],A[-1])

p=[]

for i in range(1,int(sqrt(g)+1)):
    if g%i==0:
        if i*i==g: p.append(i)
        else: p+=[i,g//i]
        
def ok(n):
    f=False
    for a in A[1:-1]:
        if a%n:
            if f: return False
            f=True
    return True
    
def GCD(L):
    if len(L)==1: return L[0]
    ans=gcd(L[0],L[1])
    if len(L)>2:
        for l in L[2:]:
            ans=gcd(ans,l)
    return ans

p.sort(reverse=True)
for num in p:
    if ok(num):
        ans=num
        break
p1=GCD(A[1:-1])
print(max([ans,gcd(p1,A[0]),gcd(p1,A[-1])]))
Buggy python submission (verdict: Runtime Error):
from math import gcd
a,b=map(int,input().split())
print(gcd(a,b))


Fixed submission:
a,b=map(int,input().split())
if a>b:
    ans=a
    a-=1
else:
    ans=b
    b-=1
print(ans+max(a,b))

Buggy python submission (verdict: Runtime Error):
from math import gcd,ceil
a,b=map(int,input().split())
print(gcd(a,b))


Fixed submission:
a,b=map(int,input().split())
if a>b:
    ans=a
    a-=1
else:
    ans=b
    b-=1
print(ans+max(a,b))

Buggy python submission (verdict: Runtime Error):
from math import gcd,ceil
a,b=map(int,input().split())
print(gcd(a,b))

Fixed submission:
a,b=map(int,input().split())
if a>b:
    ans=a
    a-=1
else:
    ans=b
    b-=1
print(ans+max(a,b))
Buggy python submission (verdict: Time Limit Exceeded):
x,y,z,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
AB=[]
for a in A:
    for b in B:
        AB.append(a+b)
AB=sorted(AB,reverse=True)[:k]
ABC=[]
for ab in AB:
    for c in C:
        ABC.append(ab+c)
ABC.sort(reverse=True)
for abc in ABC[:k]:
    print(abc)

Fixed submission:
x,y,z,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
AB=[]
for a in A:
    for b in B:
        AB.append(a+b)
AB=sorted(AB,reverse=True)[:k]
ABC=[]
for ab in AB:
    for c in C:
        ABC.append(ab+c)
ABC.sort(reverse=True)
for abc in ABC[:k]:
    print(abc)

Buggy python submission (verdict: Time Limit Exceeded):
x,y,z,k=map(int,input().split())
A=sorted(list(map(int,input().split())),reverse=True)
B=sorted(list(map(int,input().split())),reverse=True)
C=sorted(list(map(int,input().split())),reverse=True)
a=1
b=1
c=1
A.append(-10**11)
B.append(-10**11)
C.append(-10**11)
A.insert(0,-10**11)
B.insert(0,-10**11)
C.insert(0,-10**11)
print(A[a]+B[b]+C[c])

memo=[[[False]*(z+2) for _ in range(y+2)] for _ in range(x+2)]
memo[a][b][c]=True

for _ in range(k-1):
    keepv=0
    for a2 in range(1,x+2):
        for b2 in range(1,y+2):
            for c2 in range(1,z+2):
                if not memo[a2][b2][c2]:
                    if A[a2]+B[b2]+C[c2]>keepv:
                        keepv=A[a2]+B[b2]+C[c2]
                        keepa=a2
                        keepb=b2
                        keepc=c2
    a=keepa
    b=keepb
    c=keepc
    print(A[a]+B[b]+C[c])
    #print(a,b,c)
    #print(memo[2][1][1])
    memo[a][b][c]=True

Fixed submission:
x,y,z,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
AB=[]
for a in A:
    for b in B:
        AB.append(a+b)
AB=sorted(AB,reverse=True)[:k]
ABC=[]
for ab in AB:
    for c in C:
        ABC.append(ab+c)
ABC.sort(reverse=True)
for abc in ABC[:k]:
    print(abc)
Buggy python submission (verdict: Time Limit Exceeded):
N,Q=map(int,input().split())
S=input()
ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ABCD={}
for i,l in enumerate(ABC):
    ABCD[l]=i
    
memo=[[] for _ in range(26)]
for i,s in enumerate(S):
    memo[ABCD[s]].append(i)
state=[1]*N
exist=N

for _ in range(Q):
    t,d=input().split()
    
    if d=='L':
        tmp=memo[ABCD[t]][:]
        if tmp==[]: continue
        if tmp[0]==0:
            exist-=state[0]
            state[0]=0
            del tmp[0]
        for m in tmp:
            state[m-1]+=state[m]
            state[m]=0
    
    else:
        tmp=memo[ABCD[t]][::-1]
        if tmp==[]: continue
        if tmp[0]==N-1:
            exist-=state[-1]
            state[-1]=0
            del tmp[0]
        for m in tmp:
            state[m+1]+=state[m]
            state[m]=0
print(exist)

Fixed submission:
N,Q=map(int,input().split())
S=input()
TD=[tuple(input().split()) for _ in range(Q)]

def go(x):
    pos=x
    for t,d in TD:
        if pos==N: return N
        if pos==-1: return -1
        if t==S[pos]:
            if d=='L': pos-=1
            else: pos+=1
    return pos

U=N
L=-1
while U-L>1:
    x=(U+L)//2
    if go(x)==-1: L=x
    else: U=x
left=L

U=N
L=-1
while U-L>1:
    x=(U+L)//2
    if go(x)==N: U=x
    else: L=x
right=U

print(right-left-1)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q=map(int,input().split())
S=input()
ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
memo=[[] for _ in range(26)]
for i,s in enumerate(S):
    memo[ABC.index(s)].append(i)
state=[1]*N
exist=N

for _ in range(Q):
    t,d=input().split()
    
    if d=='L':
        for m in memo[ABC.index(t)]:
            if m==0:
                exist-=state[m]
            else:
                state[m-1]+=state[m]
            state[m]=0
    
    else:
        for m in memo[ABC.index(t)][::-1]:
            if m==N-1:
                exist-=state[m]
            else:
                state[m+1]+=state[m]
            state[m]=0
print(exist)

Fixed submission:
N,Q=map(int,input().split())
S=input()
TD=[tuple(input().split()) for _ in range(Q)]

def go(x):
    pos=x
    for t,d in TD:
        if pos==N: return N
        if pos==-1: return -1
        if t==S[pos]:
            if d=='L': pos-=1
            else: pos+=1
    return pos

U=N
L=-1
while U-L>1:
    x=(U+L)//2
    if go(x)==-1: L=x
    else: U=x
left=L

U=N
L=-1
while U-L>1:
    x=(U+L)//2
    if go(x)==N: U=x
    else: L=x
right=U

print(right-left-1)