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 a in range(N)]
maxnum = max(A)
maxcount = A.count(maxnum)
s = sorted(A,reverse=True)

if maxcount == 1:
  index = A.index(maxnum)
  ans = [maxnum] * N
  ans[index] = s[1]
else:
  ans = [maxnum] * N
for a in ans:
  print(a)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("## ",*x,file=sys.stderr)
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    A = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列

    left=[]
    cur=0
    for a in A:
        left.append(cur)
        cur=max(cur,a)
        
    cur=0
    right=[]
    for a in reversed(A):
        right.append(cur)
        cur=max(cur,a)
    for i in range(N):
        print(max(left[i],right[-i-1]))
    
    


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
aaa = [int(input()) for _ in range(N)]
MAX = max(aaa)
NEX = sorted(aaa)[-2]
for a in aaa:
    if a < MAX:
        print(MAX)
    else:
        print(NEX)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
b = a[:]
first = max(a)
idx = b.index(first)
b[idx] = 0
second = max(b)

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

Submission status: Accepted

Solution code:
def readinput():
    n=int(input())
    a=[]
    for i in range(1,n+1):
        a.append((int(input()),i))
    return n,a

def main(n,a):
    sa=sorted(a,key=lambda x:x[0], reverse=True)
    for i in range(1,n+1):
        if sa[0][1]!=i:
            print(sa[0][0])
        else:
            print(sa[1][0])            

if __name__=='__main__':
    n,a=readinput()
    main(n,a)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
maxa = max(a)
# print(maxa)
if a.count(maxa) == 1:
    b = sorted(a, reverse=True)

    maxa2 = b[1]
    for i in range(n):
        if a[i] == maxa:
            print(maxa2)
        else:
            print(maxa)

else:
    for i in range(n):
        print(maxa)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
m1 = max(A[0], A[1])
m2 = min(A[0], A[1])
for i in range(2,N):
    if m1 < A[i]:
        m1, m2 = A[i], m1
    elif m2 < A[i]:
        m2 = A[i]
print("\n".join(map(str, [m1 if i != m1 else m2 for i in A])))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
A_sort = sorted(A)
M_1, M_2 = A_sort[-1], A_sort[-2]
for a in A:
  if a == M_1:
    print(M_2)
  else:
    print(M_1)

Submission status: Accepted

Solution code:
n = int(input())
A = []
f = 0
s = 0
for _ in range(n):
  a = int(input())
  A.append(a)
  if f <= a:
    s = f
    f = a
  elif s <= a:
    s = a
for a in A:
  if a == f:
    print(s)
  else:
    print(f)

Submission status: Accepted

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

for _ in range(N):
    tmp = int(input())
    A.append(tmp)
    if tmp > max1:
        max1 = tmp
    elif tmp > max2:
        max2 = tmp

for i in range(N):
    if A[i] == max1:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
N = int(input())
As = []
for i in range(N):
  A = int(input())
  As.append(A)
Ass = sorted(As)
for i in range(N):
  if As[i]<Ass[-1]:
    print(Ass[-1])
  else:
    print(Ass[-2])

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


def resolve():
    # s = input()
    n = int(input())
    a = [int(input()) for _ in range(n)]
    # l = [list(map(int, input().split())) for _ in range(n)]
    aa = sorted(a)

    max_1st = aa[-1]
    max_2nd = aa[-2]

    for i, a_i in enumerate(a):
        if max_1st == a_i:
            print(max_2nd)
        else:
            print(max_1st)


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)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
import math
N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
  A[i]= int(input())
B = sorted(A,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().rstrip())
A = []
mx1 = 0
mx2 = 0
for i in range(N):
    A.append(int(input().rstrip()))
    if A[i] > mx1:
        mx1, mx2 = A[i], mx1
    elif A[i] > mx2:
        mx2 = A[i]
for j in range(N):
    if A[j] == mx1:
        print(mx2)
    else:
        print(mx1)

Submission status: Accepted

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

B=sorted(A)

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    inlis = []
    for _ in range(n):
        a = int(input())
        inlis.append(a)
    saidai = max(inlis)
    kosuu = inlis.count(saidai)
    inlis2 = sorted(inlis)
    saidai2 = inlis2[-2]
    for i in range(n):
        if inlis[i] != saidai:
            print(saidai)
        else:
            print(saidai2)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
number = []
for i in range(N):
    number.append(int(input()))
sorted_number = sorted(number,reverse=True)
max_number = sorted_number[0]
second_number = sorted_number[1]

for i in number:
    if i != max_number:
        print(max_number)
    else:
        print(second_number)

Submission status: Accepted

Solution code:
#39
import copy
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
    
Max = max(a)
b = copy.copy(a)
b.remove(Max)
sub = max(b)

for i in a:
    if i !=Max:
        print(Max)
    else:
        print(sub)

Submission status: Accepted

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

Submission status: Accepted

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

B = sorted(A, reverse = True)
max1 = B[0]
max2 = B[1]
for a in A:
    print(max2 if a == max1 else max1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
A_max = 0
for i in range(N):
  A_max = max(A_max,A[i])
cnt = 0
for j in range(N):
  if A_max == A[j]:
    cnt += 1
  if cnt == 2:
    for _ in range(N):
      print(A_max)
    exit()
A[A.index(A_max)] = 0
A_second = 0
for l in range(N):
  A_second = max(A_second,A[l])
for m in range(N):
  print(A_second if A[m] == 0 else 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)
max1 = b[0]
max2 = b[1]
for i in a:
    print(max2 if i == max1 else max1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
b=sorted(a)
if b[-1]==b[-2]:f=1
else:f=0
for i in a:
  if f:print(b[-1])
  else:
    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)]
sorted_list = sorted(a, reverse=True)
a_max = sorted_list[0]
a_sec = sorted_list[1]
for i in range(n) :
    if a[i] != a_max :
        print(a_max)
    else :
        print(a_sec)

Submission status: Accepted

Solution code:
import math
import collections
def main():
    n = int(input())
    s = []
    dd = collections.defaultdict(int)
    for i in range(n):
        a  = int(input())
        dd[a]+=1
        s.append(a)
    k = max(dd.keys())
    lis = sorted(list(dd.keys()),reverse = True)

    for i in range(n):
        if(s[i]==k and dd[s[i]]==1):
            print(lis[1])
        else:
            print(k)






if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
for v in a:
  print(b[(v<b[-1])-2])

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = [int(input()) for _ in range(n)]
b = copy.deepcopy(a)
a.sort(reverse=True)
for v in b:
  if v==a[0]:
    print(a[1])
  else:
    print(a[0])

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    sorted_A = sorted(A, reverse=True)
    ans = []
    for a in A:
        if a == sorted_A[0]:
            ans.append(str(sorted_A[1]))
        else:
            ans.append(str(sorted_A[0]))
    print("\n".join(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = [int(input()) for A in range(n)]
max_a,secound_max_a = max(a),0
b = copy.copy(a)
b.sort()
bol = True
for j in range(n,n,-1):
    if b[j] != max(a):
        secound_max_a = b[j]
        bol = False
        break
if bol:
    secound_max_a = b[-2]

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created: Jul, 13, 2020 08:21:54 by Nobody
# $Author$
# $Date$
# $URL$
__giturl__ = "$URL$"


from sys import stdin
input = stdin.readline


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

  B = sorted(A)
  max_ = B[-1]

  for x in A:
    if x == max_:
      print(B[-2])
    else:
      print(max_)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    A = []
    first = (0, 0)
    sec = 0
    for i in range(N):
        a = int(input())
        A.append(a)
        if first[0] <= a:
            sec = first[0]
            first=(a, i)
        elif sec <a:
            sec =a

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for _ in range(n):
  l.append(int(input()))
m=max(l)
mc=l.count(m)
tin=set(l)
if len(tin)==1:
  s=m
else:
  s=sorted(tin)[-2]
for i in l:
  if i==m:
    if mc>1:
      print(m)
    else:
      print(s)
  else:
    print(m)

Submission status: Accepted

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

first_max = max(b)
b.remove(first_max)
second_max = max(b)

for x in a:
    if x == first_max:
        print(second_max)
    else:
        print(first_max)

Submission status: Accepted

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

a_sorted = sorted(a)
a_max1 = a_sorted[-1]
a_max2 = a_sorted[-2]
for a_i in a:
    if a_i == a_max1:
        print(a_max2)
    else:
        print(a_max1)

Submission status: Accepted

Solution code:
from heapq import nlargest

n = int(input())
a = [int(input()) for i in range(n)]
b = nlargest(2, a)
ma = max(a)
for j in a:
  if j == ma:
    print(b[1])
  else:
    print(ma)

Submission status: Accepted

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

Submission status: Accepted

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

B=sorted(A,reverse=True)
A1=B[0]
A2=B[1]

for i in range(N):
  if A[i]==A1:
    print(A2)
  else:
    print(A1)

Submission status: Accepted

Solution code:
N = int(input())
x = int(input())
y = int(input())
A = [x, y]
if x < y:
    x, y = y, x
for i in range(N - 2):
    a = int(input())
    A.append(a)
    if x <= a:
        y = x
        x = a
    elif y < a:
        y = a
if x == y:
    for i in range(N):
        print(x)
else:
    for a in A:
        if a == x:
            print(y)
        else:
            print(x)

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N = ri()
A = []
for i in range(N):
	A.append(ri())
m = max(A)
if len([a for a in A if a == m]) > 1:
	for i in range(N):
		print(m)
else:
	m2 = -1
	i2 = -1
	for i, a in enumerate(A):
		if a == m:
			continue
		if m2 < a:
			m2 = a
			i2 = i
	for a in A:
		if a == m:
			print(m2)
		else:
			print(m)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
max_a=max(a)
index=a.index(max_a)
a.pop(index)
for i in range(n):
    if i ==index:
        print(max(a))
    else:
        print(max_a)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input())for _ 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 = [int(input()) for j in range(N)]

B = sorted(A)
a = B[-1]
b = B[-2]
c = A.index(a)
for j in range(N):
    if j == c:
        print(b)
        continue
    print(a)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
import copy

def solve():
    a = copy.copy(A)
#    print(a)
    a.sort(reverse=True)
#    print(a)
#    print(A)
    if a[0] == a[1]:
        for i in range(N):
            print(a[0])
    else:
        for i in range(N):
            if A[i] == a[0]:
                print(a[1])
            else:
                print(a[0])
    

if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#create date: 2020-07-07 11:02

import sys
stdin = sys.stdin
from heapq import heapify, heappush, heappop

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = list()
    for _ in range(n):
        a.append(ni())
    b = sorted(a, reverse=True)
    amax = b[0]
    asec = b[1]
    for ai in a:
        if ai == amax:
            print(asec)
        else:
            print(amax)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
l=[0]*n
r=[0]*n
t=0
for i in range(n):
  t=max(t,a[i])
  l[i]=t
t=0
for i in range(n-1,-1,-1):
  t=max(t,a[i])
  r[i]=t
# print(l)
# print(r)
for i in range(n):
  if i==0:
    print(r[i+1])
    continue
  if i==n-1:
    print(l[i-1])
    continue
  print(max(r[i+1],l[i-1]))

Submission status: Accepted

Solution code:
n, *aa = map(int, open(0).read().split())

bb = sorted(aa)

f, s = bb[-1], bb[-2]

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = [I() for _ in range(N)]
    
    A_first = max(A)
    A_second = 0
    if A.count(A_first) >= 2:
        A_second = A_first
    else:
        for i in A:
            if i != A_first:
                A_second = max(i, A_second)

    for i in A:
        if i == A_first:
            print(A_second)
        else:
            print(A_first)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

new_l = sorted(l)

fmax = new_l[N-1]
fmax2 = new_l[N-2]
smax = fmax

for j in reversed(range(N)):
    if not new_l[j] == fmax:
        smax = new_l[j]
        break

if fmax == fmax2:
    for k in range(N):
        print(fmax)
else:
    for m in range(N):
        if l[m] == fmax:
            print(smax)
        else:
            print(fmax)

Submission status: Accepted

Solution code:
n = int(input())

max_1 = 0
max_2 = 0
max_index = 0

for i in range(n):
    item = int(input())
    if item >= max_1:
        max_1, max_2 = item, max_1
        max_index = i
    elif item > max_2:
        max_2 = item

for i in range(n):
    ans = max_2 if i == max_index else max_1
    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]
max1 = max(A)
A.remove(max1)
max2 = max(A)

for i in B:
    if i == max1:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    A = [int(input()) for i in range(n)]
    B = sorted(A)
    mx = B[-1]
    second = B[-2]
    for a in A:
        if a == mx:
            print(second)
        else:
            print(mx)
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

amax1 = max(a)
amax_i1 = a.index(amax1)

a.pop(amax_i1)
amax2 = max(a)
amax_i2 = a.index(amax2)

for i in range(n):
    if i == amax_i1:
        print(amax2)
    else:
        print(amax1)

Submission status: Accepted

Solution code:
N = int(input())
As = [int(input()) for i in range(N)]
Bs = sorted(As, reverse = True)
ma = Bs[0]
ma2 = Bs[1]

for a in As:
  print(ma if a != ma else ma2)

Submission status: Accepted

Solution code:
N = int(input())

maxL = [0] * (N+1)
maxR = [0]*(N+1)
num = []
for i in range(N):
    num.append(int(input()))
    maxL[i+1] = maxL[i] if maxL[i] > num[i] else num[i]

for i in range(N-1, 0, -1):
    maxR[i-1] = maxR[i] if maxR[i] > num[i] else num[i]

for i in range(N):
    print(max(maxL[i], maxR[i]))

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    sortedA = sorted(A)
    for a in A:
        if a == sortedA[-1]:
            print(sortedA[-2])
        else:
            print(sortedA[-1])

    
if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
a = []
for _ in range(n):
  a_ = int(input())
  a.append(a_)
a1 = sorted(a)
ans1 = a1[-1]
ans2 = a1[-2]
for i in range(n):
  if a[i] == ans1:
    print(ans2)
  else:
    print(ans1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n,*a=map(int, open(0).read().split())
s = sorted(a)
for i in a:
    if i!=s[-1]:
        print(s[-1])
    else:
        print(s[-2])

Submission status: Accepted

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

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

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

Submission status: Accepted

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

A_sort = sorted(A)
A_max = A_sort[-1]
A_max2 = A_sort[-2]

for i in range(N):
    if A[i]==A_max:
        print(A_max2)
    else:
        print(A_max)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
a_counter=Counter(a)
a_max=max(a)
b=a_counter[a_max]
for i in range(n):
  if a[i]!=a_max:
    print(a_max)
  else:
    if b>=2:
      print(a_max)
    else:
      c=a[0:i]+a[i+1:]
      print(max(c))

Submission status: Accepted

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

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

ans=0
for i in range(N):
  ans=max(left[i],right[i+1])
  print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = np.zeros((N))
for i in range(N):
  a[i] = int(input())
A_i = np.argsort(a)
A = np.sort(a)
for j in range(N):
  if(j==A_i[N-1]):
    print(int(A[N-2]))
  else:
    print(int(A[N-1]))

Submission status: Accepted

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

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

al.sort(reverse=True)

for i in range(1,n+1):
    if al[0][1] == i:
        print(al[1][0])
    else:
        print(al[0][0])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = []
    for _ in range(n):
        a.append(int(input()))
    max_l, max_r = [a[0]], [a[-1]]
    for i in range(1, n):
        max_l.append(max(max_l[i-1], a[i]))
        max_r.append(max(max_r[i-1], a[-(i+1)]))
    max_r = max_r[::-1]
    for i in range(n):
        if i == 0:
            print(max_r[1])
        elif i == n - 1:
            print(max_l[-2])
        else:
            print(max(max_l[i-1], max_r[i+1]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
List=[]
for i in range(n):
    a=int(input())
    List.append(a)
mylist=sorted(List)
max_num=mylist[-1]
max_num_2=mylist[-2]
for i in range(n):
    if List[i]!=max_num:
        print(max_num)
    else:
        print(max_num_2)

Submission status: Accepted

Solution code:
n = int(input())
a = ['']*n
for i in range(n):
    a[i] = int(input())
_a = sorted(a, reverse=1)
a_max = _a[0]
a_second = _a[1]
ans = [a_max] * n

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

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

Submission status: Accepted

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

n = int(input())

max1 = 0
max2 = 0
maxindex = -1

for i in range(n):
    a = int(input())
    if a >= max1:
        max2 = max1
        max1 = a
        maxindex = i
    elif a >= max2:
        max2 = a

out = [max1] * n
out[maxindex] = max2

for m in out:
    print(m)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
  a = int(input())
  A.append(a)
  
A2 = sorted(A)
mx1 = A2[-1]
mx2 = A2[-2]
ind = A.index(mx1)

for i in range(N):
  if i != ind:
    print(mx1)
  else:
    print(mx2)

Submission status: Accepted

Solution code:
import heapq

N=int(input())
A=[]
B=[]
heapq.heapify(B)
for _ in range(N):
    a=int(input())
    A.append(a)
    heapq.heappush(B,-a)

X=-heapq.heappop(B)
Y=-heapq.heappop(B)

for i in range(N):
    if A[i]==X:
        print(Y)
    else:
        print(X)

Submission status: Accepted

Solution code:
import copy
n = int(input())
lst = []
for i in range(n):
  lst.append(int(input()))
slst = copy.deepcopy(lst)
slst.sort(reverse=True)
for i in range(n):
  print(slst[1] if lst[i] == slst[0] else slst[0])

Submission status: Accepted

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

Submission status: Accepted

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

temp=sorted(A,reverse=True)
maxi=temp[0]
secmaxi=temp[1]

for a in A:
    if a!=maxi:
        print(maxi)
    else:
        print(secmaxi)

Submission status: Accepted

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

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

right = [0]
left =[0]

for a in A:
    left.append(max(a,left[-1]))
    
for a in reversed(A):
    right.append(max(a,right[-1]))

    
for i in range(N):
    ans = max(left[i], right[N-i-1] )
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
B=sorted(A)
M=max(A)
count=A.count(M)
for i in range(N):
  if count>1:
    print(M)
  elif A[i]!=M:
    print(M)
  else:
    print(B[-1-count])

Submission status: Accepted

Solution code:
N = int(input())
A = [0]*N
for I in range(0,N):
    A[I] = int(input())
    
Disp = [max(A)]*N
Sort = sorted(A,reverse=True)[:2]
if Sort[0]==Sort[1]:
    print('\n'.join(map(str,Disp)))
else:
    Disp[A.index(max(A))] = Sort[1]
    print('\n'.join(map(str,Disp)))

Submission status: Accepted

Solution code:
import sys

N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
M = sorted(A)
if M[-1] == M[-2]:
    for i in range(N):
        print(M[-1])
    sys.exit()
high_num = M[-1]
for i in range(N):
    if A[i] == high_num:
        print(M[-2])
    else:
        print(M[-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

N = int(input())
An = [int(input()) for i in range(N)]
Asort = sorted(An, reverse=True)

for A in An:
    if A != Asort[0]:
        print(Asort[0])
    else:
        print(Asort[1])

Submission status: Accepted

Solution code:
import copy
n=int(input())
tmp=0
a=[]
for i in range(n):
    a.append(int(input()))
s=copy.deepcopy(a)
s.sort(reverse=True)
t=s[0]
tt=s[1]
for i in range(n):
    if a[i]==t:
        print(tt)
    else:
        print(t)