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:
n = int(input())
a_l = [ int(input()) for _ in range(n)]

l = []
r = []
l_max = 0
r_max = 0
for i in range(n):
    l_max = max(l_max, a_l[i])
    r_max = max(r_max, a_l[-i-1])
    l.append(l_max)
    r.append(r_max)
for i in range(n):
    if i == 0:
        t_l = 0
    else:
        t_l = l[i-1]
    if i == n-1:
        t_r = 0
    else:
        t_r = r[-i-2]
    print(max([t_l,t_r]))

Submission status: Accepted

Solution code:
import sys
import copy
import math
import itertools
import numpy as np
N = int(input())
A = [int(input()) for c in range(N)]
B = sorted(copy.deepcopy(A),reverse=True)

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

Submission status: Accepted

Solution code:
n = int(input())
first = 0
second = 0
a = []

for i in range(n):	
	a.append(int(input()))
	if first < a[i]:
		first = a[i]
	elif second < a[i]:
		second = a[i]


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

Submission status: Accepted

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

B = sorted(A)
mfirst = B[-1]
msecond = B[-2]

for i in range(N):
    if A[i] == mfirst:
        print(msecond)
    else:
        print(mfirst)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C
# CreatedDate:  2020-09-10 18:27:16 +0900
# LastModified: 2020-09-10 18:35:34 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from collections import Counter


def main():
    n = int(input())
    A = []
    for _ in range(n):
        A.append(int(input()))
    A_cnt = Counter(A)
    flag = 0
    A_max = max(A)
    A_next_max = sorted(A, reverse=True)[1]
    if A_cnt[A_max] > 1:
        flag = 1

    for a in A:
        if a == A_max and flag == 1:
            print(A_max)
        elif a == A_max and flag == 0:
            print(A_next_max)
        else:
            print(A_max)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
max1 = [0,[]]
max2 = 0
for i in range(N):
  a = int(input())
  if a > max1[0]:
    max2 = max1[0]
    max1[0] = a
    max1[1] = [i]
  elif a == max1[0]:
    max1[1].append(i)
  elif a > max2:
    max2 = a

if len(max1[1]) == 1:
  max1_idx = max1[1][0]
  for i in range(0,max1_idx):
    print(max1[0])
  print(max2)
  for i in range(max1_idx + 1 , N):
    print(max1[0])
else:
  for i in range(N):
    print(max1[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 10 00:13:41 2020

@author: liang
"""

N = int(input())
A = [int(input()) for i in range(N)]
B = A.copy()
B.sort()

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

Submission status: Accepted

Solution code:
n=int(input())
A=[int(input()) for _ in range(n)]
sort_A=sorted(A,reverse=True)

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

Submission status: Accepted

Solution code:
N=int(input())
a=[int(input()) for i in range(N)]
sw=True
b=sorted(a,reverse=True)

if b[0]>b[1]:
    max2=b[1]
    max1=b[0]
    sw=False

if sw==True:
    max1=b[0]
    for k in range(N):
        print(max1)
else:
    for k in range(N):
        if a[k]==max1:
            print(max2)
        else:
            print(max1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
I1 = A.index(max(A))
M1 = A.pop(I1)
M2 = max(A)
for j in range (N):
  if j == I1:
    print(M2)
  else:
    print(M1)

Submission status: Accepted

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

import copy
alist_copy = copy.copy(alist)

alist_copy.sort()
max_value = alist_copy[-1]
next_value = alist_copy[-2]
for a in alist:
    if a == max_value:
        print(next_value)
    else:
        print(max_value)

Submission status: Accepted

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

firstVal = max(A)
firstInd = A.index(firstVal)
secondVal = max(A[0:firstInd] + A[firstInd+1:])

for i in range(firstInd):
    print(firstVal)

print(secondVal)

for i in range(N-firstInd-1):
    print(firstVal)

Submission status: Accepted

Solution code:
import sys
import numpy as np
n = int(input())
a = [int(input()) for _ in range(n)]
mx = str(max(a))
mx_id = a.index(int(mx))
a.remove(int(mx))
mx_sub = str(max(a))
ans = '\n'.join(mx if i != mx_id else mx_sub for i in range(n))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
import copy
B = copy.deepcopy(A)
A = sorted(A)
for i in range(N):
    num = B[i]
    if num != A[-1]:
        print(A[-1])
    else:
        print(A[-2])

Submission status: Accepted

Solution code:
class SegTree:
    X_unit = 0
    X_f = max

    def __init__(self, N):
        self.N = N
        self.X = [self.X_unit] * (N + N)

    def build(self, seq):
        for i, x in enumerate(seq, self.N):
            self.X[i] = x
        for i in range(self.N - 1, 0, -1):
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def set_val(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def fold(self, L, R):
        L += self.N
        R += self.N
        vL = self.X_unit
        vR = self.X_unit
        while L < R:
            if L & 1:
                vL = self.X_f(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.X_f(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.X_f(vL, vR)


def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    seg = SegTree(N)
    seg.build(A)

    for i in range(N):
        print(max(seg.fold(0, i), seg.fold(i+1, N)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    L = [0]*(N+1)
    for i, a in enumerate(A, start=1):
        L[i] = max(L[i-1], a)

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

n = int(input())
A = []
#リストAにappend()を使って格納していく
for _ in range(n):
    A.append(int(input()))


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

if max_1 == max_2:
    for _ in range(n):
        print(max_1)
else:
    for i in range(n):
        if A[i] == max_1:
            print(max_2)
        else:
            print(max_1)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    sortA = sorted(A)
    #print(A,sortA)
    ans = sortA[N-1]
    ans2 = sortA[N-2]
    if ans == ans2:
        flag = 1
    else:
        flag = 0

    for i in range(N):
        if flag == 1:
            print(ans)
        else:
            if A[i] == ans:
                print(ans2)
            else:
                print(ans)

main()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
sort_A = sorted(A, reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())


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

B = [0]*N  # Bi = A1~Aiまでのmax
C = [0]*N  # Ci = Ai~ANまでのmax
for i in range(N):
    if i == 0:
        B[i] = A[i]
    else:
        B[i] = max(B[i-1],A[i])
for i in range(N-1,-1,-1):
    if i == N-1:
        C[i] = A[-1]
    else:
        C[i] = max(C[i+1],A[i])

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

Submission status: Accepted

Solution code:
from copy import copy
n, *a = map(int, open(0).read().split())

b = copy(a)
c = copy(a)

for i in range(1, n):
    b[i] = max(b[i], b[i - 1])
for i in range(n - 2, -1, -1):
    c[i] = max(c[i], c[i + 1])
b.append(-1)
c.append(-1)

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
N = int(input())

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

for i, a in enumerate(sorted(A, reverse=True)):
    if i == 0:
        first = a
    elif i == 1:
        second = a
    else:
        break
# print(first, second)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

b=a.copy()

import heapq
#これを加える！！！！
def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()    # raises appropriate IndexError if heap is empty
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt

b = sorted(b,reverse=True)

b1 = _heappop_max(b)
b2 = _heappop_max(b)

for i in range(n):
    if a[i]==b1:
        print(b2)
    else:
        print(b1)

Submission status: Accepted

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

maxa=max(a)
cnt=0

for aa in a:
    if maxa==aa:
        cnt+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n =  int(input())
A = []
for i in range(n):
    A.append(int(input()))
ma = max(A)
ma2 = sorted(A)[-2]
for a in A:
    if a == ma:
        print(ma2)
    else:
        print(ma)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


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

B = [0]*N
C = [0]*N
for i in range(N):
    if i == 0:
        B[i] = A[i]
    else:
        B[i] = max(B[i-1],A[i])
for i in range(N-1,-1,-1):
    if i == N-1:
        C[i] = A[-1]
    else:
        C[i] = max(C[i+1],A[i])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a = [int(input()) for _ in range(n)]
b = max(a)
c = a.index(b)
for i in range(n):
        if i == c:
                print(max(sorted(a)[:n-1]))
        else:
                print(b)

Submission status: Accepted

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

l = sorted(a, reverse=True)
x = l[0]
y = l[1]
for i in a:
  if i != x: print(x)
  else: print(y)

Submission status: Accepted

Solution code:
# C - Exception Handling
N = int(input())
A = [int(input()) for i in range(N)]
m1 = max(A)
i = 0
while A[i]<m1:
    i += 1
m2 = max(A[:i]+A[i+1:])
for j in range(N):
    print(m1 if i!=j else m2)

Submission status: Accepted

Solution code:
a=int(input())
b=[]
for i in range(a):
    b.append(int(input()))
c=max(b)
if b.count(c)>1:
    for i in range(a):
        print(c)
elif b.count(c)==1:
    d=sorted(b)
    d=d[a-2]
    e=b.index(c)
    for i in range(a):
        if i==e:
            print(d)
        else:
            print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
maxa = max(a)
b = a.index(maxa)
for i in range(n):
	if i == b:
		print(max(sorted(a)[:n-1]))
	else:
		print(maxa)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
aa = sorted(a)[:n-1]
maxa = max(a)
b = a.index(maxa)
for i in range(n):
	if i == b:
		print(max(aa))
	else:
		print(maxa)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
B=sorted(A)[:N-1]
m=max(A)
n=A.index(max(A))
for i in range(N):
    if  i!=n:
        print(m)
    else:
        print(max(B))

Submission status: Accepted

Solution code:
a = int(input())
b = [int(input()) for i in range(a)]
c = sorted(b)[-2]
d = max(b)
if b.count(d) >= 2:
  for i in range(a):
    print(d)
else:
  for i in b:
    if i == d:
      print(c)
    else:
      print(d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
max1 = 0
max2 = 0
for i in range(N):
  a = int(input())
  A.append((a,i))
K = A.copy()
K.sort()
m1 = K[-1]
m2 = K[-2]
for i in range(N):
  if i == m1[1]:
    print(m2[0])
  else:
    print(m1[0])

Submission status: Accepted

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

s = max(A)
B.sort()
for a in A:
    if a != s:
        print(s)
    else:
        print(B[-2])

Submission status: Accepted

Solution code:
import math
n = int(input())
A = []
for _ in range(n):
  A.append(int(input()))
B = sorted(A)
m = max(A)  
for a in A:
  if a < m:
    print(m)
  else:
    print(B[-2])

Submission status: Accepted

Solution code:
n = int(input())
al = list(int(input()) for _ in range(n))

al_s = sorted(al)
fir = al_s[-1]
sec = al_s[-2]

for a in al:
    if a == fir:
        print(sec)
    else:
        print(fir)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
al = [int(input()) for _ in range(n)]
al_s = sorted(al, reverse=True)
c = Counter(al_s)
ck = list(c.keys())
cv = list(c.values())


for a in al:
    if a == ck[0]:
        if cv[0] == 1:
            print(ck[1])
        else:
            print(ck[0])
    else:
        print(ck[0])

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
ma=max(a)
if a.count(ma)>1:
  for i in range(n):
    print(ma)

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

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N):
  A.append(int(input()))
s=sorted(A,reverse=True)
for i in range(N):
  if A[i]!=s[0]:
    print(s[0])
  else:
    print(s[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
n=int(input())
li=[int(input()) for i in range(n)]
la=copy.copy(li)
la.sort()
m=la[-1]
for i in li:
    if i==m:
        print(la[-2])
    else:
        print(m)

Submission status: Accepted

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

MAX_1 = sorted(A)[-1]
MAX_2 = sorted(A)[-2]

for i in range(N):
    if A[i] == MAX_1:
        print(MAX_2)
    else:
        print(MAX_1)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
m,m2=-1,-1
  
for i in range(n):
  b=int(input())
  a.append(b)
  m2=max(m2,min(m,b))
  m=max(m,b)

if m==m2:
  for i in range(n):
    print(m)
else:
  l=[m]*n
  l[a.index(m)]=m2
  for i in range(n):
    print(l[i])

Submission status: Accepted

Solution code:
# C - Exception Handling 
# 累積 max の問題
N = int(input())
A = [int(input()) for _ in range(N)]

# lm[i]; iより左にある値の中の最大値。 便宜上 lm[0] = 0 (i = 0 より左はない)
lm = [0]*N
# rm[i]; iより右にある値の中の最大値。 便宜上 lr[n-1] = 0 (i = n-1 より右はない)
rm = [0]*N
# 求める値は max(lm[i], rm[i]) (i = 0,1,2,...,n-1) 

# i = 0 より右にある値は i = 1,2,...,n-1 の n-1 個
for l in range(1,N): 
    lm[l] = max(lm[l-1], A[l-1])
    
# i = N-1 より左にある値は i = 0,1,2,...,n-2 の n-1 個
for r in  range(N-2,0-1,-1):# range(a,b) は [a,b)であることに注意
    rm[r] = max(rm[r+1],A[r+1])


for a in range(N):
    ans = max(lm[a],rm[a])
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

b = sorted(a)
fst = b[-1]
snd = b[-2]

for x in a:
    if x == fst:
        print(snd)
    else:
        print(fst)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input())for i in range(n)]
a_max=max(a)
index_max=a.index(a_max)
for i in range(n):
    if i==index_max:
        print(max(a[:index_max]+a[index_max+1:]))
    else:
        print(a_max)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for n in range(N)]
a1 = sorted(A)[-1]
a2 = sorted(A)[-2]

for a in A:
  if a == a1:
    print(a2)
  else:
    print(a1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, (input() for i in range(N))))
B = sorted(A, reverse=True)
a = B[0]
a1 = B[1]


for x in A:  
  if x != a:
    print(a)
  else:
    print(a1)

Submission status: Accepted

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

B.sort()

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

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# # input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
a1 = 0
a2 = 0
l = []
for i in range(n):
    value = inp()
    if(value > a1):
        a1 = value
    elif(value >= a2):
        a2 = value
    l.append(value)
for t in l:
    if(a1 == t):
        print(a2)
    else:
        print(a1)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
am,amm=0,0
for i in range(n):
    x=int(input())
    if x>am:
        am,amm=amm,x
    elif x>amm:
        amm=x
    a.append(x)
for i in range(n):
    if a[i]==am:
        print(amm)
    else:
        print(am)

Submission status: Accepted

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

max_num=max(l)
max_index=l.index(max(l))

for i in range(n):
  if i!=max_index:
    print(max_num)
  else:
    l.remove(max_num)
    print(max(l))

Submission status: Accepted

Solution code:
import heapq
n = int(input())
a = [int(input()) for i in range(n)]
maxa = max(a)
ma2 = heapq.nlargest(2, a)[1]
ca = a.count(maxa)
if ca >= 2:
    for i in range(n):
        print(maxa)
else:
    for i in range(n):
        if a[i] == maxa:
            print(ma2)
        else:
            print(maxa)

Submission status: Accepted

Solution code:
n = int(input())
num = [int(input()) for i in range(n)]
num_s =sorted(num)    
f,s = num_s[-1],num_s[-2]

for j in num:
    if j == f:
        print(s)
    else:
        print(f)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_lst = [int(input()) for _ in range(n)]

    left = [0 for _ in range(n)]
    right = [0 for _ in range(n)]

    biggest = 0
    for i in range(n):
        if biggest < a_lst[i]:
            biggest = a_lst[i]
        left[i] = biggest

    biggest = 0
    for i in range(n - 1, -1, -1):
        if biggest < a_lst[i]:
            biggest = a_lst[i]
        right[i] = biggest

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_lst = [int(input()) for i in range(n)]
    a_lst_descend = sorted(a_lst, reverse=True)

    max_value = a_lst_descend[0]
    second_value = a_lst_descend[1]

    for a in a_lst:
        if a == max_value:
            print(second_value)
        else:
            print(max_value)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_lst = [(int(input()), i + 1) for i in range(n)]
    a_lst_descend = sorted(a_lst, reverse=True)

    max_value = a_lst_descend[0][0]
    max_index = a_lst_descend[0][1]
    second_value = a_lst_descend[1][0]

    for i in range(1, n + 1):
        if i == max_index:
            print(second_value)
        else:
            print(max_value)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
L =[]
l2 = []
for _ in range(N):
   L.append(int(input()))
ma = max(L)

if L.count(ma) > 1:
  [print(ma) for _ in range(N)]
else:  
  for i in L:
    if i == ma:
      print(sorted(L)[-2])
    else:
      print(ma)

Submission status: Accepted

Solution code:
l=[]
n=int(input())
for _ in range(n):
    a=int(input())
    l.append(a)
maxl=max(l)
a=l.count(maxl)
if a>=2:
    for _ in range(n):
        print(maxl)
else:
    for i in range(n):
        if l[i]!=maxl:
            print(maxl)
        else:
            ll=l.copy()
            l.remove(maxl)
            print(max(l))
            l=ll

Submission status: Accepted

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

A_sorted = list(sorted(A))
m = A_sorted[-1]
s = A_sorted[-2]
if m == s:
    for i in range(N): print(m)
else:
    for i in range(N):
        if m != A[i]: print(m)
        else: print(s)

Submission status: Accepted

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

sort_A = sorted(A, reverse=True)
max_value = sort_A[0]
second_value = sort_A[1]

ans = []

for a in A:
    if a < max_value:
        ans.append(max_value)
    elif a == max_value:
        ans.append(second_value)

print(*ans, sep='\n')

Submission status: Accepted

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

amax = max(A)
nmax = A.count(amax)
if nmax == 1:
    B = []
    for i in range(N):
        if A[i] != amax:
            B.append(A[i])
    asec = max(B)


for i in range(N):
    if A[i] == amax:
        if nmax > 1:
            ans = amax
        else:
            ans = asec
    else:
        ans = amax
    print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    a_copy = sorted(a)
    a_max = a_copy[-1]
    a_second = a_copy[-2]
    for i in range(n):
        if a[i] == a_max:
            print(a_second)
        else:
            print(a_max)


main()

Submission status: Accepted

Solution code:
N = int(input())
A = []
p = 0
q = 0
for i in range(N):
  a = int(input())
  A.append(a)
  if a > p:
    q = p
    p = a
  elif a > q:
    q = a
for i in range(N):
  if A[i] == p:
    print(q)
  else:
    print(p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
Row = int(input())
List = []
for i in range (Row):
  List.append(int(input()))
NewList = copy.deepcopy(List)
NewList.sort()
high = NewList[Row-1]
secondHigh = NewList[Row-2]
for i in range(Row):
  if List[i] == high:
    print(secondHigh)
  else:
    print(high)

Submission status: Accepted

Solution code:
from collections import Counter

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

def solve(n,a):
  d = Counter(a)
  if len(d) == 1:
    return a
  d_lst = list(sorted(d.keys()))
  first = d_lst[-1]
  second = d_lst[-2]
  if d[first] != 1:
    return [first]*n
  else: # first は一つだけ
    return [second if a[i] == first else first for i in range(n)]

print(*solve(N,a),sep="\n")

Submission status: Accepted

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

copy =  num_list.copy()
copy.sort()
maximum = copy[-1]
second = copy[-2]

for i in num_list:
    if i == maximum:
        print(second)
    else:
        print(maximum)

Submission status: Accepted

Solution code:
N = int(input())
N_List = []
for i in range(N):
    N_List.append(int(input()))
NMax = max(N_List)
if N_List.count(NMax) == 1:
    NMax2 = sorted(N_List)[-2]
    for i in N_List:
        print((NMax,NMax2)[i==NMax])
else:
    for i in N_List:
        print(NMax)

Submission status: Accepted

Solution code:
n = int(input())
max_1 = 0
max_2 = 0
a_list = []
for i in range(n):
    a = int(input())
    a_list.append(a)
    if max_1 < a:
        max_2 = max_1
        max_1 = a
    elif max_2 < a:
        max_2 = a
#print(max_1,max_2)
for i in range(n):
    if a_list[i] == max_1:
        print(max_2)
    else:
        print(max_1)

Submission status: Accepted

Solution code:
n = int(input())
alist = []
for i in range(n):
    a = int(input())
    alist.append(a)
aalist = sorted(alist)
ans = 0
for i in range(n):
    ans = aalist[-1]
    if alist[i] == aalist[-1]:
        ans = aalist[-2]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N = int(input())
A = [int(input()) for _ in range(N)]
m = max(A)
c = 0
for a in A:
    if a == m:
        c += 1
if c == 1:
    mm = 0
    for i in range(N):
        a = A[i]
        if a == m:
            continue
        mm = max(a,mm)
    for i in range(N):
        a = A[i]
        if a == m:
            print(mm)
        else:
            print(m)
else:
    for i in range(N):
        print(m)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
sort_a = sorted(a, reverse=True)
amax = sort_a[0]
asecond = sort_a[1]
for i in range(n):
    tmp = a[i]
    if tmp == amax:
        print(asecond)
    else:
        print(amax)

Submission status: Accepted

Solution code:
import heapq
N = int(input())
A = [0]*N

maxVal = -1
for i in range(N):
    A[i] = int(input())
    maxVal = max(maxVal, A[i])

secondMax = -1
maxIndex = A.index(maxVal)
A[maxIndex] = -1
secondMax = max(A)
A[maxIndex] = maxVal

for i in range(N):
    if A[i] == maxVal:
        print(secondMax)
    else:
        print(maxVal)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [[int(input()), i] for i in range(N)]
A.sort(reverse=True)

ex = A[0]
nx = A[1]

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

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
d=sorted(l)
x=d[-2]
m=max(l)
for i in range(n):
    if(l[i]==m):
        l[i]=x
    else:
        l[i]=m
for i in l:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
max_n = -1
semi_max_n = -1
max_idx = -1
for i in range(n):
    x = int(input())
    if x > max_n:
        max_n = x
        max_idx = i
    elif x >= semi_max_n:
        semi_max_n = x
for i in range(n):
    if i == max_idx:
        print(semi_max_n)
    else:
        print(max_n)

Submission status: Accepted

Solution code:
n=int(input())
a=0
b=0
l=[]
for i in range(n):
  c=int(input())
  l.append(c)
a=max(l)
L=sorted(l)
b=L[-2]
for i in range(n):
  if l[i]==a:
    print(b)
  else:
    print(a)

Submission status: Accepted

Solution code:
N = int(input())

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

max = sorted(A)[-1]
semi_max = sorted(A)[-2]

if max != semi_max:
    ex = A.index(max)
    
    for i in range(0,ex):
        print(max)
    
    print(semi_max)
    
    for i in range(ex+1,N):
        print(max)

else:
    for i in range(N):
        print(max)