Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

s = input()
q = int(input())

rev = 0

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev += 1
    else:
        if (t[1] == "1" and rev % 2 == 0) or (t[1] == "2" and rev % 2 != 0):
            s = "".join([t[2],s])
        else:
            s = "".join([s,t[2]])

if rev % 2 == 0:
    print(s)
else:
    print(s[::-1])

### FIXED CODE ###

from collections import deque
s = deque(input())
q = int(input())

rev = False

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            s.append(t[2]) if rev else s.appendleft(t[2])
        else:
            s.appendleft(t[2]) if rev else s.append(t[2])

if rev:
    s.reverse()
print("".join(s))
### BUGGY CODE ###

N,K = map(int,input().split())
P_list = list(map(int,input().split()))
C_list = list(map(int,input().split()))

max_sum=-10**9
for i in range(N):
    m_sum=0
    k=i
    for j in range(K):
        m_sum += C_list[k]
        if m_sum > max_sum:
            max_sum=m_sum
        k=P_list[k]-1
print(max_sum)

### FIXED CODE ###

n,k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))

ans=-10**18
for i in range(n):
    start,count=i,1
    val=c[start]
    while p[start]-1 != i:
        start = p[start]-1
        count += 1
        val += c[start]
    start = i
    if val > 0:
        a = (k//count-1)*val
        ans=max(a,ans)
        num=count+k%count
    else:
        a=0
        num=min(k,count)
    for _ in range(num):
        a += c[start]
        start=p[start]-1
        ans=max(a,ans)
print(ans)
### BUGGY CODE ###

from copy import deepcopy


def set_value(l, i, j, di, dj, is_edge):
    if is_edge:
        return 1
    return l[i + di][j + dj] + 1


def main():
    height, width = map(int, input().split())
    grid = [input() for _ in range(height)]
    left = [[0 for _ in range(width)] for _ in range(height)]
    right = deepcopy(left)
    down = deepcopy(left)
    upper = deepcopy(left)
    for i in range(height):
        for j in range(width):
            if grid[i][j] == ".":
                upper[i][j] = set_value(upper, i, j, -1, 0, i == 0)
                left[i][j] = set_value(left, i, j, 0, -1, j == 0)
            if grid[-i - 1][-j - 1] == ".":
                down[-i - 1][-j - 1] = set_value(down, - i - 1,  - j - 1, 1, 0, i == -height + 1)
                right[-i - 1][-j - 1] = set_value(right, - i - 1,  - j - 1, 0, 1, j == -width + 1)
    answer = 0
    for i in range(height):
        for j in range(width):
            answer = max(answer, upper[i][j] + left[i][j] + down[i][j] + right[i][j] - 3)
    print(answer)


if __name__ == '__main__':
    main()



### FIXED CODE ###

import numpy as np
from copy import deepcopy


def main():
    height, width = map(int, input().split())
    grid = [list(input()) for _ in range(height)]
    grid = (np.array(grid) == ".") * 1
    left = np.zeros((height, width), dtype=int)
    right = deepcopy(left)
    upper = deepcopy(left)
    down = deepcopy(left)

    for i in range(height):
        if i == 0:
            upper[i] = grid[i]
        else:
            upper[i] = (upper[i - 1] + 1) * grid[i]

    for i in range(width):
        if i == 0:
            left[:, i] = grid[:, i]
        else:
            left[:, i] = (left[:, i - 1] + 1) * grid[:, i]

    for i in range(width - 1, -1, -1):
        if i == width - 1:
            right[:, i] = grid[:, i]
        else:
            right[:, i] = (right[:, i + 1] + 1) * grid[:, i]

    for i in range(height - 1, -1, -1):
        if i == height - 1:
            down[i] = grid[i]
        else:
            down[i] = (down[i + 1] + 1) * grid[i]

    print(np.max(left + right + upper + down - 3))


if __name__ == '__main__':
    main()
### BUGGY CODE ###

n = int(input())
ls = list(map(int, input().split()))
res = 1
for i in range(0, n):
    res *= ls[i]
print(res if res <= 10 ** 18 else "-1")

### FIXED CODE ###

n=int(input())
a=list(map(int, input().split()))
if 0 in a:
    print(0)
    exit()
else:
    res=1
    for i in a:
        res*=i
        if res>10**18:
            res=-1
            break
    print(res)
### BUGGY CODE ###

print(sum(range(1,int(input()))))


### FIXED CODE ###

n = int(input())
print(n*(n-1)//2)
### BUGGY CODE ###

N, M = map(int, input().split())
P_S = [ input().split() for _ in range(M)]

AC = 0
WA = 0
WA_dict = {i: 0 for i in range(1, N+1)}
exc = []

for p, s in P_S:
    if p in exc:
        continue
    elif s=='WA':
        WA_dict[int(p)] += 1
    else:
        AC += 1
        WA += WA_dict[int(p)]
        exc.append(p)

print(AC, WA)

### FIXED CODE ###

N, M = map(int, input().split())
# P_S = [ input().split() for _ in range(M)]

AC = 0
WA = 0
WA_dict = {i: 0 for i in range(1, N+1)}
AC_dict = {i: 0 for i in range(1, N+1)}

for _ in range(M):
    p, s = input().split()
    if AC_dict[int(p)]==1:
        continue
    elif s=='WA':
        WA_dict[int(p)] += 1
    else:
        AC += 1
        AC_dict[int(p)] = 1
        WA += WA_dict[int(p)]
print(AC, WA)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
#N, K = (int(i) for i in input().split())
#B = [int(x) for x in input().split()]
#A = [[int(i) for i in input().split()] for i in range(N)]

def popcountdiv(n):
    return n % (bin(n).count("1"))

memo = {}
def f(n):
    if n == 0:
        return 0
    if n in memo:
        return memo[n]
    else:
        res = f(popcountdiv(n))
        memo[n] = res+1
        return 1 + res

N = int(input())
X = input()

for i in range(N):
    print(f(int(X[:i] + str((int(X[i])+1)%2)+ X[i+1:] , 2)))


### FIXED CODE ###

import sys
input = sys.stdin.readline

N = int(input())
X = input()
Xint = int(X, 2)
Xcount = X.count("1")

"""
0010
1010 10(mod2) -> 0
i = n-1の時だけ +1
0011 -> 3 -> 1->0
0110 -> 6(mod2) -> 0
"""

if Xcount == 1:
    for i in range(N):
        if X[i] == "1":
            print(0)
        elif i == N-1:
            print(2)
        else:
            print(1)
    exit()

Modplus, Modminus = Xint%(Xcount+1), Xint%(Xcount-1)

def f(x):
    if x == 0:
        return 0
    return f(x%bin(x).count("1"))+1

for i in range(N):
    if X[i] == "1":
        print(1+f((Modminus-pow(2, N-1-i, Xcount-1))%(Xcount-1)))
    else:
        print(1+f((Modplus+pow(2, N-1-i, Xcount+1))%(Xcount+1)))
### BUGGY CODE ###

n, x = map(int, input().split())
hamburger_dict = {
  0: 'p'
}

def hamburger(n):
  if hamburger_dict.get(n):
    return hamburger_dict.get(n)
  return 'b' + hamburger(n - 1) + 'p' + hamburger(n - 1) + 'b'

print(hamburger(n)[0:x].count('p'))

### FIXED CODE ###

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

f_dict = {
  0: 1
}
def f(n):
  if f_dict.get(n):
    return f_dict.get(n)
  return (3 + f(n-1) * 2)


patty_dict = {
  0: 1
}
def total_patty(n):
  if patty_dict.get(n):
    return patty_dict.get(n)
  return (1 + 2 * total_patty(n - 1))

def count_patty(n, x):
  if n == 0 and x == 1:
    return 1
  elif x == 0:
    return 0
  elif 1 <= x and x <= f(n - 1) + 1:
    return count_patty(n - 1, x - 1)
  elif x == f(n - 1) + 2:
    return total_patty(n - 1) + 1
  elif f(n - 1) + 2 <= x and x <= 2 * f(n - 1) + 2:
    return total_patty(n - 1) + 1 + count_patty(n - 1, x - f(n - 1) - 2)
  else:
    return total_patty(n)

print(count_patty(n, x))
### BUGGY CODE ###

from collections import deque
import copy
H,W = map(int, input().split())
field = [[j == "#" for j in input()] for _ in range(H)]
dy = [1,-1,0,0]
dx = [0,0,1,-1]

queue = deque()
# sy,sx = 0,0
# gy,gx = H-1,W-1
queue = deque([(i, j) for i in range(H) for j in range(W) if field[i][j]])
# for i in range(H):
#     for j in range(W):
#         if field[i][j] == True:
#             queue.append([i,j])
# queue = [[sy,sx]]
flag = False
count = 0
while True:
    count += 1
    nqueue = deque()
    while queue:
        h,w = queue.popleft()
        for i in range(4):
            nh = h + dy[i]
            nw = w + dx[i]
            if (0<=nh<H) and (0<=nw<W):
                if field[nh][nw] == False:
                    field[nh][nw] = True
                    nqueue.append([nh,nw])
    if nqueue:
        queue = copy.copy(nqueue)
    else:
        break
print(count-1)

### FIXED CODE ###

from collections import deque
import copy

def main():
    h, w = map(int, input().split())
    a = [[j == "#" for j in input()] for _ in range(h)]


    # スタート地点をqueueに追加
    que = deque([(i, j) for i in range(h) for j in range(w) if a[i][j]])

    count = 0
    while True:
        count += 1
        nque = deque()
        while que:
            y, x = que.popleft()
            if x > 0 and not a[y][x-1]:
                a[y][x-1] = True
                nque.append((y,x-1))
            if x < w-1 and not a[y][x+1]:
                a[y][x+1] = True
                nque.append((y,x+1))
            if y > 0 and not a[y-1][x]:
                a[y-1][x] = True
                nque.append((y-1,x))
            if y < h-1 and not a[y+1][x]:
                a[y+1][x] = True
                nque.append((y+1,x))

        if nque:
            que = copy.copy(nque)
        else:
            break

    print(count - 1)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

n,k=map(int,input().split())
h=list(map(int,input().split()))
ans = [1234567890123]*n
ans[0]=0
for i in range(1,n):
    for j in range(i-1,max(i-1-k,-1),-1):
        ans[i] = min(ans[i],ans[j]+abs(h[i]-h[j]))
    
print(ans[n-1])

### FIXED CODE ###

n,k=map(int,input().split())
h=list(map(int,input().split()))
ans = [1234567890123]*n
ans[0]=0
for i in range(1,n):
    for j in range(i-1,max(i-1-k,-1),-1):
        ans[i] = min(ans[i],ans[j]+abs(h[i]-h[j]))
    
print(ans[n-1])
### BUGGY CODE ###

# ref https://atcoder.jp/contests/abc153/submissions/9758833
# ref http://drken1215.hatenablog.com/entry/2020/01/26/225000

# coding: utf-8
H, N = map(int, input().split())
A = [0]
B = [0]

for i in range(N):
    a, b = (list(map(int, input().split())))
    A.append(a)
    B.append(b)

dp = [[float('inf') for i in range(H+1)] for j in range(N+1)]

for i in range(N+1):
    dp[i][0] = 0

for i in range(1, N+1):
    for j in range(1, H+1):
        dp[i][j] = min(dp[i-1][j],
                       dp[i][max(j-A[i], 0)] + B[i])
        #print(dp[i-1][j],dp[i-1][max(j-AB[i][0],0)] + AB[i][1],dp[i][max(j-AB[i][0],0)] + AB[i][1])


print(dp[N][H])


### FIXED CODE ###

# ref https://atcoder.jp/contests/abc153/submissions/9758833
# ref http://drken1215.hatenablog.com/entry/2020/01/26/225000

# coding: utf-8
H, N = map(int, input().split())
A = [0]
B = [0]

for i in range(N):
    a, b = (list(map(int, input().split())))
    A.append(a)
    B.append(b)

dp = [[float('inf') for i in range(H+1)] for j in range(N+1)]

for i in range(N+1):
    dp[i][0] = 0

for i in range(1, N+1):
    for j in range(1, H+1):
        dp[i][j] = min(dp[i-1][j],
                       dp[i-1][max(j-A[i], 0)] + B[i],
                       dp[i][max(j-A[i], 0)] + B[i])
        #print(dp[i-1][j],dp[i-1][max(j-AB[i][0],0)] + AB[i][1],dp[i][max(j-AB[i][0],0)] + AB[i][1])


print(dp[N][H])
### BUGGY CODE ###

x=input()
x0=x.split()
n=int(x0[0])
k=int(x0[1])
a0=input()
a0=a0.split()
a=[int(s) for s in a0]
a2=list(range(n+1))
a2=[0 for s in a2]
for f in range(k):
    a1=a2[:]
    for x in range(n):
        y=max(0,x-a[x])
        a1[y]=a1[y]+1    
        z=min(n-1,x+a[x])
        a1[z+1]=a1[z+1]-1
    a[0]=a1[0]
    for x in range(1,n):    
        a[x]=a[x-1]+a1[x]
    if sum(a)==n**2:
        break
for i in range(n):
    print(a[i])

### FIXED CODE ###

from numba import njit

@njit(cache=True)
def aaa(n,k,a):
    for f in range(k):
        a1=[0]*(n+1)
        for x in range(n):
            y=max(0,x-a[x])
            a1[y]=a1[y]+1    
            z=min(n-1,x+a[x])
            a1[z+1]=a1[z+1]-1
        a[0]=a1[0]
        for x in range(1,n):    
            a[x]=a[x-1]+a1[x]
        if a==[n]*n:
            break
    return a

x=input()
x0=x.split()
n=int(x0[0])
k=int(x0[1])
a0=input()
a0=a0.split()
a=[int(s) for s in a0]

b=aaa(n,k,a)
for i in range(n):
    print(b[i])
### BUGGY CODE ###

N=int(input())
n1=[x+1 for x in range(N)]
n2=n1[1:]+[n1[0]]
s=0
for x,y in zip(n1,n2):
    s+=x%y
print(s)

### FIXED CODE ###

N=int(input())
ans=int((N-1)*N//2)
print(ans)
### BUGGY CODE ###

from heapq import heapify, heappush, heappop

n = int(input())
cities = [tuple(map(int, input().split())) for _ in range(n)]
visited = {0}
x0, y0 = cities[0]
queue = [(min(abs(xi - x0), abs(yi - y0)), i) for i, (xi, yi) in enumerate(cities) if i not in visited]
heapify(queue)
total = 0
while True:
    cost, i = heappop(queue)
    if i in visited:
        continue
    visited.add(i)
    total += cost
    if len(visited) == n:
        break
    xi, yi = cities[i]
    for j, (xj, yj) in enumerate(cities):
        if j in visited:
            continue
        heappush(queue, (min(abs(xj - xi), abs(yj - yi)), j))
print(total)


### FIXED CODE ###

from heapq import heapify, heappush, heappop

n = int(input())
x_loc, y_loc = [], []
for i in range(n):
    x, y = map(int, input().split())
    x_loc.append((x, i))
    y_loc.append((y, i))
x_loc.sort()
y_loc.sort()
links = [set() for _ in range(n)]
for (x1, i1), (x2, i2) in zip(x_loc, x_loc[1:]):
    dist = abs(x1 - x2)
    links[i1].add((dist, i2))
    links[i2].add((dist, i1))
for (y1, i1), (y2, i2) in zip(y_loc, y_loc[1:]):
    dist = abs(y1 - y2)
    links[i1].add((dist, i2))
    links[i2].add((dist, i1))

visited = {0}
queue = list(links[0])
heapify(queue)
total = 0
while True:
    cost, i = heappop(queue)
    if i in visited:
        continue
    visited.add(i)
    total += cost
    if len(visited) == n:
        break
    for cost2, j in links[i]:
        if j in visited:
            continue
        heappush(queue, (cost2, j))
print(total)
### BUGGY CODE ###

try:
    inp = list(input().split())
    #listA<-[1,2,2,3,1]
    #print(listA)
    #ここに逐次処理を書けばいいと思う。
except:
    quit();
    #または、quit(),os.exit()をして止める。

try:
    h = list(input().split())
    #listA<-[1,2,2,3,1]
    #print(listA)
    #ここに逐次処理を書けばいいと思う。
except:
    quit();
    #または、quit(),os.exit()をして止める。

N=int(inp[0])
K=int(inp[1])

dp=[0]*N



for j in range(1,N):
    min=float('inf')

    if j-K>=0:
        bnum=j-K
    else:
        bnum=0

    for i in range(bnum,j):
        num=abs(int(h[i])-int(h[j])) + dp[i]
        if num < min:
            min=num
    dp[j]=min

print(dp[N-1])



### FIXED CODE ###

import sys
import numpy as np
input = sys.stdin.readline

try:
    inp = list(input().split())
    #listA<-[1,2,2,3,1]
    #print(listA)
    #ここに逐次処理を書けばいいと思う。
except:
    quit();
    #または、quit(),os.exit()をして止める。

try:
    h = list(map(int, input().split()))
    #listA<-[1,2,2,3,1]
    #print(listA)
    #ここに逐次処理を書けばいいと思う。
except:
    quit();
    #または、quit(),os.exit()をして止める。

N=int(inp[0])
K=int(inp[1])

dp=[0]*N

dp=np.array(dp)
h=np.array(h)

for j in range(1,N):
    bnum=max(0,j-K)
    dp[j]=np.min(np.abs(h[bnum:j]-h[j]) + dp[bnum:j])

print(dp[N-1])
### BUGGY CODE ###

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

As=sorted(A, reverse=True)

for i in range(M):
    As[0] = As[0]//2
    As = sorted(As, reverse=True)

print(sum(As))

### FIXED CODE ###

from heapq import heappop,heappush,heapify
n,m=map(int,input().split())
A=list(map(int,input().split()))
A_m=list(map(lambda x: -x,A))
heapify(A_m)
pop=lambda: -heappop(A_m)
push=lambda x: heappush(A_m,-x)
for _ in range(m):
    x=pop()
    push(x//2)
print(-sum(A_m))
### BUGGY CODE ###

import math
import os
import sys

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

SX, SY, TX, TY = list(map(int, sys.stdin.readline().split()))
N = int(sys.stdin.readline())
XYR = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

S = N
T = N + 1
XYR.append([SX, SY, 0])
XYR.append([TX, TY, 0])
V = []
U = []
D = []
for i, (xi, yi, ri) in enumerate(XYR):
    for j, (xj, yj, rj) in enumerate(XYR):
        if i == j:
            continue
        d = math.sqrt((xi - xj) ** 2 + (yi - yj) ** 2) - ri - rj
        V.append(i)
        U.append(j)
        D.append(max(0, d))

ret = dijkstra(csr_matrix((D, (U, V))), directed=False, indices=S)
print(ret[T])


### FIXED CODE ###

import heapq
import math
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

SX, SY, TX, TY = list(map(int, sys.stdin.readline().split()))
N = int(sys.stdin.readline())
XYR = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

S = N
T = N + 1
XYR.append([SX, SY, 0])
XYR.append([TX, TY, 0])
graph = [[] for _ in range(N + 2)]
for i, (xi, yi, ri) in enumerate(XYR):
    for j, (xj, yj, rj) in enumerate(XYR):
        if i == j:
            continue
        d = math.sqrt((xi - xj) ** 2 + (yi - yj) ** 2) - ri - rj
        graph[i].append((max(0, d), j))

heap = [(0, S)]
heapq.heapify(heap)
dist = [IINF] * (N + 2)
while heap:
    w, v = heapq.heappop(heap)
    if w > dist[v]:
        continue
    for dw, u in graph[v]:
        if w + dw < dist[u]:
            dist[u] = w + dw
            heapq.heappush(heap, (w + dw, u))
print(dist[T])
### BUGGY CODE ###

def main():
  n, m = map(int, input().split())
  P = [None] * m
  Y = [None] * m
  for i in range(m):
    p, y =map(int, input().split())
    P[i] = p
    Y[i] = y
  
  for i in range(m):
    cnt = 1
    for j in range(m):
      if P[i] == P[j] and Y[i] > Y[j]:
        cnt += 1
    print(str(P[i]).zfill(6)+str(cnt).zfill(6))
  
if __name__ == '__main__':
  main()

### FIXED CODE ###

def main():
  n, m = map(int, input().split())
  buf = [None] * m
  cnt = {k: [] for k in range(n + 1)}
  for i in range(m):
      p, y = map(int, input().split())
      cnt[p] += [(y, i)]
  for p, l in cnt.items():
      if not l:
          continue
      l.sort()
      for j, (y, i) in enumerate(l):
          buf[i] = '{:06d}{:06d}'.format(p, j + 1)

  print('\n'.join(buf))

if __name__ == '__main__':
  main()
### BUGGY CODE ###

import copy

S = input()

len_S = len(S)

old_len_S = copy.copy(len_S)
for i in range(len_S):
    S = S.replace("10", "")
    S = S.replace("01", "")
    if old_len_S == len(S):
        break
    else:
        old_len_S = len(S)

print(len_S - len(S))

### FIXED CODE ###

S = input()

count_one = S.count("1")
count_zero = S.count("0")

print(min(count_one, count_zero)*2)
### BUGGY CODE ###

n,k,q = map(int,input().split())
d = {}
for i in range(1,n+1):
    d[i] = k

def s (x:int):
    for k,v in d.items():
        if k == x:
            pass
        else:
            d[k] = v-1

for _ in range(q):
    a = int(input())
    s (a)

for v in d.values():
    if 0 < v:
        print ("Yes")
    else:
        print ("No")


### FIXED CODE ###

n,k,q = map(int,input().split())

d = {}
for _ in range(q):
    a = int(input())
    if a in d:
        d[a] += 1
    else:
        d[a] = 1

for i in range(1,n+1):
    if not i in d:
        if q < k:
            print ("Yes")
        else:
            print ("No")
    else:
        if k <= q-d[i]:
            print ("No")
        else:
            print ("Yes")
### BUGGY CODE ###

import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(input()) for i in range(N)]

    for j in range(N):
        if j != A.index(max(A)):
            print(max(A))
        else:
            ans = [x for i, x in enumerate(A) if i != j]
            print(max(ans))


if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    n = int(input())
    As = [int(input()) for _ in range(n)]

    As_sort = sorted(As)
    second, first = As_sort[-2:]
    for i in range(n):
        if As[i] == first:
            print(second)
        else:
            print(first)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

n, k, q = map(int, input().split())
point = [k]*n

for i in range(q):
  answer = int(input())
  
  point = [p-1 for p in point]
  point[answer-1] += 1

for p in point:
  if p <= 0:
    print("No")
  else:
    print("Yes")

### FIXED CODE ###

n, k, q = map(int, input().split())
point = [k-q]*n

for i in range(q):
  answer = int(input())
  
  point[answer-1] += 1

for p in point:
  if p <= 0:
    print("No")
  else:
    print("Yes")
### BUGGY CODE ###

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

set_a = set(a)
for i in set_a:
  counter = a.count(i-1) + a.count(i)+a.count(i+1)
  if ans < counter:
    ans = counter
  
print(ans)

### FIXED CODE ###

import collections
N = int(input())
a = list(map(int, input().split()))
cand = collections.Counter(a)
cand_list = cand.most_common(len(set(a)))
ans = 0
for i in range(len(set(a))):
  num = cand_list[i][0]
  count = cand[num] + cand[num + 1] + cand[num - 1]
  ans = max(ans,count)

print(ans)
### BUGGY CODE ###

N = int(input())
M = list(map(int, input().split()))
M = sorted(M)
count = 0
for i in range(N):
  a = M[i]
  for j in range(i+1, N):
    b = M[j]
    lower = b-a
    upper = a+b
    for l in range(j+1, N):
      c = M[l]
      if c < lower:
        continue
      if c >= a+b:
        break
      count += 1
      
print(count)

### FIXED CODE ###

N = int(input())
M = list(map(int, input().split()))
M = sorted(M)
count = 0
for i in range(N):
  a = M[i]
  for j in range(i+1, N):
    b = M[j]
    upper = a+b
    l, r = j, N
    while (r - l) > 1:
      c = (l + r) // 2
      if M[c] < upper:
        l = c
      else:
        r = c  
    #print(count, j, l, r)
    count += l - j
      
print(count)
### BUGGY CODE ###

import random
def is_prime(q,k=50):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False

    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1
    
    for i in range(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        while t != q-1 and y != 1 and y != q-1: 
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True
 
a, b = map(int,input().split())
 
c = 1
for i in range(1,min(a,b)+1):
	if a % i == 0 and b % i == 0:
		if is_prime(i):
			c+=1			
		
print(c)

### FIXED CODE ###

import random

def gcd(a,b):
        if b == 0:
                return a
        else:
                return gcd(b , a%b) 
def is_prime(q,k=50):
    q = abs(q)
    if q == 2: return True
    if q < 2 or q&1 == 0: return False

    d = (q-1)>>1
    while d&1 == 0:
        d >>= 1

    for i in range(k):
        a = random.randint(1,q-1)
        t = d
        y = pow(a,t,q)
        while t != q-1 and y != 1 and y != q-1:
            y = pow(y,2,q)
            t <<= 1
        if y != q-1 and t&1 == 0:
            return False
    return True

a, b = map(int,input().split())

c = 1
m = gcd(a,b)

i = 1
while i * i <=m:
        if a%i == 0 and b%i == 0:
                if is_prime(i):
                        c+=1
                if is_prime(m//i):
                        c+=1
        i+=1
print(c)
### BUGGY CODE ###

s=input()
q=int(input())
query=["","",""]
for i in range(q):
    query=list(map(str,input().split()))
    if (query[0]=="1"):
        s=s[::-1]
    else:
        if (query[1]=="1"):
            s=query[2]+s
        else:
            s=s+query[2]
            
print(s)

### FIXED CODE ###

s=input()
q=int(input())
check=0
query=["","",""]
tfel=""
right=""
for i in range(q):
    query=list(map(str,input().split()))
    if (query[0]=="1"):
        check+=1
    else:
        if (query[1]=="1"):
            if (check%2==0):
                tfel+=query[2]
            else:
                right+=query[2]
        else:
            if (check%2==0):
                right+=query[2]
            else:
                tfel+=query[2]
            
s=tfel[::-1]+s+right
print(s if check%2==0 else s[::-1])