Problem:
 Score : 300 points    Problem Statement You are given a sequence of length N : A_1, A_2, ..., A_N .
For each integer i between 1 and N (inclusive), answer the following question:   Find the maximum value among the N-1 elements other than A_i in the sequence.       Constraints  2 \leq N \leq 200000  1 \leq A_i \leq 200000  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  :  A_N       Output Print N lines. The i -th line ( 1 \leq i \leq N ) should contain the maximum value among the N-1 elements other than A_i in the sequence.        Sample Input 1 3
1
4
3      Sample Output 1 4
3
4   The maximum value among the two elements other than A_1 , that is, A_2 = 4 and A_3 = 3 , is 4 .  The maximum value among the two elements other than A_2 , that is, A_1 = 1 and A_3 = 3 , is 3 .  The maximum value among the two elements other than A_3 , that is, A_1 = 1 and A_2 = 4 , is 4 .        Sample Input 2 2
5
5      Sample Output 2 5
5

Solution (Python):
n =  int(input())
A = []
for i in range(n):
    A.append(int(input()))
ma = max(A)
ma2 = sorted(A)[-2]
for a in A:
    if a == ma:
        print(ma2)
    else:
        print(ma)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


N = I()
A = [I() for _ in range(N)]

B = [0]*N
C = [0]*N
for i in range(N):
    if i == 0:
        B[i] = A[i]
    else:
        B[i] = max(B[i-1],A[i])
for i in range(N-1,-1,-1):
    if i == N-1:
        C[i] = A[-1]
    else:
        C[i] = max(C[i+1],A[i])

for i in range(N):
    if i == 0:
        print(C[1])
    elif i == N-1:
        print(B[-2])
    else:
        print(max(B[i-1],C[i+1]))

Solution (Python):
N, *A = map(int, open(0).read().split())
sorted_A = sorted(A)
for a in A:
    if a == sorted_A[-1]:
        print(sorted_A[-2])
    else:
        print(sorted_A[-1])

Solution (Python):
n=int(input())
a = [int(input()) for _ in range(n)]
b = max(a)
c = a.index(b)
for i in range(n):
        if i == c:
                print(max(sorted(a)[:n-1]))
        else:
                print(b)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]

l = sorted(a, reverse=True)
x = l[0]
y = l[1]
for i in a:
  if i != x: print(x)
  else: print(y)

Solution (Python):

N = int(input())
A = [int(input()) for i in range(N)]
m1 = max(A)
i = 0
while A[i]<m1:
    i += 1
m2 = max(A[:i]+A[i+1:])
for j in range(N):
    print(m1 if i!=j else m2)

Solution (Python):
a=int(input())
b=[]
for i in range(a):
    b.append(int(input()))
c=max(b)
if b.count(c)>1:
    for i in range(a):
        print(c)
elif b.count(c)==1:
    d=sorted(b)
    d=d[a-2]
    e=b.index(c)
    for i in range(a):
        if i==e:
            print(d)
        else:
            print(c)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
maxa = max(a)
b = a.index(maxa)
for i in range(n):
	if i == b:
		print(max(sorted(a)[:n-1]))
	else:
		print(maxa)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
aa = sorted(a)[:n-1]
maxa = max(a)
b = a.index(maxa)
for i in range(n):
	if i == b:
		print(max(aa))
	else:
		print(maxa)

Solution (Python):
N=int(input())
A=[int(input()) for _ in range(N)]
B=sorted(A)[:N-1]
m=max(A)
n=A.index(max(A))
for i in range(N):
    if  i!=n:
        print(m)
    else:
        print(max(B))

Solution (Python):
N=int(input())
A=[int(input()) for _ in range(N)]
B=sorted(A)[:N-1]
m=max(A)
n=A.index(max(A))
for i in range(N):
    if  i!=n:
        print(m)
    else:
        print(max(B))

Solution (Python):
a = int(input())
b = [int(input()) for i in range(a)]
c = sorted(b)[-2]
d = max(b)
if b.count(d) >= 2:
  for i in range(a):
    print(d)
else:
  for i in b:
    if i == d:
      print(c)
    else:
      print(d)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
b=sorted(a)
m=b[-1]
c=b[-2]
for i in range(0, n):
  if m == a[i]:
    print(c)
  else:
    print(m)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
a_sort = sorted(a,reverse=True)
m1 = a_sort[0]
m2 = a_sort[1]
for i in range(n):
  if a[i] == m1:
    print(m2)
  else:
    print(m1)

Solution (Python):
N = int(input())
A = []
max1 = 0
max2 = 0
for i in range(N):
  a = int(input())
  A.append((a,i))
K = A.copy()
K.sort()
m1 = K[-1]
m2 = K[-2]
for i in range(N):
  if i == m1[1]:
    print(m2[0])
  else:
    print(m1[0])

Solution (Python):
n = int(input())
A = []
B = []
for _ in range(n):
    a = int(input())
    A.append(a)
    B.append(a)

s = max(A)
B.sort()
for a in A:
    if a != s:
        print(s)
    else:
        print(B[-2])

Solution (Python):
import math
n = int(input())
A = []
for _ in range(n):
  A.append(int(input()))
B = sorted(A)
m = max(A)  
for a in A:
  if a < m:
    print(m)
  else:
    print(B[-2])

Solution (Python):
n = int(input())
al = list(int(input()) for _ in range(n))

al_s = sorted(al)
fir = al_s[-1]
sec = al_s[-2]

for a in al:
    if a == fir:
        print(sec)
    else:
        print(fir)

Solution (Python):
from collections import Counter

n = int(input())
al = [int(input()) for _ in range(n)]
al_s = sorted(al, reverse=True)
c = Counter(al_s)
ck = list(c.keys())
cv = list(c.values())


for a in al:
    if a == ck[0]:
        if cv[0] == 1:
            print(ck[1])
        else:
            print(ck[0])
    else:
        print(ck[0])

Solution (Python):
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
ma=max(a)
if a.count(ma)>1:
  for i in range(n):
    print(ma)

else:
  for i in range(n):
    if a[i]==ma:
      a[i]=0
      print(max(a))
    else:
      print(ma)

Solution (Python):
N=int(input())
A=[]
for i in range(N):
  A.append(int(input()))
s=sorted(A,reverse=True)
for i in range(N):
  if A[i]!=s[0]:
    print(s[0])
  else:
    print(s[1])

Solution (Python):
N = int(input())
A = [0]*N
for i in range(N):
  A[i] = int(input())
B = sorted(A)
for i in range(N):
  if A[i] == B[-1]:
    print(B[-2])
  else:
    print(B[-1])

Solution (Python):
import copy
n=int(input())
li=[int(input()) for i in range(n)]
la=copy.copy(li)
la.sort()
m=la[-1]
for i in li:
    if i==m:
        print(la[-2])
    else:
        print(m)

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

MAX_1 = sorted(A)[-1]
MAX_2 = sorted(A)[-2]

for i in range(N):
    if A[i] == MAX_1:
        print(MAX_2)
    else:
        print(MAX_1)

Solution (Python):
n=int(input())
a=[]
m,m2=-1,-1
  
for i in range(n):
  b=int(input())
  a.append(b)
  m2=max(m2,min(m,b))
  m=max(m,b)

if m==m2:
  for i in range(n):
    print(m)
else:
  l=[m]*n
  l[a.index(m)]=m2
  for i in range(n):
    print(l[i])

Solution (Python):


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


lm = [0]*N

rm = [0]*N



for l in range(1,N): 
    lm[l] = max(lm[l-1], A[l-1])
    

for r in  range(N-2,0-1,-1):
    rm[r] = max(rm[r+1],A[r+1])


for a in range(N):
    ans = max(lm[a],rm[a])
    print(ans)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
c=sorted(a)
b=max(a)
for i in range(n):
  if a[i]==b:
    print(c[n-2])
  else:
    print(b)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]

b = sorted(a)
fst = b[-1]
snd = b[-2]

for x in a:
    if x == fst:
        print(snd)
    else:
        print(fst)

Solution (Python):
n=int(input())
a=[int(input())for i in range(n)]
a_max=max(a)
index_max=a.index(a_max)
for i in range(n):
    if i==index_max:
        print(max(a[:index_max]+a[index_max+1:]))
    else:
        print(a_max)

Solution (Python):
N = int(input())
A = [int(input()) for n in range(N)]
a1 = sorted(A)[-1]
a2 = sorted(A)[-2]

for a in A:
  if a == a1:
    print(a2)
  else:
    print(a1)

Solution (Python):
N = int(input())
A = list(map(int, (input() for i in range(N))))
B = sorted(A, reverse=True)
a = B[0]
a1 = B[1]


for x in A:  
  if x != a:
    print(a)
  else:
    print(a1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]

B.sort()

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy







mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
a1 = 0
a2 = 0
l = []
for i in range(n):
    value = inp()
    if(value > a1):
        a1 = value
    elif(value >= a2):
        a2 = value
    l.append(value)
for t in l:
    if(a1 == t):
        print(a2)
    else:
        print(a1)

Solution (Python):
n=int(input())
a=[]
am,amm=0,0
for i in range(n):
    x=int(input())
    if x>am:
        am,amm=amm,x
    elif x>amm:
        amm=x
    a.append(x)
for i in range(n):
    if a[i]==am:
        print(amm)
    else:
        print(am)

Solution (Python):
n=int(input())
l=[int(input()) for i in range(n)]

max_num=max(l)
max_index=l.index(max(l))

for i in range(n):
  if i!=max_index:
    print(max_num)
  else:
    l.remove(max_num)
    print(max(l))

Solution (Python):
import heapq
n = int(input())
a = [int(input()) for i in range(n)]
maxa = max(a)
ma2 = heapq.nlargest(2, a)[1]
ca = a.count(maxa)
if ca >= 2:
    for i in range(n):
        print(maxa)
else:
    for i in range(n):
        if a[i] == maxa:
            print(ma2)
        else:
            print(maxa)

Solution (Python):
n = int(input())
num = [int(input()) for i in range(n)]
num_s =sorted(num)    
f,s = num_s[-1],num_s[-2]

for j in num:
    if j == f:
        print(s)
    else:
        print(f)

Solution (Python):
def main():
    n = int(input())
    a_lst = [int(input()) for _ in range(n)]

    left = [0 for _ in range(n)]
    right = [0 for _ in range(n)]

    biggest = 0
    for i in range(n):
        if biggest < a_lst[i]:
            biggest = a_lst[i]
        left[i] = biggest

    biggest = 0
    for i in range(n - 1, -1, -1):
        if biggest < a_lst[i]:
            biggest = a_lst[i]
        right[i] = biggest

    print(right[1])
    for i in range(1, n - 1):
        print(max(left[i - 1], right[i + 1]))
    print(left[-2])


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    a_lst = [int(input()) for i in range(n)]
    a_lst_descend = sorted(a_lst, reverse=True)

    max_value = a_lst_descend[0]
    second_value = a_lst_descend[1]

    for a in a_lst:
        if a == max_value:
            print(second_value)
        else:
            print(max_value)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    a_lst = [(int(input()), i + 1) for i in range(n)]
    a_lst_descend = sorted(a_lst, reverse=True)

    max_value = a_lst_descend[0][0]
    max_index = a_lst_descend[0][1]
    second_value = a_lst_descend[1][0]

    for i in range(1, n + 1):
        if i == max_index:
            print(second_value)
        else:
            print(max_value)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
L =[]
l2 = []
for _ in range(N):
   L.append(int(input()))
ma = max(L)

if L.count(ma) > 1:
  [print(ma) for _ in range(N)]
else:  
  for i in L:
    if i == ma:
      print(sorted(L)[-2])
    else:
      print(ma)

Solution (Python):
l=[]
n=int(input())
for _ in range(n):
    a=int(input())
    l.append(a)
maxl=max(l)
a=l.count(maxl)
if a>=2:
    for _ in range(n):
        print(maxl)
else:
    for i in range(n):
        if l[i]!=maxl:
            print(maxl)
        else:
            ll=l.copy()
            l.remove(maxl)
            print(max(l))
            l=ll

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

A_sorted = list(sorted(A))
m = A_sorted[-1]
s = A_sorted[-2]
if m == s:
    for i in range(N): print(m)
else:
    for i in range(N):
        if m != A[i]: print(m)
        else: print(s)

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

sort_A = sorted(A, reverse=True)
max_value = sort_A[0]
second_value = sort_A[1]

ans = []

for a in A:
    if a < max_value:
        ans.append(max_value)
    elif a == max_value:
        ans.append(second_value)

print(*ans, sep='\n')

Solution (Python):
N=int(input())
A = []
for _ in range(N):
    A.append(int(input()))

amax = max(A)
nmax = A.count(amax)
if nmax == 1:
    B = []
    for i in range(N):
        if A[i] != amax:
            B.append(A[i])
    asec = max(B)


for i in range(N):
    if A[i] == amax:
        if nmax > 1:
            ans = amax
        else:
            ans = asec
    else:
        ans = amax
    print(ans)

Solution (Python):
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    a_copy = sorted(a)
    a_max = a_copy[-1]
    a_second = a_copy[-2]
    for i in range(n):
        if a[i] == a_max:
            print(a_second)
        else:
            print(a_max)


main()

Solution (Python):
N = int(input())
A = []
p = 0
q = 0
for i in range(N):
  a = int(input())
  A.append(a)
  if a > p:
    q = p
    p = a
  elif a > q:
    q = a
for i in range(N):
  if A[i] == p:
    print(q)
  else:
    print(p)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = tuple(a)
a.sort()
for i in b:
    if i == a[n-1]:
        print(a[n-2])
    else:
        print(a[n-1])

Solution (Python):
import copy
Row = int(input())
List = []
for i in range (Row):
  List.append(int(input()))
NewList = copy.deepcopy(List)
NewList.sort()
high = NewList[Row-1]
secondHigh = NewList[Row-2]
for i in range(Row):
  if List[i] == high:
    print(secondHigh)
  else:
    print(high)

Solution (Python):
from collections import Counter

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

def solve(n,a):
  d = Counter(a)
  if len(d) == 1:
    return a
  d_lst = list(sorted(d.keys()))
  first = d_lst[-1]
  second = d_lst[-2]
  if d[first] != 1:
    return [first]*n
  else: 
    return [second if a[i] == first else first for i in range(n)]

print(*solve(N,a),sep="\n")

Solution (Python):
n = int(input())
num_list = []
for _ in range(n):
    num_list.append(int(input()))

copy =  num_list.copy()
copy.sort()
maximum = copy[-1]
second = copy[-2]

for i in num_list:
    if i == maximum:
        print(second)
    else:
        print(maximum)

Solution (Python):
n = int(input())
num_list = []
for _ in range(n):
    num_list.append(int(input()))

copy =  num_list.copy()
copy.sort()
maximum = copy[-1]
second = copy[-2]

for i in num_list:
    if i == maximum:
        print(second)
    else:
        print(maximum)

Solution (Python):
N = int(input())
N_List = []
for i in range(N):
    N_List.append(int(input()))
NMax = max(N_List)
if N_List.count(NMax) == 1:
    NMax2 = sorted(N_List)[-2]
    for i in N_List:
        print((NMax,NMax2)[i==NMax])
else:
    for i in N_List:
        print(NMax)

Solution (Python):
n = int(input())
max_1 = 0
max_2 = 0
a_list = []
for i in range(n):
    a = int(input())
    a_list.append(a)
    if max_1 < a:
        max_2 = max_1
        max_1 = a
    elif max_2 < a:
        max_2 = a

for i in range(n):
    if a_list[i] == max_1:
        print(max_2)
    else:
        print(max_1)

Solution (Python):
n = int(input())
alist = []
for i in range(n):
    a = int(input())
    alist.append(a)
aalist = sorted(alist)
ans = 0
for i in range(n):
    ans = aalist[-1]
    if alist[i] == aalist[-1]:
        ans = aalist[-2]
    print(ans)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a, reverse=True)
for i in a:
    if i == b[0]:
        print(b[1])
    else:
        print(b[0])

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

INF = float("inf")


N = int(input())
A = [int(input()) for _ in range(N)]
m = max(A)
c = 0
for a in A:
    if a == m:
        c += 1
if c == 1:
    mm = 0
    for i in range(N):
        a = A[i]
        if a == m:
            continue
        mm = max(a,mm)
    for i in range(N):
        a = A[i]
        if a == m:
            print(mm)
        else:
            print(m)
else:
    for i in range(N):
        print(m)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
sort_a = sorted(a, reverse=True)
amax = sort_a[0]
asecond = sort_a[1]
for i in range(n):
    tmp = a[i]
    if tmp == amax:
        print(asecond)
    else:
        print(amax)

Solution (Python):
import heapq
N = int(input())
A = [0]*N

maxVal = -1
for i in range(N):
    A[i] = int(input())
    maxVal = max(maxVal, A[i])

secondMax = -1
maxIndex = A.index(maxVal)
A[maxIndex] = -1
secondMax = max(A)
A[maxIndex] = maxVal

for i in range(N):
    if A[i] == maxVal:
        print(secondMax)
    else:
        print(maxVal)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A,reverse=True)

for i in range(N):
  if A[i] == B[0]:
    print(B[1])
  else:
    print(B[0])

Solution (Python):
N = int(input())
A = [[int(input()), i] for i in range(N)]
A.sort(reverse=True)

ex = A[0]
nx = A[1]

for i in range(N):
  if i == ex[1]:
    print(nx[0])
  else:
    print(ex[0])

Solution (Python):
n=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
d=sorted(l)
x=d[-2]
m=max(l)
for i in range(n):
    if(l[i]==m):
        l[i]=x
    else:
        l[i]=m
for i in l:
    print(i)

Solution (Python):
n = int(input())
max_n = -1
semi_max_n = -1
max_idx = -1
for i in range(n):
    x = int(input())
    if x > max_n:
        max_n = x
        max_idx = i
    elif x >= semi_max_n:
        semi_max_n = x
for i in range(n):
    if i == max_idx:
        print(semi_max_n)
    else:
        print(max_n)

Solution (Python):
n=int(input())
a=0
b=0
l=[]
for i in range(n):
  c=int(input())
  l.append(c)
a=max(l)
L=sorted(l)
b=L[-2]
for i in range(n):
  if l[i]==a:
    print(b)
  else:
    print(a)

Solution (Python):
n=int(input())
a=0
b=0
l=[]
for i in range(n):
  c=int(input())
  l.append(c)
a=max(l)
L=sorted(l)
b=L[-2]
for i in range(n):
  if l[i]==a:
    print(b)
  else:
    print(a)

Solution (Python):
N = int(input())

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

max = sorted(A)[-1]
semi_max = sorted(A)[-2]

if max != semi_max:
    ex = A.index(max)
    
    for i in range(0,ex):
        print(max)
    
    print(semi_max)
    
    for i in range(ex+1,N):
        print(max)

else:
    for i in range(N):
        print(max)

Solution (Python):
def resolve():
    '''
    code here
    '''
    N = int(input())
    As = [int(input()) for _ in range(N)]

    max_num = max(As)
    max_index = As.index(max_num)

    for i in range(N):
        if i == max_index:
            As.remove(max_num)
            print(max(As))
        else:
            print(max_num)
if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    '''
    code here
    '''
    N = int(input())
    As = [int(input()) for _ in range(N)]

    max_num = max(As)
    max_index = As.index(max_num)

    for i in range(N):
        if i == max_index:
            del As[max_index]
            print(max(As))
        else:
            print(max_num)
if __name__ == "__main__":
    resolve()

Solution (Python):
N = int(input())
A = list(int(input()) for _ in range(N))

A_max = sorted(A, reverse = True)[0]
A_second = sorted(A, reverse=True)[1]

for i in range(N):
    if A[i] == A_max:
        print(A_second)
    else:
        print(A_max)

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=[0]*N
    Mi=-1
    M=0
    for i in range(N):
        A[i]=I()
        if A[i]>M:
            Mi=i
            M=A[i]
            
    A[Mi]=0
    
    for i in range(N):
        if i!=Mi:
            print(M)
        else:
            print(max(A))

main()

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


N = int(input())

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

AA = Counter(A)

B = sorted(AA.items(), key=lambda x:x[0])

if B[-1][1] == 1:
    c = B[-2][0]
else:
    c = B[-1][0]

M = B[-1][0]

for i in range(N):
    if A[i] == M:
        print(c)
    else:
        print(M)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


N = int(input())

A = []
for i in range(N):
    A.append(-int(input()))

B = copy.deepcopy(A)
heapq.heapify(B)

for i in range(N):
    b = heapq.heappop(B)
    if b == A[i]:
        c = b
        b = heapq.heappop(B)
        heapq.heappush(B,c)
    heapq.heappush(B,b)
    print(-b)

Solution (Python):
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))

newA = sorted(A)
ma = newA[N-1]
for i in range(N):
    if A[i] == ma:
        print(newA[N-2])
    else:
        print(ma)

Solution (Python):




n = int(input())
a = [int(input()) for _ in range(n)]

max_a = 0
sec_max_a = 0
for i in range(n):
    if max_a <= a[i]:
        sec_max_a = max_a
        max_a = a[i]
    elif sec_max_a < a[i]:
        sec_max_a = a[i]

for i in range(n):
    if a[i] == max_a:
        print(sec_max_a)
    else:
        print(max_a)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]

l1=sorted(a)[-1]
l2=sorted(a)[-2]

for i in a:
  if i==l1:
    print(l2)
  if i!=l1:
    print(l1)

Solution (Python):
n = int(input())
a = list(int(input()) for _ in range(n))
aa = sorted(a.copy())
mx, sd = aa[n - 1], aa[n - 2]
for i in a:
    print(mx if i < mx else sd)

Solution (Python):
from heapq import nlargest
import sys
n = int(input())
a = [int(input()) for i in range(n)]
l1, l2 = nlargest(2, a)

if l1 == l2:
  for i in range(n):
    print(l1)
  sys.exit()

for i in a:
  if i == l1:
    print(l2)
  else:
    print(l1)

Solution (Python):



N=int(input())
ls = []
lsmax=0
lsmaxnum=1
lsmax2=0
for i in range (N):
    a=int(input())
    ls.append(a)
    if a==lsmax:
        if lsmaxnum==1:
            pass
        else:
            lsmaxnum+=1
    if a>lsmax:
        lsmax=a
if lsmaxnum==1:
    ls2=sorted(ls)
    lsmax2=ls2[-2]



for j in range (N):
    if ls[j]==lsmax:
        if lsmaxnum>1:
            print(lsmax)
        else:
            print(lsmax2)
    else:
        print(lsmax)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
sortA = sorted(A)
ma = sortA[-1]
ma2 = sortA[-2]
for a in A:
    if a == ma:
        print(ma2)
    else:
        print(ma)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
A_s = sorted(A, reverse = True)


A_max = A_s[0]
A_max2 = A_s[1]

for i in A:
    if i == A_max:
        print(A_max2)
    else:
        print(A_max)

Solution (Python):
N = int(input())
A = [0] * N 
for i in range(N):
    A[i] = int(input())

B = sorted(A)
max1 = B[-1]
max2 = B[-2]
for x in A:
    if x == max1:
        print(max2)
    else:
        print(max1)

Solution (Python):
n = int(input())
an = [int(input()) for _ in range(n)]

check = sorted(an, reverse=True)
for i in range(n):
  if an[i] == check[0] :
    print(check[1])
  else :
    print(check[0])

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
s = sorted(a)
for j,k in enumerate(a):
    print(s[-2]) if k == s[-1] else print(s[-1])

Solution (Python):
n = int(input())
lis = [int(input()) for i in range(n)]
lis_2 = sorted(lis,reverse = True)
fmax = lis_2[0]
smax = lis_2[1]
for i in range(n):
    if lis[i] == fmax:
        print(smax)
    else:
        print(fmax)

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]

sorted_A = sorted(A, reverse=True)

for i in range(n):
    if A[i] != sorted_A[0]:
        print(sorted_A[0])
    else:
        print(sorted_A[1])

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
A_cpy = A[:]
A_cpy.sort()
max_ = A_cpy[-1]

for a in A:
    if a == max_:
        print(A_cpy[-2])
    else:
        print(max_)

Solution (Python):
import math


def resolve():
    import sys
    import string
    input = sys.stdin.readline
    
    
    n = int(input().rstrip())
    a_list = [int(input().rstrip()) * -1 for i in range(n)]

    import numpy
    indices = numpy.argsort(a_list)
    max_num = a_list[indices[0]] * -1
    second_max_num = a_list[indices[1]] * -1

    for i in range(len(a_list)):
        if i != indices[0]:
            print(max_num)
        else:
            print(second_max_num)


if __name__ == "__main__":
    resolve()

Solution (Python):
n = int(input())
a_list = [int(input()) for _ in range(n)]
cp_list = a_list.copy()
cp_list.sort()
second_ans = cp_list[-2]
ans = cp_list[-1]
for i in range(n):
    if a_list[i] == ans:
        print(second_ans)
    else:
        print(ans)

Solution (Python):
n = int(input())
a = list(int(input()) for _ in range(n))

l = sorted(a)

max_num = l[-1]
sec_max = l[-2]

cnt = a.count(max_num)

for x in a:
    if x == max_num and cnt == 1:
        print(sec_max)
    else:
        print(max_num)

Solution (Python):
N = int(input())
b = 0
c = 0
first = 0
A = []
for i in range(N):
    a = int(input())
    if b < a:
        ind = i
        b = a
    A.append(a)
for j in range(N):
    if j==ind:
        continue
    if c < A[j]:
        sind = j
        c = A[j]
for k in range(N):
    if k==ind:
        print(c)
    else:
        print(b)

Solution (Python):
n = int(input())
l = []

for i in range(n):
    l.append(int(input()))

def has_duplicates(seq):
    return sorted(seq)[len(seq)-1] == sorted(seq)[len(seq)-2]

x = max(l)

if has_duplicates(l):
    for i in l:
        print(x)
else:
    for i in l:
        if i != x:
            print(x)
        else:
            print(sorted(l)[n-2])

Solution (Python):
n = int(input())
a_list = [int(input()) for _ in range(n)]
b_list = sorted(a_list, reverse=True)
for a in a_list:
    print(b_list[0] if a != b_list[0] else b_list[1])

Solution (Python):
N = int(input())
arr = [int(input()) for _ in range(N)]
arr2 = sorted(arr)
for i in range(N):
    print(arr2[-2] if arr[i] == arr2[-1] else arr2[-1])

Solution (Python):
N = int(input())
arr = [int(input()) for _ in range(N)]
arr2 = sorted(arr)
max1 = arr2[-1]
max2 = arr2[-2]
for i in range(N):
    if arr[i] == max1:
        print(max2)
    else:
        print(max1)

Solution (Python):
N = int(input())
A = []

for i in range(N):
  A.append(int(input()))

maxA = max(A)
idx = A.index(maxA)
A.sort()
for i in range(len(A)):
  if i == idx:
    print(max(A[:-1]))
  else:
    print(maxA)

Solution (Python):
N = int(input())
A = []
for _ in range(N):
	A.append(int(input()))
B = sorted(A, reverse = True)
m1, m2 = B[0], B[1]
for i in range(N):
	if m1 > A[i]:
		print(m1)
	else:
		print(m2)

Solution (Python):

N = int(input())
A = list(int(input()) for _ in range(N))
A_sorted = list((sorted(A, reverse = True)))
A_sorted = A_sorted[0:2]

ans = []
for i in A:
    if i == A_sorted[0]:
        ans.append(A_sorted[1])
    else:
        ans.append(A_sorted[0])

for j in ans:
    print(j)

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

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

m1, m2 = sorted(A, reverse=True)[0:2]
for a in A:
    print(m2 if a == m1 else m1)

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

mls = [-1 for _ in range(N+1)]
for i, a in enumerate(A, 1):
    mls[i] = max(mls[i-1], a)

mrs = [-1 for _ in range(N+1)]
for i, a in enumerate(A[::-1], 1):
    mrs[i] = max(mrs[i-1], a)
mrs = mrs[::-1]

for i in range(N):
    print(max(mls[i], mrs[i+1]))

Solution (Python):
N,A = int(input()),[]
for _ in range(N):
	A.append(int(input()))
B = sorted(A)

for x in A:
	print(B[N-2] if x==B[N-1] else B[N-1])

Solution (Python):
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
B=A[:]
A=sorted(A)
for i in B:
    if i==A[N-1]:
        print(A[N-2])
    else:
        print(A[N-1])

Solution (Python):
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
B=A[:]
A=sorted(A)
for i in B:
    if i==A[N-1]:
        print(A[N-2])
    else:
        print(A[N-1])

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

sort_a = sorted(a, reverse=True)
max_a = sort_a[0]
max_a_1 = sort_a[1]

for i in range(n):
  if a[i] == max_a:
    print(max_a_1)
  else:
    print(max_a)

Solution (Python):
import copy
N=int(input())
ans=[]
S=[int(input()) for i in range(N)]
W=copy.copy(S)
W.sort()
for i in range(N):
  if W[-1]==S[i]:
    ans.append(W[-2])
  else:
    ans.append(W[-1])
for j in range(N):
  print(ans[j])

Solution (Python):
N = int(input())
a = []
ans = []
for i in range(N):
    A = int(input())
    a.append(A)

b = sorted(a)

m = max(a)
for i in a:
    if i == m:
        ans.append(b[-2])
    else:
        ans.append(m)

for i in ans:
    print(i)

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    m = max(a)
    s = sorted(a)[-2]
    for i in a:
        if i == m:
            print(s)
        else:
            print(m)

if __name__=="__main__":
    main()

Solution (Python):
N=int(input())
A=[]
for i in range(N):
  a=int(input())
  A.append(a)
import copy
B=copy.copy(A)
B.sort()
max=B[-1]
max2=B[-2]

if max==max2:
  for i in range(N):
    print(max)
else:
  ind=A.index(max)
  for i in range(N):
    if i==ind:
      print(max2)
    else:
      print(max)

Solution (Python):
n = int(input())
A=[]
for i in range(n):
    A.append(int(input()))

S = sorted(A,reverse=True)
maxa = S[0]
maxa2 = S[1]
B=[]
B = [maxa2 if a==maxa else maxa for a in A]

for b in B:
    print(b)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
A2 = sorted(A)
for i in A:
    if i == A2[N-1]:
        print(A2[N-2])
    else:
        print(A2[N-1])

Solution (Python):
n = int(input())

a = [int(input()) for _ in range(n)]

b = sorted(a)

for x in a:
    if x == b[-1]:
        print(b[-2])
    else:
        print(b[-1])