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):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys, math, bisect, random, itertools
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    n = II()
    print(n*n*3)
    return


def B():
    n, d = LI()
    print((n-1)//(2*d+1)+1)
    return


def C():
    n = II()
    a = IR(n)
    b = a[::1]
    b.sort()
    for i in range(n):
        if a[i] == b[-1]:
            print(b[-2])
        else:
            print(b[-1])
    return


def D():
    return


def E():
    return


def F():
    return


if __name__ == '__main__':
    C()

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

sort = sorted(a)
m1 = sort[-1]
m2 = sort[-2]
if m1 == m2:
  for i in range(N):
    print(m1)
else:
  j = a.index(m1)
  for i in range(N):
    if i == j:
      print(m2)
    else:
      print(m1)

Solution (Python):
t=int(input())
l=[]
b=0
c=0
for j in range(t):
    a=int(input())
    l.append(a)
    if(a>b):
        b=a
    elif(a>c):
        c=a
for i in l:
    if(i!=b):
        print(b)
    else:
        print(c)

Solution (Python):
a = int(input())
ar = [int(input()) for i in range(a)]
br = sorted(ar)
am = max(ar)
bm = br[-2]
for r in ar:
    if r == am:
        print(bm)
    else:
        print(am)

Solution (Python):
N = int(input())
l = [int(input()) for i in range(N)]
l2 = sorted(l)
m = l2[-1]
m2 = l2[-2]


for i in range(N):
    if l[i] != m:
        print(m)
    else:
        print(m2)

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

L = [0] * n
for i in range(1, n):
    L[i] = max(L[i - 1], A[i - 1])


R = [0] * n
for i in range(n - 2, -1, -1):
    R[i] = max(R[i + 1], A[i + 1])

for i in range(n):
    print(max(L[i], R[i]))

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

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

M = max(A)
    
if A.count(max(A)) > 1:
    for i in range(N):
        print(M)
else:
    for i in range(N):
        if A[i] != M:
            print(M)
        else:
            print(sorted(A)[-2])

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

for i in range(N):
    A.append(int(input()))
    if A[i]>a_max:
        a_max=A[i]
    elif A[i]>=a_max_next:
        a_max_next=A[i]

for i in range(N):
    if A[i]==a_max:
        print(a_max_next)
    else:
        print(a_max)

Solution (Python):
p=print;n,*a=map(int,open(0));r=range(n);m=max(a);o=sorted(a)[-2];[[p(m)for _ in r]if m==o else[p(o)if a[i]==m else p(m)for i in r]]

Solution (Python):
N=int(input())
A=[]
first=0
first_num=0
second=0
second_num=0
for i in range(N):
    a=int(input())
    A.append(a)
    if a>first:
        first=a
        first_num=i+1
    elif a>second:
        second=a
        second_num=i+1

for j in range(N):
    if A[j]==first:
        print(second)
    else:
        print(first)

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N = int( input())
    A = [ int( input()) for _ in range(N)]
    B = [0]*N
    C = [0]*N
    B[0] = A[0]
    C[N-1] = A[N-1]
    for i in range(N-1):
        B[i+1] = max(B[i], A[i+1])
        C[N-2-i] = max( C[N-1-i], A[N-2-i])
    print(C[1])
    for i in range(1, N-1):
        print( max(B[i-1], C[i+1]))
    print(B[N-2])
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
array = [int(input()) for x in range(n)]
sorted_array = sorted(array)
max_array1 = sorted_array[-1]
max_array2 = sorted_array[-2]

for i in array:
    if i == max_array1:
        print(max_array2)
    else:
        print(max_array1)

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
B = sorted(A)
B_M = max(B)
B_M_1 = max(B[:n-1])
for i in range(n):
    if A[i] == B_M:
       print(B_M_1)
    else:
        print(B_M)

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

sorted_list = sorted(a,reverse=True)
first = sorted_list[0]
secend = sorted_list[1]

for num in a:
    if num != first:
        print(first)
    else:
        print(secend)

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

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

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

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
A=[INT() for i in range(N)]

A2=[0]+list(accumulate(A, max))+[0]

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

for i in range(1, N+1):
    print(max(A2[i-1], A3[i+1]))

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
A=[INT() for i in range(N)]

A2=[0]+list(accumulate(A, max))+[0]

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

for i in range(1, N+1):
    print(max(A2[i-1], A3[i+1]))

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

m2,m = sorted(A)[-2:]
for i in range(N):
    if A[i] == m: print(m2)
    else: print(m)

Solution (Python):
n = int(input())
v = []
f = []
for i in range(n):
	x = int(input())
	v.append(x)
	f.append(x)
f.sort()
for i in range(n):
	if v[i] == f[n-1]:
		print(str(f[n-2]) + ' ', end='')
	else:
		print(str(f[n-1]) + ' ', end='')

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
A=[INT() for i in range(N)]

A2=[0]+list(accumulate(A, max))+[0]
A3=[0]+list(accumulate(A[::-1], max))[::-1]+[0]

for i in range(1, N+1):
    print(max(A2[i-1], A3[i+1]))

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

Solution (Python):
N, *A = map(int, open(0).read().split())
B = A[:]
B.sort()
b0 = B[-1]; b1 = B[-2]
for i in range(N):
    print(b1 if b0 == A[i] else b0)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
MAX = max(a)
MAX_num = a.count(MAX)
if MAX_num == 1:
  MAX_index = a.index(MAX)
  a.sort()
  for i in range(n):
    if i == MAX_index:
      print(a[-2])
    else:
      print(MAX)
else:
  for i in range(n):
    print(MAX)

Solution (Python):
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):
n=int(raw_input())
m1,m2=0,0
i1=-1
for i in xrange(n):
    m=int(raw_input())
    if m>m1:
        m2,m1=m1,m
        i1=i
    elif m>m2:
        m2=m
for i in xrange(n):
    if i==i1:
        print m2
    else:
        print m1

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

m = max(a)
idxs = [i for i, v in enumerate(a) if v == m]

if len(idxs) > 1:
    for i in range(n):
        print(m)

if len(idxs) == 1:
    idx = idxs[0]
    a.pop(idx)
    m2 = max(a)
    for i in range(n):
        if idx == i:
            print(m2)
        else:
            print(m)

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

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd
import time


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

mod = 1000000007

if __name__ == "__main__":
    N= int(sys.stdin.readline())
    A = list(map(int,sys.stdin.readlines()))
    l = sorted(A)
    m1 = l[-1]
    m2 = l[-2]
    for i in range(N):
        if A[i] == m1:
            print(m2)
        else:
            print(m1)

Solution (Python):
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict






INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)









N = int(input())
A = []
m1 = 0
m2 = 0

for _ in range(N):
    a = int(input())
    if a >= m1:
        m2 = m1
        m1 = a
    elif a > m2:
        m2 = a
    A.append(a)

for a in A:
    if a == m1:
        print(m2)
    else:
        print(m1)

pass

Solution (Python):
N = int(input())
t = []
for i in range(N):
    t.append(int(input()))
l = t.copy()
l.sort()

for i in t:
    if i != l[-1]:
        print(l[-1])
    else:
        print(l[-2])

Solution (Python):
cnt = int(input())
lst = [int(input()) for i in range(cnt)]
lst2 = sorted(lst)
for i in lst:
  if i == lst2[-1]:
    print(lst2[-2])
  else:
    print(lst2[-1])

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

b = list(reversed(sorted(a)))

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

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

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

    A_cp = sorted(A)
    max1_A = A_cp.pop(-1)
    max2_A = max(A_cp)
    for a in A:
        if a == max1_A:
            print(max2_A)
        else:
            print(max1_A)
            
        
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
s = A.index(max(A))
x = max(A)
A.sort()
y = A[-2]
ans = []
for i in range(N):
  ans.append(x)
  
ans[s] = y

for i in ans:
  print(i)

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = list(map(int, [input() for _ in range(n)]))
    SA = sorted(A)
    ma, ma2 = SA[-1], SA[-2]
    ans = (ma if i != ma else ma2 for i in A)
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = list(map(int, [input() for _ in range(n)]))
    SA = sorted(A)
    ma, ma2 = SA[-1], SA[-2]
    ans = [ma if i != ma else ma2 for i in A]
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = list(map(int, [input() for _ in range(n)]))
    SA = sorted(A)
    ma, ma2 = SA[-1], SA[-2]
    ans = []
    for i in A:
        if i != ma:
            ans += [ma]
        else:
            ans += [ma2]
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

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

p=[0]*(2*n)
q=[0]*(2*n)
m=0
for i in range(n+2):
    m=max(a[i],m)
    p[i]=m
m=0
for i in reversed(range(n+2)):
    m=max(a[i],m)
    q[i]=m
for i in range(1,n+1):
    print(max(p[i-1],q[i+1]))

Solution (Python):
import copy
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
b = copy.deepcopy(a)
b.sort()
firint = b[-1]
secint = b[-2]

for i in a:
    if i == firint:
        print(secint)
    else:
        print(firint)

Solution (Python):
import sys
import math
import copy
a = int(input());
b = [int(input()) for i in range(a)];
c = 0;
first = max(b);
g = b.copy();
h = g.index(first);
del g[h];
second = max(g);

for i in range(a):
    if first > b[i]:
        print(first);
    elif first == b[i]:
        print(second);

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

n = int(input())
data = [int(input()) for k in range(n)]
m1,m2 = sorted(data, reverse=1)[:2]
b = [m2 if x==m1 else m1 for x in data]
print('\n'.join(map(str,b)))

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

b = sorted(a, reverse = True)

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

Solution (Python):
n=int(input())
a=[int(input())for i 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):
n = int(input())
data = [int(input()) for k in range(n)]
m1,m2 = sorted(data, reverse=1)[:2]
b = [m2 if x==m1 else m1 for x in data]
print('\n'.join(map(str,b)))

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

sorted_inputs = sorted(inputs, reverse=True)
for i in range(N):
    if sorted_inputs[0] == inputs[i]:
        print(sorted_inputs[1])
    else:
        print(sorted_inputs[0])

Solution (Python):
n = int(input())
a = []
k = []
for i in range(n):
    c = int(input())
    a.append(c)
    k.append(c)
a = sorted(a)
m = a[-1]
l = a[-2]
for i in range(len(k)):
    if(m!=k[i]):
        print(m)
    else:
        print(l)

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

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

new_A = sorted(A, reverse=True)

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

Solution (Python):
from itertools import accumulate

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

L = [0] + list(accumulate(A, max))
R = [0] + list(accumulate(reversed(A), max))
R.reverse()

for l, r in zip(L[:-1], R[1:]):
    print(max(l, r))

Solution (Python):
N = int(input())
L=[]
i = 0
s = 0
M2 = 0
a = 0
for i in range (0,N):
    L.append(int(input()))
M = L[0]
for s in range(1,N):
    if  L[s] > M:
        M = L[s]
for s in range(0,N):
    if L[s] == M:
        a += 1
    else:
        if L[s] > M2:
            M2 = L[s]
for i in range(0,N):
    if L[i] == M and a < 2:
        print(M2)
    else:
        print(M)

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    max_i = 0
    sec_i = 0
    m = 0
    s = 0
    for ind, val in enumerate(A):
        if val > m:
            s = m
            sec_i = max_i
            m = val
            max_i = ind
        elif s < val <= m:
            s = val
            sec_i = ind
    for i in range(N):
        if i == max_i:
            print(s)
        else:
            print(m)

main()

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

B = sorted(A)
f, s = B[-1], B[-2]

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

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

L_max=[0]*(N+1)
R_max=[0]*(N+1)

L=0
for i in range(N):
    L=max(L,a[i])
    L_max[i]=L

R=0
for i in range(N):
    R=max(R,a[-i-1])
    R_max[i]=R

for j in range(N):
    print(max(L_max[j-1],R_max[N-j-2]))

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

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

B=copy.copy(S)
B.sort()
if N>2:
	C=int(B[N-1])
	D=int(B[N-2])
	for i in S:
		if int(i)>=C:
			print(D)
		else:
			print(C)

else:
	print(S[1])
	print(S[0])

Solution (Python):
def c_exception_handling(N, A):
    max_from_left = [0] * N
    max_from_right = [0] * N

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

    ans = [max(a, b) for a, b in zip(max_from_left, max_from_right)]
    return '\n'.join(map(str, ans))

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

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

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

b = sorted(a, reverse = True)
maximum = b[0]

for i in a: 
	print( b[1] if i == maximum else maximum )

Solution (Python):
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)

Solution (Python):
N = int(input())
one = two = 0
A = []
for i in range(N):
    a = int(input())
    if a >= one:
        one,two = a,one
    elif a >= two:
        two = a
    A.append(a)

for i in range(N):
    if A[i] == one:
        print(two)
    else:
        print(one)

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

b = sorted(a, reverse = True)
maximum = b[0]

for i in a:
    if i == maximum:
        print(b[1])
    else:
        print(maximum)

Solution (Python):


n = int(input())

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

a_sorted = sorted(a, reverse=True)

for i in range(n):
    if a[i] == a_sorted[0]:
        print(a_sorted[1])
        continue

    print(a_sorted[0])

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

a_max1 = 0
a_max2 = 0
a_max1_index = 0
for i in range(n):
    a_i = int(input())
    if a_max1 <= a_i:
        a_max2 = a_max1
        a_max1 = a_i
        a_max1_index = i
    elif a_max2 <= a_i: 
        a_max2 = a_i

for i in range(n):
    if i == a_max1_index:
        print(a_max2)
    else:
        print(a_max1)

Solution (Python):


n = int(input())

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

a_sorted = sorted(a, reverse=True)

for i in range(n):
    if a[i] == a_sorted[0]:
        print(a_sorted[1])
        continue

    print(a_sorted[0])

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

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

Solution (Python):
N = int(input())
 
A = []
max1 = 0
max2 = 0
for _ in range(N):
    ai = int(input())
    if max1 < ai:
        max1 = ai
    elif max2 < ai:
        max2 = ai
 
    A.append((ai, max2, max1))

for i in range(N):
    ai, _, _ = A[i]


    if max1 == ai:
        print(max2)
    else:
        print(max1)

Solution (Python):
n = int(input())
arr = [int(input()) for _ in range(n)]
first = 0
second = 0
for a in arr:
  if a >= first:
    second = first
    first = a
  elif a > second:
    second = a
for a in arr:
  print(first if a != first else second)

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

left = [0] * n
for i in range(n):
    if i == 0:
        left[i] = a[i]
    else:
        left[i] = max(left[i - 1], a[i])

right = [0] * n
for i in range(n - 1, -1, -1):
    if i == n - 1:
        right[i] = a[i]
    else:
        right[i] = max(right[i + 1], a[i])

for i in range(n):
    if i == 0:
        print(right[1])
    elif i == n - 1:
        print(left[n - 2])
    else:
        print(max(left[i - 1], right[i + 1]))

Solution (Python):
n=int(input())
max1 = 0
max2 = 0
A=[]
for _ in range(n):
    num = int(input())
    if num == max(num, max1):
        max2 = max1
        max1 = num
    elif num == max(num, max2):
        max2 = num
    A.append(num)
for a in A:
    if a == max1:
        print(max2)
    else:
        print(max1)

Solution (Python):
a=[int(input()) for _ in [0]*int(input())]
m1,m2=sorted(a, reverse=True)[:2]
b=[m2 if x==m1 else m1 for x in a]
print('\n'.join(map(str,b)))

Solution (Python):
def c_exception_handling(N, A):
    max_from_left = [0]
    max_from_right = []

    tmp = 0
    for a in A[:N - 1]:
        if a > tmp:
            tmp = a
        max_from_left.append(tmp)
    tmp = 0
    for a in A[N:0:-1]:
        if a > tmp:
            tmp = a
        max_from_right.append(tmp)

    max_from_right = max_from_right[::-1] + [0]
    ans = [max(a, b) for a, b in zip(max_from_left, max_from_right)]
    return '\n'.join(map(str, ans))

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

Solution (Python):
import copy
n=int(input())
a=[int(input()) for i in range(n)]
a_copy=copy.deepcopy(a)
a_copy=sorted(a_copy)
max_num=a_copy[-1]
sec_num=a_copy[-2]
answer=[]
for j in range(len(a)):
  if a[j]==max_num:
    answer.append(sec_num)
  else:
    answer.append(max_num)
for k in range(len(answer)):
  print(answer[k])

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

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


max_a = np.max(a)
premax_a = np.sort(a)[::-1][1]

for i in range(N):
    if a[i] == max_a:
        print(premax_a)
    else:
        print(max_a)

Solution (Python):
n=int(input())
A=[int(input()) for _ in range(n)]
ma=max(A)
ma_i=A.index(ma)
for i in range(n):
    if i==ma_i:
        print(max(A[:i]+A[i+1:]))
    else:
        print(ma)

Solution (Python):
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)

N = int(input())
A = [int(input()) for _ in range(N)]
largest_num = 0
largest_ind = -1
second_large_num = 0

for i in range(N):
	if A[i] > largest_num:
		second_large_num = largest_num
		largest_num = A[i]
		largest_ind = i
	elif A[i] > second_large_num:
		second_large_num = A[i]
		
for i in range(N):
	if i == largest_ind:
		print(second_large_num)
	else:
		print(largest_num)

Solution (Python):
N = int(input())
a = []
for i in range(N):
        x = int(input())
        a.append(x)
m = max(a)
n = a.index(max(a))
for i in range(N):
        if i == n:
                b = a[:n] + a[n+1:]
                print(max(b))
        else:
                print(m)

Solution (Python):
import sys
input = sys.stdin.readline
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 = []
for i in range(N):
    A.append(int(input()))
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())
l=[]
for i in range(n):
    l.append(int(input()))
m=sorted(l)
m1=m[-1]
m2=m[-2]
c=l.count(m1)

for j in l:
    if j==m1:
        if c>1:
            print(m1)
        else:
            print(m2)
    else:
        print(m1)

Solution (Python):

import numpy as np

def main():
    N = int(input().strip())
    arr = np.zeros(N, np.int32)
    for i in range(N):
        arr[i] = int(input().strip())
    arr2 = np.sort(arr)[::-1]
    max1, max2 = arr2[0], arr2[1]

    for i in range(N):
        print(max1) if arr[i] != max1 else print(max2)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
l = [int(input()) for _ in range(n)]
Max1 = sorted(l)[-1]
Max2 = sorted(l)[-2]
for i in range(n):
    print(Max1 if l[i] != Max1 else Max2)

Solution (Python):
N = int(input())
M = 1
prev_M = 0
A = []
for i in range(N):
  a = int(input())
  A += [a]
  if a == M:
    prev_M = M
  elif a > M:
    prev_M = M
    M = a
  elif a > prev_M:
    prev_M = a
for i in range(N):
  if A[i] != M:
    print(M)
  else:
    print(prev_M)

Solution (Python):
import numpy as np

N=int(input())

X=[]
for x in range(N):
    X.append(int(input()))

Y=[]
Y=np.sort(X)[::-1]

for i in range(N):
    ans=Y[0]
    if ans==X[i]:
        ans=Y[1]
    print(ans)

Solution (Python):
n=int(input())
arr=[int(input()) for _ in range(n)]
lmax=[0]*n
rmax=[0]*n
tmp=arr[0]
for i in range(1,n):
  lmax[i]=tmp
  tmp=max(tmp,arr[i])
tmp=arr[-1]
for i in range(n-2,-1,-1):
  rmax[i]=tmp
  tmp=max(tmp,arr[i])
for i in range(n):
  print(max(lmax[i],rmax[i]))

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

sorted_a = sorted(a, reverse=True)
first_max = sorted_a[0]
second_max = sorted_a[1]

ans = ""
ans += (str(first_max) + '\n') * (a.index(first_max))
ans += (str(second_max) + '\n')
ans += (str(first_max) + '\n') * (N - a.index(first_max) - 1)
print(ans.rstrip('\n'))

Solution (Python):


import sys
import copy

sys.setrecursionlimit(1000000)


from collections import defaultdict



N = int(input())
A = []
B = []

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

B.sort(reverse=True)

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

Solution (Python):
_,*a=map(int,open(0))
b=sorted(a)
for a in a:print(b[(a<b[-1])-2])

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

Solution (Python):
import sys
input = sys.stdin.readline
 
def main():
  n = int(input())
  numList  = [int(input()) for _ in range(n)]
  maxVal = max(numList)
  workList = numList[:]
  workList.remove(maxVal)
  secondVal = max(workList)
  for num in numList:
    if num != maxVal:
      print(maxVal)
    else:
      print(secondVal)
      
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
first = 0
second = 0
As = []
for i in range(N):
    n = int(input())
    As.append(n)
    if first < n:
        first = n
    elif second < n:
        second = n

for a in As:
    if a == first:
        print(second)
    else:
        print(first)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
ma = max(a)
sma = sorted(a)[-2]

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

Solution (Python):
import numpy as np
N = int(input())
A = np.array([int(input()) for i in range(N)])
max_index=np.argmax(A)
a_max=int(A[max_index])
a_second=int(np.amax(np.delete(A,max_index)))
for i in range(N):
    if i==max_index:
        print(a_second)
    else:
        print(a_max)

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

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

m1=0
m2=0
for a in A:
  if a>=m1:
    m1,m2 = a,m1
  elif m1>a>=m2:
    m1,m2 = m1,a

ans = [m1 if a!=m1 else m2 for a in A]
for a in ans:
  print(a)

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

n = int(input()) 
num = [int(input()) for _ in range(n)] 
ma=max(num)
ind=num.index(ma)
ma2=sorted(num)[-2]
for i in range(n):
    if ind==i:
        print(ma2)
    else:
        print(ma)

Solution (Python):
from itertools import accumulate, chain, islice

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

L = accumulate(A, max)
R = accumulate(reversed(A), max)

L = chain([-1], L)
R = chain([-1], R)
R = islice(reversed(list(R)), 1, None)

M = (max(l, r) for l, r in zip(L, R))

for m in M:
    print(m)

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

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

m2,m1 = sorted(A)[N-2:]

ans = [m1 if a!=m1 else m2 for a in A]
for a in ans:
  print(a)

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np
n = int(input()) 
num = [input() for _ in range(n)] 
num = np.array(num).astype("int64")


sorted_num=sorted(num)
ma=sorted_num[-1]
ma2=sorted_num[-2]
for i in range(n):
    if ma != num[i]:
        print(str(ma).strip())
    else:
        print(str(ma2).strip())

Solution (Python):
import sys
input = sys.stdin.readline
 
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    max_val = max(A)
    AA = A[:]
    AA.remove(max_val)
    second_max_val = max(AA)
    for a in A:
        if a != max_val:
            print(max_val)
        else:
            print(second_max_val)
 
if __name__ == '__main__':
    main()

Solution (Python):
_,*a=map(int,open(0))
b=sorted(a)
for a in a:print(b[(a<b[-1])-2])

Solution (Python):
n=int(input())
l=[int(input()) for _ in range(n)]
l_s=sorted(l,reverse=True)
id=l.index(l_s[0])
for i in range(n):
    if i==id:print(l_s[1])
    else: print(l_s[0])

Solution (Python):
N = int(input())
A = []
 
for _ in range(N):
    A.append(int(input()))
    
a = max(A)
b =  sorted(A)[-2]
 
for i in range(N):
    if A[i] < a:
        print(a)
    else:
        print(b)

Solution (Python):
n = int(input())
numList  = [int(input()) for _ in range(n)]
maxVal = max(numList)
workList = numList[:]
workList.remove(maxVal)
secondVal = max(workList)
for num in numList:
  if num != maxVal:
    print(maxVal)
  else:
    print(secondVal)