Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys
import math
import fractions
input = sys.stdin.readline


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


a, b = map(int, input().split())
x = fractions.gcd(a, b)
check_list = [1]
i = 2
while (1):
    flag = False
    if i > x:
        break
    while(x % i == 0):
        x /= i
        flag = True

    if flag == True:
        check_list.append(i)
    i += 1

# print(check_list)

ans = 1
for check in check_list:
    if is_prime(check):
        ans += 1

print(ans)


Fixed submission:
# coding: utf-8
import sys
import math
import fractions
input = sys.stdin.readline


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


a, b = map(int, input().split())
x = fractions.gcd(a, b)
check_list = []
i = 2
"""
while (1):
    flag = False
    if i > x:
        break
    while(x % i == 0):
        x /= i
        flag = True

    if flag == True:
        check_list.append(i)
    i += 1
"""
while x % 2 == 0:
    check_list.append(2)
    x //= 2
i = 3
while i * i <= x:
    if x % i == 0:
        check_list.append(i)
        x //= i
    else:
        i += 2
if x != 1:
    check_list.append(x)

print(len(set(check_list))+1)

# print(check_list)
"""
ans = 1
for check in check_list:
    if is_prime(check):
        ans += 1

print(ans)
"""

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
from fractions import gcd
import sys
input = sys.stdin.readline

x, y = map(int, input().split())
a = min(x, y)
b = max(x, y)

check = str(bin(a ^ b))
# print(check)
ans = 0

for i in range(1, len(bin(a)) - 2):
    print(i)
    if check[-i] == '1':
        ans += 1
if ans == 0:
    ans += 1
print(ans)
print(len(bin(a)))


Fixed submission:
# coding: utf-8
import sys
import math
import fractions
input = sys.stdin.readline


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


a, b = map(int, input().split())
x = fractions.gcd(a, b)
check_list = []
i = 2
"""
while (1):
    flag = False
    if i > x:
        break
    while(x % i == 0):
        x /= i
        flag = True

    if flag == True:
        check_list.append(i)
    i += 1
"""
while x % 2 == 0:
    check_list.append(2)
    x //= 2
i = 3
while i * i <= x:
    if x % i == 0:
        check_list.append(i)
        x //= i
    else:
        i += 2
if x != 1:
    check_list.append(x)

print(len(set(check_list))+1)

# print(check_list)
"""
ans = 1
for check in check_list:
    if is_prime(check):
        ans += 1

print(ans)
"""
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)


def coupon(i, m, pay):
    if m == 0 or i == n:
        return sum(l[i:])
    for maisuu in range(1, 31):
        #print("{}に{}まい使う".format(i, maisuu))
        now = l[i] // (2 ** maisuu)
        pay = min(pay, coupon(
            i + 1, m - maisuu, pay) + now)
        if now == 0:
            break

    return pay


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

l = [int(c) for c in input().split()]
l.sort()
l.reverse()
max_pay = sum(l)
# print(l)


print(coupon(0, m, max_pay))


Fixed submission:
s = input()

if s == 'Sunny':
    print('Cloudy')
elif s == 'Cloudy':
    print('Rainy')
else:
    print('Sunny')
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)


def coupon(i, m, pay):
    if m == 0 or i == n:
        return sum(l[i:])
    for maisuu in range(1, m+1):
        #print("{}に{}まい使う".format(i, maisuu))
        now = l[i] // (2 ** maisuu)
        pay = min(pay, coupon(
            i + 1, m - maisuu, pay) + now)
        if now == 0:
            break

    return pay


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

l = [int(c) for c in input().split()]
l.sort()
l.reverse()
max_pay = sum(l)
# print(l)


print(coupon(0, m, max_pay))


Fixed submission:
import sys
import heapq
input = sys.stdin.readline
sys.setrecursionlimit(100000)

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

l = [int(c)*(-1) for c in input().split()]
# 優先度付きキューにする
heapq.heapify(l)

for _ in range(m):
    tmp = heapq.heappop(l)*(-1)
    heapq.heappush(l, (tmp // 2)*(-1))

print(sum(l)*(-1))
Buggy python submission (verdict: Runtime Error):
n, a, b, c, d = map(int, input().split())
s = input()

# c<dの場合
tooi = max(c, d)
for i in range(a, tooi - 1):
    # 二連続石は無理
    if s[i] == '#' and s[i + 1] == '#':
        print('No')
        exit()
# d<cなら飛び越え必要=b~d区間に3マス連続空きが必要
if d < c:
    for i in range(b, d + 1):
        if s[i - 1] == '.' and s[i] == '.' and s[i + 1] == '.':
            print('Yes')
            exit()
    print('No')
else:
    print('Yes')


Fixed submission:
def iwa_check(start, end):
    for i in range(start, end):
        if s[i] == '#' and s[i + 1] == '#':
            return False
    return True


n, a, b, c, d = map(int, input().split())
s = list(input())
s = ['#'] + s + ['#']

# 岩が2個並ぶ
if not iwa_check(a, c) or not iwa_check(b, d):
    print('No')
    exit()

# 追い越す場合
if c > d:
    ok = False
    for i in range(b, d + 1):
        if s[i - 1] == '.' and s[i] == '.' and s[i + 1] == '.':
            ok = True
            break
    if not ok:
        print('No')
        exit()

# 追い越さない
print('Yes')

Buggy python submission (verdict: Runtime Error):
n, a, b, c, d = map(int, input().split())
s = list(input())

# 岩が2個並ぶ
before = s[0]
for ss in s[1:]:
    if ss == before and ss == '#':
        print('No')
        exit()
    before = ss

# 追い越す場合
if c > d:
    # new_s = s[:d+1]
    # for i in range(b - 1, len(new_s) - 2):
    #     ss = new_s[i:i + 3]
    #     # print(ss)
    #     if len(set(ss)) == 1 and ss[0] == '.':
    #         print('Yes')
    #         exit()
    ok = False
    for i in range(b, d + 1):
        if s[i - 1] == '.' and s[i] == '.' and s[i + 1] == '.':
            ok = True
            break
    if not ok:
        print('No')
        exit()

# 追い越さない
print('Yes')


Fixed submission:
def iwa_check(start, end):
    for i in range(start, end):
        if s[i] == '#' and s[i + 1] == '#':
            return False
    return True


n, a, b, c, d = map(int, input().split())
s = list(input())
s = ['#'] + s + ['#']

# 岩が2個並ぶ
if not iwa_check(a, c) or not iwa_check(b, d):
    print('No')
    exit()

# 追い越す場合
if c > d:
    ok = False
    for i in range(b, d + 1):
        if s[i - 1] == '.' and s[i] == '.' and s[i + 1] == '.':
            ok = True
            break
    if not ok:
        print('No')
        exit()

# 追い越さない
print('Yes')
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    ss = s[l - 1:r]
    ss_rep = ss.replace('AC', '')
    print((len(ss) - len(ss_rep)) // 2)


Fixed submission:
import bisect
n, q = map(int, input().split())
s = input()
cnt = 0
apoint = []
cpoint = []
for i in range(len(s) - 1):
    if s[i] + s[i + 1] == 'AC':
        cnt += 1
        apoint.append(i)
        cpoint.append(i)
# print('cnt:', cnt)
# print('acpoint:', apoint)
for i in range(q):
    l, r = map(int, input().split())
    l, r = l - 1, r - 1
    left = bisect.bisect_left(apoint, l)
    right = bisect.bisect_left(cpoint, r)
    print(right - left)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
bef = len(s)
ans = 0
while(1):
    s = s.replace('10', '').replace("01", "")
    now = len(s)
    if bef == now:
        break
    else:
        ans += bef-now
        bef = now
print(ans)


Fixed submission:
s = input()

s0 = 0
s1 = 0

for c in s:
    if c == '0':
        s0 += 1
    else:
        s1 += 1

print(min(s0, s1)*2)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())

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

distance = []

for i in range(len(x) - 1):
    tmp = x[i + 1] - x[i]
    distance.append(tmp)

distance.sort()

for i in range(n - 1):
    distance.pop()

print(sum(distance))


Fixed submission:
n, m = map(int, input().split())

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

distance = []

for i in range(len(x) - 1):
    tmp = x[i + 1] - x[i]
    distance.append(tmp)

distance.sort()
if n >= m:
    print(0)
else:
    for i in range(n - 1):
        distance.pop()

    print(sum(distance))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))
inf = 1001001001
dp = [inf] * (n+1)

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(i + k):
        if i + j >= n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[n-1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))
inf = 1001001001
dp = [inf] * (n+1)

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(k):
        if i + j >= n:
            break
        dp[i + j] = min(dp[i + j], dp[i-1] + abs(h[i+j] - h[i-1]))
print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))
inf = 1001001001
dp = [inf for _ in range(n + 1)]

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(i + k):
        if i + j >= n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[n-1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))
inf = 1001001001
dp = [inf] * (n+1)

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(k):
        if i + j >= n:
            break
        dp[i + j] = min(dp[i + j], dp[i-1] + abs(h[i+j] - h[i-1]))
print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))
inf = 1001001001
dp = [inf for _ in range(n + 1)]

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(i + k):
        if i + j >= n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[n-1])


Fixed submission:
n, k = map(int, input().split())
h = list(map(int, input().split()))
inf = 1001001001
dp = [inf] * (n+1)

dp[0] = 0
dp[1] = abs(h[0]-h[1])

for i in range(n):
    for j in range(k):
        if i + j >= n:
            break
        dp[i + j] = min(dp[i + j], dp[i-1] + abs(h[i+j] - h[i-1]))
print(dp[n-1])
Buggy python submission (verdict: Runtime Error):
n = int(input())


v = [int(c) for c in input().split()]
v1 = [0 for _ in range(10**5)]
v2 = [0 for _ in range(10**5)]
for i in range(n):
    if i % 2 == 0:
        v1[v[i]] += 1
    else:
        v2[v[i]] += 1

if v1.index(max(v1)) == v2.index(max(v2)):
    m1 = sorted(set(v1))[-2]
    m2 = sorted(set(v2))[-2]
    if m1 > m2:
        print(n-m1-max(v2))
    else:
        print(n-max(v1)-m2)
else:
    print(n-max(v1)-max(v2))


Fixed submission:
import sys
from collections import defaultdict
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
tmp = list(map(int, input().split()))
# 偶奇で分けて辞書作る
odd = defaultdict(int)
even = defaultdict(int)
for i in range(n):
    if i % 2 == 0:
        odd[tmp[i]] += 1
    else:
        even[tmp[i]] += 1

# 降順ソート
odd = sorted(odd.items(), key=lambda x: -x[1])
even = sorted(even.items(), key=lambda x: -x[1])
# 各配列の最頻，二番目を取ってくる
o1 = odd[0]
e1 = even[0]
# 1種類しかない場合は適当に二番目を作成
if len(odd) != 1:
    o2 = odd[1]
else:
    o2 = (0, 0)
if len(even) != 1:
    e2 = even[1]
else:
    e2 = (0, 0)

# 最多文字が異なる
if o1[0] != e1[0]:
    print(n - (o1[1] + e1[1]))
# 最多文字が同じ
else:
    ans = min((n - (o2[1] + e1[1])), (n - (o1[1] + e2[1])))
    print(ans)
Buggy python submission (verdict: Runtime Error):
s = [i for i in input().strip()]
t = [i for i in input().strip()]

s.sort()
t.sort(reverse=True)

#print(s)
#print(t)

for i in range(len(s)):
  if s[i] < t[i]:
    print("Yes")
    exit()
    
print("No")    

Fixed submission:
s = [i for i in input().strip()]
t = [i for i in input().strip()]

s.sort()
t.sort(reverse=True)

#print(s)
#print(t)

if s < t:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
s = list(input())
t = list(input())
len_t = len(t)

for i in range(len(s) - len(t) + 1):
    tmps = s[i:i + len(t)]
    ok = True
    for j in range(len(tmps)):
        if t[j] == tmps[j] or tmps[j] == '?':
            pass
        else:
            ok = False
            break
    if ok == True:
        for k in range(len(t)):
            s[k+i] = t[k]
        break

if ok == False:
    print("UNRESTORABLE")
else:
    for c in s:
        if c == '?':
            print('a', end="")
        else:
            print(c, end="")
    print()


Fixed submission:
import copy

s = list(input())
t = list(input())
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()

ans_list = []
for i in range(len(s) - len(t) + 1):
    tmps = s[i:i + len(t)]
    ok = True
    for j in range(len(tmps)):
        if t[j] == tmps[j] or tmps[j] == '?':
            pass
        else:
            ok = False
            break
    if ok == True:
        cp_s = copy.deepcopy(s)
        for k in range(len(t)):
            cp_s[k+i] = t[k]
        ans_list.append(cp_s)

if len(ans_list) == 0:
    print("UNRESTORABLE")
else:
    # ?をaに書き換え
    for i in range(len(ans_list)):
        for j in range(len(s)):
            if ans_list[i][j] == '?':
                ans_list[i][j] = 'a'
    ans_list.sort()
    for c in ans_list[0]:
        print(c, end="")
    print()
Buggy python submission (verdict: Runtime Error):
s = input()
sr = s[-1::-1]
left = 0
right = 0
ans = 0
while (1):
    if left + right == len(s) - 1:
        break
    if s[left] != sr[right]:
        if s[left] == 'x':
            left += 1
        elif sr[right] == 'x':
            right += 1
        else:
            print(-1)
            exit()
        ans += 1
    elif s[left] == sr[right]:
        left += 1
        right += 1

print(ans)


Fixed submission:
s = input()
sr = s[-1::-1]
left = 0
right = 0
ans = 0
while (1):
    if left + right >= len(s) - 1:
        break
    if s[left] != sr[right]:
        if s[left] == 'x':
            left += 1
        elif sr[right] == 'x':
            right += 1
        else:
            print(-1)
            exit()
        ans += 1
    elif s[left] == sr[right]:
        left += 1
        right += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
check = [0]
ans = 0
now = 1
while (1):
    now = l[now - 1]
    ans += 1

    if now == 2:
        print(ans)
        exit()
    if now in check:
        print(-1)
        exit()
    check.append(now)


Fixed submission:
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
ans = 0
now = 1
while (1):
    now = l[now - 1]
    ans += 1

    if now == 2:
        print(ans)
        exit()
    if ans > n:
        print(-1)
        exit()
Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
count = 0
k += 1
for x in range(k):
    for y in range(k):
        for z in range(k):
            if x + y + z == s:
                count += 1

print(count)


Fixed submission:
k, s = map(int, input().split())
count = 0
for x in range(k+1):
    for y in range(k+1):
        if s - x - y <= k and s-x-y >= 0:
            #print(x, y)
            count += 1

print(count)
Buggy python submission (verdict: Runtime Error):
a = list(map,int(input().split()))
a.sort()

if a[0]+a[1] == a[2]:
  print('Yes')
else:
  print('No')

Fixed submission:
a = list(map(int,input().split()))
a.sort()

if a[0]+a[1] == a[2]:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=0
for i in range(1,n):
    for j in range(1,n):
        if i*j<n:
            s+=1
print(s)    

Fixed submission:
n=int(input())
s=[n-1]
for i in range(2,n):
    s.append((n-1)//i)
t=0    
for j in s:
    t+=j
print(t)
Buggy python submission (verdict: Runtime Error):
s,t=input().split()
u=0
for i in range(0,len(s)):
    if s[i]!=t[i]:
        u+=1
print(u)


Fixed submission:
s=input()
t=input()
u=0
for i in range(0,len(s)):
    if s[i]!=t[i]:
        u+=1
print(u)
Buggy python submission (verdict: Runtime Error):
A,B,X=map(int,input().split())
if A<=X<=A+B:
    print(YES)
else:
    print(NO)

Fixed submission:
A,B,X=map(int,input().split())
if A<=X<=A+B:
    print('YES')
else:
    print('NO')
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: Runtime Error):
import functools
import operator
n = int(input())
a = list(map(int,input().split()))
c = functools.reduce(operator.mul,a)
if c > 10**18:
    print(-1)
else:
    print(c)

Fixed submission:
from decimal import Decimal
a,b = input().split()
a=int(a)
b=Decimal(str(b))
c = a*b

print(int(c))
Buggy python submission (verdict: Runtime Error):
N = input()
N = int(N)
l = list(int(input().split))
l.sort()
sum = 0
for i in range(N-1):
    sum += l[i]
    
if sum >= l[N-1]:
    print('No')
else:
    print('Yes')





Fixed submission:
N = input()
N = int(N)
l = list(map(int,input().split()))
l.sort()
sum = 0
for i in range(N-1):
    sum += l[i]
    
if sum > l[N-1]:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
N = input();
l = list(int(input().split))
l.sort()
sum = 0
for i in range(N-1):
    sum += l[i]
    
if sum >= l[N-1]:
    print('No')
else:
    print('Yes')





Fixed submission:
N = input()
N = int(N)
l = list(map(int,input().split()))
l.sort()
sum = 0
for i in range(N-1):
    sum += l[i]
    
if sum > l[N-1]:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

times = 1
for i in range(K):
    times *= A[i]
# print(times)

for i in range(K , N):
    pre_times = times
    # print(i)
    # print(i)
    # print(A[i - K])
    # print(A[i])
    times = times // A[i - K ]
    times *= A[i]
    # print(times)
    if pre_times < times:
        print("Yes")
    else:
        print("No")

Fixed submission:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

for i in range(K, N):
    if A[i - K] < A[i]:
        print("Yes")
    else:
        print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())


def prime_numbers(n):
    """
    n以下の素数列挙
    O(n)
    """
    eratosthenes = [2] * (n + 1)
    eratosthenes[1] = 1
    num_sum = 1
    for i in range(2, n // 2 + 1):
        k = 2
        while True:
            if i * k < n + 1:
                eratosthenes[i * k] += 1
                k += 1
            else:
                break

    return eratosthenes, num_sum


eratos, num_sum = prime_numbers(N)
ans = 0
for i in range(N):
    ans += eratos[i+1]*(i+1)
print(ans)

Fixed submission:
N = int(input())


def prime_numbers(n):
    """
    n以下の素数列挙
    O(n)
    """
    eratosthenes = [2] * (n + 1)
    eratosthenes[1] = 1
    num_sum = 1
    for i in range(2, n // 2 + 1):
        k = 2
        while True:
            if i * k < n + 1:
                eratosthenes[i * k] += 1
                k += 1
            else:
                break

    return eratosthenes, num_sum


eratos, num_sum = prime_numbers(N)
ans = 0
for i in range(N):
    ans += eratos[i+1]*(i+1)
print(ans)
Buggy python submission (verdict: Runtime Error):
import math
A ,B = list(map(int,input().split()))

def problem_koganemaru(A,B):  
  for price in range(10000):
    if math.ceil(price*0.08) == A and math.ceil(price*0.1) == B:
      return price
  return -1

print(problem_koganemru(A,B))

Fixed submission:
import math
A ,B = list(map(int,input().split()))
 
def problem_koganemaru(A,B):  
  for price in range(10000):
    if math.floor(price*0.08) == A and math.floor(price*0.1) == B:
      return price
  return -1
 
print(problem_koganemaru(A,B))

Buggy python submission (verdict: Runtime Error):
A ,B = list(map(int,input().split()))
def problem_koganemaru(A,B):  
  # x =  A / (0.08) 
  # y =  B / (0.10) 
  # print(x,y)
  # if x == y:
  #   return x
  # else:
  #   return -1
  for price in range(10000):
    if math.ceil(price*0.08) == A and math.ceil(price*0.1) == B:
      return price
  return -1
print(problem_koganemaru(A,B))

Fixed submission:
import math
A ,B = list(map(int,input().split()))
 
def problem_koganemaru(A,B):  
  for price in range(10000):
    if math.floor(price*0.08) == A and math.floor(price*0.1) == B:
      return price
  return -1
 
print(problem_koganemaru(A,B))
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
# print("Q=")
Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 # 偶数のとき正常, 奇数の時反転
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      check_reverse += 1
      # S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if check_reverse % 2 == 0: #反転していない時:
        if int(query[1]) == 2:
          S = S + query[2]
        else:
          S = query[2] + S
      else: #反転している時
        if int(query[1]) == 1:
          S = S + query[2]
        else:
          S = query[2] + S
  if check_reverse % 2==1:
    S = S[::-1]
  return S
print(problem_gksn(S,Q))


Fixed submission:
S = input()
# print("Q=")
Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 # 偶数のとき正常, 奇数の時反転
  atama = ""
  oshiri = ""
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      check_reverse += 1
      # S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if int(query[1]) == 2 - check_reverse % 2:
        oshiri += query[2]
      else:
        atama += query[2]
      # print("atama",atama)
      # print("oshiri",oshiri)
  S = atama[::-1] + S + oshiri
  if check_reverse % 2==1:
    S = S[::-1]
  
  return S

print(problem_gksn(S,Q))

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

def problem_gksn(S,Q):
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if int(query[1]) == 2:
        S = S + query[2]
      else:
        S = query[2] + S
  
  return S
print(problem_gksn(S,Q))

Fixed submission:
S = input()
# print("Q=")
Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 # 偶数のとき正常, 奇数の時反転
  atama = ""
  oshiri = ""
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      check_reverse += 1
      # S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if int(query[1]) == 2 - check_reverse % 2:
        oshiri += query[2]
      else:
        atama += query[2]
      # print("atama",atama)
      # print("oshiri",oshiri)
  S = atama[::-1] + S + oshiri
  if check_reverse % 2==1:
    S = S[::-1]
  
  return S

print(problem_gksn(S,Q))