Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from random import shuffle

n = int(input())
a = list(map(int, input().split()))

b = []
c = []

f = a.count(2)

a = [i for i in a if i != 2]

for i in range(len(a)):
    if a[i] % 4:
        b.append(a[i])
    else:
        c.append(a[i])

d = []

while b and c:
    d.append(b.pop())
    d.append(c.pop())

e = d.copy()

d.extend(b)
d.extend([2] * f)
d.extend(c)

for i in range(1, n):
    if (d[i] * d[i-1]) % 4:
        break
else:
    print('Yes')
    exit()

e.extend(b)
e.extend(c)
e.extend([2] * f)

for i in range(n - 1):
    if (e[i] * e[i+1]) % 4:
        break
else:
    print('Yes')
    exit()

e.sort()

for i in range(n - 1):
    if (e[i] * e[i+1]) % 4:
        break
else:
    print('Yes')
    exit()

for _ in range(100):
    shuffle(e)

    for i in range(n - 1):
        if (e[i] * e[i+1]) % 4:
            break
    else:
        print('Yes')
        break

print('No')

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

c0 = c1 = c2 = 0

for i in a:
    if i % 4 == 0:
        c0 += 1
    elif i % 2:
        c1 += 1
    else:
        c2 += 1

if c2:
    if c1 <= c0:
        print('Yes')
    else:
        print('No')
else:
    if c1 <= c0 + 1:
        print('Yes')
    else:
        print('No')

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

b = []
c = []

f = a.count(2)

a = [i for i in a if i != 2]

for i in range(len(a)):
    if a[i] % 4:
        b.append(a[i])
    else:
        c.append(a[i])

d = []

while b and c:
    d.append(b.pop())
    d.append(c.pop())

e = d.copy()

d.extend(b)
d.extend([2] * f)
d.extend(c)

for i in range(1, n):
    if (d[i] * d[i-1]) % 4:
        break
else:
    print('Yes')
    exit()

e.extend(b)
e.extend(c)
e.extend([2] * f)

for i in range(1, n):
    if (e[i] * e[i-1]) % 4:
        print('No')
        break
else:
    print('Yes')
    exit()


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

c0 = c1 = c2 = 0

for i in a:
    if i % 4 == 0:
        c0 += 1
    elif i % 2:
        c1 += 1
    else:
        c2 += 1

if c2:
    if c1 <= c0:
        print('Yes')
    else:
        print('No')
else:
    if c1 <= c0 + 1:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n = input()
l = map(int, raw_input().split())
ans = 1
for i in l:
    if (ans * i) <= 1e18:
        ans = ans * i
    else:
        ans = -1
print(ans) 

Fixed submission:
n = input()
l = map(int, raw_input().split())
ans = 1
for i in l:
    if i == 0:
        ans = 0
if (ans != 0):
    for i in l:
        if (ans * i) <= 1e18:
            ans = ans * i
        else:
            ans = -1
            break
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

A = sorted(A)[::-1]

ans = 0
for i, a in enumerate(A):
    if i==0:
        first = a
    elif i==1:
        ans += first
        pre = [A[1], A[1]]
    else:
        ans += pre[0]
        pre = pre[1:]
        pre.append(a)
        pre.append(a)

print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

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

A = sorted(A)[::-1]

ans = 0
for i, a in enumerate(A):
    if i==0:
        ans += a
        itr = 2
        if itr >= N:
            break
    else:
        ans += a
        itr += 1
        if itr >= N:
            break
        ans += a
        itr += 1
        if itr >= N:
            break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

A = sorted(A)[::-1]

ans = 0
for i, a in enumerate(A):
    if i==0:
        first = a
    elif i==1:
        ans += first
        pre = [A[1], A[1]]
    else:
        ans += pre[0]
        pre = pre[1:]
        pre.append(a)
        pre.append(a)

print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

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

A = sorted(A)[::-1]

ans = 0
for i, a in enumerate(A):
    if i==0:
        ans += a
        itr = 2
        if itr >= N:
            break
    else:
        ans += a
        itr += 1
        if itr >= N:
            break
        ans += a
        itr += 1
        if itr >= N:
            break

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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


large = 10**10
time = 0
ans = 0
while len(A) != 0 or len(B) != 0:
    a = large
    b = large

    if len(A)!=0:
        a = A[0]
    if len(B)!=0:
        b = B[0]

    if a < b:
        if time+a > K:
            break
        time += a
        A = A[1:]
        ans += 1
    elif a > b:
        if time+b > K:
            break
        time += b
        B = B[1:]
        ans += 1


print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

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


new_A = [0]
pre = 0
for a in A:
    if a > K:
        break
    pre += a
    if pre > K:
        break
    new_A.append(pre)

new_B = [0]
pre = 0
for b in B:
    if b > K:
        break
    pre += b
    if pre > K:
        break
    new_B.append(pre)

ans = 0
j = len(new_B) - 1

for i, a in enumerate(new_A):
    while new_B[j] > K - a:
        j -= 1
    ans = max(ans, i+j)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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


large = 10**10
time = 0
ans = 0
while len(A) != 0 or len(B) != 0:
    a = large
    b = large

    if len(A)!=0:
        a = A[0]
    if len(B)!=0:
        b = B[0]

    if a < b:
        if time+a > K:
            break
        time += a
        A = A[1:]
        ans += 1
    elif a > b:
        if time+b > K:
            break
        time += b
        B = B[1:]
        ans += 1


print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

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


new_A = [0]
pre = 0
for a in A:
    if a > K:
        break
    pre += a
    if pre > K:
        break
    new_A.append(pre)

new_B = [0]
pre = 0
for b in B:
    if b > K:
        break
    pre += b
    if pre > K:
        break
    new_B.append(pre)

ans = 0
j = len(new_B) - 1

for i, a in enumerate(new_A):
    while new_B[j] > K - a:
        j -= 1
    ans = max(ans, i+j)

print(ans)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-


def cal_xor(xor_list, rm_ind):
    ans = None
    for i, xi in enumerate(xor_list):
        if i!=rm_ind:
            if ans is not None:
                ans = ans^xi
            else:
                ans = xi
    return ans

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

ans_str = ''

if N==2:
    print("{} {}".format(a[1], a[0]))
else:

    xors = []
    for i in range(N//2):
        xors.append(a[2*i]^a[2*i+1])

    for i, ai in enumerate(a):
        sum_xor = cal_xor(xors, i//2)
        if i%2==0:
            ind = i+1
        else:
            ind = i-1

        ans = sum_xor^a[ind]
        ans_str += str(ans)+' '
    print(ans_str[:-1])


Fixed submission:
# -*- coding: utf-8 -*-

N, = map(int, input().split())
A = list(map(int, input().split()))
Q, = map(int, input().split())
B = []
C = []
for _ in range(Q):
    Bi, Ci = map(int, input().split())
    B.append(Bi)
    C.append(Ci)

e_dict = {}
for a in A:
    if a in e_dict.keys():
        e_dict[a] += 1
    else:
        e_dict[a] = 1

ans = sum(A)

for b, c in zip(B, C):
    keys = e_dict.keys()
    if b in keys:
        poped = e_dict.pop(b)
        gap = c - b
        ans += (gap * poped)

        if c in keys:
            e_dict[c] += poped
        else:
            e_dict[c] = poped

    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-


def cal_xor(xor_list, rm_ind):
    ans = None
    for i, xi in enumerate(xor_list):
        if i!=rm_ind:
            if ans is not None:
                ans = ans^xi
            else:
                ans = xi
    return ans

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

ans_str = ''

if N==2:
    print("{} {}".format(a[1], a[0]))
else:

    xors = []
    for i in range(N//2):
        xors.append(a[2*i]^a[2*i+1])

    for i, ai in enumerate(a):
        sum_xor = cal_xor(xors, i//2)
        if i%2==0:
            ind = i+1
        else:
            ind = i-1

        ans = sum_xor^a[ind]
        ans_str += str(ans)+' '
    print(ans_str[:-1])


Fixed submission:
# -*- coding: utf-8 -*-


def cal_xor(xor_list):
    ans = None
    for xi in xor_list:
        if ans is not None:
            ans = ans^xi
        else:
            ans = xi
    return ans

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

ans_str = ''

if N==2:
    print("{} {}".format(a[1], a[0]))
else:

    xors = []
    for i in range(N//2):
        xors.append(a[2*i]^a[2*i+1])

    sum_xor = cal_xor(xors)
    for i, ai in enumerate(a):
        tmp_xor = sum_xor^xors[i//2]
        if i%2==0:
            ind = i+1
        else:
            ind = i-1

        ans = tmp_xor^a[ind]
        ans_str += str(ans)+' '
    print(ans_str[:-1])
Buggy python submission (verdict: Runtime Error):
import numpy as np

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


thr = 10**18

res = np.prod(A)

if res > thr:
    res = -1

print(int(res))


Fixed submission:
# coding: utf-8

#import numpy as np

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

thr = 10**18

res = 1

if 0 in A:
    res = 0
else:
    for a in A:

        res *= a

        if res > thr:
            res = -1
            break

print(int(res))

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import numpy as np

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


thr = 10**18

res = np.prod(A)

if res > thr:
    res = -1

print(int(res))


Fixed submission:
# coding: utf-8

#import numpy as np

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

thr = 10**18

res = 1

if 0 in A:
    res = 0
else:
    for a in A:

        res *= a

        if res > thr:
            res = -1
            break

print(int(res))

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import numpy as np

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


thr = 10**18

res = np.prod(A)

if res > thr:
    res = -1

print(res)


Fixed submission:
# coding: utf-8

#import numpy as np

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

thr = 10**18

res = 1

if 0 in A:
    res = 0
else:
    for a in A:

        res *= a

        if res > thr:
            res = -1
            break

print(int(res))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

res = 1
thr = 10**18
for a in A:
    res *= a

    if res > thr:
        res = -1

if 0 in A:
    res = 0

print(res)


Fixed submission:
# coding: utf-8

#import numpy as np

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

thr = 10**18

res = 1

if 0 in A:
    res = 0
else:
    for a in A:

        res *= a

        if res > thr:
            res = -1
            break

print(int(res))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

res = 1
thr = 10**18
for a in A:
    res *= a

if res > thr:
    res = -1

print(res)


Fixed submission:
# coding: utf-8

#import numpy as np

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

thr = 10**18

res = 1

if 0 in A:
    res = 0
else:
    for a in A:

        res *= a

        if res > thr:
            res = -1
            break

print(int(res))
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys
import math


sys.setrecursionlimit(10 ** 6)

input_line = input()
inputs = input_line.split(' ')
A, = list(map(int, inputs))

print(2*R*math.pi)


Fixed submission:
# -*- coding: utf-8 -*-

import sys
import math


sys.setrecursionlimit(10 ** 6)

input_line = input()
inputs = input_line.split(' ')
R, = list(map(int, inputs))

print(2*R*math.pi)
Buggy python submission (verdict: Runtime Error):

inputs = input_line.split(' ')
a, b, c = [int(s) for s in inputs]
print("{} {} {}".format(c, a, b))


Fixed submission:
# -*- coding: utf-8 -*-
input_line = input()

inputs = input_line.split(' ')
a, b, c = [int(s) for s in inputs]
print("{} {} {}".format(c, a, b))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
q = int(input())
dict_A = Counter(A)
sum_list = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    if b not in dict_A:
        print(sum_list)
    else:
        dict_A[c] += dict_A[b]
        dict_A[b] = 0
        sum_list = 0
        for key, value in dict_A.items():
            sum_list += key * value
        print(sum_list)

Fixed submission:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
q = int(input())
dict_A = Counter(A)
sum_list = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    if b not in dict_A:
        print(sum_list)
    else:
        b_num = dict_A[b]
        dict_A[c] += dict_A[b]
        dict_A[b] = 0
        sum_list = sum_list + (c - b) * b_num
        print(sum_list)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
A = input().split()
q = int(input())
dict_A = Counter(A)
sum_list = 0
for key, value in dict_A.items():
        sum_list += int(key) * value

for i in range(q):
    b, c = map(int, input().split())
    if str(b) not in dict_A:
        print(sum_list)
    else:
        dict_A[str(c)] += dict_A[str(b)]
        dict_A[str(b)] = 0
        sum_list = 0
        for key, value in dict_A.items():
            sum_list += int(key) * value
        print(sum_list)

Fixed submission:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
q = int(input())
dict_A = Counter(A)
sum_list = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    if b not in dict_A:
        print(sum_list)
    else:
        b_num = dict_A[b]
        dict_A[c] += dict_A[b]
        dict_A[b] = 0
        sum_list = sum_list + (c - b) * b_num
        print(sum_list)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
A = input().split()
q = int(input())
dict_A = Counter(A)
sum_list = 0
for key, value in dict_A.items():
        sum_list += int(key) * value

for i in range(q):
    b, c = map(int, input().split())
    if str(b) not in dict_A:
        print(sum_list)
    else:
        dict_A[str(c)] += dict_A[str(b)]
        dict_A[str(b)] = 0
        sum_list = 0
        for key, value in dict_A.items():
            sum_list += int(key) * value
        print(sum_list)

Fixed submission:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
q = int(input())
dict_A = Counter(A)
sum_list = sum(A)
for i in range(q):
    b, c = map(int, input().split())
    if b not in dict_A:
        print(sum_list)
    else:
        b_num = dict_A[b]
        dict_A[c] += dict_A[b]
        dict_A[b] = 0
        sum_list = sum_list + (c - b) * b_num
        print(sum_list)
Buggy python submission (verdict: Runtime Error):
x, y = map(int, input().split())
if x * 2 > y:
    flag = False
elif x * 4 < y:
    flag = False
elif (y - x * 2) % 2 == 0:
    flag = True

print('Yes' if flag == True else 'No')

Fixed submission:
x, y = map(int, input().split())
if x * 2 > y:
    flag = False
elif x * 4 < y:
    flag = False
elif (y - x * 2) % 2 == 0:
    flag = True
else:
    flag = False

print('Yes' if flag == True else 'No')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = sorted(list(map(int, input().split())))
dp = [0] * (10 ** 6 + 10)
for x in A:
    i = 0
    while x + i <= 10 ** 6 + 10:
        dp[(x-1) + i] += 1
        i += x

cnt = 0
for x in A:
    if dp[x-1] == 1:
        cnt += 1

print(cnt)

Fixed submission:
n = int(input())
A = sorted(list(map(int, input().split())))
dp = [0] * (10 ** 6 + 10)
for x in A:
    i = 0
    while x + i <= 10 ** 6 + 10:
        if dp[(x-1)] == 2:
            break
        else:
            dp[(x-1) + i] += 1
            i += x

cnt = 0
for x in A:
    if dp[x-1] == 1:
        cnt += 1

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = sorted(list(map(int, input().split())))
dp = [0] * (10 ** 6 + 10)
for x in A:
    i = 0
    while x + i <= 10 ** 6 + 10:
        dp[(x-1) + i] += 1
        i += x

cnt = 0
for x in A:
    if dp[x-1] == 1:
        cnt += 1

print(cnt)

Fixed submission:
n = int(input())
A = sorted(list(map(int, input().split())))
dp = [0] * (10 ** 6 + 10)
for x in A:
    i = 0
    while x + i <= 10 ** 6 + 10:
        if dp[(x-1)] == 2:
            break
        else:
            dp[(x-1) + i] += 1
            i += x

cnt = 0
for x in A:
    if dp[x-1] == 1:
        cnt += 1

print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
import functools
import operator
n = int(input())
A = list(map(int, input().split()))
ans = functools.reduce(operator.mul, A)

print(ans if ans <= 10**18 else '-1')

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
ans = 1
if min(A) == 0:
    ans = 0
else:
    for a in A:
        ans *= a
        if ans > 10 ** 18:
            ans = '-1'
            break


print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))
tmp = 1
for a in A:
    tmp *= a

print(tmp if tmp <= 10**18 else '-1')

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
ans = 1
if min(A) == 0:
    ans = 0
else:
    for a in A:
        ans *= a
        if ans > 10 ** 18:
            ans = '-1'
            break


print(ans)
Buggy python submission (verdict: Runtime Error):
from itertools import product
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    elif s[i] == 'B':
        b.append(i)

ng_cnt = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue
        else:
            k = 2 * j - i
            if k < n and s[i] != s[k] and s[j] != s[k]:
                ng_cnt += 1

print(r * g * b - ng_cnt)

Fixed submission:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    elif x == 'B':
        b += 1

ng_cnt = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue
        else:
            k = 2 * j - i
            if k < n and s[i] != s[k] and s[j] != s[k]:
                ng_cnt += 1

print(r * g * b - ng_cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    elif s[i] == 'B':
        b.append(i)

lst = list(product(r,g,b))
ans = len(lst)
for i in range(len(lst)):
    tmp = sorted(lst[i])
    if tmp[1]-tmp[0] == tmp[2]-tmp[1]:
        ans -= 1

print(ans)

Fixed submission:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    elif x == 'B':
        b += 1

ng_cnt = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue
        else:
            k = 2 * j - i
            if k < n and s[i] != s[k] and s[j] != s[k]:
                ng_cnt += 1

print(r * g * b - ng_cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(len(s)):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    elif s[i] == 'B':
        b.append(i)

lst = list(product(r,g,b))
ans = 0
for i in range(len(lst)):
    tmp = sorted(lst[i])
    if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
        ans += 1

print(ans)

Fixed submission:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    elif x == 'B':
        b += 1

ng_cnt = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue
        else:
            k = 2 * j - i
            if k < n and s[i] != s[k] and s[j] != s[k]:
                ng_cnt += 1

print(r * g * b - ng_cnt)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
dict_A = Counter(A)
for a in A:
    ans = 0
    dict_A[a] -= 1
    for v in dict_A.values():
        ans += (v * (v - 1)) / 2
    dict_A[a] += 1
    print(int(ans))


Fixed submission:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
c = Counter(A)
sum = 0
for v in c.values():
    sum += int(v*(v-1)/2)

for i in A:
    print(sum - (c[i] - 1))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
c = Counter(A)
s_A = set(A)
d = {}
for a in s_A:
    tmp = 0
    for item in c.items():
        if item[0] == a:
            if item[1] > 1:
                    tmp += int((c[a] - 1 ) * (c[a] - 2) / 2)
            continue
        if item[1] > 1:
            tmp += int(item[1] * (item[1] - 1) / 2)
    d[a] = tmp

for a in A:
    print(d[a])

Fixed submission:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
c = Counter(A)
sum = 0
for v in c.values():
    sum += int(v*(v-1)/2)

for i in A:
    print(sum - (c[i] - 1))
Buggy python submission (verdict: Time Limit Exceeded):
a = input()
n = int(input())
tmp = 0 
for i in range(n):
    q = input().split()
    if len(q) == 1:
        tmp += 1
    elif len(q) == 3:
        if q[1] == '1':
            if tmp % 2 == 0:
                a = q[2] + a
            else:
                a = a + q[2]
        elif q[1] == '2':
            if tmp % 2 == 0:
                a = a + q[2]
            else:
                a = q[2] + a

print(a if tmp % 2 == 0 else a[::-1])

Fixed submission:
from collections import deque
a = input()
a = deque(a)
n = int(input())
tmp = 0
for i in range(n):
    q = input().split()
    if len(q) == 1:
        tmp += 1
    elif len(q) == 3:
        if q[1] == '1':
            if tmp % 2 == 0:
                a.appendleft(q[2])
            else:
                a.append(q[2])
        elif q[1] == '2':
            if tmp % 2 == 0:
                a.append(q[2])
            else:
                a.appendleft(q[2])

print(''.join(a) if tmp % 2 == 0 else ''.join(a)[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
a = input()
n = int(input())
for i in range(n):
    q = input().split()
    if len(q) == 3:
        if q[1] == '1':
            a = q[2] + a
        elif q[1] == '2':
            a = a + q[2]
    else:
        a = a[::-1]

print(a)

Fixed submission:
from collections import deque
a = input()
a = deque(a)
n = int(input())
tmp = 0
for i in range(n):
    q = input().split()
    if len(q) == 1:
        tmp += 1
    elif len(q) == 3:
        if q[1] == '1':
            if tmp % 2 == 0:
                a.appendleft(q[2])
            else:
                a.append(q[2])
        elif q[1] == '2':
            if tmp % 2 == 0:
                a.append(q[2])
            else:
                a.appendleft(q[2])

print(''.join(a) if tmp % 2 == 0 else ''.join(a)[::-1])