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 input(): return sys.stdin.readline().strip()
def resolve():
    n=int(input())
    l=[int(input()) for i in range(n)]
    l1=sorted(l,reverse=True)
    for j in range(n):
        if l[j]!=l1[0]:
            print(l1[0])
        else:
            print(l1[1])
resolve()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=[int(input()) for i in range(n)]
    biggest = max(l)
    lsort=sorted(l)
    niban=lsort[-2]
    for j in range(n):
        if l[j]!=biggest:
            print(biggest)
        else:
            print(niban)
resolve()

Solution (Python):
n = int(input())
a = [0]*n
b = [0]*n
for i in range(n):
    a[i] = int(input())
    b[i] = a[i]
max_a = max(a)
max_idx = a.index(max_a)
b[max_idx] = 0
max_b = max(b)
for i in range(n):
    if i != max_idx:
        print(max_a)
    else:
        print(max_b)

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

a_sort = sorted(a, reverse=True)

for v in a:
    if v == a_sort[0]:
        print(a_sort[1])
    else:
        print(a_sort[0])

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
t=max(a)
s=a.index(t)
a.pop(s)
u=max(a)
for i in range(n):
  if i==s:
    print(u)
  else:
    print(t)

Solution (Python):
N=int(input())
S=[int(input()) for i in range(N)]
T=sorted(S,reverse=True)

for j in range(N):
  if S[j]!=T[0]:
    print(T[0])
  else:
    print(T[1])

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

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

l=[int(input()) for _ in range(n)]
ls=sorted(l)
first=ls[-1]
second=ls[-2]

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

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):
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd 
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) 






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

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

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

a2 = []
a2 = a.copy()
a2.sort()

mx = max(a)
mx2 = list(reversed(a2))[1]
mc = a.count(mx)


if mc > 1:
  for i in range(n):
    print(mx)

else:
  for i in range(n):
    if a[i] == mx:
      print(mx2)
    else:
      print(mx)

Solution (Python):
import sys
input=sys.stdin.buffer.readline
n=int(input())
L=list(int(input()) for i in range(n))
N=sorted(L)
for i in range(n):
    print(N[-1] if L[i]!=N[-1] else N[-2])

Solution (Python):
n = int(input())
m1, m1_index = 0, 0
m2, m2_index = 0, 0
for i in range(n):
    a = int(input())
    if a > m1:
        m1 = a
        m1_index = i
    elif a > m2:
        m2 = a
for i in range(n):
    if i != m1_index:
        print(m1)
    else:
        print(m2)

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

max = 0
next = 0
count_m = 0
for i in range(N):
    a = int(input())
    if a >= max:
        next = max
        max = a
        count_m = i

    if max > a > next:
        next = a

for j in range(N):
    if j == count_m:
        print(next)
    else:
        print(max)

Solution (Python):
n=int(input())
l=[]
import copy
h=[]
for i in range(n):
    l.append(int(input()))
l_max=max(l)

h=copy.copy(l)
h.sort()
l_second=h[-2]


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

Solution (Python):
def main():
	n  = int(input())
	arr = []
	for _ in range(n):
		arr.append(int(input()))
	arr2 = arr.copy()
	arr2.sort()
	mx = arr2[-1]
	mx2 = arr2[-2]
	for i in range(len(arr)):
		if arr[i]==mx:
			print(mx2)
		else:
			print(mx)

main()

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
m1=max(a)
m2=sorted(a)[-2]
l=[i for i, x in enumerate(a) if x == m1]

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

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
MAX = max(a)
se = list(reversed(sorted(a)))[1]
for i in range(n):
    if a[i] == MAX:
        print(se)
    else:
        print(MAX)

Solution (Python):
n = int(input())
li = [int(input()) for _ in range(n)]
sort_li = sorted(li)
max_num = max(sort_li)
for i in li:
    if i == max_num:
        print(sort_li[-2])
    else:
        print(max_num)

Solution (Python):

import heapq

n = int(input())
a = []
hq = []
for i in range(n):
    i = int(input())
    a.append(i)
    heapq.heappush(hq, -i)

ma1 = heapq.heappop(hq) * -1
ma2 = heapq.heappop(hq) * -1

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

Solution (Python):
N=int(input())
S=[int(input()) for i in range(N)]
T=sorted(S,reverse=True)

for j in range(0,N):
  if S[j]!=T[0]:
    print(T[0])
  else:
    print(T[1])

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

b=sorted(a,reverse=True)
m1=b[0]
m2=b[1]


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

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

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


hakomax= max(hako)

hako2 = copy.deepcopy(hako)

hako2max = max(hako2)

hako2.remove(hako2max)

hako2max = max(hako2)


for i in range(n):
    if hako[i] == hakomax:
        print(hako2max)
    else:
        print(hakomax)

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

mx = 0
mxid = 0
sc = 0
for i in range(n):
  a = int(input())
  if a >= mx:
    sc = mx
    mx = a
    mxid = i
  elif a > sc:
    sc = a
    
for i in range(n):
  if i == mxid:
    print(sc)
  else:
    print(mx)

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

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

a_max = max(a)

if a.count(a_max) >= 2:
    for _ in range(N):
        print(a_max)
    exit()

for i in a:
    if i == a_max:
        tmp = sorted(a)
        tmp = tmp[:-1]
        print(max(tmp))
    else:
        print(a_max)

Solution (Python):

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

ls = sorted(l, reverse=True)

mx = ls[0]
sc = ls[1]


for i in l:
  if i == mx:
    print(sc)
  else:
    print(mx)

Solution (Python):
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
max_1 = max(a)
a[a.index(max_1)] = -1
max_2 = max(a)
a[a.index(-1)] = max_1
for i in range(0,n):
  if a[i] == max_1:
    print(max_2)
  else:
    print(max_1)

Solution (Python):
def main():
    n=int(input())
    b=[]
    amax=0
    amaxx=-1
    memo=-1
    A=list([] for i in range(n))
    for i in range(n):

        temp=int(input())
        b.append(temp)
        if amax<=temp:
            memo=i
            amaxx,amax=amax,temp
    b.sort()  
    for i in range(n):
        if i==memo:
            print(b[-2])
        else:
            print(amax)

main()

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

for i in range(n):
    if i == index:
        print(second)
    else:
        print(first)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
f = 0
s = 0
index = 0
for i in range(n):
    if a[i] > f:
        index = i
        s = f
        f = a[i]
    elif a[i] > s:
        s = a[i]

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

Solution (Python):
N = int(input())
A = []
B = []
for i in range(N):
    a = int(input())
    A.append(a)
    B.append(a)
B.sort(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 = []
maximum = 0
maximum_idx = 0
for i in range(n):
    a = int(input())
    A.append(a)
    if a > maximum:
        maximum = a
        maximum_idx = i

for i in range(n):
    if i != maximum_idx:
        print(maximum)
    else:
        print(sorted(A)[-2])

Solution (Python):
N=int(input())
a=[int(input()) for i in range(N)]
b=sorted(a)
ans_1=b[-1]
ans_2=b[-2]

for i in range(N):
    if a[i]==ans_1:
        print(ans_2)
    else:
        print(ans_1)

Solution (Python):
N=int(input())
a=[int(input()) for i in range(N)]
b=sorted(a)
ans_1=b[-1]
ans_2=b[-2]

for i in range(N):
    if a[i]==ans_1:
        print(ans_2)
    else:
        print(ans_1)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
m = max(A)
L = [m for _ in range(N)]
if A.count(m) == 1:
    for i in range(N):
        if A[i] == m:
            A[i] = 0
            L[i] = max(A)
for i in range(N):
    print(L[i])

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

a = []
for i in range(n):
    a.append(int(input()))
rev_a = sorted(a, reverse=True)

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

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

a_list = []
for _ in range(n):
    a = int(input())
    a_list.append(a)
b_list = sorted(a_list, reverse=True)

for a in a_list:
    ans = b_list[0] if a != b_list[0] else b_list[1]
    print(ans)

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

c.sort()
MAX = c[-1]
second = c[-2]

for num in nums:
    if num == MAX:
        print(second)
    else:
        print(MAX)

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

sorted_a = a.copy()
sorted_a.sort()
if sorted_a[-1] == sorted_a[-2]:
  for i in range(n):
    print(sorted_a[-1])
else:
  for i in range(n):
    if a[i] == sorted_a[-1]:
      print(sorted_a[-2])
    else:
      print(sorted_a[-1])

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

L = A[:]
m1 = max(A)
m1i = A.index(m1)
A.pop(m1i)
m2 = max(A)
m2i = A.index(m2)
A.pop(m2i)

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
max_num0 = max(A)
temp = A.index(max_num0)
A[temp] = 0
max_num1 = max(A)
A[temp] = max_num0

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

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

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

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
m1 = max(a)
mi = a.index(max(a))
a.pop(mi)
m2 = max(a)
for i in range(n):
    if i == mi:
        print(m2)
    else:
        print(m1)

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for a in range(N) ]
    C = sorted(A)
    Af = C[-1]
    As = C[-2]
    for i in range(N):
        if A[i] != Af:
            print(Af)
        else:
            print(As)
if __name__ == "__main__":
    main()

Solution (Python):
import copy
def main():
    N = int(input())
    A = [int(input()) for a in range(N) ]
    C = sorted(A)
    Af = C[-1]
    As = C[-2]

    for i in range(N):
        if A[i] != Af:
            print(Af)
        else:
            print(As)
if __name__ == "__main__":
    main()

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

Solution (Python):
import numpy as np
import sys

n=int(sys.stdin.readline())
a=np.array([sys.stdin.readline() for _ in range(n)],dtype='int64')

ma=a.max()
ju=0
for i in range(n):
    if a[i]!=ma or ju==1:
        print(ma)
    else:
        if i==0:
            b=max(a[i+1:])
        elif i==n-1:
            b=max(a[:i])
        else:
            b=max(max(a[:i]),max(a[i+1:]))
        if ju==0:
            if b==ma:
                ju=1
            else:
                ju=-1
        print(b)

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


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

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

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

cnt = Counter(a)
maxs = sorted(list(set(a)))[::-1]
for ai in a:
  if ai == maxs[0]:
    if cnt[ai] > 1:
      print(ai)
    else:
      print(maxs[1])
  else:
    print(maxs[0])

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

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a_copy = a.copy()
biggest = max(a)
a_copy.remove(biggest)
second_biggest = max(a_copy)

for j in range(n):
    if a[j] == biggest:
        print(second_biggest)
    else:
        print(biggest)

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

for i in range(N):
	A[i].append(i+1)

A.sort(key = lambda x: x[0], reverse = True)

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

Solution (Python):


int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n = II()
    A = [II() for _ in range(n)]
    A2 = sorted(A, reverse=True)
    mx = A2[0]
    mx2 = A2[1]

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


if __name__ == '__main__':
    solve()

Solution (Python):


int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

from collections import Counter

def solve():
    n = II()
    A = [II() for _ in range(n)]
    c = Counter(A)
    c = sorted(c.items())[::-1]
    mx = c[0][0]
    mx_n = c[0][1]
    if mx_n == 1:
        mx2 = c[1][0]
        for a in A:
            if a == mx:
                print(mx2)
            else:
                print(mx)
    else:
        for _ in A:
            print(mx)


if __name__ == '__main__':
    solve()

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

max_a = max(a_inputs)
max_index = a_inputs.index(max(a_inputs))
next_max = sorted(a_inputs)[-2]

for i in range(n):
    if i==max_index:
        print(next_max)
    else:
        print(max_a)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


N = int(input())
A = [int(input()) for _ in range(N)]
sort_a = sorted(A, reverse=True)

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


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

max_a = max(A)
max_idx = [i for i, x in enumerate(A) if x == max_a]

if len(max_idx) > 1:
    for i in range(N):
        print(max_a)
    exit()
else:
    for i in max_idx:
        A[i] = NIL
    second_max = max(A)

    for i in range(N):
        if i in max_idx:
            print(second_max)
        else:
            print(max_a)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a)
max_a = max(a)
max_sc = b[-2]
max_cnt = a.count(max_a)
for i in range(n):

    if max_a > a[i]:
        print(max_a)
    else:
        if max_cnt > 1:
            print(max_a)
        else:
            print(max_sc)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a)
max_a = max(a)
max_cnt = a.count(max_a)
for i in range(n):

    if max_a > a[i]:
        print(max_a)
    else:
        if max_cnt > 1:
            print(a[i])
        else:
            print(b[-2])

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

max_value=max(A)
max_index=A.index(max_value)
A.sort()
second_value=A[-2]

for j in range(N):
    if j==max_index:
        print(second_value)
    else:
        print(max_value)

Solution (Python):
import copy

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

A_max=max(A)
ind_max=A.index(A_max)
B=copy.copy(A)
B.remove(A_max)
B_max=max(B)

for i in range(N):
    if i!=ind_max:
        print(A_max)
    else:
        print(B_max)

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    f, s = 0, 0
    for i in range(N):
        if A[i] >= f:
            f, s = A[i], f
        elif A[i] > s:
            s = A[i]

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


if __name__ == '__main__':
    main()

Solution (Python):
import copy

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

A_max=max(A)
ind_max=A.index(A_max)
B=copy.copy(A)
B.remove(A_max)
B_max=max(B)

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

Solution (Python):
N=int(input())
mx=int(input())
a=[mx]
for i in range(N-1):
    l=int(input())
    a.append(l)
    mx=max(mx,l)
num=a.index(mx)
a.remove(mx)
mx2=a[0]
for j in range(1,N-1):
    mx2=max(mx2,a[j])
for k in range(N):
    if k==num:
        print(mx2)
    else:
        print(mx)

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

B=copy.copy(A)
B.remove(A_max)
B_max=max(B)
 
for i in range(N):
    if A[i]!=A_max:
        print(A_max)
    else:
        print(B_max)

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

B.sort(reverse=True)
max_first = B[0]
max_second = B[1]

for i in range(N):
    if A[i] == max_first:
        print(max_second)
    else:
        print(max_first)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
max1=1
m=0
for i in range(n):
  if max1<a[i]:
    max1=a[i]
    m=i
a.pop(m)
max2=max(a)
a.insert(m,max1)
for i in range(n):
  if i!=m:
    print(max1)
  else:
    print(max2)

Solution (Python):
def main():
    n = int(input())
    m,p = 0,0
    A = []
    for i in range(n):
        a = int(input())
        if a>m:
            m = a
            p = i
        A.append(a)
    for i in range(n):
        if i==p:
            A_ = A[:i]+A[i+1:]
            print(max(A_))
        else:
            print(m)

if __name__ == "__main__":
    main()

Solution (Python):

import io
import math

n,*a = map(int,open(0).read().split())
m = sorted(a)
max = m[-1]
maxi= m[-2]
  
for i in range(n):
    if a[i] == max:
        print(maxi)
    else:
        print(max)

Solution (Python):










mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    
    
    
    
    
    A = [int(input()) for _ in range(n)]

    left = [0] * (n+1)
    right = [0] * (n+1)
    for i in range(1,n):
        left[i] = max(left[i-1],A[i-1])
    for i in range(n-2,-1,-1):
        right[i] = max(right[i+1],A[i+1]) 
    for i in range(n):
        print(max(left[i],right[i]))
if __name__ == '__main__':
  main()

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

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

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

Solution (Python):
n,*a=map(int,open(0).read().split())
b=sorted(a)[::-1]
ma1=b[0]
ma2=b[1]
for i in a:
  if ma1==i:
    print(ma2)
  else:
    print(ma1)

Solution (Python):
n = int(input())
a = []
while True:
  try:
    a.append(int(input()))
  except:
    break

A = sorted(a,reverse=True)

MAX = A[0]
SMAX = A[1]

for i in range(n):
  if a[i]==MAX:
    print(SMAX)
  else:
    print(MAX)

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

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

Solution (Python):
A = int(input())
No1,No2 = 0,0
for i in range(A):
    B = int(input())
    if No2 < B:
        No2 = B
        if No1 < B:
            place = i
        No1,No2 = max(No1,No2),min(No1,No2)
if No1 == No2:
    for i in range(A):
        print(No1)
else:
    for i in range(place):
        print(No1)
    print(No2)
    for i in range(A-place-1):
        print(No1)

Solution (Python):
import sys
n = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for i in range(n)]
B = sorted(A)
m1 = B[-1]
m2 = B[-2]

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

Solution (Python):
import sys
N = int(input())
input = sys.stdin.readline
A = [int(input()) for i in range(N)]
B = sorted(A)
B.reverse()
for j in range(N):
  if A[j] == B[0]:
    print(B[1])
  else:
    print(B[0])

Solution (Python):
import sys
import numpy as np

n = int(sys.stdin.readline().rstrip())
a = np.array(sys.stdin.read().split(), dtype=np.int32)
b = np.sort(a.copy())[::-1]

def main():
    bl = a == b[0]
    a[bl], a[~bl] = b[1], b[0]
    print(*a, sep='\n')

if __name__ ==  '__main__':
    main()

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

    A_asc = sorted(A)
    m1 = A_asc[-1]
    m2 = A_asc[-2]

    if m1 == m2:
        print('\n'.join([str(m1) for _ in range(N)]))
    else:
        for a in A:
            if a != m1:
                print(m1)
            else:
                print(m2)


if __name__ == "__main__":
    main()

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

Solution (Python):










mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    LIST = []
    for i in range(n):
        LIST.append(int(input()))
    
    
    
    
    
    
    
    
    
    left = [0] * (n+1)
    right = [0] * (n+1)
    for i in range(1,n):
        left[i] = max(left[i-1],LIST[i-1])
    for i in range(n-2,-1,-1):
        right[i] = max(right[i+1],LIST[i+1])
    for i in range(n):
        print(max(left[i],right[i]))

if __name__ == '__main__':
  main()

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = max(A)
C = [B]*N
D = sorted(A)[-2]
i = A.index(B)
if A.count(B) == 1:
    C[i] = D

for a in C:
    print(a)

Solution (Python):










mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    LIST = []
    for i in range(n):
        LIST.append(int(input()))
    s_L = sorted(LIST,reverse=True)
    ma_1 = s_L[0]
    ma_2 = s_L[1]

    for i in range(n):
        if LIST[i] != ma_1:
            print(ma_1)
        else:
            print(ma_2)

if __name__ == '__main__':
  main()

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


sorted_A = list(reversed(sorted(A)))

maxiuam = sorted_A[0]

is_there_tow_maximum = A.count(maxiuam) >= 2
if is_there_tow_maximum:
    answer = (str(maxiuam) + "\n") * N
    print(answer)
    exit()

second = None
for Ai in sorted_A:
    if Ai < maxiuam:
        second = Ai
        break

answer = [second if Ai == maxiuam else maxiuam for Ai in A]
answer = "\n".join(map(str, answer))

print(answer)

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


sorted_A = list(reversed(sorted(A)))

maxiuam = sorted_A[0]

is_there_tow_maximum = A.count(maxiuam) >= 2
if is_there_tow_maximum:
    answer = (str(maxiuam) + "\n") * N
    print(answer)
    exit()

second = None
for Ai in sorted_A:
    if Ai < maxiuam:
        second = Ai
        break

answer = []
for Ai in A:
    if Ai == maxiuam:
        answer.append(second)
    else:
        answer.append(maxiuam)
answer = "\n".join(map(str, answer))

print(answer)

Solution (Python):
import collections


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


sorted_A = list(reversed(sorted(A)))

maxiuam = sorted_A[0]
is_there_tow_maximum = A.count(maxiuam) >= 2

second = None
for Ai in sorted_A:
    if Ai < maxiuam:
        second = Ai
        break

answer = []
for Ai in A:
    if is_there_tow_maximum:
        answer.append(maxiuam)
    elif Ai == maxiuam:
        answer.append(second)
    else:
        answer.append(maxiuam)
answer = "\n".join(map(str, answer))

print(answer)

Solution (Python):
import sys

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

sorted_A = sorted(A)
first_max = sorted_A[-1]
second_max = sorted_A[-2]

for a in A:
    if a == first_max:
        print(second_max)
    else:
        print(first_max)

Solution (Python):

import heapq
n = int(input())
a = [int(input()) for _ in range(n)]
a_t = a[:]
a = list(map(lambda x:x*(-1),a))
heapq.heapify(a)
for i in range(n):
    m = heapq.heappop(a)*(-1)
    if m == a_t[i]:
        m = heapq.heappop(a)*(-1)
        heapq.heappush(a,a_t[i]*(-1))
    print(m)
    heapq.heappush(a,m*(-1))

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


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

b = sorted(a)
m = max(b)

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

Solution (Python):
N = int(input())
M1 = 0
M2 = 0
n = 0

for i in range(N):
  A = int(input())
  if M1 < A:
    M1 = A
    n = i
  elif M2 < A:
    M2 = A
  else:
    continue

for i in range(N):
  if n == i and M1 > M2:
    print(M2)
  else:
    print(M1)

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

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

Solution (Python):
n,*a=map(int,open(0).read().split())
p=max(a)
k=a.index(p)
s=max(a[:k]+a[k+1:])
print('\n'.join(str(p) if i!=k else str(s) for i in range(n)))

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
sort_A = sorted(A)
max_A = sort_A[-1]

for i in A:
    if max_A != i:
        print(max_A)
    else:
        print(sort_A[-2])

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
a1 = sorted(a)
if a1[n-1]==a1[n-2]:
  flag = True 
  maxone = a1[n-1]
else:
  flag = False 
  maxone = a1[n-1]
  maxtwo = a1[n-2]
for i in range(n):
  if a[i]==maxone and flag==True: print(maxone)
  elif a[i]==maxone and flag==False: print(maxtwo)
  elif a[i]!=maxone: print(maxone)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
Max=max(a)
Second=sorted(a)[-2]
Index=a.index(Second)
[print(Max) if i!=Max else print(Second) for i in a]

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


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

dammyA = sorted(A)
maxfir = dammyA[-1]
maxsec = dammyA[-2]


for i in range(N):
    if A[i]==maxfir:
        print(maxsec)
    else:
        print(maxfir)

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

for i in range(N):
    if A[i] == m1:
        print(m2)
    else:
        print(m1)

Solution (Python):
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))
big = max(A)
place = A.index(big)
for i in range(N):
  if i != place: print(big)
  else: print(max(A[:i] + A[i + 1:]))

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

Solution (Python):
def myAnswer(N:int,A:list) -> None:
   MAX1 = max(A)
   tmp = A[:]
   tmp.remove(MAX1)
   MAX2 = max(tmp)
   for i in range(N):
      if(A[i] == MAX1):
         print(MAX2)
      else:
         print(MAX1)

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A =[]
   for _ in range(N):
      A.append(int(input()))
   myAnswer(N,A)

if __name__ == '__main__':
   main()

Solution (Python):
N = int(input())
maxv1 = 0
maxv2 = 0

for i in range(N):
    a = int(input())
    if a >= maxv1:
        maxv1, maxv2 = a, maxv1
        flag = i
        continue
    if a >= maxv2:
        maxv2 = a

for i in range(N):
    if i == flag:
        print(maxv2)
    else:
        print(maxv1)

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