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 copy
def calcmax(a,b,fst,scd,n):
    for i in xrange(n):
        if fst == a[i]:
            count = i

    for i in xrange(n):
        if count == i:
            print scd
        else:
            print fst

if __name__ == '__main__':
    n = int(raw_input())
    a = [int(raw_input()) for i in xrange(n)]
    print("")

    fst = max(a)

    b = copy.deepcopy(a)
    b.remove(fst)

    scd = max(b)

    calcmax(a,b,fst,scd,n)

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

nums = [int(input()) for i in range(N)]
first = sorted(nums)[-1]
second = sorted(nums)[-2]

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

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

nums = [int(input()) for i in range(N)]
first = sorted(nums)[-1]
second = sorted(nums)[-2]

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

Solution (Python):
n = int(input())
num = [ int(input()) for _ in range(n) ]
B = sorted(num,reverse=True)
maxnum = B[0]
maxnum2 = B[1]

for i in range(n):
    if num[i] != maxnum:
        print(maxnum)
    else:
        print(maxnum2)

Solution (Python):
n = int(input())
num = [ int(input()) for _ in range(n) ]
B = sorted(num,reverse=True)
maxnum = B[0]
maxnum2 = B[1]

for i in range(n):
    if num[i] != maxnum:
        print(maxnum)
    else:
        print(maxnum2)

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

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

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

def main():
	n = int(input().strip())
	a = [0] * n
	for i in range(n):
		a[i] = int(input().strip())
	l = [0] *(n+1)
	r = [0] *(n+1)
	for i in range(0, n):
		l[i+1] = max(l[i], a[i])
	for i in reversed(range(1, n)):
		r[i-1] = max(r[i], a[i])
	for i in range(n):
		print(max(l[i], r[i]))

if __name__ == '__main__':
	main()

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

def main():
	n = int(input().strip())
	a = [0] * n
	for i in range(n):
		a[i] = int(input().strip())
	b = sorted(a, reverse=True)
	first = b[0]
	second = b[1]
	for i in range(n):
		if a[i] == first:
			print(second)
		else:
			print(first)

if __name__ == '__main__':
	main()

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

Solution (Python):
import numpy as np
n = int(input())
a = [int(input()) for _ in range(n)]
t1 = max(a)
t1_idx = [i for i, v in enumerate(a) if v == t1]
a.sort()

if len(t1_idx) < 2:
    try:
        t2 = a[-1*len(t1_idx)-1]
    except:
        for i in range(n):
            print(t1)

    for i in range(n):
        if i in t1_idx:
            print(t2)
        else:
            print(t1)
else:
    for i in range(n):
        print(t1)

Solution (Python):

n = int(input())
arr = []
for i in range(n):
  t = int(input()) 
  arr.append([i, t])

sorted_arr = list(sorted(arr, key = lambda x : x[1]))
biggest = sorted_arr[-1]
second_biggest = sorted_arr[-2]

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

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

sorted_data = sorted(A)[::-1]
for a in A:
    if a == sorted_data[0]:
        print(sorted_data[1])
    else:
        print(sorted_data[0])

Solution (Python):
n = int(input())
values = [int(input()) for i in range(n)]
all_max = max(values)
count = 0

max_count = 0
is_include_two_max = False
for val in values:
    if val == all_max:
        max_count += 1
    if max_count == 2:
        is_include_two_max = True
        break

if is_include_two_max:
    print('\n'.join([str(all_max) for i in range(n)]))
else:
    for i, val in enumerate(values):
        if val != all_max:
            print(all_max)
        else:
            print(max(values[0:i] + values[i+1:]))

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

main()

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

n = int(input())
li = [0]*n
for i in range(n):
    li[i] = int(input())
a = 0
b = 0

for i in li:
    if i > a:
        b = a
        a = i
    elif i > b:
        b = i

for i in li:
    if i == a:
        print(b)
    else:
        print(a)

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

for i in li:
    if i > a:
        b = a
        a = i
    elif i > b:
        b = i

for i in li:
    if i == a:
        print(b)
    else:
        print(a)

Solution (Python):
from copy import copy


def main():
    N = int(input())
    A_list = [int(input()) for _ in range(N)]
    tmp_list = copy(A_list)
    tmp_list.sort()
    max_num = tmp_list[-1]
    second_max_num = tmp_list[-2]
    max_index = A_list.index(max_num)
    for i in range(N):
        if i is max_index:
            print(second_max_num)
            continue
        print(max_num)


if __name__ == '__main__':
    main()

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

tmp = A.copy();
tmp.sort()
max1_A = tmp[-1];
max2_A = tmp[-2];
max_count = A.count(max1_A);


if max_count > 1:
  for i in range(N):
    print(max1_A)

else:
  for i in range(N):
    if A[i] < max1_A:
      print(max1_A)
    else:
      print(max2_A)
      break
  for j in range(i+1,N):
    print(max1_A)

Solution (Python):
n,*a=map(int,open(0))
m=max(a)

if a.count(m)>1:
    for i in range(n):
        print(m)
else:
    s=sorted(set(a))[::-1][1]
    for i in a:
        if i==m:
            print(s)
        else:
            print(m)

Solution (Python):
import copy

n = int(input().split()[0])
a_list = []

for _ in range(n):
    a_list.append(int(input().split()[0]))

kouho_list = sorted(a_list)
max_n = max(kouho_list)
second_max_n = kouho_list[-2]

max_list = [str(max_n) if x != max_n else str(second_max_n) for x in a_list]
max_str = '\n'.join(max_list)

print(max_str)

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
a_max=max(a)
x=a.index(a_max)
a_sort=sorted(a)
y=a_sort[-2]
for i in range(n):
  if i==x:
    print(y)
  else:
    print(a_max)

Solution (Python):

N = int(input())
A = []
f = 0
s = 0
for i in range(N):
    a = int(input())
    if a >= f:
        s = f
        f = a
    elif a >= s:
        s = a
    A.append(a)

for i in range(N):
    if A[i] == f:
        print(s)
    else:
        print(f)

Solution (Python):
from sys import stdin

def main():
    n = int(stdin.readline().rstrip())
    xs = [int(stdin.readline().rstrip()) for _ in range(n)]

    max_1 = max(xs)
    key_max = xs.index(max_1)
    
    for i in range(n):
        if key_max == i:
            print(max(xs[:i]+xs[i+1:]))
        else:
          	print(max_1)

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

def main():
    n = int(stdin.readline().rstrip())
    xs = [int(stdin.readline().rstrip()) for _ in range(n)]

    max_1 = max(xs)
    key_max = xs.index(max_1)

    for i in range(n):
        if key_max == i:
            print(max(xs[0:i]+xs[i+1:]))
        else:
          	print(max_1)

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

def main():
    n = int(stdin.readline().rstrip())
    xs = [int(stdin.readline().rstrip()) for _ in range(n)]

    max_1 = max(xs)
    key_max = xs.index(max_1)

    for i in range(n):
        if key_max != i:
            print(max_1)
        elif i == 0:
            print(max(xs[i+1:]))
        elif i == n-1:
            print(max(xs[0:i]))
        else:
            print(max(max(xs[0:i]),max(xs[i+1:])))

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

def main():
    n = int(stdin.readline().rstrip())
    xs = [int(stdin.readline().rstrip()) for _ in range(n)]

    max_1 = max(xs)
    key_max = xs.index(max_1)

    for i in range(n):
        if key_max != i:
            print(max_1)
        elif i == 0:
            print(max(xs[i+1:]))
        elif i == n-1:
            print(max(xs[0:i]))
        else:
            print(max(max(xs[0:i]),max(xs[i+1:])))

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

def main():
    n = int(stdin.readline().rstrip())
    xs = [int(stdin.readline().rstrip()) for _ in range(n)]

    max_1, max_2 = sorted(xs)[-1], sorted(xs)[-2]

    for i in range(n):
        if xs[i] == max_1:
            print(max_2)
        else:
            print(max_1)
            
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
maxvalue = max(a)
maxindex = a.index(max(a))
for i in range(n):
    if i != maxindex:
        print(maxvalue)
    else:
        a_tmp = a[:]
        a_tmp.pop(maxindex) 
        print(max(a_tmp))

Solution (Python):
N = int(input())
m1 = -1
m2 = -1
As = []
for i in range(N):
    n = int(input())
    As.append(n)
    if n > m1:
        m2 = m1
        m1 = n
    else:
        if n > m2:
            m2 = n

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

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

lmax = [0]
rmax = [0]

for i in range(N-1):
    lmax.append(max(lmax[i], a[i]))
    rmax.append(max(rmax[i], a[-(i+1)]))

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

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

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

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

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

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

M = max(d)
D = d[:]
d.remove(M)
m = max(d)

for j in range(N):
    if D[j]==M:
        print(m)
    else:
        print(M)

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

B = sorted([(x, i) for i, x in enumerate(A)], reverse=True)

m0, mi = B[0]
m1, _ = B[1]


for i in range(N):
    if i != mi:
        print(m0)
    else:
        print(m1)

Solution (Python):
N = int(input())
A_list = [int(input()) for x in range(N)]

sorted_list = sorted(A_list, reverse = True)
max_1st = sorted_list[0]
max_2nd = sorted_list[1]

for num in A_list:
    if num != max_1st:
        print(max_1st)
    else:
        print(max_2nd)

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

x1 = sorted(s)[-1]
x2 = sorted(s)[-2]

if ( x1 == x2 ) :
    for val in s:
        print ( x1 )
else :
    for val in s:
        if ( val == x1 ):
            print ( x2 )
        else:
            print ( x1 )

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

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

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

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
LM=[0,A[0]]
a=A[::-1]
RM=[a[0]]
for i in range(1,N-1):
    LM.append(max(LM[-1],A[i]))
    RM.append(max(RM[-1],a[i]))
RM=RM[::-1]+[0]
for i in range(N):
    print(max(LM[i],RM[i]))

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

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

L = sorted(A)
mx1 = L[N-1]
mx2 = L[N-2]

for i in range(N):
    if A[i] == mx1:
        print(mx2)
    else:
        print(mx1)

Solution (Python):
N = int(input())
first, second, num = 0, 0, 0
l = []
for i in range(N):
	n = int(input())
	if n > first:
		first, second, num = n, first, i
	elif n > second:
		second = n
for i in range(N):
	if i == num:
		print(second)
	else:
		print(first)

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

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

mx = max(A)
mxi = A.index(max(A))

for i in range(N):
    if i != mxi:
        print(mx)
    elif i == 0:
        print(max(A[1:N]))
    elif i == N-1:
        print(max(A[0:N-1]))
    else:
        print(max(max(A[0:i]),max(A[i+1:N])))

Solution (Python):
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
Mod = 1000000007



def main(): 
    N = int(input())
    A = [int(input()) for i in range(N)]
    p = -float("inf")
    keepnumber = 0
    for i in range(N):
        if p <= A[i]:
            p = A[i]
            keepnumber = i
    for i in range(N):
        if i == keepnumber:
            print(max(A[:i]+A[i+1:]) if i != N else print(max(A[:N-1])))
        else:
            print(p)       
if __name__ == "__main__":
    main() 




def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

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

Solution (Python):
import math
import sys
minint = -sys.maxint - 1
maxint = sys.maxint

n = int(raw_input())
a = []
m = [0]*2
mx, mx2, ind, ind2 = minint, minint, 0, 0
for i in range(n):
  v = int(raw_input())
  if v > mx:
    mx, ind = v, i
  elif v > mx2:
    mx2, ind2 = v, i
  a += [v]
for i in range(n):
  if i == ind:
    print(mx2)
  else:
    print(mx)

Solution (Python):
import copy
N=int(input())
l=[]
for i in range(N):
  l.append(int(input()))
M=max(l)
L=copy.copy(l)
l.remove(M)
m=max(l)
for j in range(N):
  if L[j]==M:
    print(m)
  else:
    print(M)

Solution (Python):
import copy

def main():

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

    srtdA = copy.copy(A)
    srtdA.sort(reverse=True)
    atmax = srtdA[0]
    atsecond = srtdA[1]


    for  a in A:
        if a==atmax:
            print(atsecond)
        else:
            print(atmax)

if __name__== "__main__":
    main()

Solution (Python):
n = int(input())
an = []
for _ in range(n):
  an.append(int(input()))
maxl = [an[0]]
maxr = [an[-1]]
for a in an[1:]:
  maxl.append(max(maxl[-1], a))
for a in an[:-1][::-1]:
  maxr.append(max(maxr[-1], a))
maxr = maxr[::-1]

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

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

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



d_max = max(d_arr)
d_ind = d_arr.index(d_max)

for i in range(N):
    if i == d_ind:
        d_arr.pop(d_ind)
        print(max(d_arr))
    else:
        print(d_max)

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

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

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

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

n = int(input())
l = []
m1 = 0
m2 = 0
for i in range(n):
    num = int(input())
    l.append(num)
    if num > m1:
        m1 = num
    elif num > m2:
        m2 = num

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

Solution (Python):
n = int(input())
l = []
m1 = 0
m2 = 0
for i in range(n):
    num = int(input())
    l.append(num)
    if num > m1:
        m1 = num
    elif num > m2:
        m2 = num

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

Solution (Python):
n = int(input())
l = []
m1 = 0
m2 = 0
for i in range(n):
    num = int(input())
    l.append(num)
    if num > m1:
        m1 = num
    elif num > m2:
        m2 = num

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

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


r=0
count=0
for i in range(N):
    A[i]=int(input())
    if A[i]>r:
     r=A[i]
     count=i

for i in range(N):
    if i==count:
     del A[i]
     print(max(A))
    else:
     print(r)

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

Solution (Python):
import copy
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
ma =max(a)
cn =0
for i in range(n):
  if a[i]==ma and cn<2:
    b=copy.deepcopy(a)
    b[i]=0
    print(max(b))
    cn +=1
  else:
    print(ma)

Solution (Python):
import sys
import math

if sys.platform =='ios':
  sys.stdin=open('input134c.txt')



n = int(input())
a = []
max=0
max2=0
for i in range(n):
	ai = int(input())
	a.append(ai)
	if max < ai:
		max2 = max
		max = ai
	elif max2 < ai:
		max2 = ai			



for j in a:
	if j == max:
		print(max2)
	else:
		print(max)

Solution (Python):
N=int(input())
L=[]
for _ in range(N):
  L.append(int(input()))
sL=sorted(L)
sL.reverse()
m=max(L)
m2=sL[1]

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

Solution (Python):

import sys
import numpy as np

def solve(N: int, A: "List[int]"):
    maxA = 0
    maxInd = 0
    for i in range(N):
        if A[i] > maxA:
            maxA = A[i]
            maxInd = i
    A[maxInd] = 0
    subMaxA = max(A)
    for i in range(N):
        if i == maxInd:
            A[i] = subMaxA
        else:
            A[i] = maxA
    print('\n'.join(map(str, A)))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, A)

if __name__ == '__main__':
    main()

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

f = 0
s = 0
index = 0
for i in range(N):
  a = int(input())
  if a > f:
    index = i
    s = f
    f = a
  elif a > s:
    s = a

for i in range(N):
  if i == index:
    print(s)
  else:
    print(f)

Solution (Python):




















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

ma_index=a.index(ma)
for i in range(n):
    if i==ma_index:
        
        print(max(a[:i]+a[i+1:]))
    else:
        print(ma)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]

max_i = 0
max_num = 0
sec_num = 0
for i in range(n):
    if a[i] > max_num:
        max_num = a[i]
        max_i = i

a.sort(reverse=True)

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

Solution (Python):
n = int(input())
fa = 0
sa = 0
fi = 0
si = 0
for i in range(n):
    a = int(input())
    if a > fa:
        sa, si = fa, fi
        fa = a
        fi = i
    elif a > sa:
        sa = a
        si = i

for i in range(n):
    if i != fi:
        print(fa)
    else:
        print(sa)

Solution (Python):
N = int(input())
A_s = [int(input()) for _ in range(N)]
max_a = max(A_s)
is_double_max = False
for i in range(N):
  if A_s[i] != max_a or is_double_max:
      print(max_a)
  else:
      A_s.pop(i)
      if max(A_s) == max_a:
          is_double_max = True
      print(max(A_s))
      A_s.insert(i,max_a)

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

sA = sorted(A)
x, y = sA[-1], sA[-2]

ix = A.index(x)
for i in range(N):
    if i == ix:
        print(y)
    else:
        print(x)

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

Solution (Python):
def main():
    n = int(input())
    a = []
    for i in range(n):
        a.append(int(input()))
    a_max = max(a)
    ind_M = a.index(a_max)
    tmpl = a
    tmpl.pop(ind_M)
    for i in range(n):
        if i != ind_M:
            print(a_max)
        else:
            print(max(tmpl))


if __name__ == "__main__":
    main()

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

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

Solution (Python):
n=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
a=sorted(l)
a.reverse()
for v in l:
    if v==a[0]:
        print(a[1])
    else:
        print(a[0])

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

A = []

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

maxa = max(A)

copy = A[:]

copy.remove(maxa)

maxa2 = max(copy)

for i in A:
    if i == maxa:
        print(maxa2)
    else:
        print(maxa)

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

L_max=max(L)

L_max_count=0
for l in L:
    if l==L_max:
        L_max_count+=1
    if L_max_count > 1:
        break

for i in range(N):
    now=L[i]
    
    if L_max_count > 1:
        ans=L_max
    
    elif now!=L_max:
        ans=L_max
        
    else:
        L_temp=L.copy()
        L_temp_p=L_temp.pop(i)
        ans=max(L_temp)
    
    print(ans)

Solution (Python):
n=int(input())
arr=[]
for i in range(n):
    a = int(input())
    arr.append( (a,i) )
brr = sorted(arr)
max_num = brr[-1][0]
max_ind = brr[-1][1]
second_num = brr[-2][0]

for i in range(n):
    if i==max_ind:
        print(second_num)
    else:
        print(max_num)

Solution (Python):
n = int(input())
 
a = [int(input()) for i in range(n)]
ma1 = max(a)
ma2 = a.count(ma1)
 
for i in range(n):
    if a[i] == ma1:
        if ma2 == 1:
            a[i] = 1
            print(max(a))
            a[i] = ma1
        else:
            print(ma1)
    else:
        print(ma1)

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

a = [int(input()) for i in range(n)]
ma1 = max(a)
ma2 = a.count(ma1)

for i in range(n):
    if a[i] == ma1:
        if ma2 == 1:
            a[i] = 1
            print(max(a))
            a[i] = ma1
        else:
            print(ma1)
    else:
        print(ma1)

Solution (Python):

import collections

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

ys = sorted(xs)
mx1 = ys[-1]
mx2 = ys[-2]

for x in xs:
    if x == mx1:
        print(mx2)
    else:
        print(mx1)

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

Solution (Python):
N = int(input())
A = [0]*N
for i in range(N):
    A[i] = int(input())
AA = sorted(A, reverse=True)
no1 = AA[0]
no2 = AA[1]
for i in A:
    if i == no1:
        print(no2)
    else:
        print(no1)

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

AS=sorted(A, reverse=True)

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

Solution (Python):


N = int(input())
t1 = 0
t2 = 0
num1 = 0
num2 = 0
for i in range(N):
    x = int(input())
    if t1 < x:
        t2 = t1
        num2 = num1
        t1 = x
        num1 = i
    else:
        if t2 < x:
            t2 = x
            num2 = i
for j in range(0, num1):
    print(t1)
print(t2)
for k in range(num1+1, N):
    print(t1)

Solution (Python):
import copy
n=int(input())
a=[0]*n
for i in range(n):
  a[i]=int(input())
b=copy.deepcopy(a)
m1=max(a)
a.remove(m1)
m2=max(a)
for i in range(n):
  if b[i]==m1:
    print(m2)
  else:
    print(m1)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
_A = sorted(A)
_max = _A[-1]
_max2 = _A[-2]

for a in A:
    if a == _max:
        print(_max2)
    else:
        print(_max)

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

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


k = max(range(N), key=lambda i: A[i])
ans = '\n'.join(
    str(
        max(A[:k] + A[k + 1:]) if i == k else
        A[k]
    )
    for i in range(N)
)

print(ans)

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

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

sort = sorted(A)

max = sort[-1]
maxx = sort[-2]

for i in range(n):
  if max == A[i]:
    print(maxx)
  else:
    print(max)

Solution (Python):
n = int(input())
ls = []
for i in range(n):
    ls.append(int(input()))
maxnum = max(ls)
ind = ls.index(maxnum)
ls.remove(maxnum)
secondnum = max(ls)
ls = ls[0:ind] + [maxnum] + ls[ind:]

if maxnum == secondnum:
    for i in range(n):
        print(maxnum)
    quit()
for i in range(n):
    if ls[i] == maxnum:
        print(secondnum)
    else:
        print(maxnum)

Solution (Python):
from collections import defaultdict
N = int(input())
A = []
a_dict = defaultdict(int)
for _ in range(N):
    a = int(input())
    A.append(a)
    a_dict[a] += 1

max_a = max(A)
for i, a in enumerate(A):
    if a == max_a and a_dict[a] == 1:
        B = []
        for j, b in enumerate(A):
            if b != a:
                B.append(b)
        ans = max(B)
        print(ans)
    else:
        print(max_a)

Solution (Python):
n = int(input())
s = []
second = 0
max_k = 0
for _ in range(n):
    k = int(input())
    max_k, min_k = max(k, max_k), min(k, max_k)
    second = max(min_k, second)
    s.append(k)
kk = s.index(max_k)
for i in range(n):
    if i != kk:
        print(max_k)
    else:
        print(second)

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

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

Solution (Python):
N = int(input())
max = 0
max2 = 0
n_max = 0
A = []
for _ in range(N):
  a = int(input())
  A.append(a)
  if a > max:
    max2 = max
    max = a
    n_max = 1
  elif a == max:
    n_max += 1
  elif a > max2:
    max2 = a

for a in A:
  if a == max:
    if n_max == 1:
      print(max2)
    else:
      print(max)
  else:
    print(max)

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

b = [0]*n
c = [0]*n
b[0] = a[0]
c[n-1] = a[n-1]
for i in range(1,n):
    b[i]=max(b[i-1],a[i])
    c[n-1-i]=max(c[n-i],a[n-1-i])

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

Solution (Python):
N  = int(input())
A_list = []
primary_max = 0
secondary_max = 0

for i in range(N) :
    n = int(input())
    if (primary_max <= n) :
        if (primary_max == secondary_max) :
            primary_max = n
        else :
            secondary_max = primary_max
            primary_max = n
    elif (secondary_max < n and n < primary_max) :
        secondary_max = n

    A_list.append(n)

for a in A_list :
    if (a == primary_max) :
        print(secondary_max)
    else :
        print(primary_max)

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

Solution (Python):


N = int(input())

values = [0]*N
max_value_count = 0
max_value = 0
next_max_value = max_value

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

    if in_value == max_value:
        max_value_count = max_value_count + 1
    elif in_value > max_value:
        next_max_value = max_value
        max_value = in_value
        max_value_count = 1
    elif in_value > next_max_value:
        next_max_value = in_value

for row in range(N):
    if values[row] == max_value and max_value_count == 1:
        print(next_max_value)
    else:
        print(max_value)

Solution (Python):


N = int(input())

values = [0]*N
value_count_map = {}
max_value = 0
next_max_value = max_value

for i in range(N):
    in_value = int(input())
    values[i] = in_value
    value_count = 0
    if in_value in value_count_map:
        value_count = value_count_map[in_value]
    value_count_map[in_value] = value_count + 1

    if in_value > max_value:
        next_max_value = max_value
        max_value = in_value
    elif in_value > next_max_value:
        next_max_value = in_value

max_value_count = value_count_map[max_value]

for row in range(N):
    if values[row] == max_value and max_value_count == 1:
        print(next_max_value)
    else:
        print(max_value)