Problem:


Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5



Sample input:
3
1
4
3


Sample output:
4
3
4

Submission status: Accepted

Solution code:

import math




def main():
    n = int(input())
    lista = []
    for i in range(0, n):
        lista.append(int(input()))
    maxim1 = -999999
    maxim2 = -9999999
    for element in lista:
        if element > maxim1:
            maxim2 = maxim1
            maxim1 = element
        elif element <= maxim1 and element > maxim2:
            maxim2 = element
    
    for element in lista:
        if element == maxim1:
            print(maxim2)
        else:
            print(maxim1)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() 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)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n = I()
L = IR(n)
sorted_L = sorted(L)
k1, k2 = sorted_L[-1], sorted_L[-2]


for i in range(n):
    if L[i] == k1:
        print(k2)
    else:
        print(k1)

Submission status: Accepted

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

A_sorted = sorted(A)

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

Submission status: Accepted

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

sorted_A = sorted(A)
gt1 = sorted_A[-1]
gt2 = sorted_A[-2]

for a in A:
    if a == gt1:
        print(gt2)
    else:
        print(gt1)

Submission status: Accepted

Solution code:
N = int(input())

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

new = sorted(L)


ans = 0
for i in range(N):
    if L[i] != new[-1]:
        ans = new[-1]
    else:
        ans = new[-2]
    print(ans)

Submission status: Accepted

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

b = sorted(a)
max_ind = a.index(b[n - 1])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

maxv = max(A)
max_id = A.index(maxv)
second = max(A[:max_id] + A[max_id+1:])

for i in range(N):
  if i == max_id:
    print(second)
  else:
    print(maxv)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

n = int(input())
A = []
for _ in range(n):
    A.append(int(input()))

tmp = copy.deepcopy(A)
tmp.sort(reverse=True)

M = max(A)
if A.count(M) >= 2:
    for _ in range(n):
        print(M)
else:
    for a in A:
        if a == M:
            print(tmp[1])
        else:
            print(M)

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:
N=int(input())
A=[int(input()) for i in range(N)]
MAX1=max(A)
copyA=A.copy()
copyA.remove(MAX1)
MAX2=max(copyA)
for i in A:
  if (i==MAX1):
    print(MAX2)
  else:
    print(MAX1)

Submission status: Accepted

Solution code:
#134

N = int(input())
A_max = 0
A_max_s = 0
flag = 0
A = []

for i in range(N):
    a = int(input())
    A.append(a)
    if a > A_max:
        A_max_s = A_max
        A_max = a
        ind_max = i
        flag = 0
    elif A_max_s < a < A_max:
        A_max_s = a
    elif a == A_max:
        flag += 1
        
if flag >= 1:
    for i in range(N):
        print(A_max)
elif flag == 0:
    for i in range(N):
        if i != ind_max:
            print(A_max)
        else:
            print(A_max_s)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

N = int(input())
A = [int(input()) for i in range(N)]
hq = []
for i in range(N):
    heappush(hq, -A[i])
max1 = -heappop(hq)
max2 = -heappop(hq)
ans = [0] * N
for i in range(N):
    if A[i] == max1:
        ans[i] = max2
    else:
        ans[i] = max1
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
N = int(input())
A = []
for c in range(N):
  A.append(int(input()))
m, i = 0, -1
for j in range(len(A)):
  if A[j] > m:
    m, i = A[j], j
B = A[:]
B.remove(m)
M = max(B)
for k in range(len(A)):
  if k != i:
    A[k] = m
  else:
    A[k] = M
for l in range(len(A)):
  print(A[l])

Submission status: Accepted

Solution code:
n = int(input())
ind = []
m = 0
k = 0
for i in range(n):
    a = int(input())
    if m == a:
        ind = -1
    elif m < a:
        k, m = m, a
        ind = i
    elif k < a:
        k = a
if ind == -1:
    print("\n".join(map(str, [m for i in range(n)])))
else:
    print("\n".join(map(str, [k if i == ind else m for i in range(n)])))

Submission status: Accepted

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

if A.count(base_ans) >= 2:
    for i in range(N):
        print(base_ans)
else:
    ans_list=[base_ans]*N
    A.remove(base_ans)
    ans_min = max(A)
    ans_list[index]=ans_min
    for i in range(N):
        print(ans_list[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
c=0
m=0
for i in range(N):
  if m<A[i]:
    m=A[i]
    d=i
B=A
B[d]=0
for i in range(N):
  if i==d:
    print(max(B))
  else:
    print(m)

Submission status: Accepted

Solution code:
import copy #listは参照渡しだからこれが必要

n = int(input())
a = [int(input()) for i in range(n)]
max_a = max(a)
count_max = a.count(max_a)

if count_max > 1:
    for i in range(n):
        print(max_a)
else:
    for i in range(n):
        if a[i] != max_a:
            print(max_a)
        else:
            b = copy.deepcopy(a) #b = aだとアドレス？ごとbも渡してしまう
            b.pop(b.index(max_a))
            print(max(b))

Submission status: Accepted

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

A_sort = sorted(A)
first = A_sort[-1]
second = A_sort[-2]
if first == second:
    for n in range(N):
        print(first)
else:
    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 i in range(n)]
a_1 = max(a)
a_2 = sorted(a)[-2]
for i in a:
    if i != a_1: print(a_1)
    else: print(a_2)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
maxa=max(a)
if a.count(maxa)>=2:
    for i in range(n):
        print(maxa)
else:
    maxi=a.index(maxa)
    a.remove(maxa)
    maxa2=max(a)
    for i in range(n):
        if i==maxi:
            print(maxa2)
        else:
            print(maxa)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(int(input()) for i in range(N))
B = sorted(A)
print(*((B[N-2] if A[i]==B[N-1] else B[N-1]) for i in range(N)), sep="\n")

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

n = int(input())
l = [int(input()) for _ in range(n)]
m = max(l)
n = sorted(l)[-2]
for i in l:
    if i != m:
        print(m)
    else:
        print(n)

Submission status: Accepted

Solution code:

def main(): 

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

    max_v = 0
    max_sv = 0

    for i in range(n):
        if a[i] > max_v:
            max_sv = max_v
            max_v = a[i]
        elif a[i] > max_sv:
            max_sv = a[i]

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




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def test():
    n = int(input())
    s = 0; t = 0; index_ = 0
    for i in range(n):
        a = int(input())
        if s < a:
            index_ = i
            s = a
        elif t < a and a <= s:
            t = a
    for i in range(n):
        if index_ == i:
            print(t)
        else:
            print(s)


if __name__ == "__main__":
    test()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')


N=int(input())
l=[]
for i in range(N):
	l.append(int(input()))
	
l_sort=sorted(l)

max=l_sort[-1]
sub_max=l_sort[-2]

for i in l:
	if i == max:
		print(sub_max)
	else:
		print(max)

Submission status: Accepted

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

Submission status: Accepted

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

sorted_A = sorted(A)
max_a = sorted_A[-1]
second_max = sorted_A[-2]

for i in A:
  if i == max_a:
    print(second_max)
  else:
    print(max_a)

Submission status: Accepted

Solution code:
import copy
N = int(input())
A = [int(input()) for i in range(N)]
set_A = list(set(A))
set_A.sort(reverse=True)
A_copy = copy.deepcopy(A)
A_copy.sort(reverse=True)
Max = max(A)
cnt = 0
for i in range(N):
    if A[i] == Max:
        cnt += 1
for i in range(N):
    if A[i] == Max:
        if cnt > 1:
            print(Max)
        else:
            print(set_A[1])
    else:
        print(Max)

Submission status: Accepted

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

max = 0
sub_max = 0
for x in A:
    if max <= x:
        sub_max = max
        max = x
    elif sub_max <= x:
        sub_max = x

for x in A:
    if x == max:
        print(sub_max)
    else:
        print(max)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import copy
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
orig_a = copy.copy(a)
a.sort(reverse=True)
max_a = a[0]
semi_max_a = a[1]

if max_a == semi_max_a:
    for i in range(n):
        print(semi_max_a)
else:
    for i in range(n):
        if orig_a[i] == max_a:
            print(semi_max_a)
        else:
            print(max_a)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
max_num = max(A)
temp = A.copy()
temp.remove(max_num)
max_num2 = max(temp)

for a in A:
    if a == max_num:
        print(max_num2)
    else:
        print(max_num)

Submission status: Accepted

Solution code:
#Coding:Utf-8

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

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

Submission status: Accepted

Solution code:
while True:
    try:
        N = int(input())
        A = [int(input()) for _ in range(N)]
        B = sorted(A)
        max1 = B[-1]
        max2 = B[-2]
        for _ in A:
            if _ == max1:
                print(max2)
            else:
                print(max1)
    except:
        break

Submission status: Accepted

Solution code:
N = raw_input()
counter = 0
num_array = []
max_data = 0
second_max = 0
while counter < int(N):
  num_array.append(int(raw_input()))
  if(num_array[counter] > max_data):
    max_data = num_array[counter]
  elif(num_array[counter] > second_max):
    second_max = num_array[counter]
  counter += 1
counter = 0  
while counter < int(N):
  if(num_array[counter] != max_data):
    print max_data
  else:
    print second_max
  counter += 1

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
sort_a=sorted(a,reverse=True)

for j in a:
    if j==sort_a[0]:
        print(sort_a[1])
    else:
        print(sort_a[0])

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
from heapq import *
sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N = int(input())
    AS = [int(input()) for _ in range(N)]
    AS_dup = AS[:]
    AS.sort()
    for i in range(N):
        if AS_dup[i] == AS[-1]:
            print(AS[-2])
        else:
            print(AS[-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
b=sorted(a)
ans=[b[-1]]*n
ans[a.index(b[-1])]=b[-2]
print(*ans,sep="\n")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
B = sorted(A,reverse=True)
if B[0] == B[1]:
    for i in range(N):
        print(B[0])
else:
    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 = [int(input()) for _ in range(n)]
s = max(a)
if a.count(s) >= 2:
  for i in range(n):  
    print(s)
else:
  for i in range(n):
    if a[i] == s:
      l = a.copy()
      l.sort()
      print(l[-2])
    else:
      print(s)

Submission status: Accepted

Solution code:
N = int(input())
a = []
for _ in range(N):
    a.append(int(input()))
a_sorted = sorted(a)
max_a = a_sorted.pop()
second_a = a_sorted.pop()
if max_a == second_a:
    for _ in range(N):
        print(max_a)
else:
    idx = 0
    while idx < N:
        if a[idx] == max_a:
            print(second_a)
        else:
            print(max_a)
        idx += 1

Submission status: Accepted

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

B = [A[i] for i in range(N)]
  
m1 = max(A)
idx = A.index(m1)
B.pop(idx)
m2 = max(B)

for i in range(N):
  if i == idx:
    print(m2)
  else:
    print(m1)

Submission status: Accepted

Solution code:
n = int(input())
seq = [int(input()) for i in range(n)]
s_seq = sorted(seq, reverse = True)

for x in seq:
    print(s_seq[s_seq[0] == x])

Submission status: Accepted

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

data_set = sorted(data, reverse=True)

max1, max2 = data_set[0], data_set[1]

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = sorted({i: int(input()) for i in range(N)}.items(), key=lambda x: x[1])
    for index in range(N):
        if not A[-1][0] == index:
            print(A[-1][1])
        else:
            print(A[-2][1])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=[0]*n
for i in range(n):
    a[i]=int(input())
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 = [int(input()) for _ in range(N)]
m1,m2 = sorted(A)[-2:]

for i in A:
    print(m1 if i==m2 else m2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
m1,m2,*_ = sorted(A)[::-1]

for i in A:
    if m1 == i:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

Solution code:
n=int(input())
A=[int(input()) for _ in range(n)]
m1, m2=0, 0
for a in A:
  if a >= m1:
    m1, m2 = a, m1
  elif a >= m2:
    m2=a
for a in A:
  if a==m1:
    print(m2)
  else:
    print(m1)

Submission status: Accepted

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

a1=max(a)
b=sorted(a)
a2=b[N-2]

for j in range(N):
  if a[j] != a1:
    print(a1)
  else:
    print(a2)

Submission status: Accepted

Solution code:
N = int(input())
A =[]
ma = 0
se = 0
for i in range(N):
  a = int(input())
  A.append(a)
  ma = max(ma,a)
b = sorted(A)
ma = b[N-1]
se = b[N-2]
for i in range(N):
  if A[i]==ma:
    print(se)
  else:
    print(ma)

Submission status: Accepted

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


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


# Generated by 1.1.5 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())
lis=list(int(input()) for _ in range(n))
lis2=lis
lis2=sorted(lis2)
MAX=lis2[-1]
second=lis2[-2]
if lis.count(MAX) ==1:
    lis3=[MAX for _ in range(n)]
    lis3[lis.index(MAX)]=second
    for i in range(n):
        print(lis3[i])
        
else:
    for _ in range(n):
        print(MAX)

Submission status: Accepted

Solution code:
import heapq as hq
n = int(input())
arr = []
stack = []
for i in range(n):
    arr.append(int(input()))
    hq.heappush(stack, -arr[-1])

first = -hq.heappop(stack)
second = - hq.heappop(stack)

for i in arr:
    print(first if i != first else second)

Submission status: Accepted

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

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[0])
    else:
        print(B[1])

Submission status: Accepted

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

lis = sorted(li)
for i in range(N):
  if lis[-1] == li[i]:
    print(lis[-2])
  else: print(lis[-1])

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:
N = int(input())

idx_1st=1
val_1st=1
idx_2nd=0
val_2nd=0

for i in range(N):
  Ai = int(input())
  if Ai>=val_1st:
    idx_2nd=idx_1st
    val_2nd=val_1st
    
    idx_1st=i
    val_1st=Ai
    
  elif Ai<val_1st:
    if Ai>val_2nd:
      idx_2nd=i
      val_2nd=Ai
  else:
    raise ValueError

for i in range(N):
  if i!=idx_1st:
    print(val_1st)
  elif i==idx_1st:
    print(val_2nd)
  else:
    raise ValueError

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for x in range(n)]
b = sorted(a[::])
a_1, a_2 = b[-1], b[-2]
c = a.count(a_1)

for i in a:
  if c == 1 and i == a_1:
    print(a_2)
  else:
    print(a_1)

Submission status: Accepted

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

n = int(input())

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

a.sort(reverse=True)

for x in a_copy:
    if x == a[0]:
        print(a[1])
    else:
        print(a[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [int(input().rstrip('\n')) for _ in range(n)]
    b = sorted(a, reverse=True)
    for i in a:
        if i == b[0]:
            print(b[1])
        else:
            print(b[0])


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
a2 = sorted(a)[-2:]
for i in range(n):
    if a[i]==a2[1]:
        print(a2[0])
    else:
        print(a2[1])

Submission status: Accepted

Solution code:
import numpy as np
ls = []
n = int(input())
for i in range(n):
    ls.append(int(input()))
ls = np.array(ls)
maxindex1 = np.argmax(ls)
for j in range(n):
    if j != maxindex1:
        print(ls[maxindex1])
    else:
        tmp = ls[maxindex1]
        ls[maxindex1] = 0 
        maxindex2 = np.argmax(ls)
        print(ls[maxindex2])
        ls[maxindex1] = tmp

Submission status: Accepted

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

Submission status: Accepted

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

sorted_a = sorted(a)
a_first = sorted_a[-1]
a_second = sorted_a[-2]

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

Submission status: Accepted

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

#最大値とその次に大きい値を取り出す
first_A = sorted(A)[-1]
second_A = sorted(A)[-2]

for i in range(N):
    if (A[i]== first_A):
        print(second_A)
    else:
        print(first_A)

Submission status: Accepted

Solution code:
N = int(input())
A = [0] * N
for i in range(N):
  A[i] = int(input())
  
A_Max = sorted(A)[-1]
A_2nd = sorted(A)[-2]

for i in range(N):
  if A[i] == A_Max:
    print(A_2nd)
  else:
    print(A_Max)

Submission status: Accepted

Solution code:
n = int(input())
a1 = [int(input()) for i in range(n)]
a2 = sorted(a1, reverse=True)

for v1 in a1:
    count = 0
    for v2 in a2:
        if v1 == v2 and count == 0:
            count += 1
        else:
            print(v2)
            break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import copy

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

B = sorted(A, reverse=True)
m1 = B[0]
m2 = B[1]

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

"""
# TLE
B = copy.copy(A) # B=AとするだけではAを変更した時にBも変更してしまう
for i in range(N):
    A[i] = 0
    print(max(A))
    A[i] = B[i]
"""

Submission status: Accepted

Solution code:
a=int(input())
b=[int(input()) for i in range(a)]
sr=list(reversed(sorted(b)))
if sr[1]==sr[0]:
  for i in range(a):
    print(sr[0])
else:
  for i in b:
    if i==sr[0]:
      print(sr[1])
    else:
      print(sr[0])

Submission status: Accepted

Solution code:
import sys
import math
from copy import deepcopy
from collections import deque
sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    nums = [int(input()) for _ in range(N)]
    a = deepcopy(nums)
    a.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

n = int(input())
# n = 3

arr = [0]*n

pq = []
for i in range(n):
	arr[i] = int(input())
	heapq.heappush(pq, (-arr[i], i))
heapq.heapify(pq)
lg = heapq.heappop(pq)
slg = heapq.heappop(pq)
lg, slg = lg[1], slg[1]

for i in range(n):
	if i==lg:
		print(arr[slg])
	else:
		print(arr[lg])

Submission status: Accepted

Solution code:
n = int( input() )
first = 0
second = 0
a = []
for i in range(n):
    tmp = int(input())
    if tmp >= first:
        second = first
        first = tmp
    elif tmp > second:
        second = tmp
    a.append(tmp)

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

Submission status: Accepted

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

copy_a = copy.deepcopy(a)
max_num = max(a)
a.remove(max_num)
second_num = max(a)

for i in range(n):
  if copy_a[i] >= max_num:
    print(second_num)
  else:
    print(max_num)

Submission status: Accepted

Solution code:
# n = 3
n = int(input())

arr = [0]*n

for i in range(n):
	arr[i] = (int(input()), i)

sorted_arr = sorted(arr, reverse=True)
lg, slg = sorted_arr[0][1], sorted_arr[1][1]
for i in range(n):
	if i==lg:
		print(arr[slg][0])
	else:
		print(arr[lg][0])

Submission status: Accepted

Solution code:
#C - Exception Handling(ABC134)
N=int(input())
B=0
b=0
C=0
for i in range(N):
    A=int(input())
    if B<A:
        B=A
        b=i
    elif C<A:
        C=A
for i in range(N):
    if b==i:
        print(C)
    else:
        print(B)

Submission status: Accepted

Solution code:

n = int(input())
la = [int(input()) for i in range(n)]

s_la = sorted(la, reverse=True)

for a in la:
	if a == s_la[0]:
		print(s_la[1])
	else:
		print(s_la[0])

Submission status: Accepted

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

mi = li.index(max(li))
m = max(li)

for a, b in enumerate(li):
  if a != mi:
    print(m)
  else:
    print(max(li[a+1:]+li[:a]))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
max_a = max(a)
max_idx = a.index(max_a)
for i in range(n):
    if i == max_idx:
        tmp = a
        tmp.sort(reverse=True)
        print(tmp[1])
    else:
        print(max_a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#また長さが1の時を考慮していない
#出来るだけループの外で計算する
n=int(input())
a=[int(input()) for i in range(n)]
m1=max(a)
m2=sorted(a,reverse=True)[1]
for i in range(n):
    if m1==a[i]:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
list1=[]
for i in range(n):
    list1.append(int(input()))
list2=sorted(list1)
#print(list2)
big1=list2[-1]
big2=list2[-2]
for i in range(n):
    if big1==list1[i]:
        print(big2)
    else:
        print(big1)

Submission status: Accepted

Solution code:
n=int(input())
list1=[0 for i in range(2)]#list1[0]が最大値 list1[1]が二番めに大きい値
list2=[0]*2#list2[0]が最大値index list2[1]が二番めに大きい値index
count=1
for i in range(n):
    a=int(input())
    if list1[0]<=a:
        list1[1]=list1[0]
        list1[0]=a
        list2[1]=list2[0]
        list2[0]=count
    elif list1[1]<=a:
        list1[1]=a
        list2[1]=count
    count+=1
#print(list1)
#print(list2)
for i in range(1,n+1):
    if i==list2[0]:
        print(list1[1])
    else:
        print(list1[0])

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:
N = int(input())
A = [int(input()) for i in range(N)]
M = max(A)
Mn = A.count(M)
if Mn == N:
    m = 0
else:
    m = max([i for i in A if i!=M])

for i in range(N):
    if Mn >= 2:
        print(M)
    else:
        if A[i] == M:
            print(m)
        else:
            print(M)

Submission status: Accepted

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

B = list(reversed(list(sorted(A))))
maxA = max(A)

for e in A:
    if e == maxA:
        print(B[1])
    else:
        print(B[0])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [int(input().rstrip('\n')) for _ in range(n)]
    b = sorted(a, reverse=True)
    for i in a:
        if i == b[0]:
            print(b[1])
        else:
            print(b[0])
            
            
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for i in range(n)]
maxA = max(A)
if A.count(maxA) == 1:
  maxA_2 = max([i for i in A if i != maxA])
  for i in range(n):
    if A[i] != maxA:
      print(maxA)
    else:
      print(maxA_2)
else:
  for i in range(n):
    print(maxA)

Submission status: Accepted

Solution code:
import copy

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

m_1 = sorted(a)[-1]
m_2 = sorted(a)[-2]

for i in range(n):
    if a[i] == m_1:
        print(m_2)
    else:
        print(m_1)

Submission status: Accepted

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

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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