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=[None]*N
for i in range(N):
        A[i]=int(input())
B=sorted(A)
M=B[N-1]
if B[N-1]==B[N-2]:
        for i in range(N):
                print(M)
else:
        for i in range(N):
                if A[i]==M:
                        print(B[N-2])
                else:
                        print(M)

Submission status: Accepted

Solution code:
import numpy as np
import math

N = int(input())

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


max_a = np.max(A)
max_i = np.argmax(A)

for i in range(N):
   if(i == max_i):
      aa = np.sort(A)
      print(int(aa[-2]))
   else:
      print(int(max_a))

Submission status: Accepted

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

a_max=max(a)

if a.count(a_max)>=2:
    for i in range(n):
        print(a_max)
else:
    max_ind=a.index(a_max)
    a.remove(a_max)
    a_2nd=max(a)
    for _ in range(1,max_ind+1):
        print(a_max)
    print(a_2nd)
    for _ in range(max_ind+1,n):
        print(a_max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
b = []
n = int(input())
m = 0
for i in range(n):
  a = int(input())
  b.append(a)
  if m < a:
    m = a
    c = [i]
  elif m == a:
    c.append(i)

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

Submission status: Accepted

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

maxx = 0
submax = 0

for i in range(n):
    k = int(input())
    if k > maxx:
        submax = maxx
        maxx = k
    elif k > submax:
        submax = k

    a.append(k)

for i in range(n):
    if a[i] == maxx:
        print(submax)
    else:
        print(maxx)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a_sort = list(sorted(a))
a_max = a_sort[-1]
a_second = a_sort[-2]
for i in range(n):
    if a[i] == a_max:
        print(a_second)
    else:
        print(a_max)

Submission status: Accepted

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

sorted_A = sorted(A, reverse=True)

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

Submission status: Accepted

Solution code:
from sys import stdin

if __name__ == '__main__':
    N = int(stdin.readline().rstrip())

    A = []
    for i in range(N):
        A.append(int(stdin.readline().rstrip()))
    B = A.copy()
    A.sort(reverse=True)

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

Submission status: Accepted

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

if a_[-1] == a_[-2]:
    for i in range(n):
        print(a_[n-1])
    exit()

b = list(set(a))

if len(b) == 1:
    for i in range(n):
        print(b[0])
    exit()

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 _ in range(N)]

L = [0] * (N+1)
R = [0] * (N+2)
A.append(0)

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

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

Submission status: Accepted

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

b = a[::-1]

amax = [0] * n
bmax = [0] * n

amax[0] = a[0]
bmax[0] = b[0]
for i in range(1, n):
    amax[i] = max(amax[i-1], a[i])
    bmax[i] = max(bmax[i-1], b[i])

bmax = bmax[::-1]

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

Submission status: Accepted

Solution code:
n=input()
n=int(n)
l=[]
for i in range(n):
    x=input()
    x=int(x)
    l.append(x)
ans=max(l)
x=l.index(ans)
del l[x]
sec=max(l)
for i in range(n):
    if i==x:
        print(sec)
        continue
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
for i in range(n):
    a[i] = int(input())
import numpy as np
b=np.array(a).argsort()
imax1=b[-1]
amax=a[imax1]
for j in range(n):
    if j==imax1: print(a[b[-2]])
    else:print(amax)

Submission status: Accepted

Solution code:
n=int(input())
alist=[int(input()) for i in range(n)]
max_value=max(alist)
acount=alist.count(max_value)
if acount!=1:
	for i in range(n):
		print(max_value)
else:
	for i in range(n):
		if alist[i]!=max_value:
			print(max_value)
		else:
			blist=alist[:]
			del blist[i]
			print(max(blist))

Submission status: Accepted

Solution code:
N = int(input())
A =[]
for _ in range(N):
  A.append(int(input()))
a = max(A)
a2= max(sorted(A)[:-1])
f = False
if(a == a2):
  f = True
for i in range(N):
  if(f):
    print(a)
  elif(A[i] == a):
    print(a2)
  else:
    print(a)

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(input()) for i in range(n)]
m = max(lst)
tmp = lst[:]
del tmp[tmp.index(max(tmp))]
s = max(tmp)
if lst.count(m) > 1:
  for i in range(n):
    print(m)
else:
  for i in range(n):
    if lst[i]==m:
      print(s)
    else:
      print(m)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for _ in range(N):
  A.append(int(input()))
  
B=sorted(A)
M1=B[-1]
M2=B[-2]

if B.count(M1)>1:
    for i in range(N):
        print(M1)
else:
    for i in range(N):
        if A[i] == M1:
            print(M2)
        else:
            print(M1)

Submission status: Accepted

Solution code:
import fractions
from collections import deque
import numbers as np
import math


def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    ids = [i for i in range(N)]
    C = list(zip(A, ids))
    sort = sorted(C, key=lambda x: x[0], reverse=True)
    rest = [sort[0][0] for _ in range(N)]
    rest[sort[0][1]] = sort[1][0]
    for i in range(N):
        print(rest[i])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import copy
N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input())
amax = max(A)
B = copy.copy(A)
B.remove(amax)
nextmax = max(B)


for i in range(N):
    if A[i] == amax:
        print(nextmax)
    else:
        print(amax)

Submission status: Accepted

Solution code:
N = int(input())

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

b_in = a_list.index(max(a_list))
b_max = max(a_list)

a_list.pop(b_in)

c_max = max(a_list)

for i in range(N):
    if b_in != i:
        print(b_max)
    else:
        print(c_max)

Submission status: Accepted

Solution code:
N = int(input())

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

max1 = max(A)
if A.count(max1) >= 2:
    max2 = max1
else:
    max2 = 0
    for num in A:
        if max2 < num and max1 > num:
            max2 = num

for num in A:
    if num == max1:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))
sorted_A = sorted(A,reverse=True)
m1 = sorted_A[0]
m2 = sorted_A[1]

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
maxa = max(a)
max_index = [i for i, x in enumerate(a) if x == maxa]
if len(max_index) >= 2:
    for i in range(n):
        print(maxa)
else:
    for i in range(n):
        if i == max_index[0]:
            print(sorted(a)[-2])
        else:
            print(maxa)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))
max_A = max(A)
max_index = A.index(max_A)
A.pop(max_index)
next_max = max(A)
for i in range(N):
    if i == max_index:
        print(next_max)
    else:
        print(max_A)

Submission status: Accepted

Solution code:
N = int(input())

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

sort_A = sorted(A, reverse=True)

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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    As = [int(stdin.readline().rstrip()) for _ in range(N)]
    max1 = max(As)
    if len([x for x in As if x == max1]) == 1:
        max2 = max([x for x in As if x != max1])
    else:
        max2 = max1
    for A in As:
        if A == max1:
            print(max2)
        else:
            print(max1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def solve():
    pass

def main():
    N = int(input())
    A = [ (i, int(input())) for i in range(N) ]
    A.sort(key=lambda x: x[1], reverse=True)
    
    for i in range(N):
        if i == A[0][0]:
            print(A[1][1])
        else:
            print(A[0][1])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = []
A_ap = A.append
for i in range(N):
    A_ap(int(input()))
    
max_A = sorted(A)[-1]
max_2A = sorted(A)[-2]
for i in range(N):
    if A[i] != max_A:
        print(max_A)
    else:
        print(max_2A)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)] 
a_sorted = sorted(a,reverse=True)
first = a_sorted[0]
second = a_sorted[1]
for ai in a:
  if ai==first:
    print(second)
  else:
    print(first)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

sec = sorted(A)[-2]
a = np.array(A)
maxA=max(a)
maxIndex = [i for i, x in enumerate(a) if x == maxA]
l=len(maxIndex)
for i in range(N):
    if l !=1:
        print(maxA)
    elif i in maxIndex:
        print(sec)
    else:
        print(maxA)

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(input()) for _ in range(n)]
if a_list[0] > a_list[1]:
    i_max = 0
    i_max2 = 1
else:
    i_max = 1
    i_max2 = 0
a_max = a_list[i_max]
a_max2 = a_list[i_max2]
for i, a in enumerate(a_list):
    if i < 2:
        continue
    if a > a_max:
        i_max2 = i_max
        a_max2 = a_max
        i_max = i
        a_max = a
    elif a > a_max2:
        i_max2 = i
        a_max2 = a
for i in range(n):
    if i == i_max:
        print(a_max2)
    else:
        print(a_max)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
sortedA = sorted(a)
if sortedA[-1] == sortedA[-2]:
    for j in range(n):
        print(sortedA[-1])
else:
    l = a.index(sortedA[-1])
    for j in range(l):
        print(sortedA[-1])
    print(sortedA[-2])
    for k in range(n-l-1):
        print(sortedA[-1])

Submission status: Accepted

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

maximum = max(a)
if a.count(maximum):
    num = a.index(maximum)
    x = max(a[:num]+a[num+1:])
    for _ in range(num):
        print(maximum)

    print(x)

    for _ in range(num+1, n):
        print(maximum)

else:
    for _ in range(n):
        print(maximum)

Submission status: Accepted

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
B=sorted(A)
f=B[-1]
s=B[-2]
q=A.index(f)

for i in range(q):
  print(f)
print(s)
for i in range(n-q-1):
  print(f)

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:
  print(b[1] if i == b[0] else b[0])

Submission status: Accepted

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

Submission status: Accepted

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

mx2 = -1
cnt = 0
for i in range(N):
    if A[i] == mx1:
        cnt += 1
    else:
        mx2 = max(mx2, A[i])
    if cnt == 2:
        break
if mx2 == -1:
    mx2 = mx1
for i in range(N):
    if cnt >= 2:
        print(mx1)
    else:
        if A[i] == mx1:
            print(mx2)
        else:
            print(mx1)

Submission status: Accepted

Solution code:
import sys

def main():
    N = int(input())
    As = list(map(int, sys.stdin.readlines()))

    max_a = max(As)
    max_count = As.count(max_a)

    if max_count > 1:
        for _ in range(N):
            print(max_a)
        sys.exit()
    
    _As = As[:]
    _As.remove(max_a)
    max_a2 = max(_As)

    for i in As:
        if i == max_a:
            print(max_a2)
        else:
            print(max_a)
    
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

n = int(input())

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

a_s = sorted(a, reverse=True)
a_max = a_s[0]
a_max_2 = a_s[1]

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

Submission status: Accepted

Solution code:
N=int(input())
fir=0
sec=0
adr=[]
for i in range(N):
  a=int(input())
  if a==fir:
    adr.append(i)
  elif a>fir:
    sec=fir
    fir=a
    adr=[i]
  elif a<fir:
    if a>sec:
      sec=a
if len(adr)==1:
  for i in range(N):
    if i!=adr[0]:
      print(fir)
    else:
      print(sec)
else:
  for i in range(N):
    print(fir)

Submission status: Accepted

Solution code:
N=int(input())

As = [int(input()) for _ in range(N)]
top2 = sorted(As)[-2:]
for A in As:
    if A==top2[1]:
        print(top2[0])
    else:
        print(top2[1])

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = []
for i in range(N):
  a.append(int(input()))
aa = np.array(a)
max_a = max(aa)
k = np.argmax(aa)
aa[k] = 0
m = max(aa)


for i in range(N):
  if i == k:
    print(m)
  else:
    print(max_a)

Submission status: Accepted

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

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

copy_values = [v for v in values]

copy_values.remove(a)

if len(copy_values) == 0:
  b = a
else:
  b = max(copy_values)

for i in range(N):
  if values[i] == a:
    print(b)
  else:
    print(a)

Submission status: Accepted

Solution code:
N = int(input())

firstMax = 0
secondMax = 0
firstMaxIndex = -1

for i in range(N):
    num = int(input())
    if firstMax < num:
        firstMax = num
        firstMaxIndex = i
    elif secondMax < num:
        secondMax = num

for i in range(N):
    if i == firstMaxIndex:
        print(secondMax)
    else:
        print(firstMax)

Submission status: Accepted

Solution code:
import sys
import copy


def input():
    return sys.stdin.readline()[:-1]


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    a2 = copy.copy(a)
    a2.sort()
    m = a2[len(a)-1]
    m2 = a2[len(a)-2]

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

new_list=sorted(A)
second=new_list[N-2]


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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


def resolve():
    N = [int(x) for x in sys.stdin.readline().split()][0]
    a_list = [[int(x) for x in sys.stdin.readline().split()][0]
              for _ in range(N)]

    logger.debug('{} {}'.format(N, a_list))

    max1, max2 = sorted(a_list, reverse=True)[:2]

    for a in a_list:
        if a == max1:
            print(max2)

        else:
            print(max1)


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
1
4
3"""
        output = """4
3
4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2
5
5"""
        output = """5
5"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """2
1
1"""
        output = """1
1"""
        self.assertIO(input, output)

Submission status: Accepted

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


ans = [0]*N
a1 = max(A)
cnt = A.count(a1)

for i in range(N):
    if (A[i]==a1)and(cnt==1):
        B = copy.deepcopy(A)
        del(B[i])
        ans[i]=max(B)
    else:
        ans[i]=a1

for i in range(N):
    print(ans[i])

Submission status: Accepted

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

    mx = max(A)
    sec = sorted(A)[-2]
    for i in range(N):
        s = A[i]
        if mx != s:
            print(mx)
        else:
            print(sec)

resolve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
n = int(input())
a_cnt = defaultdict(int)
a_list = [0 for _ in range(n)]
for i in range(n):
    num = int(input())
    a_cnt[num] += 1
    a_list[i] = num

a_sort = sorted(a_cnt.keys(), reverse=True)

largest_num = a_sort[0]
if len(a_sort) == 1:
    second_num = largest_num
else:
    second_num = a_sort[1]

if a_cnt[largest_num] == 1:
    for num in a_list:
        if largest_num == num:
            print(second_num)
        else:
            print(largest_num)
else:
    for _ in a_list:
        print(largest_num)

Submission status: Accepted

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

# http://tjkendev.github.io/procon-library/python/range_query/rmq_segment_tree.html
# N: 処理する区間の長さ
N0 = 2**(N-1).bit_length()
INF = 2**31-1
data = [0]*(2*N0)

# a_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])

# 区間[l, r)の最小値
def query(l, r):
    L = l + N0
    R = r + N0
    s = 0
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])

        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1
        R >>= 1
    return s


for i, a in enumerate(A):
    update(i, a)

l, r = 0, 1
for _ in range(N):
    a, b = query(0, l), query(r, N)
    print(max(a, b))
    l += 1
    r += 1

Submission status: Accepted

Solution code:
n = int(input())

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

t = a
a = sorted(a)
memo = a[n-1]

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

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

Submission status: Accepted

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

M = max(l)
ind = l.index(M)
if ind==0:
    m = max(l[1:])
elif ind==N-1:
    m = max(l[:N-1])
else:
    m = max(max(l[0:ind]),max(l[ind+1:]))
for i in range(N):
    if i == ind:
        print(m)
    else:
        print(M)

Submission status: Accepted

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

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

max_a = max(A)
aa = sorted(A, reverse=True)
second = aa[1]
flag = second == max_a

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

Submission status: Accepted

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

if count > 1:
    for n in range(N):
        print(max_a)
else:
    for n in range(N):
        if A[n] == max_a:
            print(max(A[:n] + A[n+1:]))
            continue
        print(max_a)

Submission status: Accepted

Solution code:
N = int(input())

maxNum= 0
maxIndex= -1
secondNum= 0



for i in range(N):
    tmp= int(input())
    if tmp> maxNum :
        secondNum= maxNum
        maxNum= tmp
        maxIndex= i
    elif tmp> secondNum:
        secondNum= tmp



for j in range(N):
    if(j == maxIndex):
        print(secondNum)
    else :
        print(maxNum)

Submission status: Accepted

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

Submission status: Accepted

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

B = sorted(A)
m, n = B[N - 2], B[N - 1]
tie = False
if m == n:
  tie = True

for i in range(N):
  if A[i] == n:
    if tie:
      print(n)
    else:
      print(m)
  else:
    print(n)

Submission status: Accepted

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

b = sorted(a)

t1 = b[-1]
t2 = b[-2]

for i in a:
    if i == t1:
        print(t2)
    else:
        print(t1)

Submission status: Accepted

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

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

A_sorted=sorted(A,reverse=True)

for i in A:
	if i==A_sorted[0]:
		print(A_sorted[1])
	else:
		print(A_sorted[0])

Submission status: Accepted

Solution code:
import sys
p = [int(r) for r in  sys.stdin.read().split('\n') if r != '']
N = p[0]
A = p[1:]
b = sorted(A)
m = b[-1]
l = b[-2]
for i, n in enumerate(A):
    print(m if A[i] != m else l)

Submission status: Accepted

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

for a in A:
  if a==max_num: print(second_num)
  else: print(max_num)

Submission status: Accepted

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

Amax = -1
Amax2 = -1
index = -1
index2 = -1

for i in range(N) :
    if Amax < A[i] :
        Amax = A[i]
        index = i

if A.count(Amax) > 1 :
    for i in range(N) :
        print(Amax)
    exit()

for i in range(N) :
    if i == index :
        continue
    if Amax2 < A[i] :
        Amax2 = A[i]
        index2 = i

for i in range(N) :
    if i == index :
        print(Amax2)
    else :
        print(Amax)

Submission status: Accepted

Solution code:
from sys import stdin

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

    a = sorted(A, reverse=1)
    for i in A:
        if i < a[0]:
            print(a[0])
        else:
            print(a[1])

input = lambda: stdin.readline()
main()

Submission status: Accepted

Solution code:
N=int(input())

A=[]

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

    A.append(d)

A.sort(key=lambda x:x[0])

#print(A)

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
sorted_A = sorted(A)
max_num = sorted_A[-1]
for a in A:
    if a == max_num:
        print(sorted_A[-2])
    else:
        print(max_num)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = [0,0]
index = 0

for i in range(N):
	A = int(input())
	if A > S[0]:
		S[1] = S[0]
		S[0] = A
		index = i
	elif A > S[1]:S[1] = A

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = [int(input()) for _ in range(N)]
MA = max(A)
ans = [MA] * N
MAI = np.argmax(np.array(A))
A[MAI] = 1
MA2 = max(A)
ans[MAI] = MA2
for a in ans :
    print(a)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
B = [0]*(N+2)
C = [0]*(N+2)
for i in range(1,N+1):
    B[i] = max(A[i-1], B[i-1])
    C[-i-1] = max(A[-i], C[-i])
for i in range(1,N+1):
    print(max(B[i-1], C[i+1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def main():

	N = int(input())
	# = list(input())
	#L,R = map(int, input().split(" "))
	#L = list(map(int, input().split(" ")))

	A = []
	a_max = 0
	for i in range(N):
		tmp = int(input())
		A.append(tmp)
		if a_max < tmp:
			a_max = tmp

	B = copy.deepcopy(A)
	B.sort()
	for i in A:
		if i == a_max:
			print(B[-2])
		else:
			print(a_max)



if __name__ == "__main__":
	main()

Submission status: Accepted

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

s = sorted(A)
ans = []

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

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
A_Sorted = sorted(A)
A_max = A_Sorted[n-1]
A_sec = A_Sorted[n-2]

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

Submission status: Accepted

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

#first_max_index = -1
#second_max_index = -1

for i in range(N):
    if A[i] > first_max:
        second_max = first_max
        first_max = A[i]
        #first_max_index = i
    elif A[i] > second_max:
        second_max = A[i]

for i in range(N):
    if A[i] == first_max:
        print(second_max)
    else:
        print(first_max)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
    
MAX1 = max(A)
MAX2 = -1
index = A.index(MAX1)
for i in range(N):
    if i != index:
        MAX2 = max(MAX2,A[i])
        
if MAX1 == MAX2:
    for i in range(N):
        print(MAX1)
else:
    for i in range(N):
        if i == index:
            print(MAX2)
        else:
            print(MAX1)

Submission status: Accepted

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


N = int(input())
A = [int(input()) for i in range(N)]
A.append(-1)
ma = N
na = N
for i in range(N):
    if A[i] > A[ma]:
        na = ma
        ma = i
    elif A[i] > A[na]:
        na = i

for i in range(N):
    if i == ma:
        print(A[na])
    else:
        print(A[ma])

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
def input(): return sys.stdin.readline().strip()
def gcd(x, y): return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y): return (x * y) // gcd(x, y)
MOD = 10 **9 + 7

N = int(input())
# S = input()
# N, M = map(int, input().split())
# 横
# L = [int(x) for x in input().split()]
# 縦
A = [[i, int(input())] for i in range(N)]
# 多次元
# L = [[int(x) for x in input().split()] for _ in range(N)]

A = sorted(A, key=lambda x : x[1], reverse=True)

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

Submission status: Accepted

Solution code:
# -*- coding utf-8 -*-
import copy
N = int(input())

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

m = max(A)
c = A.count(m)

for a in A:
    if a != m:
        print(m)
    else:
        if c > 1:
            print(m)
        else:
            l = copy.deepcopy(A)
            l.remove(m)
            print(max(l))

Submission status: Accepted

Solution code:
n=int(input())
hoge=[]
huga=[]
for i in range(n):
  x=int(input())
  hoge.append(x)
  huga.append(x)
huga.sort(reverse=True)
x=huga[0]
y=huga[1]
if(huga[0]==huga[1]):
  for i in range(n):
    print(x)
else:
  for i in range(n):
    if(hoge[i]==huga[0]):
      print(y)
    else:
      print(x)

Submission status: Accepted

Solution code:
import heapq

N = int(input())
h = []
ax = []
for _ in range(N):
    a = int(input())
    heapq.heappush(h,a* (-1))
    ax.append(a)

m1 = heapq.heappop(h) * (-1)
m2 = heapq.heappop(h) * (-1)

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