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

#!/usr/bin/env python3
import sys

def main():
    input = sys.stdin.buffer.readline
    n, m = map(int, input().split())

    w, v = [0] * n, [0] * n
    for i in range(n):
        w[i], v[i] = map(int, input().split())

    dp = [0] * (m + 1)
    for j in range(w[0], m + 1):
        dp[j] = v[0]

    for i in range(n - 1):
        vi = v[i + 1]
        wi = w[i + 1]
        for j in reversed(range(wi, m + 1)):
            dp[j] = max(vi + dp[j - wi], dp[j])

    print(dp[-1])
main()

### FIXED CODE ###

#!/usr/bin/env python3
import sys, os
range = xrange

def main():
    inp = [int(x) for x in os.read(0, os.fstat(0).st_size).split()]
    n, m = inp[0],inp[1]

    w, v = inp[2::2], inp[3::2]

    dp = [0] * (m + 1)
    for j in range(w[0], m + 1):
        dp[j] = v[0]

    for i in range(n - 1):
        vi = v[i + 1]
        wi = w[i + 1]
        for j in reversed(range(wi, m + 1)):
            dp[j] = max(vi + dp[j - wi], dp[j])

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

n,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[0 for i in range(n)]
c=1

while c<=k:
    b=[0 for i in range(n)]
    for i in range(n):
        l=max(0,i-a[i])
        r=min(n-1,i+a[i])
        b[l]+=1
        if r+1<n:
            b[r+1]-=1
    for i in range(1,n):
        b[i]+=b[i-1]
    if min(b)==n:
        break
    c+=1
    a=b
print(" ".join(map(str,a)))

### FIXED CODE ###

n,k=map(int,input().split())
a=[int(i) for i in input().split()]
k=min(k,41)

for j in range(k):
    b=[0 for i in range(n)]
    for i in range(n):
        l=max(0,i-a[i])
        r=min(n-1,i+a[i])
        b[l]+=1
        if r+1<n:
            b[r+1]-=1
    for i in range(1,n):
      b[i]+=b[i-1]
    a=b
print(" ".join(map(str,a)))
### BUGGY CODE ###

import sys 

def func():
    n=int(input())
    print(0)
    sys.stdout.flush()
    gl=input()
    if gl == 'Vacant': 
        return
    print(n-1)
    sys.stdout.flush()
    gr=input()
    if gr == 'Vacant':
        return
    l=0
    r=n-1
    m=0
    while l<r-1:
        m=(l+r)>>1
        print(m)
        sys.stdout.flush()
        gm=input()
        if gm == 'Vacant' : 
            return  
        if (m-l)%2==0 and gm==gl :
            l=m
        elif (m-l)%2==1 and gm!=gl : 
            l=m
        else :
            r=m

    if m==l : 
        print(r)
    else: 
        print(l)
    return 

func()


### FIXED CODE ###

import sys 

def func():
    n=int(input())
    print(0)
    sys.stdout.flush()
    gl=input()
    if gl == 'Vacant': 
        return
    print(n-1)
    sys.stdout.flush()
    gr=input()
    if gr == 'Vacant':
        return
    l=0
    r=n-1
    m=0
    while l<r-1:
        m=(l+r)>>1
        print(m)
        sys.stdout.flush()
        gm=input()
        if gm == 'Vacant' : 
            return  
        if (m-l)%2==0 and gm==gl :
            l=m
        elif (m-l)%2==1 and gm!=gl : 
            gl=gm
            l=m
        else :
            gr=gm
            r=m

    if m==l : 
        print(r)
        sys.stdout.flush()
    else: 
        print(l)
        sys.stdout.flush()
    return 

func()
### BUGGY CODE ###

k, s = list(map(int, input().split()))

count = 0

for p in range(k+1):
    if p > s:
        break
    for q in range(k+1):
        if p + q > s:
            break
        for r in range(k+1):
            t = p + q + r
            if t == s:
                count += 1

print(count)

### FIXED CODE ###

k, s = list(map(int, input().split()))
count = 0

for p in range(k+1):
    for q in range(k+1):
        r = s-p-q
        if r>=0 and r<=k:
            count += 1

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

a,b,c,d = (int(i) for i in input().split())
count = 0
for i in range(a,b+1):
  if i%c != 0 and i%d !=0:
    count += 1
print(count)

### FIXED CODE ###

a,b,c,d = (int(i) for i in input().split())
count = 0

# 最大公約数。Euclidの互除法.
def gcd(m, n):
  if m < n:
    return gcd(n, m)
  if m%n == 0:
    return n
  else:
    return gcd(n, m%n)

# 最小公倍数
l = c*d//gcd(c,d)
def lowX(x,c,d,l):
  ans = x - x//c - x//d + x//l
  return ans
lowB = lowX(b,c,d,l)
lowA = lowX(a-1,c,d,l)
print(int(lowB-lowA))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
bit = [0] * 20

count = 0
l, r = 0, 0
isOk = True

while r < n:
    temp = a[r]
    r += 1

    bitCheck = 1
    for i in range(20):
        if (temp & bitCheck) > 0:
            bit[i] += 1

        if bit[i] >= 2:
            isOk = False
        
        bitCheck <<= 1
    
    if not isOk:
        while not isOk or l == r:
            isOk = True
            temp = a[l]
            l += 1
            bitCheck = 1
            for i in range(20):
                if (temp & bitCheck) > 0:
                    bit[i] -= 1

                if bit[i] >= 2:
                    isOk = False

                bitCheck <<= 1

    count += (r - l)

print(count)

    


### FIXED CODE ###

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

r = 0
tempSum = 0
xor = 0
count = 0

for l in range(n):
    while r < n and (tempSum + a[r]) == (xor ^ a[r]):
        tempSum += a[r]
        xor ^= a[r]
        r += 1
    
    count += (r - l)

    tempSum -= a[l]
    xor ^= a[l]

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

def resolve2():
    # ダブリング
    n, k = map(int, input().split())
    to = [[-1] * n for _ in range(60)]
    to[0] = list(map(lambda x: int(x) - 1, input().split()))

    # 2^i回テレポートした後何処にいるのか？
    for i in range(59):
        for j in range(n):
            to[i + 1][j] = to[i][to[i][j]]

    v = 0
    for i in reversed(range(60)):
        l = 1 << i
        if l <= k:
            v = to[i][v]
            k -= l

    print(v + 1)


if __name__ == '__main__':
    resolve2()

### FIXED CODE ###

import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    tree = [[] for _ in range(n)]
    for idx, a in enumerate(A):
        tree[idx].append(a - 1)

    start = []
    depth = [0 for _ in range(n)]

    def dfs(v, p, d, cnt):
        depth[v] = d
        for u in tree[v]:
            if visited[u]:
                start.append([u + 1, depth[u], cnt + 1])
                return
            else:
                visited[u] = True
                dfs(u, v, d + 1, cnt + 1)
        return

    visited = [False for _ in range(n)]
    visited[0] = True
    dfs(0, -1, 0, 0)

    if k <= start[0][1]:
        print(depth.index(k) + 1)
    else:
        cnt = (k - start[0][1]) % (start[0][2] - start[0][1])
        res = start[0][1] + cnt
        print(depth.index(res) + 1)


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

N=int(input())
a=[0]*N
s=set()
for i in range(N):
	a[i] = int(input())
	s.add(a[i])
res=0
for j in s:
	res += (1 if not a.count(j) % 2 == 0 else 0)

print(res)

### FIXED CODE ###

N=int(input())
a=[0]*N
s={}
for i in range(N):
	a[i] = int(input())
	if a[i] not in s:
		s[a[i]] = 1
	else:
		s[a[i]] += 1

res=0
for key,value in s.items():
#	print(key,value)
	res += (1 if not value % 2 == 0 else 0)

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

a, b = map(int, input().split())
def primes(n):
  if n >= 2:
    prime_nums = [2]
    for i in range(3, n + 1):
      for p in prime_nums:
        if i % p == 0:
          break
      else:
        prime_nums.append(i)
  return prime_nums
if a == 1 or b == 1:
  print(1)
else:
  Z = primes(max(a, b))
  ans = 1
  for i in range(len(Z)):
    if a%Z[i] == b%Z[i] == 0:
      ans += 1
  print(ans)

### FIXED CODE ###

import sys 
input = sys.stdin.readline
a, b = map(int, input().split())
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

A = set(prime_factorize(a))
B = set(prime_factorize(b))
ans = A&B
print(len(ans)+1)
### BUGGY CODE ###

n = int(input())

cnt = 0
for r in range(1, n):
    a = r * (n - r)
    for i in range(a, 0, -r):
        if i < n:
            cnt += i // r
            break

print(cnt)

### FIXED CODE ###

n = int(input())
print(sum((n - 1) // i for i in range(1, n)))
### BUGGY CODE ###

n = int(input())

def num_div(n):
  num_div_n = 0
  for i in range(1, int(n**0.5) + 1):
    if n % i == 0:
      num_div_n += 2
      if n // i == i:
        num_div_n -= 1
        
  return num_div_n

sum_div = 0
for i in range(n):
  sum_div += (i + 1) * num_div(i + 1)
  
print(sum_div)
  

### FIXED CODE ###

def g(x, n): #g(x, n): xの倍数(< n)の和
  y = n // x
  return x * y * (y + 1) // 2

n = int(input())

sum = 0
for i in range(1, n + 1):
  k = n // i
  sum += g(i, n)
  
print(sum)
### BUGGY CODE ###

def find(i, m):
    if m[i] < 0:
        return i
    return find(m[i], m)


def count(i, m):
    if m[i] < 0:
        return abs(m[i])
    return count(m[i], m)


def union(i, j, par, mem):
    pi, pj = find(i, par), find(j, par)
    mi = mem[i]
    if pi != pj:
        par[pj] += par[pi]
        par[pi] = pj
        for k in mi:
            par[k] = pj

        mem[i] = []
        mem[j].extend(mi)


def main():
    import sys
    sys.setrecursionlimit(200000)
    n, m, *ab = map(int, open(0).read().split())
    ab = ab[::-1]
    tmp = n * (n - 1) // 2
    utl = []
    par = [-1] * (n + 1)
    mem = [[] for _ in range(n + 1)]
    for u, v in zip(ab[::2], ab[1::2]):
        if find(u, par) == find(v, par):
            utl.append(tmp)
        else:
            i, j = count(u, par), count(v, par)
            utl.append(tmp)
            tmp -= i * j
            union(u, v, par, mem)

    ans = '\n'.join(map(str, utl[::-1]))
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def find(i, m):
    if m[i] < 0:
        return i
    return find(m[i], m)


def count(i, m):
    if m[i] < 0:
        return abs(m[i])
    return count(m[i], m)


def main():
    n, m, *ab = map(int, open(0).read().split())
    ab = ab[::-1]
    tmp = n * (n - 1) // 2
    utl = []
    par = [-1] * (n + 1)

    for u, v in zip(ab[::2], ab[1::2]):
        if find(u, par) == find(v, par):
            utl.append(tmp)
        else:
            i, j = find(u, par), find(v, par)
            utl.append(tmp)
            tmp -= par[i] * par[j]
            if par[i] > par[j]:
                i, j = j, i
            par[i] += par[j]
            par[j] = i

    ans = '\n'.join(map(str, utl[::-1]))
    print(ans)


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

#nord_aで使っている色の＋１
#初期入力
import sys
input = sys.stdin.readline
import heapq
N = int(input())
connection   ={i:set() for i in range(1,N+1)}
A            =[i for i in range(N-1)]
for i in range(N-1):
    a,b = (int(x) for x in input().split())
    connection[a].add(b) # つながりを逆向きも含めて入力
    connection[b].add(a) # 
    A[i] =a,b            #色を出力するため

#色の数(各頂点の最大値)
color_max =0
root =0
for i,v in connection.items():
    if color_max < len(v) :
        color_max =len(v)
        root =i         #枝が一番多い所を根 ⇒スタート

#各頂点に色を準備⇒辺の色決定
aa=[]
color =0                                 #辺に塗る色の候補
color_node ={i:[] for i in range(1,N+1)} #頂点に使った色の管理
color_edge ={}
for a,b in A:
    color_edge[(a,b)] =-1                   #辺の色 -1で初期化
now_set =set() #=start
now_set.add(root) #=start
next =set()
next_next =set()
passed =set()
count =0
while -1 in color_edge.values():
        for now in now_set: #幅優先探索
            next =connection[now]
            for nex_2 in next:
                count =0
                #while count < color_max:
                q,color =divmod(color,color_max)
                if color not in color_node[now] and color not in color_node[nex_2]:
                    n1,n2=0,0
                    if now > nex_2:
                        n1,n2 =nex_2,now
                    else:
                        n1,n2 =now,nex_2
                    color_edge[(n1,n2)] =color +1  #辺の色決定
                    color_node[now]     .append(color)
                    color_node[nex_2]   .append(color)
                    passed              .add(now)
                    if connection[nex_2] not in passed:
                        next_next       .add(nex_2)  #つながっている頂点（次に探索する）
                    passed      .add(nex_2)
                    color       +=1
                    #count       +=1
                else:
                    color +=1
                    #count +=1
        now_set     .clear()
        now_set     |=next_next
        next_next   .clear()
    
#出力
print(color_max)
for i in A:
    print(color_edge[i])

### FIXED CODE ###

"""
参考    yy4さん 2019-11-24 21:24:43
516 ms          PyPy3 (2.4.0)
"""
#def solve():
from collections import deque
import sys

input = sys.stdin.readline

n = int(input())

g = tuple(set() for _ in range(n))
for idx in range(n - 1):
    a, b = (int(x) - 1 for x in input().split())
    g[a].add((b, idx))
    g[b].add((a, idx))

q = deque()
q.append((0, -1, -1))

e = [-1] * n  # e[-1]は使わない
used = [False] * n
used[0] = True
while q:
    v, v_idx, prev_color = q.popleft()
    curr = 1 if prev_color != 1 else 2
    for u, u_idx in g[v]:
        if used[u]:
            continue
        used[u] = True
        e[u_idx] = curr
        q.append((u, u_idx, curr))
        curr += 1
        if curr == prev_color:
            curr += 1

ret = e[:-1]
#return len(set(ret)), ret

    
#l, ret = solve()
l=len(set(ret))
print(l)
print(*ret, sep='\n')
### BUGGY CODE ###

from itertools import combinations
n = int(input())
l = [int(i) for i in input().split()]
cnt = 0
h = []
for i in set(combinations(l, 3)):
  a, b, c = sorted(i)
  if c < (a + b):
    cnt += 1
print(cnt)

### FIXED CODE ###

from collections import Counter
N = int(input())
L = Counter(int(i) for i in input().split())
maxL = max(L)
key = sorted(L.keys())
val = [L[i] for i in range(1, max(L) + 1)]
acc = [0] * (len(val) + 1)
for i in range(len(val)):
    acc[i + 1] = acc[i] + val[i]
cnt = 0
for a in key:
    cnt += L[a] * (L[a] - 1) * (L[a] - 2) // 6
    cnt += max(acc[min(2 * a - 1, maxL)] - acc[a], 0) * L[a] * (L[a] - 1) // 2
for i in range(len(key) - 1):
    for j in range(i + 1, len(key)):
        a, b = key[i], key[j]
        cnt += L[a] * L[b] * (L[b] - 1) // 2
for i in range(len(key) - 2):
    for j in range(i + 1, len(key) - 1):
        a, b = key[i], key[j]
        cnt += max(acc[min(a + b - 1, maxL)] - acc[b], 0) * L[a] * L[b]
print(cnt)
### BUGGY CODE ###

N = int(input())
S = input()
ans = 0
for i in range(1000):
  indexS = 0 #Sの見ている場所
  indexI = 0 #3桁の暗証番号の見ている場所(0~2)
  
  while indexS < len(S):
    if indexI > 2:
      break #探索終了
    if str(str(i).zfill(3))[indexI] == S[indexS]:
      indexI +=1
    indexS +=1
  if indexI >2:
    ans +=1
print(ans)

### FIXED CODE ###

def func1(i,N,S):
  indexS = 0 #Sの見ている場所
  indexI = 0 #3桁の暗証番号の見ている場所(0~2)
  i2 = str(i).zfill(3) # 0埋めしたi
  
  while indexS < N:
    if indexI > 2:
      break #探索終了
    if i2[indexI] == S[indexS]:
      indexI +=1
    indexS +=1
  return 1 if indexI >2 else 0

def main():
  N = int(input())
  S = input()
  print(sum([func1(i,N,S) for i in range(1000)]))

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

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

xorall = 0
for i in xorz:
    xorall ^= i

res = ""
for i in range(N):
    numx = xorall ^ xorz[i]
    res += str(numx)
    if i < N-1:
        res += " "


print(res)


### FIXED CODE ###

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

xorall = 0
for i in xorz:
    xorall ^= i

numx = [0]*N
for i in range(N):
    numx[i] = xorall ^ xorz[i]

for i in range(N):
    print(numx[i]),
### BUGGY CODE ###

from sys import stdin
input = stdin.readline

n = int(input())
re_nums = []
nums = []
A = list(map(int, input().split()))
for a in A:
    if a not in re_nums:
        re_nums.append(a)
    else:
        nums.append(a)
nums.sort(reverse=True)
l = len(nums)
ans = 0
if l < 2:
    ans = 0
else:
    l1 = nums[0]
    l2 = nums[1]
    if l2 == l1:
        if l > 3:
            if nums[2] == l2:
                ans = l1**2
            else:
                ans = l1*nums[2]
        else:
            ans = 0
    else:
        ans = l1*l2
print(ans)

### FIXED CODE ###

from sys import stdin
input = stdin.readline

n = int(input())
dic = {}
A = list(map(int, input().split()))
for a in A:
    if a not in dic:
        dic[a] = 1
    else:
        dic[a] += 1
nums = list(dic.items())
nums.sort(reverse = True)
l = len(nums)
l1 = 0
for i in range(l):
    if l1 == 0 and nums[i][1] >= 4:
        ans = nums[i][0]**2
        break
    elif l1 == 0 and nums[i][1] >= 2:
        l1 = nums[i][0]
    elif l1 > 0 and nums[i][1] >= 2:
        ans = l1*nums[i][0]
        break
else:
    ans = 0
print(ans)
### BUGGY CODE ###

n, m = map(int, input().split())
a = [int(x) for x in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    k = [c]*b
    a = a + k
    a.sort()
    a = a[-n::]
ans = sum(a)
print(ans)

### FIXED CODE ###

n, m = map(int, input().split())
a = [int(x) for x in input().split()]
p = [(a[i], 1) for i in range(n)]

for i in range(m):
    b, c = map(int, input().split())
    p.append((c, b))
    
p.sort()
p.reverse()
ans, count = 0, n
for (s, t) in p:
    k = min(count, t)
    ans += s * k
    count -= t
    if count <= 0:
        break
    
print(ans)
### BUGGY CODE ###

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

for i in range(n):
    x[i]=x[i]-a
    
n2=n//5
n3=n-n2*4

y1=[]
y2=[]
y3=[]
y4=[]
for i in range(n2):
    y1.append(x[i])
    y2.append(x[i+n2])
    y3.append(x[i+n2*2])
    y4.append(x[i+n2*3])
y9=[]
for i in range(n3):
    y9.append(x[i+n2*4])
    
# itertools 使用 bit全探索
import itertools

def xdic(n2,xx):
    xsum=dict()
    for i in itertools.product([0,1], repeat=n2):
        isum=0
        for ii in range(n2):
            if i[ii]==0:
                isum=isum+xx[ii]
        if isum in xsum:
            xsum[isum]=xsum[isum]+1
        else:
            xsum[isum]=1
    return xsum

xsum1=xdic(n2,y1)
xsum2=xdic(n2,y2)
xsum3=xdic(n2,y3)
xsum4=xdic(n2,y4)
xsum9=xdic(n3,y9)
    
icnt=0
for xs1 in xsum1.items():
    for xs2 in xsum2.items():
        for xs3 in xsum3.items():
            for xs4 in xsum4.items():
                for xs9 in xsum9.items():
                    if xs1[0]+xs2[0]+xs3[0]+xs4[0]+xs9[0]==0:
                        icnt=icnt+xs1[1]*xs2[1]*xs3[1]*xs4[1]*xs9[1]

print(icnt-1)                        


### FIXED CODE ###

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

for i in range(n):
    x[i]=x[i]-a
    
nx=n*n
dp=[[0]*(2*nx+1) for i in range(n+1)]

#print(dp)

dp[0][nx]=1

for j in range(1,n+1):
    jj=j-1
    for t in range(2*nx+1):
        if t-x[jj]<0 or 2*nx<t-x[jj]:
            dp[j][t]=dp[j-1][t]
        elif 0<t-x[jj] and  t-x[jj]<2*nx:
            dp[j][t]=dp[j-1][t]+dp[j-1][t-x[jj]]
        else:
            dp[j][t]=0

print(dp[n][nx]-1)
### BUGGY CODE ###

N = int(input())
A = list(int(i) for i in input().split())
cnt = 0
n = 0

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

print(cnt)
    

### FIXED CODE ###

N = int(input())
A = list(int(i) for i in input().split())
B = []
B.append(0)
n = 1
cnt = 0

for i in A:
    B[0] += i
    B.append(B[0])
B[0] = 0
B.sort()

for i in range(1,N+1):
    if B[i] == B[i-1]:
        n += 1
    else:
        cnt += int(n*(n-1)/2)
        n = 1

cnt += int(n*(n-1)/2)

print(cnt)
### BUGGY CODE ###

# -*- coding:utf-8 -*-
if __name__ == "__main__":
    x = int(input())
    ans = float('-inf')
    for p in range(1,x+1):
        for b in range(2, x+2):
            if p**b <= x:
                ans = max(ans, p**b)
    else:
        print(ans)

### FIXED CODE ###

# -*- coding:utf-8 -*-
if __name__ == "__main__":
    x = int(input())
    ans = float('-inf')
    for p in range(1,x+1):
        for b in range(2, x+2):
            if p**b <= x:
                ans = max(ans, p**b)
            else:
                break
    else:
        print(ans)
### BUGGY CODE ###

INF = float('inf')
N,M,L = map(int,input().split())
cost = [[INF]*N for _ in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k]+cost[k][j]:
                cost[i][j] = cost[i][k]+cost[k][j]

for i in range(N):
    for j in range(N):
        if cost[i][j] > L:
            cost[i][j] = INF
        else:
            cost[i][j] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k]+cost[k][j]:
                cost[i][j] = cost[i][k]+cost[k][j]

for i in range(N):
    for j in range(N):
        if cost[i][j] == INF:
            cost[i][j] = -1
        else:
            cost[i][j] -= 1

Q = int(input())

for i in range(Q):
    s,t = map(int,input().split())
    print(cost[s-1][t-1])



### FIXED CODE ###

INF = float('inf')
N,M,L = map(int,input().split())
cost = [[INF]*N for _ in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    cost[a-1][b-1] = c
    cost[b-1][a-1] = c

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k]+cost[k][j]:
                cost[i][j] = cost[i][k]+cost[k][j]

for i in range(N):
    for j in range(N):
        if cost[i][j] > L:
            cost[i][j] = INF
        else:
            cost[i][j] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            if cost[i][j] > cost[i][k]+cost[k][j]:
                cost[i][j] = cost[i][k]+cost[k][j]

for i in range(N):
    for j in range(N):
        if cost[i][j] == INF:
            cost[i][j] = -1
        else:
            cost[i][j] -= 1

Q = int(input())

for i in range(Q):
    s,t = map(int,input().split())
    print(cost[s-1][t-1])
### BUGGY CODE ###

s = str(input())
q = int(input())
Q = []
for i in range(q):
    query = list(map(str, input().split()))
    Q.append(query)

flip = -1
for query in Q:
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if flip == -1:
                s = s + query[2]
            else:
                s = query[2] + s
if flip == 1:
    s = s[::-1]
print(s)

### FIXED CODE ###

from collections import deque

s = str(input())
q = int(input())
flip = -1
strf = deque()
strb = deque()

for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                strf.appendleft(query[2])
            else:
                strb.append(query[2])
        elif query[1] == '2':
            if flip == -1:
                strb.append(query[2])
            else:
                strf.appendleft(query[2])

# print(strf)
# print(strb)
ans = ''.join(strf) + s + ''.join(strb)
if flip == 1:
    ans = ans[::-1]
print(ans)

# s = str(input())
# q = int(input())
# flip = -1
# strs = '/'
 
# for i in range(q):
#     query = list(map(str, input().split()))
#     if query[0] == '1':
#         flip = flip * -1
#     elif query[0] == '2':
#         if query[1] == '1':
#             if flip == -1:
#                 strs = query[2] + strs
#             else:
#                 strs = strs + query[2]
#         else:
#             if flip == -1:
#                 strs = strs + query[2]
#             else:
#                 strs = query[2] + strs
 
# if flip == -1:
#     ans = strs.replace('/', s)
# else:
#     ans = strs[::-1].replace('/', s[::-1])
 
# print(ans)
### BUGGY CODE ###

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

for i in range(N):
	print(max([a for n, a in enumerate(A) if n !=  i]))


### FIXED CODE ###

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

MAX = max(A)
sorted_A = sorted(A)

for a in A:
	if a == MAX:
		print(sorted_A[-2])
	else:
		print(MAX)