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):
import collections
import copy

def solve():
    a = copy.copy(A)

    a.sort(reverse=True)


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

if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    solve()

Solution (Python):
from copy import deepcopy
n=int(input())
a=[int(input()) for _ in range(n)]
b = deepcopy(a)
b.sort()

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

Solution (Python):


import sys
stdin = sys.stdin
from heapq import heapify, heappush, heappop

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = list()
    for _ in range(n):
        a.append(ni())
    b = sorted(a, reverse=True)
    amax = b[0]
    asec = b[1]
    for ai in a:
        if ai == amax:
            print(asec)
        else:
            print(amax)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
l=[0]*n
r=[0]*n
t=0
for i in range(n):
  t=max(t,a[i])
  l[i]=t
t=0
for i in range(n-1,-1,-1):
  t=max(t,a[i])
  r[i]=t


for i in range(n):
  if i==0:
    print(r[i+1])
    continue
  if i==n-1:
    print(l[i-1])
    continue
  print(max(r[i+1],l[i-1]))

Solution (Python):
n, *aa = map(int, open(0).read().split())

bb = sorted(aa)

f, s = bb[-1], bb[-2]

for a in aa:
    if a == f:
        print(s)
    else:
        print(f)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = [I() for _ in range(N)]
    
    A_first = max(A)
    A_second = 0
    if A.count(A_first) >= 2:
        A_second = A_first
    else:
        for i in A:
            if i != A_first:
                A_second = max(i, A_second)

    for i in A:
        if i == A_first:
            print(A_second)
        else:
            print(A_first)

if __name__ == '__main__':
    resolve()

Solution (Python):
import copy
N = int(input())
A = []
for i in range(N):
  tmp = int(input())
  A.append(tmp)

B = copy.copy(A)
B.sort()

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

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

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

new_l = sorted(l)

fmax = new_l[N-1]
fmax2 = new_l[N-2]
smax = fmax

for j in reversed(range(N)):
    if not new_l[j] == fmax:
        smax = new_l[j]
        break

if fmax == fmax2:
    for k in range(N):
        print(fmax)
else:
    for m in range(N):
        if l[m] == fmax:
            print(smax)
        else:
            print(fmax)

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

max_1 = 0
max_2 = 0
max_index = 0

for i in range(n):
    item = int(input())
    if item >= max_1:
        max_1, max_2 = item, max_1
        max_index = i
    elif item > max_2:
        max_2 = item

for i in range(n):
    ans = max_2 if i == max_index else max_1
    print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]
max1 = max(A)
A.remove(max1)
max2 = max(A)

for i in B:
    if i == max1:
        print(max2)
    else:
        print(max1)

Solution (Python):
def resolve():
    n = int(input())
    A = [int(input()) for i in range(n)]
    B = sorted(A)
    mx = B[-1]
    second = B[-2]
    for a in A:
        if a == mx:
            print(second)
        else:
            print(mx)
resolve()

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

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

amax1 = max(a)
amax_i1 = a.index(amax1)

a.pop(amax_i1)
amax2 = max(a)
amax_i2 = a.index(amax2)

for i in range(n):
    if i == amax_i1:
        print(amax2)
    else:
        print(amax1)

Solution (Python):
N = int(input())
As = [int(input()) for i in range(N)]
Bs = sorted(As, reverse = True)
ma = Bs[0]
ma2 = Bs[1]

for a in As:
  print(ma if a != ma else ma2)

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

maxL = [0] * (N+1)
maxR = [0]*(N+1)
num = []
for i in range(N):
    num.append(int(input()))
    maxL[i+1] = maxL[i] if maxL[i] > num[i] else num[i]

for i in range(N-1, 0, -1):
    maxR[i-1] = maxR[i] if maxR[i] > num[i] else num[i]

for i in range(N):
    print(max(maxL[i], maxR[i]))

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

    
if '__main__' == __name__:
    resolve()

Solution (Python):
n = int(input())
a = []
for _ in range(n):
  a_ = int(input())
  a.append(a_)
a1 = sorted(a)
ans1 = a1[-1]
ans2 = a1[-2]
for i in range(n):
  if a[i] == ans1:
    print(ans2)
  else:
    print(ans1)

Solution (Python):

n,*a=map(int, open(0).read().split())
s = sorted(a)
for i in a:
    if i!=s[-1]:
        print(s[-1])
    else:
        print(s[-2])

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

B = sorted(A, reverse=True)[:2]

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

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

A_sort = sorted(A)
A_max = A_sort[-1]
A_max2 = A_sort[-2]

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

Solution (Python):
from collections import Counter
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
a_counter=Counter(a)
a_max=max(a)
b=a_counter[a_max]
for i in range(n):
  if a[i]!=a_max:
    print(a_max)
  else:
    if b>=2:
      print(a_max)
    else:
      c=a[0:i]+a[i+1:]
      print(max(c))

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

left=[0]*(N+1)
right=[0]*(N+1)
for i in range(N):
  left[i+1]=max(left[i],A[i])
  right[N-1-i]=max(right[N-i],A[N-i-1])

ans=0
for i in range(N):
  ans=max(left[i],right[i+1])
  print(ans)

Solution (Python):
import numpy as np
N = int(input())
a = np.zeros((N))
for i in range(N):
  a[i] = int(input())
A_i = np.argsort(a)
A = np.sort(a)
for j in range(N):
  if(j==A_i[N-1]):
    print(int(A[N-2]))
  else:
    print(int(A[N-1]))

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

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

al.sort(reverse=True)

for i in range(1,n+1):
    if al[0][1] == i:
        print(al[1][0])
    else:
        print(al[0][0])

Solution (Python):
n = int(input())
S = [int(input()) for _  in range(n)]
lim = sorted(S,reverse = True)

for i in range(n):
    if S[i] == lim[0]:
        print(lim[1])
    else:
        print(lim[0])

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

def main():
    n = int(input())
    a = []
    for _ in range(n):
        a.append(int(input()))
    max_l, max_r = [a[0]], [a[-1]]
    for i in range(1, n):
        max_l.append(max(max_l[i-1], a[i]))
        max_r.append(max(max_r[i-1], a[-(i+1)]))
    max_r = max_r[::-1]
    for i in range(n):
        if i == 0:
            print(max_r[1])
        elif i == n - 1:
            print(max_l[-2])
        else:
            print(max(max_l[i-1], max_r[i+1]))

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
List=[]
for i in range(n):
    a=int(input())
    List.append(a)
mylist=sorted(List)
max_num=mylist[-1]
max_num_2=mylist[-2]
for i in range(n):
    if List[i]!=max_num:
        print(max_num)
    else:
        print(max_num_2)

Solution (Python):
n = int(input())
a = ['']*n
for i in range(n):
    a[i] = int(input())
_a = sorted(a, reverse=1)
a_max = _a[0]
a_second = _a[1]
ans = [a_max] * n

for i in range(n):
    if a[i] == a_max:
        ans[i] = a_second
    else:
        pass

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

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

n = int(input())

max1 = 0
max2 = 0
maxindex = -1

for i in range(n):
    a = int(input())
    if a >= max1:
        max2 = max1
        max1 = a
        maxindex = i
    elif a >= max2:
        max2 = a

out = [max1] * n
out[maxindex] = max2

for m in out:
    print(m)

Solution (Python):
N = int(input())
A = []
for i in range(N):
  a = int(input())
  A.append(a)
  
A2 = sorted(A)
mx1 = A2[-1]
mx2 = A2[-2]
ind = A.index(mx1)

for i in range(N):
  if i != ind:
    print(mx1)
  else:
    print(mx2)

Solution (Python):
import heapq

N=int(input())
A=[]
B=[]
heapq.heapify(B)
for _ in range(N):
    a=int(input())
    A.append(a)
    heapq.heappush(B,-a)

X=-heapq.heappop(B)
Y=-heapq.heappop(B)

for i in range(N):
    if A[i]==X:
        print(Y)
    else:
        print(X)

Solution (Python):
import copy
n = int(input())
lst = []
for i in range(n):
  lst.append(int(input()))
slst = copy.deepcopy(lst)
slst.sort(reverse=True)
for i in range(n):
  print(slst[1] if lst[i] == slst[0] else slst[0])

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
temp = sorted(a)
m1 = temp[-1]
m2 = temp[-2]
for i in range(n):
    if a[i] == m1:
        print(m2)
    else:
        print(m1)

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

temp=sorted(A,reverse=True)
maxi=temp[0]
secmaxi=temp[1]

for a in A:
    if a!=maxi:
        print(maxi)
    else:
        print(secmaxi)

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

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

right = [0]
left =[0]

for a in A:
    left.append(max(a,left[-1]))
    
for a in reversed(A):
    right.append(max(a,right[-1]))

    
for i in range(N):
    ans = max(left[i], right[N-i-1] )
    print(ans)

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
B=sorted(A)
M=max(A)
count=A.count(M)
for i in range(N):
  if count>1:
    print(M)
  elif A[i]!=M:
    print(M)
  else:
    print(B[-1-count])

Solution (Python):
N = int(input())
A = [0]*N
for I in range(0,N):
    A[I] = int(input())
    
Disp = [max(A)]*N
Sort = sorted(A,reverse=True)[:2]
if Sort[0]==Sort[1]:
    print('\n'.join(map(str,Disp)))
else:
    Disp[A.index(max(A))] = Sort[1]
    print('\n'.join(map(str,Disp)))

Solution (Python):
import sys

N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
M = sorted(A)
if M[-1] == M[-2]:
    for i in range(N):
        print(M[-1])
    sys.exit()
high_num = M[-1]
for i in range(N):
    if A[i] == high_num:
        print(M[-2])
    else:
        print(M[-1])

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

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A, reverse=True)
for a in A:
    if a == B[0]:
        print(B[1])
    else:
        print(B[0])

Solution (Python):









import sys
input = sys.stdin.readline

N = int(input())
An = [int(input()) for i in range(N)]
Asort = sorted(An, reverse=True)

for A in An:
    if A != Asort[0]:
        print(Asort[0])
    else:
        print(Asort[1])

Solution (Python):
import copy
n=int(input())
tmp=0
a=[]
for i in range(n):
    a.append(int(input()))
s=copy.deepcopy(a)
s.sort(reverse=True)
t=s[0]
tt=s[1]
for i in range(n):
    if a[i]==t:
        print(tt)
    else:
        print(t)

Solution (Python):
N = int(input())
lis = []
for i in range(N):
    lis.append(int(input()))
max_value = max(lis)
max_index = lis.index(max_value)
max_count = lis.count(max_value)
next_max = sorted(lis)[-2]
if max_count ==1:
    for i in range(N):
        if i == max_index:
            print(next_max)
        else:
            print(max_value)
else:
    for i in range(N):
            print(max_value)

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

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

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

B = sorted(A)
for n in A:
    if B[-1] != n:
        print(B[-1])
    else:
        print(B[-2])

Solution (Python):
n = int(input())
an=[]
for _ in range(n):
    an.append(int(input()))
bn = sorted(an, reverse=True)
s = bn[0]
t= bn[1]
for i in an:
    if i == s:
        print(t)
    else:
        print(s)

Solution (Python):
n=int(input())
a=[]
for _ in range(n):
    a.append(int(input()))
s=0
for i in range(n):
    if s<=a[i]:
        s=a[i]
t=0
for i in range(n):
    if s==a[i]:
        t+=1
if t>=2:
    for i in range(n):
        print(s)
else:
    u=0
    for i in range(n):
        if u<=a[i] and a[i]!=s:
            u=a[i]
    for i in range(n):
        if a[i]==s:
            print(u)
        else:
            print(s)

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

a = [int(input()) for _ in range(n)]
l_new = sorted(a,reverse=True)
m = max(a)

b = a.count(m)

if b >= 2 :
    for i in range(n) :
        print(m)

else :
    for i in range(n) :
        if a[i] != m :
            print(m)
        else :
            print(l_new[1])

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

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

first = (0, 0)
second = 0
for i in range(N):
    A = int(input())
    if first[1] < A:
        first = (i, A)
    elif second < A:
        second = A

for i in range(N):
    if i != first[0]:
        print(first[1])
    else:
        print(second)

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

max_1st = max(a)
max_2nd = sorted(a)[-2]
for i in range(n):
    if a[i] == max_1st:
        print(max_2nd)
    else:
        print(max_1st)

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

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
m = max(a)
p = a.index(m)
a.remove(m)
s = max(a)
for i in range(n):
    if i != p:
        print(m)
    else:
        print(s)

Solution (Python):
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
big=max(A)
bigsize=A.count(big)
if bigsize==1:
    a=sorted(A)
    next=a[-2]
    for i in range(N):
        if A[i]==big:
            print(next)
        else:
            print(big)
else:
    for i in range(N):
        print(big)

Solution (Python):
n=int(input())
import copy 
l=[int(input()) for i in range(n)]
a=copy.deepcopy(l)
l.sort(reverse=True)
ma=l[0]
sec=l[1]
for i in a:
  if i==ma:
    print(sec)
  else:
    print(ma)

Solution (Python):
n,*l=map(int,open(0))
s=sorted(l)
for i in l: print(s[(i<s[-1])-2])

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

a_max = max(a)
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
from operator import itemgetter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    a_max = max(A)
    max_count = A.count(a_max)
    for a in A:
        if a != a_max:
            print(a_max)
        elif max_count > 1:
            print(a_max)
        else:
            B = A[:]
            B.remove(a_max)
            print(max(B))

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from operator import itemgetter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())
    
    B = [(i, a) for i, a in enumerate(A)]
    B.sort(key=itemgetter(1))
    ans = [0] * N
    for i, a in B[:-1]:
        ans[i] = B[-1][1]
        
    ans[B[-1][0]] = B[-2][1]
    
    print(*ans, sep='\n')
    
    return


if __name__ == '__main__':
    main()

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

B=sorted(A)
count=B[-1]==B[-2]
M=B[-1]
M2=B[-2]
for a in A:
    if count:
        print(M)
    elif a==M:
        print(M2)
    else:
        print(M)

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
A2 = sorted(A, reverse = True)

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

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

for i in range(N):
  A.append(int(input()))
B=sorted(A)
B=B[:len(B)-1]
C=[max(A)]*N
C[A.index(max(A))]=max(B)
for j in range(N):
  print(C[j])

Solution (Python):
import copy
n = int(input())
a = [0]*n
for i in range(n):
  a[i] = int(input())
b = copy.copy(a)
b.sort(reverse=True)

for j in range(n):
  if a[j] == b[0]:
    print(b[1])
  else:
    print(b[0])

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A,reverse=True)
x = B[1]
y = B[0]
m = A.index(max(A))

for i in range(N):
  if i == m:
    print(x)
  else:
    print(y)

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

b = sorted(a)
a1 = b[-1]
a2 = b[-2]
for i in range(n):
    if a[i] == a1:
        print(a2)
    else:
        print(a1)

Solution (Python):
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n = i()
a = [i() for _ in range(n)]

b = sorted(a)
ans1 = b[-1]
ans2 = b[-2]

for i in range(n):
	if a[i] == ans1 :
		print(ans2)
	else:
		print(ans1)

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

fromL=[0 for _ in range(N)]
for i in range(1,N):
  fromL[i]=max([fromL[i-1],ls[i-1]])

fromR=[0 for _ in range(N)]
for i in range(1,N):
  fromR[N-1-i]=max([fromR[N-i],ls[N-i]])

for i in range(N):
  print(max([fromL[i],fromR[i]]))

Solution (Python):
n = int(input())
a = list()
for x in range(n):
    a.append(int(input()))

copy_a = sorted(a, reverse=True)

A_Max = copy_a[0]
A_Second = copy_a[1]

for x in range(n):
    if a[x] == A_Max:
        print(A_Second)
    else:
        print(A_Max)

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

fromleft = [0]*N
fromright = [0]*N
fromleft[0] = A[0]
fromright[-1] = A[-1]

for i in range(1,N):
    fromleft[i] = max(A[i],fromleft[i-1])
    fromright[-(i+1)] = max(A[-(i+1)],fromright[-i])




for i in range(N):
    print(max(fromleft[i],fromright[i]))

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
maxi = max(A)
ind = A.index(maxi)
A[ind] = 0
ans = [maxi]*N
nmaxi = max(A)
ans[ind] = nmaxi
for i in ans:
    print(i)

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

Solution (Python):
n=int(input())
a=[]
for i in range(n):
  a_=int(input())
  a.append(a_)
  
maxa=max(a)
s=sorted(a)
s.reverse()
if s[1]==maxa:
  [print(maxa) for i in range(n)]
  
else:
  for i in range(len(a)):
    if a[i]==maxa:
      a[i]=s[1]
      
    else:
      a[i]=maxa
  
  [print(i) for i in a]

Solution (Python):
N = int(input())
lis = [int(input()) for _ in range(N)]
lis_c = lis.copy()

mx1 = max(lis_c)
lis_c.remove(mx1)
mx2 = max(lis_c)

for i in lis:
    if i == mx1:
        print(mx2)
    else:
        print(mx1)

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

a = [int(input()) for i in range(n)]
a_max = max(a)
a2 = a[:]
a2.remove(a_max)
a_second_max = max(a2)

for i in range(n):
    if a[i] == a_max:
        print(a_second_max)
    else:
        print(a_max)

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

sa = sorted(a)
amx = sa[-1]
amx2 = sa[-2]

for ai in a:
  print(amx if ai != amx else amx2)

Solution (Python):
def MI():
    return map(int, input().split())

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

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

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

Solution (Python):
import numpy as np
N = int(input())
A_lis = [int(input()) for _ in range(N)]

num = sorted(A_lis, reverse=True)[:2]
for i in A_lis:
    if i != num[0]:
        print(num[0])
    else:
        print(num[1])

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

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
B = list(set(A))
B.sort()
B.reverse()
if len(B) == 1 or A.count(B[0])>= 2:
  for i in range(N):
    print(B[0])
else:
  for i in range(N):
    if A[i] == B[0]:
      print(B[1])
    else:
      print(B[0])

Solution (Python):








import sys,collections
sys.setrecursionlimit(1000000)
INF = float("inf")

N = int(sys.stdin.readline())
A = tuple(int(sys.stdin.readline()) for _ in range(N)) 

left=[-INF]*(N+1)
right=[-INF]*(N+1)

for i in range(N):
    left[i+1] = max(left[i],A[i])

for i in range(N)[::-1]:
    right[i] = max(right[i+1],A[i])


for i in range(N):
    
    print(max(left[max(0,i)],right[min(N,i+1)]))

Solution (Python):
L=list()
N=int(input())
for i in range(N):
  a=int(input())
  L.append(a)
c=max(L)
d=sorted(L)[-2]
for i in range(N):
  if L[i]==c:
    print(d)
  else:
    print(c)

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

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

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
b=list(set(a))
if len(b)==1:
  for i in range(n):
    print(b[0])
elif a.count(b[-1])>=2:
  for i in a:
    print(b[-1])
else:
  for i in a:
    if i==b[-1]:
      print(b[-2])
    else:
      print(b[-1])

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
    
right=[0]
left=[0]
maxr=0
maxl=0
for i in range(n-1):
    maxl=max(maxl,a[i])
    maxr=max(maxr,a[-i-1])
    left.append(maxl)
    right.append(maxr)
for i in range(n):
    print(max(left[i],right[-i-1]))

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

ans1=max(a)
for i in range(n):
    if a[i]==ans1:
        a[i]=0
        point=i
        break
b=[ans1]
b*=n
ans2=max(a)
b[point]=ans2
for i in range(n):
    print(b[i])

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
max_list = max(a)
ans = sorted(list(a))[-2]
for i in range(n):
    if a[i] == max_list:
        print(ans)
    else :
        print(max_list)

Solution (Python):
def resolve():
    N = int(input())
    A = []
    C = []
    for _ in range(N):
        a = int(input())
        A.append(a)
        C.append(a)
    B = max(A)
    C.remove(B)
    D = max(C)
    for i in range(N):
        if B != A[i]:
            print(B)
        else:
            print(D)
resolve()

Solution (Python):


import sys
from copy import deepcopy
input=sys.stdin.readline

def main():
    N=int(input())
    A=[int(input()) for i in range(N)]
    B=deepcopy(A)
    B.sort()
    M=max(A)
    if A.count(M)>1:
        for i in range(N):
            print(M)
    else:
        for i in range(N):
            if A[i] == M:
                print(B[N-2])
            else:
                print(M)
    
if __name__=="__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
readline = sys.stdin.readline
A = [int(input()) for _ in range(n)]

import heapq

Q = [-a for a in A]
heapq.heapify(Q)

ans = []

for i in range(n):
    if -Q[0] == A[i]:
        tmp = heapq.heappop(Q)
        ans.append(-Q[0])
        heapq.heappush(Q, tmp)
    else:
        ans.append(-Q[0])

print(*ans)

Solution (Python):
n = int(input())
a = []
b = []
for i in range(n):
  s = int(input())
  a.append(s)
  b.append(s)
m = max(a)
b.sort()
b = b[:-1]
m2 = b[-1]
for i in range(n):
  if a[i] == m:
    print(m2)
  else:
    print(m)

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

if __name__ == '__main__':
    main()

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

if a.count(b[-1]) > 1:
    for i in range(N):
        print(b[-1])
else:
    ind = a.index(b[-1])
    for i in range(N):
        if i == ind:
            print(b[-2])
        else:
            print(b[-1])

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


def read():
    N = int(input().strip())
    A = []
    for i in range(N):
        a = int(input().strip())
        A.append(a)
    return N, A


def solve(N, A):
    L = list(accumulate(A, max))
    R = list(accumulate(A[::-1], max))[::-1]
    for i in range(N):
        ans = 0
        if i > 0:
            ans = max(ans, L[i-1])
        if i < N-1:
            ans = max(ans, R[i+1])
        print(ans)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):






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





idx_max = A.index(max(A))
tmp = [x for x in A]
del tmp[idx_max]
most_max = max(A)
second_max = max(tmp)
for i in range(N):
    if i == idx_max:
        print(second_max)
    else:
        print(most_max)

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

Solution (Python):
n=int(input())
top=0
top_num=0
second_num=0
for i in range(n):
    a=int(input())
    if a>top_num:
        top=i
        top_num=a
    elif a>second_num:
        second_num=a

for i in range(n):
    if i==top:
        print(second_num)
    else:
        print(top_num)

Solution (Python):
def resolve():
    N = int(input())
    A = [int(input()) for n in range(N)]
    B = A.copy()
    B.sort()
    mx = max(A)
    count = A.count(mx)

    for i in range(N):
        if mx > A[i]:
            print(mx)
        elif mx == A[i]:
            if count >= 2:
                print(mx)
            else:
                print(B[-2])
    return


resolve()

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

res=max(a)
ans=[res]*n
s=[i for i, x in enumerate(a) if x ==res]
a.sort()
res2=a[-2]
for i in s:
    ans[i]=res2
for i in range(n):
    print(ans[i])

Solution (Python):

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

mx = max(A)
sorted_A = sorted(A)
mx2 = sorted_A[-2]

for j in range(N):
    if mx != A[j]:
        print(mx)
    else:
        print(mx2)

Solution (Python):

import copy

n = int(input())
A = list()

for num in range(n):
    A.append(int(input()))
    
sorted_A = copy.copy(A)
sorted_A.sort()

for num in A:
    if num == 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 = sorted(a, reverse=True)
a_max = b[0]
for i in range(len(a)):
    if a[i] == a_max:
        print(b[1])
    else:
        print(a_max)

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

first = max(A)
firstCnt = 0
firstIdx = 0
second = 0
secondIdx = 0

for i,a in enumerate(A):
  if a < first:
    if a > second:
      second = a
      secondIdx = i
  else:
    firstIdx = i
    firstCnt += 1

if firstCnt > 1:
  for i in range(N):
    print(first)
else:
  for i in range(N):
    if i == firstIdx:
      print(second)
    else:
      print(first)

Solution (Python):
n=int(input())
a=[]
m1=0
m2=0
for i in range(n):
    a.append(int(input()))
    A=a[i]
    if a[i]>m1:
        m2=m1
        m1=a[i]
        index=i
    elif m1==a[i]:
        m2=a[i]
    elif a[i]>m2:
        m2=a[i]
for i in range(n):
    if i==index:
        print(m2)
    else:
        print(m1)

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

Solution (Python):
import sys

input = lambda: sys.stdin.readline().rstrip()


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

    asort = sorted(A, reverse=True)
    m1 = asort[0]
    m2 = asort[1]

    for i in range(N):
        if A[i] == m1:
            print(m2)
        else:
            print(m1)


if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
a = [int(input()) for _ in range(N)]
first = max(a)
f_id = a.index(max(a))
second = max(a[:f_id] + a[f_id+1:])

for i in range(N):
  if a[i] == first:
    print(second)
  else:
    print(first)

Solution (Python):
n=int(input())
a=[]
mx=0
sec=0
for i in range(0,n):
  a.append(int(input()))
  if a[i]>sec:
    if a[i]>mx:
      sec=mx
      mx=a[i]
    else:
      sec=a[i]

for x in a:
  if x==mx:
    print(sec)
  else:
    print(mx)

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

    As_sort = sorted(As)
    second, first = As_sort[-2:]
    for i in range(n):
        if As[i] == first:
            print(second)
        else:
            print(first)


if __name__ == "__main__":
    main()