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

# https://atcoder.jp/contests/abc158/tasks/abc158_d

S = input()
Q = int(input())

elements = [None for _ in range(Q * 2 + len(S) * 2)]

head = Q + len(S)
last = len(S)
for i, s in enumerate(S):
    elements[head + i] = s

for _ in range(Q):
    query = input()
    if query[0] == '1':
        elements[head:head + last] = elements[head + last - 1:head - 1:-1]
    else:
        if query[2] == '1':
            head -= 1
            elements[head] = query[4]
        else:
            elements[head + last] = query[4]

        last += 1

elements = filter(lambda a: a is not None, elements)
print(''.join(elements))


### FIXED CODE ###

# https://atcoder.jp/contests/abc158/tasks/abc158_d
from collections import deque

S = input()
Q = int(input())

is_reverse = False
text = deque(S)
for _ in range(Q):
    query = input()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if query[2] == '1':
            if is_reverse:
                text.append(query[4])
            else:
                text.appendleft(query[4])
        else:
            if is_reverse:
                text.appendleft(query[4])
            else:
                text.append(query[4])

if is_reverse:
    print(''.join(text)[::-1])
else:
    print(''.join(text))
### BUGGY CODE ###

n=int(input())
s=[''.join(sorted(input())) for _ in range(n)]
ans=0
t=[]
for i in range(n):
    ans+=t.count(s[i])
    t.append(s[i])
print(ans)

### FIXED CODE ###

from scipy import misc
n=int(input())
s=[''.join(sorted(input())) for _ in range(n)]
s=sorted(s)
ans=0
c=1
t=s[0]
for i in range(1,n):
    if t==s[i]:
        c+=1
    else:
        ans+=misc.comb(c,2,True)
        t=s[i]
        c=1
ans+=misc.comb(c,2,True)
print(ans)
### BUGGY CODE ###

N,M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC = sorted(BC, reverse=True, key=lambda x:x[1])
cnt = 0
for B,C in BC:
    A += [C]*B
    cnt += B
    if B >= N:
        break
A = sorted(A, reverse=True)
print(sum(A[:N]))


### FIXED CODE ###

n,m=map(int,input().split())
ans=list(map(int,input().split()))
arr=[list(map(int,input().split())) for _ in range(m)]
arr=sorted(arr,reverse=True,key=lambda x:x[1])
tmp=0
for cnt,val in arr:
  ans+=[val]*cnt
  tmp+=cnt
  if tmp>=n:
    break
ans=sorted(ans,reverse=True)
print(sum(ans[:n]))
### BUGGY CODE ###

from math import sqrt,floor
from sys import stdin

def cntPrime(n):
    mx = sqrt(n) + 1
    if n % 2 == 0:
      l = [0, 1] + [1, 0] * (int(n/2) - 1)
    else:
      l = [0, 1] + [1, 0] * (int(n/2) - 1) + [1]
    c = 3
    while c < mx:
        for k in range(c, floor(n/c) + 1, 1):
            l[c * k -1] = 0
        c = l[:c].index(1) + c
        while c % 2 == 0: 
            c = l[:c].index(1) + c
    return sum(l)
for n in stdin:
    n = int(n)
    print(cntPrime(n))

### FIXED CODE ###

from math import sqrt,floor
from sys import stdin

def cntPrime(n):
    mx = sqrt(n)
    if n % 2 == 0:
      l = [0, 1] + [1, 0] * (int(n/2) - 1)
    else:
      l = [0, 1] + [1, 0] * (int(n/2) - 1) + [1]
    c = 3
    while c < mx:
        for k in range(c*2, n+1, c):
            l[k-1] = 0
        c = l[(c+1):].index(1) + c +2
        while c % 2 == 0:
            c = l[(c+1):].index(1) + c +2
    return l
t = cntPrime(1000000)
for n in stdin:
    n = int(n)
    print(sum(t[:n]))
### BUGGY CODE ###

def main():
    [N, M] = [int(num) for num in input().split()]
    A = [int(num) for num in input().split()]

    for i in range(M):
        expensive = max(A)
        A[A.index(expensive)] = expensive / 2

    A = [a//1 for a in A]
    print(int(sum(A)))

if __name__ == '__main__':
    main()


### FIXED CODE ###

import heapq

def main():
    [N, M] = [int(num) for num in input().split()]
    A = [int(num) for num in input().split()]
    hp = []

    for a in A:
        heapq.heappush(hp, - a)

    for i in range(M):
        expensive = heapq.heappop(hp)
        heapq.heappush(hp, expensive / 2)

    hp = [int(- a // 1) for a in hp]
    print(sum(hp))


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

N = int(input())

A = [int(input()) for _ in range(N)]

for i in range(N):
	ans = max(A[:i] + A[i+1:])
	print(ans)


### FIXED CODE ###

N = int(input())

A = [int(input()) for _ in range(N)]

B = sorted(A)
for i in range(N):
	if A[i] == B[-1]:
		print(B[-2])
	else:
	    print(B[-1])
### BUGGY CODE ###

# 初期入力
import sys
#input = sys.stdin.readline  #文字列では使わない
#S =input().strip()
*S, = input() 
count =0
flag =1
while len(S) >=2 and flag==1:
    flag =0
    for i in range(len(S)):
        if S[i:i+2] ==["1","0"] or S[i:i+2] ==["0","1"]:
            #print(S[i:2])
            del S[i:i+2]
            #print(S,end=" ")
            count +=2
            #print(count)
            flag =1
print(count)

### FIXED CODE ###

#計算でもとまるのか・・・
# 初期入力
import sys
#input = sys.stdin.readline  #文字列では使わない
#*S, = input() 
S =input().strip()
c1 =S.count("1")
c0 =S.count("0")
print(min(c0,c1)*2)
### BUGGY CODE ###

from operator import itemgetter
from math import log,ceil
from fractions import gcd
mod = 10**9 + 7

def lcm(a:int,b:int):
    return a * b // gcd(a, b)

def main():
    N = int(input())
    A  =list(map(int,input().split()))
    neko = 1
    for a in A:
        neko = lcm(neko,a)
    sm = 0
    for a in A:
        sm = (sm + neko // a)%mod
    print(sm)

    return


if __name__ == "__main__":
    main()

### FIXED CODE ###

from operator import itemgetter
from math import log,ceil
from fractions import gcd
mod = 10**9 + 7


def euclid(a:int,b:int)->(int,int):
    #return x,y s.t ax + by = gcd(a,b)
    #a > b >= 0
    if b == 0:
        return 1,0
    x,y = euclid(b,a%b)
    return y,x - y*(a // b)

def inverse(a:int,mod:int)->int:
    #gcd(p,b) == 1
    if mod > a:
        n,x = euclid(mod,a)
    else:
        x,n = euclid(a,mod)
    return x

def devide_mod(a:int,b:int,mod:int)->int:
    #a // b
    x = inverse(b,mod)
    return  (a*x)%mod



def lcm(a:int,b:int):
    return a*b // gcd(a, b)

def main():
    N = int(input())
    A  =list(map(int,input().split()))
    neko = 1
    for a in A:
        neko = lcm(neko,a)
    sm = 0
    neko = neko%mod
    for a in A:
        sm = (sm + devide_mod(neko , a,mod))%mod
    print(sm)

    return


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

N,Q = map(int,input().split())
S = input()

lr = [list(map(int,input().split())) for _ in range(Q)]

#print(N,Q,S)
#print(lr)

#print(S[0:5])

CC=[]
for _ in range(1,N+1):
    CC.append(S[0:_].count('AC'))

#print(CC)

for _ in range(Q):
    l = lr[_][0]
    r = lr[_][1]

    print(CC[r-1]-CC[l-1])

#for _ in range(Q):
#    l = lr[_][0]
#    r = lr[_][1]
    #print(_,S[0:r],S[0:(l-1)])
    #print(S[0:r].count('AC')-S[0:l].count('AC'))
    #print(_,S[l-1:r])
#    print(S[l-1:r].count('AC'))


### FIXED CODE ###

N,Q = map(int,input().split())
S = input()

lr = [list(map(int,input().split())) for _ in range(Q)]

#print(N,Q,S)
#print(lr)

#print(S[0:5])

CC=[0]
for _ in range(1,N):
    if (S[_-1:_+1] == 'AC'):
        CC.append(CC[_-1]+1)
    else:
        CC.append(CC[_-1])

    #CC.append(S[0:_].count('AC'))

#print(CC)

for _ in range(Q):
    l = lr[_][0]
    r = lr[_][1]

    print(CC[r-1]-CC[l-1])

#for _ in range(Q):
#    l = lr[_][0]
#    r = lr[_][1]
    #print(_,S[0:r],S[0:(l-1)])
    #print(S[0:r].count('AC')-S[0:l].count('AC'))
    #print(_,S[l-1:r])
#    print(S[l-1:r].count('AC'))
### BUGGY CODE ###

import sys
from random import randint
input = sys.stdin.readline
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
t = [randint(0, 25) for i in range(D)]
last = [0] * 26
def score(day, score):
  for i in range(26): score -= (day - last[i]) * c[i]
  return score
def run(day):
  global last
  last = [0] * 26
  res = 0
  for d in range(day):
    contest = t[d]
    last[contest] = d + 1
    res = score(d + 1, res) + s[d][contest]
  return res


for d in range(1, D + 1):
  mx = -float("inf")
  mxx = randint(0, 25)
  for x in range(26):
    t[d - 1] = x
    res = run(d)
    if mx < res:
      mx = res
      mxx = x
  t[d - 1] = mxx

for l in range(365, 0, -1):
  last = [0] * 26
  run(l)
  #print(last)
  zmx = -float("inf")
  zmxz = 0
  for z in range(26):
    t[l - 1] = z

    for d in range(l + 1, D + 1):
      mx = -float("inf")
      mxx = t[d - 1]
      for x in range(26):
        t[d - 1] = x
        res = score(d, 0) + s[d - 1][x]
        if mx < res:
          mx = res
          mxx = x
      t[d - 1] = mxx
      last[mxx] = d
    zres = run(D)
    if zres > zmx:
      zmx = zres
      zmxz = z
  t[l - 1] = zmxz

for x in t: print(x + 1)

### FIXED CODE ###

import sys
from random import randint
input = sys.stdin.readline
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]

t = [0] * D
last = [0] * 26
def score(day, score):
  for i in range(26): score -= (day - last[i]) * c[i]
  return score
def run(day):
  global last
  res = 0
  for d in range(day):
    contest = t[d] - 1
    last[contest] = d + 1
    res = score(d + 1, res) + s[d][contest]
  return res


for d in range(1, D + 1):
  mx = 0
  mxx = randint(1, 27)
  for x in range(26):
    t[d - 1] = x
    res = run(d)
    if mx < res:
      mx = res
      mxx = x
  t[d - 1] = mxx
for d in range(1, D + 1):
  mx = 0
  mxx = t[d - 1]
  for x in range(26):
    t[d - 1] = x
    res = run(d)
    if mx < res:
      mx = res
      mxx = x
  t[d - 1] = mxx

for x in t: print(x + 1)
### BUGGY CODE ###

from sys import stdin
f_i = stdin

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

A.sort()

from bisect import bisect_left

def shake_count(power):
    return N ** 2 - sum(bisect_left(A, power - left_hand) for left_hand in A)

lower = A[0] * 2 - 1
upper = A[-1] * 2 + 1
while lower + 1 < upper:
    mid = (lower + upper) // 2
    if shake_count(mid) >= M:
        lower = mid
    else:
        upper = mid

ans = 0
for left_hand in A:
    idx = bisect_left(A, upper - left_hand)
    ans += sum(A[idx:]) + left_hand * (N - idx)

ans += lower * (M - shake_count(upper))

print(ans)

### FIXED CODE ###

from sys import stdin
f_i = stdin

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

A.sort()

from bisect import bisect_left

def shake_count(power):
    return N ** 2 - sum(bisect_left(A, power - left_hand) for left_hand in A)

lower = A[0] * 2 - 1
upper = A[-1] * 2 + 1
while lower + 1 < upper:
    mid = (lower + upper) // 2
    if shake_count(mid) >= M:
        lower = mid
    else:
        upper = mid

X = tuple(bisect_left(A, upper - left_hand) for left_hand in A)

from itertools import accumulate
acc = tuple(accumulate([0] + A))

s = acc[-1]
ans = sum(s - acc[x] for x in X) + sum(a * (N - x) for a, x in zip(A, X))

rest_shake_count = M - (N ** 2 - sum(X))
ans += lower * rest_shake_count

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

c = [0] * 200
while 1:
    try:
        for x in raw_input().lower():
            c[ord(x)] += 1
    except:
        for i in range(ord('a'), ord('z') + 1):
            print "%s : %d" % (chr(i), c[i])

### FIXED CODE ###

c = [0] * 200
while 1:
    try:
        for x in raw_input().lower():
            c[ord(x)] += 1
    except EOFError:
        for i in range(ord('a'), ord('z') + 1):
            print "%s : %d" % (chr(i), c[i])
        break
### BUGGY CODE ###

N, M = map(int, input().split())
flag = True
d = {}

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

for i in range(M):
    a, b = map(int,input().split())
    d[a].append(b)
    d[b].append(a)

if M < N-1:
    flag = False

que = [1]
checked = set()
ans = {1:-1}
if flag:
    while que:
        if len(ans) == N:
            break
        now = que.pop(0)
        for h in d[now]:
            if not h in ans:
                ans[h] = now
                que.append(h)
    
if len(ans) == N:
    print('Yes')
    for i in range(N-1):
        print(ans[i+2]) 
else:
    print('No')



### FIXED CODE ###

from collections import deque

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

d = {}

for i in range(N):
    d[i+1] = set()

for i in range(M):
    a, b = map(int,input().split())
    d[a].add(b)
    d[b].add(a)

que = deque([1])
checked = set()
ans = {1:-1}

while que:
    now = que.popleft()
    for h in d[now]:
        if not h in ans:
            ans[h] = now
            que.append(h)
    if len(ans) == N:
        break
    
if len(ans) == N:
    print('Yes')
    for i in range(N-1):
        print(ans[i+2]) 
else:
    print('No')
### BUGGY CODE ###

import sys
sys.setrecursionlimit(3000)
h,w=map(int,input().split())
s=['#'*(w+2)]+['#'+input()+'#' for _ in [0]*h]+['#'*(w+2)]
l=[[0]*(w+2) for _ in [0]*(h+2)]
for i in range(h+2):
    for j in range(w+2):
        if s[i][j]=='#':
            l[i][j]=-1
def sol(m,n):
    global l
    if l[m][n]>=0:
        for [i,j] in [[m-1,n],[m+1,n],[m,n-1],[m,n+1]]:
            if l[i][j]==0 and [i,j]!=[1,1]:
                l[i][j]=l[m][n]+1
                sol(i,j)
            elif l[i][j]>l[m][n]+1:
                l[i][j]=l[m][n]+1
                sol(i,j)
sol(1,1)
if l[h][w]==0:print(-1)
else:print(h*w-l[h][w]-(sum(x.count('#') for x in s)-2*h-2*w-4)-1)


### FIXED CODE ###

import sys
sys.setrecursionlimit(3000)
h,w=map(int,input().split())
s=['#'*(w+2)]+['#'+input()+'#' for _ in [0]*h]+['#'*(w+2)]
l=[[0]*(w+2) for _ in [0]*(h+2)]
for i in range(h+2):
    for j in range(w+2):
        if s[i][j]=='#':
            l[i][j]=-1
def sol(m,n):
    global l
    if l[m][n]>=0:
        for i,j in [(m-1,n),(m+1,n),(m,n-1),(m,n+1)]:
            if l[i][j]==0 and [i,j]!=[1,1]:
                l[i][j]=l[m][n]+1
                sol(i,j)
            elif l[i][j]>l[m][n]+1:
                l[i][j]=l[m][n]+1
                sol(i,j)
sol(1,1)
if l[h][w]==0:print(-1)
else:print(h*w-l[h][w]-(sum(x.count('#') for x in s)-2*h-2*w-4)-1)
### BUGGY CODE ###

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

def dfs(k, lst, my_lst):
    if k == 0:
        return max(sum(my_lst), 0)
    if len(my_lst) == 0:
        a = dfs(k-1, lst[1:], my_lst + [lst[0]])
        b = dfs(k-1, lst[:-1], my_lst + [lst[-1]])
        return max(a, b)
    if len(lst) == 0:
        return dfs(k-1, lst + [min(my_lst)], sorted(my_lst)[1:])
    else:
        a = dfs(k-1, lst[1:], my_lst + [lst[0]])
        b = dfs(k-1, lst[:-1], my_lst + [lst[-1]])
        c = dfs(k-1, lst + [min(my_lst)], sorted(my_lst)[1:])
        d = dfs(k-1, [min(my_lst)] + lst, sorted(my_lst)[1:])
        return max(a,b,c,d)
    
print(dfs(k, lst, []))

### FIXED CODE ###

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

res = 0
max_num = min(n, k)
for i in range(max_num+1):
    for j in range(max_num+1 - i):
        my_lst = []
        my_lst += lst[:i]
        if j != 0:
            my_lst += lst[-j:]
        
        s = sum(my_lst)
        sorted_my_lst = sorted(my_lst)
        #print('my_lst :', my_lst)
        for l in range(k- i - j):
            if len(my_lst) > l:
                if sorted_my_lst[l] < 0:
                    s -= sorted_my_lst[l]
        res = max(res, s)

print(res)
### BUGGY CODE ###

import numpy as np

N, K, Q = map(int, input().split())

indexes = np.array(list(int(input()) for _ in range(Q)))-1

scores = np.array([K for _ in range(N)])

for i in indexes:
    scores[i] = scores[i]+1
    scores = scores-1

for j in scores:
    if j > 0:
        print('Yes')
    else:
        print('No')


### FIXED CODE ###

N, K, Q = map(int, input().split())

right_answers = list(int(input()) for _ in list(range(Q)))
indexes = [v-1 for v in right_answers]

scores = [K-Q for _ in range(N)]

for i in indexes:
    scores[i] = scores[i]+1

for j in scores:
    if j > 0:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

import functools
import operator


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

j = 1000000000000000000

ans = 1

'''
for i in range(n) :
    ans *= An[i]
'''
result = functools.reduce(operator.mul, An)

if result <= j:
    print(result)
    
else :
    print('-1')



### FIXED CODE ###


n = int(input())
An = [int(x) for x in input().split()]
An.sort(reverse=True)

if 0 in An:
    print(0)
    exit()

j = 10 ** 18
ans = 1
for i in range(n):
    if ans > j:
        print(-1)
        quit()
    ans *= An[i]

if ans > j :
    print(-1)
    
else :
    print(ans)
### BUGGY CODE ###

s = input()
k = int(input())
l = len(s)
substrings = [None]*(l*(l+1)//2)
ind = 0
for i in range(1,l+1):
    for j in range(l-i+1):
        substrings[ind] = s[j:j+i]
        ind += 1
substrings = sorted(set(substrings))
print(substrings[k-1])

### FIXED CODE ###

s = input()
k = int(input())
l = len(s)
substrings = []
for i in range(1,k+1):
    for j in range(l-i+1):
        substrings.append(s[j:j+i])
substrings = sorted(set(substrings))
print(substrings[k-1])
### BUGGY CODE ###

n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    while(s>x):
        count+=1
        a[ind+1]-=1
        s=i+a[ind+1]
print(count)
    


### FIXED CODE ###

n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    if(s>x):
        count+=s-x
        a[ind+1]-=s-x
        #s=i+a[ind+1]
print(count)
### BUGGY CODE ###

import re
S = input()
s = [S]
count = 0
while True:
    s = re.subn('ABCBC','BCABC',s[0])
    count += s[1]
    s = re.subn('AABC','ABCA',s[0])
    count += s[1]
    s = re.subn('ABC','BCA',s[0])
    if s[1]:
        count += s[1]
    else:
        break
print(str(count))

### FIXED CODE ###

import re
S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = re.split('B|C',N1)
 
count = 0
for l in S_li:
    a = sum([i for i,x in enumerate(l) if '1' in x])
    su=sum([x for x in range(l.count('1'))])
    count += (a-su)
print(str(count))
### BUGGY CODE ###

import bisect

n = int(input())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
c = sorted(map(int, input().split()))

ans = 0
bs = 0
for ai in range(n):
    bs = bs + bisect.bisect_right(b[bs:], a[ai])
    cs = 0
    for bi in range(bs, n):
        cs = cs + bisect.bisect_right(c[cs:], b[bi])
        ans = ans + (n-cs)

print(ans)

### FIXED CODE ###

import bisect

n = int(input())
a = sorted(map(int, input().split()))
b = sorted(map(int, input().split()))
c = sorted(map(int, input().split()))

ans = 0
for bb in b:
    ai = bisect.bisect_left(a, bb)
    ci = bisect.bisect_right(c, bb)
    ans = ans + (ai * (n-ci))

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

n, k = map(int,input().split())
a = list(map(int,input().split()))
def trans(v):
  l = [0]*(k+1)
  for i in range(k+1):
    l[i] = (v&1)
    v >>= 1
  return l

a.sort()
lef = [1]*n
rig = [1]*n
mask = (1 << (k+1)) - 1
for i in range(n-1):
  lef[i+1] = lef[i] | (lef[i] << a[i]) & mask
  rig[-i-2] = rig[-i-1] | (rig[-i-1] << a[-i-1]) & mask
ok = -1
ng = n
while ng - ok > 1:
  mid = (ng + ok) // 2
  lc = lef[mid]
  rc = rig[mid]
  cov = ((1 << k) - 1) ^ ((1 << max(k - a[mid], 0)) - 1)
  for i in range(k):
    if lc & 1 and rc & cov:
      ng = mid
      break
    lc >>= 1
    cov >>= 1
  else:
    ok = mid
print(ok + 1)

### FIXED CODE ###

n, k = map(int,input().split())
a = [k+1 if x > k else x for x in map(int,input().split())]
a.sort()
lef = [1]*n
rig = [1]*n
mask = (1 << (k+1)) - 1
for i in range(n-1):
  lef[i+1] = lef[i] | (lef[i] << a[i]) & mask
  rig[-i-2] = rig[-i-1] | (rig[-i-1] << a[-i-1]) & mask
ok = -1
ng = n
while ng - ok > 1:
  mid = (ng + ok) // 2
  if a[mid] >= k:
      ng = mid
      continue
  lc = lef[mid]
  rc = rig[mid]
  cov = ((1 << k) - 1) ^ ((1 << (k - a[mid])) - 1)
  for i in range(k):
    if lc & 1 and rc & cov:
      ng = mid
      break
    lc >>= 1
    cov >>= 1
  else:
    ok = mid
print(ok + 1)
### BUGGY CODE ###

S = input()
Q = int(input())
k = 1
l = ""
li = ""
r = ""
ri = ""
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                l = c + l
                li = li + c
            else:
                r = r + c
                ri = c + ri
        elif f == '2':
            if k > 0:
                r = r + c
                ri = c + ri
            else:
                l = c + l
                li = li + c

if k > 0:
    print(l + S + r)
else:
    print(ri + S[::-1] + li)


### FIXED CODE ###

from collections import deque
S = input()
Q = int(input())
k = 1
d = deque(S)
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                d.appendleft(c)                
            else:
                d.append(c)
        elif f == '2':
            if k > 0:
                d.append(c)
            else:
                d.appendleft(c)
d = list(d)
if k > 0:
    print(''.join(d))
else:
    print(''.join(d[::-1]))
### BUGGY CODE ###

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

while M > 0:
    A[0] //= 2
    for i in range(1, N):
        if A[0] > A[i]:
            A = A[1:i] + [A[0]] + A[i:]
            break
    else:
        A = A[1:] + [A[0]]
    M -= 1
print(sum(A))


### FIXED CODE ###

import heapq


class Reverse:
    def __init__(self, val):
        self.val = val

    def __lt__(self, other):
        return self.val > other.val

    def __repr__(self):
        return repr(self.val)


class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        if len(self.hq) == 0:
            return None
        if self.desc:
            return heapq.heappop(self.hq).val
        else:
            return heapq.heappop(self.hq)

    def push(self, a):
        if self.desc:
            heapq.heappush(self.hq, Reverse(a))
        else:
            heapq.heappush(self.hq, a)


N, M = map(int, input().split())
q = Heapq(list(map(int, input().split())), desc=True)

while M > 0:
    a = q.pop() // 2
    q.push(a)
    M -= 1

tmp = 0
while True:
    try:
        a = q.pop()
        tmp += a
    except:
        break
print(tmp)
### BUGGY CODE ###

import numpy as np
N,M,Q = map(int,input().split())
LR = []
matrix = np.zeros((N,N)).astype('int')
for i in range(M):
    l,r = map(int,input().split())
    matrix[l-1][r-1] += 1
for i in range(Q):
    p,q = map(int,input().split())
    print(np.sum(matrix[p-1:q,p-1:q]))

### FIXED CODE ###

import sys
input = sys.stdin.readline
N,M,Q = map(int,input().split())
LR = []
matrix = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(M):
    l,r = map(int,input().split())
    matrix[l][r] += 1
for i in range(N+1):
    for j in range(N):
        matrix[i][j+1] += matrix[i][j]
for i in range(N+1):
    for j in range(N):
        matrix[j+1][i] += matrix[j][i]
for i in range(Q):
    p,q = map(int,input().split())
    ans = matrix[q][q]-matrix[q][p-1]-matrix[p-1][q]+matrix[p-1][p-1]
    print(ans)
### BUGGY CODE ###

from sys import stdin
import numpy as np
import math

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

for i in range(n):
    point = 0
    lin = li[:]
    lin.pop(i)
    lin = [0] + lin + [0]
    for j in range(1,len(lin)):
        point += abs(lin[j]-lin[j-1])
    print(point)


### FIXED CODE ###

from sys import stdin
import numpy as np
import math
from itertools import accumulate

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

point = 0
lin = li[:]
lin = [0] + lin + [0]
for i in range(1,len(lin)):
    point += abs(lin[i]-lin[i-1])

for j in range(1,len(lin)-1):
    print(point-(abs(lin[j]-lin[j-1])+abs(lin[j+1]-lin[j]))+abs(lin[j+1]-lin[j-1]))