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)]
sorted_A = sorted(A)
for i in A:
    if i == sorted_A[-1]:
        print(sorted_A[-2])
    else:
        print(sorted_A[-1])

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

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

a_sort = sorted(a)

a_max = a_sort[-1]
a_second = a_sort[-2]

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

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

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


MAX = max(A)

if A.count(MAX) != 1:
  for _ in range(N):
    print(MAX)

  
else:

  two = sorted(set(A))[-2]
  for i in range(N):
    if A[i] == MAX :
      print(two)
    else:
      print(MAX)

Solution (Python):
n = int(input())
a = [0] * n
mx = 0
mx_i = 0
mx2 = 0
for i in range(n):
    aa = int(input())
    a[i] = aa
    if mx < aa:
        mx = aa
        mx_i = i
a[mx_i] = 0
mx2 = max(a)
a[mx_i] = mx

for i, aa in enumerate(a):
    if aa == mx:
        print(mx2)
    else:
        print(mx)

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

    n=int(input())
    A=[int(input())for _ in range(n)]
    a_max1=max(A)
    A1=A[:]
    A1.remove(a_max1)
    a_max2=max(A1)
    for i in range(n):
        if A[i]!=a_max1:print(a_max1)
        else:print(a_max2)

if __name__=='__main__':
    main()

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
maxA = max(A)
copy_A = A[:]
copy_A.remove(maxA)
secondA = max(copy_A)
for x in A:
    if x == maxA:
        print(secondA)
    else:
        print(maxA)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
f = max(A)
if A.count(f) >=2:
    for i in range(N):
        print(f)
else:
    M = A.index(f)
    del A[M]
    for i in range(M):
        print(f)
    print(max(A))
    for i in range(M+1,N):
        print(f)

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

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

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


n0 = int(input())
nm1, nm2 = n0, 0
Ind1, Ind2 = [0], []

i = 1
while i < n:
    n0 = int(input())
    if n0 > nm1: 
        nm2 = nm1
        nm1 = n0
        Ind2 = Ind1
        Ind1 = [i]
    elif n0 == nm1:
        Ind1.append(i)
    elif n0 > nm2: 
        nm2 = n0
        Ind2 = [i]
    elif n0 == nm2: 
        Ind2.append(i)
    i += 1
if len(Ind1) >= 2: 
    ans = (str(nm1) + '\n') * n
else: 
    i0 = Ind1[0]
    ans = (str(nm1) + '\n') * i0
    ans += str(nm2) + '\n'
    ans += (str(nm1) + '\n') * (n-1-i0)
print(ans)

Solution (Python):
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
s=sorted(a,reverse=True)
M1=s[0]
M2=s[1]

if M1==M2:
  for i in range(n):
    
    print(M1)
else:
  key=a.index(M1)
  for i in range(n):
    if i==key:
      print(M2)
    else:
      print(M1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
m1 = B[-1]
m2 = B[-2]
ANS = [m1 if x != m1 else m2 for x in A]
print('\n'.join(map(str,ANS)))

Solution (Python):
n = int(input())
num = []
maxnum = 0
count = 1
for i in range(n):
  a = int(input())
  if a>maxnum:
    maxnum = max(maxnum,a)
    count = 1
  elif a==maxnum:
    count += 1
  num.append(a)
  
for i in range(n):
  if count>1:
    print(maxnum)
  else:
    if num[i]!=maxnum:
      print(maxnum)
    else:
      subnum = num[0:i] + num[i+1:n]
      subnum = sorted(subnum)
      print(subnum[n-2])

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
first = max(A)
second = sorted(A)[-2]
index = A.index(first)
for i in range(N):
    print(first if i != index else second)

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

M1 = max(A)
M1idx = A.index(M1)
M2 = max(A[:M1idx]+A[M1idx+1:])
for i in range(N):
  if A[i] != M1:
    print(M1)
  else:
    print(M2)

Solution (Python):
N=int(input())
a=[]
for i in range(N):
	a.append(int(input()))
k=max(a)
count=0
for j in range(N):
	if count==1:
		print(k)
	else:
		if a[j]==k:
			b=a[:j]+a[j+1:]
			count=1
			print(max(b))
		else:
			print(k)

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

L = sorted(L)

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

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

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

Solution (Python):
num_list = [] 
n = int(input())
for i in range(n):
    num_list.append(int(input()))
tmp = num_list.copy()
tmp.sort()
tmp = tmp[::-1]

if tmp[0] == tmp[1]:
    num = tmp[0]
    for i in range(n):
        print(num)
        
else:
    for i in range(n):
        num = tmp[0]
        if num_list[i] != tmp[0]:
            print(tmp[0])
            
        else:
            print(tmp[1])

Solution (Python):
import bisect

n = int(input())

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

a2 = sorted(a)

max_a = a2[-1]
index = bisect.bisect_left(a2,max_a)
num = n-index

if num > 1:
  for _ in range(n):
    print(a2[-1])
else:
  for i in range(n):
    if a[i] == a2[-1]:
      print(a2[-2])
    else:
      print(a2[-1])

Solution (Python):
import sys
input = sys.stdin.readline
import math
import collections
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n=I()
nums=[]
for i in range(n):
    nums.append(int(input()))
nms=sorted(nums,reverse=True)
for i in range(n):
    if nums[i]==nms[0]:
        print(nms[1])
    else:
        print(nms[0])

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

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 = [list(map(int, input().split())) for _ in range(n)]
a1 = []
b = []
for s in a:
    a1.extend(s)
b = sorted(a1)

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

Solution (Python):
n = int(input())
ls=[int(input()) for i in range(n)]
maxn = max(ls)
ans=[maxn]*n
nma = ls.index(maxn)
ls2=ls.copy()
ls2.remove(maxn) 
maxcel= max(ls2)
del ans[nma]
ans.insert(nma,maxcel)
a='\n'.join(map(str,ans))
print(a)

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = []
for i in range(n):
    A.append(ii())

amax = max(A)
anmax = sorted(A)[-2]
cntmax = A.count(amax)

if cntmax > 1:
    for j in A:
        print(amax)
else:
    for k in A:
        if k == amax:
            print(anmax)
        else:
            print(amax)

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

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

sorted_arr = sorted(arr)
maximum = sorted_arr[-1]
for i in range(n):
    if arr[i] == maximum:
        print(sorted_arr[-2])
    else:
        print(sorted_arr[-1])

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

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

B = sorted(A)
M, m = B[N-1], B[N-2]

for i in A:
    if i == M:
        print(m)
    else:
        print(M)

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

l = 0
L = [l]
for a in A:
    l = max(l, a)
    L.append(l)

r = 0
R = [r]
for a in A[::-1]:
    r = max(r, a)
    R.append(r)
R.reverse()

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

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

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

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

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

Solution (Python):
n = int(input())
mx = 0
smx = 0
mxi = 0
for i in range(n):
  ai = int(input())
  if ai > mx:
    smx = mx
    mx = ai
    mxi = i
  elif ai > smx:
    smx = ai
for j in range(mxi):
  print(mx)
print(smx)
for j in range(mxi+1,n):
  print(mx)

Solution (Python):
def q3():
    """ C - Exception Handling """
    N = int(input())
    A = []
    for n in range(N):
        A.append(int(input()))

    max_A = max(A)
    second_A = sorted(A, reverse=True)[1]
    for n in range(N):
        if A[n] == max_A:
            print(second_A)
        else:
            print(max_A)

            
if __name__ == '__main__':
    q3()

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

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

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

Solution (Python):
import bisect
n = int(input())
A = [int(input()) for i in range(n)]
A_sd = sorted(A)
for a in A:
  if bisect.bisect_left(A_sd, a) == n - 1:
    print(A_sd[-2])
  else:
    print(A_sd[-1])

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
mx = max(a)
mxi = a.index(mx)
mx2 = max(a[:mxi] + a[mxi+1:])

for i in range(n):
    if i == mxi: print(mx2) 
    else: print(mx)

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

Solution (Python):
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))
A_max = max(A)
i = A.index(A_max)
del A[i]
B_max = max(A)
for j in range(N):
  if j == i:
    print(B_max)
  else:
    print(A_max)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
b = a.copy()
b.sort(reverse=True)
f, s = b[:2]

for i in range(n):
    if a[i] == f:
        print(s)
    else:
        print(f)

Solution (Python):
import copy
N=int(input())
A=[int(input()) for i in range(N)]
A_f=copy.deepcopy(A)
A_f.sort()
A_max=A_f[N-1]
A_sec=A_f[N-2]

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

Solution (Python):
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
l2 = sorted(l)
a1 = l2[-1]
a2 = l2[-2]
for i in l:
    if i == a1:
        print(a2)
    else:
        print(a1)

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

for j in range(len(numbers)):
    if numbers[j] == ordered[0]:
        print(ordered[1])
    else:
        print(ordered[0])

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

for i in A:
  if i==ma:
    print(mb)
  else:
    print(ma)

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

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
s = max(a)
l = a.index(max(a))
a[l] = 0
t = max(a)

for i in range(n):
    if i == l:
        print(t)
    else:
        print(s)

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

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

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

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
ma1 = max(A)
ma2 = 0
c = 0
for a in A:
    if a == ma1:
        c+=1
    else:
        ma2=max(ma2,a)
for a in A:
    if a!=ma1:
        print(ma1)
    else:
        if c>1:
            print(ma1)
        else:
            print(ma2)

Solution (Python):
n = int(input())
a1 = int(input())
a2 = int(input())
if a1 >= a2:
    maxa1 = a1
    maxa2 = a2
    res = 0
else:
    maxa1 = a2
    maxa2 = a1
    res = 1

for i in range(2, n):
    a_ = int(input())
    if maxa1 <= a_:
        maxa2 = maxa1
        maxa1 = a_
        res = i
    elif maxa2 <= a_:
        maxa2 = a_

for i in range(n):
    if i == res:
        print(maxa2)
    else:
        print(maxa1)

Solution (Python):
n=int(input())
a={}
maxkey=0
maxindex=0
bkmaxkey=0
for i in range(n):
  key=int(input())
  if key in a:
    a[key]+=1
  else:
    a[key]=1
  if maxkey<key:
    if bkmaxkey!=0:
      bkmaxkey=maxkey
    maxkey=key
    maxindex=i
if a[maxkey]>1:
  for i in range(n):
    print(maxkey)
else:
  for i in range(n):
    if i==maxindex:
      a.pop(maxkey)
      print(max(a))
    else:
      print(maxkey)

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

if len(a_re) > 1:
  for i in range(n):
    if a[i] != a_re[0]:
      print(a_re[0])
    elif a[i] == a_re[0]:
      print(a_re[1])
elif len(a_re) == 1 :
  for i in range(n):
    print(a_re[0])

Solution (Python):
n = int(input())
A=[0]*n
maxd = 0
smax = 0
for i in range(n):
	A[i] = int(input())
	if A[i] >= maxd:
		smax = maxd
		maxd =A[i]
	elif A[i] >= smax:
		smax = A[i]
for i in range(n):
	if A[i]==maxd:
		print(smax)
	else:
		print(maxd)

Solution (Python):
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect




from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  n = int(readline())
  A = [int(input()) for _ in range(n)]
  B = sorted(A)
  last1 = B[-1]
  last2 = B[-2]
  if last1==last2:
    for _ in range(n):
      print(last1)
  else:
    for i in range(n):
      if A[i]==last1:
        print(last2)
      else:
        print(last1)
  
  
if __name__ == '__main__':
  main()

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

Solution (Python):
n=int(input())
a=[]
best_cnt=0
[a.append(int(input())) for _ in range(n)]
b = sorted(a)
if len(set(b))==1:
  best=better=b[-1]
else:
  best=list(set(b))[-1]
  better=list(set(b))[-2]
for i in range(n):
  if a[i]==best:
    best_cnt+=1
    
for i in range(n):
  if a[i] == best:
    if best_cnt==1:
      print(better)
    else:
      print(best)
  else:
    print(best)

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

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

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

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

if a.count(max1) >= 2:
    for i in range(n):
        print(max1)
elif a.count(max1) == 1:
    for i in range(n):
        if a[i] == max1:
            print(max2)
        else:
            print(max1)

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

left = [0] * (n+1)
right = [0] * (n+2)
for j in range(1, n):
   left[j] = max(left[j-1], a[j])

right[n+1] = 0
for j in reversed(range(2, n+1)):
   right[j] = max(right[j+1], a[j])

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

Solution (Python):
import heapq
n = int(input())
slist = [int(input())*(-1) for i in range(n)]
sslist = slist[::]
heapq.heapify(slist)
for i in range(n):
  a = heapq.heappop(slist)
  if a == sslist[i]:
    b = heapq.heappop(slist)
    print((-1)*b)
    heapq.heappush(slist,b)
  else:print(-a)
  heapq.heappush(slist,a)

Solution (Python):
import heapq

n = int(input())
a = [int(input()) for i in range(n)]
b = heapq.nlargest(2, set(a))
c = a.count(b[0])

if len(b) == 1 or c > 1: 
    for i in range(n):
        print(b[0]) 

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

Solution (Python):
from collections import deque

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

leftA = [0]
tmp = A[0]
for a in A:
    tmp = max(tmp, a)
    leftA.append(tmp)


rightA = deque([0])
tmp = A[-1]
for a in reversed(A):
    tmp = max(tmp, a)
    rightA.appendleft(tmp)
rightA.appendleft(0)


for i in range(1, n+1):
    print(max(leftA[i-1], rightA[i+1]))

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

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
m1=a[0]
x1=0
m2=0
for i in range(1,n):
  if a[i]>m1:
    m2=m1
    x1=i
    m1=a[i]
  elif a[i]>m2:
    m2=a[i]

for i in range(n):
  if i==x1:
    print(m2)
  else:
    print(m1)

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

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
tmp = sorted(A,reverse=True)
Max = tmp[0]
Second = tmp[1]
for i in range(n):
    if A[i] != Max:
        print(Max)
    else:
        print(Second)

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

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

Solution (Python):
N = int(input())
A = [0] * N
for _ in range(N):
    A[_] = int(input())
    if _ == 1:
        b1 = max(A[0], A[1])
        b2 = min(A[0], A[1])
    elif _ != 0 and A[_] >= b1:
        b2 = b1 
        b1 = A[_]
    elif _ != 0 and A[_] > b2:
        b2 = A[_]

for _ in range(N):
    if A[_] == b1:
        print(b2)
    else:
        print(b1)

Solution (Python):
import heapq as hq
n=int(input())
A=[]
hq.heapify(A)
B=[]
for i in range(n):
    a=int(input())
    hq.heappush(A,-a)
    B.append(a)
M1=-hq.heappop(A)
M2=-hq.heappop(A)
for b in B:
    if b!=M1:
        print(M1)
    elif M1==M2:
        print(M1)
    else:
        print(M2)

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

for i in range(N):
	if A[i] < a_max or count >= 2:
		print(a_max)
	else:
		b_max = 0
		for j in range(N):
				if A[j] == a_max:
					continue
				if A[j] > b_max:
					b_max = A[j]
		print(b_max)

Solution (Python):

"""
Created on Sat Mar 28 17:53:21 2020

@author: Kanaru Sato
"""

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

asort = sorted(a,reverse=True)

Max = asort[0]
MaxIndex = a.index(Max)
Pmax = asort[1]
PmaxIndex = a.index(Pmax)

if Pmax != Max:
    for i in range(n):
        if i == MaxIndex:
            print(Pmax)
        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 a:
  if i==b[-1]:
    print(b[-2])
  else:
    print(b[-1])

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

Solution (Python):
n=int(input())
a=[0]*n
max1=0
max2=0
for i in range(n):
  a[i]=int(input())
  if (max1<=a[i]):
    max2=max1
    max1=a[i]
  elif (max2<=a[i]):
    max2=a[i]

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

Solution (Python):
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

n = load()
A = []
for i in range(n):
    A.append(load())

mx = max(A)
mx_count = 0
for e in A:
    if e == mx:
        mx_count += 1

sa = reversed(sorted(A))
mx2 = 0
for e in sa:
    if e != mx:
        mx2 = e
        break

for e in A:
    if e == mx:
        if mx_count == 1:
            print(mx2)
            continue
        else:
            print(mx)
            continue
    print(mx)

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

Solution (Python):
import bisect

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

if len(set(A)) == 1:
    for i in range(n):
        print(A[0])
else:
    A_ = sorted(A)
    max_1 = A_[-1]
    max_2 = A_[-2]
    for i in A:
        if i == max_1:
            print(max_2)
        else:
            print(max_1)

Solution (Python):
from copy import deepcopy

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

s = deepcopy(a)
s.sort()


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

Solution (Python):
import copy
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=int(input())

first=max(lst)
lst2=copy.copy(lst)
lst2.remove(first)

second=max(lst2)

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
A_dic = {}
for ai in A:
    if ai not in A_dic:
        A_dic[ai] = 1
    else:
        A_dic[ai] += 1

A_sort = sorted(A, reverse=True)
max_1 = A_sort[0]
max_2 = A_sort[1]

for ai in A:
    if ai == max_1 and A_dic[ai] == 1:
        print(max_2)
    else:
        print(max_1)

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

M = max(a)
b = a.count(M)
if b >=2:
    for i in range(n):
        print(M)
else:
    A = copy.copy(a)
    for i in range(b):
        a.remove(M)
        m = max(a)
    for i in A:
        if i == M:
            print(m)
        else:
            print(M)

Solution (Python):
import numpy as np
n = int(input())
a = [int(input()) for i in range(n)]
max = np.max(a)
max_pos = np.argmax(a)
b = [max]*n
b[max_pos] = np.max(a[:max_pos]+a[max_pos+1:])
for key in b:
    print(key)

Solution (Python):

from collections import Counter
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
b = a.copy()
b.sort()
b.reverse()
fi = b[0]
se = b[1]
for i in a:
    if i == fi:
        print(se)
    else:
        print(fi)

Solution (Python):
N = int(input())
k = []
for i in range(N):
    k.append(int(input()))
B = k.copy()
B.sort()
B.reverse()
P = B[0]
Q = B[1]
for i in range(N):
    if k[i] == P:
        print(Q)
    else:
        print(P)

Solution (Python):
def main():
    n = int(input())
    a = [int(input()) for i in range(n)]
    a_sorted = sorted(a)
    first, second = a_sorted[-1], a_sorted[-2]
    f_idx = a.index(first)

    for idx in range(n):
        if idx == f_idx:
            print(second)
        else:
            print(first)

main()

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

A_copy = copy.copy(A)
A_copy.sort(reverse=True)

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

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

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

Solution (Python):
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
b = a.copy()
m = max(a)
b2 = b.remove(m)
mb = max(b)
for j in a:
  if j < m:
    print(m)
  elif j == m:
    print(mb)

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())
a = [int(input()) for _ in range(n)]
b = a.copy()
b.sort()

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

Solution (Python):
import copy

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

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

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

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

sot = sorted(a,reverse = True)
ans1 = sot[0]
ans2 = sot[1]

for i in a:
  if i != ans1:
    print(ans1)
  else:
    print(ans2)

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

Solution (Python):
n = int(input())
a = [0 for i in range(n)]
a[0] = int(input())
max = a[0]
max2 = 0
for i in range(1,n):
    a[i] = int(input())
    if  a[i] >= max:
        max2 = max
        max = a[i]
    elif a[i] >= max2:
        max2 = a[i]
for i in range(n):
    if a[i] == max:
        print(max2)
    else:
        print(max)

Solution (Python):








import numpy as np



n = int(input())

lists = [int(input()) for _ in range(n)]
arr = np.array(lists)

maxnum = max(lists)
filter = arr == maxnum
maxarr = arr[filter]
filt2 = arr < maxnum
nextmaxnum = max(arr[filt2]) if len(arr[filt2])!=0 else maxnum

if len(maxarr)>1:
    for _ in range(n):
        print(maxnum)
else:
    for num in arr:
        if num != maxnum:
            print(maxnum)
        else:
            print(nextmaxnum)

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array([0] + [ir() for _ in range(N)] + [0])
max_left = np.maximum.accumulate(A)
max_rihgt = np.maximum.accumulate(A[::-1])[::-1]
answer = np.zeros(N+1, np.int64)
answer[1:] = np.maximum(max_left[:-2], max_rihgt[2:])
print('\n'.join(answer[1:].astype(str)))

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

Solution (Python):








def resolve():
    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[0])
        else:
            print(B[1])



resolve()