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 sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,*A = map(int, read().split())

    A_ = sorted(A)
    m = max(A)

    for a in A:
        if a == m:
            print(A_[-2])
        else:
            print(m)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import deepcopy


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    b = deepcopy(a)
    b.sort()
    for aa in a:
        if aa == b[-1]:
            print(b[-2])
        else:
            print(b[-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

As = sorted(A, reverse=True)
M = As[0]
Ms = As[1]

for i in range(N):
  if M == A[i]:
    print(Ms)
  else:
    print(M)

Submission status: Accepted

Solution code:
n = int(input())
aaa = [int(input()) for i in range(n)]
max_l = [0] * (n - 1)
max_r = [0] * (n - 1)
max_l[0] = aaa[0]
for i in range(1, n - 1):
    max_l[i] = max(aaa[i], max_l[i - 1])
max_l = [1] + max_l

aaa.reverse()
max_r[0] = aaa[0]
for i in range(1, n - 1):
    max_r[i] = max(aaa[i], max_r[i - 1])
max_r = [1] + max_r
max_r.reverse()

for i in range(n):
    print(max(max_l[i], max_r[i]))

Submission status: Accepted

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

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

x = sorted(a)
ans1 = x[-1]
ans2 = x[-2]

for i in range(n):
    if a[i] == ans1:
        print(ans2)
    else:
        print(ans1)

Submission status: Accepted

Solution code:
n = int(input())
ll = [int(input()) for i in range(n)]
llc = ll.copy()
llc.sort()
m1 = llc[n-1]
m2 = llc[n-2]

for l in ll:
    if l == m1:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = ILs(N)
B = A[:]
B.sort()

MAX1 = B[-1]
MAX2 = B[-2]

for a in A:
  if a == MAX1:
    P(MAX2)
  else:
    P(MAX1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = ILs(N)
import copy
B = copy.copy(A)
B.sort()

MAX1 = B[-1]
MAX2 = B[-2]

for a in A:
  if a == MAX1:
    P(MAX2)
  else:
    P(MAX1)

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
A = max(a)
b = sorted(a,reverse=True)
import collections
c = collections.Counter(a)
if c[A] >= 2:
    for i in range(n):
        print(A)
else:
    for i in range(n):
        if a[i] == A:
            print(b[1])
        else:
            print(A)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# For taking integer inputs.
def inp():
    return(int(input()))


N = inp()
A = []
i1, i2 = 0, 0

for i in range(N):
    A.append(inp())

srt = sorted(A)
m1 = srt[-1]
m2 = srt[-2]

for i in range(N):
    if A[i] == m1:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

max1 = [A.index(max(A)), max(A)]
p = A.pop(A.index(max(A)))
max2 = max(A)
for k in range(N):
    if k == max1[0]:
        print(max2)
    else:
        print(max1[1])

Submission status: Accepted

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

max1 = [A.index(max(A)), max(A)]
p = A.pop(A.index(max(A)))
max2 = max(A)
ans = [max2 if j == max1[0] else max1[1] for j in range(N)]
for k in range(N):
    print(ans[k])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Blist = sorted(Alist, reverse=True)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
MAX = max(A)
AA = A[:]
AA.pop(A.index(MAX))
SECOND = max(AA)
for a in A:
    if a == MAX and a == SECOND:
        print(MAX)
    elif a == MAX and a != SECOND:
        print(SECOND)
    else:
        print(MAX)

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

B = sorted(A)

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

Submission status: Accepted

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

Submission status: Accepted

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

max1 = 0
max2 = 0

for a in A:
    if a > max1:
        max2 = max1
        max1 = a
    elif a > max2:
        max2 = a

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
left_max = [0] * (N + 1)
right_max = [0] * (N + 1)

for index, a in enumerate(A, start=1):
    left_max[index] = max(left_max[index - 1], a)

for index, a in enumerate(reversed(A), start=1):
    right_max[index] = max(right_max[index - 1], a)

for i in range(N):
    print(max(left_max[i], right_max[N - i - 1]))

Submission status: Accepted

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

for a in A:
    left_max.append(max(left_max[-1], a))

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

for i in range(N):
    print(max(left_max[i], right_max[N - i - 1]))

Submission status: Accepted

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

for a in A:
    if a == sorted_a[0]:
        print(sorted_a[1])
    else:
        print(sorted_a[0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
dic={}
mx=0
al=[]
for i in range(n):
    a=int(input())
    al.append(a)

order=sorted(al,reverse=True)
mx1=order[0]
mx2=order[1]

for a in al:
    if a==mx1:
        print(mx2)
    else:
        print(mx1)

Submission status: Accepted

Solution code:
n = int(input())
num = list(int(input()) for i in range(n))
test = sorted(num, reverse=True)
i = 0

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

Submission status: Accepted

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

max_A = 0
max_index = 0
next_A = 0

for i in range(N):
    if A_list[i] > max_A:
        max_A = A_list[i]
        max_index = i
    elif max_A >= A_list[i] > next_A:
        next_A = A_list[i]

for i in range(N):
    if i == max_index:
        print(next_A)
    else:
        print(max_A)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
l = [0] * n
r = [0] * n
for i in range(1, n):
    l[i] = max(l[i-1], a[i-1])
for i in range(n-2, -1, -1):
    r[i] = max(r[i+1], a[i+1])
for i in range(n):
    print(max(l[i], r[i]))

Submission status: Accepted

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

n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(copy(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:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
B = sorted(A)
for n in A:
    if B[-1] != n:
        print(B[-1])
    else:
        print(B[-2])

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline
N = int(input())
A = [int(input()) for i in range(N)]
A_sort =sorted(A)
ans =[]*N
max_1 =A_sort[-1]
max_2 =A_sort[-2]

for i in A:
    if i !=max_1:
        ans.append(max_1)
    else:
        ans.append(max_2)
[print(i) for i in ans]

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    maxA = max(A)
    countMax = A.count(maxA)
    if countMax > 1:
        [print(maxA) for _ in range(N)]
    else:
        next = -1
        maxIdx = A.index(maxA)
        if maxIdx == 0:
            next = max(A[1:])
        elif maxIdx == N - 1:
            next = max(A[:N - 1])
        else:
            next = max(A[0:maxIdx])
            next = max(next, max(A[maxIdx + 1:N]))
        for i in range(N):
            if not i == maxIdx:
                print(maxA)
            else:
                print(next)


resolve()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

# check
for a in A:
    if a == sort_A[0]:
        print(sort_A[1])
    else:
        print(sort_A[0])

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
#print(a)
lm = [0] * n
rm = [0] * n

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

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


for i in range(n):
    print(max(lm[i], rm[i]))

Submission status: Accepted

Solution code:
n = int(input())
li = []
for i in range(n):
    li.append(int(input()))
k = max(li)
l = li.index(k)
for i in range(n):
    if i == l:
        li[i] = 0
        print(max(li))
    else:
        print(k)

Submission status: Accepted

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

lm = [0 for i in range(n)]
rm = [0 for i in range(n)]
for i in range(1, n):
  lm[i] = max(lm[i-1], a[i-1])

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

for i in range(n):
  print(max(lm[i], rm[i]))

Submission status: Accepted

Solution code:
N = int(input())
nums = []
first = (-1,-float('inf'))
second = (-1,-float('inf'))
for i in range(N):
  num = int(input())
  nums.append(num)
  if num >= first[1]:
    second = first
    first = (i, num)
  elif num >= second[1]:
    second = (i, num)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
s = [] 
for _ in range(n):
    s += [int(input())]
s_sort = sorted(s)
for i in s:
    if i == s_sort[-1]:
        print(s_sort[-2])
    else:
        print(s_sort[-1])

Submission status: Accepted

Solution code:
n = int(input())
m1,m2 = 0,0
a = []
for i in range(n):
    a.append(int(input()))
    if a[i]>m1:
        m2=m1
        m1=a[i]
    elif a[i]>m2: 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())
A=[int(input()) for _ in range(N)]
ll=sorted(A)
a=max(A)
b=ll[-2]
  
for n in A:
  if n==a:
    print(b)
  else:
    print(a)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
A = np.array(list(int(input()) for i in range(n)))
A_sort = np.sort(A)
A_1 = A_sort[n-1]
A_2 = A_sort[n-2]


for i in range(n):
    if A[i] != A_1:
        print(A_1)
    else:
        print(A_2)

Submission status: Accepted

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


for j in range(N):
    if j == i:
        print(B[-2])
    else:
        print(a)

Submission status: Accepted

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

B = sorted(A)


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

Submission status: Accepted

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

import copy
B = copy.copy(A)
B.remove(a)

for j in range(N):
    if j == i:
        print(max(B))
    else:
        print(a)

Submission status: Accepted

Solution code:
import copy
def exception_handling(n, ali):

    sortali = copy.copy(ali)
    sortali.sort(reverse=True)

    alimax = sortali[0]
    alimax2 = sortali[1]

    ans = []

    for i in ali:
        if i != alimax:
            ans.append(alimax)
        else:
            ans.append(alimax2)

    return ans

def main():
    n = int(input())
    ali = [int(input())for i in range(n)]
    ans = exception_handling(n, ali)
    for i in ans:
        print(i)

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, reverse=True)
M1 = b[0]
M2 = b[1]
for i in range(n):
    if a[i] != M1:
        print(M1)
    else:
        print(M2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]
M = max(A)
B.remove(M)
S = max(B)
big = A.index(M)
for i in range(N):
  if i == big:
    print(S)
  else:
    print(M)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
for ii in range(n):
  a[ii]=int(input())
import copy
a0=copy.copy(a)
a0.sort(reverse=True)
for ii in range(n):
  if a[ii]==a0[0]:
    print(a0[1])
  else:
    print(a0[0])

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

B = sorted(A, reverse=True)
max = B[0]
next_max = B[1]

for i in range(N):
    if A[i] == max:
        print(next_max)
    else:
        print(max)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
m1 = 0
m2 = 0
mp = 0
for i in range(n):
    if (m1 < a[i]):
        m2 = m1
        m1 = a[i]
        mp = i
    elif (m2 < a[i]):
        m2 = a[i]
#print(m1,m2,mp)
for i in range(n):
    if (mp == i):
        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)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=[int(input()) for _ in range(n)]
sort_a=sorted(a, reverse=True)
max=sort_a[0]
semimax=sort_a[1]
for i in range(n):
  if a[i] != max:
    print(max)
  else:
    print(semimax)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N): A.append(int(input()))
S=sorted(A,key=int)
max1=S[-1]
max2=S[-2]
for i in A:
    if i==max1: print(max2)
    else: print(max1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
lst=[int(input()) for i in range(n)]
a=max(lst)
aind=lst.index(a)
if aind==0:
  b=max(lst[1:])
elif aind==n-1:
  b=max(lst[:n-2])
else:
  tempa=max(lst[:aind])
  tempb=max(lst[aind+1:])
  b=max(tempa, tempb)
bind=lst.index(b)
for j in range(n):
  if j==aind:
    print(b)
  else:
    print(a)

Submission status: Accepted

Solution code:
n = int(input())
ai = [0]*n
for i in range(n):
  ai[i] = int(input())
bi = ai[:]
bi.sort()
num1 = bi[-1]
num2 = bi[-2]
for i in ai:
  if i == num1:
    print(num2)
  else:
    print(num1)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list()
for _ in range(n):
    a_list.append(int(input()))

sorted_a_list = sorted(a_list)

max_v = max(sorted_a_list)

is_max_v_multi = sorted_a_list[-1] == sorted_a_list[-2]

next_max_v = 0
for i in range(len(sorted_a_list) - 1, -1, -1):
    if sorted_a_list[i] != max_v:
        next_max_v = sorted_a_list[i]
        break

for a in a_list:
    if is_max_v_multi:
        print(max_v)
    else:
        if a == max_v:
            print(next_max_v)
        else:
            print(max_v)

Submission status: Accepted

Solution code:
n = int(input())
p = []
for i in range(n):
    a = int(input())
    p.append(a)
q = sorted(p, reverse=True)
max1 = q[0]
max2 = q[1]
max1_i = p.index(max1)
max2_i = p.index(max2)
for i in range(n):
    if i == max1_i:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
import sys


def main():
  n, *lst = map(int, sys.stdin.read().split())
  max1 = max(lst)
  idx = lst.index(max1)
  max2 = max(lst[:idx] + lst[idx+1:])
  print('\n'.join(str(max1) if j != max1 else str(max2) for j in lst))


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys


def main():
  n, *lst = map(int, sys.stdin.read().split())
  max1 = max(lst)
  idx = lst.index(max1)
  max2 = max(lst[:idx] + lst[idx+1:])
  if max2 < max1:
    print(*[max1 if j != max1 else max2 for j in lst], sep='\n')
  else:
    print(*[max1]*n, sep='\n')

    
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys


def main():
  n, *lst = map(int, sys.stdin.read().split())
  copy = sorted(lst)
  max1, max2 = copy[-1], copy[-2]
  if max2 < max1:
    print(*[max1 if j != max1 else max2 for j in lst], sep='\n')
  else:
    print(*[max1]*n, sep='\n')

    
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
n, *lst = map(int, sys.stdin.read().split())
copy = sorted(lst)
max1, max2 = copy[-1], copy[-2]

if max2 < max1:
  for j in lst:
    if j != max1:
      print(max1)
    else:
      print(max2)
else:
  for j in lst:
    print(max1)

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
import copy
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

maximum = max(a)

count_m = a.count(maximum)

co = copy.deepcopy(a)
for i in range(n):
    if co[i] == maximum:
        co[i] = 0
maximum2 = max(co)

for i in range(n):
    if count_m == 1 and a[i] == maximum:
        print(maximum2)
    else: print(maximum)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A_ = sorted(A, reverse=True)
ma = A_[0]
mi = A_[1]
for j in A:
    if j == ma:
        print(mi)
    else:
        print(ma)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import copy
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0

a = []
n = iin()
for i in range(n):
    a.append(iin())
    if neko < a[i]:
        neko = a[i]
b = copy.copy(a)
a.remove(neko)
nya = max(a)
for i in range(n):
    if b[i] == neko:
        print(nya)
    else:
        print(neko)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
saidai=sorted(a)[-1]
nibanme=sorted(a)[-2]

for i in range(n):
  if a[i]==saidai:
    print(nibanme)
  else:
    print(saidai)

Submission status: Accepted

Solution code:
n=int(input())
an=[]
an=[int(input()) for i in range(n)]
s_an=sorted(an,reverse=True)
m_an=max(an)
m_an2=s_an[1]
for i in an:
    if m_an==i:
        print(m_an2)
    else:
        print(m_an)

Submission status: Accepted

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

ma = max(A)
ma2 = sorted(A)[-2]
for a in A:
    if a==ma:
        ans = ma2
    else:
        ans = ma
    print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = ''
    N = int(input())
    A = []
    for _ in range(N):
        A.append(int(input()))
    M, M2 = sorted(A)[-1], sorted(A)[-2]
    for i in range(N):
        if A[i] == M:
          ans += str(M2) + '\n'
        else:
          ans += str(M) + '\n'
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict

geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(input())

N = gete(int)
a1, a2, i1 = 0, -1, -1

for i in range(N):
    ai = gete(int)
    if ai >= a1:
        a1, a2 = ai, a1
        i1 = i
    elif ai >= a2:
        a2 = ai

if a1 == a2:
    print(*[a1] * N, sep="\n")
else:
    print(*[a2 if i == i1 else a1 for i in range(N)], sep="\n")

Submission status: Accepted

Solution code:
from math import factorial as fact
import math
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
from collections import deque


#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(range(n),r))



def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets

#公約数の列挙
def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	#x:縦，y:横
	return fact(x+y)/fact(x)/fact(y)

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
	
def count_str(m):
	tmp={}
	for i in range(10):
		if i in tmp:
			tmp[i]=1
		else:
			tmp[i]+=1
	return tmp

def is_prime(n):
	if n==1: return True

	for i in range(2,int(math.sqrt(n))+1):
		if n%i==0:
			return False
	return True


n=int(input())
A=[]
for _ in range(n):
	A.append(input1())
B=sorted(A)
max_1=B[-1]
max_2=B[-2]

for i in A:
	if i==max_1:
		print(max_2)
	else:
		print(max_1)