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



Sample input:
3
1
4
3


Sample output:
4
3
4

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    非再帰
    segment tree
    '''

    def __init__(self, _n, func, init=float('inf')):
        '''
        _n->配列の長さ
        func:func(a,b)->val,　func=minだとRMQになる
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(_n-1).bit_length()
        self.inf = init
        self.data = [init]*(2*self.n)
        self.func = func

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def build(self):
        '''
        setの後に一斉更新
        '''
        for k in reversed(range(self.n-1)):
            self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

        while k > 0:
            k = (k-1)//2
            self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])

    def query(self, l, r):
        '''
        [l,r)のfuncを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.inf
        while L < R:
            if R & 1:
                R -= 1
                ret = self.func(ret, self.data[R-1])
            if L & 1:
                ret = self.func(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


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

Seg = SegmentTree(N, max, init=0)
for i, a in enumerate(A):
    Seg.set(i, a)

Seg.build()

for i in range(N):
    print(max(Seg.query(0, i), Seg.query(i+1, N)))

Submission status: Accepted

Solution code:
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

SortedA = sorted(A)
A_first = SortedA[N-1]
A_second = SortedA[N-2]

for i in range(N):
    if A[i] == A_first:
        print(A_second)
    else:
        print(A_first)

Submission status: Accepted

Solution code:
N = int(input())
L = []
L2 = []
for _ in range(N):
    x = int(input())
    L.append(x)
    L2.append(x)

L2 = sorted(L2)
m1 = L2[-1]
m2 = L2[-2]
for x in L:
    if x == m1:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(int(input()) for _ in range(N))
asc, des = [0], [0]
for i in range(N-1):
    asc.append(max(asc[-1], A[i]))
    des.append(max(des[-1], A[-1-i]))

for i in range(N):
    print(max(asc[i], des[-1-i]))

Submission status: Accepted

Solution code:
n = int(input())
l = [int(input()) for i in range(n)]
la = sorted(l, reverse=True)
m1 = la[0]
m2 = la[1]
for i in l:
    if i == m1:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

A_ = sorted(A, reverse=True)
first, second = A_[0], A_[1]

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

Submission status: Accepted

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

A_ = sorted(A.copy(), reverse=True)
max_num = [A_[0], A_[1]]

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
 
 
left_max = [-1] * N
right_max = [-1] * N
all = sum(A)
left_max[0] = A[0]
right_max[-1] = A[-1]
for i in range(1, N):
        left_max[i] = max(left_max[i-1], A[i])
for i in reversed(range(0, N-1)):
        right_max[i] = max(right_max[i+1], A[i])
 

print(right_max[1])
for i in range(1,N-1):
        print(max(left_max[i-1], right_max[i+1]))
print(left_max[-2])

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline())
a = [int(stdin.readline()) for _ in range(n)]
b = a.copy()
m1 = max(b)
b.remove(m1)
m2 = max(b)
for i in range(n):
  if a[i] == m1:
    print(m2)
  else:
    print(m1)

Submission status: Accepted

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

maxN = 0 
secmax = 0

for i in range(N):
  x = int(input())
  A.append(x)
  if maxN <= x:
    secmax = maxN
    maxN = x
  elif secmax <= x:
    secmax = x
  
for a in A:
  print(secmax if maxN == a else maxN)

Submission status: Accepted

Solution code:
import copy

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

A_original = copy.deepcopy(A)
A.sort()

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

Submission status: Accepted

Solution code:
n = int(input())
no1 = -1
no1_i = -1
no2 = -2
no2_i = -1
S = []
for _ in range(n):
    S.append(int(input()))
 
for i in range(n):
  if S[i] > no1:
    no1_i =i
    no1 = S[i]
  elif S[i] > no2:
    no2_i = i
    no2 = S[i]
      
for i in range(n):
  if no1_i == i:
    print(no2)
  else:
    print(no1)

Submission status: Accepted

Solution code:
n = int(input())
lis = []
for _ in range(n):
  lis.append(int(input()))
lis_up = [-1]*n
lis_down = [-1]*n

for i in range(n):
  if i == 0:
    lis_up[i] = lis[i]
  else:
    lis_up[i] = max(lis_up[i-1],lis[i])

for i in range(n-1,-1,-1):
  if i == n-1:
    lis_down[i] = lis[i]
  else:
    lis_down[i] = max(lis_down[i+1],lis[i])
    
for i in range(n):
  if i == 0:
    print(lis_down[i+1])
  elif i == n-1:
    print(lis_up[i-1])
  else:
    print(max(lis_up[i-1],lis_down[i+1]))

Submission status: Accepted

Solution code:
from sys import stdin

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

first = 0
second = -1

for k in range(N):
    if A[k] > first:
        second = first
        first = A[k]
    elif second < A[k] <= first:
        second = A[k]

for l in range(N):
    if A[l] == first:
        print(second)
    else:
        print(first)

Submission status: Accepted

Solution code:
n = int(input())
a = []
num = []
prem = 0
m = 0
j = 0
for i in range(n):
    a.append(int(input()))
for i in range(n):
    if a[i] > m:
        prem = m
        m = a[i]
        num = []
        num.append(i)
    elif a[i] == m:
        num.append(i)
    elif a[i] > prem:
        prem = a[i]
for i in range(n):
    if len(num) > 1:
        print(m)
    elif j < len(num) and i == num[j]:
        print(prem)
        j += 1
    else:
        print(m)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys, math, bisect, random, itertools
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n = II()
    print(n*n*3)
    return

#B
def B():
    n, d = LI()
    print((n-1)//(2*d+1)+1)
    return

#C
def C():
    n = II()
    a = IR(n)
    b = a[::1]
    b.sort()
    for i in range(n):
        if a[i] == b[-1]:
            print(b[-2])
        else:
            print(b[-1])
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

sort = sorted(a)
m1 = sort[-1]
m2 = sort[-2]
if m1 == m2:
  for i in range(N):
    print(m1)
else:
  j = a.index(m1)
  for i in range(N):
    if i == j:
      print(m2)
    else:
      print(m1)

Submission status: Accepted

Solution code:
t=int(input())
l=[]
b=0
c=0
for j in range(t):
    a=int(input())
    l.append(a)
    if(a>b):
        b=a
    elif(a>c):
        c=a
for i in l:
    if(i!=b):
        print(b)
    else:
        print(c)

Submission status: Accepted

Solution code:
a = int(input())
ar = [int(input()) for i in range(a)]
br = sorted(ar)
am = max(ar)
bm = br[-2]
for r in ar:
    if r == am:
        print(bm)
    else:
        print(am)

Submission status: Accepted

Solution code:
N = int(input())
l = [int(input()) for i in range(N)]
l2 = sorted(l)
m = l2[-1]
m2 = l2[-2]


for i in range(N):
    if l[i] != m:
        print(m)
    else:
        print(m2)

Submission status: Accepted

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

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


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

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

Submission status: Accepted

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

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

M = max(A)
    
if A.count(max(A)) > 1:
    for i in range(N):
        print(M)
else:
    for i in range(N):
        if A[i] != M:
            print(M)
        else:
            print(sorted(A)[-2])

Submission status: Accepted

Solution code:
a_max=0
a_max_next=0
A=[]
N=int(input())

for i in range(N):
    A.append(int(input()))
    if A[i]>a_max:
        a_max=A[i]
    elif A[i]>=a_max_next:
        a_max_next=A[i]

for i in range(N):
    if A[i]==a_max:
        print(a_max_next)
    else:
        print(a_max)

Submission status: Accepted

Solution code:
p=print;n,*a=map(int,open(0));r=range(n);m=max(a);o=sorted(a)[-2];[[p(m)for _ in r]if m==o else[p(o)if a[i]==m else p(m)for i in r]]

Submission status: Accepted

Solution code:
N=int(input())
A=[]
first=0
first_num=0
second=0
second_num=0
for i in range(N):
    a=int(input())
    A.append(a)
    if a>first:
        first=a
        first_num=i+1
    elif a>second:
        second=a
        second_num=i+1

for j in range(N):
    if A[j]==first:
        print(second)
    else:
        print(first)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N = int( input())
    A = [ int( input()) for _ in range(N)]
    B = [0]*N
    C = [0]*N
    B[0] = A[0]
    C[N-1] = A[N-1]
    for i in range(N-1):
        B[i+1] = max(B[i], A[i+1])
        C[N-2-i] = max( C[N-1-i], A[N-2-i])
    print(C[1])
    for i in range(1, N-1):
        print( max(B[i-1], C[i+1]))
    print(B[N-2])
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
array = [int(input()) for x in range(n)]
sorted_array = sorted(array)
max_array1 = sorted_array[-1]
max_array2 = sorted_array[-2]

for i in array:
    if i == max_array1:
        print(max_array2)
    else:
        print(max_array1)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
B = sorted(A)
B_M = max(B)
B_M_1 = max(B[:n-1])
for i in range(n):
    if A[i] == B_M:
       print(B_M_1)
    else:
        print(B_M)

Submission status: Accepted

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

sorted_list = sorted(a,reverse=True)
first = sorted_list[0]
secend = sorted_list[1]

for num in a:
    if num != first:
        print(first)
    else:
        print(secend)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

A2=[0]+list(accumulate(A, max))+[0]
# A3=[0]+list(accumulate(A[::-1], max))[::-1]+[0]
A3=[0]*(N+2)
A3[N]=A[-1]
for i in range(N-2, -1, -1):
    A3[i+1]=max(A3[i+2], A[i])

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

A2=[0]+list(accumulate(A, max))+[0]
# A3=[0]+list(accumulate(A[::-1], max))[::-1]+[0]
A3=[0]*N
A3[-1]=A[-1]
for i in range(N-2, -1, -1):
    A3[i]=max(A3[i+1], A[i])
A3=[0]+A3+[0]

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
v = []
f = []
for i in range(n):
	x = int(input())
	v.append(x)
	f.append(x)
f.sort()
for i in range(n):
	if v[i] == f[n-1]:
		print(str(f[n-2]) + ' ', end='')
	else:
		print(str(f[n-1]) + ' ', end='')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

A2=[0]+list(accumulate(A, max))+[0]
A3=[0]+list(accumulate(A[::-1], max))[::-1]+[0]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())
B = A[:]
B.sort()
b0 = B[-1]; b1 = B[-2]
for i in range(N):
    print(b1 if b0 == A[i] else b0)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
MAX = max(a)
MAX_num = a.count(MAX)
if MAX_num == 1:
  MAX_index = a.index(MAX)
  a.sort()
  for i in range(n):
    if i == MAX_index:
      print(a[-2])
    else:
      print(MAX)
else:
  for i in range(n):
    print(MAX)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
n=int(raw_input())
m1,m2=0,0
i1=-1
for i in xrange(n):
    m=int(raw_input())
    if m>m1:
        m2,m1=m1,m
        i1=i
    elif m>m2:
        m2=m
for i in xrange(n):
    if i==i1:
        print m2
    else:
        print m1

Submission status: Accepted

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

m = max(a)
idxs = [i for i, v in enumerate(a) if v == m]

if len(idxs) > 1:
    for i in range(n):
        print(m)

if len(idxs) == 1:
    idx = idxs[0]
    a.pop(idx)
    m2 = max(a)
    for i in range(n):
        if idx == i:
            print(m2)
        else:
            print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd
import time


def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    N= int(sys.stdin.readline())
    A = list(map(int,sys.stdin.readlines()))
    l = sorted(A)
    m1 = l[-1]
    m2 = l[-2]
    for i in range(N):
        if A[i] == m1:
            print(m2)
        else:
            print(m1)

Submission status: Accepted

Solution code:
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


N = int(input())
A = []
m1 = 0
m2 = 0

for _ in range(N):
    a = int(input())
    if a >= m1:
        m2 = m1
        m1 = a
    elif a > m2:
        m2 = a
    A.append(a)

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

pass

Submission status: Accepted

Solution code:
N = int(input())
t = []
for i in range(N):
    t.append(int(input()))
l = t.copy()
l.sort()

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

Submission status: Accepted

Solution code:
cnt = int(input())
lst = [int(input()) for i in range(cnt)]
lst2 = sorted(lst)
for i in lst:
  if i == lst2[-1]:
    print(lst2[-2])
  else:
    print(lst2[-1])

Submission status: Accepted

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

b = list(reversed(sorted(a)))

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
p=a[::]
m=max(a)
a.remove(m)
m2=max(a)
for i in p:
  if i==m:
    print(m2)
  else:
    print(m)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = []
    for n in range(N):
        A.append(int(input()))

    A_cp = sorted(A)
    max1_A = A_cp.pop(-1)
    max2_A = max(A_cp)
    for a in A:
        if a == max1_A:
            print(max2_A)
        else:
            print(max1_A)
            
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
s = A.index(max(A))
x = max(A)
A.sort()
y = A[-2]
ans = []
for i in range(N):
  ans.append(x)
  
ans[s] = y

for i in ans:
  print(i)

Submission status: Accepted

Solution code:
# ABC134
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = list(map(int, [input() for _ in range(n)]))
    SA = sorted(A)
    ma, ma2 = SA[-1], SA[-2]
    ans = (ma if i != ma else ma2 for i in A)
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC134
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = list(map(int, [input() for _ in range(n)]))
    SA = sorted(A)
    ma, ma2 = SA[-1], SA[-2]
    ans = [ma if i != ma else ma2 for i in A]
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC134
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    A = list(map(int, [input() for _ in range(n)]))
    SA = sorted(A)
    ma, ma2 = SA[-1], SA[-2]
    ans = []
    for i in A:
        if i != ma:
            ans += [ma]
        else:
            ans += [ma2]
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

p=[0]*(2*n)
q=[0]*(2*n)
m=0
for i in range(n+2):
    m=max(a[i],m)
    p[i]=m
m=0
for i in reversed(range(n+2)):
    m=max(a[i],m)
    q[i]=m
for i in range(1,n+1):
    print(max(p[i-1],q[i+1]))

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
b = copy.deepcopy(a)
b.sort()
firint = b[-1]
secint = b[-2]

for i in a:
    if i == firint:
        print(secint)
    else:
        print(firint)

Submission status: Accepted

Solution code:
import sys
import math
import copy
a = int(input());
b = [int(input()) for i in range(a)];
c = 0;
first = max(b);
g = b.copy();
h = g.index(first);
del g[h];
second = max(g);

for i in range(a):
    if first > b[i]:
        print(first);
    elif first == b[i]:
        print(second);

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
data = [int(input()) for k in range(n)]
m1,m2 = sorted(data, reverse=1)[:2]
b = [m2 if x==m1 else m1 for x in data]
print('\n'.join(map(str,b)))

Submission status: Accepted

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

b = sorted(a, reverse = True)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
inputs = [int(input()) for _ in range(N)]

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
k = []
for i in range(n):
    c = int(input())
    a.append(c)
    k.append(c)
a = sorted(a)
m = a[-1]
l = a[-2]
for i in range(len(k)):
    if(m!=k[i]):
        print(m)
    else:
        print(l)

Submission status: Accepted

Solution code:
N = int(input())

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

new_A = sorted(A, reverse=True)

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

L = [0] + list(accumulate(A, max))
R = [0] + list(accumulate(reversed(A), max))
R.reverse()

for l, r in zip(L[:-1], R[1:]):
    print(max(l, r))

Submission status: Accepted

Solution code:
N = int(input())
L=[]
i = 0
s = 0
M2 = 0
a = 0
for i in range (0,N):
    L.append(int(input()))
M = L[0]
for s in range(1,N):
    if  L[s] > M:
        M = L[s]
for s in range(0,N):
    if L[s] == M:
        a += 1
    else:
        if L[s] > M2:
            M2 = L[s]
for i in range(0,N):
    if L[i] == M and a < 2:
        print(M2)
    else:
        print(M)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    max_i = 0
    sec_i = 0
    m = 0
    s = 0
    for ind, val in enumerate(A):
        if val > m:
            s = m
            sec_i = max_i
            m = val
            max_i = ind
        elif s < val <= m:
            s = val
            sec_i = ind
    for i in range(N):
        if i == max_i:
            print(s)
        else:
            print(m)

main()

Submission status: Accepted

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

B = sorted(A)
f, s = B[-1], B[-2]

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

Submission status: Accepted

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

L_max=[0]*(N+1)
R_max=[0]*(N+1)

L=0
for i in range(N):
    L=max(L,a[i])
    L_max[i]=L

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

for j in range(N):
    print(max(L_max[j-1],R_max[N-j-2]))

Submission status: Accepted

Solution code:
N = int(input())
num_line = [int(input()) for _ in range(N)]
sort_line = sorted(num_line)
for i in num_line:
    if i == sort_line[N-1]:
        print(sort_line[N-2])
    else:
        print(sort_line[N-1])

Submission status: Accepted

Solution code:
import copy
N = int(input())
S = []
for i in range(N):
    S.append(int(input()))

B=copy.copy(S)
B.sort()
if N>2:
	C=int(B[N-1])
	D=int(B[N-2])
	for i in S:
		if int(i)>=C:
			print(D)
		else:
			print(C)

else:
	print(S[1])
	print(S[0])

Submission status: Accepted

Solution code:
def c_exception_handling(N, A):
    max_from_left = [0] * N
    max_from_right = [0] * N

    for i in range(1, N):
        max_from_left[i] = max(A[i - 1], max_from_left[i - 1])
        max_from_right[N - i - 1] = max(A[N - i], max_from_right[N - i])

    ans = [max(a, b) for a, b in zip(max_from_left, max_from_right)]
    return '\n'.join(map(str, ans))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [ int(input()) for x in range(n) ] 

b = sorted(a, reverse = True)
maximum = b[0]

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a =  [int(input()) for i in range(n)]
max_a_num = a.count(max(a))
b = sorted(a, reverse=True)
m1, m2 = b[0], b[1]
for i in range(n):
  if max_a_num == 1:
    if a[i] == m1:
      print(m2)
    else:
      print(m1)
  else:
    print(m2)

Submission status: Accepted

Solution code:
N = int(input())
one = two = 0
A = []
for i in range(N):
    a = int(input())
    if a >= one:
        one,two = a,one
    elif a >= two:
        two = a
    A.append(a)

for i in range(N):
    if A[i] == one:
        print(two)
    else:
        print(one)

Submission status: Accepted

Solution code:
n = int(input())
a = [ int(input()) for x in range(n) ] 

b = sorted(a, reverse = True)
maximum = b[0]

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n = int(input())

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

a_sorted = sorted(a, reverse=True)

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

    print(a_sorted[0])

Submission status: Accepted

Solution code:
n = int(input())

a_max1 = 0
a_max2 = 0
a_max1_index = 0
for i in range(n):
    a_i = int(input())
    if a_max1 <= a_i:
        a_max2 = a_max1
        a_max1 = a_i
        a_max1_index = i
    elif a_max2 <= a_i: 
        a_max2 = a_i

for i in range(n):
    if i == a_max1_index:
        print(a_max2)
    else:
        print(a_max1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]
B.sort(reverse=True)
# print(A)
fmax = B[0]
smax = B[1]
for i in A:
  if i == fmax:
    print(smax)
  else:
    print(fmax)

Submission status: Accepted

Solution code:
N = int(input())
 
A = []
max1 = 0
max2 = 0
for _ in range(N):
    ai = int(input())
    if max1 < ai:
        max1 = ai
    elif max2 < ai:
        max2 = ai
 
    A.append((ai, max2, max1))

for i in range(N):
    ai, _, _ = A[i]


    if max1 == ai:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(input()) for _ in range(n)]
first = 0
second = 0
for a in arr:
  if a >= first:
    second = first
    first = a
  elif a > second:
    second = a
for a in arr:
  print(first if a != first else second)

Submission status: Accepted

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

left = [0] * n
for i in range(n):
    if i == 0:
        left[i] = a[i]
    else:
        left[i] = max(left[i - 1], a[i])

right = [0] * n
for i in range(n - 1, -1, -1):
    if i == n - 1:
        right[i] = a[i]
    else:
        right[i] = max(right[i + 1], a[i])

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

Submission status: Accepted

Solution code:
n=int(input())
max1 = 0
max2 = 0
A=[]
for _ in range(n):
    num = int(input())
    if num == max(num, max1):
        max2 = max1
        max1 = num
    elif num == max(num, max2):
        max2 = num
    A.append(num)
for a in A:
    if a == max1:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
a=[int(input()) for _ in [0]*int(input())]
m1,m2=sorted(a, reverse=True)[:2]
b=[m2 if x==m1 else m1 for x in a]
print('\n'.join(map(str,b)))

Submission status: Accepted

Solution code:
def c_exception_handling(N, A):
    max_from_left = [0]
    max_from_right = []

    tmp = 0
    for a in A[:N - 1]:
        if a > tmp:
            tmp = a
        max_from_left.append(tmp)
    tmp = 0
    for a in A[N:0:-1]:
        if a > tmp:
            tmp = a
        max_from_right.append(tmp)

    max_from_right = max_from_right[::-1] + [0]
    ans = [max(a, b) for a, b in zip(max_from_left, max_from_right)]
    return '\n'.join(map(str, ans))

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