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: Time Limit Exceeded

Solution code:
import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for _ in range(N)]

a = np.array(list(itertools.accumulate(A, max)))
b = list(itertools.accumulate(A[::-1], max))[::-1]

for i in range(len(A)):
    r = 0
    if len(a[:i]) > 0:
        r = max(r, *a[:i])
    if len(b[i + 1:]) > 0:
        r = max(r, *b[i + 1:])
    print(r)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = [int(input()) for i in range(n)]
ans = 0
for i, l in enumerate(a):
    tmp = copy.deepcopy(a)
    tmp.pop(i)
    print(max(tmp))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
for i in range(n):
  k=a[:i]+a[i+1:]
  print(max(k))

Submission status: Time Limit Exceeded

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

for i in range(len(num)):
    a = num[:i]
    b = num[i+1:]
    c = a + b
    print(max(c))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
lst=[]
for i in range(n):
  a=int(input())
  lst.append(a)
for i in range(n):
  print(max(lst[:i]+lst[i+1:]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

import math


class SegmentTree(object):
    # セグメント木

    def __init__(self, N):
        """サイズNの配列を元にしたセグメント木
        """
        # 単位元
        self.idelem = -INF
        # 比較関数
        self.func = max

        self.N0 = 2**(math.ceil(math.log2(N)))
        self.tree = [self.idelem]*(2*self.N0)

    def update(self, k, x):
        """k番目の値をxに更新
        """
        k += self.N0-1
        self.tree[k] = x
        while k > 1:
            k = (k-1)//2
            self.tree[k] = self.func(self.tree[2*k+1], self.tree[2*k+2])

    def query(self, l, r):
        """区間[l, r)の最大
        """
        l += self.N0-1
        r += self.N0-1
        s = self.idelem
        while l < r:
            if r % 2 == 0:
                r -= 1
                s = self.func(s, self.tree[r])

            if l % 2 == 0:
                s = self.func(s, self.tree[l])
                l += 1
            l //= 2
            r //= 2
        return s

    def __str__(self):
        return str(self.tree[self.N0-1:-1])


def solve(N: int, A: "List[int]"):
    st = SegmentTree(N)
    for i, a in enumerate(A):
        st.update(i, a)
    print(st.query(1, N))
    for i in range(1, N-1):
        print(max(st.query(0, i), st.query(i+1, N)))
    print(st.query(0, N-1))

    return


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: Time Limit Exceeded

Solution code:
import copy
N=int(input())
A=list(int(input()) for _ in range(N))
a=copy.deepcopy(A)
for i in range(N):
    a = copy.deepcopy(A)
    a.pop(i)
    print(max(a))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
# import bisect
# import math
import numpy as np
# from fractions import gcd
import itertools

"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        # 参考
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        # https://atcoder.jp/contests/abc105/submissions/4088632
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)

    def lcm(self, x, y):
        return (x * y) // self.gcd(x, y)


"""ここからメインコード"""


def main():
    # 1文字に省略
    r, e = range, enumerate
    ip = IP()
    id = Idea()
    """この下から書いてね"""
    n = ip.I()
    a = ip.ILS(n)

    for i in range(n):
        b = a.copy()
        b.pop(i)
        print(max(b))


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
b = []
#mx = -10000
for i in range(n):
    num = int(input())
    a.append(num)
for i in range(n):
    print(max(a[:i]+a[i+1:]))

Submission status: Time Limit Exceeded

Solution code:
n = input()
a = [input() for _ in xrange(n)]
m = max(a)
cnt = 0
m2 = 0
for x in a:
    if x == m:
        cnt += 1
    else:
        m2 = max(m2, x)

for x in a:
    if cnt >= 2 or x != m:
        print m
    else:
        print m2

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
for i in range(n):
    max1 = 0
    max2 = 0
    if a[:i] != []:
        max1 = max(a[:i])
    if a[i+1:] != []:
        max2 = max(a[i+1:])
    print(max(max1, max2))

Submission status: Time Limit Exceeded

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


for i in range(n):
  p=A[i]
  A[i]=0
  print(max(A))
  A[i]=p

Submission status: Time Limit Exceeded

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(L[:i] + R[i+1:]))

Submission status: Time Limit Exceeded

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

for i in range(n):
    print(max(a[:i] + a[i+1:]))

Submission status: Time Limit Exceeded

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

for i in range(N) :
    sub = A[:i] + A[i+1:]
    print(max(sub))

Submission status: Accepted

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

Submission status: Accepted

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

max_ = max(a)
max2 = sorted(a)[-2]
for i in range(n):
    if a[i] != max_:
        print(max_)
    else:
        print(max2)

Submission status: Accepted

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

Asorted=sorted(A)
Amax1=Asorted[-1]
Amax2=Asorted[-2]

for a in A:
	if a!=Amax1:
		print(Amax1)
	else:
		print(Amax2)

Submission status: Accepted

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

Submission status: Accepted

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

sortedA = list(reversed(sorted(A)))
for a in A:
  count = 0
  for b in sortedA:
    if b != a:
      print(b)
      break
    if a == b:
      if count == 0:
        count += 1
      else:
        print(b)
        break

Submission status: Accepted

Solution code:
#import math
#import itertools
#import numpy as np
#from collections import deque
# sys.setrecursionlimit(10 ** 6)
#mod = 10 ** 9 + 7
#INF = 10 ** 9
#PI = 3.14159265358979323846

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    A=[]
    for i in range(n):
        A.append(INT())
    
    B=sorted(A,reverse=True)
    m1=A.count(B[0])
    m2=A.count(B[1])
    if m1>=2:
        flg=True
    else:
        index=A.index(B[0])
        flg=False

    #print(A,B,index)

    if flg:
        for i in range(n):
            print(B[0])
    else:
        for i in range(n):
            if i!=index:
                print(B[0])
            else:
                print(B[1])


if __name__=='__main__':
    do()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
m = max(A)
mindex = A.index(m)
for i in range(N):
    if(i != mindex):
        print(m)
    else:
        pop = A.pop(i)
        print(max(A))
        A.insert(i,pop)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
Alist=[]
maxi=0
n2=0
for _ in range(N):
    A=int(input())
    Alist.append(A)
    if A>maxi:
        n2=maxi
        maxi=A
    elif A>n2:
        n2=A
count=Counter(Alist)
for A in Alist:
    if A!=maxi:
        print(maxi)
    elif A==maxi and count[A]>1:
        print(maxi)
    else:
        print(n2)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(input()) for _ in range(n)]
ar = sorted(arr)
mx = ar[-1]
sc = ar[-2]

for ele in arr:
    if ele == mx:
        print(sc)
    else:
        print(mx)

Submission status: Accepted

Solution code:
arr = []
for _ in range(int(input())):
  arr.append(int(input()))
arr2 = arr.copy()
arr2.sort()
z = arr2[-1]
zz = arr2[-2]
for k in arr:
  if k != z:
    print(z)
  else:
    print(zz)

Submission status: Accepted

Solution code:
#ABC134 C (再考察)

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

Submission status: Accepted

Solution code:
N = int(input())
b = []
max_num = 0
for i in range(N):
  a = int(input())
  b.append(a)
  max_num = max(a,max_num)
max_num2 = 0  
num = b.count(max_num)
if num >1:
  for i in range(N):
    print(max_num)
elif num ==1:
  for i in b:
    if i == max_num:
      pass
    else:
      max_num2 = max(max_num2,i)
  for i in b:
    if max_num == i:
      print(max_num2)
    else:
      print(max_num)

Submission status: Accepted

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

l = [0] * (n+2)
l[0] = 0
for i in range(1,n+1):
  l[i] = max(l[i-1], a[i])

r = [0] * (n+2)
r[n] = 0
for i in range(n,0,-1):
  r[i] = max(r[i+1], a[i])

for i in range(1,n+1):
  print(max(l[i-1],r[i+1]))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
sort_A = sorted(A)
max_val = max(A)
second_val = sort_A[-2]

for i in range(N):
    if A[i] == max_val:
        print(second_val)
    else:
        print(max_val)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
A = [int(input()) for _ in range(N)]
sort_A = sorted(A)
max_val = max(A)
second_val = sort_A[bisect.bisect_right(sort_A, max_val) - 2]

for i in range(N):
    if A[i] == max_val:
        print(second_val)
    else:
        print(max_val)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = sorted((int(input()), i) for i in range(n))
for i in range(n):
    print(a[-1][0] if i != a[-1][1] else a[-2][0])

Submission status: Accepted

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
ma=max(A)
flag=False
if A.count(ma)>=2:
  flag=True
for i in range(n):
  if A[i] != ma:
    print(ma)
  else:
    if flag:
      print(ma)
    else:
      A[i]=0
      print(max(A))

Submission status: Accepted

Solution code:
# ABC 134 C
N = int(input())
A = []
for i in range(N):
    a = int(input())
    A.append(a)
B = sorted(A)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import heapq

n = int(input())
A = [int(input()) for _ in range(n)]
m1, m2 = heapq.nlargest(2, A)
print(*[m2 if a == m1 else m1 for a in A], sep='\n')

Submission status: Accepted

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

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

Submission status: Accepted

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

M = max(A)
i = A.index(M)
M2 = max(A[:i] + A[i+1:])

for a in A:
    print(M if a != M else M2)

Submission status: Accepted

Solution code:
import copy

N = int(input())

ary = []
for _ in range(N):
    ary.append(int(input()))

ary_copy = copy.copy(ary)
ary_copy.sort()

max_1 = ary_copy[-1]
max_2 = ary_copy[-2]

for i in ary:
    if i == max_1:
        print(max_2)
    elif i != max_1:
        print(max_1)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    max = 0
    next = 0
    for i in A:
        if i >= max:
            next = max
            max = i
        elif i >= next:
            next = i

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


# Generated by 1.1.7.1 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 copy
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
B = copy.copy(A)
B.sort()
A_max = B[-1]
A_max2 = B[-2]

for i in range(n):
    if A[i] < A_max:
        print(A_max)
    elif A[i] == A_max:
        print(A_max2)

Submission status: Accepted

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

    a_sort = sorted(a)
    a_max = a_sort[-1]
    a_second = a_sort[-2]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    a_sort = sorted(a)
    a_max = a_sort[-1]
    a_second = a_sort[-2]

    for i in range(n):
        if a[i] == a_sort[-1]:
            print(a_second)
        else:
            print(a_max)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    a_sort = sorted(a)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[:]
p=max(a)
b.remove(p)
q=max(b)

for i in a:
    if i!=p:
        print(p)
    else:
        print(q)

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = []
for i in range(n):
  s = int(input())
  a.append(s)
b = copy.copy(a)
a.sort(reverse=True)
c = b.index(a[0])
for j in b:
  if j == b[c]:
    print(a[1])
  else:
    print(a[0])

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=sorted(a,reverse=True)
first,second=b[0],b[1]

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

Submission status: Accepted

Solution code:
n=int(input())

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

a_max=max(a)
a_max_cnt=a.count(a_max)

for i in range(n):
    if (a[i]!=a_max or a_max_cnt>1):
        print(a_max)
    else:
        print(max(a[:i]+a[i+1:]))

Submission status: Accepted

Solution code:
n = int(input())
max_a = 0
next_a = 0
max_loc = 0
for i in range(n):
    ai = int(input())
    if ai > max_a:
        max_a =ai
        max_loc = i
    elif ai > next_a:
        next_a = ai
if next_a == 0:
    for i in range(n):
        print(max_a)
else:
    for i in range(max_loc):
        print(max_a)
    print(next_a)
    for i in range(n-max_loc-1):
        print(max_a)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    A = int(input())
    a.append(A)
a_1 = list(set(a))[-1]
if a.count(a_1) >= 2:
    for i in range(n):
        print(a_1)
else:
    a_2 = list(set(a))[-2]
    for i in range(n):
        if a[i] == a_1:
            print(a_2)
        else:
            print(a_1)

Submission status: Accepted

Solution code:
n = int(input())
alst = [int(input()) for _ in range(n)]
max_2 = [0, 0]
for a in alst:
  if a > max_2[0]:
    max_2[1] = max_2[0]
    max_2[0] = a
  elif a > max_2[1]:
    max_2[1] = a

for a in alst:
  if a == max_2[0]:
    print(max_2[1])
  else:
    print(max_2[0])

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [next(map(int, input().split())) for _ in range(N)]

AA = sorted(A, reverse=True)

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

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = [int(input()) for i in range(n)]
m = sorted(list(set(a)))
c =collections.Counter(a)

t = [max(a)]*n

if c[max(a)] == 1:
    t[a.index(max(a))] = m[-2]

[print(i) for i in t]

Submission status: Accepted

Solution code:
from copy import deepcopy
n = int(input())
A = [int(input()) for i in range(n)]
A2 = deepcopy(A)
A2.sort()
for i in range(n):
    #print (A[i], A2[-1])
    if A[i] == A2[-1]:
        print (A2[-2])
    else:
        print (A2[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc134/tasks/abc134_c"

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    sort_a = sorted(a, reverse=True)
    for v in a:
        if sort_a[0] == v:
            print(sort_a[1])
        else:
            print(sort_a[0])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

mx = max(A)
mxidx = A.index(mx)

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

Submission status: Accepted

Solution code:
# coding: utf-8

def main():
    N = int(input())
    fir, fir_i, sec, sec_i = 0, 0, 0, 0

    for i in range(N):
        tmp = int(input())
        if tmp > fir:
            sec = fir
            sec_i = fir_i
            fir = tmp
            fir_i = i
        elif tmp > sec:
            sec = tmp
            sec_i = i

    for i in range(N):
        if i != fir_i:
            print(fir)
        else:
            print(sec)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
N = int(input())
A = [int(input()) for _ in range(N)]
a1, a2 = sorted(A)[::-1][:2]
for a in A:
    if a != a1:
        print(a1)
    else:
        print(a2)

Submission status: Accepted

Solution code:
N = int(input())
l = [int(input()) for _ in range(N)]
largest = max(l)
largest_index = l.index(max(l))
second_largest = sorted(l)[-2]
second_largest_index = l.index(second_largest)

for idx, n in enumerate(l):
  if idx == largest_index:
    print(second_largest)
  else:
    print(largest)

Submission status: Accepted

Solution code:
def abc134c():
    n = int(input())
    a = []
    for _ in range(n):
        a.append(int(input()))
    a_sorted = sorted(a)
    for item in a:
        if item == a_sorted[-1]:
            print(a_sorted[-2])
        else:
            print(a_sorted[-1])
abc134c()

Submission status: Accepted

Solution code:
a = input()
N = int(a)
A = [int(input()) for i in range(N)]
ind = A.index(max(A))
max = max(A)

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

Submission status: Accepted

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

lmax=[0]*N
rmax=[0]*N

for i in range(1,N):
  lmax[i]=max(lmax[i-1],A[i-1])
for i in range(N-2,-1,-1):
  rmax[i]=max(rmax[i+1],A[i+1])

for i in range(N):
  print(max(lmax[i],rmax[i]))

Submission status: Accepted

Solution code:
n = int(input())
L = list(int(input()) for _ in range(n))
SL = sorted(L)
first_max = SL[-1]
second_max = SL[-2]
for l in L:
  if (l == first_max):
    print(second_max)
  else:
    print(first_max)

Submission status: Accepted

Solution code:
n = int(input())
L = list(int(input()) for _ in range(n))
SL = sorted(L)
first_max = SL[-1]
second_max = SL[-2]
for l in L:
  if (l != first_max):
    print(first_max)
  else:
    print(second_max)

Submission status: Accepted

Solution code:
n = int(input())
L = list(int(input()) for _ in range(n))
SL = sorted(L)
first_max = SL[-1]
second_max = SL[-2]
for l in L:
  if (l != first_max):
    print(first_max)
  elif (l == first_max):
    print(second_max)

Submission status: Accepted

Solution code:
N = int(input())
A = []
import sys
input = sys.stdin.readline
for i in range(N):
  A.append(int(input().rstrip()))
from collections import Counter
counter = Counter(A)
max_A = max(A)
#B = sorted(set(A), key = A.index)
#B = sorted(B, reverse = True)
#max_A = B[0]
try:
  second_max = sorted(set(A))[-2]
except:
  second_max = max_A
j = False
if counter[max_A] >= 2:
  j = True
for i in range(N):
  if A[i] == max_A and j == False:
    print(second_max)
  else:
    print(max_A)

Submission status: Accepted

Solution code:
num = int(input())

table = []
for i in range(num):
    table.append(int(input()))

table2 = sorted(table, reverse=True)

for j in range(num):
    if table[j] != table2[0]:
        print(table2[0])
    else:
        print(table2[1])

Submission status: Accepted

Solution code:
N,*D=map(int,open(0).read().split())
S=sorted(D,reverse=True)
if S[1]==S[0]:
  [print(S[0]) for i in range(N)]
else:
  import numpy as np
  max_i=np.arange(N)[np.array(D)==S[0]]
  for i in range(N):
    if i==max_i:
      print(S[1])
    else:
      print(S[0])

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
M1=max(A[0],A[1])
M2=min(A[0],A[1])
for i in range(2,N):
    if A[i]>=M1:
        M2=M1
        M1=A[i]
    else:
        M2=max(M2,A[i])
for i in range(N):
    if A[i]==M1:
        print(M2)
    else:
        print(M1)

Submission status: Accepted

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

for i in range(n):
    x = int(input())
    lst.append(x)

a = max(lst)
b = []
for i in range(n):
    if (lst[i] == a):
        b.append(i)

if (len(b) > 1):
    for i in range(n):
        print(a)

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

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import heapq

def main():
    numbers=[]
    n = int(input())
    [numbers.append(int(input().rstrip())) for _ in range(n)]

    max_n=0
    second_n=0

    for i in numbers:
        if i > max_n:
            max_n=i
        elif i > second_n:
            second_n=i
    
    for i in numbers:
        if i==max_n:
            print(second_n)
        else:
            print(max_n)
    

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())

a=[]
a_sort=[]
for i in range(n):
    tmp=int(input())
    a.append(tmp)
    a_sort.append(tmp)
a_sort.sort(reverse=True)

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

Submission status: Accepted

Solution code:
n=int(input())

import heapq
a=[]
a_heap=[]
for i in range(n):
    tmp=int(input())
    a.append(tmp)
    heapq.heappush(a_heap,(-1)*tmp)

for i in range(n):
    max_1=heapq.heappop(a_heap)
    if max_1==(-1)*a[i]:
        max_2=heapq.heappop(a_heap)
        print((-1)*max_2)
        heapq.heappush(a_heap,max_2)
    else:print((-1)*max_1)
    heapq.heappush(a_heap,max_1)

Submission status: Accepted

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

m,s = 0,0
for a in A:
    if a > m:
        m = a
    elif a > s:
        s = a

for a in A:
    if a == m:
        print(s)
    else:
        print(m)

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = Counter(a)
    maximum = max(a)
    if cnt[maximum] > 1:
        print("\n".join([str(maximum)] * n))
    else:
        second = sorted(a, reverse=True)[1]
        for i in range(n):
            if a[i] != maximum:
                print(maximum)
            else:
                print(second)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import copy
n = int(input())
an = [int(input()) for i in range(n) ]
bn = copy.copy(an)
top = max(an)
an.remove(top)
top2 = max(an)


for x in bn:
  if x == top:
    print(top2)
  else:
    print(top)

Submission status: Accepted

Solution code:
N = int(input())
L = []
M = []
for i in range(N):
  a = int(input())
  L.append(a)
  M.append(a)
L.sort()
for j in range(N):
  if M[j] == L[len(L) - 1]:
    print(L[len(L) - 2])
  else:
    print(L[len(L) - 1])

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
 
M = max(A)
M_index = A.index(M)
sub_M = 0
for i in range(N):
  if i != M_index: sub_M = max(sub_M, A[i])

for i in range(N):
  if i == M_index: print(sub_M)
  else: print(M)

Submission status: Accepted

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

M = max(A)
M_index = A.index(M)
sub_M = 0
for i in range(N):
  if i != M_index: sub_M = max(sub_M, A[i])

for i in range(N):
  if i == M_index: print(sub_M)
  else: print(M)