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 sys

def solve(N: int, A: "List[int]"):
    return




if __name__ == '__main__':
    
    N = int(input())
    A = [0]*N
    max_a1 = 0
    max_a2 = 0
    for i in range(N):
        a = int(input())
        if a > max_a1:
            max_a1 ,max_a2 = a, max_a1
        elif a > max_a2:
            max_a2 = a
        A[i] = a

    for i in range(N):
        print(max_a1) if A[i] < max_a1 else print(max_a2)

Solution (Python):
import os, sys, re, math

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

sa = sorted(A, reverse=True)

for a in A:
    print(sa[1] if a == sa[0] else sa[0])

Solution (Python):
N = int(input())
An = []
for n in range(N):
    A = int(input())
    An.append(A)
An_sorted = sorted(An)
for Ai in An:
    if An_sorted[-1] == Ai:
        print(An_sorted[-2])
    else:
        print(An_sorted[-1])

Solution (Python):
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
sorted_l = sorted(l)  
mmm = sorted_l[-1]
only = mmm != sorted_l[-2]
for p in l:
    if not only or p != mmm:
        print(mmm)
    else:
        print(sorted_l[-2])

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

ma_n = 0
ma_b = 0
mn = 0
for i in range(N):
    if A[i] >= ma_n:
        ma_b = ma_n
        ma_n = A[i]
        mn = i
    elif A[i] >= ma_b:
        ma_b = A[i]
for i in range(N):
    if i != mn:
        print(ma_n)
    else:
        print(ma_b)

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

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

Solution (Python):
import sys
from heapq import heappush, heappop


def main():
    n=int(input())
    a=[(int(input()),i) for i in range(n)]
    a.sort(reverse=True)
    m,mi=a[0][0],a[0][1]
    for i in range(n):
        if mi==i:
            print(a[1][0])
        else:
            print(m)
  
if __name__ == '__main__':
    main()

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

c = [0] * n
d = [0] * n

i = 0
for j in range(n):
    c[j] = i
    i = max(i, a[j])
i = 0
for j in reversed(range(n)):
    d[j] = i
    i = max(i, a[j])

for i in range(n):
    print(max(c[i],d[i]))

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

Solution (Python):
n=int(input())
l=[int(input()) for _ in range(n)]
s=sorted(l)
a,b=s[-1],s[-2]
for i in l: print([b,a][a>i])

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

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

b = sorted(a, reverse=True)

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

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

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

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

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


max_num = max(numbers)


max_nums = numbers.count(max_num)

if max_nums >= 2:
    for i in range(len(numbers)):
        print(max_num)
else :
    max_index = numbers.index(max(numbers))
    for i in range(len(numbers)):
        if i == max_index:
            second_max = sorted(numbers)[-2]
            print(second_max)
        else :
            print(max_num)

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

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

if max1 == max2:
  for _ in range(N):
    print(max1)
else:
  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)]
b = a.index(max(a))
c = max(a)
a.remove(max(a))
for i in range(n):
  if i == b:
    print(max(a))
  else:
    print(c)

Solution (Python):
import heapq
q = []
N = int(input())
A = [int(input()) for _ in range(N)]
for i in range(N):
    heapq.heappush(q, -A[i])

MAX1 = -heapq.heappop(q)
MAX2 = -heapq.heappop(q)
for i in range(N):
    if A[i] == MAX1:
        print(MAX2)
    else:
        print(MAX1)

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

Solution (Python):
from copy import deepcopy

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

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

solve()

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


arr = sorted(a)

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

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

A_sort = sorted(A)

A_max = A_sort[-1]
A_second_max = A_sort[-2]

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

Solution (Python):

"""
C - Exception Handling
https://atcoder.jp/contests/abc134/tasks/abc134_c

"""
import sys


def solve(arr):
    sarr = sorted(arr)
    ans = [sarr[-1] if a != sarr[-1] else sarr[-2] for a in arr]
    return ans


def main(args):
    N = int(input())
    arr = [int(input()) for _ in range(N)]
    ans = solve(arr)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import sys
import os
import copy

def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.readline().rstrip())
    A = [int(sys.stdin.readline().rstrip()) for _ in range(N)]
    B = copy.copy(A)
    A.sort(reverse=True)
    f = A[0]
    s = A[1]

    for n in range(N):
        t = B[n]
        if f == t:
            print(s)
        else:
            print(f)


if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())
a = [int(input()) for i in range(n)]
maxa = [-1,0]
premaxa = [-1,-1]
for i,num in enumerate(a):
    if num >= maxa[1]:
        premaxa = [maxa[0], maxa[1]]
        maxa = [i,num]
    elif num > premaxa[1]:
        premaxa = [i,num]
for i in range(n):
    if i == maxa[0]:
        print(premaxa[1])
    else:
        print(maxa[1])

Solution (Python):
N = int(input())
a = list()
tmp = [0, 0]

for _ in range(N):
    ai = int(input())
    a.append(ai)
    if ai > tmp[1]:
        tmp[1] = ai
    elif ai > tmp[0]:
        tmp[0] = ai

m = tmp[1]

for ai in a:
    ans = m
    if ai == m:
        ans = tmp[0]
    print(ans)

Solution (Python):
n = int(input())
first = 0
second = 0
first_index = 0
second_index = 0
for i in range(n):
    x = int(input())
    if x > first:
        second_index = first_index
        second = first
        first = x
        first_index = i
    elif x > second:
        second = x
        second_index = i
for i in range(n):
    if i == first_index:
        print(second)
    else:
        print(first)

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

my_max = np.max(np.array(A))
max_count = np.count_nonzero(A == my_max)

if max_count > 1:
    [print(my_max) for _ in range(N)]
else:
    my_next_max = np.max(np.array([a for a in A if a != my_max]))
    [print(my_max) if a != my_max else print(my_next_max) for a in A]

Solution (Python):
N = int(input())
A = []
my_max = 0
for _ in range(N):
    a = int(input())
    A.append(a)
    my_max = max(my_max, a)

max_count = A.count(my_max)

if max_count > 1:
    [print(my_max) for _ in range(N)]
else:
    my_next_max = max([a for a in A if a != my_max])
    [print(my_max) if a != my_max else print(my_next_max) for a in A]

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
AM = sorted(A)[-2:]
for a in A:
    print(AM[a != AM[1]])

Solution (Python):
n=int(input())
A=[int(input()) for _ in range(n)]
m=max(A)
ind=A.index(m)
A[ind]=0
m2=max(A)
for i in range(n):
    print(m2 if i == ind else m)

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

Solution (Python):
n = int(input())
import heapq
a = []
heapq.heapify(a) 
z = []
for i in range(n):
    ai = int(input())
    z.append(ai)
    if i < 2:
        heapq.heappush(a, ai)
    else:
        t = heapq.heappop(a)
        
        heapq.heappush(a, max(ai, t))

x2 = heapq.heappop(a)
x1 = heapq.heappop(a)

for zi in z:
    if x1 != zi:
        print(x1)
    else:
        print(x2)

Solution (Python):
n = int(input())
l = [int(input()) for i in range(n)]
lr = sorted(l,reverse = True)
ma = max(l)
ind = l.index(ma)
for i in range(n):
  if i == ind:
    print(lr[1])
  else:
    print(ma)

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

SA = sorted(A, reverse=True)
max_1A = SA[0]
max_2A = SA[1]

for i in range(N):
  if A[i] == max_1A:
    print(max_2A)
  else:
    print(max_1A)

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

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)[::-1]
mx = b[0]
mx2 = b[1]

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

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

M = max(A)
ind = A.index(M)

A.pop(ind)
m = max(A)
for i in range(N):
  
  if i != ind:
    print(M)
  else:
    print(m)

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

a1=max(a)
temp=copy.copy(a)
temp.remove(a1)
a2=max(temp)


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

Solution (Python):
N = int(input())
List = [int(input()) for _ in range(N)]
s_list = sorted(List,reverse=True)
Max1 = s_list[0]
Max2 = s_list[1]

for i in range(N):
    if List[i]==Max1:
        print(Max2)
    else:
        print(Max1)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
m1=max(a)
m2=-1
c=0
for x in a:
    if x==m1:c+=1
    if x!=m1:m2=max(m2,x)
if c>=2:m2=m1
if m2==-1:m2=m1
for x in a:
    if x==m1:print(m2)
    else:print(m1)

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

sort_a = sorted(a)

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

Solution (Python):
n = int(input())
a = []
maxi = [0, 0]
for _ in range(n):
    a.append(int(input()))
    if maxi[0] < a[-1]: 
        maxi[1] = maxi[0]
        maxi[0] = a[-1]
    elif maxi[1] < a[-1]:
        maxi[1] = a[-1]

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

Solution (Python):
N=int(input())
first=0
second=0
d=[0]
for i in range(N):
  a=int(input())
  if first<a:
    first=a
    d=i
  elif second<a:
    second=a
if first==second:
  for _ in range(N):
    print(first)
else:
  for j in range(N):
    if j==d:
      print(second)
    else:
      print(first)

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

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

Solution (Python):
from collections import Counter

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

c = Counter(A)
sort_A = sorted(c.items(), key=lambda x: x[0], reverse=True)
if sort_A[0][1] > 1:
    for _ in range(N):
        print(sort_A[0][0])
else:
    for i in range(N):
        if A[i] != sort_A[0][0]:
            print(sort_A[0][0])
        else:
            print(sort_A[1][0])

Solution (Python):
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))

INF = 10**7
max = -INF
max2 = -INF

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

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
ma=max(a)
b=a
for i in range(n):
  if b[i]==ma:
    b[i]=0
    p=i
    break
    
maa=max(b)
A=[]
for i in range(n):
  if i!=p:
    A.append(ma)
  else:
    A.append(maa)
    
for i in range(n):
  print(A[i])

Solution (Python):
import heapq

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

list_ans = list_score.copy()
list_ans.sort()


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

Solution (Python):
def main(n,a):
    lst = sorted(a, reverse = True)
    first, second = lst[0:2]
    res = []
    for i in range(len(a)):
        if a[i] == first:
            res.append(str(second))
        else:
            res.append(str(first))
    return res

n = int(input())
a = [int(input()) for _ in range(n)]
print("\n".join(main(n,a)))

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

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

Solution (Python):

n=int(input())
a=[int(input()) for _ in range(n)]
max1,max2=0,0
for i in range(n):
    if a[i]>max1:
        max2=max1
        max1=a[i]
    elif a[i]>max2:
        max2=a[i]
        
for i in range(n):
    print(max1 if a[i]!=max1 else max2)

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

for i in l:
	if i<k[-1]:
		print(k[-1])
	if i==k[-1]:
		print(k[-2])

Solution (Python):


n = int(input().rstrip())

numbers = [int(input().rstrip()) for i in range(n)]

ordered = list(reversed(sorted(numbers)))

for i in numbers:
    if i == ordered[0]:
        print(ordered[1])
    else:
        print(ordered[0])

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
M0 = B[-1]
M1 = B[-2]

for a in A:
    if a==M0:
        print(M1)
    else:
        print(M0)

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

for i in range(N):
    if As[-1] > A[i]:
        print(As[-1])
        
    elif As[-1] == A[i]:
        print(As[-2])

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(stdin.readline().rstrip()) for _ in range(n)]

a_s = sorted(a)
m1 = a_s[-1]
m2 = a_s[-2]

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

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

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

maxA = max(A)
B = sorted(A, reverse = True)
secA = max(B[1:])
for i in range(N) :
    if A[i] == maxA :
        print(secA)
    else :
        print(maxA)

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

M = max(A)
ind = A.index(M)
A[ind] = 0
M2 = max(A)


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

Solution (Python):

n = int(input())

A = [0] * n
for i in range(n):
    A[i] = int(input())

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

First = max(A)
ind = A.index(First)
A[ind] = 0
Second = max(A)
A[ind] = First

for i in range(n):
    if A[i] == First:
        print(Second)
    else:
        print(First)

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

Solution (Python):
def main(n: int, a: list):
    a_max, a_second = sorted(a, reverse=True)[:2]

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


if __name__ == "__main__":
    n = int(input())
    a = [int(input()) for _ in range(n)]

    main(n, a)

Solution (Python):
import sys
N = int(input())
A = [int(input()) for _ in range(N)] 
A_max1 = max(A)
if A.count(A_max1) >= 2:
    for i in range(N):
        print(A_max1)
    sys.exit()    
elif  A.count(A_max1) == 1:
    A[A.index(A_max1)] = 0
    A_max2 = max(A)
    A[A.index(0)] = A_max1
    for i in range(N):
        if A[i] == A_max1:
            print(A_max2)
        else:
            print(A_max1)

Solution (Python):


from copy import copy


def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A_max = copy(A)
    A_max.sort(reverse=True)

    for a in A:
        if a == A_max[0]:
            print(A_max[1])
        else:
            print(A_max[0])


if __name__ == "__main__":
    main()

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

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

A = []
max1 = 0
max2 = 0
max1w = False
for _ in range(n):
    a = int(input())
    A.append(a)
    if a > max1:
        max2 = max1
        max1 = a
        max1w = False
    elif a == max1:
        max1w = True
    elif a > max2:
        max2 = a

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

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

right = [0]
left = [0]

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

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

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

Solution (Python):
N = int(input())
A = [0]*N
for i in range(0, N):
    A[i] = int(input())
B = A.copy()
B.sort()
m1, m2 = B[N-1], B[N-2]
for i in range(0, N):
    if A[i] != m1:
        print (m1)
    else:
        print (m2)

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

for _ in range(N):
    A.append((-1)*(int(input())))

B = A.copy()

heapq.heapify(B)
max_1 = 0 - heapq.heappop(B)
max_2 = 0 - heapq.heappop(B)

if max_1 == max_2:
    for _ in range(N):
        print(max_1)
        
else:
    index = A.index(-max_1)
    
    for i in range(index):
        print(max_1)

    print(max_2)

    for i in range(N - index - 1):
        print(max_1)

Solution (Python):





import sys

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    a = [0] * n

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

    a_lmax = [0] * n
    a_rmax = [0] * n
    a_lmax[0] = 0
    a_rmax[-1] = 0

    for i in range(1, n):
        a_lmax[i] = max(a_lmax[i-1],a[i-1])
        a_rmax[-1-i] = max(a_rmax[-i], a[-i])

    for i in range(n):
        print(max(a_lmax[i], a_rmax[i]))


if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    s = sorted(list(set(A)))
    mc = A.count(s[-1])

    for a in A:
        if len(s) == 1:
            print(s[-1])
        elif a == s[-1] and mc == 1:
            print(s[-2])
        else:
            print(s[-1])



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

n, *a = map(int, sys.stdin.read().split())

x, y = sorted(list(a))[n-2:n]
for i in a:
    if i == y:
        print(x)
    else:
        print(y)

Solution (Python):
import copy

n = int(input())
lst = []
for _ in range(n):
    lst.append(int(input()))
clean_lst = copy.deepcopy(lst)

m1 = max(lst)
lst.remove(m1)
m2 = max(lst)

for i in clean_lst:
    print(m1 if i!=m1 else m2)

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

sorted_a = sorted(a_ls,reverse = True)
max_a = sorted_a[0]
second_a = sorted_a[1]
max_ind = a_ls.index(max_a)
for i in range(n):
    if not i == max_ind:
        print(max_a)
    else:
        print(second_a)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
m = max(a)
if a.count(m) > 1:
    for i in range(n):
        print(m)
else:
    for i in range(n):
        if a[i] == m:
            b = a.copy()
            b.remove(m)
            print(max(b))
        else:
            print(m)

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
y,x=sorted(A)[-2:]
for a in A:
    print(x if a!=x else y)

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

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

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

desc = sorted(list(set(a)), reverse=True)
max = desc[0]
large_2 = desc[1] if len(desc) > 1 else 0

if a.count(max) == 1:
    for val in a:
        if val == max:
            print(large_2)
        else:
            print(max)
else:
    for _ in range(n):
        print(max)

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):
n = int(input())
a = [0]*n
for i in range(n):
  a[i] = int(input())

x = sorted(set(a), reverse = True)
t = [0]*2
if len(x) > 1:
  t[0],t[1] = x[0],x[1]
else:
  t[0],t[1] = x[0],x[0]
  
max = t[0]
count= a.count(max)
for i in range(n):
  if a[i] == max:
    if count > 1:
      print(max)
    else:
      print(t[1])
  else:
    print(max)

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
a = sorted(set(A))
max_count = A.count(a[-1])

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

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

Solution (Python):
import copy
N=int(input())
A=[int(input()) for _ in range(N)]
l=[]
for i in range(N):
    l.append([A[i],i])
l.sort(key=lambda x:x[0],reverse=True)

excep=l[0][1]

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

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

    max_left = [0] * N
    max_right = [0] * N

    ary = []

    for i in range(N):
        v = int(input())
        ary.append(v)

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

    for i in range(N - 1, 0, -1):
        max_right[i-1] = max(max_right[i], ary[i])

    for i in range(N):
        v = max(max_left[i], max_right[i])
        print(v)


main()

Solution (Python):
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())
Anp = np.array(A)

L = np.maximum.accumulate(Anp)
R = np.maximum.accumulate(Anp[::-1])[::-1]

LL = [0] + L.tolist()[:-1]
RR = R.tolist()[1:] + [0]

for x, y in zip(LL, RR):
    print(x if x > y else y)

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


sorted_A = sorted(A)
max_A = sorted_A[-1]
second_max_A = sorted_A[-2]

for i in range(N):
    if A[i] != max_A:
        print(max_A)
    else:
        print(second_max_A)

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

max_value = max(a)
max_index = a.index(max_value)

for i in range(n):
    if not i == max_index:
        print(max_value)
    else:
        newlist = a[:i] + a[i+1:]
        print(max(newlist))

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

max1 = 0
max2 = 0
for i in range (N):
    if A[i] >= max1:
        max2 = max1
        max1 = A[i]
    elif A[i] >= max2:
        max2 = A[i]

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

Solution (Python):
import sys
import itertools



read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())
ma = max(A)
num = A.index(ma)
A.remove(ma)
ma2 = max(A)
A.insert(num, ma)
if ma == ma2:
    for _ in range(N):
        print(ma)
else:
    for i in range(N):
        if i == num:
            print(ma2)
        else:
            print(ma)

Solution (Python):
N = int(input())
A = list()
for i in range(N):
  A.append(int(input()))
ma = max(A)
am = sorted(A,reverse = True)
for i in range(N):
  if ma != A[i]:
    print(ma)
  else:
    print(am[1])

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

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
AL = [A[0]]*N
AR = [A[-1]]*N
for i in range(1,N):
    AL[i] = max(AL[i-1], A[i])
    AR[N-i-1] = max(A[N-i-1], AR[N-i])
INF = float("inf")
AL = [-INF]+AL+[-INF]
AR = [-INF]+AR+[-INF]
for i in range(N):
    print(max(AL[i],AR[i+2]))

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

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

Asort = sorted(A,reverse=True)
first = max(Asort)
second = max(Asort[1:N])

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

Solution (Python):
N = int(input())
A_list = []
for i in range(N):
    A = int(input())
    A_list.append(A)
sort_A = sorted(A_list, reverse = True)
max_f = sort_A[0]
max_s = sort_A[1]
for i in range(N):
    print(max_s) if A_list[i] == max_f else print(max_f)

Solution (Python):
import copy

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

cd = copy.copy(ab)

cd.sort()

for i in ab:
    if i == cd[-1]:
        print(cd[-2])
    else:
        print(cd[-1])

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

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

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

max_index=0
_max=max(a)
s=0

for i in range(n):
    if a[i]==_max and s<1:
        s+=1
        max_index=i
    elif a[i]==_max and s>=1:
        s+=1
        break

if s==1:
    for i in range(n):
        if i==max_index:
            print(max(a[:max_index]+a[max_index+1:]))
        else:
            print(_max)
else:
    for i in range(n):
        print(_max)

Solution (Python):
N = int(input())
A = []
first = 0
second = 0
for _ in range(N):
    t = int(input())
    if t > first:
        second = first
        first = t
    elif t > second:
        second = t
    A.append(t)

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

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

A_asc = sorted(A)
max1 = A_asc[-1]
max2 = A_asc[-2]

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

Solution (Python):
import numpy as np

N = int(input())
A = np.zeros(N, np.int32)
for i in range(N):
  A[i] = int(input())

A_asc = np.sort(A)
max1 = A_asc[-1]
max2 = A_asc[-2]

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

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

Solution (Python):

N = int(input())
A = {}
AA = []
for i in range(N):
    a = int(input())
    if a not in A.keys():
        A[a] = 1
    else:
        A[a] += 1
    AA.append(a)
key = list(A.keys())
key.sort(reverse=True)
ans = []
for i in range(N):
    if AA[i] != key[0]:
        print(key[0])
    else:
        if A[key[0]] < 2:
            print(key[1])
        else:
            print(key[0])

Solution (Python):

N = int(input())
A = {}
AA = []
for i in range(N):
    a = int(input())
    if a not in A.keys():
        A[a] = 1
    else:
        A[a] += 1
    AA.append(a)
key = list(A.keys())
key.sort(reverse=True)
ans = []
for i in range(N):
    if AA[i] != key[0]:
        ans.append(key[0])
    else:
        if A[key[0]] < 2:
            ans.append(key[1])
        else:
            ans.append(key[0])
print(*ans, sep="\n")

Solution (Python):
def main():
  
  n,*l=(map(int,open(0).read().split()))
  m=sorted(list(l))
  a,b=m[-1],m[-2]
  s=""
  for x in l:
    if x!=a:
      s+=str(a)+"\n"
    else:
      s+=str(b)+"\n"
  print(s)
  
if __name__=="__main__":
  main()

Solution (Python):


itemCount = int(input().rstrip())
items = [int(input().rstrip()) for i in range(itemCount)]
max_items = [i for i in reversed(sorted(items))][0:2]

for i in items:
    if i == max_items[0]:
        print(max_items[1])
    else:
        print(max_items[0])

Solution (Python):
N=int(input())
a=[]
for _ in range(N):
    a.append(int(input()))
x=sorted(a,reverse=True)
m1=x[0]
m2=x[1]
for i in a:
    if i==m1:
        print(m2)
    else:
        print(m1)

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

new_lst = list(set(a_lst))
new_lst.sort()

if len(new_lst)==1:
    for _ in range(n):
        print(new_lst[-1])

else:
    maximum = new_lst[-1]
    semi_maximum = new_lst[-2]
    count = a_lst.count(maximum)

    for i in range(n):
        a = a_lst[i]

        if a==maximum:
            if count==1:
                print(semi_maximum)
            elif count>1:
                print(maximum)
        elif a<maximum:
            print(maximum)