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=[int(input()) for i in range(n)]
t=list(enumerate(a))
t.sort(key=lambda x:x[1])
mx=t[-1][1]
ans=[mx for i in a]
ans[t[-1][0]]=t[-2][1]
for k in ans:
    print(k)

Submission status: Accepted

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

maxinum = max(nums)
maxinum2 = -1
for i in range(N):
    if nums[i] == maxinum:
        if maxinum2 == -1:
            maxinum2 = max(nums[0:i] + nums[i+1:])
        print(maxinum2)
    else:
        print(maxinum)

Submission status: Accepted

Solution code:
N = int(input())
A_l = []
for i in range(N):
    A_l.append(int(input()))
    
A_max = max(A_l)
A_tmp = A_l.copy()
A_tmp.remove(A_max)
A_second = max(A_tmp)
for j in range(N):
    if A_l[j] != A_max:
        print(A_max)
    else:
        print(A_second)

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
	A.append(int(input()))
	
max_1 = max(A)
B = A[:]
B.remove(max_1)
max_2 = max(B)

for i in A:
	if i == max_1:
		print(max_2)
	else:
		print(max_1)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
max_a = max(a)
if a.count(max(a)) == 1:
  sec_max = sorted(a)[-2]
else:
  sec_max = max_a

for i in range(n):
  if a[i] == max_a:
    print(sec_max)
  else:
    print(max_a)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=[]
m=[]
for i in range(n):
  a.append(int(input()))
m.append(a.index(max(a)))
m.append(a.pop(m[0]))
m.append(max(a))

for i in range(m[0]):
  print(m[1])
  
print(m[2])

for i in range(m[0]+1,n):
  print(m[1])

Submission status: Accepted

Solution code:
import numpy as np

i = input()
N = int(i)
A = np.array([0]*N)
i = input()
a = int(i)
A[0] = a
i = input()
b = int(i)
A[1] = b
snd = min(a, b)
fst = max(a, b)
for x in range(N - 2):
    j = input()
    y = int(j)
    A[x+2] = y
    if y <= fst:
        snd = max(snd, y)
    fst = max(fst, y)

for x in range(N):
    if A[x] < fst:
        print(fst)
    else:
        print(snd)

Submission status: Accepted

Solution code:
import heapq
N = int(input())
A = [None]*N
for i in range(N):
    A[i] = {
        "index": i,
        "value": int(input())
    }

B = heapq.nlargest(2, A, key=lambda e:e["value"])
for i in range(N):
    if i == B[0]["index"]:
        print(B[1]["value"])
    else:
        print(B[0]["value"])

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=[]
for i in range(n):
    num=int(input())
    a.append(num)
    b.append(num)
b.sort(reverse=True)
x=a.count(b[0])
for i in range(n):
    if(a[i]==b[0]):
        if(x==1):
            print(b[1])
        else:
            print(b[0])
    else:
        print(b[0])

Submission status: Accepted

Solution code:
N = int(input())
a_list = []
max_v = -1
sec = -1
tmp = 0
drop = False
max_ind = []
for i in range(N):
    a_list.append(int(input()))
    if a_list[-1]>max_v:
        tmp = max_v
        max_v = a_list[-1]
        max_ind = [i]
    elif a_list[-1]<max_v:
        if a_list[-1]>sec:
            drop = True
            sec = a_list[-1]
    else:
        max_ind.append(i)
if len(max_ind)>1:
    for i in range(N):
        print(max_v)
elif drop:
    for i in range(N):
        if i in max_ind:
            print(sec)
        else:
            print(max_v)
else:
    for i in range(N):
        if i in max_ind:
            print(tmp)
        else:
            print(max_v)

Submission status: Accepted

Solution code:
n = int(input())
list = []
max = 0
second = 0
max_index = []

for i in range(n):
    num = int(input())
    if (num > max):
        max = num
    elif (num > second):
        second = num
    list.append(num)

counter = list.count(max)

for i,x in enumerate(list):
    if (x == max) & (counter == 1):
        print(second)
    else:
        print(max)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
arr = np.array([int(input()) for _ in range(n)])

a_max = arr.max()

idx = np.where(arr == a_max)[0]

if len(idx) > 1:
    for i in range(n):
        print(a_max)

    exit()

not_arr = arr[arr != a_max]

b_max = not_arr.max()

t = idx[0]
for i in range(n):
    if i == t:
        print(b_max)
    else:
        print(a_max)

Submission status: Accepted

Solution code:
import copy

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

mcnt = 0
mv = max(l)
for i in range(N):
  if l[i] == mv and mcnt == 0:
    a = copy.copy(l)
    del a[i]
    print(max(a))
    mcnt = mcnt + 1
  else:
    print(mv)

Submission status: Accepted

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

max_ = max(list)
for i in range(n):
    a = list[i]
    if a == max_:
        del list[i]
        ans = max(list)
        print(ans)
        list.insert(i,a)
        if ans == max_:
            for j in range(n-i-1):
                print(max_)
            break
    else:
        print(max_)

Submission status: Accepted

Solution code:
N = int(input())
s = [int(input()) for i in range(N)]
ld = sorted(s, reverse=True)
D = int(s.index(ld[0]))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np 
n = int(input())
a = [int(input()) for i in range(n)]
aa = np.array(a)
maxa = max(aa)
argmax = np.argmax(aa)

for i in range(n):
    if i == argmax:
        tmp = np.delete(aa,i)
        print(max(tmp))
    else:
        print(maxa)

Submission status: Accepted

Solution code:
n = int(input())
al = [int(input()) for i in range(n)]
m1, m2 = 0, 0
for a in al:
    if m1 < a:
        m1=a
    elif m2 < a:
        m2=a
for a in al:
    print(m2) if m1 == a else print(m1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
AList = [int(input()) for i in range(N)]
max_AList = max(AList)
max2nd_AList = sorted(AList, reverse = True)[1]

for i in range(N) :
    if AList[i] == max_AList :
        print(max2nd_AList)
    else :
        print(max_AList)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import itertools

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

max_list = [0] + list(itertools.accumulate(a, func=max))
max_list_r = ([0] +list(itertools.accumulate(reversed(a), func=max)))[::-1]
for i in range(n):
    print(max(max_list[i], max_list_r[i + 1]))

Submission status: Accepted

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

N = int(input())
A = [int(input()) for _ in range(N)]
amax = max(A)
amaxcnt = A.count(amax)

for i in range(N):
    ai = A[i]
    if ai == amax and amaxcnt == 1:
        A.pop(i)
        print(max(A))
        A.insert(i, 0)
    else:
        print(amax)

Submission status: Accepted

Solution code:
from sys import stdin


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

    new_A = sorted(A, reverse=True)
    max_A = new_A.pop(0)
    next_max_A = new_A.pop(0)

    for a in A:
        if max_A == a:
            print(next_max_A)
        else:
            print(max_A)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N=int(input())
A=[int(input()) for _ in range(N)]
B=list(itertools.accumulate(A,func=max))
C=list(itertools.accumulate(A[::-1],func=max))[::-1]
print(C[1])
for i in range(1,N-1):
    print(max(B[i-1],C[i+1]))
print(B[-2])

Submission status: Accepted

Solution code:
import copy

data = []
i = int(input())

data = [int(input()) for _n in range(i)]
maxInt = max(data)
data2 = copy.copy(data)
data2.remove(maxInt)
max2Int = max(data2)
for n in range(i):
	if maxInt != data[n]:
		print(maxInt)
	else:
		print(max2Int)

Submission status: Accepted

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

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

x = sorted(A, reverse=True)

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

Submission status: Accepted

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

ans = []
front_max_list, back_max_list = [0], [0]

for i in range(N):
    front_max_list.append(max(front_max_list[i], A[i]))
    back_max_list.append(max(back_max_list[i], A[N - i - 1]))

for i in range(N):
    print(max(front_max_list[i], back_max_list[N - i - 1]))

Submission status: Accepted

Solution code:
n = int(input())

a = [int(input()) for _ in range(n)]
b = sorted(a)
m1 = b[-1]
m2 = b[-2]

for i in range(n):
    if a[i] == m1:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

max_val = 0
for i in range(N):
    input_val = int(input())
    if (max_val < input_val):
        max_val = input_val
    A.append(input_val)
    if (i < N -1):
        M1.append(max_val)

max_val = 0
for i in range(N-1):
    input_val = A[N-1-i]
    if (max_val < input_val):
        max_val = input_val
    M2.append(max_val)

if (N<=2):
    print(A[1])
    print(A[0])
else:
    for i in range(N):
        print(max(M1[i], M2[N-1-i]))

Submission status: Accepted

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

maxmax=max(a)
maxmax_ind=a.index(maxmax)
a[maxmax_ind]=0
maxmax2=max(a)
a[maxmax_ind]=maxmax

if maxmax==maxmax2:
    for i in range(n):
        print(maxmax)

else:
    for i in range(n):
        if i==maxmax_ind:
            print(maxmax2)
        else:
            print(maxmax)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    a = int(input())
    list = []
    list2=[]
    for i in range(a):
        b = int(input())
        list.append(b)
        list2.append(b)
    list.sort(reverse=True)
    for i in list2:
        if i == list[0]:
            print(list[1])
        else:
            print(list[0])

Submission status: Accepted

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

    max1st = max(A)
    max1stIndex = A.index(max1st)

    for i in range(len(A)):
        if i == max1stIndex:
            tmp = A[i]
            A[i] = 0
            print(max(A))
            continue

        print(max1st)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

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

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

new_li = sorted(li, reverse=True)

M = new_li[0]
m = new_li[1]

for i in range(N):
    if li[i] != M:
        print(M)
    elif li[i] == M:
        print(m)

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = []
for s in range(n):
    a.append(int(input()))
m = max(a)
b = sorted(a)
if b[-2] == m:
  for c in range (n):
    print(m)
else:
  for t in range(n):
    if a[t] == m:
      b = copy.copy(a)
      b[t:t+1] = []
      print(max(b))
    else:
      print(m)

Submission status: Accepted

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

maxnumber = max(A)
secondmax = sorted(A)[-2]

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

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
b=max(a)
c=a.index(b)
a.pop(c)
d=max(a)
for i in range(n):
    if (i==c):
        print('{0}'.format(d))
    else:    
        print('{0}'.format(b))

Submission status: Accepted

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

max_l, max_r = [A[0]], [A[-1]]
for i in range(N-1):
    max_l.append(max(A[i+1],   max_l[i]))
    max_r.append(max(A[N-2-i], max_r[i]))

print(max_r[-2])
for i in range(1,N-1):
    print(max(max_r[-2-i], max_l[i-1]))
print(max_l[N-2])

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
m1 = max(a)
c = a.count(m1)
if len(set(a)) == 1:
	for i in range(n):
		print(m1)
else:
	m2 = sorted(set(a))[-2]
	for i in range(n):
		if a[i] < m1:
			print(m1)
		elif a[i] == m1:
			if c > 1:
				print(m1)
			else:
				print(m2)
		else:
			print(m2)

Submission status: Accepted

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

maxA=max(A)
countNumber=A.count(maxA)

for i in range(N):
  if A[i]<maxA:
    print(maxA)
  elif countNumber>1:
    print(maxA)
  else:
    B=A[:]
    del B[i]
    print(max(B))

Submission status: Accepted

Solution code:
N = int(input())

m0 = 1
m1 = 1
max_i = []
for i in range(N):
    a = int(input())
    if a > m0:
        m1 = m0
        m0 = a
        max_i = []
        max_i.append(i)
    elif a == m0:
        m1 = a
    elif a > m1:
        m1 = a
for i in range(N):
    if i in max_i:
        print(m1)
    else:
        print(m0)

Submission status: Accepted

Solution code:
n = int(input())
array = []

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

m = max(array)
c = array.count(m)
sa = array[:]
sa.remove(m)
m2 = max(sa)

for e in array:

    if e != m:
        print(m)
    else:
        if 1 < c:
            print(m)
        else:
            print(m2)

Submission status: Accepted

Solution code:
import copy
n = int(input())
string_list = [int(input()) for i in range(n)]

sorted_list = sorted(string_list)
max_num = max(string_list)

for j in string_list:
    if j != max_num:
        print(max_num)
    else:
        print(sorted_list[-2])

Submission status: Accepted

Solution code:
n = int(input())
A = []
for i in range(n):
  a = int(input())
  A.append(a)
m = max(A)
c = A.count(m)

if len(set(A)) > 1:
	m2 = sorted(set(A))[-2]

bool = False
if c == 1:
  bool = True

for i in A:
  if bool and i==m:
    print(m2)
  else:
    print(m)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A_m = str(max(A))
a = A.index(int(A_m))
A = sorted(A)
if A.count(int(A_m)) >= 2:
    print((A_m + '\n')*(N-1) + A_m)
else:
    print((A_m + '\n')*a + str(A[-2]))
    if N-(a+1) == 1:
        print(A_m)
    elif N-(a+1) < 1:
        pass
    else:
        print((A_m + '\n')*(N-(a+2)) + A_m)

Submission status: Accepted

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

import numpy as np
an = np.array(an)
_an = an.argsort()[::-1]
_max = np.max(an[_an[0]])
_max_next = np.max(an[_an[1]])
an[an < _max] = _max
an[_an[0]] = _max_next
an = an.astype("str")
print("\n".join(list(an)))

Submission status: Accepted

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

c = sorted(a)
a1 = c[-1] 
a2 = c[-2]

for j in range(N):
    if a[j]==a1:
        print(a2)
    else:
        print(a1)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

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

if(N==1):
    print(a[0])
else:
    a_max = 0
    n_a_max = 0
    all_max = max(a)
    max_arg = np.argmax(a)

    if(max_arg!=0):
        a_max = max(a[:max_arg])
    if(max_arg!=N-1):
        n_a_max = max(a[max_arg+1:])

    if(a_max>=n_a_max):
        next_max = a_max
    else:
        next_max = n_a_max

    for i in range(N):
        if a[i]==all_max:
            print(next_max)
        else:
            print(all_max)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
m_num = max(a)
m_index = [i for i, x in enumerate(a) if x == m_num]

if len(m_index) > 1:
  for i in range(n):
    print(m_num)
else:
  if len(a) == 1:
    print(a[0])
  else:
    m2_num = sorted(a)[-2]
    for i in range(n):
      if (i in m_index):
        print(m2_num)
      else:
        print(m_num)

Submission status: Accepted

Solution code:
n = int(input())

li = [int(input()) for i in range(n)]
ls = sorted(li, reverse = True)

m1 = ls[0]
m2 = ls[1]
"""
for i in range(0,n):
    if(ls[i] != m1):
        m2 = ls[i]
        break
"""

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

Submission status: Accepted

Solution code:
import copy
import numpy as np
N = int(input())
sequences = [int(input()) for _ in range(N)]
np_sequences = np.array(sequences)
max_index = np_sequences.argmax()
for i in range(N):
        if i != max_index:
            print(sequences[max_index])
        else:
            tmp = copy.copy(sequences)
            tmp.pop(i)
            tmp_set = set(tmp)
            print(max(tmp_set))

Submission status: Accepted

Solution code:
import sys
N=input()
A=[ int(sys.stdin.readline().strip())  for _ in range(N) ]

L=[]
for i,a in enumerate(A):
	if i==0:
		L.append(a)
	else:
		L.append( max(L[-1], a) )

R=[]
for i,a in enumerate(A[::-1]):
	if i==0:
		R.append(a)
	else:
		R.append( max(R[-1], a) )

R=R[::-1]


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

Submission status: Accepted

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

b=sorted(a,reverse=True)

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

Submission status: Accepted

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

D=max(A)
C=sorted(A)[-2]
for j in range(N):
	if A[j] != D:
		print(D)
	else:
		print(C)

Submission status: Accepted

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

import math

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

    value = sorted(A)
    first = value[N-1]
    second = value[N-2]


    # print(A)

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

Submission status: Accepted

Solution code:
import copy
from sys import stdin
n=int(input())
A=[int(stdin.readline().rstrip()) for _ in range(n)]
z=max(A)
B=sorted(A)
o=B[n-2]
for j in range(0,n):
    if A[j]<z:
       print(z)
    else:
        if B[n-2]==B[n-1]:
           print(z)
        else:
           print(o)

Submission status: Accepted

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

li = [int(input()) for i in range(n)]
m = max(li)
m2 = sorted(li)[-2]

for j in range(n):
  if m == li[j]:
    print(m2)
  else:
    print(m)

Submission status: Accepted

Solution code:
n = int(input())
a = []
first = 0
second = 0
for num in range(n):
	hoge = int(input())
	a.append(hoge)
	if hoge >= first:
		second = first
		first = hoge
	elif second <= hoge and hoge <= first:
		second = hoge

#print(first, second)
for num in range(n):
	if(first <= a[num]):
		print(second)
	else:
		print(first)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
maxA = max(A)
secA = sorted(A)[-2]
num=A.count(maxA)
#print(N,A,maxA,secA,num)
 
for i in range(N):
  if num>1:print(maxA)
  elif A[i]==maxA:print(secA)
  else:print(maxA)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
max_a = max(a)
second_a = sorted(a)[-2]
max_count = a.count(max_a) - 1
if max_count:
    [print(max_a) for _ in range(len(a))]
else:
    [print(max_a) if a[i] != max_a else print(second_a) for i in range(len(a))]

Submission status: Accepted

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

a_sorted = sorted(a_li, reverse=True)
max_a = a_sorted[0]
second = a_sorted[1]
for i in range(n):
    if a_li[i] == max_a:
        print(second)
    else :
        print(max_a)

Submission status: Accepted

Solution code:
n = int(input())
LA = [int(input()) for i in range(n)]
LB = []
LB.extend(LA)
LA.sort()
z = LA[-1]
y = LA[-2]
for i in range(n):
    if LB[i] == z:
        print(y)
    else:
        print(z)

Submission status: Accepted

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

maximum = max(A)
second_maximum = sorted(A, reverse=True)[1]
for n in range(N):
    if A[n] == maximum:
        print(second_maximum)
    else:
        print(maximum)

Submission status: Accepted

Solution code:
import copy
n = int(input())
all_list = [int(input()) for _ in range(n)]
max_value = max(all_list)
max_value_count = all_list.count(max_value)
for i in range(n):
    if all_list[i] == max_value:
        if max_value_count > 2:
            print(max_value)
        else:
            tmp = copy.copy(all_list)
            tmp.pop(i)
            print(max(tmp))
    else:
        print(max_value)

Submission status: Accepted

Solution code:
N=int(input())
L=[int(input()) for i in range(N)]
tl=sorted(L)
index1=L.index(tl[-1])
index2=L.index(tl[-2])
for i in range(N):
	if i==index1:
		print(L[index2])
	else:
		print(L[index1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
aMax=0
multi=0
a = [0 for i in range(n)]
for i in range(n):
  a[i] = int(input())
  if aMax == a[i]:
    multi = 1
  elif aMax < a[i]:
    aMax = a[i]
    multi = 0

if multi == 1:
  for i in range(n):
    print(aMax)
else:
  for i in range(n):
    if aMax != a[i]:
      print(aMax)
    else:
      if i == 0:
        print(max(a[1:]))
      elif i == n - 1:
        print(max(a[0:i]))  
      else:
        aMax2=max(max(a[:i]),max(a[i+1:]))
        print(aMax2)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
max = 0
max2 = 0
tmp = 0
for i in range(n):
    if max<a[i]:
        max = a[i]
        tmp=i
for i in range(n):
    if tmp!=i and max2<a[i]:
        max2 = a[i]

for i in range(n):
    if i != tmp:
        print(max)
    else:
        print(max2)

Submission status: Accepted

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

m = max(a)
l = []
for i, k in enumerate(a):
    if k == m:
        l.append(i)

if len(l) == 1:
    for i in range(n):
        if i != l[0]:
            print(m)
        else:
            a[l[0]] = 0
            print(max(a))
else:
    for i in range(n):
        print(m)

Submission status: Accepted

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

first_max = max(a)

former_arr = [0] + a[0:a.index(first_max)]
latter_arr = [0] + a[a.index(first_max) + 1:]
second_max = (max(max(former_arr), max(latter_arr)))

ans = ""
ans += (str(first_max) + '\n') * (a.index(first_max))
ans += (str(second_max) + '\n')
ans += (str(first_max) + '\n') * (N - a.index(first_max) - 1)
print(ans.rstrip('\n'))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
a1=max(A)
a2=0
j=0
for i in range(N):
    if A[i]==a1:
        j=j+1
    else:
        j=j
if j!=1:
    for i in range(N):
        print(a1)
else:
    for i in range(N):
        if A[i]>a2 and A[i]!=a1:
            a2=A[i]
    for i in range(N):
        if A[i]==a1:
            print(a2)
        else:
            print(a1)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
A_sub = A.copy()
MAX = max(A)
A_sub.remove(MAX)
MAX2 = max(A_sub)

for i in range(N):
    if A[i] < MAX:
        print(MAX)
    elif A[i] == MAX:
        print(MAX2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
n = A.index(max(A))
m = max(A)
A.remove(max(A))

for i in range(n):    
    print(m)
print(max(A))
for i in range(N-n-1):
    print(m)

Submission status: Accepted

Solution code:
import sys
INPUT = sys.stdin.readline
#入力関数
def SORO_INT(): return int(INPUT())
def MULT_INT_LIST(): return list(map(int, INPUT().split()))
def MULT_INT_MAP(): return map(int, INPUT().split())
def SORO_STRING(): return INPUT()
def MULT_STRING(): return INPUT().split()

N = SORO_INT()
A = list([int(input()) for i in range(N)])
t = sorted(A)[-2]

B = max(A)
B_in = A.index(B)


for i in range(N):
    A[i] = B
A[B_in] = t
for i in range(N):
    print(A[i])

Submission status: Accepted

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

B.sort()

max_a = B[N-1]
max_dup = False
if B[N-2] == max_a:
    max_dup = True
second_max_a = -1
for i in range(N-1,-1,-1):
    if B[i] < max_a:
        second_max_a = B[i]
        break

for a in A:
    if a < max_a:
        print(max_a)
    elif max_dup:
        print(max_a)
    else:
        print(second_max_a)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
d = []
h = []
for i in range(n):
    k = readi()
    d.append(k)
    heapq.heappush(h,-k)
t1 = -heapq.heappop(h)
t2 = -1
if h:
    t2 = -heapq.heappop(h)
if n == 1:
    print(d[0])
else:
    for i in range(n):
        if d[i] == t1:
            print(t2)
        else:
            print(t1)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_list = []
    for i in range(n):
        a = int(input())
        #[タグ、値]
        a_list.append([i,a])

    ans = sorted(a_list, key=lambda x:x[1])
    #print(ans)

    #print("-"*75)
    for i in range(n):
        #if 値 == max(ans)
        if a_list[i][1] == ans[-1][1]:
            #ansの最後から二番目の値
            print(ans[-2][1])
        else:
            print(ans[-1][1])

main()

Submission status: Accepted

Solution code:
N = int(input())

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

max_value = max(a_list)

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

Submission status: Accepted

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

a_max = max(a)
idx = [i for i, x in enumerate(a) if x==a_max]

if len(idx) > 1:
    for i in range(n):
        print(a_max)
else:
    for i in range(n):
        if a[i] == a_max:
            print(max(a[0:i]+a[i+1:]))
        else:
            print(a_max)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from bisect import bisect
n = int(input())

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

max_val = max( vals )
vals_not_max = [a for a in vals if a != max_val]
if len(vals_not_max) < n -1:
	max_val_exclude  = max_val
else:
	max_val_exclude = max(vals_not_max)
	
for a in vals:
	if a == max_val:
		print(max_val_exclude)
	else:
		print(max_val)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

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

a = np.array(a)

max_a = a.max()
ans = np.array([max_a for i in range(n)])

max_ind = np.where(a == max_a)[0]

if len(max_ind) == 1:
    b = np.sort(a)
    ans[max_ind] = b[-2]

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
def main():
	import sys
	import numpy as np
	input = sys.stdin.readline
	N = int(input())
	A = [int(input()) for _ in range(N)]
	A = np.array(A)
	maxNum = np.max(A)
	maxIndex = np.where(A == A.max())[0]
	if len(maxIndex) > 1:
		for _ in range(N):
			print(maxNum)
	elif maxIndex[0] == 0:
		print(np.max(A[1:]))
		for i in range(1, N):
			print(maxNum)
	elif maxIndex[0] == N-1:
		for i in range(N-1):
			print(maxNum)
		print(np.max(A[:N-1]))
	else:
		for i in range(maxIndex[0]):
			print(maxNum)
		prev = max(np.max(A[:maxIndex[0]]), np.max(A[maxIndex[0]+1:]))
		print(prev)
		for i in range(maxIndex[0]+1, N):
			print(maxNum)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A=[]
for i in range(N):
    A.append(int(input()))
max_A=max(A)
ind_max = A.index(max_A)
A2=A
del A2[ind_max]
max2_A=max(A2)
for i in range(N):
    if i==ind_max:
        print(max2_A)
    else:
        print(max_A)