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 itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    sort_a = sorted(a)

    for v in a:
        if v == sort_a[-1]:
            print(sort_a[-2])
        else:
            print(sort_a[-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import numpy as np

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

from_left = np.maximum.accumulate(A)
from_right = np.maximum.accumulate(A[::-1])[::-1]

from_left, from_right

from_left = [0] + from_left.tolist()[:-1]
from_right = from_right[1:].tolist() + [0]

for x,y in zip(from_left, from_right):
    print(x if x > y else y)

Submission status: Accepted

Solution code:
n=int(input())
M=0
m=0
a_list=[]
for i in range(n):
    a=int(input())
    a_list.append(a)
    if a>M:
        temp=M
        M=a
        m=temp
    else:
        if a>m:
            m=a
for i in range(n):
    if a_list[i]==M:
        print(m)
    else:
        print(M)

Submission status: Accepted

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

import copy
b = copy.deepcopy(a)
b.sort(reverse=True)

m1 = b[0]
m2 = b[1]

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

Submission status: Accepted

Solution code:
import copy
N=int(input())

L=[]
for i in range(N):
    n=int(input())
    L.append(n)
A=copy.deepcopy(L)
A.sort(reverse=True)
#print(L)
#print(A)
for i in range(N):
    if L[i]==A[0]:
        print(A[1])
    else:
        print(A[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

def solve():
  N = int(input())
  A = [int(input()) for i in range(N)]
  first, second = heapq.nlargest(2, A)
  for a in A:
    print(first if a != first else second)

solve()

Submission status: Accepted

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

fst = np.max(lst)
fst_idx = np.argmax(lst)
lst.remove(fst)
snd = np.max(lst)

for i in range(N):
    if i == fst_idx:
        print(snd)
    else:
        print(fst)

Submission status: Accepted

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

B = sorted(A)
maxval = max(A)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = []
    A_ = A[:]
    A_.sort(reverse=True)
    for a in A:
        if a == A_[0]:
            ans.append(A_[1])
        else:
            ans.append(A_[0])
    return '\n'.join(map(str, ans))


def main():
    N = read_int()
    A = [read_int() for _ in range(N)]
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
fir,sec = -1,-1
fi = -1
for i,x in enumerate(a):
    if x > fir:
        fir,fi = x,i
    elif x > sec:
        sec = x
for i in range(n):
    if i == fi:
        print(sec)
        continue
    print(fir)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
a_ = sorted(a)
m_1 = a_[-1]
m_2 = a_[-2]

for i in a:
    if i == m_1:
        print(m_2)
    else:
        print(m_1)

Submission status: Accepted

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

Submission status: Accepted

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

k = max(range(N), key=lambda i: A[i])
for i in range(N):
    if i != k:
        print(A[k])
    else:
        print(
            max(
                max(A[:k], default=0),
                max(A[(k + 1):], default=0)
            )
        )

Submission status: Accepted

Solution code:

N = int(input())
nums = []
for _ in range(N):
    nums.append(int(input()))
temp = nums[:]
temp.sort(reverse=True)
ma = temp[0]
ma2 = temp[1]
# print(nums)
for i in nums:
    if i == ma:
        print(ma2)
    else:
        print(ma)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import copy


def solve(N: int, A: "List[int]"):
    B = copy.deepcopy(A)
    B.sort(reverse = True)
    B1 = B[0]
    B2 = B[1]

    for i in A:
        if i == B1:
            print(B2)
        else:
            print(B1)
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = [INT() for _ in range(N)]

A_sorted = sorted(A, reverse=True)
max_ = A_sorted[0]

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

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

n = getN()

nums = []
for i in range(n):
    nums.append(getN())

mx = 0
nxt = -1
dup = 1
for num in nums:
    if num == mx:
        dup += 1
    elif num > mx:
        nxt = mx
        mx = num
        dup = 1
    else:
        if num > nxt:
            nxt = num

if dup >= 2:
    for i in range(n):
        print(mx)

else:
    for num in nums:
        if num == mx:
            print(nxt)
        else:
            print(mx)

Submission status: Accepted

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

b = sorted(a, reverse=True)

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

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs,math
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): #intのlist
	return list(map(int,input().split()))
def m(): #複数文字
	return map(int,input().split())
def onem(): #Nとかの取得
	return int(input())
def s(x): #圧縮
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): #listをスペースごとに分ける
	return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
	return max(map(max,x))

n = onem()

a = []
b = []
for i in range(n):
	k = onem()
	a.append(k)
	b.append(k)

a.sort(reverse = True)

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

Submission status: Accepted

Solution code:
import copy
N = int(input())
A = []
for _ in range(N):
  A.append(int(input()))
a = max(A)
AA = copy.deepcopy(A)
AA.remove(a)
b = max(AA)

for hoge in A:
  if hoge == a:
    print(b)
  else:
    print(a)

Submission status: Accepted

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

ruiseki1 = [0] * (n+1)
ruiseki2 = [0] * (n+1)

for i in range(n):
    ruiseki1[i+1] = max(ruiseki1[i], a[i])
for i in range(n)[::-1]:
    ruiseki2[i] = max(ruiseki2[i+1], a[i])   


for i in range(n):
    print(max(ruiseki1[i], ruiseki2[i+1]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n=I()
a,b=[],[]
for i in range(n):
    a.append(I())
b=sorted(a)[::-1]
M=b[0]
m=b[1]
ans=0
for i in range(n):
    if a[i]==M:
        ans=m
    else:
        ans=M
    print(ans)

Submission status: Accepted

Solution code:
inf = 10 ** 9
n = int(input())
a = [int(input()) for x in range(n)]
m1 = -inf
m2 = -inf
for i in range(n):
    if a[i] >= m1:
        m2 = m1
        m1 = a[i]
    elif a[i] > m2:
        m2 = a[i]
for i in range(n):
    if m1 != a[i]:
        print(m1)
    else:
        print(m2)

Submission status: Accepted

Solution code:
import sys
fastinput = sys.stdin.readline
N=int(fastinput())
A=[int(fastinput()) for i in range(N)]
B = [a for a in A]
B.sort(reverse=True)
First = B[0]
Second = B[1]
Findex = A.index(First)
if First == Second:
    for i in range(N):print(First)
else:
    for a in A:
        if a==First:print(Second)
        else:print(First)

Submission status: Accepted

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

Submission status: Accepted

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

B.sort()

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

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  alist = [int(input()) for _ in range(n)]
  maxn, maxc, max2n = 0, 0, 0
  for a in alist:
    if maxn <= a:
      if maxn == a:
        maxc += 1
      else:
        max2n = maxn
        maxn = a
        maxc = 1
    elif max2n < a:
      max2n = a

  for a in alist:
    if a == maxn:
      if maxc > 1:
        print(maxn)
      else:
        print(max2n)
    else:
      print(maxn)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n = int(input())
a = []
m1 = -1
i1 = 0
m2 = -1
i2 = 1
for i in range(n):
  t = int(input())
  a.append(t)
  if t>m2:
    i2 = i
    m2 = t
  if m2>m1:
    i1, i2 = i2, i1
    m1, m2 = m2, m1
for i in range(n):
  if i!=i1:
    print(m1)
  else:
    print(m2)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a_max = max(a)
max_list = []
for i in a:
    if i == a_max:
        max_list.append(a.index(i))
if len(max_list) >= 2:
    for i in range(n):
        print(a_max)
else:
    for i in range(n):
        if i in max_list:
            a.remove(a_max)
            print(max(a))
        else:
            print(a_max)

Submission status: Accepted

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

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

A.sort(key=lambda A : A[0], reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

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

pre=[]
suf=[]
temp=-10000000000
for i in range(len(A)):
    temp=max(A[i],temp)
    pre+=[temp]

temp=-100000000000
for i in range(len(A)-1,-1,-1):
    temp=max(temp,A[i])
    suf+=[temp]
suf=suf[::-1]
ans=[]
for i in range(len(A)):
    if i==0:
        ans+=[suf[1]]
    elif i==len(A)-1:
        ans+=[pre[len(A)-2]]
    else:
        ans+=[max(pre[i-1],suf[i+1])]
for x in ans:
    print(x)

Submission status: Accepted

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

ma = max(A)
B = sorted(A)

ma = B[-1]
ma2 = B[-2]

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

Submission status: Accepted

Solution code:
from copy import copy

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




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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC134 C

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

n = int(input())
a = [int(input()) for _ 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:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

# n
# a, b = 
# s = input()


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


b = [0]*2
for e in A:
    if e > b[0]:
        b[1] = b[0]
        b[0] = e
    elif e > b[1]:
        b[1] = e
        
for e in A:
    if b[0] == e:
        print(b[1])
    else:
        print(b[0])

Submission status: Accepted

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

As = sorted(A)

M1 = As[-1]
M2 = As[-2]
for i in range(N):
    a = A[i]
    if a != M1:
        print(M1)
    else:
        print(M2)

Submission status: Accepted

Solution code:
N, = (int(x) for x in input().split())
A_arr = []
for _ in range(N):
    A_arr.append(int(input()))

A2_arr = sorted(A_arr, reverse=True)

for a in A_arr:
    if a == A2_arr[0]:
        print(A2_arr[1])
    else:
        print(A2_arr[0])

Submission status: Accepted

Solution code:
n = int(input())
max = 0
sub_max = 0
max_index = 0
for i in range(n):
    a = int(input())
    if(a > max):
        sub_max = max
        max = a
        max_index = i
    elif(a > sub_max):
        sub_max = a
for i in range(n):
    if(i == max_index):
        print(sub_max)
    else:
        print(max)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n = int(input())
    a = [int(input()) for i in range(n)]
    sa = sorted(a, reverse=True)

    for i in a:
        if i == sa[0]:
            print(sa[1])
        else:
            print(sa[0])


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
sa=sorted(a)
ma=max(a)
ma_2=0
n_ma=0
for i in range(n):
    target = sa[n-1-i]
    if target==ma:
        n_ma+=1
    else:
        ma_2=target
        break
for i in a:
    if i==ma and n_ma>=2:
        print(ma)
    elif i==ma and n_ma==1:
        print(ma_2)
    else:
        print(ma)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]
l1 = [0] * (n + 1)
l2 = [0] * (n + 1)
for i in range(n):
    l1[i + 1] = max(l1[i], a[i])
    l2[n - i - 1] = max(l2[n - i], a[n - i - 1])
for i in range(n):
    print(max(l1[i], l2[i + 1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import *
import sys
inp = raw_input

def err(s):
    sys.stderr.write('{}\n'.format(s))

def ni():
    return int(inp())

def nl():
    return [int(_) for _ in inp().split()]

N = ni()
A = [ni() for _ in range(N)]

A2 = list(A)
A2.sort()
for ai in A:
    if ai == A2[-1]:
        print(A2[-2])
    else:
        print(A2[-1])

Submission status: Accepted

Solution code:
N = int(input())
indexed_A = list(sorted([(int(input()), i) for i in range(N)]))
indexed_A = list(reversed(indexed_A))

for i in range(N):
    for a, j in indexed_A:
        if i != j:
            print(a)
            break

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    sa = sorted(a)

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


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
aa = []
for _ in range(n):
    a = int(input())
    aa.append(a)
a2, a1 = sorted(aa)[-2:]

for a in aa:
    if a == a1:
        print(a2)
    else:
        print(a1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


fin = sys.stdin.readline
N = int(fin())
A_list = [int(fin()) for _ in range(N)]
max_prefix = []
cur_max = float('-inf')
for A in A_list:
    cur_max = max(cur_max, A)
    max_prefix.append(cur_max)

cur_max = float('-inf')
max_suffix = [None] * N
for i in reversed(range(N)):
    A = A_list[i]
    cur_max = max(cur_max, A)
    max_suffix[i] = cur_max

print(max_suffix[1])
for i in range(1, N - 1):
    print(max(max_prefix[i - 1], max_suffix[i + 1]))
print(max_prefix[-2])

Submission status: Accepted

Solution code:
N = int(input())
a_list = [int(input()) for _ in range(N)]
a_list2 = sorted(a_list)
max1 = a_list2[-1]
max2 = a_list2[-2]

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
m1 = int(input())
m2 = -1
f = 0
s = -1
for i in range(n-1):
    q = int(input())
    if m1 <= q:
        m2 = m1
        m1 = q
        s = f
        f = i+1
    elif m2 <= q:
        m2 = q
        s = i

for i in range(n):
    if i == f:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

MAX_A = max(A)
MAX_A_CNT = A.count(MAX_A)

Second_MAX_A = B[1]
if MAX_A_CNT >= 2:
    for i in range(N):
        print(MAX_A)
else:
    for i in range(N):
        if A[i] == MAX_A:
            print(Second_MAX_A)
        else:
            print(MAX_A)

Submission status: Accepted

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

Submission status: Accepted

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

t = copy.deepcopy(A)
t.sort()
ma = t[-1]
maa = t[-2]

for i in A:
  if i == ma:
    print(maa)
  else:
    print(ma)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in [0]*N]
B = sorted(A)[::-1]
for a in A:
    if a!=B[0]:print(B[0])
    else:print(B[1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    B = sorted(A)
    M1 = B[-1]
    M2 = B[-2]
    for a in A:
        if a == M1:
            print(M2)
        else:
            print(M1)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

A_list_sorted = sorted(A_list)

M1 = A_list_sorted[-1]
M2 = A_list_sorted[-2]

for i in range(N):
    if A_list[i] == M1:
        print(M2)
    else:
        print(M1)

Submission status: Accepted

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

b = tuple(sorted(a, reverse=True))

for i, aa in enumerate(a):
    if b[0] == aa:
        print(b[1])
    else:
        print(b[0])

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
raw = []
arr = []

for i in range(n):
    a = int(input())
    raw.append(a)
    arr.append(a)

arr.sort()

# print(arr)

key = arr[-1]
sec = arr[-2]

for i in range(n):
    if raw[i] == key:
        print(sec)
    else:
        print(key)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

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

A_c = copy.deepcopy(A)
A_c.sort()
for i in range(N):
    if A_c[-1] == A[i]:
        print(A_c[-2])
    else:
        print(A_c[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    print(3*n**2)
    return

#B
def B():
    n,d = LI()
    f = [1]*(n+2*d+2)
    ans = 0
    for i in range(n):
        if f[i]:
            for j in range(i,i+2*d+1):
                f[j] = 0
            ans += 1
    print(ans)
    return

#C
def C():
    n = I()
    a = IR(n)
    mi = max(a)
    if a.count(mi) > 1:
        for i in range(n):
            print(mi)
    else:
        b = [a[i] for i in range(n)]
        b.sort()
        b = b[::-1]
        for i in range(n):
            if mi != a[i]:
                print(mi)
            else:
                print(b[1])

    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.readline
inf = 10 ** 18
P = 10 ** 9 + 7

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

A_sorted = sorted(A)

for Ai in A:
    if Ai == A_sorted[-1]:
        print(A_sorted[-2])
    else:
        print(A_sorted[-1])