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

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
max_a = max(a)
max_idx = a.index(max_a)
for i in range(n):
    if i == max_idx:
        tmp = a
        tmp.sort(reverse=True)
        print(tmp[1])
    else:
        print(max_a)

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

for i in A:
  if i==A_[0]:
    print(A_[1])
  else:
    print(A_[0])

Solution (Python):


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

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

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

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

big1=list2[-1]
big2=list2[-2]
for i in range(n):
    if big1==list1[i]:
        print(big2)
    else:
        print(big1)

Solution (Python):
n=int(input())
list1=[0 for i in range(2)]
list2=[0]*2
count=1
for i in range(n):
    a=int(input())
    if list1[0]<=a:
        list1[1]=list1[0]
        list1[0]=a
        list2[1]=list2[0]
        list2[0]=count
    elif list1[1]<=a:
        list1[1]=a
        list2[1]=count
    count+=1


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

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

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
M = max(A)
Mn = A.count(M)
if Mn == N:
    m = 0
else:
    m = max([i for i in A if i!=M])

for i in range(N):
    if Mn >= 2:
        print(M)
    else:
        if A[i] == M:
            print(m)
        else:
            print(M)

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

B = list(reversed(list(sorted(A))))
maxA = max(A)

for e in A:
    if e == maxA:
        print(B[1])
    else:
        print(B[0])

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

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

Solution (Python):
def main():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [int(input().rstrip('\n')) for _ in range(n)]
    b = sorted(a, reverse=True)
    for i in a:
        if i == b[0]:
            print(b[1])
        else:
            print(b[0])
            
            
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
maxA = max(A)
if A.count(maxA) == 1:
  maxA_2 = max([i for i in A if i != maxA])
  for i in range(n):
    if A[i] != maxA:
      print(maxA)
    else:
      print(maxA_2)
else:
  for i in range(n):
    print(maxA)

Solution (Python):
import copy

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

m_1 = sorted(a)[-1]
m_2 = sorted(a)[-2]

for i in range(n):
    if a[i] == m_1:
        print(m_2)
    else:
        print(m_1)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [I() 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(input())
a=[int(input()) for i in range(n)]
ma=max(a)
aa=sorted(a,reverse=True)
for i in a:
    if i !=ma:
        print(ma)
    else:
        print(aa[1])

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

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

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

global_max = max(a)
count = 0
for i in range(n):
    if a[i] == global_max:
        count += 1
    if count == 2:
        break

for i in range(n):
    if a[i] == global_max:
        if count == 1:
            print(max(a[:i] + a[i + 1:]))
        else:
            print(global_max)
    else:
        print(global_max)

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()
li = []
for i in range(n):
    li.append(ni())
sli = sorted(li)
for i in range(n):
    if sli[-1] != li[i]:
        print(sli[-1])
    else:
        print(sli[-2])

Solution (Python):
n = int(input())
arr = [int(input()) for _ in range(n)]
sorted_arr = sorted(arr, reverse=True)
result = []

for a in arr:
  multi_same_num_flag = False
  for m in sorted_arr:
    if m > a:
      result.append(str(m))
      break
    elif m == a:
      if not multi_same_num_flag:
        multi_same_num_flag = True
        continue
      else:
        result.append(str(m))
        break
    elif m < a:
      result.append(str(m))
      break


print('{}'.format('\n'.join(result)))

Solution (Python):
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)


n = int(input())
a_list = [int(input()) for i in range(n)]


argmax = np.argmax(a_list)
max_value = np.max(a_list)
second_max_value = np.max(a_list[:argmax] + a_list[argmax+1:])

for i in range(n):
    if i == argmax:
        print(second_max_value)
    else:
        print(max_value)

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

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

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
li = list(int(stdin.readline().rstrip()) for _ in range(n))
li_to = sorted(li)
one = li_to[-1]
two = li_to[-2]
for i in range(n):
    if one == li[i]:
        print(two)
    else:
        print(one)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
a_m = max(a)
a_i = a.index(a_m)
a.sort()
for i in range(len(a)):
  if i == a_i:
    print(a[-2])
  else:
    print(a_m)

Solution (Python):

N = int(input())

A = []

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

maxA=max(A)
maxAi=A.index(maxA)

A[maxAi] = 0
maxA2 = max(A)

for i in range(N):
    if i == maxAi :
        print(maxA2)
    else :
        print(maxA)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
a_m = max(a)
a_i = a.index(a_m)
a.sort()
for i in range(len(a)):
  if i == a_i:
    print(a[-2])
  else:
    print(a_m)

Solution (Python):
N=int(input())
l=[]
for i in range(N):
  l.append(int(input()))
l_max=max(l)
i=l.index(l_max)
if i==0:
  l1=l[1:]
  print(max(l1))
  for a in range(N-1):
    print(l_max)
elif i==N:
  l1=l[:N]
  for a in range(N-1):
    print(l_max)
  print(max(l1))
else:
  l1=l[:i]+l[i+1:]
  for a in range(i):
    print(l_max)
  print(max(l1))
  for a in range(N-i-1):
    print(l_max)

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

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

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
    
A_sorted = sorted(A)
A_max1 = A_sorted[-1]
A_max2 = A_sorted[-2]

if A_max1 == A_max2:
    for i in range(N):
        print(A_max1)
else:
    max_idx = A.index(A_max1)
    for i in range(N):
        if i==max_idx:
            print(A_max2)
        else:
            print(A_max1)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LIN(N)
    A_ = sorted(A,reverse = True)
    A_1 = A_[0]
    A_2 = A_[1]
    for a in A:
        if a == A_1:
            print(A_2)
        else:
            print(A_1)
    



if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
A = sorted([(int(input()), i) for i in range(n)],
           key=lambda a: -a[0])
for i in range(n):
    j = 0
    while A[j][1] == i:
        j += 1
    print(A[j][0])

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

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

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

maxa = 0
maxa1 = 0
maxcnt = 0
for i in range(n):
    ai = int(input())
    a.append(ai)
    if maxa < ai:
        maxa1 = maxa
        maxa = ai
        maxcnt = 0
    elif maxa == ai:
        maxcnt += 1
    elif maxa1 < ai:
        maxa1 = ai

for i in range(n):
    if a[i] == maxa:
        if maxcnt > 0:
            print(maxa)
        else:
            print(maxa1)
    else:
        print(maxa)

Solution (Python):
n = int(input())
nums = [int(input()) for _ in range(n)]
sorted_nums = sorted(nums)
for num in nums:
    if num < sorted_nums[-1]:
        print(sorted_nums[-1])
    else:
        print(sorted_nums[-2])

Solution (Python):
my_max=0
my_sec=0
cnt=0
n=int(input())
a=[0]*n
r_n =range(n)
for i in r_n:
  a[i]=int(input())
  if my_max<a[i]:
    my_max=a[i]
  elif my_sec<a[i]:
    my_sec=a[i]
    
for i in r_n:
  if my_max==a[i]:
    cnt+=1
    
for i in r_n:
  if my_sec!=0:
    if my_max!=a[i]:
      print(my_max)
    elif cnt>1:
      print(my_max)
    else:
      print(my_sec)
  else:
    print(my_max)

Solution (Python):
my_max=0
my_sec=0
cnt=0
n=int(input())
a=[0]*n
r_n =range(n)
for i in r_n:
  a[i]=int(input())
  if my_max<a[i]:
    my_max=a[i]
  elif my_sec<a[i]:
    my_sec=a[i]
    
for i in r_n:
  if my_max==a[i]:
    cnt+=1
    
for i in r_n:
  if my_sec!=0:
    if my_max!=a[i]:
      print(my_max)
    elif cnt>1:
      print(my_max)
    else:
      print(my_sec)
  else:
    print(my_max)

Solution (Python):


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

a_max = max(a)
max_idx = a.index(a_max)
c = a.count(a_max)

for i in range(n):
  if i!=max_idx:
    print(a_max)
  elif i==max_idx and c>=2:
    print(a_max)
  else:
    a.remove(a[i])
    print(max(a))

Solution (Python):
import math
import copy
import sys
import numpy as np



def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList





n = getInputNum()

a = getSomeInputInt(n)

sorted_a= sorted(a, reverse=True)

maxNum = sorted_a[0]
second = sorted_a[1]

for i in range(n):
    if a[i] == maxNum:
        print(second)
    else:
        print(maxNum)

Solution (Python):
r=[int(input()) for _ in range(int(input()))]
maxValue=max(r)

r2=r[:]
r2.remove(maxValue)
nextValue=max(r2)

for item in r:
  print(maxValue if item!=maxValue else nextValue)

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

Solution (Python):


n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a,reverse=True)

p = a.index(max(a))

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

Solution (Python):
import copy

N = int(input())

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

m = max(A)
flag = 1
for n in range(N):
    if flag == 1 and A[n] == m:
        t = copy.deepcopy(A)
        del t[n]
        print(max(t))
        flag = 0
        continue

    print(m)

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

k = [i for i, x in enumerate(a) if x == m]

if len(k)>=2:
	for i in a:
		print(m)
else:
	b = []
	b += a
	mi = b.index(m)
	b.pop(mi) 
	m2 = max(b)
	for i in a:
		if i != m:
			print(m)
		else:
			print(m2)

Solution (Python):
import copy

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

chinpo = max(A)
B = copy.copy(A)
B.remove(max(A))
chinchin = max(B)

for i in range(A.index(max(A))):
    print(chinpo)
print(chinchin)
for i in range(A.index(max(A))+1,N):
    print(chinpo)

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

for i in range(N):
  i = int(input())
  A.append(i)
  
ma = max(A)
sa = sorted(A)

for k in range(N):
  if ma != A[k]:
    print(ma)
  else:
    print(sa[-2])

Solution (Python):
n = int(input())
arr = []
queue = []
from heapq import heappush, heappop
for i in range(n):
    a = int(input())
    heappush(queue, -a)
    arr.append(a)
first = -heappop(queue)
second = -heappop(queue)
for a in arr:
    print(first if a != first else second)

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

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

SA = sorted(A)
mx = max(A)

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

Solution (Python):
import sys
n = int(input())
a = list(map(int, (input() for i in range(n))))

max = max(a)
max2 = sorted(a)[-2]

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

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

Solution (Python):
n=int(input())
A=[int(input())for _ in range(n)]
M=max(A)
m=sorted(A)[-2]
for i in range(n):
    a=A[i]
    if a<M:
        print(M)
    else:
        print(m)

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

Solution (Python):
N =int(input())
A = [int(input()) for i in range(N)]
mx1=max(A)
mx1p = A.index(max(A))
Awomax=A
Awomax[mx1p]=0
mx2=max(Awomax)
ans = [mx1]*N
ans[mx1p] = mx2
for i in range(N):
    print(ans[i])

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

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

Solution (Python):
from copy import deepcopy

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

B = deepcopy(A)
B.sort(reverse=1)
max_a = B[0]
max_b = B[1]

if A.count(max_a) == 1:
	for a in A:
		if a == max_a:
			print(max_b)
		else:
			print(max_a)
else:
	for a in A:
		print(max_a)

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

c = b.index(max(b))
b.sort(reverse=True)

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

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

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

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

Solution (Python):
n = int(input())
lst = [int(input()) for _ in range(n)]
sorted_lst = sorted(lst)
x,y = sorted_lst[-1],sorted_lst[-2]
for i in range(n):
    if lst[i] == x:
        print(y)
    else:
        print(x)

Solution (Python):
import copy
n = int(input())
a_list = [ int(input()) for _ in range(n)]
max_val = max(a_list)
index = [a_list.index(max_val)]
tmp_list = copy.copy(a_list)
for i, a in enumerate(a_list):
  if 2 <= len(index):
    print(max_val)
  elif i in index:
    tmp_list.pop(i)
    print(max(tmp_list))
    tmp_list = copy.copy(a_list)
  else:
    print(max_val)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
max_A = max(A)
max_num = 0
for i in range(N):
    if A[i] == max_A:
        
        max_num += 1
        if max_num > 1:
            print(max_A)
        else:
            print(max(A[:i] + A[i + 1:]))
    else:
        print(max_A)

Solution (Python):
n = int(input())
a_lst = [0]*n
a_lst[0] = int(input())
a_lst[1] = int(input())
if a_lst[0] >= a_lst[1]:
    max = 0
    sec_max = 1
else:
    max = 1
    sec_max = 0

for i in range(2, n):
    a_lst[i] = int(input())
    if a_lst[i] > a_lst[max]:
        max = i
    elif a_lst[i] > a_lst[sec_max]:
            sec_max = i

for i in range(n):
    if i == max:
        print(a_lst[sec_max])
    else:
        print(a_lst[max])

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
max_A = max(A)
max_num = 0
for i in range(N):
    if A[i] == max_A:
        
        max_num += 1
        if max_num > 1:
            print(max_A)
        else:
            print(max(A[:i] + A[i + 1:]))
    else:
        print(max_A)

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

newList = sorted(A,reverse=True)

for i in A:
    if newList[0] == i:
        print(newList[1])
    else:
        print(newList[0])

Solution (Python):
import math


def solve():
    n = int(input())
    al = [int(input()) for _ in range(n)]
    l = al[:]
    l.sort()
    if len(l) == 1:
        f, s = l[0], l[0]
    else:
        f, s = l[-1], l[-2]
    for a in al:
        if a == f:
            print(s)
        else:
            print(f)


if __name__ == "__main__":
    solve()

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

max_1,max_2 = sorted(Alist,reverse=True)[:2]
for A in Alist:
    if A==max_1:
        print(max_2)
    else:
        print(max_1)

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

s=sorted(listA,reverse=True)[:2]
for i in listA:
    if s[0]==i:
        print(s[1])
    else:
        print(s[0])

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

L = [0] * (200000+1)
R = [0] * (200000+1)

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

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




for i in range(N):
    ans = max(L[i], R[i+1])
    print(ans)

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

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

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

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
tmp = 0
for i in range(N):
    if A[i] > A[tmp]:
        tmp = i
maxA = A[tmp]
answer = [maxA]*N
B = sorted(A)
A[tmp] = -float("inf")
C = sorted(A)
answer[tmp] = C[-1]

print(*answer, sep="\n")

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

n = int(input())
l = []
for _ in range(n):
	l.append(int(input()))
p = sorted(l)
fm = p[-1]
sm = p[-2]
if fm == sm:
	for _ in range(n):
		print(fm)
else:
	for i in range(n):
		if l[i] == fm:
			print(sm)
		else:
			print(fm)

Solution (Python):
import copy
N = int(input())
A = list()
for i in range(N):
    A.append(int(input()))
s = sorted(A)[::-1]
m1 = s[0]
m2 = s[1]

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

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

max_value = max(A)
number = A.count(max_value)

if number > 1:
    for j in range(N):
        print(max_value)
else:
    position = A.index(max_value)
    A.remove(max_value)
    for j in range(N):
        if j == position:
            print(max(A))
        else:
            print(max_value)

Solution (Python):
N = int(input())
L = []
for i in range(N):
  k = int(input())
  L.append([k,i])
L.sort()
for i in range(N):
  if i == L[N-1][1]:
    print(L[N-2][0])
  else:
    print(L[N-1][0])

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

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
p = A.index(max(A))
m1, m2 = sorted(A, reverse=True)[:2]

for i in range(N):
    if i == p:
        print(m2)
    else:
        print(m1)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
p = A.index(max(A))
A.sort(reverse=True)
m1, m2 = A[0], A[1]

for i in range(N):
    if i == p:
        print(m2)
    else:
        print(m1)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
m = max(A)
p = A.index(m)
A.remove(m)
n = max(A)

for i in range(N):
    if i == p:
        print(n)
    else:
        print(m)

Solution (Python):
n = int(input())
a = []
a1 = a2 = 0
for i in range(n):
    ai = int(input())
    a.append(ai)
    if ai > a1:
        a1, a2 = ai, a1
    elif ai > a2:
        a2 = ai

for i in range(n):
    if a[i] == a1:
        print(a2)
    else:
        print(a1)

Solution (Python):
import sys
from collections import deque
import copy

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    A = list(map(int, inputs))
    B = copy.copy(A)
    B.sort()
    first_num = B[-1]
    second_num = B[-2]
    ret = ''
    for a in A:
        if a == first_num:
            ret += str(second_num) + '\n'
        else:
            ret += str(first_num) + '\n'
    return ret.strip()


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

Solution (Python):
n = int(input())
t = [int(input()) for i in range(n)] 
m = max(t)
m2 = sorted(t)[-2]
for i in t:
  if i != m:
    print(m)
  else:
    print(m2)

Solution (Python):
N = int(input())
max_num = (0,0) 
max_candi = (0,0) 
for i in range(N):
    A = int(input())
    if max_candi[0] < A and max_num[0] < A:
        max_candi = max_num
        max_num = (A,i)
    elif max_candi[0] < A:
        max_candi = (A,i)

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

Solution (Python):
import numpy as np

N=int(input())
A=[]
for i in range(N):
  A.append(int(input()))
  
m1=np.max(A)
m1i=np.argmax(A)

A.remove(m1)
m2=np.max(A)

for i in range(N):
  if i!=m1i:
    print(m1)
  else:
    print(m2)

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

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

reverse_A = sorted(A, reverse=True)
max1 = reverse_A[0]
max2 = reverse_A[1]

for a in A:
  if a == max1:
    print(max2)
  else:
    print(max1)

Solution (Python):
N = int(input())
num_list = [int(input()) for i in range(N)]
max_num = max(num_list)
max_num_num = num_list.count(max_num)

for cnt in range(N):
  if num_list[cnt] == max_num and max_num_num == 1:
    print(max(num_list[0:cnt] + num_list[cnt+1:]))
  else:
    print(max_num)

Solution (Python):
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
b = a[:]
mx = max(b)
b.remove(mx)
mx2 = max(b)

for t in a:
    if t == mx:
        print(mx2)
    else:
        print(mx)

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

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

pre_max = 0
pre_max_2 = 0
forward = [0]
backward = [0]
for ii in range(1, N):
    pre_max = max(pre_max, A[ii-1])
    forward.append(pre_max)
    pre_max_2 = max(pre_max_2, A[-ii])
    backward.append(pre_max_2)

backward.reverse()

for ii in range(N):
    print(max(forward[ii], backward[ii]))

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

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

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

Solution (Python):
N = int(input())
A_lis = [int(input()) for i in range(N)]
A_lis_sort = sorted(enumerate(A_lis),key = lambda x:x[1])
max_index = A_lis_sort[-1][0] 
for i in range(N):
  if i == max_index:
    print(A_lis_sort[-2][1])
  else:
    print(A_lis_sort[-1][1])

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

k = max(A)
if A.count(k) >= 2:
    for i in range(n):
        print(k)
else:
    for i in range(n):
        if A[i] < k:
            print(k)
        else:
            A[i] = 0
            print(max(A))

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

nums = [int(input()) for x in range(N)]

a = sorted(nums)[-1]
b = sorted(nums)[-2]



if a == b:
    for _ in nums:
        print(a)
else:
    for x in [b if x == a else a for x in nums]:
        print(x)

Solution (Python):
import sys
import copy
def input():
	return sys.stdin.readline().rstrip()
def main():
	N = int(input())
	A = [int(input()) for _ in range(N)]
	B = list(set(A))
	if (len(B) == 1) or (A.count(B[-1]) > 1):
		for i in range(N):
			print(B[-1])
	else :
		for i in range(N):
			if A[i] == B[-1]:
				print(B[-2])
			else:
				print(B[-1])
if __name__ == '__main__':
	main()

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


no1_value = 0
no2_value = 0
for n in range(N):
  a = int(input())
  if a > no1_value:
    no1_value = a
    no1_key = n
  elif a > no2_value:
    no2_value = a
    no2_key = n



for n in range(N):
  if n == no1_key:
    print(no2_value)
  else:
    print(no1_value)

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

max_val = max(A)
count = A.count(max_val)

subset = list(set(A))
if len(subset) > 1:
  subset.remove(max_val)
  max_val_2 = max(subset)

for i, num in enumerate(A):
  if num < max_val:
    print(max_val)
  else:
    if count > 1:
      print(max_val)
    else:
      print(max_val_2)

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

l=[]
mydic={}
for i in range(n) :
  s=int(input())
  l.append(s)
  if not s in mydic.keys() :
    mydic[s]=1
  else :
    mydic[s]=mydic[s]+1
l2=sorted(l,reverse=True)


mx=l2[0]
for i in range(n) :
  if l[i]==mx :
    if mydic[mx]==1 :
      print(l2[1])
    else:
      print(mx)
  else :
    print(mx)

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

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

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

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

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

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

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

x = b[0]
y = b[1]

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