Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
'''
def main():
    N, D = map(int, input().split(" "))
    xy = []
    for i in range(N):
        xy.append(list(map(int, input().split(" "))))
    D2 = D **2
    cnt = 0
    for i in range(N):
        if xy[i][0] **2 + xy[i][1] **2 > D2:
            continue
        cnt += 1
    print(cnt)
'''
#if,contiueの使い方による実行時間の比較用
def main():
    N, D = map(int, input().split(" "))
    xy = []
    for i in range(N):
        xy.append(list(map(int, input().split(" "))))
    D2 = D **2
    cnt = 0
    for i in range(N):
        if xy[i][0] **2 + xy[i][1] **2 <= D2:
            cnt += 1
    print(cnt)

if __name__ == "__main__":
    main()

Fixed submission:

def main():
    N = int(input())
    L = list(map(int, input().split(" ")))
    cnt = 0
    for i in range(N-2):
        for j in range(i + 1, N-1):
            for k in range(j + 1, N):
                l1 = L[i]
                l2 = L[j]
                l3 = L[k]
                l_max = max(l1, l2, l3)
                if l1 == l2 or l2 == l3 or l3 == l1:
                    continue
                if (l1+l2+l3) - l_max <= l_max:
                    continue
                cnt += 1
    print(cnt)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    q = int(input())
    bc = []
    for _ in range(q):
        bc.append(list(map(int, input().split(" "))))
    for i in range(q):
        for j in range(n):
            if bc[i][0] == a[j]:
                a[j] = bc[i][1]
        print(sum(a))
        
    


if __name__ == "__main__":
    main()

Fixed submission:
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    q = int(input())
    bc = []
    sum1 = sum(a)
    for _ in range(q):
        bc.append(list(map(int, input().split(" "))))
    d = defaultdict(lambda:0)
    for i in range(n):
        d[a[i]] += 1

    for i in range(q):
        if bc[i][0] in d:
            sum1 += (bc[i][1] - bc[i][0]) * d[bc[i][0]]
            d[bc[i][1]] += d[bc[i][0]]
            del d[bc[i][0]] 
        print(sum1)
        

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):

def main():
    x, n = map(int, input().split(" "))
    p =[]
    dif = 1e2
    ans = 0
    l_a = [i for i in range(101)]
    if n != 0:
        p = list(map(int, input().split(" ")))
    for ele in l_a[::-1]:
        if abs(x - ele) <= dif and ele not in p:
            ans = ele
            dif = abs(x - ele)
    print(ans)
if __name__ == "__main__":
    main()

Fixed submission:

def main():
    x = list(map(int, input().split(" ")))
    for i in range(5):
        if x[i] == 0:
            print(i +1)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):

def main():
    n, m, x = map(int, input().split(" "))
    ca =[]
    for i in range(n):
        ca.append(list(map(int, input().split(" "))))
    min_c = 1e6
    for i in range(1<<n):
        a = [0]*(m-1)
        c = 0
        for j in range(n):
            if i >> j & 1 ==1:
                for k in range(1,m):
                    a[k-1] += ca[j][k]
                c += ca[j][0]
        if min(a) >= x and c <min_c:
            min_c = c
    if min_c == 1e6: print(-1)
    else:print(min_c)


if __name__ == "__main__":
    main()

Fixed submission:

def main():
    n, m, x = map(int, input().split(" "))
    ca =[]
    a = []
    c = []
    ans = 1e9
    for i in range(n):
        ca.append(list(map(int, input().split(" "))))
    for i in range(n):
        c.append(ca[i][0])
        a.append(ca[i][1:])
    for i in range(1<<n):
        a_sum = [0]*m
        c_sum = 0
        for j in range(n):
            if i >> j & 1 == 1:
                for k in range(m):
                    a_sum[k] += a[j][k]
                c_sum += c[j]
        if min(a_sum) >= x and c_sum < ans:
            ans = c_sum
    if ans == 1e9:
        print(-1)
    else:
        print(ans)
        

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    s = []
    ord = [-1]*(n)
    u = 1
    while ord[u] == -1:
        ord[u] = len(s)
        s.append(u)
        u = a[u-1]
    l = len(s) - ord[u]
    if k < ord[u]:
        print(s[k])
    else:
        print(s[(k-ord[u])%l+ord[u]])

'''
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]

    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break

    cc = s.index(bb)
    s[cc]=-1
    dd = s.index(bb)
    loop_len = dd-cc
    s[cc]=s[dd]
        
    if bb ==None or k < cc:
        print(s[k]+1)
    else:
        y = (k-cc) % loop_len
        print(s[y+cc]+1)
'''
if __name__ == "__main__":
    main()

Fixed submission:
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]

    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break

    cc = s.index(bb)
    s[cc]=-1
    dd = s.index(bb)
    loop_len = dd-cc
    s[cc]=s[dd]
        
    if bb ==None or k < cc:
        print(s[k]+1)
    else:
        y = (k-cc) % loop_len
        print(s[y+cc]+1)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):

def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    u = [0]
    f = True
    loop_len = 1
    z = 0
    x = a[0]
    while f:
        if x in s:
            f = False
            b=s.index(x)
            s = s[b:]
            loop_len = len(s) - b
            z = s.index(x)
        else:
            s.append(x)
            x = a[x]
    y = (k-z) % loop_len
    print(s)
    print(s[y]+1)

    



if __name__ == "__main__":
    main()

Fixed submission:
from collections import defaultdict
def main():
    n, k = map(int, input().split(" "))
    a = list(map(lambda i: int(i)-1, input().split(" ")))
    s = [0]
    d = defaultdict(lambda:0)
    x = a[0]
    for i in range(n):
        s.append(x)
        x = a[x]

    bb=None
    for i in range(n):
        d[s[i]] += 1
        if d[s[i]] ==2:
            bb=s[i]
            break

    cc = s.index(bb)
    s[cc]=-1
    dd = s.index(bb)
    loop_len = dd-cc
    s[cc]=s[dd]
        
    if bb ==None or k < cc:
        print(s[k]+1)
    else:
        y = (k-cc) % loop_len
        print(s[y+cc]+1)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):

def gcd(p, q):
    if(p%q ==0): 
        return q
    return gcd(q, p%q)

def main():
    ans = 0
    k = int(input())
    for i in range(k):
        for j in range(k):
            for l in range(k):
                ans += gcd(i+1,gcd(j+1,l+1))
    print(ans)


if __name__ == "__main__":
    main()

Fixed submission:
from math import gcd
def main():
    ans = 0
    k = int(input())
    for i in range(k):
        for j in range(k):
            for l in range(k):
                ans += gcd(i+1,gcd(j+1,l+1))
    print(ans)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import math
def main():
    ans = 0
    k = int(input())
    for i in range(k):
        for j in range(k):
            for l in range(k):
                ans += math.gcd(i+1,math.gcd(j+1,l+1))
    print(ans)


if __name__ == "__main__":
    main()

Fixed submission:
from math import gcd
def main():
    ans = 0
    k = int(input())
    for i in range(k):
        for j in range(k):
            for l in range(k):
                ans += gcd(i+1,gcd(j+1,l+1))
    print(ans)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n = int(input())
    s = input()
    cnt = 0
    for i in range(len(s)):
        x = s[i]
        for j in range(i+1, len(s)):
            y = s[j]
            if x == y:
                continue
            for k in range(j+1, len(s)):
                z = s[k]
                if z == x or z == y:
                    continue
                if j - i == k - j:
                    continue
                cnt += 1
                # print(f"{i} {j} {k}")
    print(cnt)

if __name__ == "__main__":
    main()

Fixed submission:

def main():
    '''
    #このコードだと、tleになる
    n = int(input())
    s = input()
    cnt = 0
    for i in range(len(s)):
        x = s[i]
        for j in range(i+1, len(s)):
            y = s[j]
            if x == y:
                continue
            for k in range(j+1, len(s)):
                z = s[k]
                if z == x or z == y:
                    continue
                if j - i == k - j:
                    continue
                cnt += 1
                # print(f"{i} {j} {k}")
    print(cnt)
    '''
    n = int(input())
    s = input()
    cnt = [0]*3
    ans = 1
    for i in s:
        if i == 'R':cnt[0]+=1
        elif i == 'G':cnt[1]+=1
        else:cnt[2]+=1
    for i in cnt:
        ans *= i
    for i in range(n):
        for j in range(i+1,n):
            k = j + (j - i)
            if k < n:
                if s[i] == s[j]: continue
                if s[j] == s[k]: continue
                if s[k] == s[i]: continue
                ans -= 1
    print(ans)



if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    d = defaultdict(lambda:0)
    d2 = {}
    for i in a:
        d[i] += 1
    for i,j in d.items():
        x = j*(j-1)//2
        d2[i] = x
    ans = 0
    for i,j in d.items():
        ans = sum(d2.values())
    for i in range(n):
        print(ans-d[a[i]]+1)

if __name__ == "__main__":
    main()

Fixed submission:
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    d = defaultdict(lambda:0)
    d2 = {}
    for i in a:
        d[i] += 1
    for i,j in d.items():
        x = j*(j-1)//2
        d2[i] = x
    ans = 0
    ans = sum(d2.values())
    for i in range(n):
        print(ans-d[a[i]]+1)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n, k = map(int, input().split(" "))
    p = list(map(int, input().split(" ")))
    u = []
    v = []
    for i in range(n-k+1):
        u.append(sum(p[i:i+k]))
    x = u.index(max(u))
    for i in range(k):
        v.append(p[x+i])
    a=0
    for i in range(k):
        a += 1+0.5*(v[i]-1)
    print(a)


if __name__ == "__main__":
    main()

Fixed submission:

def main():
    n, k = map(int, input().split(" "))
    p = list(map(int, input().split(" ")))
    u = [0]
    v = []
    w = []
    for i in range(n):
        u.append(u[i]+p[i])
    for i in range(n-k+1):
        w.append(u[k+i]-u[i])
    x = w.index(max(w))
    for i in range(k):
        v.append(p[x+i])
    a=0
    for i in range(k):
        a += 1+0.5*(v[i]-1)
    print(a)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n, k = map(int, input().split(" "))
    p = list(map(int, input().split(" ")))
    ans = []
    for i in range(n-k+1):
        x = p[i:i+k]
        if sum(ans) < sum(x):
            ans = x
    a=0
    for i in range(k):
        a += 1+0.5*(ans[i]-1)
    print(a)


if __name__ == "__main__":
    main()

Fixed submission:

def main():
    n, k = map(int, input().split(" "))
    p = list(map(int, input().split(" ")))
    u = [0]
    v = []
    w = []
    for i in range(n):
        u.append(u[i]+p[i])
    for i in range(n-k+1):
        w.append(u[k+i]-u[i])
    x = w.index(max(w))
    for i in range(k):
        v.append(p[x+i])
    a=0
    for i in range(k):
        a += 1+0.5*(v[i]-1)
    print(a)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n, k = map(int, input().split(" "))
    p = list(map(int, input().split(" ")))
    ans = []
    for i in range(n-k+1):
        x = p[i:i+k]
        if sum(ans) < sum(x):
            ans = x
    a=0
    for i in range(k):
        b=0
        for j in range(ans[i]):
            b += j+1
        a += b/ans[i]
    print(a)


if __name__ == "__main__":
    main()

Fixed submission:

def main():
    n, k = map(int, input().split(" "))
    p = list(map(int, input().split(" ")))
    u = [0]
    v = []
    w = []
    for i in range(n):
        u.append(u[i]+p[i])
    for i in range(n-k+1):
        w.append(u[k+i]-u[i])
    x = w.index(max(w))
    for i in range(k):
        v.append(p[x+i])
    a=0
    for i in range(k):
        a += 1+0.5*(v[i]-1)
    print(a)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n = int(input())
    p = list(map(int, input().split(" ")))
    ans = 0
    for i in range(n):
        if p[i] <= min(p[:i+1]):
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:

def main():
    n = int(input())
    p = list(map(int, input().split(" ")))
    ans = 0
    min = 2e5+1
    for i in range(n):
        if min > p[i]:
            min = p[i]
        if p[i] <= min:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
N = int(input())
l = [0] * N
l = list(map(int, input().split()))
sum = 0
a = [0] * N
for i in range(N):
    sum += l[i]
x = 0
result = 1145141919810931931
for i in range(N):
    x += l[i]
    if i + 1 < N:
        result = min((result, abs(sum - 2 * x)))
print(result)


Fixed submission:
a, b = list(map(int, input().split()))
if a % 3 == 0 or b % 3 == 0 or (a + b) % 3 == 0:
    print("Possible")
else:
    print("Impossible")

Buggy python submission (verdict: Runtime Error):
N = int(input())
l = [0] * N
l = list(map(int, input().split()))
sum = 0
a = [0] * N
for i in range(N):
    sum += l[i]
x = 0
result = 1145141919810931931
for i in range(N):
    x += l[i]
    result = min((result, abs(sum - 2 * x)))
print(result)


Fixed submission:
a, b = list(map(int, input().split()))
if a % 3 == 0 or b % 3 == 0 or (a + b) % 3 == 0:
    print("Possible")
else:
    print("Impossible")
Buggy python submission (verdict: Runtime Error):
nums = [0]
n = int(input())
nums *= n
nums = list(map(int,input().split()))
max = 0
min = 1000
for i in range(n):
    if nums[i]<min:
        min = nums[i]
    if max<list[i]:
        max = nums[i]
print(max-min)


Fixed submission:
nums = [0]
n = int(input())
nums *= n
nums = list(map(int,input().split()))
max = 0
min = 1000
for i in range(n):
    if nums[i]<min:
        min = nums[i]
    if max<nums[i]:
        max = nums[i]
print(max-min)
Buggy python submission (verdict: Runtime Error):
N = int(input())
dp = [0] * 701
dp[0] = 1
target = [3, 4, 5, 6, 7, 8, 9]
for i in range(0, 600):
    for t in target:
        dp[i+t] += dp[i]
print(dp[N])


Fixed submission:
N = int(input())
MOD = 10 ** 9 + 7
dp = [0] * 2010
dp[0] = 1
for i in range(3, N + 1):
    for j in range(i - 3 + 1):
        dp[i] = (dp[i] + dp[j]) % MOD
print(dp[N])
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
friends = dict()
for i in range(M):
    a, b = map(int, input().split())
    if friends.get(a) is None:
        friends[a] = set([b])
    else:
        friends[a].add(b)
    if friends.get(b) is None:
        friends[b] = set([a])
    else:
        friends[b].add(a)
ans = max([len(a) for a in friends.values()]) + 1
#print(friends)
print(ans)


Fixed submission:
from collections import deque

n, m = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(m)]

neighbor = [[] for _ in range(n)]
for edge in edges:
    neighbor[edge[0] - 1].append(edge[1] - 1)  # 1- → 0-index
    neighbor[edge[1] - 1].append(edge[0] - 1)

#print(neighbor)
visited = [-1] * n
ans = 1
for idx in range(n):
    if visited[idx] == -1:
        queue = deque()
        queue.append(idx)
        group_population = 1
        visited[idx] = 0
        while len(queue) > 0:
            vertex = queue.popleft()
            for nei in neighbor[vertex]:
                if visited[nei] == -1:
                    visited[nei] = 0
                    queue.append(nei)
                    group_population += 1

    ans = max(ans, group_population)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))

ans = 0
A.sort(reverse=True)
#print(A)
for i in range(1,N):
    if i == 1:
        ans += A.pop(0)
        continue
    if i % 2 == 0:
        ans += A[0]
    else:
        ans += A.pop(0)
#print(A)
print(ans)



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

ans = 0
A.sort(reverse=True)
#print(A)
from collections import deque
d = deque(A)
for i in range(1,N):
    if i == 1:
        ans += d.popleft()
        continue
    if i % 2 == 0:
        ans += d[0]
    else:
        ans += d.popleft()
#print(A)
print(ans)
Buggy python submission (verdict: Runtime Error):
A, B = input().split()
A = int(A)
fb = float(B)
B100 = int(fb * 100)
ans = A * B100
print(int(ans/100))

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
ans = 1
A.sort(reverse=True)
t = A.pop()
if t == 0:
    print(0)
    exit(0)

A.append(t)

for i in range(N):
    n = A[i]
    if ans * n > 10 ** 18:
        print(-1)
        exit(0)
    else:
        ans *= n
print(ans)
Buggy python submission (verdict: Runtime Error):
sadf

Fixed submission:
A, B, H, M = map(int, input().split())

import math

M_rad = math.radians(360 * (M / 60))
H_rad = math.radians(30 * (M / 60) + 360 * (H / 12))

AB = abs(M_rad - H_rad)
tmp = A ** 2 + B ** 2 - 2 * A * B * math.cos(AB)
ans = math.pow(tmp, 1 / 2)
print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
routes = list(map(int, input().split()))

otozureta = set([1])
otozureta_list = [1]


def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index
#print(otozureta)

if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + ((K - loop_made_length) % loop_length)])


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

otozureta = set([1])
otozureta_list = [1]

import sys
sys.setrecursionlimit(1000000)

def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index
#print(otozureta)

if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + ((K - loop_made_length) % loop_length)])

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

otozureta = set([1])
otozureta_list = [1]


def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index
print(otozureta)

if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + (K % loop_length) - 1])


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

otozureta = set([1])
otozureta_list = [1]

import sys
sys.setrecursionlimit(1000000)

def search(list, next):
    if not otozureta.isdisjoint(set([next])):
        return next
    else:
        otozureta.add(next)
        otozureta_list.append(next)
        return search(list, routes[next - 1])


target = search(routes, routes[0])
target_index = otozureta_list.index(target)

otozureta_list_len = len(otozureta_list)
loop_length = otozureta_list_len - target_index
loop_made_length = target_index
#print(otozureta)

if K < otozureta_list_len:
    print(otozureta_list[K])
else:

    print(otozureta_list[loop_made_length + ((K - loop_made_length) % loop_length)])
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
T = [input().split() for i in range(Q)]

cnt = 0
for i in range(Q):
    if T[i][0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 0:
            if T[i][1] == '1':
                S = T[i][2] + S
            else:
                S = S + T[i][2]
        else:
            if T[i][1] == '2':
                S = T[i][2] + S
            else:
                S = S + T[i][2]
if cnt % 2 == 0:
    print(S)
else:
    print(S[::-1])

Fixed submission:
from collections import deque
# N = int(input())
S = input()
Q = int(input())
T = [input().split() for i in range(Q)]
d = deque(S)
cnt = 0
for i in range(Q):
    if T[i][0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 0:
            if T[i][1] == '1':
                d.appendleft(T[i][2])
            else:
                d.append(T[i][2])
        else:
            if T[i][1] == '2':
                d.appendleft(T[i][2])
            else:
                d.append(T[i][2])
if cnt % 2 == 0:
    print(''.join(d))
else:
    print(''.join(d)[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
# N = int(input())
S = input()
Q = int(input())
T = [input().split() for i in range(Q)]

for t in T:
    if t[0] == '1':
        S = S[::-1]
    else:
        if t[1] == '1':
            S = t[2] + S
        else:
            S = S + t[2]

print(S)

Fixed submission:
from collections import deque
# N = int(input())
S = input()
Q = int(input())
T = [input().split() for i in range(Q)]
d = deque(S)
cnt = 0
for i in range(Q):
    if T[i][0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 0:
            if T[i][1] == '1':
                d.appendleft(T[i][2])
            else:
                d.append(T[i][2])
        else:
            if T[i][1] == '2':
                d.appendleft(T[i][2])
            else:
                d.append(T[i][2])
if cnt % 2 == 0:
    print(''.join(d))
else:
    print(''.join(d)[::-1])

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

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

print(S)

Fixed submission:
from collections import deque
# N = int(input())
S = input()
Q = int(input())
T = [input().split() for i in range(Q)]
d = deque(S)
cnt = 0
for i in range(Q):
    if T[i][0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 0:
            if T[i][1] == '1':
                d.appendleft(T[i][2])
            else:
                d.append(T[i][2])
        else:
            if T[i][1] == '2':
                d.appendleft(T[i][2])
            else:
                d.append(T[i][2])
if cnt % 2 == 0:
    print(''.join(d))
else:
    print(''.join(d)[::-1])