Harryxun/stf_run huggingface dataset:
def comb(n, r):
    if r<0 or r>n:
        return 0
    r=min(r, n-r)
    return g1[n]*g2[r]*g2[n-r]%MOD

MOD=10**9+7
MAXN=100000*2+10
g1=[1, 1]
g2=[1, 1]
inverse=[0, 1]

for i in range(2, MAXN+1):
    g1.append((g1[-1]*i)%MOD)
    inverse.append(-inverse[MOD%i]*(MOD//i)%MOD)
    g2.append((g2[-1]*inverse[-1])%MOD)

r1, c1, r2, c2=map(int, input().split())
s1=comb(r2+c2+2, r2+1)
s2=comb(r1+c2+1, r1)
s3=comb(r2+c1+1, r2+1)
s4=comb(r1+c1, r1)
print(s1-s2-s3+s4)


### FIXED CODE ###

def comb(n, r):
    if r<0 or r>n:
        return 0
    r=min(r, n-r)
    return g1[n]*g2[r]*g2[n-r]%MOD

MOD=10**9+7
MAXN=2000000+10
g1=[1, 1]
g2=[1, 1]
inverse=[0, 1]

for i in range(2, MAXN+1):
    g1.append((g1[-1]*i)%MOD)
    inverse.append(-inverse[MOD%i]*(MOD//i)%MOD)
    g2.append((g2[-1]*inverse[-1])%MOD)

r1, c1, r2, c2=map(int, input().split())
s1=comb(r2+c2+2, r2+1)-1
s2=comb(r1+c2+1, r1)-1
s3=comb(r2+c1+1, r2+1)-1
s4=comb(r1+c1, r1)-1
print((s1-s2-s3+s4)%MOD)
from collections import Counter
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    CNT = Counter(A + B)
    num = CNT.most_common()[0][1]
    if num > N:
        print("No")
        return

    j = 0
    for i in range(N):
        if A[i] == B[i]:
            for _ in range(N):
                j += 1
                if A[i] != B[j] and A[j] != B[i]:
                    B[i], B[j] = B[j], B[i]
                    break
                if j == N - 1:
                    j = 0

    print('Yes')
    print(*B)


if __name__ == "__main__":
    resolve()


### FIXED CODE ###


from collections import Counter
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    CNT = Counter(A+B)
    num = CNT.most_common()[0][1]
    if num > N:
        print("No")
        return 

    j, pre = 0, -1
    for i in range(N):
        if A[i] != pre:
            j = 0
        if A[i] == B[i]:
            while j < N:
                if A[i] != B[j] and A[j] != B[i]:
                    B[i], B[j] = B[j], B[i]
                    break
                j += 1
        pre = A[i]

    print('Yes')
    print(*B)


if __name__ == "__main__":
    resolve()
k = input()
a, b = map(int, input().split())
if (b % k) - (a % k) <= 0 or b - a >= 0:
  print("OK")
else:print("NG")


### FIXED CODE ###

k = int(input())
a, b = map(int, input().split())
ka = a // k
kb = b // k
if b - a >= k - 1:
  print("OK")
elif kb - ka > 0:
  print("OK")
elif a % k == 0 or b % k == 0:
  print("OK")
else:
  print("NG")
n = int(input())
alist = [int(input())-1 for _ in range(n)]

visitedlist = [0]
from = 0
for _ in range(n):
    to = alist[from]
    if to in visitedlist:
        print(-1)
        break
    elif to == 1:
        print(len(visitedlist))
        break
    else:
        visitedlist.append(to)
        from = to


### FIXED CODE ###

n = int(input())
alist = [int(input())-1 for _ in range(n)]

now = 0
count = 0
while True:
    if now == 1:
        print(count)
        break
    elif count >= n:
        print(-1)
        break
    else:
        count += 1
        now = alist[now]
# coding: utf-8
A, B = map(int, input().split())
B = B-A
for i in range(20):
	if B<=0:
    	print(i+1)
    	break()
    B=B-A+1

### FIXED CODE ###

# coding: utf-8

A,B = map(int, input().split())
for i in range(20):
  if (A-1)*i+1 >= B:
    print(i)
    break
K, N = map(int, input().split())
houselist = list(map(int, input().split()))
max=0
for i in range(1,N):
  if houselist[i]-houselist[i-1]>max:
    max=houselist[i]-houselist[i-1]
if K+houselist[0]-houselist[N]>max:
  max=K+houselist[0]-houselist[N]
if max==0:
  print(0)
else:
  print(K-max)


### FIXED CODE ###

K, N = map(int, input().split())
houselist = list(map(int, input().split()))
max=0
for i in range(1,N):
  if houselist[i]-houselist[i-1]>max:
    max=houselist[i]-houselist[i-1]
if K+houselist[0]-houselist[N-1]>max:
  max=K+houselist[0]-houselist[N-1]
if max==0:
  print(0)
else:
  print(K-max)
import numpy as np

A = np.empty(9)
b = np.empty(10)
place = ''

for i in range(0,7,3):
  A[i], A[i+1], A[i+2] = map(int,input().split())

N = int(input())

for n in range(N):
  b[n] = int(input())
  for j in range(9):
  	if b[n] == A[j]:
  		place = place + ' ' + str(j)

ans='No'

for i in range(0,7,3):
  if str(i) and str(i+1) and str(i+2) in place:
  	ans = 'Yes'

for i in range(3):
  if str(i) and str(i+3) and str(i+6) in place:
  	ans = 'Yes'

if '0' and '4' and '8' in place:
	ans = 'Yes'
if '2' and '4' and '6' in place:
	ans = 'Yes'

print(ans)


### FIXED CODE ###


N = input()

ans = -(-int(N) // 2)

print(ans)
N = int(input())
T,A  =[int(i) for i in input().split()]
H = [int(i) for i in input.split()]
diff = (abs(A-(T-H[0]*0.006)))
palace = 0
for i in range(H):
    if(abs(A-(T-H[i]*0.006)) < diff):
        palace = i
        diff = abs(A-(T-H[i]*0.006))
print(palace)


### FIXED CODE ###

N = int(input())
T,A  =[int(i) for i in input().split()]
H = [int(i) for i in input().split()]
diff = (abs(A-(T-H[0]*0.006)))
palace = 0
for i in range(len(H)):
    if(abs(A-(T-H[i]*0.006)) < diff):
        palace = i
        diff = abs(A-(T-H[i]*0.006))
print(palace+1)
n,*a=map(int,open(0).read().split())
m=6**8
c=[0]*m
p=[1,1]+c[:]
for i in range(m):
  if p[i]<1:
    for j in range(i,m,i):p[j]=i
for a in a:
  c[a]+=1
  while p[a]!=a:
    a//=p[a]
    c[a]+=1
1/(max(c[2:])==n)
print(('not','psaeitr'[max(c[2:])>1::2]+'wise')[max(c[2:])<n],'coprime')

### FIXED CODE ###

n,*a=map(int,open(0).read().split())
m=6**8
c=[0]*m
p=[1,1]+c[:]
for i in range(m):
  if p[i]<1:
    for j in range(i,m,i):p[j]=i
for a in a:
  s={a}
  while a!=p[a]:
    s|={p[a]}
    a//=p[a]
    s|={a}
  for i in s:c[i]+=1
print(('not','psaeitr'[max(c[2:])>1::2]+'wise')[max(c[2:])<n],'coprime')
while True:
    m, f, r = [int(i) for i in input().solit()]

    if m == f == r == -1:
        break

    if m == -1 or f == -1:
        print("F")

    if m + f >= 80:
        print("A")

    if 65 <= m + f < 80:
        print("B")

    if 50 <= m + f < 65:
        print("C")

    if 30 <= m + f < 50:
        print("D")

    elif r >= 50:
        print("C")

    if m + f < 30:
        print("F")

### FIXED CODE ###

while True:
    m, f, r = [int(i) for i in input().split()]

    if m == f == r == -1:
        break

    elif m == -1 or f == -1:
        print("F")

    elif m + f >= 80:
        print("A")

    elif 65 <= m + f < 80:
        print("B")

    elif 50 <= m + f < 65:
        print("C")

    elif 30 <= m + f < 50:
        if r >= 50:
            print("C")
        else:
            print("D")

    elif m + f < 30:
        print("F")

    elif m + f < 30:
        print("F")
a,b= map(int,input().split())
ans=[1,2,3]
ans.remove(a)
ans.remove(b)
print(ans[0])

### FIXED CODE ###

a=int(input())
b=int(input())

ans=[1,2,3]
ans.remove(a)
ans.remove(b)
print(ans[0])
a,b=int(input().split())
print(max(a-b*2,0))

### FIXED CODE ###

a,b=map(int,input().split())
print(max(a-b*2,0))
s = str(input())
k = len(s)

cnt=0
for i in range(k):
  if s[i]=!s[int((n+1)/2)-i]:
    cnt+=1
  if s[int((n+3)/2)+i]=!s[int((3*n+3)/2)-i]:
    cnt+=1

if cnt==0:
  print('Yes')
else:
  print('No')


### FIXED CODE ###

s = input()
n = len(s) // 2

if s[:n] == s[-n:]:
    print('Yes')
else:
    print('No')
input=list(input())
  
 if int(input[0])==int(input[1]) or int(input[1])==int(input[2]) or int(input[2])==int(input[3]):
          print("Bad")
 else:
          print("Good")

### FIXED CODE ###

input=list(input())


if int(input[0])==int(input[1]) or int(input[1])==int(input[2]) or int(input[2])==int(input[3]):
	 print("Bad")
else:
	print("Good")
import sys
input=sys.stdin.readline
h,w,k=map(int,input().split())
board=list(input() for _ in range(h))
num=0
cutting=[[0 for _1 in range(w)] for _2 in range(h)]
for row in range(h):
  num+=1
  if board[row].count("#")==0:
    continue
  col=0
  while board[row][col]==".":
    cutting[row][col]=num
    col+=1
  cutting[row][col]=num
  col+=1
  while col < w:
    cutting[row][col]=num
    col+=1
    if board[row][col]=="#":
      num+=1
centrow = (h-1)//2
for row in range(centrow, h):
  if cutting[row][0]==0:
    cutting[row]=cutting[row-1]
  if cutting[h-1-row][0]==0:
    cutting[h-1-row]=cutting[h-row]

for row in range(h):
  print(*cutting[row])


### FIXED CODE ###

import sys
input=sys.stdin.readline
h,w,k=map(int,input().split())
board=list(input() for _ in range(h))
num=0
cutting=[[0 for _1 in range(w)] for _2 in range(h)]
for row in range(h):
  if board[row].count("#")==0:
    continue
  num+=1
  col=0
  while board[row][col]==".":
    cutting[row][col]=num
    col+=1
  cutting[row][col]=num
  col+=1
  while col < w:
    if board[row][col]=="#":
      num+=1
    cutting[row][col]=num
    col+=1
    
for row in range(1,h):
  if cutting[row][0]==0:
    cutting[row]=cutting[row-1]
  if cutting[h-1-row][0]==0:
    cutting[h-1-row]=cutting[h-row]

for row in range(h):
  print(*cutting[row])
from fractions import gcd
n,k = map(int,input().split())

def process(a,b):
	flag = True
	while flag:
		a,b = max(a,b),min(a,b)
		a = a%b
		if a == 0:
			flag = False

	for i in range(1000):
		a = min(a,abs(a-b))
	return a
if n == 0:
	print(0)
elif k == 1 or n == 1:
	print(0)
else:
	print(process(n,k))




### FIXED CODE ###

x,y,z = map(int,input().split())

x,y = y,x
x,z = z,x
print(x,y,z)
N = int(input())

def gcd(x, y):
    if(y == 0):
        return x
    else:
        return gcd(y, x%y)

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

ans = int(input())  
for _ in range(N-1):
    T = int(input())
    ans = lcm(ans, T)
print(int(ans))

### FIXED CODE ###

import fractions

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

def lcm(x, y):
    return (x*y)//fractions.gcd(x, y)

ans = T[0]

for i in range(N):
    ans = lcm(ans, T[i])

print(ans)
import math
A, B = input().split()
print(math.floor(Decimal(A)*Decimal(B)))

### FIXED CODE ###

from decimal import *
import math
A, B = input().split()
print(math.floor(Decimal(A) * Decimal(B)))
a, b = map(int , input().split())
if (a*b) % 2 = 1:
    print("Odd")
else:
    print("Even")


### FIXED CODE ###

a, b = map(int , input().split())
if (a*b) % 2:
    print("Odd")
else:
    print("Even")
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  field=[list(S()) for _ in range(h)]

  q=collections.deque()
  nokori=h*w
  dist=[[-1]*w for _ in range(h)]
  for i in range(h):
    for j in range(w):
      if field[i][j]=='#':
        q.append((i,j))
        dist[i][j]=0
        nokori-=1

  while len(q)>0:
    if nokori==0:
      return d

    y,x=q.popleft()

    for dy,dx in dd:
      ny=y+dy
      nx=x+dx

      if 0<=ny<h and 0<=nx<w:
        if field[ny][nx]=='.':
          field[ny][nx]='#'
          d=dist[y][x]+1
          dist[ny][nx]=d
          q.append((ny,nx))
          nokori-=1

  return d

# main()
print(main())


### FIXED CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  h,w=LI()
  field=[list(S()) for _ in range(h)]
  d=0

  q=collections.deque()
  nokori=h*w
  dist=[[-1]*w for _ in range(h)]
  for i in range(h):
    for j in range(w):
      if field[i][j]=='#':
        q.append((i,j))
        dist[i][j]=0
        nokori-=1

  while q:
    if nokori==0:
      return d

    y,x=q.popleft()

    for dy,dx in dd:
      ny=y+dy
      nx=x+dx

      if 0<=ny and ny<h and 0<=nx and nx<w:
        if field[ny][nx]=='.':
          field[ny][nx]='#'
          d=dist[y][x]+1
          dist[ny][nx]=d
          q.append((ny,nx))
          nokori-=1

  return d

# main()
print(main())
N = int(input())
h = list(map(int,input().split()))

for i in range(1,N):
    if h[i-1] > h[i]:
        h[i-1] -= 1

for i in range(1,N):
    if h[i-1] <= h[i]:
        flag = 1
    else:
        flag = 0
        break

if flag == 1:
    print('Yes')
else:
    print('No')


### FIXED CODE ###

N = int(input())
h = list(map(int,input().split()))
ans = True
for i in range(N-2,-1,-1):
    if h[i] > h[i+1]:
        h[i]-=1
for i in range(N-1):
    if h[i] <= h[i+1]:
        ans = True
    else:
        ans = False
        break
if ans:
    print('Yes')
else:
    print('No')
flag = False
for i in range(len(s)-1):
    if int(s[i]) == int(s[i+1]):
        flag = True

if flag:
    print('Bad')
else:
    print('Good')

### FIXED CODE ###

s = input()
flag = False
for i in range(len(s)-1):
    if int(s[i]) == int(s[i+1]):
        flag = True

if flag:
    print('Bad')
else:
    print('Good')
def main(lines):
	line = lines  

if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)


### FIXED CODE ###

import sys
def main(lines):
  n = int(lines[0])
  a = []
  hei = lines[1].split()
  for i in range(n):
    a.append(int(hei[i]))
  tmp = a[0]
  ans = 0
  for i in range(1,n):
    if tmp < a[i]:
      tmp = a[i]
    else:
      ans += tmp-a[i]
  print(ans)

if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)
import math
N,K=map(int,input().split())
a=list(map(int,input().split()))
bef=a[0]
gcd=a[0]
max_=a[0]
for i in range(1,N):
    if max_<a[i]:
        max_=a[i]
    gcd=math.gcd(gcd,a[i])
if max_<K:
    print("IMPOSSIBLE")
else:
    if K%gcd==0:
        print("POSSIBLE")
    else:
        print("IMPOSSIBLE")

### FIXED CODE ###

import fractions
N,K=map(int,input().split())
a=list(map(int,input().split()))
bef=a[0]
gcd=a[0]
max_=a[0]
for i in range(1,N):
    if max_<a[i]:
        max_=a[i]
    gcd=fractions.gcd(gcd,a[i])
if max_<K:
    print("IMPOSSIBLE")
else:
    if K%gcd==0:
        print("POSSIBLE")
    else:
        print("IMPOSSIBLE")
a, b = map(int, input().split())
print(max(max((a + b), (a - b)), (a * b))

### FIXED CODE ###

a, b = map(int, input().split())
print(max(max((a + b), (a - b)), (a * b)))
from scipy.misc import comb
N = int(input())
A = sorted(list(map(int, input().split())), reverse = True)

if N == 2:
	print(A[0], A[1])
else:
	q = max(A)
	B = list(map(lambda x : abs(q // 2 - x),  A))
	print(q, A[B.index(min(B))])

### FIXED CODE ###

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

if N == 2:
	print(A[0], A[1])
else:
	q = max(A)
	B = list(map(lambda x : abs(q // 2 - x),  A))
	print(q, A[B.index(min(B))])
# -*- coding: utf-8 -*-
from collections import  deque
from heapq import heappush, heappop, heapify, merge
from collections import defaultdict
import sys
sys.setrecursionlimit(10**10)
def inpl(): return tuple(map(int, input().split()))
 
N, M = inpl()
A = inpl()
tree = [[-1, 1]  for _ in range(N)]# [next, rank]

def find(i):
    if tree[i][0] == -1:
        group = i
    else:
        group = find(tree[i][0]) 
        tree[i][0] = group
    return group
  
def unite(x, y):
    px = find(x)
    py = find(y)
      
    if tree[px][1] == tree[py][1]: # rank is same
        tree[py][0] = px
        tree[px][1] += 1
    else:
        if tree[px][1] < tree[py][1]:
            px, py = py, px
        tree[py][0] = px
  
for _ in range(M):
    x, y = tuple(map(int, input().split()))
    if not int(find(x) == find(y)):
        unite(x, y)

D = defaultdict(list)
for n in range(N):
    D[find(n)].append(A[n])

H = []
S = deque([]) # single
for k, v in D.items():
    if len(v) == 1:
        S.append(v)
    else:
        H.append(sorted(v))
heapify(H)

res = 0 
while True:
    if len(H) == 0 or len(S) + len(H) <= 1:
        break   
    if S:
        A = S.leftpop()
    else:
        A = heappop(H)
    B = heappop(H)
    res += A[0] + B[0]
    C = list(merge(A[1:], B[1:]))
    if len(C) == 1:
        S.append(C)
    else:
        heappush(H, C)

if len(S) > 2:
    print("Impossible")
    exit()
elif len(S) == 2:
    res += S[0]+S[1]
elif len(S) == 1:
    A = S[0]
    if len(H) == 1:
        B = heappop(H)
        res += A[0] + B[0]
else:
    pass
print(res)

### FIXED CODE ###

# -*- coding: utf-8 -*-
from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)
def inpl(): return tuple(map(int, input().split()))
 
N, M = inpl()
A = inpl()
tree = [[-1, 1]  for _ in range(N)] # [next, rank]
 
def find(i):
    if tree[i][0] == -1:
        group = i
    else:
        group = find(tree[i][0]) 
        tree[i][0] = group
    return group
  
def unite(x, y):
    px = find(x)
    py = find(y)
      
    if tree[px][1] == tree[py][1]: # rank is same
        tree[py][0] = px
        tree[px][1] += 1
    else:
        if tree[px][1] < tree[py][1]:
            px, py = py, px
        tree[py][0] = px
  
for _ in range(M):
    x, y = tuple(map(int, input().split()))
    if not int(find(x) == find(y)):
        unite(x, y)
 
D = defaultdict(list)
for n in range(N):
    D[find(n)].append(A[n])
 
H = []
res = 0 

for k, v in D.items():
    v = sorted(v)
    res += v[0]
    H.extend(v[1:])
 
if N < 2*(N-M-1):
    print("Impossible")
elif M == N-1:
    print(0)
else:
    res += sum(sorted(H)[:2*(N-M-1) - len(D.keys())])
    print(res)
N = 2
ARR = [157, 193]

N = 6
ARR = [200, 180, 160, 140, 120, 100]

N = 7
ARR = [100, 130, 130, 130, 115, 115, 150]

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


# N = 5
# ARR = [157, 193, 193, 193, 191]

def prepare(n, arr):
    result = []
    for i in range(n):
        if i == 0:
            result.append(arr[0])
        else:
            if arr[i] != arr[i - 1]:
                result.append(arr[i])
    return result


def calculate(arr):
    n = len(arr)
    result = []
    for i in range(n):
        if i == n-1:
            if arr[i] > arr[i-1]:
                result.append({'type': 1, 'value': arr[i]})
            if arr[i] < arr[i-1]:
                result.append({'type': 0, 'value': arr[i]})
            continue

        if arr[i + 1] > arr[i]:
            result.append({'type': 0, 'value': arr[i]})
        if arr[i+1] < arr[i]:
            result.append({'type': 1, 'value': arr[i]})

    money = 1000
    num = 0
    history = []
    for res in result:
        type = res.get('type')
        value = res.get('value')
        if type == 0:
            num += money // value
            money -= num * value
        else:
            money += num * value
            history.append(money)
            num = 0

    print(max(history))

# arr = prepare(N, ARR)
calculate(ARR)


### FIXED CODE ###

N = 2
ARR = [157, 193]

N = 6
ARR = [200, 180, 160, 140, 120, 100]

N = 7
ARR = [100, 130, 130, 130, 115, 115, 150]

N = int(input())
ARR = list(map(int, input().split()))
#

# N = 5
# ARR = [111, 111, 111, 111, 111]

def prepare(n,arr):
    result = []
    for i in range(n):
        if i == 0:
            result.append(arr[0])
        else:
            if arr[i] != arr[i-1]:
                result.append(arr[i])
    return result


def calculate(arr):
    n = len(arr)
    result = []
    for i in range(n):
        if i == n-1:
            if arr[i] > arr[i-1]:
                result.append({'type': 1, 'value': arr[i]})
            continue

        if i == 0:
            if arr[1] > arr[0]:
                result.append({'type': 0, 'value': arr[i]})
            continue

        if (arr[i + 1] > arr[i]) and (arr[i] < arr[i - 1]):
            result.append({'type': 0, 'value': arr[i]})

        if (arr[i + 1] < arr[i]) and (arr[i] > arr[i - 1]):
            result.append({'type': 1, 'value': arr[i]})

    money = 1000
    num = 0
    for index,res in enumerate(result):
        type = res.get('type')
        value = res.get('value')

        if index == (len(result) - 1):
            if value == 1:
                continue

        if type == 0:
            tmpNum = money // value
            num += tmpNum
            money -= value * tmpNum
        else:
            money += num * value
            num = 0

    print(money)


arr = prepare(N, ARR)
calculate(arr)
S = [input()]
q = int(input().strip())

for i in range(q):
  query = input()
  if query[0] == "1":
    S.reverse()
  elif query[0] == "2":
    query = query.strip().split(" ")
    if query[1] == "1":
      S.insert(query[2])
    else:
      S.append(query[2])

print("".join(S))

### FIXED CODE ###

from collections import deque

S = deque(input())
q = int(input().strip())


rev_count = 0
for i in range(q):
  query = input()
  if query[0] == "1":
    rev_count += 1
  elif query[0] == "2":
    query = query.strip().split(" ")
    if rev_count & 1 == 1:
      if query[1] == "1":
        S.append(query[2])
      else:
        S.appendleft(query[2])
    else:
      if query[1] == "1":
        S.appendleft(query[2])
      else:
        S.append(query[2])

if rev_count & 1 == 1:
  S.reverse()

print("".join(S))
if field[0][0] == '#':
    print(-1)
elif dist[(H - 1, W - 1)] == -1:
    print(-1)
else:
    steps_min = dist[(H - 1, W - 1)] + 1
    n_white = W * H - n_black
    ans = n_white - steps_min
    print(ans)


### FIXED CODE ###

#!/usr/bin/env python3

from collections import deque

H, W = map(int, input().split())

field = []
for _ in range(H):
    line = list(input())
    field.append(line)
# print(H, W, field)


def is_valid(i, j):
    cond_i = 0 <= i <= H - 1
    cond_j = 0 <= j <= W - 1
    return cond_i and cond_j


def is_reachable(i, j, ):
    if is_valid(i, j):
        if field[i][j] == '.':
            return True
    return False


def adj(i, j):
    up, down = [i - 1, j], [i + 1, j]
    left, right = [i, j - 1], [i, j + 1]
    res = []
    for i_adj, j_adj in up, down, left, right:
        if is_reachable(i_adj, j_adj):
            res.append([i_adj, j_adj])
    return res


def bfs(que, dist):
    while len(que) > 0:
        i, j = que.popleft()
        # print(f"i, j: {i}, {j}")
        for i_adj, j_adj in adj(i, j):
            # print(f"i_adj, j_adj: {i_adj}, {j_adj}")
            if dist[(i_adj, j_adj)] == -1:
                que.append([i_adj, j_adj])
                dist[(i_adj, j_adj)] = dist[(i, j)] + 1


que = deque()
keys = [(i, j) for i in range(H) for j in range(W)]
values = [-1 for _ in range(H * W)]
dist = dict(zip(keys, values))
# print(dist)

n_black = 0
for i in range(H):
    for j in range(W):
        if field[i][j] == '#':
            n_black += 1

que.append([0, 0])
dist[(0, 0)] = 0
bfs(que, dist)

if dist[(H - 1, W - 1)] == -1:
    print(-1)
else:
    steps_min = dist[(H - 1, W - 1)] + 1
    n_white = W * H - n_black
    ans = n_white - steps_min
    print(ans)
N = int(input())
A = list(map(int,input().split()))
dp = [0] * (N+10)
dp[2] = abs(A[1] - A[0])
for i in range(2,N):
    Ai2 = abs(A[i] - A[i-2])
    dp[i+1] = min(dp[i]+,dp[i-1]+Ai2)

print(dp[N])

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
dp = [0] * (N+10)
dp[2] = abs(A[1] - A[0])
for i in range(2,N):
    Ai = abs(A[i] - A[i-1])
    Ai2 = abs(A[i] - A[i-2])
    dp[i+1] = min(dp[i]+Ai,dp[i-1]+Ai2)

print(dp[N])
n, k = map(int, input().split())
ans = 0

for i in range(n):
    if k ** (i - 1) <= n < k ** i:
        ans = i
        break
if ans == 0:
    ans += 1
print(ans)


### FIXED CODE ###

n = int(input())
x = list(map(int, input().split()))
x.sort()
ans = float("inf")

for i in range(x[0], x[-1]+1):
    t = 0
    for q in range(n):
        t += (i - x[q]) ** 2
    else:
        if ans > t:
            ans = t

print(ans)