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

A2=list(input())
A1=[]
L=len(A2)
A1.append(A2[0])
A2.pop(0)
for i in range(1,L):
    A1.append(A2[0])
    A2.pop(0)
    if len(A1)>=2 and A1[-2]=="S" and A1[-1]=="T":
        A1.pop(-1)
        A1.pop(-1)
print(len(A1))

### FIXED CODE ###

A2=list(input())
A1=[]
L=len(A2)
A1.append(A2[0])

for i in range(1,L):
    if len(A1)>=1 and A1[-1]=="S" and A2[i]=="T":
        A1.pop(-1)
    else:
        A1.append(A2[i])

print(len(A1))
### BUGGY CODE ###

S = input()
Q = int(input())
for i in range(Q):
  T = input().split()
  #print("S", S)
  #print("T", T)
  if T[0] == '1':
    S = S[::-1]
  elif T[0] == '2':
    if T[1] == '1':
      S = T[2] + S 
    elif T[1] == '2':
      S = S + T[2]
print(S)

### FIXED CODE ###

from collections import deque

S = input()
Q = int(input())
queue = deque(S)

rev_cnt = 0
rev = 0
for i in range(Q):
  query = input().split()
  if query[0]=="1":
    rev_cnt += 1
    rev = rev_cnt % 2
  elif query[0]=="2":
    #print(query)
    s=query[2]
    if query[1] == "1" and rev == 0:
      queue.appendleft(s)
    elif query[1] == "1" and rev == 1:
      queue.append(s)
    elif query[1] == "2" and rev == 0:
      queue.append(s)
    elif query[1] == "2" and rev == 1:
      queue.appendleft(s)
if rev == 0:
  print(''.join(list(queue)))
else:
  print(''.join(reversed(list(queue))))
### BUGGY CODE ###

#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


def main():
    import sys
    input = sys.stdin.readline
    s = input().rstrip("\n")
    q = int(input())
    han = 0
    qq=[input().split() for i in range(q)]

    for que in qq:
        if len(s) == 1 and que[0] == "1":
            continue

        if que[0] == "1":
            han += 1
            #print(han, s, que)
        else:
            if han % 2 == 0:  # 反転なし
                if que[1] == "1":
                    s = que[2]+s
                else:
                    s += que[2]
            else:  # あり
                if que[1] == "1":
                    s +=que[2]
                else:
                    s = que[2]+s
            #print(han, s, que)

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


if __name__ == '__main__':
    main()


### FIXED CODE ###

#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


def main():
    import sys
    input = sys.stdin.readline
    s = input().rstrip("\n")
    q = int(input())
    
    qq = [input().split() for i in range(q)]
    cnt=0
    r=""
    l=""
    #print(qq)
    for que in qq:
        if que[0] == "1":
            #print("a")
            r,l=l,r
            cnt+=1
            #print(r,l,cnt)
        else:
            if que[1] == "1":
                l+=que[2]
            else:
                r+=que[2]
            #print(r,l, que)
    #print(r,l)
    if cnt%2==0:
        print(l[::-1]+s+r)
    else:
        print(l[::-1]+s[::-1]+r)



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

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

Asum=0
for i in range(N):
  for j in range(i):
    Asum+=A[i]*A[j]

print(Asum%(10**9+7))

### FIXED CODE ###

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

AjtoN=sum(A)
Asum=0
for i in range(N):
    AjtoN-=A[i]
    Asum+=A[i]*AjtoN
    Asum=Asum%(10**9+7)

print(Asum)
### BUGGY CODE ###

n = int(input())
nodes = []
id_list = []
output = []
cnt = 0
for i in range(n):
    id, _, *li = list(map(int, input().split(' ')))
    nodes.append(li)
    id_list.append(id)

def rooted_tree(id, depth, parent):
    out = {}
    ind = id_list.index(id)
    out['node'] = id
    out['parent'] = parent
    out['depth'] = depth
    out['c'] = nodes[ind]
    if len(nodes[ind]) == 0:
        if depth == 0:
            out['type'] = 'root'
        else:
            out['type'] = 'leaf'
        output.append(out)
        return
    if depth == 0:
        out['type'] = 'root'
    else:
        out['type'] = 'internal node'
    output.append(out)
    for i in (nodes[ind]):
        rooted_tree(i, depth+1, id)

check_parent = [0 for i in range(n)]
for i in range(n):
    for j in nodes[i]:
        check_parent[j] += 1
root = check_parent.index(0)
rooted_tree(root, 0, -1)
for line in sorted(output, key=lambda x:x['node']):
    print("node {}: parent = {}, depth = {}, {}, {}".format(line['node'], line['parent'], line['depth'], line['type'], line['c']))



### FIXED CODE ###

n = int(input())
nodes = [0 for i in range(n)]
output = [None for i in range(n)]
cnt = 0
root = set(range(n))
for i in range(n):
    id, _, *li = list(map(int, input().split(' ')))
    root -= set(li)
    nodes[id] = li

def rooted_tree(id, depth, parent):
    out = {}
    out['node'] = id
    out['parent'] = parent
    out['depth'] = depth
    out['c'] = nodes[id]
    if depth == 0:
        out['type'] = 'root'
    elif len(nodes[id]) == 0:
        out['type'] = 'leaf'
    else:
        out['type'] = 'internal node'
    output[id] = out
    for i in (nodes[id]):
        rooted_tree(i, depth+1, id)

rooted_tree(root.pop(), 0, -1)
for line in output:
    print("node {}: parent = {}, depth = {}, {}, {}".format(line['node'], line['parent'], line['depth'], line['type'], line['c']))
### BUGGY CODE ###

import sys
N = int(input())
A = list(map(int,input().split()))
t = 0
for i in range(1,N+1):
    print(A.count(i))
    t += A.count(i)
    if t == N-1:
        for j in range(i+1,N+1):
            print(0)
        sys.exit()


### FIXED CODE ###

n=int(input())
A=list(map(int,input().split()))
N=[0]*n
for a in A:
    N[a-1]+=1
 
for i in N:
    print(i)
### BUGGY CODE ###

a,aa=map(int,input().split())
b=list(map(int,input().split()))
c=[float("inf") for i in range(a)]
c[0]=0
for i in range(0,a):
  for j in range(1,aa+1):
    if i+j==a:
      break
    else:
      c[i+j]=min(c[i]+abs(b[i]-b[i+j]),c[i+j])
print(c[-1])

### FIXED CODE ###

def solve():
  a,aa=map(int,input().split())
  b=list(map(int,input().split()))
  c=[0]*a
  c[0]=0
  for i in range(1,a):
    c[i] = min(c[j] + abs(b[i] - b[j]) for j in range(max(0,i-aa),i))
  print(c[-1])
  
solve()
### BUGGY CODE ###

from operator import itemgetter 

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
S = read().split()

dic = {}
for i in S:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
        
dic_sorted = sorted(dic.items(), key=itemgetter(1, 0))[::-1]

res = []
n = dic_sorted[0][1]
i = 0
l = len(dic_sorted)
while i < l and dic_sorted[i][1] == n:
    #print(dic_sorted[i][0])
    res = [dic_sorted[i][0].decode()] + res
    i += 1
    
print('\n'.join(res))

### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
S = read().split()

S = sorted(S)
ls = [[] for i in range(N + 1)]
max_num = 0
left = 0
right = 0
while right < N:
    while right < N and S[left] == S[right]:
        right += 1
    if (right  - left) >= max_num:
        ls[right - left].append(S[left].decode())
        max_num = right - left
    left = right
    
print('\n'.join(ls[max_num]))
### BUGGY CODE ###

n, w = map(int, input().split())
W, V = [], []
sum_w = 0
for i in range(n):
  wi, vi = map(int ,input().split())
  W.append(wi)
  V.append(vi)
  sum_w += wi

memo = [[0] * (w + 1) for _ in range(n+1)]

for i in range(n):
  for w_i in range(w + 1):
    memo[i+1][w_i] = max(memo[i+1][w_i], memo[i][w_i])
    if w_i - W[i] >= 0:
      memo[i+1][w_i] = max(memo[i+1][w_i], memo[i][w_i-W[i]] + V[i])
print(memo[-1][-1])

### FIXED CODE ###

n, w = map(int, input().split())
W, V = [], []
for i in range(n):
    wi, vi = map(int, input().split())
    W.append(wi)
    V.append(vi)
    
# Dp[i][sum_w] := i-1 番目までの品物から重さがsum_wを超えないように選んだときの、価値の総和の最大値
Dp = [[0] * (w + 1) for _ in range(n+1)]

for i in range(n):
    for sum_w in range(w+1):
        # i番目の商品を選ぶ場合
        if sum_w - W[i] >= 0:
            Dp[i+1][sum_w] = max(Dp[i+1][sum_w], Dp[i][sum_w - W[i]] + V[i])
        # i番目の商品を選ばない場合
        Dp[i+1][sum_w] = max(Dp[i+1][sum_w], Dp[i][sum_w])
print(Dp[-1][-1])
### BUGGY CODE ###

# -*- coding: utf-8 -*-
N = int(input())
a_list = []
for i in range(N):
    a_list.append(int(input()))
color_counter = 0
for i in range(N):
    color_counter += 1
    compare_a = a_list.pop(0)
    next_a_list = []
    for ite in a_list:
        if compare_a < ite:
            compare_a = ite
        else:
            next_a_list.append(ite)
    if len(next_a_list) == 0:
        break
    else:
        a_list = next_a_list
print(color_counter)

### FIXED CODE ###

# -*- coding: utf-8 -*-
from bisect import bisect_right, bisect_left
N = int(input())
a_list = [int(input()) for _ in range(N)]

color_list = [1]
for a in a_list:
    a *= -1
    index = bisect_right(color_list, a)
    if index < len(color_list):
        color_list[index] = a
    else:
        color_list.append(a)
#     print(color_list, a)
print(len(color_list))
### BUGGY CODE ###

# editorialを参考に作成
S = input()
lenS = len(S)
DP = [0] * lenS
for k in range(lenS):
    DP[k] = [0] * 13

if S[lenS-1] == "?":
    for j in range(10):
        DP[0][j % 13] += 1
else:
    DP[0][int(S[lenS-1]) % 13] += 1

for i in range(1, len(S)):
    if S[lenS-i-1] == "?":
        for p in range(13):
            for h in range(10):
                DP[i][(p+(h * ((10 ** i) % 13))) % 13] += DP[i-1][p]

    else:
        for q in range(13):
            DP[i][(q + (((10 ** i) % 13) * int(S[lenS-i-1]))) %
                  13] = DP[i - 1][q]

    for s in range(13):
        DP[i][s] %= 10**9+7

print(DP[lenS-1][5])
# print(DP)


### FIXED CODE ###

# editorialを参考に作成
S = input()
lenS = len(S)
DP = [0] * 13
ans = [0]*13
i_mod = [1] * lenS
if S[lenS-1] == "?":
    for j in range(10):
        ans[j % 13] += 1
else:
    ans[int(S[lenS-1]) % 13] += 1

for i in range(1, len(S)):
    DP = [0]*13
    for r in range(13):
        DP[r] += ans[r] % (10**9+7)
    ans = [0]*13
    i_mod[i] = (i_mod[i - 1] * 10) % 13
    i_now = 0
    i_now += i_mod[i]
    if S[lenS-i-1] == "?":
        for p in range(13):
            for h in range(10):
                ans[(p+h * i_now) % 13] += DP[p]

    else:
        for q in range(13):
            ans[(q+i_now * int(S[lenS-i-1])) % 13] = DP[q]


print(ans[5] % (10**9+7))
# print(DP)
### BUGGY CODE ###

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

for i in A:
    sum *= i

if sum > 10**18:
    print(-1)
else:
    print(sum)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
flag = False
sum = 1

for i in A:
    if not flag:
        if sum*i > 10**18:
            flag = True
        else:
            sum *= i
    else:
        if i == 0:
            print(0)
            exit()
if flag:
    print(-1)
else:
    print(sum)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
A, B, K = map(int, input().split())
for _ in range(K):
  if A >= 1:
    A -= 1
  elif B >= 1:
    B -= 1
  else:
    pass
print(A, B)

### FIXED CODE ###

import sys
input = sys.stdin.readline
A, B, K = map(int, input().split())
chk = K - A
if chk > 0:
    if K - A - B >= 0:
        print(0, 0)
    else:
        print(0, A + B - K)
elif chk <= 0:
    print(A-K, B)
### BUGGY CODE ###

N = int(input())
d = {}
for i in range(N):
    s = input()
    if s in d.keys():
        d[s] += 1
    else:
        d[s] = 1
keys = [k for k, v in d.items() if v == max(d.values())]
keys.sort()
for k in keys:
    print(k)

### FIXED CODE ###

N = int(input())
d = {}
for i in range(N):
    s = input()
    if s in d.keys():
        d[s] += 1
    else:
        d[s] = 1
Max = max(d.values())
keys = [k for k, v in d.items() if v == Max]
keys.sort()
for k in keys:
    print(k)
### BUGGY CODE ###

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

M = 1000

P = [True for i in range(M+1)]
p_set = set()
for i in range(2,M+1):
  if P[i]:
    p_set.add(i)
    for j in range(2*i, M+1, i):
      P[j] = False
#print( len(p_set) )
#N = 10 ** 6
#A = [1001 for i in range(N)]

c = 0
for p in p_set:
  c_tmp = 0
  for i in range(N):
    if A[i] % p == 0:
      c_tmp += 1
      while A[i] % p == 0:
        A[i] //= p
  c = max(c, c_tmp)

dic = {}
for a in A:
  if a > M:
    if a in dic:
      dic[a] += 1
    else:
      dic[a] = 1
for v in dic.values():
  c = max(c, v)

if c == N:
  print("not coprime")
elif c < 2:
  print("pairwise coprime")
else:
  print("setwise coprime")

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
#A = [888881 for i in range(10**6)]
M = max(A)


#高速素因数分解
#前処理：その数にある最小の素因数→エラトステネスの篩で最初におとすもの
P = [1 for i in range(M+1)]
for i in range(2, M+1):
    if P[i] == 1:
        for j in range(i, M+1, i):
            if P[j] == 1:
                P[j] = i
dic = {}
for a in A:
  t_dic = {}
  x = a
  while x > 1:
    if P[x] in t_dic:
      t_dic[ P[x] ] += 1
    else:
      t_dic[ P[x] ] = 1    
    x //= P[x]
  for k in t_dic.keys():
    if k in dic:
      dic[k] += 1
    else:
      dic[k] = 1
#print(dic)
c = 0
for v in dic.values():
  c = max(c, v)

if c == len(A):
  print("not coprime")
elif c < 2:
  print("pairwise coprime")
else:
  print("setwise coprime")
### BUGGY CODE ###

x = int(input())
 
for d in range(1, int(x**0.5)+1):
    if x % d == 0:
        for a in range(0,x+1):
            b = a - d
            if a**5 - b**5 == x:
                print(a, b)
                exit()
            elif a**5 - b**5 > x:
                continue

### FIXED CODE ###

x = int(input())
for a in range(-118,120):
    for b in range(-119, 119):
        if a**5 - b**5 == x:
            print(a,b)
            exit()
### BUGGY CODE ###

n, *a = map(int, open(0).read().split())

ans = 1
for ai in a:
    ans *= ai

if ans > 10 ** 18:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

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

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

ans = 1
for ai in a:
    ans *= ai
    if ans > 10 ** 18:
        print(-1)
        exit()

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

import numpy as np
n = int(input())
a =  [int(input()) for i in range(n)]

for i in range(n):
  print(int(max(np.concatenate([a[:i], a[i+1:]]))))

### FIXED CODE ###

import numpy as np
n = int(input())
a =  [int(input()) for i in range(n)]
max_a_num = a.count(max(a))
b = sorted(a, reverse=True)
m1, m2 = b[0], b[1]
for i in range(n):
  if max_a_num == 1:
    if a[i] == m1:
      print(m2)
    else:
      print(m1)
  else:
    print(m2)
### BUGGY CODE ###

S,T=input(),input()
for _ in[0]*int(input()):
    a,b,c,d=map(int,input().split())
    S_sub,T_sub=S[a-1:b],T[c-1:d]
    r=S_sub.count('A')+2*S_sub.count('B')-T_sub.count('A')-2*T_sub.count('B')
    print(['YES','NO'][r%3>0])

### FIXED CODE ###

S,T=[0],[0]
for k,c in enumerate(input()):S+=[S[-1]+(c=='A')]
for k,c in enumerate(input()):T+=[T[-1]+(c=='A')]
for _ in[0]*int(input()):
    a,b,c,d=map(int,input().split())
    print(['YES','NO'][(2*(b-a-d+c)+T[d]-T[c-1]-S[b]+S[a-1])%3>0])
### BUGGY CODE ###

from heapq import heappush, heappop
import itertools
sx,sy,gx,gy=map(int,input().split())
n=int(input())
g = [[] for i in range(n+2)]
data=[(sx,sy,0)]
data+=[tuple(map(int,input().split())) for i in range(n)]
data.append((gx,gy,0))

for i,j in itertools.product(range(n+2), range(n+2)):
    if i>=j:
        continue
    tmp=((data[i][0]-data[j][0])**2+(data[i][1]-data[j][1])**2)**0.5-data[i][2]-data[j][2]
    g[i].append((tmp,j))
    g[j].append((tmp,i))

dist=[2828427127 for i in range(n+2)]
dist[0]=0
visited=[False for i in range(n+2)]
que=[]
heappush(que,(0,0))
while que:
    c,v=heappop(que)
    if visited[v]:
        continue
    visited[v]=True
    for x in g[v]:
        if visited[x[1]]:
            continue
        tmp=max(x[0],0)+c
        if tmp<dist[x[1]]:
            dist[x[1]]=tmp
            heappush(que,(tmp,x[1]))
print(dist[n+1])


### FIXED CODE ###

from heapq import heappush, heappop
sx,sy,gx,gy=map(int,input().split())
n=int(input())
g = [[] for i in range(n+2)]
data=[(sx+sy*1j,0)]
for i in range(n):
    a,b,c=map(int,input().split())
    data.append((a+b*1j,c))
data.append((gx+gy*1j,0))

for i in range(n+2):
    for j in range(i+1,n+2):
        tmp=abs(data[i][0]-data[j][0])-data[i][1]-data[j][1]
        g[i].append((tmp,j))
        g[j].append((tmp,i))

dist=[2828427127 for i in range(n+2)]
visited=[False for i in range(n+2)]
dist[0]=0
que=[]
heappush(que,(0,0))

while que:
    c,v=heappop(que)
    if visited[v]:
        continue
    visited[v]=True
    for x in g[v]:
        if visited[x[1]]:
            continue
        tmp=x[0]+c if not x[0]<0 else c
        if tmp<dist[x[1]]:
            dist[x[1]]=tmp
            heappush(que,(tmp,x[1]))
print(dist[n+1])
### BUGGY CODE ###

n = int(input())
a = [[int(input())] for _ in range(n)]
for i in range(n):
    tmp = a[:]
    del tmp[i]
    print(max(tmp)[0])

### FIXED CODE ###

n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

tmp = a[:]
max1_val = max(tmp)
max1_idx = tmp.index(max1_val)
del tmp[max1_idx]
max2_val = max(tmp)

for i in range(n):
    if i != max1_idx:
        print(max1_val)
    else:
        print(max2_val)
### BUGGY CODE ###

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

from functools import lru_cache


@lru_cache(maxsize=200000)
def popcount(bits: str):
    return len([elm for elm in bits if elm == '1'])


@lru_cache(maxsize=100000000000)
def mod(a, b):
    return a % b

@lru_cache(maxsize=1000000000000000)
def f(num):
    if num == 0:
        return 0
    else:
        # if not cache.get(num % pc, None) is None:
        #     return cache[num % pc]
        # pc = popcount('{:b}'.format(num))
        pc = popcount(''.join(sorted('{:b}'.format(num))))
        ret = 1 + f(mod(num, pc))
        # cache[num % pc] = ret
        return ret


def reverse_bit(bits: str, digit):
    def toggle(a):
        if a == '1':
            return '0'
        else:
            return '1'

    ret = list(bits)
    ret[digit-1] = toggle(bits[digit-1])
    return ''.join(ret)

def convert(x: str):
    ans = 0
    i = 0
    for elm in list(x)[::-1]:
        if elm == '1':
            ans += 2 ** i
        i += 1
    return ans


answers = {}

for i in range(1, N+1):
    bits = reverse_bit(X, i)
    # pc = popcount(bits)
    # print(f'# {bits} {convert(bits)} {pc}')
    x = convert(bits)
    if not answers.get(x, None) is None:
        print(answers[x])
        continue
    ret = f(x)
    answers[x] = ret
    print(ret)

### FIXED CODE ###

n = int(input())
# for i in range(n):
#     print('0', end='')
# print(flush=True)
s = input()
x = int(s, base=2)
popcount = s.count('1')
y_zero = x % (popcount + 1)
if popcount > 1:
    y_one = x % (popcount - 1)
dp = [-1] * n
dp[0] = 0
 
 
def calc(y):
    if dp[y] != -1:
        return dp[y]
    dp[y] = 1 + calc(y % bin(y).count('1'))
    return dp[y]
 
 
ans = [0] * n
rem_zero = 1
if popcount > 1:
    rem_one = popcount - 2
for i in range(n):
    if s[n - 1 - i] == '0':
        ans[n - 1 - i] = 1 + calc((y_zero + rem_zero) % (popcount + 1))
    elif popcount > 1:
        ans[n - 1 - i] = 1 + calc((y_one + rem_one) % (popcount - 1))
    else:
        ans[n - 1 - i] = 0
    rem_zero = rem_zero * 2 % (popcount + 1)
    if popcount > 1:
        rem_one = rem_one * 2 % (popcount - 1)
for i in range(n):
    print(ans[i])
### BUGGY CODE ###

import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N, K = MI()
    p = LI()
    max_ans = 0
    max_i = 0
    ans = 0

    for i in range(0, N-K+1):
        sum_i = sum(p[i:i+K])
        #sum_i = p[i]
        #print("i",i)
        #for j in range(i+1,i+K):
        #    sum_i += p[j]
        #print("sum",sum_i)
        if max_ans <= sum_i:
            max_ans = sum_i
            max_i = i
    #print("max",max_i) 
    for j in range(max_i,max_i+K):
        #print("max_i",j)  
        ans += sum(range(1, p[j] + 1))/p[j]
    
    print(ans)




if __name__ == "__main__":
	main()

### FIXED CODE ###

import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N, K = MI()
    p = LI()

    sum_i = sum(p[0:K])
    max_ans = sum_i
    max_i = 0
    ans = 0
    for i in range(1, N-K+1):
        sum_i = sum_i - p[i-1] + p[K+i-1]
        #print(i,p[i-1],p[K+i-1])
        #print(sum_i)
        #for j in range(i+1,i+K):
        #    sum_i += p[j]
        #print("sum",sum_i)
        if max_ans <= sum_i:
            max_ans = sum_i
            max_i = i
    #print("max",max_i) 
    for j in range(max_i,max_i+K):
        #print("max_i",j)  
        ans += sum(range(1, p[j] + 1))/p[j]
    
    print(ans)




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

def main():
    N = int(input())
    A = []
    for _ in range(N):
        a = list(map(int, input().split()))
        a = [i-1 for i in a]
        a.reverse()
        A.append(a)
    t = []
    d = 0
    n = set(range(N))
    while True:
        p = 0
        l = set()
        d += 1
        for i in n:
            if A[i] == [] or i in l:
                continue
            j = A[i][-1]
            if A[i][-1] not in l and A[j][-1] == i:
                l.add(i)
                l.add(A[i][-1])
                A[j] = A[j][:-1]
                A[i] = A[i][:-1]
                p += 1
        n = l
        if p == 0:
            print(-1)
            break
        if sum([len(i) for i in A]) == 0:
            print(d)
            break
main()


### FIXED CODE ###

def main():
    N = int(input())
    A = []
    for _ in range(N):
        a = list(map(int, input().split()))
        a = [i-1 for i in a]
        a.reverse()
        A.append(a)
    t = N * (N - 1) / 2
    d = 0
    n = set(range(N))
    while True:
        p = 0
        l = set()
        d += 1
        for i in n:
            if A[i] == [] or i in l:
                continue
            j = A[i][-1]
            if A[i][-1] not in l and A[j][-1] == i:
                l.add(i)
                l.add(A[i][-1])
                A[j].pop() 
                A[i].pop()
                t -= 1
                p += 1
        n = l
        if t == 0:
            print(d)
            break
        if p == 0:
            print(-1)
            break
main()
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
b_c = [list(map(int, input().split())) for i in range(q)]

ans = 0

for i in range(q):
    before = b_c[i][0]
    after = b_c[i][1]
    for j in range(n):
        if a[j] == before:
            a[j] = after
    ans = sum(a)
    print(ans)


### FIXED CODE ###

import collections

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc =[list(map(int,input().split())) for i in range(q)]

ans = sum(a)
num = collections.Counter(a)

for i in range(q):
    before = bc[i][0]
    after = bc[i][1]
    ans += num[before]*(after-before)
    num[after] += num[before]
    num[before] = 0
    #print(a.count(before))
    #print(num(before))
    print(ans)
### BUGGY CODE ###

n = int(input())
s_list = []
anagrams = []
ans = 0

for i in range(n):
    s = list(input())
    s = sorted(s)
    s_list.append(s)

for i in range(n):
    for j in range(i+1,n):
        if s_list[i] == s_list[j]:
            ans = ans + 1
        elif s_list[i] != s_list[j]:
            continue

print(ans)


### FIXED CODE ###

n = int(input())
dict = {}
ans = 0

for i in range(n):
    s = ''
    list = sorted(input())
    for j in list:
        s += j
    if i != 0 and s in dict.keys():
        ans += dict[s]
        dict[s] += 1
    else:
        dict[s] = 1

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

n = int(input())
a = [input() for _ in range(n)]
for i in range(n):
  print(max(a[:i] + a[(i + 1):]))
  

### FIXED CODE ###

n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
x = b[-1]
y = b[-2]
for i in a:
  if i != x:
  	print(x)
  else:
    print(y)