Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
#c
n,m =map(int, input().split())
h = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
result = 0
for i in range(1,n+1):
    flag = 1
    for j in range(len(ab)):
        
        if i in ab[j]:
            if i == ab[j][0]:
                if h[i-1] <= h[ab[j][1]-1]:
                    flag = 0
                    break
            else:
                if h[i-1] <= h[ab[j][0]-1]:
                    flag = 0
                    break
    
    if flag == 1:
        result += 1
print(result)

Fixed submission:
#c
n,m =map(int, input().split())
h = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
a, b = [list(i) for i in zip(*ab)]
result = []
for i in ab:
    if h[i[0]-1] > h[i[1]-1]:
        result.append(i[1])
    elif h[i[1]-1] > h[i[0]-1]:
        result.append(i[0])
    else:
        result.append(i[0])
        result.append(i[1])
result = list(set(result))
print(n - len(result))

Buggy python submission (verdict: Time Limit Exceeded):
n,m =map(int, input().split())
h = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
#a, b = [list(i) for i in zip(*ab)]
test = []
for i in range(1,n+1):
    check = [j[1] for j in ab if j[0] == i] + [j[0] for j in ab if j[1] == i]
    check = list(set(check))
    test.append(check)
result = 0
for j in range(len(test)):
    flag = 1
    for k in test[j]:
        if h[k-1] >= h[j]:
            flag = 0
            break
    if flag == 1:
        result += 1
print(result)

Fixed submission:
#c
n,m =map(int, input().split())
h = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
a, b = [list(i) for i in zip(*ab)]
result = []
for i in ab:
    if h[i[0]-1] > h[i[1]-1]:
        result.append(i[1])
    elif h[i[1]-1] > h[i[0]-1]:
        result.append(i[0])
    else:
        result.append(i[0])
        result.append(i[1])
result = list(set(result))
print(n - len(result))

Buggy python submission (verdict: Time Limit Exceeded):
n,m =map(int, input().split())
h = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
a, b = [list(i) for i in zip(*ab)]
test = []
for i in range(1,n+1):
    check = [j[1] for j in ab if j[0] == i] + [j[0] for j in ab if j[1] == i]
    check = list(set(check))
    test.append(check)
result = 0
for j in range(len(test)):
    flag = 1
    for k in test[j]:
        if h[k-1] >= h[j]:
            flag = 0
            break
    if flag == 1:
        result += 1
print(result)

Fixed submission:
#c
n,m =map(int, input().split())
h = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
a, b = [list(i) for i in zip(*ab)]
result = []
for i in ab:
    if h[i[0]-1] > h[i[1]-1]:
        result.append(i[1])
    elif h[i[1]-1] > h[i[0]-1]:
        result.append(i[0])
    else:
        result.append(i[0])
        result.append(i[1])
result = list(set(result))
print(n - len(result))
Buggy python submission (verdict: Runtime Error):

x = int(input())
for a in range(1,100):
    a_5 = a**5
    
    if a_5 > x:
        b = 1
        s = 0
        while s > x:
            s = a_5 - b**5
            if s == x:
                print(a, b)
                sys.exit()
            b += 1
            
    elif a_5 < x:
        b = -1
        s = 0
        while s < x:
            s = a_5 - b**5
            if s == x:
                print(a, b)
                sys.exit()
            b -= 1
    else:
        print(a,0)
        sys.exit()


Fixed submission:
import sys

x = int(input())
for a in range(1,1000):
    a_5 = a**5
    if a_5 > x:
        b = 1
        s = x+1
        while s > x:
            s = a_5 - b**5
            if s == x:
                print(a, b)
                sys.exit()
            b += 1
            
    elif a_5 < x:
        b = -1
        s = x-1
        while s < x:
            s = a_5 - b**5
            if s == x:
                print(a, b)
                sys.exit()
            b -= 1
    else:
        print(a,0)
        sys.exit()
Buggy python submission (verdict: Runtime Error):
n = int(input())
result = [0]*n**3
for a in range(n):
    for b in range(n):
        for c in range(n):
            if a > b:
                result[n*n*a +n*b + c]= result[n*n*b +n*a + c]
            elif b > c:
                result[n*n*a +n*b + c]= result[n*n*a +n*c + b]
            else:
                result[n*n*a +n*b + c] = gcd(a+1, gcd(b+1,c+1))
print(sum(result))

Fixed submission:
n = int(input())
answer = 0

def gcd(i, j):
    while j:
        i, j = j, i % j
    return i

for a in range(1,n+1):
    for b in range(1,n+1):
        d = gcd(a,b)
        for c in range(1, n+1):
            answer += gcd(c, d)
print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int, input().split())
if n % k == 0:
    print(0)
else:
    before =n
    while True:
        if abs(n-k) < before:
            n = abs(n - k)
            before = n
        else:
            print(n)
            break

Fixed submission:
#c
n,k=map(int, input().split())

if n % k == 0:
    print(0)
else:
    n = n - (k*(n // k))
    if n > abs(n - k):
        print(abs(n-k))
    else:
        print(n)
Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
ans = [str(i) for i in range(1,10)]
c = 0
while len(ans)!=k:
    now = ans[c]
    t = int(now[-1])
    if t != 0:
        ans.append(str(now) +str(t-1))

    ans.append(str(now) +str(now)[-1])

    if t != 9:
        ans.append(str(now) +str(t+1))

    c += 1
print(ans[k-1])

Fixed submission:
k = int(input())
ans = [str(i) for i in range(1,10)]
c = 0

while len(ans) < k:
    now = ans[c]
    t = int(now[-1])
    if t != 0:
        ans.append(str(now) +str(t-1))

    ans.append(str(now) +str(now)[-1])

    if t != 9:
        ans.append(str(now) +str(t+1))

    c += 1
print(ans[k-1])

Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
ans = []
queue = [str(i) for i in range(1,10)]

while queue:
    if len(ans)==k:
        print(ans[k-1])
        break
    now = queue.pop(0)
    ans.append(now)
    t = int(now[-1])
    if t -1 >= 0:
        queue.append(str(now) +str(t-1))

    queue.append(str(now) +str(now)[-1])

    if t +1 <= 9:
        queue.append(str(now) +str(t+1))

Fixed submission:
k = int(input())
ans = [str(i) for i in range(1,10)]
c = 0

while len(ans) < k:
    now = ans[c]
    t = int(now[-1])
    if t != 0:
        ans.append(str(now) +str(t-1))

    ans.append(str(now) +str(now)[-1])

    if t != 9:
        ans.append(str(now) +str(t+1))

    c += 1
print(ans[k-1])

Buggy python submission (verdict: Time Limit Exceeded):
#幅
k = int(input())
ans = []
queue = [i for i in range(1,10)]

while queue:
    if len(ans)==k:
        print(ans[k-1])
        break
    now = queue.pop(0)
    ans.append(now)
    if int(str(now)[-1]) -1 >= 0:
        queue.append(int(str(now) +str(int(str(now)[-1])-1)))

    queue.append(int(str(now) +str(now)[-1]))

    if int(str(now)[-1]) +1 <= 9:
        queue.append(int(str(now) +str(int(str(now)[-1])+1)))

Fixed submission:
k = int(input())
ans = [str(i) for i in range(1,10)]
c = 0

while len(ans) < k:
    now = ans[c]
    t = int(now[-1])
    if t != 0:
        ans.append(str(now) +str(t-1))

    ans.append(str(now) +str(now)[-1])

    if t != 9:
        ans.append(str(now) +str(t+1))

    c += 1
print(ans[k-1])
Buggy python submission (verdict: Time Limit Exceeded):
n,x,y=map(int, input().split())
x -= 1
y -= 1
queue = []
zumi = []
result = [[0]* n for i in range(n)]
next_point = []

for i in range(n):
    queue.append(i)
    zumi = []
    result[i][i] = 0
    
    while queue:
        next_point = []
        now = queue.pop(0)
        zumi.append(now)
        
        #次に行ける場所
        if now - 1 >= 0:
            next_point.append(now - 1)
        if now + 1 < n :
            next_point.append(now +1)
        if now == x:
            next_point.append(y)
        if now == y:
            next_point.append(x)
            
        for a in next_point:
            if a in zumi :
                pass
            elif result[i][a] == 0:
                result[i][a] = result[i][now] + 1
                queue.append(a)
                
for i in range(1,n):
    count = 0
    for j in result:
        count += j.count(i)
    print(int(count/2))

Fixed submission:
n,x,y=map(int,input().split())
ans=[0]*n #個数を数えるのにバケットを用意する
for i in range(1,n+1): #i<jなるすべての頂点の組について最短距離を求める
    for j in range(i+1,n+1):
        dist1=j-i
        dist2=abs(x-i)+abs(y-j)+1
        dist3=abs(y-i)+abs(x-j)+1
        d=min(dist1,dist2,dist3) #最短距離は上式により求められる
        ans[d]+=1 #最短距離がdとなる組の個数を1増やす
for i in range(1,n):
    print(ans[i])

Buggy python submission (verdict: Runtime Error):
#借り物 テスト
ans=[0]*n #個数を数えるのにバケットを用意する
for i in range(1,n+1): 
    for j in range(i+1,n+1):
        dist1=j-i
        dist2=abs(x-i)+abs(y-j)+1
        #dist3=abs(y-i)+abs(x-j)+1
        d=min(dist1,dist2) 
        ans[d]+=1 
for i in range(1,n):
    print(ans[i])

Fixed submission:
n,x,y=map(int,input().split())
ans=[0]*n #個数を数えるのにバケットを用意する
for i in range(1,n+1): #i<jなるすべての頂点の組について最短距離を求める
    for j in range(i+1,n+1):
        dist1=j-i
        dist2=abs(x-i)+abs(y-j)+1
        dist3=abs(y-i)+abs(x-j)+1
        d=min(dist1,dist2,dist3) #最短距離は上式により求められる
        ans[d]+=1 #最短距離がdとなる組の個数を1増やす
for i in range(1,n):
    print(ans[i])
Buggy python submission (verdict: Runtime Error):
a,b=map(int, input().split())
answer = 0

#偶数2
if a > 1 :
    answer += math.factorial(a) / (math.factorial(a - 2)*2)

#奇数2
if b > 1:
    answer += math.factorial(b) / (math.factorial(b - 2)*2)

print(int(answer))

Fixed submission:
import math
a,b=map(int, input().split())
answer = 0

if a > 1 :
    answer += math.factorial(a) / (math.factorial(a - 2)*2)

if b > 1:
    answer += math.factorial(b) / (math.factorial(b - 2)*2)

print(int(answer))

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

answer = 0

#偶数2
answer += math.factorial(a) / (math.factorial(a - 2)*2)

#奇数2
answer += math.factorial(b) / (math.factorial(b - 2)*2)

print(int(answer))

Fixed submission:
import math
a,b=map(int, input().split())
answer = 0

if a > 1 :
    answer += math.factorial(a) / (math.factorial(a - 2)*2)

if b > 1:
    answer += math.factorial(b) / (math.factorial(b - 2)*2)

print(int(answer))
Buggy python submission (verdict: Runtime Error):
print(target[int(input()) - 1])

Fixed submission:
target = [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]
print(target[int(input()) - 1])
Buggy python submission (verdict: Runtime Error):
a,b=map(int, input().split()) 
if a =1 or b = 1:
    print(1)
    exit()
if a%2 != 0:
    if b%2 != 0:
        if a > b:
            print(int((-(-a//2))+(a*(b-1)/2)))
        else:
            print(int((-(-a//2))+(a*(b-1)/2)))
    else:
        print(int(a*b/2))
else:
    print(int(a*b/2))

Fixed submission:
a,b=map(int, input().split()) 
if a ==1 or b == 1:
    print(1)
    exit()
if a%2 != 0:
    if b%2 != 0:
        if a > b:
            print(int((-(-a//2))+(a*(b-1)/2)))
        else:
            print(int((-(-a//2))+(a*(b-1)/2)))
    else:
        print(int(a*b/2))
else:
    print(int(a*b/2))
Buggy python submission (verdict: Time Limit Exceeded):
#c
n = int(input())
a = [int(input()) for _ in range(n)]
import copy
ans = []
a_max = max(a)
for i in range(len(a)):
    if a[i]==a_max:
        tmp = copy.deepcopy(a)
        tmp.pop(i)
        print(max(tmp))
    else:
        print(a_max)

Fixed submission:
#c
n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a, reverse=True)
a_max = b[0]
for i in range(len(a)):
    if a[i] == a_max:
        print(b[1])
    else:
        print(a_max)

Buggy python submission (verdict: Time Limit Exceeded):
#c
n = int(input())
a = [int(input()) for _ in range(n)]
import copy
ans = []
for i in range(len(a)):
    tmp = copy.deepcopy(a)
    tmp.pop(i)
    print(max(tmp))

Fixed submission:
#c
n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a, reverse=True)
a_max = b[0]
for i in range(len(a)):
    if a[i] == a_max:
        print(b[1])
    else:
        print(a_max)
Buggy python submission (verdict: Runtime Error):
n,k=map(int, input().split())
mod = 10**9 + 7
ans = [0]*k
import math
ans[0]=(math.factorial(2+n-k-1) // (math.factorial(2+n-k-1-n+k) * math.factorial(n-k))) %mod

for i in range(1,k):#iは仕切りの数
    spl = math.factorial(k-1) // (math.factorial(k-1- i) * math.factorial(i))#仕切りのパターン
    rest = n-k-i
    print(spl,rest)
    if rest < 0:
        break
    if rest == 0:
        ans[i] = spl%mod
    else:
        ans[i] = (spl*(math.factorial(2+i) // (math.factorial(2+i-rest) * math.factorial(rest))))%mod
for i in ans:
    print(i)

Fixed submission:
n,k=map(int, input().split())
mod = 10**9 + 7
ans = [0]*k
import math
ans[0]=(math.factorial(2+n-k-1) // (math.factorial(2+n-k-1-n+k) * math.factorial(n-k))) %mod

for i in range(1,k):#iは仕切りの数
    spl = math.factorial(k-1) // (math.factorial(k-1- i) * math.factorial(i))#仕切りのパターン
    rest = n-k-i
    if rest < 0:
        break
    if rest == 0:
        ans[i] = spl%mod
    else:
        ans[i] = (spl*(math.factorial(2+i+rest-1) // (math.factorial(2+i-1) * math.factorial(rest))))%mod
for i in ans:
    print(i)
Buggy python submission (verdict: Runtime Error):
prob = 0
#サイコロの確率(1/n) 

for i in range(1,n+1):
    if i < k: #サイコロを振った時点で勝っているか？
        a = 1
        while i * (2**a) < k:
            a += 1
        prob += (1/n) * ((1/2) ** a)
    else:
        prob += (1/n)
print(prob)

Fixed submission:
n,k=map(int, input().split())
prob = 0
#サイコロの確率(1/n) 

for i in range(1,n+1):
    if i < k: #サイコロを振った時点で勝っているか？
        a = 1
        while i * (2**a) < k:
            a += 1
        prob += (1/n) * ((1/2) ** a)
    else:
        prob += (1/n)
print(prob)
Buggy python submission (verdict: Time Limit Exceeded):
from numba import jit
N = int(input())
ans = 0

@jit
def num(n):
  num = 0
  for i in range(1,int(n**0.5)+1):
    if n%i==0:
      num+=1
      if n!=i**2:
        num+=1
  return num
  
for n in range(1,N):
  ans+=num(n)

print(ans)

Fixed submission:
N = int(input())
ans = 0

for n in range(1,N):
  ans+=(N-1)//n

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from numpy import *
N,K = map(int,input().split())
LR = [list(map(int,input().split())) for k in range(K)]
D = (N+2)*[0]
dp = (N+1)*[0]
dp[1] = 1
mod = 998244353

for L,R in LR:
  D[L]+=1
  D[R+1]-=1

D = cumsum(D)

for i in range(1,N+1):
  for j in range(1,i):
    if D[j]==1:
      dp[i] = (dp[i]+dp[i-j])%mod

print(dp[N])

Fixed submission:
N,K = map(int,input().split())
LR = [list(map(int,input().split())) for k in range(K)]

dp = (3*N)*[0]
dp[0] = 1
dp[1] = -1

for n in range(N):
  for L,R in LR:
    dp[L+n]+=dp[n]
    dp[n+R+1]-=dp[n]
  dp[n+1] = (dp[n]+dp[n+1])%998244353

print(dp[N-1])
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n,k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
 
cycles, check = [], [False]*n
for pi in p:
  if not check[pi-1]:
    curr, cycle = pi, []
    check[pi-1] = True
    while pi!=p[curr-1]:
      check[curr-1] = True
      cycle.append(c[curr-1])
      curr = p[curr-1]
  check[curr-1] = True
  cycles.append(cycle + [c[curr-1]])

scores = []
for cycle in (np.array(cycle) for cycle in cycles):
  for i in range(cycle.size):
    cum = np.roll(cycle, i).cumsum()
    if cum[-1] > 0 and k > cycle.size:
      scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max())
    else:
      scores.append(cum[:min(k, cum.size)].max())

print(max(scores))

Fixed submission:
import numpy as np
 
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
 
cycles, check = [], [False]*n
for pi in p:
    if not check[pi-1]:
        curr, cycle = pi, []
        check[pi-1] = True
        while pi != p[curr-1]:
            check[curr-1] = True
            cycle.append(c[curr-1])
            curr = p[curr-1]
        check[curr-1] = True
        cycles.append(cycle + [c[curr-1]])

scores = []
for cycle in (np.array(cycle) for cycle in cycles):
    for i in range(cycle.size):
        cum = np.roll(cycle, i).cumsum()
        if cum[-1] > 0 and k > cycle.size:
            scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max())
        else:
            scores.append(cum[:min(k, cum.size)].max())

print(max(scores))
Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())
ans = 1
sev = 7

while sev%K:
  ans+=1
  sev=(10*sev+7)%K
else:
  print(ans)
  exit()

print(-1)

Fixed submission:
K = int(input())
a = 7

for n in range(1,1+K):
  if a%K==0:
    print(n)
    exit()
  a = (a*10+7)%K

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())
ans = 1
sev = 7

while sev%K:
  ans+=1
  sev=(10*sev+7)%K
else:
  print(ans)
  exit()

print(-1)

Fixed submission:
K = int(input())
a = 7

for n in range(1,1+K):
  if a%K==0:
    print(n)
    exit()
  a = (a*10+7)%K

print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = []
P = 1

for k in range(K):
  P*=A[k]
B+=[P]

for n in range(N-K):
  P = P/A[n]*A[n+K]
  B+=[P]
  
  if B[n]<B[n+1]:
    print("Yes")
  else:
    print("No")

Fixed submission:
from numpy import *
N,K = map(int,input().split())
A = log(array(list(map(int,input().split()))))
B = [sum(A[:K])]

for n in range(N-K):
  B+=[B[-1]+A[n+K]-A[n]]
  
  if B[n]<B[n+1]:
    print("Yes")
  else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(input().split())
C = []

for n in range(N):
  B = A[:]
  B.pop(n)
  C.append(eval("^".join(B)))

print(*C,sep=" ")

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
b = 0
C = []

for a in A:
  b^=a

for a in A:
  C.append(a^b)

print(*C)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))

for a in A:
  A = np.delete(A,np.where((A%a==0) & ((A!=a) | (2<=len(np.where(A==a)[0])))))

print(len(A))

Fixed submission:
N = int(input())
A = sorted(list(map(int,input().split())))
B = (10**6+1)*[0]

for a in A:
  B[a]+=1
  if B[a]==1:
    for n in range(2*a,10**6+1,a):
      B[n]+=10

print(B.count(1))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N = int(input())
A = np.array(sorted(list(map(int,input().split()))))

if 1 in A:
  A = np.delete(A,np.where((A%1==0) & ((A!=1) | (2<=len(np.where(A==1)[0])))))
if 2 in A:
  A = np.delete(A,np.where((A%2==0) & ((A!=2) | (2<=len(np.where(A==2)[0])))))

for a in A:
  A = np.delete(A,np.where((A%a==0) & ((A!=a) | (2<=len(np.where(A==a)[0])))))

print(len(A))

Fixed submission:
N = int(input())
A = sorted(list(map(int,input().split())))
B = (10**6+1)*[0]

for a in A:
  B[a]+=1
  if B[a]==1:
    for n in range(2*a,10**6+1,a):
      B[n]+=10

print(B.count(1))
Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
S = set()

for n in range(N):
  input()
  S|=set(input().split())

print(N-len(S))

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

for k in range(K):
  input()
  S|=set(input().split())

print(N-len(S))

Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
S = set()

for n in range(N):
	input()
	S|=set(input().split())

print(N-len(S))

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

for k in range(K):
  input()
  S|=set(input().split())

print(N-len(S))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
print(*[A.count(n+1) for n in range(N)],sep="\n")

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
B = N*[0]

for a in A:
  B[a-1]+=1

print(*B,sep="\n")
Buggy python submission (verdict: Runtime Error):
N,A,B = list(map(int,input().split()))
S = ""

while len(S)<=N:
  S+=A*"b"
  S+=B*"r"

print(S[:N].count("b"))

Fixed submission:
N,A,B = list(map(int,input().split()))
print(A*(N//(A+B))+min(A,N%(A+B)))
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
n = 0

for q in range(Q):
  R = input()
  
  if int(R[0])==1:
    n = (n+1)%2
  else:
    T,F,C = R.split()
    F = int(F)
    
    if n==0 and F==1:
      S = C+S
    elif n==0 and F==2:
      S = S+C
    elif n==1 and F==1:
      S = S+C
    else:
      S = C+S

if n==0:
  print(S)
else:
  print(S[::-1])

Fixed submission:
S = input()
Q = int(input())
T = ""

for q in range(Q):
  que = input().split()
  if que[0]=="1":
        S,T = T,S
  else:
    if que[1]=="1":
      T+=que[2]
    else:
      S+=que[2]

print(T[::-1]+S)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = int(input())
Q = [input().split() for n in range(N)]
F = False

for n in range(N-1):
  if F:
    F = False
    continue
  if int(Q[n][0])==1 and int(Q[n+1][0])==1:
    F = True
    continue
  else:
    if int(Q[n][0])==1:
      S = S[::-1]
    else:
      if int(Q[n][1])==1:
        S = Q[n][2]+S
      else:
        S = S+Q[n][2]

if int(Q[-1][0])==1:
  if F:
    print(S)
  else:
    print(S[::-1])
else:
  if int(Q[-1][1])==1:
    print(Q[-1][2]+S)
  else:
    print(S+Q[-1][2])

Fixed submission:
S = input()
Q = int(input())
T = ""

for q in range(Q):
  que = input().split()
  if que[0]=="1":
        S,T = T,S
  else:
    if que[1]=="1":
      T+=que[2]
    else:
      S+=que[2]

print(T[::-1]+S)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

D = deque(input())

for i in range(int(input())):
  q = input().split()
  
  if int(q[0])==1:
    D = deque(reversed(D))
  else:
    if int(q[1])==1:
      D.appendleft(q[2])
    else:
      D.append(q[2])

print(*D,sep="")

Fixed submission:
S = input()
Q = int(input())
T = ""

for q in range(Q):
  que = input().split()
  if que[0]=="1":
        S,T = T,S
  else:
    if que[1]=="1":
      T+=que[2]
    else:
      S+=que[2]

print(T[::-1]+S)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())

for i in range(Q):
  q = input().split()
  
  if int(q[0])==1:
    S = S[::-1]
  else:
    if int(q[1])==1:
      S = q[2]+S
    else:
      S = S+q[2]

print(S)

Fixed submission:
S = input()
Q = int(input())
T = ""

for q in range(Q):
  que = input().split()
  if que[0]=="1":
        S,T = T,S
  else:
    if que[1]=="1":
      T+=que[2]
    else:
      S+=que[2]

print(T[::-1]+S)
Buggy python submission (verdict: Runtime Error):
from numpy import *
N,K = map(int,input().split())
A = array(sorted(list(map(int,input().split()))))
nega = A[A<0]
zera = A[A==0]
posa = A[0<A]

def cnt(x):
  y = 0
  if x>=0:
    y+=n*len(zera)
  y+=searchsorted(A,x//posa,side="right").sum()
  y+=N*len(nega)-searchsorted(A,-(-x//nega)).sum() 
  y-=len(A[A*A<=x])
  return y//2

r = 10**18
l = -r-1

while r-l>1:
  c=(r+l)//2
  if K<=cnt(c):
    r=c
  else:
    l=c

print(r)

Fixed submission:
from numpy import *
N,K = map(int,input().split())
A = array(sorted(list(map(int,input().split()))))
nega = A[A<0]
zera = A[A==0]
posa = A[0<A]

def cnt(x):
  y = 0
  if x>=0:
    y+=N*len(zera)
  y+=searchsorted(A,x//posa,side="right").sum()
  y+=N*len(nega)-searchsorted(A,-(-x//nega)).sum() 
  y-=len(A[A*A<=x])
  return y//2

r = 10**18
l = -r-1

while r-l>1:
  c=(r+l)//2
  if K<=cnt(c):
    r=c
  else:
    l=c

print(r)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N,K = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
B = sorted([i*j for i,j in itertools.combinations(A,2)])
print(B[K-1])

Fixed submission:
from numpy import *
N,K = map(int,input().split())
A = array(sorted(list(map(int,input().split()))))
nega = A[A<0]
zera = A[A==0]
posa = A[0<A]

def cnt(x):
  y = 0
  if x>=0:
    y+=N*len(zera)
  y+=searchsorted(A,x//posa,side="right").sum()
  y+=N*len(nega)-searchsorted(A,-(-x//nega)).sum() 
  y-=len(A[A*A<=x])
  return y//2

r = 10**18
l = -r-1

while r-l>1:
  c=(r+l)//2
  if K<=cnt(c):
    r=c
  else:
    l=c

print(r)