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

#!/usr/bin/env python3

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

d = -1
cnt, ans = 0, 1
for i in range(N - 1):
    l = A[i]
    r = max(A[i + 1:])
    if l <= r:
        if r - l == d:
            cnt += 1
            ans = max(ans, cnt)
        else:
            cnt = 1
            d = max(d, r - l)

print(ans)


### FIXED CODE ###

#!/usr/bin/env python3

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

d = 0
ans = 1
l = A[0]
for a in A[1:]:
    l = min(l, a)
    r = a
    if r - l == d:
        ans += 1
    elif r - l > d:
        ans = 1
        d = r - l

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

N=int(input())

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

cnt=0
for i in range(N):
    for j in range(i,N):
        if sum(A[i:j+1])==0:
            cnt+=1
print(cnt)


### FIXED CODE ###

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

S=[]
temp=0
for i in range(N):
    temp=temp+A[i]
    S.append(temp)

from collections import Counter

cnt=Counter(S)

ans=0
for c in cnt.values():
    ans+=c*(c-1)//2

if cnt[0]!=0:
    ans+=cnt[0]

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

n=int(input())
L=[]
M=[]
c=0
for i in range (n):
    s=input()
    if s in L:
        M[L.index(s)]+=1
    else:
        L+=[s]
        M+=[1]
        c+=1
b=max(M)
F=[]
for i in range(c):
    if M[i]==b:
        F+=[L[i]]
F.sort()
for i in F:
    print(i)
    


### FIXED CODE ###

n=int(input())
L={}
c=0
for i in range (n):
    s=input()
    if s in L:
        L[s]+=1
    else:
        L.update({s:1})
        c+=1
M=list(L.values())
C=list(L.keys())
b=max(M)
F=[]
for i in range(c):
    if M[i]==b:
        F+=[C[i]]
F.sort()
for i in F:
    print(i)
### BUGGY CODE ###

N = int(input())
L = [int(i) for i in input().split()]

import random
from bisect import bisect_left

L_len = len(L)
L.sort()

count = 0
for i in range(0, L_len - 2):
    for j in range(i + 1, L_len - 1):
        length = L[i] + L[j]
        idx = bisect_left(L, length, lo = j + 1, hi = L_len)
        count += idx - j - 1

print(count)


### FIXED CODE ###

N = int(input())
L = [int(i) for i in input().split()]

from bisect import bisect_left

L.sort()

count = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        length = L[i] + L[j]
        idx = bisect_left(L, length)
        count += idx - j - 1

print(count)
### BUGGY CODE ###

n,x,y=map(int, input().split())
dist=[]

for i in range(1,n+1):
    for j in range(i+1,n+1):
        dist.append(min(j-i,abs(j-y)+1+abs(i-x)))
for i in range(1,n):
    print(dist.count(i))

### FIXED CODE ###

n,x,y=map(int, input().split())
dist=[0]*(n+1)

for i in range(1,n+1):
    for j in range(i+1,n+1):
        dist[min(j-i,abs(j-y)+1+abs(i-x))]+=1
for i in range(1,n):
    print(dist[i])
### BUGGY CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
A = list(map(int, input().split()))
total = sum(A)
ans = []
for i in range(N):
    t = 0
    for j in range(N//2):
        ind = i+1+j*2
        if ind >= N:
            ind -= N
        t += A[ind]*2
    ans.append(total-t)
print(*ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
# 山1に降った量の半分をxと置くと、山2はA1-x、山3はA2-(A1-x)となり、翻って山1はA3-(A2-(A1-x))
# x = A3-A2+A1+x
# x = (A1+A3-A2) // 2
x = (sum(A[::2]) - sum(A[1::2])) // 2
ans = [x*2]
for i in range(N-1):
    a = A[i] - x
    ans.append(a*2)
    x = a
print(*ans)
### BUGGY CODE ###

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

if len(h) == 1:
    print('Yes')
else:
    count = 0
    for i in range(len(h)-1):
        if i == 0:
                if h[i] <= h[i+1]+1:
                    count += 0
                else:
                    count += 1
        else:
            if max(h[0:i]) <= h[i+1]+1:
                count += 0
            else:
                count += 1
    
    if count == 0:
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

# 減らせるときは減らす
N = int(input())
h = list(map(int, input().split()))

stop_flg = 0
h[0] = h[0]-1
for i in range(N-1):
    if stop_flg == 0:
        if h[i] <= h[i+1]-1:
            h[i+1] = h[i+1]-1
        elif h[i] == h[i+1]:
            continue
        else:
            stop_flg = 1

if stop_flg == 1:
    print('No')
else:
    print('Yes')
### BUGGY CODE ###

def main():
    import numpy as np
    n = int(input())
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    d_alpha = {j: i for i, j in enumerate(alphabet)}
    s = input()
    s = np.array([d_alpha[i] for i in s], dtype=np.int8)
    m = 0
    for i in range(1, n):
        cnt_m = 0
        cnt = 0
        t = (s[:n-i] == s[i:])
        for j in t:
            if j:
                cnt += 1
            else:
                cnt_m = max(cnt, cnt_m)
                cnt = 0
        cnt_m = min(max(cnt, cnt_m), i)
        m = max(cnt_m, m)
    print(m)


main()


### FIXED CODE ###

def main():
    n=int(input())
    s=input()
    m=0
    for i in range(1,n):
        cnt_m=0
        cnt=0
        for j in range(i,n):
            if s[j-i]==s[j]:
                cnt=min(cnt+1,i)
            else:
                cnt_m=max(cnt,cnt_m)
                cnt=0
        m=max(cnt,cnt_m,m)
    print(m)

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

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque

def agc033_a():
    H, W = map(int, readline().split())
    dist = [[-1] * W for _ in range(H)]
    que = deque()
    for i, ln in enumerate(read().split()):
        for j, c in enumerate(ln.strip().decode('UTF-8')):
            if c == '#':
                que.append((i, j))
                dist[i][j] = 0

    while que:
        x, y = que.popleft()
        for i, j in ((x-1, y), (x, y-1), (x, y+1), (x+1, y)):
            if i < 0 or H-1 < i or j < 0 or W-1 < j: continue
            if dist[i][j] != -1: continue
            dist[i][j] = dist[x][y] + 1
            que.append((i, j))

    ans = 0
    for row in dist:
        ans = max(max(row), ans)
    print(ans)

agc033_a()

### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque

def agc033_a():
    H, W = map(int, readline().split())
    dist = [[-1] * W for _ in range(H)]
    que = deque()
    for i, ln in enumerate(read().split()):
        for j, c in enumerate(ln.strip().decode('UTF-8')):
            if c == '#':
                que.append((i, j))
                dist[i][j] = 0

    while que:
        x, y = que.popleft()
        for i, j in ((x-1, y), (x, y-1), (x, y+1), (x+1, y)):
            if i < 0 or H-1 < i or j < 0 or W-1 < j: continue
            if dist[i][j] != -1: continue
            dist[i][j] = dist[x][y] + 1
            que.append((i, j))

    ans = 0
    for row in dist:
        ans = max(max(row), ans)
    print(ans)

agc033_a()
### BUGGY CODE ###

import sys
import numpy as np

read = sys.stdin.read

N, D, A, *XH = map(int, read().split())
X = np.array(XH[::2], np.int64)
H = np.array(XH[1::2], np.int64)
D *= 2

arg = np.argsort(X)
X = X[arg]
H = H[arg]

answer = 0
for i in range(N):
    if H[i] <= 0:
        continue
    h = H[i]
    cnt = h // A + min(1, h % A)
    n = np.searchsorted(X, X[i] + D, side='right')
    H[i:n] -= A * cnt
    answer += cnt

print(answer)


### FIXED CODE ###

import sys
from bisect import bisect_right

read = sys.stdin.read

N, D, A, *XH = map(int, read().split())
X = XH[::2]
H = XH[1::2]
D *= 2
X, H = zip(*sorted(zip(X, H)))

damage = [0] * (N + 1)
answer = H[0] // A + min(1, H[0] % A)
damage[0] += A * answer
damage[bisect_right(X, X[0] + D)] -= A * answer

for i in range(1, N):
    damage[i] += damage[i - 1]
    h = H[i]
    x = X[i]
    if damage[i] >= h:
        continue
    else:
        h -= damage[i]

    cnt = h // A + min(1, h % A)
    answer += cnt
    damage[i] += A * cnt
    damage[bisect_right(X, x + D)] -= A * cnt

print(answer)
### BUGGY CODE ###

#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for _ in range(n)]
def LIR(n):
    return [LI() for _ in range(n)]
def SR(n):
    return [S() for _ in range(n)]
def LSR(n):
    return [LS() for _ in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    b = LI()
    cnt = defaultdict(lambda : 0)
    for i in a:
        cnt[i] += 1
    for i in b:
        if not cnt[i]:
            continue
        cnt[i] += 1
    for i in cnt.values():
        if i > n:
            print("No")
            return
    while 1:
        r = n-1
        update = False
        for l in range(n):
            while r >= 0 and a[r] != b[r]:
                r -= 1
            if a[l] == b[l]:
                update = True
                b[l],b[r] = b[r],b[l]
                r -= 1
        if not update:
            break
    print("Yes")
    print(*b)
    return

#Solve
if __name__ == "__main__":
    solve()


### FIXED CODE ###

#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for _ in range(n)]
def LIR(n):
    return [LI() for _ in range(n)]
def SR(n):
    return [S() for _ in range(n)]
def LSR(n):
    return [LS() for _ in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    b = LI()
    cnt = defaultdict(lambda : 0)
    for i in a:
        cnt[i] += 1
    for i in b:
        if not cnt[i]:
            continue
        cnt[i] += 1
    for i in cnt.values():
        if i > n:
            print("No")
            return
    r = n-1
    while 1:
        update = False
        for l in range(n):
            while r >= 0 and a[r] != b[r]:
                r -= 1
            if a[l] == b[l]:
                update = True
                b[l],b[r] = b[r],b[l]
                r -= 1
        if not update:
            break
    print("Yes")
    print(*b)
    return

#Solve
if __name__ == "__main__":
    solve()
### BUGGY CODE ###

d,g=map(int,input().split())
g=g//100
p_list=[0]
c_list=[0]
for i in range(d):
    p,c=map(int,input().split())
    p_list.append(p)
    c_list.append(c//100)
dp=[]
for i in range(d+1):
    sub_list=[]
    for j in range(g+1):
        sub_list.append(10**9)
    dp.append(sub_list)
dp[0][0]=0
for i in range(1,d+1):
    for j in range(g+1):
        for k in range(p_list[i]+1):
            if k==p_list[i]:
                if j-i*k-c_list[i]>=0:
                    dp[i][j]=min(dp[i][j],dp[i-1][j-i*k-c_list[i]]+k)
                else:
                    dp[i][j]=min(dp[i][j],dp[i-1][0]+k)
            else:
                if j-i*k>=0:
                    dp[i][j]=min(dp[i][j],dp[i-1][j-i*k]+k)
                else:
                    dp[i][j]=min(dp[i][j],dp[i-1][0]+k)
print(dp[d][g])


### FIXED CODE ###

d,g=map(int,input().split())
g=g//100
p_list=[]
c_list=[]
s_list=[]
for i in range(d):
    p,c=map(int,input().split())
    c=c//100
    p_list.append(p)
    c_list.append(c)
    s_list.append(p*(i+1)+c)
Min=1000
for i in range(2**d):
    Sum=0
    Num=0
    for j in range(d):
        if (i>>j)&1==1:
            Sum=Sum+s_list[j]
            Num=Num+p_list[j]
        else:
            M=j
    if Sum>=g:
        if Num<Min:
            Min=Num
    else:
        r=((g-Sum)+M)//(M+1)
        if r<=p_list[M]:
            Num=Num+r
            if Num<Min:
                Min=Num
print(Min)
### BUGGY CODE ###

l, r = map(int, input().split())
mod = 0
tmp = 0

for i in range(l, r):
    for j in range(i+1, r+1):
        tmp = (i*j % 2019)
        if (i == l and j == l+1) or tmp < mod:
            mod = tmp
print(mod)

### FIXED CODE ###

import sys
l, r = map(int, input().split())
mod = 0
tmp = 0

for i in range(l, r):
    for j in range(i+1, r+1):
        tmp = (i*j % 2019)
        if (i == l and j == l+1) or tmp < mod:
            mod = tmp
            if mod == 0:
                print(mod)
                sys.exit()
print(mod)
### BUGGY CODE ###

numbers =input().rstrip().split(" ")
prices=input().rstrip().split(" ")
noa =int(numbers[0])
noc =int(numbers[1])
for i in range(noa):
  prices[i] =int(prices[i])
prices=sorted(prices ,reverse=True)
for i in range(noc):
  prices[0]=prices[0]//2
  prices=sorted(prices ,reverse=True)
answer=0
for i in range(noa):
  answer=answer+prices[i]
print(answer)

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
#from math import floor, ceil
#from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
#inf = 10**17
#mod = 10**9 + 7

n,m=MI()
As=LI()
for i in range(n):
    As[i]*=-1
    
#print(As)

heapify(As)
for i in range(m):
    mn=heappop(As)
    mn=(abs(mn)//2)*(-1)
    heappush(As,mn)

print(-sum(As))
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
mxbit = 0
for i in range(N):
  mxbit = max(mxbit, a[i].bit_length())
cs = [[0] * mxbit for _ in range(N + 1)]
for i in range(N):
  for j in range(mxbit):
    t = 1 << j
    #print(a[i], t, a[i] & t)
    x = (a[i] & t) > 0
    cs[i + 1][j] = (cs[i][j] + x) % mod
res = 0
#print(cs[-1])
#print(cs[2])
for i in range(N):
  t = 0
  for j in range(mxbit):
    x = 1 << j
    if a[i] & x != 0:
      x %= mod
      #if i == 1: print((1 << j) * (N - i - 1), (cs[-1][j] - cs[i + 1][j]))
      t += x * ((N - i - 1) - (cs[-1][j] - cs[i + 1][j]))
    else:
      x %= mod
      #if i == 1: print(a[i] & (1 << j), (cs[-1][j] - cs[i + 1][j]))
      t += x * (cs[-1][j] - cs[i + 1][j])
    #print(t)
    t %= mod
  res += t
  res %= mod
print(res % mod)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
mxbit = 0
for i in range(N):
  mxbit = max(mxbit, a[i].bit_length())
cs = [0] * mxbit
for i in range(N):
  for j in range(mxbit):
    t = 1 << j
    #print(a[i], t, a[i] & t)
    x = (a[i] & t) > 0
    cs[j] += x
res = 0
#print(cs[-1])
#print(cs)
for i in range(N):
  t = 0
  for j in range(mxbit):
    x = 1 << j
    if a[i] & x != 0:
      x %= mod
      #if i == 1: print((1 << j) * (N - i - 1), (cs[-1][j] - cs[i + 1][j]))
      t += x * (N - (cs[j]))
    else:
      x %= mod
      #if i == 1: print(a[i] & (1 << j), (cs[-1][j] - cs[i + 1][j]))
      t += x * cs[j]
    #print(t)
    t %= mod
  res += t
  res %= mod
print(res % mod * pow(2, mod - 2, mod) % mod)
### BUGGY CODE ###

s = input()
n = len(s)
ans = 0
for i in range(n-4):
    for j in range(i+4, n+1):
        num = int(s[i:j])
        # print(num)
        if num % 2019 == 0:
            ans += 1
            # print(i, j)

print(ans)

### FIXED CODE ###

s = input()[::-1]

sum_of_digits = 0

cnts = [0] * 2019
cnts[0] = 1

d = 1

for c in s:
    sum_of_digits += int(c) * d
    sum_of_digits %= 2019
    d *= 10
    d %= 2019
    cnts[sum_of_digits] += 1

ans = 0

for cnt in cnts:
    ans += cnt * (cnt - 1) // 2
 
print(ans)
### BUGGY CODE ###

N=int(input())
S=input()

# Zアルゴリズムで解く

c=0
max_num=0
for sub in range(len(S)):
    subS = S[sub:]
    length = len(subS)
    if length <= max_num:
        break
    
    A=[0 for i in range(length-max_num)]

    for i in range(1,length-max_num):
        if i+ A[i-c] < c+A[c]:
            A[i] = A[i-c]
        else:
            j=max(0,c+A[c]-i)
            while i+j<length and subS[j]==subS[i+j]:
                j+=1
            A[i]=j
            if max_num<A[i] and i>=A[i]:
                max_num = A[i]
print(max_num)

### FIXED CODE ###

N=int(input())
S=input()

# Zアルゴリズムで解く

def z_algorithm(S):
    N=len(S)
    A = [N] + [0]*(N-1)
    
    left=1
    length=0
    
    while left < N:
        
        # prefixが同じ間length加算
        while left + length < N and S[length] == S[left+length]:
            length += 1
        
        # prefixの一致数保存
        A[left] = length
        
        # 一文字一致していなければ戻る
        if length == 0:
            left+=1
            continue
        
        # lengthの分だけleftとindexの間を埋めていく(はみ出ていないとこまで)
        k=1
        while k+left < N and k+A[k]<length:
            A[left+k] = A[k]
            k += 1
            
        left += k
        length -= k
    return A

c=0
max_num=0
for sub in range(len(S)):
    subS = S[sub:]
    A=z_algorithm(subS)
    for i,a in enumerate(A):
        if i>=a and max_num<a:
            max_num=a
print(max_num)
### BUGGY CODE ###

from collections import deque
from heapq import *

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
maze = [input() for i in range(H)]

walk_move = [[-1, 0], [0, -1], [0, 1], [1, 0]]
magic_move = []
for i in range(-2, 3):
  for j in range(-2, 3):
    if not i and not j:
      continue
    else:
      magic_move.append([i ,j])
inf = float("inf")
table = [[inf for i in range(W)] for i in range(H)]
table[Ch-1][Cw-1] = 0
queue = [[Ch-1, Cw-1, 0]]

def main():
  while queue:
    x, y, cost = heappop(queue)
    for i, j in walk_move:
      dx = x+i
      dy = y+j
      if 0 <= dx <= H-1 and 0 <= dy <= W-1:
        if maze[dx][dy] == "." and table[dx][dy] > cost:
          table[dx][dy] = cost
          heappush(queue, [dx, dy, cost])
    for k, l in magic_move:
      dx = x+k
      dy = y+l
      if 0 <= dx <= H-1 and 0 <= dy <= W-1:
        if maze[dx][dy] == "." and table[dx][dy] > cost+1:
          table[dx][dy] = cost+1
          heappush(queue, [dx, dy, cost+1])
  return table[Dh-1][Dw-1]
        
ans = main()
if ans == inf:
  print("-1")
else:
  print(int(ans))

### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
maze = [input() for i in range(H)]

queue = deque()
queue.append((Ch-1, Cw-1, 0))
inf = float("inf")
table = [[inf] * W for i in range(H)]
table[Ch-1][Cw-1] = 0
walk_move = ((-1, 0), (0, -1), (1, 0), (0, 1))
magic_move = []
for i in range(-2, 3):
  for j in range(-2, 3):
    if not i and not j:
      continue
    else:
      magic_move.append([i, j])

while queue:
  x, y, cost = queue.popleft()
  for i,j in walk_move:
    dx = x+i
    dy = y+j
    if 0 <= dx <= H-1 and 0 <= dy <= W-1 and maze[dx][dy] == "." and table[dx][dy] > cost:
      queue.appendleft((dx, dy, cost))
      table[dx][dy] = cost
  for k,l in magic_move:
    dx = x+k
    dy = y+l
    if 0 <= dx <= H-1 and 0 <= dy <= W-1 and maze[dx][dy] == "." and table[dx][dy] > cost+1:
      queue.append((dx, dy, cost+1))
      table[dx][dy] = cost+1

ans = table[Dh-1][Dw-1]
if ans == inf:
  print("-1")
else:
  print(table[Dh-1][Dw-1])
### BUGGY CODE ###

n=int(input())
a=[]
for i in range(n):
  b=input()
  if b not in a:
    a.append(b)
print(len(a))

### FIXED CODE ###

n = int(input())
a = set()
for i in range(n):
  a.add(input())
print(len(a))
### BUGGY CODE ###

def main():
    N,K = map(int, input().split())
    A = list(map(int,input().split()))
    P=[0]*N
    for i in range(N):
        P[i]=(A[i]+1)/2
    max=0
    check=0
    for i in range(N-K+1):
        for j in range(i,i+K):
            check +=P[j]
        if check > max:
            max=check
        check=0
    print(max)
main()    

### FIXED CODE ###

def main():
    N,K = map(int, input().split())
    A = list(map(int,input().split()))
    P=[0]*N
    for i in range(N):
        P[i]=(A[i]+1)/2
    max=0
    check=0
            
    #for j in range(K):
    #    check +=P[j]
    check = sum(P[0:K])
    max=check
    #print(check)
    for i in range(K,N):
        check = check+P[i]-P[i-K]
        #print(i)
        #print(check)
        if check > max:
            max=check
    print(max)
main()
### BUGGY CODE ###

def main():
  import sys
  input = sys.stdin.readline
  from collections import deque
  s = list(input())
  s = s[:-1]
  s = deque(s)
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]

  for i in c:
    if i[0] == '1':
      s = deque(reversed(s))
    else:
      if i[1] == '1':
        s.appendleft(i[2])
      else:
        s.append(i[2])
  print(''.join(s))
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
  import sys
  input = sys.stdin.readline
  from collections import deque
  s = list(input())
  s = s[:-1]
  s = deque(s)
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]
  cnt = 1
  for i in c:
    if i[0] == '1':
      cnt *= -1
    else:
      if (i[1] == '1' and cnt == 1) or (i[1] == '2' and cnt == -1):
        s.appendleft(i[2])
      else:
        s.append(i[2])
  if cnt == 1:
    print(''.join(s))
  else:
    print(''.join(s)[::-1])
if __name__ == '__main__':
    main()
### BUGGY CODE ###

n = int(input())
seq = list(map(int, input().split()))
res = 0
i = 0
j = 1
for i in range(n - 1):
  for j in range(i + 1, n):
    res += (seq[i] * seq[j])
print(res  % (10 ** 9 + 7))

### FIXED CODE ###

n = int(input())
seq = list(map(int, input().split()))
a = 10 ** 9 + 7
s = sum(seq)
res = 0
for i in range(n - 1):
  s -= seq[i]
  res += (seq[i] * s) % a
print(res % a)
### BUGGY CODE ###

import numpy as np

N,K = map(int, input().split())
h = np.array(list(map(int, input().split())))
dp = np.full(N,10**10)
dp[0] = 0
for i in range(1,N):
    for j in range(max(0,i-K),i):
        # print(h[j],':',h[i])
        dp[i] = np.fmin(dp[i], dp[j]+ np.abs(h[j]-h[i]))
    # print(dp)
print(int(dp[-1]))

### FIXED CODE ###

import numpy as np

N,K = map(int, input().split())
h = np.array(list(map(int, input().split())))
dp = np.full(N,10**10)
dp[0] = 0
for i in range(1,N):
    start = max(0,i-K)
    dp[i] = np.min(dp[start:i]+np.abs(h[i]-h[start:i]))
print(int(dp[-1]))