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())
lis = []
for i in range(N):
    lis.append(int(input()))
max_value = max(lis)
max_index = lis.index(max_value)
max_count = lis.count(max_value)
next_max = sorted(lis)[-2]
if max_count ==1:
    for i in range(N):
        if i == max_index:
            print(next_max)
        else:
            print(max_value)
else:
    for i in range(N):
            print(max_value)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
an=[]
for _ in range(n):
    an.append(int(input()))
bn = sorted(an, reverse=True)
s = bn[0]
t= bn[1]
for i in an:
    if i == s:
        print(t)
    else:
        print(s)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for _ in range(n):
    a.append(int(input()))
s=0
for i in range(n):
    if s<=a[i]:
        s=a[i]
t=0
for i in range(n):
    if s==a[i]:
        t+=1
if t>=2:
    for i in range(n):
        print(s)
else:
    u=0
    for i in range(n):
        if u<=a[i] and a[i]!=s:
            u=a[i]
    for i in range(n):
        if a[i]==s:
            print(u)
        else:
            print(s)

Submission status: Accepted

Solution code:
n = int(input())

a = [int(input()) for _ in range(n)]
l_new = sorted(a,reverse=True)
m = max(a)

b = a.count(m)

if b >= 2 :
    for i in range(n) :
        print(m)

else :
    for i in range(n) :
        if a[i] != m :
            print(m)
        else :
            print(l_new[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

first = (0, 0)
second = 0
for i in range(N):
    A = int(input())
    if first[1] < A:
        first = (i, A)
    elif second < A:
        second = A

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

Submission status: Accepted

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

max_1st = max(a)
max_2nd = sorted(a)[-2]
for i in range(n):
    if a[i] == max_1st:
        print(max_2nd)
    else:
        print(max_1st)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
big=max(A)
bigsize=A.count(big)
if bigsize==1:
    a=sorted(A)
    next=a[-2]
    for i in range(N):
        if A[i]==big:
            print(next)
        else:
            print(big)
else:
    for i in range(N):
        print(big)

Submission status: Accepted

Solution code:
n=int(input())
import copy 
l=[int(input()) for i in range(n)]
a=copy.deepcopy(l)
l.sort(reverse=True)
ma=l[0]
sec=l[1]
for i in a:
  if i==ma:
    print(sec)
  else:
    print(ma)

Submission status: Accepted

Solution code:
n,*l=map(int,open(0))
s=sorted(l)
for i in l: print(s[(i<s[-1])-2])

Submission status: Accepted

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

a_max = max(a)
a_second = sorted(a, reverse=True)[1]

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

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    a_max = max(A)
    max_count = A.count(a_max)
    for a in A:
        if a != a_max:
            print(a_max)
        elif max_count > 1:
            print(a_max)
        else:
            B = A[:]
            B.remove(a_max)
            print(max(B))

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())
    
    B = [(i, a) for i, a in enumerate(A)]
    B.sort(key=itemgetter(1))
    ans = [0] * N
    for i, a in B[:-1]:
        ans[i] = B[-1][1]
        
    ans[B[-1][0]] = B[-2][1]
    
    print(*ans, sep='\n')
    
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

B=sorted(A)
count=B[-1]==B[-2]
M=B[-1]
M2=B[-2]
for a in A:
    if count:
        print(M)
    elif a==M:
        print(M2)
    else:
        print(M)

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(N):
  A.append(int(input()))
B=sorted(A)
B=B[:len(B)-1]
C=[max(A)]*N
C[A.index(max(A))]=max(B)
for j in range(N):
  print(C[j])

Submission status: Accepted

Solution code:
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 j in range(n):
  if a[j] == b[0]:
    print(b[1])
  else:
    print(b[0])

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A,reverse=True)
x = B[1]
y = B[0]
m = A.index(max(A))

for i in range(N):
  if i == m:
    print(x)
  else:
    print(y)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n = i()
a = [i() for _ in range(n)]

b = sorted(a)
ans1 = b[-1]
ans2 = b[-2]

for i in range(n):
	if a[i] == ans1 :
		print(ans2)
	else:
		print(ans1)

Submission status: Accepted

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

fromL=[0 for _ in range(N)]
for i in range(1,N):
  fromL[i]=max([fromL[i-1],ls[i-1]])

fromR=[0 for _ in range(N)]
for i in range(1,N):
  fromR[N-1-i]=max([fromR[N-i],ls[N-i]])

for i in range(N):
  print(max([fromL[i],fromR[i]]))

Submission status: Accepted

Solution code:
n = int(input())
a = list()
for x in range(n):
    a.append(int(input()))

copy_a = sorted(a, reverse=True)

A_Max = copy_a[0]
A_Second = copy_a[1]

for x in range(n):
    if a[x] == A_Max:
        print(A_Second)
    else:
        print(A_Max)

Submission status: Accepted

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

fromleft = [0]*N
fromright = [0]*N
fromleft[0] = A[0]
fromright[-1] = A[-1]

for i in range(1,N):
    fromleft[i] = max(A[i],fromleft[i-1])
    fromright[-(i+1)] = max(A[-(i+1)],fromright[-i])

#print(fromleft)
#print(fromright)

for i in range(N):
    print(max(fromleft[i],fromright[i]))

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
maxi = max(A)
ind = A.index(maxi)
A[ind] = 0
ans = [maxi]*N
nmaxi = max(A)
ans[ind] = nmaxi
for i in ans:
    print(i)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
  a_=int(input())
  a.append(a_)
  
maxa=max(a)
s=sorted(a)
s.reverse()
if s[1]==maxa:
  [print(maxa) for i in range(n)]
  
else:
  for i in range(len(a)):
    if a[i]==maxa:
      a[i]=s[1]
      
    else:
      a[i]=maxa
  
  [print(i) for i in a]

Submission status: Accepted

Solution code:
N = int(input())
lis = [int(input()) for _ in range(N)]
lis_c = lis.copy()

mx1 = max(lis_c)
lis_c.remove(mx1)
mx2 = max(lis_c)

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

Submission status: Accepted

Solution code:
n = int(input())

a = [int(input()) for i in range(n)]
a_max = max(a)
a2 = a[:]
a2.remove(a_max)
a_second_max = max(a2)

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

Submission status: Accepted

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

sa = sorted(a)
amx = sa[-1]
amx2 = sa[-2]

for ai in a:
  print(amx if ai != amx else amx2)

Submission status: Accepted

Solution code:
def MI():
    return map(int, input().split())

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

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:
import numpy as np
N = int(input())
A_lis = [int(input()) for _ in range(N)]

num = sorted(A_lis, reverse=True)[:2]
for i in A_lis:
    if i != num[0]:
        print(num[0])
    else:
        print(num[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
B = list(set(A))
B.sort()
B.reverse()
if len(B) == 1 or A.count(B[0])>= 2:
  for i in range(N):
    print(B[0])
else:
  for i in range(N):
    if A[i] == B[0]:
      print(B[1])
    else:
      print(B[0])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys,collections
sys.setrecursionlimit(1000000)
INF = float("inf")

N = int(sys.stdin.readline())
A = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param

left=[-INF]*(N+1)
right=[-INF]*(N+1)

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

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

#print(left,right)
for i in range(N):
    #print(i,i+2)
    print(max(left[max(0,i)],right[min(N,i+1)]))

Submission status: Accepted

Solution code:
L=list()
N=int(input())
for i in range(N):
  a=int(input())
  L.append(a)
c=max(L)
d=sorted(L)[-2]
for i in range(N):
  if L[i]==c:
    print(d)
  else:
    print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=list(set(a))
if len(b)==1:
  for i in range(n):
    print(b[0])
elif a.count(b[-1])>=2:
  for i in a:
    print(b[-1])
else:
  for i in a:
    if i==b[-1]:
      print(b[-2])
    else:
      print(b[-1])

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    ai=int(input())
    a.append(ai)
    
right=[0]
left=[0]
maxr=0
maxl=0
for i in range(n-1):
    maxl=max(maxl,a[i])
    maxr=max(maxr,a[-i-1])
    left.append(maxl)
    right.append(maxr)
for i in range(n):
    print(max(left[i],right[-i-1]))

Submission status: Accepted

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

ans1=max(a)
for i in range(n):
    if a[i]==ans1:
        a[i]=0
        point=i
        break
b=[ans1]
b*=n
ans2=max(a)
b[point]=ans2
for i in range(n):
    print(b[i])

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
max_list = max(a)
ans = sorted(list(a))[-2]
for i in range(n):
    if a[i] == max_list:
        print(ans)
    else :
        print(max_list)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = []
    C = []
    for _ in range(N):
        a = int(input())
        A.append(a)
        C.append(a)
    B = max(A)
    C.remove(B)
    D = max(C)
    for i in range(N):
        if B != A[i]:
            print(B)
        else:
            print(D)
resolve()

Submission status: Accepted

Solution code:
#

import sys
from copy import deepcopy
input=sys.stdin.readline

def main():
    N=int(input())
    A=[int(input()) for i in range(N)]
    B=deepcopy(A)
    B.sort()
    M=max(A)
    if A.count(M)>1:
        for i in range(N):
            print(M)
    else:
        for i in range(N):
            if A[i] == M:
                print(B[N-2])
            else:
                print(M)
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

import heapq

Q = [-a for a in A]
heapq.heapify(Q)

ans = []

for i in range(n):
    if -Q[0] == A[i]:
        tmp = heapq.heappop(Q)
        ans.append(-Q[0])
        heapq.heappush(Q, tmp)
    else:
        ans.append(-Q[0])

print(*ans)

Submission status: Accepted

Solution code:
n = int(input())
a = []
b = []
for i in range(n):
  s = int(input())
  a.append(s)
  b.append(s)
m = max(a)
b.sort()
b = b[:-1]
m2 = b[-1]
for i in range(n):
  if a[i] == m:
    print(m2)
  else:
    print(m)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if a.count(b[-1]) > 1:
    for i in range(N):
        print(b[-1])
else:
    ind = a.index(b[-1])
    for i in range(N):
        if i == ind:
            print(b[-2])
        else:
            print(b[-1])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from itertools import accumulate


def read():
    N = int(input().strip())
    A = []
    for i in range(N):
        a = int(input().strip())
        A.append(a)
    return N, A


def solve(N, A):
    L = list(accumulate(A, max))
    R = list(accumulate(A[::-1], max))[::-1]
    for i in range(N):
        ans = 0
        if i > 0:
            ans = max(ans, L[i-1])
        if i < N-1:
            ans = max(ans, R[i+1])
        print(ans)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


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


# In[6]:


idx_max = A.index(max(A))
tmp = [x for x in A]
del tmp[idx_max]
most_max = max(A)
second_max = max(tmp)
for i in range(N):
    if i == idx_max:
        print(second_max)
    else:
        print(most_max)


# In[ ]:

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
top=0
top_num=0
second_num=0
for i in range(n):
    a=int(input())
    if a>top_num:
        top=i
        top_num=a
    elif a>second_num:
        second_num=a

for i in range(n):
    if i==top:
        print(second_num)
    else:
        print(top_num)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = [int(input()) for n in range(N)]
    B = A.copy()
    B.sort()
    mx = max(A)
    count = A.count(mx)

    for i in range(N):
        if mx > A[i]:
            print(mx)
        elif mx == A[i]:
            if count >= 2:
                print(mx)
            else:
                print(B[-2])
    return


resolve()

Submission status: Accepted

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

res=max(a)
ans=[res]*n
s=[i for i, x in enumerate(a) if x ==res]
a.sort()
res2=a[-2]
for i in s:
    ans[i]=res2
for i in range(n):
    print(ans[i])

Submission status: Accepted

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

mx = max(A)
sorted_A = sorted(A)
mx2 = sorted_A[-2]

for j in range(N):
    if mx != A[j]:
        print(mx)
    else:
        print(mx2)

Submission status: Accepted

Solution code:
#C
import copy

n = int(input())
A = list()

for num in range(n):
    A.append(int(input()))
    
sorted_A = copy.copy(A)
sorted_A.sort()

for num in A:
    if num == sorted_A[-1]:
        print(sorted_A[-2])
    else:
        print(sorted_A[-1])

Submission status: Accepted

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

Submission status: Accepted

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

first = max(A)
firstCnt = 0
firstIdx = 0
second = 0
secondIdx = 0

for i,a in enumerate(A):
  if a < first:
    if a > second:
      second = a
      secondIdx = i
  else:
    firstIdx = i
    firstCnt += 1

if firstCnt > 1:
  for i in range(N):
    print(first)
else:
  for i in range(N):
    if i == firstIdx:
      print(second)
    else:
      print(first)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
m1=0
m2=0
for i in range(n):
    a.append(int(input()))
    A=a[i]
    if a[i]>m1:
        m2=m1
        m1=a[i]
        index=i
    elif m1==a[i]:
        m2=a[i]
    elif a[i]>m2:
        m2=a[i]
for i in range(n):
    if i==index:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


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

    asort = sorted(A, reverse=True)
    m1 = asort[0]
    m2 = asort[1]

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
first = max(a)
f_id = a.index(max(a))
second = max(a[:f_id] + a[f_id+1:])

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

Submission status: Accepted

Solution code:
n=int(input())
a=[]
mx=0
sec=0
for i in range(0,n):
  a.append(int(input()))
  if a[i]>sec:
    if a[i]>mx:
      sec=mx
      mx=a[i]
    else:
      sec=a[i]

for x in a:
  if x==mx:
    print(sec)
  else:
    print(mx)

Submission status: Accepted

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

    As_sort = sorted(As)
    second, first = As_sort[-2:]
    for i in range(n):
        if As[i] == first:
            print(second)
        else:
            print(first)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: str(sys.stdin.buffer.readline().rstrip())
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

n = ri()
aa = [ri() for _ in range(n)]
a = sorted(aa)
m = a[-1]
if m == a[-2]:
    for _ in range(n):
        print(m)
else:
    for i in aa:
        if i < m:
            print(m)
            continue
        else:
            print(a[-2])

Submission status: Accepted

Solution code:
N = int(input())

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

M = sorted(A)

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

Submission status: Accepted

Solution code:
def main():
    a=[int(input()) for _ in range(int(input()))]
    m=sorted(a, reverse=True)
    i=a.index(m[0])
    for j in range(len(a)):
        print(m[0] if i != j else m[1])
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    a=[int(input()) for _ in range(int(input()))]
    idx = -1
    m = -1
    n = -1
    for i, v in enumerate(a):
        if v >= m:
            idx = i
            n = m
            m = v
        elif v >= n:
            n = v
    for i, v in enumerate(a):
        if idx == i:
            print(n)
        else:
            print(m)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq
 
n = int(input())
a = [int(input()) for _ in range(n)]
m1, m2 = heapq.nlargest(2, a)
for ai in a:
  print(m1 if ai != m1 else m2)

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for n in range(N):
  temp = int(input())
  A.append(temp)
  B.append(temp)
list.sort(A, reverse = True)
for n in range(N):
  if B[n] == A[0]:
    print(A[1])
  else:
    print(A[0])

Submission status: Accepted

Solution code:
N=int(input())

seq=[]

for i in range(N):
    a= int(input())
    seq.append(a)

import heapq

tmp=list(map(lambda x: x*(-1),seq))
heapq.heapify(tmp)

a=heapq.heappop(tmp)*(-1)
b=heapq.heappop(tmp)*(-1)

for j in range(N):
    if seq[j]==a:
        print(b)
    else:
        print(a)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]


def solve():
    n = int(input())
    x = n
    a = []
    c = defaultdict(int)
    for _ in range(n):
        a.append(int(input()))
        c[a[-1]] += 1
    b = a[:]
    a = list(set(a))
    a.sort(reverse=True)
    for e in b:
        i = 0
        while e == a[i] and c[e] == 1:
            i += 1
        print(a[i])

        
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()



"""

1 4
7
RRRR
1 1 0 1
----
4

1 4
47
RCRR
1 1 0 1
----
-1

2 2
10
RR
RR
1 7
4 1
----
4

2 2
10
RR
RR
7 4
7 4
----
-1

3 3
8
RCR
RRC
RRR
1 1 1
1 1 1
1 3 1


3 3
1
RCR
RRC
RRR
1 1 1
1 1 1
1 3 1


"""

Submission status: Accepted

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

Submission status: Accepted

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

maxa = max(a)
maxb = max(a)
cntmax = a.count(maxa)
if cntmax >= 2:
    for i in range(n):
        print(maxa)
    exit()
else:
    maxi = a.index(maxa)
    x = 0
    y = 0
    if maxi >= 1:
        x = max(a[:maxi])
    if maxi < n - 1 :
        y = max(a[maxi+1:])
    maxb = max(x,y)

for i in a:
    if i == maxa:
        print(maxb)
    else:
        print(maxa)

Submission status: Accepted

Solution code:
N = int(input())
A = []
A_max = -1
index = 0
for i in range(N):
  a = int(input())
  if A_max <= a:
    A_max,index = a,i
  A.append(a)

for i in range(N):
  if i == index:
    A.pop(i)
    print(max(A))
  else:
    print(A_max)

Submission status: Accepted

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

T.sort()
T.reverse()

first=T[0]
second=T[1]

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

Submission status: Accepted

Solution code:
import copy
a=int(input())
b=[int(input()) for _ in range(a)]
c=copy.deepcopy(b)

n=max(b)
c.remove(n)
m=max(c)

o=b.index(n)

l=0
p=0

while l!=o:
    print(n)
    l+=1

print(m)
q=a-o-1
while p!=q:
    print(n)
    p+=1

Submission status: Accepted

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

Submission status: Accepted

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

l2 = l
l2 = sorted(l2)
l2.reverse()
max = max(l)


ans = []
if l2[1] == max:
    for i in range(n):
        print(max)
else:
    for j in l:
        if j != max:
            print(max)
        else:
            print(l2[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
N = list(map(int,input().split()))
S = [str(input()) for _ in range(N)]
S = [list(map(int,list(input()))) for _ in range(h)] 
print(*S,sep="")
"""

import sys
sys.setrecursionlimit(10**6)
input = lambda: sys.stdin.readline().rstrip()
inf = float("inf") # 無限

n = int(input())
a = []

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

first = int(max(a))
if 1<a.count(first):
    second = int(first)
else:
    second = sorted(set(a))[-2]

for _ in a[:]:
    if _ == first:
        print(second)
    else:
        print(first)

Submission status: Accepted

Solution code:
def solve():
    n=int(input())
    num=[]
    def append(num1):
        num.append(num1)
    def count1(num2):
        return num.count(num2)
    for i in range(n):
        a=int(input())
        append(a)
    max1=max(num)
    count=count1(max1)
    if count>1:
        for i in range(n):
            print(max1)
    else:
        for i in range(n):
            if num[i]==max1:
                if i==0:
                    print(max(num[1:]))
                elif i==n-1:
                    print(max(num[:n-1]))
                else:
                    print(max(max(num[:i]),max(num[i+1:])))
            else:
                print(max1)
solve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A_sorted = sorted(A, reverse=True)
    A_max = A_sorted[0]
    A_semi_max = A_sorted[1]
    
    for i in range(N):
        if A[i] != A_max:
            print(A_max)
        else:
            print(A_semi_max)
main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = [int(input()) for _ in range(N)]
max_A = max(A)
sorted_A = sorted(A, reverse=True)
if sorted_A[1] == max_A:
    for _ in range(N):
        print(max_A)
    exit()

for a in A:
    if a == max_A:
        print(sorted_A[1])
    else:
        print(max_A)

Submission status: Accepted

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

high = sorted(A, reverse=True)[: 2]

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

Submission status: Accepted

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

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

Submission status: Accepted

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

data_s = sorted(data)

for d in data:
  if d == data_s[-1]:
    print(data_s[-2])
  else:
    print(data_s[-1])

# print(data)
# print(data_s)

Submission status: Accepted

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

max1_a = sorted_A[-1]
max2_a = sorted_A[-2]
if A.count(max1_a) > 1:
    for i in range(N):
        print(max1_a)
else:
    for i in range(N):
        if A[i] == max1_a:
            print(max2_a)
        else:
            print(max1_a)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]
ma=sorted(l)[-2:]
for j in l:
    if j==ma[1]:
        print(ma[0])
    else:
        print(ma[1])

Submission status: Accepted

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

A_max = max(A)

B = A.copy()
B.sort()
B.reverse()

if B[1] == A_max:
    for i in range(n):
        print(A_max)
else:
    for a in A:
        if a != A_max:
            print(A_max)
        else:
            print(B[1])

Submission status: Accepted

Solution code:
import heapq
n = int(raw_input())
ais = [int(raw_input()) for _ in range(n)]
heap = []
for ai in ais:
	heapq.heappush(heap, ai)
	if len(heap) == 3: 
		heapq.heappop(heap)

u,v = heapq.heappop(heap),heapq.heappop(heap)
for ai in ais:
	if ai == v:
		print u
	else:
		print v

Submission status: Accepted

Solution code:
import copy

n = int(input())

lis = []
for _ in range(n):
    lis.append(int(input()))
    
slis = sorted(copy.deepcopy(lis), reverse=True)
    
m = max(lis)
s = sorted(slis, reverse=True)
s.remove(m)
sm = max(s)


for i in range(n):
    if lis[i] != m:
        print(m)
    else:
        print(sm)

Submission status: Accepted

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

A_max = max(A)
A_max_ind = A.index(max(A))

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