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:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

import re
# import numpy as np
# from scipy.misc import perm
# from scipy.misc import comb

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 # 10^9 + 7

########

N = i_inpl()
A = line_inpl(N)

max_val = max(A)
s_max = sorted(A)[-2]
max_cnt = A.count(max_val)

for Ai in A:
    if Ai == max_val and max_cnt == 1:
        print(s_max)
    else:
        print(max_val)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



N = int(input())
An = [int(input()) for _ in range(N)]
mxAn = [0,0]
for a in An:
  if mxAn[0] < a:
    mxAn = sorted([mxAn[1],a])

for a in An:
  if a == mxAn[1]:
    print(mxAn[0])
  else:
    print(mxAn[1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
# print(N, A)
max1_a = -1
max1_i = -1
max2_a = -1
max2_i = -1

for i in range(N):
    a = A[i]
    if a >= max1_a:
        max1_a = a
        max1_i = i

for i in range(N):
    if i == max1_i:
        continue
    a = A[i]
    if a >= max2_a:
        max2_a = a
        max2_i = i

for i in range(N):
    if i == max1_i:
        print(max2_a)
    else:
        print(max1_a)

Submission status: Accepted

Solution code:
n = int(input())

max = 0
next = 0
a = []
for i in range(n):
    temp = int(input())
    a.append(temp)
    if max < temp:
        next = max
        max = temp
    elif next < temp:
        next = temp

#print(a)
#print(max,next)

for i in a:
    if i == max:
        print(next)
    else:
        print(max)

Submission status: Accepted

Solution code:
import sys, math

readline = sys.stdin.readline

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

top1 = max(A)
top1_ind = A.index(top1)

A[top1_ind] = -1
top2 = max(A)
A[top1_ind] = top1

for i in range(N):
    if i == top1_ind:
        print(top2)
    else:
        print(top1)

Submission status: Accepted

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

Submission status: Accepted

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

a_sorted = sorted(ai)
a_max = a_sorted[-1]
a_max2 = a_sorted[-2]
for i in range(0, n):
    if ai[i] == a_max:
        print(a_max2)
    else:
        print(a_max)

Submission status: Accepted

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

Ar = [-1] + list(accumulate(A, max)) + [-1]
Al = [-1] + list(accumulate(A[::-1], max))[::-1] +[-1]
Ans = [None]*(N+1)
for i in range(1, N+1):
    Ans[i] = max(Ar[i-1], Al[i+1])
print('\n'.join(map(str, Ans[1:])))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
import math
mod = 10**9 + 7
def I(): return int(input())
def II(): return map(int, input().split())
def L(): return list(map(int, input().split()))
def TWO(M):
  L=[0]*M
  R=[0]*M
  for i in range(M):
    L[i],R[i] = II()
  return L, R

N = I()
A = [0]*N
for i in range(N):
  A[i] = I()
  
import numpy as np

index = np.argsort(A)

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

Submission status: Accepted

Solution code:
n = int(input())
max1 = 0
max2 = 0
max_index = 0
for i in range(n):
    a = int(input())
    if a >= max1:
        max2 = max1
        max1 = a
        max_index = i
    elif a > max2:
        max2 = a

for i in range(n):
    if i == max_index:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
import copy
n=int(input())
A=[int(input()) for _ in range(n)]
A_=copy.deepcopy(A)
A_.sort()
MA=A_[-1]
mA=A_[-2]
for i in range(n):
    if A[i]<MA:
        print(MA)
    else:
        print(mA)

Submission status: Accepted

Solution code:
N = int(input())

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

L = [-10**9]
m = -10**9

for i in range(N):
    m=max(m,A[i])
    L.append(m)


R = [-10**9]
m = -10**9

for i in range(N):
    m=max(m,A[N-i-1])
    R.append(m)

R = R[::-1]

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
first = 0
second = 0
n = -1
for i in range(N):
  a = int(input())
  if a >= first:
    second = first
    first = a
    n = i
  else:
    second = max(a, second)
for i in range(N):
  if i == n:
    print(second)
    continue
  print(first)

Submission status: Accepted

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

b_list = a_list.copy()
b_list.sort(reverse=True)
max_num = max(b_list)

for i in range(N):

    a = a_list[i]

    if a != max_num:
        print(max_num)
    else:
        f = True
        cnt = 0
        for b in b_list:
            if a != b:
                print(b)
                f = False
                break
            else:
                cnt += 1
                if cnt >= 2:
                    break
        if f:
            print(b)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(input()) for _ in range(n)]
a, b = sorted(l)[-2:]
print('\n'.join(str(a) if e == b else str(b) for e in l))

Submission status: Accepted

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

import heapq
m1, m2 = heapq.nlargest(2,A)

ans = [m1 if a!=m1 else m2 for a in A]
for a in ans:
  print(a)

Submission status: Accepted

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

first = 0
second = 0
for a in a_list:
    if a >= first:
        second = first
        first = a
    else:
        if a>= second:
            second = a

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
n = sorted(A, reverse=True)
x, x_ = n[0], n[1]

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
A=[]

append=A.append
for i in range(n):
  append(int(input()))
m1,m2=sorted(A)[-2],sorted(A)[-1]
for i in range(n):
  print(m1 if A[i]==m2 else m2)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
b = a[:]
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 _ in range(N)]

tmp=sorted(A)[::-1]
if tmp[0]==tmp[1]:
    for _ in range(N):
        print(tmp[0])
else:
    for i in range(N):
        if A[i]==tmp[0]:
            print(tmp[1])
        else:
            print(tmp[0])

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

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


acc1 = [0]*(n+1)
acc2 = [0]*(n+1)

for i in range(n):
    acc1[i+1] = max(acc1[i],a[i])
    acc2[-i-2] = max(acc2[-i-1],a[-i-1])

for i in range(n):
    print(max(acc1[i], acc2[i+1]))

#print(a)
#print(acc1)
#print(acc2)

Submission status: Accepted

Solution code:
N = int(input())
lst = []
for i in range(0, N):
	lst.append([i, int(input())])
lst.sort(lambda v: v[1], reverse = True)
index = lst[0][0]
for i in range(0, N):
	if i == index:
		print(lst[1][1])
	else:
		print(lst[0][1])

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for _ in range(n):
    x=int(input())
    a.append(x)
import numpy as np
A=np.array(a)
b=A.argsort()[::-1]
c=b[0]
for i in range(n):
    if i==c:
        print(a[b[1]])
    else:
        print(a[c])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

a, b = sorted(A, reverse=True)[:2]
for x in A:
    if x != a:
        print(a)
    else:
        print(b)

Submission status: Accepted

Solution code:
import heapq
N = int(input())
A = []
for i in range(N):
    A.append(int(input())) 
m = heapq.nlargest(2, A)
for a in A:
    if a == m[0]:
        print(m[1])
    else:
        print(m[0])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
max_a = 0
max2_a = 0
for i in range(N):
	a = int(input())
	A.append(a)
	if a >= max_a:
		max2_a = max_a
		max_a = a
	elif a >= max2_a:
		max2_a = a

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

Submission status: Accepted

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

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

Submission status: Accepted

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

sortA = sorted(A)
maxA = sortA[-1]
next_maxA = sortA[-2]

for i in range(N):
    if A[i] == maxA:
        print(next_maxA)
    else:
        print(maxA)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in [0]*n]
b,c=sorted(a,reverse=True)[0:2]
for A in a:
    if A==b:
        print(c)
    else:
        print(b)

Submission status: Accepted

Solution code:
n=int(input())
arr=[int(input()) for _ in range(n)]
m1=max(arr)
arr2=sorted(arr,reverse=True)
m2=0
cnt=0
for val in arr2:
  if val==m1:
    cnt+=1
  else:
    m2=val
    break
else:
  m2=m1
for val in arr:
  if cnt>=2:
    print(m1)
  else:
    if val==m1:
      print(m2)
    else:
      print(m1)

Submission status: Accepted

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

max_left = list()
max_right = list()

for i in range(n):
    if i == 0:
        max_left.append(As[i])
        max_right.append(As[-1 - i])
    
    else:
        max_left.append(max(max_left[-1], As[i]))
        max_right.append(max(max_right[-1], As[-1-i]))

max_right.reverse()

for i in range(n):
    if i == 0:
        print(max_right[i + 1])
    elif i == n - 1:
        print(max_left[i - 1])
    else:
        print(max(max_left[i - 1], max_right[i + 1]))

Submission status: Accepted

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

mA = max(A)
sA = sorted(A, reverse=True)

for a in A:
    if not a == mA:
        print(mA)
    else:
        print(sA[1])

Submission status: Accepted

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


MAX=[-1]

for n in A:
    MAX.append(max(n,MAX[-1]))

MAXINV=[-1]

for n in A[::-1]:
    MAXINV.append(max(n,MAXINV[-1]))

MAXINV=[-1]+MAXINV[::-1]


for i in range(1,N+1):
    print(max(MAX[i-1],MAXINV[i+1]))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
aa = []

for i in range(n):
    a = ni()
    aa.append(a)

bb = sorted(aa)
o = bb[-1]
p = bb[-2]

for i in range(n):
    k = p if aa[i] == o else o
    print(k)

Submission status: Accepted

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

B = sorted(A)

tmp1 = B[-1]
tmp2 = B[-2]

for i in range(N):
    if A[i] == tmp1:
        print(tmp2)
    else:
        print(tmp1)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

Ls = [0] + list(accumulate(As, max))
Rs = list(accumulate(As[::-1], max))[::-1] + [0]

anss = [max(Ls[i], Rs[i+1]) for i in range(N)]
print('\n'.join(map(str, anss)))

Submission status: Accepted

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

mx1 = max(A)
A_ = A[:]
A_.remove(mx1)
mx2 = max(A_)
for a in A:
    if a == mx1:
        print(mx2)
    else:
        print(mx1)

Submission status: Accepted

Solution code:
n = int(input())

nums = []
maxN = 0
secondN = 0

for i in range(n):
    num = int(input())
    nums.append(num)
    if maxN < num:
        secondN = maxN
        maxN = num
    elif secondN < num:
        secondN = num

for i in range(n):
    if nums[i] != maxN:
        print(maxN)
    else:
        if secondN == maxN:
            print(maxN)
        else:
            print(secondN)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys

sys.setrecursionlimit(100000)

MIN = -10 ** 9
MAX = 10 ** 9 + 7


def main():
    N = int(input())
    A = [
        int(input())
        for _ in range(N)
    ]
    # N, D = [int(a) for a in input().split()]
    b = sorted(A)
    l1, l2 = b[-1], b[-2]
    if l1 == l2:
        for i in range(N):
            print(l1)
        return
    for a in A:
        if a == l1:
            print(l2)
        else:
            print(l1)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
l=[int(input()) for i in range(n)]
L=sorted(l)
L.reverse()
x=L[0];y=L[1]
for i in range(n):
  if l[i]!=x:
    print(x)
  else:
    print(y)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
maxA = 0
secondMaxA = 0
maxIndex = -1
for i in range(N):
  A = int(input())
  if A >= maxA:
    secondMaxA = maxA
    maxA = A
    maxIndex = i
  elif A >= secondMaxA:
    secondMaxA = A

for i in range(N):
  if i == maxIndex:
    print(secondMaxA)
  else:
    print(maxA)

Submission status: Accepted

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

B = sorted(A)
s = ""
for b in A:
    if b != B[-1]:
        s += str(B[-1]) + "\n"
    else:
        s += str(B[-2]) + "\n"
print(s)

Submission status: Accepted

Solution code:
def main():

    """ convenient functions
    # for i, a in enumerate(iterable)
    # q, mod = divmod(a, b)
    # divmod(x, y) returns the tuple (x//y, x%y)
    # Higher-order function: reduce(operator.mul, xyz_count, 1)
    # manage median(s) using two heapq https://atcoder.jp/contests/abc127/tasks/abc127_f
    """

    """convenient decorator
    # @functools.lru_cache():
    # to facilitate use of recursive function
        # ex:
        # from functools import lru_cache
        # import sys
        # sys.setrecursionlimit(10**9)
        # @lru_cache(maxsize=None)
        # def fib(n):
        #     if n < 2:
        #         return n
        #     return fib(n-1) + fib(n-2)
        # print(fib(1000))
    """
    
    # import numpy as np
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product # https://docs.python.org/ja/3/library/itertools.html
    # accumulate() returns iterator! to get list: list(accumulate())
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  # prime factor
        b, e = 2, 0  # base, exponent
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        # TODO: How should I do when n - r is negative?
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict # https://docs.python.org/ja/3/library/collections.html#collections.deque
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
    from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
    import operator
    from operator import itemgetter
    # ex1: List.sort(key=itemgetter(1))
    # ex2: sorted(tuples, key=itemgetter(1,2))
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    # first create factorial_list
    # fac_list = mod_factorial_list(n)
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) # Recursive function in python is slow!
    def modinv(a, p = MOD):
        # evaluate reciprocal using Fermat's little theorem:
        # a**(p-1) is identical to 1 (mod p) when a and p is coprime
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        # fac_list = modfactorial_list(100)
        # print(modcomb(100, 5, modfactorial_list(100)))
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    # initialize variables
        # to initialize list, use [0] * n
        # to initialize two dimentional array, use [[0] * N for _ in range(N)]
    # set inputs
        # open(0).read() is a convenient method:
        # ex) n, m, *x = map(int, open(0).read().split())
        #     min(x[::2]) - max(x[1::2])
        # ex2) *x, = map(int, open(0).read().split())
        #     don't forget to add comma after *x if only one variable is used
    # preprocessing
        # transpose = [x for x in zip(*data)]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
        # flat = [flatten for inner in data for flatten in inner]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # calculate and output
        # output pattern
        # ex1) print(*l) => when l = [2, 5, 6], printed 2 5 6
    """

    # functions used
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    # single int: int(r())
    # single string: r()
    # single float: float(r())
    # line int: R()
    # line string: r().split()
    # line (str, int, int): [j if i == 0 else int(j) for i, j in enumerate(r().split())]
    # lines int: [R() for _ in range(n)]
    """

    if __name__ != '__main__':
        # for test
        sys.stdin = open('sample.txt')

    # ----------------------------------
    # main

    N = int(r())
    A = [int(r()) for _ in range(N)]
    B = sorted(A)
    if B[-1] == B[-2]:
        for _ in range(N):
            print(B[-1])
    else:
        for a in A:
            if a == B[-1]:
                print(B[-2])
            else:
                print(B[-1])
    
    # end of main
    # ----------------------------------

    """memo: how to use defaultdict of list
    # initialize
    Dic = defaultdict(list)
    # append / extend
    Dic[x].append(y)
    # for
    for k, v in Dic.items():
    """

if __name__ == '__main__':
    main()

Submission status: Accepted

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

A_sort = sorted(A, reverse=True)
A_1st = A_sort[0]
A_2nd = A_sort[1]

for a in A:
    if a == A_1st:
        print(A_2nd)
    else:
        print(A_1st)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

n = int(input())
a_list = [int(input()) for _ in range(n)]
sorted_list = sorted(a_list, reverse=True)
for a in a_list:
    check = sorted_list[0]
    if a == check:
        print(sorted_list[1])
    else:
        print(check)

Submission status: Accepted

Solution code:
import math
def Solve():
    L=[]
    N=int(input())
    for i in range(N):
        L.append(int(input()))

    mx=max(L)
    if L.count(mx)-1:#最大の個数が1個よりおおかったら
        for _ in L:
            print(mx)
    else: 
        for i,a in enumerate(L):
            if a==mx:
                K=L[:i]+L[i+1:]
                print(max(K))#i番目を除いた最大値
            else:
                print(mx)

Solve()

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
m1,m2,mi=0,0,-1
for i,a in enumerate(A):
    if a>m1:
        m1=a
        mi=i
    elif a>m2:
        m2=a
for i in range(N):
    print(m2 if i==mi else m1)

Submission status: Accepted

Solution code:
n = int(input())

biggest = 0
biggest_i = 0
next_biggest = 0
for i in range(n):
    x = int(input())
    if x >= biggest:
        next_biggest = biggest
        biggest = x
        biggest_i = i
    elif x >= next_biggest:
        next_biggest = x

for i in range(n):
    if i == biggest_i:
        print(next_biggest)
    else:
        print(biggest)

Submission status: Accepted

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

for item in a:
    if item == a_max and not multiple_max:
        print(a_second)
    else:
        print(a_max)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

N = int(input().strip())
As = [(int(input().strip()), i) for i in range(N)]
As.sort(reverse=True)
exception_index = As[0][1]
for i in range(N):
    if i == exception_index:
        print(As[1][0])
    else:
        print(As[0][0])

Submission status: Accepted

Solution code:
N = int(input())

import collections
A_list = []
A_collections = collections.Counter()
for i in range(N):
    A_list.append(int(input()))
    A_collections[A_list[-1]] += 1
    
max_num = max(A_list)
max_count = A_collections[max_num]
if(max_count > 1):
    for i in range(N):
        print(max_num)
else:
    A_collections.pop(max_num)
    for i in range(N):
        if(A_list[i] == max_num):
            print(max(A_collections))
        else:
            print(max_num)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a)
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)]

A_sort = sorted(A)
A_sort = A_sort[::-1]

maxim = A_sort[0]
maxim2 = A_sort[1]

flag = False
if maxim == maxim2:
    flag = True

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

Submission status: Accepted

Solution code:
N = int(input())

f = 0
s = 0
index = 0
for i in range(N):
    a = int(input())
    if a > f:
        index = i
        s = f
        f = a
    elif a > s:
        s = a

for i in range(N):
    if i == index:
        print(s)
    else:
        print(f)

Submission status: Accepted

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

sA.sort(reverse=True)

f = sA[0]
s = sA[1]

for i in range(N):
    if A[i] == f:
        print(s)
    else:
        print(f)

Submission status: Accepted

Solution code:
N = int(input())

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

m = max(A)
ind = A.index(m)
A_ = A[:ind]+A[ind+1:]
m_ = max(A_)

for i in range(N):
	if i != ind:
		print(m)
	else:
		print(m_)

Submission status: Accepted

Solution code:
n=int(input())
da=[int(input()) for i in range(n)]
x,y=0,0
for i in range(n):
    if da[i]>x:
        y=x
        x=da[i]
    else:
        if y<da[i]:
            y=da[i]

for i in range(n):
    if da[i]==x:
        print(y)
    else:
        print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = [int(input()) for i in range(int(input()))]
a_s = sorted(a)[::-1]
mx, submx = a_s[0], a_s[1]
cnt = a.count(mx)
for n in a:
    if n!=mx: print(mx)
    elif cnt>1: print(mx)
    else: print(submx)

Submission status: Accepted

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

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

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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