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=[]
b=[]
for _ in range(N):
  a_=int(input())
  a.append(a_)
  b.append(a_)
x=max(a)
b.sort()
if a.count(x)>=2:
  for i in range(N):
    print(x)
else:
  for i in range(N):
    if a[i]==x:
      print(b[-2])
    else:print(x)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
M = max(A)
c = A.count(M)
a = list(set(A))
a.sort(reverse=True)
 
for i in range(N):
  if A[i] != M:
    print(M)
  else:
    if c >= 2:
      print(M)
    else:
      print(a[1])

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

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

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

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
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()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

index = A.index(max(A))
A_sort = sorted(A)

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

Solution (Python):
import sys
import numpy as np 

n = int(sys.stdin.readline().rstrip())
a = np.array(sys.stdin.read().split(), dtype=np.int64)

def main():
    b = np.sort(a)
    m1 = b[-1]
    m2 = b[-2]
    res = np.zeros(n, dtype=np.int64)
    res[a <= m2] = m1
    res[res == 0] = m2
    return res
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
import sys
import numpy as np 

n = int(sys.stdin.readline().rstrip())
a = np.array(sys.stdin.read().split(), dtype=np.int64)

def main():
    b = np.sort(a)
    m1 = b[-1]
    m2 = b[-2]
    res = np.zeros(n, dtype=np.int64)
    res[a <= m2] = m1
    res[a == m1] = m2
    return res
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
import sys

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

def main():
    b = sorted(a.copy())
    m1 = b[-1]
    m2 = b[-2]
    if m1 == m2:
        res = [m1] * n
    else:
        res = [None] * n
        for i in range(n):
            if a[i] < m1:
                res[i] = m1
            else:
                res[i] = m2
    return res
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Solution (Python):
N = int(input())
a = []
for i in range(N):
  a.append(int(input()))
a_copy = sorted(a, 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 i in range(n)]

inx=a.index(max(a))
a_s=sorted(a)

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

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

m = -float("inf")
m2 = -float("inf")
max_idx = None
for i in range(N):
    el = int(input())    
    if (el > m):
        m2 = m
        m = el
        max_idx = i
    elif (el > m2):
        m2 = el


for i in range(max_idx):
    print(m)

print(m2)

for i in range(N-max_idx-1):
    print(m)

Solution (Python):
N = int(input())
l = [None] * N
m = -float("inf")
m2 = -float("inf")
for i in range(N):
    l[i] = int(input())
    
    if (l[i] > m):
        m2 = m
        m = l[i]
    elif (l[i] > m2):
        m2 = l[i]


for el in l:
    if el == m:
        print(m2)
    else:
        print(m)

Solution (Python):
N=int(input())
A=[]
for i in range(0,N):
    A.append(int(input()))
M=max(A)
x=A.index(M)
A[x]=0
y=max(A)
for i in range(N):
    if i!=x:
        print(M)
    else:
        print(y)

Solution (Python):
n = int(input())
al = list(int(input()) for _ in range(n))
al_s = sorted(list(set(al)), reverse=True)

if al.count(al_s[0]) == 1:
    for a in al:
        if a != al_s[0]:
            print(al_s[0])
        else:
            print(al_s[1])
else:
    for i in range(n):
        print(al_s[0])

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

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

first = 0
second = 1
if A[first] < A[second]:
    first, second = second, first
for i in range(2, n):
    if A[first] < A[i]:
        second = first
        first = i
    elif A[second] < A[i]:
        second = i

print(*[A[first] if i != first else A[second] for i in range(n)], sep='\n')

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
left_l = [0]
right_l = [0]
for i in range(1, n):
    maxVal = max(left_l[i-1], a[i-1])
    left_l.append(maxVal)
for j in range(1, n):
    maxVal = max(right_l[j-1], a[-j])
    right_l.append(maxVal)
right_l.reverse()
for k in range(n):
    maxVal = max(left_l[k],right_l[k])
    print(maxVal)

Solution (Python):
n=int(input())
v=[]
for i in range(n):
  p=int(input())
  v.append(p)
vec=sorted(v)
for i in range(n):
  if v[i]==vec[n-1]:
    print(vec[n-2])
  else:
    print(vec[n-1])

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

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

Solution (Python):
def main():
    N=int(input())
    A=[0 for i in range(0,N)]
    for i in range(0,N):
        A[i]=int(input())
    s=max(A)
    index=0
    value=[0 for i in range(0,s+1)]
    for i in range(0,N):
        value[A[i]]+=1
        if A[i]==s:
            index=i
    if value[s]>1:
        for i in range(0,N):
            print(s)
    else:
        for i in range(0,N):
            if i==index:
                A[index]=0
                print(max(A))
            else:
                print(s)

if __name__ == '__main__':
    main()

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np
 
N = int(readline())
A = np.zeros(N+2,np.int32)
A[1:-1] = np.array(read().split(),np.int32)

left_max = np.maximum.accumulate(A[:-2])
right_max = np.maximum.accumulate(A[::-1])[::-1][2:]
answer = np.maximum(left_max, right_max)
print('\n'.join(answer.astype(str)))

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.zeros(N+2, np.int32)
A[1:-1] = np.array([ir() for _ in range(N)])

left_max = np.maximum.accumulate(A[:-2])
right_max = np.maximum.accumulate(A[::-1])[::-1][2:]
answer = np.maximum(left_max, right_max)
print('\n'.join(answer.astype(str)))

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.zeros(N+2, np.int32)
A[1:-1] = np.array([ir() for _ in range(N)])

left_max = np.maximum.accumulate(A[:-2])
right_max = np.maximum.accumulate(A[::-1])[::-1][2:]
answer = np.maximum(left_max, right_max)
print('\n'.join(map(str, answer)))

Solution (Python):
import sys

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

N = ir()
A = [ir() for _ in range(N)]
top = max(A)
if A.count(top) == 1:
    second = sorted(A, reverse=True)[1]
    for i in range(N):
        if A[i] == top:
            print(second)
        else:
            print(top)
else:
    for i in range(N):
        print(top)

Solution (Python):
n = int(input())
s = list(int(input()) for _ in range(n))
i = 0
sort_s = sorted(s)

for i in s:
    if i == sort_s[-1]:
        print(sort_s[-2])
    else:
        print(sort_s[-1])

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

aa = sorted(a, reverse=True)[:2]

for e in a:
  if e == ma:
    print(aa[1])
  else:
    print(aa[0])

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

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

maximum = max(a)
ans = [maximum]*n
if a.count(maximum) ==1:
    maximum_index = a.index(maximum)
    ans[maximum_index] = max(
        a[:maximum_index]+a[maximum_index+1:]
    )
for i in range(n):
    print(ans[i])

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

f, s = sorted(a, reverse=True)[:2]

for aa in a:
    if aa == f:
        print(s)
    else:
        print(f)

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

m1 = 0
m2 = 0

for ind, i in enumerate(a):
    if i>m1:
        m2 = m1
        m1 = i
        m_i = ind
    elif i>m2:
        m2 = i

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

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

dp1 = [0]*(N+1)
dp2 = [0]*(N+2)

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

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

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

Solution (Python):
N = int(input())
A = tuple(int(input()) for _ in range(N))
B = tuple(sorted(A, reverse=True))
print(*[B[0] if a != B[0] else B[1] for a in A], sep='\n')

Solution (Python):
n = int(input())
a_list = [int(input()) for i in range(n)]
a_max = max(a_list)
a_max2 = sorted(a_list)[-2]
for i in a_list:
    if i == a_max:
        print(a_max2)
    else:
        print(a_max)

Solution (Python):
n = int(input())
As = [int(input()) for _ in range(n)]
sorted_As = sorted(As, reverse = True)
max_val, next_max_val = sorted_As[0], sorted_As[1]

for A in As:
    if A == max_val:
        print(next_max_val)
    else:
        print(max_val)

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

num1 = num.copy()
num1.sort(reverse=True)
mx1 = num1[0]
mx2 = num1[1]
for i in num:
    if i==mx1:
        ans = mx2
        print(ans)
    else:
        ans = mx1
        print(ans)

Solution (Python):
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def main():
    n = int(input())
    As = [int(input()) for i in range(n)]
    sAs = sorted(As)
    mn1 = sAs[-1]
    mn2 = sAs[-2]
    for i in range(n):
        if As[i] == mn1:
            print(mn2)
        else:
            print(mn1)
    return 

if __name__ == '__main__':
    main()

Solution (Python):
import heapq

num = int(input())
nums = [int(input()) for _ in range(num)]

maxs = heapq.nlargest(2, nums)
for n in nums:
  if n == maxs[0]:
    print(maxs[1])
  else:
    print(maxs[0])

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

max_A = max(A)
semimax_A = max_A if A.count(max_A) > 1 else list(sorted(list(set(A))))[-2]
for i in range(N):
    if A[i] == max_A:
        print(semimax_A)
    else:
        print(max_A)

Solution (Python):

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

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

Solution (Python):
import heapq
n=int(input())
a=[int(input()) for i in range(n)]
b=list(map(lambda x:x*(-1), a))
heapq.heapify(b)
max1=heapq.heappop(b)*(-1)
max2=heapq.heappop(b)*(-1)

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

Solution (Python):
class SegmentTree:
    def __init__(self, array, seg_func, ide_ele):
        n = len(array)
        self.size = 2**(n - 1).bit_length()
        self.seg_func = seg_func
        self.ide_ele = ide_ele
        self.seg = [self.ide_ele] * (2 * self.size - 1)

        for i in range(n):
            self.seg[i + self.size - 1] = array[i]
        for i in range(self.size - 2, -1, -1):
            self.seg[i] = self.seg_func(self.seg[2 * i + 1], self.seg[2 * i + 2])

    def update(self, index, value):
        index += self.size - 1
        self.seg[index] = value
        while index:
            index = (index - 1) // 2
            self.seg[index] = self.seg_func(self.seg[index * 2 + 1], self.seg[index * 2 + 2])

    def query(self, lower_ep, upper_ep):
        if upper_ep <= lower_ep:
            return self.ide_ele
        lower_ep += self.size - 1
        upper_ep += self.size - 2
        res = self.ide_ele
        while (upper_ep - lower_ep) > 1:
            if lower_ep & 1 == 0:
                res = self.seg_func(res, self.seg[lower_ep])
            if upper_ep & 1 == 1:
                res = self.seg_func(res, self.seg[upper_ep])
                upper_ep -= 1

            lower_ep //= 2
            upper_ep = (upper_ep - 1) // 2
        if lower_ep == upper_ep:
            res = self.seg_func(res, self.seg[lower_ep])
        else:
            res = self.seg_func(self.seg_func(res, self.seg[lower_ep]), self.seg[upper_ep])
        return res

N = int(input())
Alist = [int(input()) for _ in range(N)]
S = SegmentTree(Alist, max, 0)
for i in range(N):
    print(max(S.query(0, i), S.query(i + 1, N)))

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
sort_a = sorted(a)
max_a = max(a)
max_ind = 0
for i in range(n):
    if a[i] == max_a: max_ind = i
for i in range(n):
    ans = max_a if i != max_ind else sort_a[-2]
    print(ans)

Solution (Python):

n=int(input())
a=[]
for i in range(n):
    x=int(input())
    a.append(x)
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 = A.copy()
B.sort()
B.reverse()
for i in range(0,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)]
second, first = sorted(A)[-2:]

ans = [second if a==first else first for a in A]
[print(a) for a in ans]

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

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

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
m = max(a)
sec_a = []
flag = True
for i in range(n):
    if a[i] != m:
        sec_a.append(a[i])
    else:
        if flag:
            index = i
        else:
            sec_a.append(a[i])
        flag = False
sec_m = max(sec_a)
for i in range(n):
    if i == index:
        print(sec_m)
    else:
        print(m)

Solution (Python):
import copy
n = int(input())
a = [0]*n

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

b = copy.copy(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())
t = [int(input()) for i in range(n)]
p = 0
q = 0
l = [0]
r = [0]

for i in range(1,n):
    if p < t[i-1]:
        p = t[i-1]
        l.append(p)
    else:
        l.append(p)
    
    if q < t[-i]:
        q = t[-i]
        r.append(q)
    else:
        r.append(q)

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

Solution (Python):
n = int(input())
t = [int(input()) for i in range(n)]
tt = sorted(t)
m = tt[-1]
mm = tt[-2]

for i in range(n):
    if t[i] == m:
        print(mm)
    else:
        print(m)

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

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

max_num = max(A)
a = sorted(A)
second = a[-2]

for i in range(N):
    if A[i] == max_num:
        print(second)
    else:
        print(max_num)

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

Solution (Python):
n = int(input())
a = []
max = int(input())
a.append(max)
second_max = int(input())
a.append(second_max)
if max<second_max:
    second_max,max = max,second_max
for i in range(n-2):
    g = int(input())
    a.append(g)
    if g>max:
        max = g
    elif g>second_max:
        second_max = g

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

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

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

  a=max(A)
  k=A.count(a)
  if k>=2:
    for i in range(N):
      print(a)
  else:
    b=A.index(a)
    for i in range(N):
      if i != b:
        print(a)
      else:
        s=sorted(A)
        print(s[N-2])

if __name__ == "__main__":
    main()

Solution (Python):
import copy

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

  a=max(A)
  k=A.count(a)
  if k>=2:
    for i in range(N):
      print(a)
  else:
    b=A.index(a)
    for i in range(N):
      if i != b:
        print(a)
      else:
        s=sorted(A)
        print(s[N-2])

if __name__ == "__main__":
    main()

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

a=max(A)
k=A.count(a)
if k>=2:
  for i in range(N):
    print(a)
else:
  b=A.index(a)
  for i in range(N):
    if i != b:
      print(a)
    else:
      s=sorted(A)
      print(s[N-2])

Solution (Python):



import sys
input = sys.stdin.readline


def solve(N, A):
    max_idx = A.index(max(A))
    s = sorted(A, reverse=True)

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


N = int(input())





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


solve(N, A)

Solution (Python):
n = int(input())
s = [int(input()) for i in range(n)] 
max_idx = s.index(max(s)) 
s = sorted(s, reverse=True)

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

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

m2, m1  = sorted(A)[-2:]
for i in range(n):
    if A[i]==m1:
        print(m2)
    else:
        print(m1)

Solution (Python):
N = int(input())
A = list(int(input()) for i in range(N))
B = []
count = 0
maxA = max(A)
for i in A:
  if i == maxA:
    count += 1
  else:
    B.append(i)

if B != []:
  maxB = max(B)

if count > 1:
  for i in range(N):
    print(maxA)
else:
  for i in range(N):
    if A[i] == maxA:
      print(maxB)
    else:
      print(maxA)

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

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    left = [A[0]] * N
    right = [A[-1]] * N

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

    for i in range(N):
        if i == 0:
            print(right[1])
        elif i == N-1:
            print(left[N-2])
        else:
            print(max(left[i-1], right[i+1]))


    return



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

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = []
B = []
for i in range(N):
    a = int(input())
    A.append(a)
    B.append(a)
B.sort(reverse = True)
M1 = B[0]
M2 = B[1]

if M1 == M2:
    for i in range(N):
        print(M1)
else:
    for i in range(N):
        if A[i] == M1:
            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]

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

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    left = [A[0]] * N
    right = [A[-1]] * N

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

    for i in range(N):
        if i == 0:
            print(right[1])
        elif i == N-1:
            print(left[N-2])
        else:
            print(max(left[i-1], right[i+1]))


    return



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

if __name__ == '__main__':
    main()

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    left = [A[0]] * N
    right = [A[-1]] * N

    for i in range(1,N):
        if(A[i] > left[i-1]):
            left[i] = A[i]
        else:
            left[i] = left[i-1]

    for i in reversed(range(0,N-1)):
        if(A[i] > right[i+1]):
            right[i] = A[i]
        else:
            right[i] = right[i+1]

    for i in range(N):
        if(i == 0):
            print(right[1])
        elif(i == N-1):
            print(left[N-2])
        else:
            print(max(left[i-1], right[i+1]))


    return



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

if __name__ == '__main__':
    main()

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

sorted_a = sorted(a)
first = sorted_a[-1]
second = sorted_a[-2]
first_place = a.index(first)
for i in range(n):
    if first == second:
        print(first)
    else:
        if i == first_place:
            print(second)
        else:
            print(first)

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    m1 = 0
    m2 = 0
    la = []
    for i in range(n):
        a = ri()
        if m1 <= a:
            m2 = m1
            m1 = a
        elif m2 < a:
            m2 = a
        la.append(a)
    for a in la:
        if a == m1:
            print(m2)
        else:
            print(m1)


if __name__ == '__main__':
    main()

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

B = sorted(A)
maxA = B[-1]
maxA2 = B[-2]


for i in range(N):
    if A[i] == maxA:
        print(maxA2)
    else:
        print(maxA)

Solution (Python):


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

Solution (Python):
n=int(input())
l = [int(input()) for _ in [0] * n]
m = sorted(l)
a = m[-1]
b = m[-2]

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

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

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

s = max(li)
for index, i in enumerate(li):
  if s == li[index]:
    print(new[1])
  else:
    print(new[0])

Solution (Python):
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))
A_sort = list(sorted(A))
for i in range(N):
  print([A_sort[-2], A_sort[-1]][A[i] < A_sort[-1]])

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

b = list(a)
a.sort()

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

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

Solution (Python):

import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

MOD = int(1e09) + 7

def main():
    N = cin.int()
    A = cin.intColsList(N)

    m1,m2 = sorted(A,reverse=True)[0],sorted(A,reverse=True)[1]
    for a in A:
        if a == m1:
            print(m2)
        else:
            print(m1)
    return

if __name__ == "__main__":
    main()

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

for i in range(N):

    if (l[i] != l_sort[-1]):
        print(l_sort[-1])
    else:
        print(l_sort[-2])

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

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

B = A
maxim = max(A)
max_i = A.index(maxim)
B.remove(maxim)
second = max(B)

for i in range(N):
    if (i == max_i):
        print(second)
    else:
        print(maxim)

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

m, m2 = 0,0
for ai in a:
    if ai > m:
        m, m2 = ai, m
    elif ai > m2:
        m2 = ai

for ai in a:
    if ai == m:
        print(m2)
    else:
        print(m)

Solution (Python):
import copy

def main():
    n = int(input())
    num_list = [int(input()) for _ in range(n)]

    tmp = copy.deepcopy(num_list)
    tmp.sort()

    max_first = max(num_list)
    max_second = tmp[n-2]
    
    for num in num_list:
        if num == max_first:
            print(max_second)
            continue
        print(max_first)
        
if __name__ == '__main__':
    main()

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

Solution (Python):
n=int(input())
imax=0
amax=0
amax2=amax
for i in range(n):
    ai=int(input())
    if amax<ai:
        adummy=amax
        amax=ai
        imax=i
        amax2=adummy
    elif amax2<ai:
        amax2=ai
if imax==0:
    print(amax2)
    for i in range(1,n):
        print(amax)
elif imax==n-1:
    for i in range(n-1):
        print(amax)
    print(amax2)
else:
    for i in range(imax):
        print(amax)
    print(amax2)
    for i in range(n-imax-1):
        print(amax)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)[::-1]
mx1 = B[0]
mx2 = B[1]

for i in A:
    if i == mx1:
        print(mx2)
    else:
        print(mx1)

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

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

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

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

Solution (Python):
"""
セグメント木：蟻本P153
できること：a[i]の更新(O(log n))、a[L],...,a[R-1]の最小値など(O(log n))
入力：n,a(配列)
     'ide_ele' : 単位元(min:∞, 和:0, 積:1, gcd:0)
     'segfunc' : return min(x,y) とかにする
出力:'init(a)': 配列aで初期化。O(N)
     'update(k,x)': a[k]をxに変更 O(logN)
     'query(p,q)': [p,q)について "segfunc" したものを返す O(logN)
"""
n=int(input())
a=[int(input()) for i in range(n)]

def segfunc(x,y):
    return max(x,y)


def init(init_val):
    
    for i in range(n):
        seg[i+num-1]=init_val[i]    
    
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res


ide_ele = 0
num =2**(n-1).bit_length()
seg=[ide_ele]*2*num
init(a)
print(query(1,n))
for i in range(1,n):
    print(max(query(0,i),query(i+1,n)))

Solution (Python):
n=int(input())
a=int(input())
f,v,w=0,a,a
a=int(input())
if a>=v:
    w,v,f=v,a,1
else:
    w=a
for i in range(2,n):
    a=int(input())
    if a>=v:
        w,v,f=v,a,i
    elif a>=w:
        w=a
for i in range(n):
    if i==f:
        print(w)
    else:
        print(v)

Solution (Python):
import copy
N=int(input())
A=[int(input()) for i in range(N)]
B = copy.deepcopy(A)
B.sort(reverse=True)
for i in range(N):
  if A[i]==B[0]:
    print(B[1])
  else:
    print(B[0])