Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
a = list(map(int, input().split()))
b, c = a[0], a[1]
ans = 0
while c > 0:
    if c > 1 and b > 0:
        ans += 1
        b -= 1
        c -= 2
    elif c > 3:
        c -= 4
        ans += 1
    else:
        break

print(ans)

Fixed submission:
a = list(map(int, input().split()))
b, c = a[0], a[1]
ans = min(b, c // 2)
if b > c // 2:
    c = c % 2
else:
    c -= b * 2
b -= min(b, c // 2)
if c > 3:
    ans += c // 4
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np
n, m = map(int, input().split())
if m == 0:
    print(0)
    exit()
r = [[] for i in range(m)]
for i in range(m):
    f, s = map(int, input().split())
    r[f-1].append(s - 1)
    r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
    global ans
    if len(visited) == n:
        ans.append([visited])
        return
    for j in r[now]:
        if j not in visited:
            dfs(j, visited + [j])
dfs(0, [0])
print(len(ans))

Fixed submission:
import numpy as np
n, m = map(int, input().split())
if m == 0:
    print(0)
    exit()
r = [[] for i in range(n)]
for i in range(m):
    f, s = map(int, input().split())
    r[f-1].append(s - 1)
    r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
    global ans
    if len(visited) == n:
        ans.append([visited])
        return
    for j in r[now]:
        if j not in visited:
            dfs(j, visited + [j])
dfs(0, [0])
print(len(ans))

Buggy python submission (verdict: Runtime Error):
import numpy as np
n, m = map(int, input().split())
r = [[] for i in range(m)]
for i in range(m):
    f, s = map(int, input().split())
    r[f-1].append(s - 1)
    r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
    try:
        global ans
        if len(visited) == n:
            ans.append([visited])
            return
        for j in r[now]:
            if j not in visited:
                dfs(j, visited + [j])
    except:
        return
dfs(0, [0])
print(len(ans))

Fixed submission:
import numpy as np
n, m = map(int, input().split())
if m == 0:
    print(0)
    exit()
r = [[] for i in range(n)]
for i in range(m):
    f, s = map(int, input().split())
    r[f-1].append(s - 1)
    r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
    global ans
    if len(visited) == n:
        ans.append([visited])
        return
    for j in r[now]:
        if j not in visited:
            dfs(j, visited + [j])
dfs(0, [0])
print(len(ans))

Buggy python submission (verdict: Runtime Error):
import numpy as np
n, m = map(int, input().split())
r = [[] for i in range(m)]
for i in range(m):
    f, s = map(int, input().split())
    r[f-1].append(s - 1)
    r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
    global ans
    if len(visited) == n:
        ans.append([visited])
        return
    for j in r[now]:
        if j not in visited:
            dfs(j, visited + [j])
dfs(0, [0])
print(len(ans))

Fixed submission:
import numpy as np
n, m = map(int, input().split())
if m == 0:
    print(0)
    exit()
r = [[] for i in range(n)]
for i in range(m):
    f, s = map(int, input().split())
    r[f-1].append(s - 1)
    r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
    global ans
    if len(visited) == n:
        ans.append([visited])
        return
    for j in r[now]:
        if j not in visited:
            dfs(j, visited + [j])
dfs(0, [0])
print(len(ans))
Buggy python submission (verdict: Runtime Error):
s = input()


def solve(query):
    if query == "":
        print("YES")
        exit()
    for frag in ("eraser", "erase", "dream", "dreamer"):
        if query.startswith(frag):
            solve(query[len(frag):])


solve(s)
print("NO")


Fixed submission:
s = input()


def solve(query):
    while 1:
        for frag in ("erase", "eraser", "dream", "dreamer"):
            if query.endswith(frag):
                query = query[:-len(frag)]
                break
        else:
            print("NO")
            break
        if not query:
            print("YES")
            break



solve(s)
Buggy python submission (verdict: Time Limit Exceeded):
a = input()
while 1:
    if 'ST' in a:
        a = a[:a.index('ST')] + a[a.index('ST')+2:]
    else:
        break
print(len(a))


Fixed submission:
a = list(input())

n = 0
res = 0
for x in a:
    if x == 'T':
        if n == 0:
            res += 1
        elif n > 0:
            n -= 1
    elif x == 'S':
        n += 1
res += n

print(res)
Buggy python submission (verdict: Runtime Error):
# Useful data structures
from collections import Counter, defaultdict, deque, namedtuple

# Most useful math functions
from math import gcd

# For sys.std*, sys.argv etc...
import sys

# For cases when input tokens are spread unevenly over many lines.
# This provides methods for reading tokens of various types.
class BufferedTokenizer:
    pass

def read_ints():
    return [int(token) for token in input().split()]

def read_tokens():
    return input().split()

def main():
    print("Yes" if len(set(read_ints())) == 2 else "No")

if __name__ == '__main__':
    main()


Fixed submission:
# For sys.std*, sys.argv etc...
import sys

# For cases when input tokens are spread unevenly over many lines.
# This provides methods for reading tokens of various types.
class BufferedTokenizer:
    pass

def read_ints():
    return [int(token) for token in input().split()]

def read_tokens():
    return input().split()

def main():
    print("Yes" if len(set(read_ints())) == 2 else "No")

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input()) 
A = list(int(i) for i in input().split())  
Q = int(input()) 
B_C = [[int(i) for i in input().split()] for i in range(Q)] 
for j in range(Q):
  #print(j)
  A = [B_C[j][1] if i == B_C[j][0] else i for i in A]
  print(sum(A))

Fixed submission:
N = int(input()) 
A = list(int(i) for i in input().split())  
Q = int(input()) 
B_C = [[int(i) for i in input().split()] for i in range(Q)] 
first_sum = sum(A)
import collections
c = collections.Counter(A)
for i in range(Q):
  change_value = (B_C[i][1]-B_C[i][0])*c[B_C[i][0]]
  c[B_C[i][1]] += c[B_C[i][0]]
  c[B_C[i][0]] = 0
  first_sum += change_value
  print(first_sum)
  #print(c)
Buggy python submission (verdict: Runtime Error):
N,K = (int(i) for i in input().split()) 
A = [int(i) for i in input().split()] 
A_done = [0]*N
A_route = []
point = 1
end_point = 0
while A_done[point-1]==0:
  A_done[point-1] = 1
  A_route.append(point)
  point = A[point-1]
  if A_done[point-1]==1:
    end_point = point
cal = K%len(A_route)
roop = A_route.index(end_point)
f_len = len(A_route[:roop])
roop_len = len(A_route[roop:])
A_roop = A_route[roop:]
if K >= f_len + roop_len:
  cal = (K-f_len)%(len(A_roop))
  #print(cal)
  print(A_roop[cal])
else:
  print(A_route(K))

Fixed submission:
N,K = (int(i) for i in input().split()) 
A = [int(i) for i in input().split()] 
A_done = [0]*N
A_route = []
point = 1
end_point = 0
while A_done[point-1]==0:
  A_done[point-1] = 1
  A_route.append(point)
  point = A[point-1]
  if A_done[point-1]==1:
    end_point = point
cal = K%len(A_route)
roop = A_route.index(end_point)
f_len = len(A_route[:roop])
roop_len = len(A_route[roop:])
A_roop = A_route[roop:]
if K >= f_len + roop_len:
  cal = (K-f_len)%(len(A_roop))
  #print(cal)
  print(A_roop[cal])
else:
  print(A_route[K])
Buggy python submission (verdict: Runtime Error):
X = int(input())
for i in range(130):
  for j in range(-66,66):
    if i**5 - j**5 ==X:
      A = i
      B = j
      break
print(str(A)+str(" ")+str(B))

Fixed submission:
X = int(input())
for i in range(130):
  for j in range(-130,130):
    if i**5 - j**5 ==X:
      A = i
      B = j
      break

print(str(A)+str(" ")+str(B))
Buggy python submission (verdict: Runtime Error):
A,B,N = (int(i) for i in input().split())
max_num = -10**100
tmp_num = 0
cnt = 1
flg = False
while flg  == False:
  tmp_num = math.floor(A*cnt/B) - A*math.floor(cnt/B)
  #print(tmp_num)
  if tmp_num > max_num :
    max_num = tmp_num
    cnt += 1
  else:
    flg = True
print(max_num)

Fixed submission:
a, b, n = map(int, input().split())
if n >= b - 1:
    print(int(a - a / b))
else:
    print(int(a * n / b) - a * int(n / b))
Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())
import math
from functools import reduce
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
tmp_sum = 0
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      tmp_list = [i,j,k]
      tmp_sum += gcd_list(tmp_list)
print(tmp_sum)


Fixed submission:
K = int(input())
import math
from functools import reduce
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
tmp_sum = 0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if i != j and j != k:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)*6
      elif i == j and j == k:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)
      else:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)*3
print(tmp_sum)

Buggy python submission (verdict: Runtime Error):
import fractions
import math
from functools import reduce
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
tmp_sum = 0
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      tmp_list = [i,j,k]
      tmp_sum += gcd_list(tmp_list)
print(tmp_sum)

Fixed submission:
K = int(input())
import math
from functools import reduce
def gcd_list(numbers):
    return reduce(math.gcd, numbers)
tmp_sum = 0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if i != j and j != k:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)*6
      elif i == j and j == k:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)
      else:
        tmp_list = [i,j,k]
        tmp_sum += gcd_list(tmp_list)*3
print(tmp_sum)
Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())
Q = int(input())
Q_l = []
for i in range(Q):
    Q_l.append(input().split())
for i in range(Q):
    #print(S)
    if int(Q_l[i][0]) == 1:
        S.reverse()
    else:
        if int(Q_l[i][1]) == 1:
            S = list(Q_l[i][2]) + S
        else:
            S.append(Q_l[i][2])
print("".join(S))

Fixed submission:
S = list(input())
Q = int(input())
Q_l = []
for i in range(Q):
    Q_l.append(input().split())
rev_flg = 0
head_list =[]
tail_list = []
for i in range(Q):
    if int(Q_l[i][0]) == 1:
        rev_flg += 1
    else:
        if rev_flg % 2 == 0 and int(Q_l[i][1]) == 1:
            head_list.append(Q_l[i][2])
        elif rev_flg % 2 == 1 and int(Q_l[i][1]) == 1:
            tail_list.append(Q_l[i][2])
        elif rev_flg % 2 == 0 and int(Q_l[i][1]) == 2:
            tail_list.append(Q_l[i][2])
        else:
            head_list.append(Q_l[i][2])
head_list.reverse()
head_list.extend(S)
head_list.extend(tail_list)
if rev_flg %2 ==1:
    head_list.reverse()
print("".join(head_list))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
N = int(input())
A = [int(i) for i in input().split()]
flg = 0
for i in range(len(A)-1):
  #print("i:"+str(i))
  for j in range(i+1,len(A)):
    #print("  j:"+str(j))
    if A[i] == A[j]:
      print("NO")
      sys.exit()
print("YES")

Fixed submission:
N = int(input())
A = [int(i) for i in input().split()]
B = list(set(A))
if len(A) == len(B):
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Time Limit Exceeded):
N, K = (int(i) for i in input().split())
p = [int(i) for i in input().split()]
sum_max = 0
sum_max_index = 0
for i in range(N-K+2):
  #print(i)
  tmp_sum = sum(p[i:i+K])
  #print(sum_max)
  if sum_max < tmp_sum:
    sum_max = tmp_sum
    sum_max_index = i
total_num = 0
for i in range(sum_max_index,sum_max_index+K):
  total_num += (p[i]+1)/2
print(total_num)

Fixed submission:
N, K = (int(i) for i in input().split())
p = [int(i) for i in input().split()]
p_ex = [(i+1)/2 for i in p]
sum_list = [sum(p_ex[0:K]),sum(p_ex[N-K:N])]
tmp_num = sum_list[0]
if N == K:
  print(sum(p_ex))
else:
  for i in range(1,N-K):
    #print("i:"+str(i))
    tmp_num = tmp_num - p_ex[i-1] + p_ex[i+K-1]
    #print("  tmp_num = tmp_num - "+str(p_ex[i-1])+" + "+str(p_ex[i+K-1]))
    sum_list.append(tmp_num)
  print(max(sum_list))
Buggy python submission (verdict: Runtime Error):
N = int(input())
P = [int(i) for i in input().split()]
P_min = P.copy()
for i in range(1,N):
    P_min[i] = min(P_min[i-1],P[i])
cnt = 0
for i in range(N):
    if P_min[i] >= P[i]:
        cnt += 1
print(cnt)

Fixed submission:
N = int(input())
P = [int(i) for i in input().split()]
P_min = P.copy()
for i in range(1,N):
    P_min[i] = min(P_min[i-1],P[i])
cnt = 0
for i in range(N):
    if P_min[i] >= P[i]:
        cnt += 1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
P = [int(i) for i in input().split()]
cnt = 0
for i in range(N):
    if min(P[:i+1]) >= P[i]:
        cnt += 1
print(cnt)

Fixed submission:
N = int(input())
P = [int(i) for i in input().split()]
P_min = P.copy()
for i in range(1,N):
    P_min[i] = min(P_min[i-1],P[i])
cnt = 0
for i in range(N):
    if P_min[i] >= P[i]:
        cnt += 1
print(cnt)
Buggy python submission (verdict: Runtime Error):
X = int(input())
import random

def is_prime3(q,k=50):
    q = abs(q)
    #計算するまでもなく判定できるものははじく
    if q == 2: return True
    if q < 2 or q&1 == 0: return False

    #n-1=2^s*dとし（但しaは整数、dは奇数)、dを求める
    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1
    
    #判定をk回繰り返す
    for i in range(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        #[0,s-1]の範囲すべてをチェック
        while t != q-1 and y != 1 and y != q-1: 
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True
 i = X
cnt = 0
flg = is_prime3(i)
if flg == False:
    while flg==False:
        i += 1
        cnt = i
        flg = is_prime3(i)
    print(cnt)
else:
    print(i)

Fixed submission:
X = int(input())
import random

def is_prime3(q,k=50):
    q = abs(q)
    #計算するまでもなく判定できるものははじく
    if q == 2: return True
    if q < 2 or q&1 == 0: return False

    #n-1=2^s*dとし（但しaは整数、dは奇数)、dを求める
    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1
    
    #判定をk回繰り返す
    for i in range(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        #[0,s-1]の範囲すべてをチェック
        while t != q-1 and y != 1 and y != q-1: 
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True
  
i = X
cnt = 0
flg = is_prime3(i)
if flg == False:
    while flg==False:
        i += 1
        cnt = i
        flg = is_prime3(i)
    print(cnt)
else:
    print(i)
Buggy python submission (verdict: Time Limit Exceeded):
A, B = (int(i) for i in input().split())
def euclid(a, b):
    while b:
        a, b = b, a%b
    return a
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors
num = make_divisors(euclid(A,B))
if len(num) <= 2:
  print(len(num))
else:
  total_num  = [0]*len(num)
  for i in range(1,len(num)):
    for j in range(i,len(num)):
      #print(i)
      #print(" "+str(j))
      try:
        #print(i)
        #print(" "+str(j))
        tmp_index = num[i]*num[j]
        total_num[num.index(tmp_index)] = 1
      except:
        #print("NG")
        total_num = total_num
  print(total_num.count(0))


Fixed submission:
A, B = (int(i) for i in input().split())
def euclid(a, b):
    while b:
        a, b = b, a%b
    return a
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(n)
  return table
try:
  print(len(list(set(prime_decomposition(euclid(A,B)))))+1)
except:
  print(1)

Buggy python submission (verdict: Time Limit Exceeded):
A, B = (int(i) for i in input().split())
def euclid(a, b):
    while b:
        a, b = b, a%b
    return a
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors
num = make_divisors(euclid(A,B))
if len(num) <= 2:
  print(len(num))
else:
  total_num  = [0]*len(num)
  for i in range(1,len(num)):
    for j in range(i,len(num)):
      #print(i)
      #print(" "+str(j))
      try:
        #print(i)
        #print(" "+str(j))
        tmp_index = num[i]*num[j]
        total_num[num.index(tmp_index)] = 1
      except:
        #print("NG")
        total_num = total_num
  print(total_num.count(0))

Fixed submission:
A, B = (int(i) for i in input().split())
def euclid(a, b):
    while b:
        a, b = b, a%b
    return a
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(n)
  return table
try:
  print(len(list(set(prime_decomposition(euclid(A,B)))))+1)
except:
  print(1)
Buggy python submission (verdict: Runtime Error):
a=int(input())
b=int(input())
print(a*b

Fixed submission:
a, b=map(int, input().split())
print(a*b)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int, input().split()))
s = []
for i in l:
    if (i in s):
        print("NO")
        exit(0)
    s.append(i)
print("YES")


Fixed submission:
n = int(input())
l = list(map(int, input().split()))
s = set()
for i in l:
    if (i in s):
        print("NO")
        exit(0)
    s.add(i)
print("YES")
Buggy python submission (verdict: Runtime Error):
a,b = map(int,intput().split())
ans = max(a-2*b,0)
print(ans)

Fixed submission:
a,b = map(int,input().split())
ans = max(a-2*b,0)
print(ans)
Buggy python submission (verdict: Runtime Error):
def solve():
    S= input()
    ue = int(S[:2])
    sita = int(S[2:])

    if 1 <= sita <= 12 and not(1 <= ue <= 12):
        print("YYMM")
    elif (not (1 <= ue <= 12)) and 1 <= sita <= 12:
        print("MMYY")
    elif (not (1 <= ue <= 12)) and (not (1 <= sita <= 12)):
        print("NA")
    elif 1 <= sita <= 12 and (1 <= ue <= 12):
        print("AMBIGUOUS")
    else:
        raise "ERROR"
if __name__ == "__main__":
    solve()

Fixed submission:
def solve():
    S= input()
    ue = int(S[:2])
    sita = int(S[2:])

    ueok = 1 <= ue <= 12
    sitaok = 1 <= sita <= 12
    if ueok and sitaok:
        print("AMBIGUOUS")
    elif not ueok and sitaok:
        print("YYMM")
    elif ueok and not sitaok:
        print("MMYY")
    else:
        print("NA")

if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 998244353

N = int(input())
A = [int(input()) for _ in range(N)]
SUM = sum(A)

dp = [ [0] * (SUM + 1) for _ in range(N+1) ]
dp2 = [ [0] * (SUM + 1) for _ in range(N+1) ]
# red [i][r] : rまで塗った
dp[0][0] = 1
dp2[0][0] = 1
for i in range(N):
    a = A[i]
    for r in range(0, SUM-a +1):
        #pick
        dp[i+1][r+a] = ( dp[i+1][r+a] + dp[i][r] ) % MOD
        dp2[i+1][r+a] = ( dp2[i+1][r+a] + dp2[i][r] ) % MOD


    for r in range(0, SUM + 1):
        # nopick    
        # B,G に入れる 2通り考える
        dp[i+1][r] = (dp[i+1][r] +  dp[i][r] * 2 ) % MOD
        dp2[i+1][r] = (dp2[i+1][r] +  dp2[i][r]  ) % MOD

if SUM % 2 ==0:
    notans = sum(dp[N][ SUM//2 : ]) * 3 % MOD

    notans = ( notans - 3 * dp2[N][SUM//2] ) %MOD
    cand = pow(3, N, MOD)

    print(  (cand - notans + MOD) % MOD  )
else:
    notans = sum(dp[N][ SUM//2 + 1: ]) * 3 % MOD
    cand = pow(3, N, MOD)
    print(  (cand - notans + MOD) % MOD  )



Fixed submission:
MOD = 998244353

N = int(input())
A = [int(input()) for _ in range(N)]
SUM = sum(A)

dp = [ [0] * (SUM + 1) for _ in range(N+1) ]
dp2 = [ [0] * (SUM + 1) for _ in range(N+1) ]
# red [i][r] : rまで塗った
dp[0][0] = 1
dp2[0][0] = 1
for i in range(N):
    a = A[i]
    for r in range(0, SUM-a +1):
        #pick
        dp[i+1][r+a] = ( dp[i+1][r+a] + dp[i][r] ) % MOD
        dp2[i+1][r+a] = ( dp2[i+1][r+a] + dp2[i][r] ) % MOD


    for r in range(0, SUM + 1):
        # nopick    
        # B,G に入れる 2通り考える
        dp[i+1][r] = (dp[i+1][r] +  dp[i][r] * 2 ) % MOD
        dp2[i+1][r] = (dp2[i+1][r] +  dp2[i][r]  ) % MOD

if SUM % 2 ==0:
    notans = sum(dp[N][ SUM//2 : ]) * 3 % MOD

    notans = ( notans - 3 * dp2[N][SUM//2] ) %MOD
    cand = pow(3, N, MOD)

    print(  (cand - notans + MOD) % MOD  )
else:
    notans = sum(dp[N][ SUM//2 + 1: ]) * 3 % MOD
    cand = pow(3, N, MOD)
    print(  (cand - notans + MOD) % MOD  )
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()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

apb =  []

for a in A:
    for b in B:
        apb.append(a+b)
apb.sort(reverse=True)

apb = apb[:K]
ans = []

for k in apb:
    for c in (C):
        ans.append(k+c)
ans.sort(reverse=True)

for k in range(K):
    print(ans[k])

    


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

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

apb =  []

for a in A:
    for b in B:
        apb.append(a+b)
apb.sort(reverse=True)

apb = apb[:K]
ans = []

for k in apb:
    for c in (C):
        ans.append(k+c)
ans.sort(reverse=True)

for k in range(K):
    print(ans[k])
Buggy python submission (verdict: Runtime Error):
N,Q = map(int,input().split())
S = input()
QUE = [ input().split() for _ in range(Q)]

N = len(S)

l = 0
r = N-1

for i in range(Q-1, -1, -1):
    t,d = QUE[i]
    if d == "L" and t == S[l]:
        l += 1
    if d == "R" and t == S[r]:
        r -= 1

print(max(0, r-l+1))

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


N = len(S)
ans = 0

# N-1 falls from right edge ? 
nowpos = N-1
fall_f = False
for i in range(Q):
    t,d = TD[i]
    nowChar = S[nowpos]
    if t == nowChar:
        if d == "L":
            nowpos -= 1
            if nowpos == -1:
                break
        if d == "R":
            nowpos += 1
            if nowpos == N:
                fall_f = True
                break

if fall_f:
    ok = N-1
    ng = -1
    mid = (ok+ng)//2
    while abs(ok - ng) > 1:
        nowpos = mid
        fall_f = False

        for i in range(Q):
            t,d = TD[i]
            nowChar = S[nowpos]
            if t == nowChar:
                if d == "L":
                    nowpos -= 1
                    if nowpos == -1: break
                if d == "R":
                    nowpos += 1
                    if nowpos == N:
                        fall_f = True
                        break
        if fall_f :
            ok = mid
        else:
            ng = mid

        mid = (ok+ng)//2

    ans += N - ok 

    

    
# 0 fallse from left edge  ?
nowpos = 0
fall_f = False
for i in range(Q):
    t,d = TD[i]
    nowChar = S[nowpos]
    if t == nowChar:
        if d == "L":
            nowpos -= 1
            if nowpos == -1:
                fall_f = True
                break
        if d == "R":
            nowpos += 1
            if nowpos == N:
                break
if fall_f:

    ok = 0
    ng = N
    mid = (ok+ng)//2
    while abs(ok - ng) > 1:

        nowpos = mid
        fall_f = False

        for i in range(Q):
            t,d = TD[i]
            nowChar = S[nowpos]
            if t == nowChar:
                if d == "L":
                    nowpos -= 1
                    if nowpos == -1: 
                        fall_f = True
                        break
                if d == "R":
                    nowpos += 1
                    if nowpos == N:
                        break
        if fall_f :
            ok = mid
        else:
            ng = mid

        mid = (ok+ng)//2

    ans= min( ans +  ok + 1,  N)

print(N - ans)