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:
def main():

    N = int(input())
    A = []
    for i in range(N):
        A.append(int(input()))
    prefix = [0]
    suffix = [0]
    for i in range(N):
        prefix.append(max(prefix[-1], A[i]))
        suffix.append(max(suffix[-1], A[N-i-1]))
    suffix.sort(reverse = True)

    for i in range(N):
        ans = max(prefix[i], suffix[i+1])
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = [list(map(int, input().split())) for _ in range(n)]
m = sorted(l)
max = m[-1][0]
sub_max = m[-2][0]
for i in range(n):
    if l[i][0] == max:
        print(sub_max)
    else:
        print(max)

Submission status: Accepted

Solution code:
n = int(input())

a = [int(input()) for _ in range(n)]
sorted_a = sorted(a, reverse=True)

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

Submission status: Accepted

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

A = sorted(A, key=lambda x:x[0], reverse=True)
for i in range(N):
    if A[0][1] == i:
        print(A[1][0])
    else:
        print(A[0][0])

Submission status: Accepted

Solution code:
def main():

    n,*a = map(int,open(0).read().split())
    b = sorted(a)
    max = b[-1]
    maxs= b[-2]

    for i in range(n):
        if a[i] == max:
            print(maxs)
        else:
            print(max)
    return()

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n,*a = map(int,open(0).read().split())
b = sorted(a)
max = b[-1]
maxs= b[-2]

for i in range(n):
    if a[i] == max:
        print(maxs)
    else:
        print(max)

Submission status: Accepted

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

max_A = max(A)
max_A_count = A.count(max_A)

set_A = set(A)
len_set_A = len(set_A)

if(len_set_A > 1):
    set_A.remove(max_A)

max_set_A = max(set_A)

for i in range(N):
    if(A[i] < max_A):
        print(max_A)
    elif A[i] == max_A and max_A_count > 1:
        print(max_A)
    else:
        print(max_set_A)

Submission status: Accepted

Solution code:
def main():
    n, *a = map(int, open(0).read().split())

    sorted_a  = sorted(a)
    max_a = sorted_a[-1]
    next_max_a = sorted_a[-2]
    for cur_a in a:
        if cur_a == max_a:
            print(next_max_a)
        else:
            print(max_a)
    return()

if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = heapq.nlargest(2,a)

if m[0]==m[1]:
    for i in range(n):
        print(m[0])

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

Submission status: Accepted

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

# ほとんどは全体のmax?
m1 = max(a)

# max値が多い場合用に2nd max
m2 = sorted(a)[n-2]

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

Submission status: Accepted

Solution code:
N = int(input())

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

_A = sorted(A)

list_max = _A[N-1]
list_second_max = _A[N-2]

import copy

for i in range(N):
    if A[i] != list_max:
        print(list_max)
    else:
        print(list_second_max)

Submission status: Accepted

Solution code:
#134c tle
n=int(input())
s=[int(input()) for i in range(n)]
a=sorted(s)
max_=a[-1]
max_2=a[-2]
#print(s)
for i in range(n):
    if s[i]==max_:
        print(max_2)
    else:
        print(max_)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A1 = sorted(A,reverse=True)
for i in range (0,N):
  if A[i] == A1[0]:
    print(A1[1])
  else:
    print(A1[0])

Submission status: Accepted

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


nn = int(input())
n = []
for i in range(nn):
    n.append(int(input()))
a = ia = b = ib = c = 0
for i in n:
    if a<i: a = i; ia = c
    elif b<i: b = i; ib = c
    c += 1
for i in range(len(n)):
    if i == ia: print(b)
    else: print(a)

Submission status: Accepted

Solution code:
n = int(input())
A = []
for i in range(n):
	A.append(int(input()))           
B = sorted(A)
f = B[n-1]
s = B[n-2]

for i in A:
		if i != f:
				print(f)
		else:
				print(s)

Submission status: Accepted

Solution code:
N = int(input())
max_val_1 = 0
max_val_2 = 0
for i in range(N):
    A = int(input())
    if max_val_1 < A:
        max_val_2 = max_val_1
        max_val_1 = A
        max_num = i
    elif max_val_2 < A:
        max_val_2 = A
        
for i in range(N):
    if i == max_num:
        print(max_val_2)
    else:
        print(max_val_1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = [int(input()) for i in range(n)]
b =sorted(a, reverse = True)
#print(b)
#print(a)
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)]
A_sort = sorted(A)
for i in range(N) :
    if A[i] == A_sort[-1] :
        print(A_sort[-2])
    else :
        print(A_sort[-1])

Submission status: Accepted

Solution code:
n = int(input())
a = [0 for _ in range(n)]
max_a = 0
second_a = 0
for i in range(n):
    a[i] = int(input())
    if a[i] > max_a:
        max_a = a[i]
    elif a[i] > second_a:
        second_a = a[i]
for i in range(n):
    if a[i] == max_a:
        print(second_a)
    else:
        print(max_a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    value = int(input())
    a.append(value)
temp = sorted(a)
max_a = temp[-1]
if len(a)>=2:
    second_a = temp[-2]
for i in range(n):
    if a[i] == max_a:
        check = i
        break
for i in range(n):
    if i == check:
        print(second_a)
    else:
        print(max_a)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
   a.append(int(input()))
b=sorted(a,reverse=True)
tempa=b[0]
tempb=b[1]
for i in range(n):
    if a[i]<tempa:
        print(tempa)
    else:
        print(tempb)

Submission status: Accepted

Solution code:
N = int(input())

m1, m2 = 0, 0
A = [0] * N

for i in range(N):
  a = int(input())
  A[i] = a
  if a >= m2:
    if a >= m1:
      m2 = m1
      m1 = a
    
    else:
      m2 = a

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

Submission status: Accepted

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

if N == 2:
    print("\n".join(map(str, A[::-1])))
else:
    a = sorted(A)
    maxv1 = a[-1]
    maxi1 = A.index(maxv1)
    maxv2 = a[-2]

    print("\n".join(map(str, [maxv1 if i != maxi1 else maxv2 for i in range(N)])))

Submission status: Accepted

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

if N == 2:
    print("\n".join(map(str, A[::-1])))
else:
    a = sorted(A)
    maxv1 = a[-1]
    maxi1 = A.index(maxv1)
    maxv2 = a[-2]

    for i in range(N):
        if i == maxi1:
            print(maxv2)        
        else:
            print(maxv1)

Submission status: Accepted

Solution code:
import copy
N = int(input())
A = [int(input()) for _ in range(N)]
a_copy = copy.deepcopy(A)
a_max = max(a_copy)
a_copy.remove(a_max)
b_max = max(a_copy)

for i in A:
    if i == a_max:
        print(b_max)
    else:
        print(a_max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N  = int(input())
max = 1
max2nd = 0
max_index = 0

for index in range(N):
    a = int(input())
    if a >= max:
        max2nd = max
        max = a
        max_index = index
    elif a >= max2nd:
        max2nd = a

for index in range(N):
    if not index == max_index:
        print(max)
    else:
        print(max2nd)

Submission status: Accepted

Solution code:
# C - Exception Handling
N = int(input())
A = [int(input()) for i in range(N)]

#print(A)
max1 = sorted(A, reverse=True)
max2 = max1[0]

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

    else:
        print(max2)

Submission status: Accepted

Solution code:
n = int(input())
li_a = list()
d_a = dict()

for _ in range(n):
    wk = int(input())
    li_a.append(wk)

    get = d_a.get(wk, 0)
    d_a[wk] = get + 1

li_top2 = sorted(d_a.keys(), reverse=True)[:2]

for i in range(n):
    val = li_a[i]

    if val not in li_top2:
        print(li_top2[0])
    else:
        if val == li_top2[0] and d_a.get(val, 0) == 1:
            print(li_top2[1])
        else:
            print(li_top2[0])

Submission status: Accepted

Solution code:
SEQUENCE_LEN_MIN = 2
SEQUENCE_LEN_MAX = 200000
NUMERIC_MIN = 1
NUMERIC_MAX = 200000
 
 
class NumericInfo:
    def __init__(self):
        self.index = - 1
        self.value = NUMERIC_MIN - 1
 
    def update(self, idx, val):
        self.index = idx
        self.value = val
 
    def copy(self, info):
        self.update(info.index, info.value)
 
 
class SequenceProcessor:
    def __init__(self, sequence):
        self.sequence_length = len(sequence)
        self.first_largest_num_info = NumericInfo()
        self.second_largest_num_info = NumericInfo()
        self._process(sequence)
 
    def _process(self, sequence):
        for i, num in enumerate(sequence):
            if self.first_largest_num_info.value < num:
                self.second_largest_num_info.copy(self.first_largest_num_info)
                self.first_largest_num_info.update(i, num)
            elif self.second_largest_num_info.value < num:
                self.second_largest_num_info.update(i, num)
 
    def _max_value(self, idx):
        if idx == self.first_largest_num_info.index:
            return self.second_largest_num_info.value
        else:
            return self.first_largest_num_info.value
 
    def all_max_value(self):
        for i in range(self.sequence_length):
            yield self._max_value(i)
 
 
def _validate_sequence_length(s):
    r = int(s)
 
 
def get_sequence_length_from_stdin():
    s = input()
    s = s.strip()
    return int(s)
 
 
def _validate_numeric(s):
    r = int(s)
 
 
def get_sequence_from_stdin(length):
    result = []
    for i in range(1, length+1):
        while True:
            n = input()
            try:
                result.append(int(n))
                break
            except ValueError:
                continue
    return result
 
 
def main():
    seq_len = get_sequence_length_from_stdin()
    sequence = get_sequence_from_stdin(seq_len)
    seq_processor = SequenceProcessor(sequence)
    for num in seq_processor.all_max_value():
        print(num)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 累積和(max)
N = int(input())
a = [int(input()) for _ in range(N)]
lcm = a[:]
rcm = a[:]
for i in range(1, N):
  lcm[i] = max(lcm[i], lcm[i - 1])
for i in range(N - 2, -1, -1):
  rcm[i] = max(rcm[i], rcm[i + 1])
print(rcm[1])
for i in range(1, N - 1):
  print(max(lcm[i - 1], rcm[i + 1]))
print(lcm[N - 2])

Submission status: Accepted

Solution code:
# 累積和(max)
N = int(input())
a = [int(input()) for _ in range(N)]
lam = a[:]
ram = a[:]
for i in range(1, N):
  lam[i] = max(lam[i], lam[i - 1])
for i in range(N - 2, -1, -1):
  ram[i] = max(ram[i], ram[i + 1])
print(ram[1])
for i in range(1, N - 1):
  print(max(lam[i - 1], ram[i + 1]))
print(lam[N - 2])

Submission status: Accepted

Solution code:
N = int(input())
a = [int(input()) for _ in range(N)]
lam = [0] * N
ram = [0] * N
lam[0] = a[0]
ram[N - 1] = a[N - 1]
for i in range(1, N):
  lam[i] = max(lam[i - 1], a[i])
  ram[N - 1 - i] = max(ram[N - 1 - i + 1], a[N - 1 - i])
print(ram[1])
for i in range(1, N - 1):
  print(max(lam[i - 1], ram[i + 1]))
print(lam[N - 2])

Submission status: Accepted

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

for i in range(n):
  value = int(input())  
  max_value = max(max_value, value)
  a.append(value)
  
for i in range(n):
  if a[i] == max_value:
    max_count += 1
  
if max_count > 1:
  for i in range(n):
    print(max_value)
else:
  max_index = a.index(max_value)
  a.pop(max_index)
  second_max = max(a)
  
  for i in range(n):
    if i != max_index:
      print(max_value)
    else:
      print(second_max)

Submission status: Accepted

Solution code:
import heapq  # heapqライブラリのimoprt
N = int(input())
A = []

for i in range(N):
 a = int(input())
 if(i == 0):
    A_max,A_max_index = a,i
 else: 
    if(A_max <= a):
      A_max,A_max_index = a,i
 A.append(a)

for i in range(N):
  if(i != A_max_index): 
    print(A_max)
  else:
    A[i] = 0
    A =  list(map(lambda x: x*(-1), A))
    heapq.heapify(A)
    ans = -heapq.heappop(A)
    print(ans)

Submission status: Accepted

Solution code:
import copy
n = int(input())
alist = [int(input()) for i in range(n)]
blist = copy.deepcopy(alist)

alistmax = max(alist)
blist.remove(alistmax)
alist2max = max(blist)

for i in range(n):
    if alist[i] == alistmax:
        print(alist2max)
    else:
        print(alistmax)

Submission status: Accepted

Solution code:
n = int(input())

a = list(int(input()) for _ in range(n))
b = sorted(a)

max = b[n-1]
sec_max = b[n-2]

for i in range(n):
  if a[i] == max:
    print(sec_max)
  else:
    print(max)

Submission status: Accepted

Solution code:
import heapq
import copy

N = int(input())
A_l = [int(input()) * -1 for _ in range(N)] 
A = copy.deepcopy(A_l)
heapq.heapify(A_l)

m = heapq.heappop(A_l) * -1
n = heapq.heappop(A_l) * -1

for a in A:
  if a * -1 == m:
    print(n)
  else:
    print(m)

Submission status: Accepted

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

s_A_l = sorted(A_l)

m = s_A_l[-1]
n = s_A_l[-2]


for a in A_l:
  if a == m:
    print(n)
  else:
    print(m)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))
list = A
list = sorted(list)
max = list[-1]
second_max = list[-2]
cnt = A.count(max)
for i in range(N):
    if A[i] == max and  cnt == 1:
        print(second_max)
    else:
        print(max)

Submission status: Accepted

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

B = sorted(A, reverse = True)
max_val = B[0]
second_max_val = B[1]

for val in A:
    if val == max_val:
        print(second_max_val)
    else:
        print(max_val)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
max_a = max(a)
set_a = set(a)
max_n = a.count(max_a)
if len(set_a) == 1:
    s_set_a = max_a
else:
    if max_n > 1:
        s_set_a = max_a
    else:
        s_set_a = sorted(set_a)[-2]

for i in range(n):
    if max_a == a[i]:
        print(s_set_a)
    else:
        print(max_a)

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 b[0] == a[i]:
        ans = b[1]
    else:
        ans = b[0]
    print(ans)

Submission status: Accepted

Solution code:
import math
import copy
N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input())
#修正ver
max_item = max(A)
count = 0
second_item = 0
for i in range(N):
    if A[i] == max_item:
        count += 1
    elif second_item < A[i]:
        second_item = A[i]
    if count > 1:
        break
X = [0] * N
judge = False
for i in range(N):
    if count > 1:
        judge = True
        break
    if A[i] == max_item:
        X[i] = second_item
    else:
        X[i] = max_item

if judge:
    for i in range(N):
        X[i] = max_item
for i in range(N):
    print(X[i])

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
n = int(stdin.readline().rstrip())
ary = np.array([int(stdin.readline().rstrip()) for _ in range(n)])
m = ary.argmax()
mv = ary.max()
for i in range(n):
    if i == m:
        if i == 0:
            print(np.amax(ary[i + 1:]))
        elif i == n - 1:
            print(np.amax(ary[:i]))
        else:
            print(max(np.amax(ary[:i]), np.amax(ary[i + 1:])))
    else:
        print(mv)

Submission status: Accepted

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

maxL = max(L)
subL = L
num = ["n"] * N

if L.count(maxL) >= 2:
	for i in range(N):
		print(maxL)
		
else:	
	for i in range(N):
		if L[i] == maxL:
			L[i] = 0
			print(max(L))
		else:
			print(maxL)

Submission status: Accepted

Solution code:
n = int(input())
nums = [int(input()) for i in range(n)]
n_max = max(nums)
ex_nums = [i for i in nums]
ex_nums.remove(n_max)
n_semi_max = max(ex_nums)
for i in nums:
    if i == n_max:
        print(n_semi_max)
    else:
        print(n_max)

Submission status: Accepted

Solution code:
import copy
N = int(input())
A = [int(input()) for i in range(N)]
tmp = copy.copy(A)
tmp.sort()
MAX = tmp[-1]
MAXsub = tmp[-2]
place = A.index(MAX)
for i in range(N):
    if i != place:
        print(MAX)
    else:
        print(MAXsub)

Submission status: Accepted

Solution code:
import sys
import heapq
import copy

n = int(input())
a = []

for _ in range(n):
	a.append(-1 * int(input()))

b = copy.deepcopy(a)
heapq.heapify(b)

for i in range(n):
	maximum = heapq.heappop(b)
	if maximum == a[i]:
		tmp = maximum
		maximum = heapq.heappop(b)
		print(maximum * (-1))
		heapq.heappush(b,tmp)
	else:
		print(maximum * (-1))
	heapq.heappush(b,maximum)

Submission status: Accepted

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

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

Submission status: Accepted

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

cs = sorted(A)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
b = max(a)
maxid = a.index(b)
for i in range(N):
    if i != maxid:
        print(b)
    elif i == maxid:
        del a[i]
        print(max(a))

Submission status: Accepted

Solution code:
N = int(input())
An = list()
for i in range(N):
    An.append(int(input()))
MaxIndex = An.index(max(An))
SecondIndex = An.index(sorted(An)[-2])
for i in range(0,N):
    if i == MaxIndex:
        print(An[SecondIndex])
    else:
        print(An[MaxIndex])

Submission status: Accepted

Solution code:
n = int(input())
f_max = s_max = 0
arr = []
for _ in range(n):
    a = int(input())
    arr.append(a)
    if a >= f_max:
        s_max = f_max
        f_max = a
    elif a >= s_max:
        s_max = a

for integer in arr:
    if integer == f_max:
        print(s_max)
    else:
        print(f_max)

Submission status: Accepted

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

MAX = max(A)
SECONDMAX = sorted(A)[N-2]

for i in range(N):
    if A[i] ==MAX:
        print(SECONDMAX)
    else:
        print(MAX)

Submission status: Accepted

Solution code:
#134c
N = int(input())
list_A=[]
for i in range(N):
    list_A.append(int(input()))
    
# 1 以上 N 以下の各整数 iに対し、数列中の Aiを除く N−1 個の要素のうちの最大の値を求めよ。

max_1st_value_in_list_A = max(list_A)
max_1st_index_in_list_A = list_A.index(max(list_A))

list_A.pop(max_1st_index_in_list_A)

max_2nd_value_in_list_A = max(list_A)
#2番目に大きいもののインデックスは使わないのでコメントアウト
#max_2nd_index_in_list_A = list_A.index(max(list_A))
#
#if max_2nd_index_in_list_A>=max_1st_index_in_list_A:
#    max_2nd_index_in_list_A += 1  
#print(max_1st_index_in_list_A,max_1st_value_in_list_A)
#print(max_2nd_index_in_list_A,max_2nd_value_in_list_A)

for i in range(N):
    if i!=max_1st_index_in_list_A:
        print(max_1st_value_in_list_A)
    else:
        print(max_2nd_value_in_list_A)

Submission status: Accepted

Solution code:

n=int(input())
a=[int(input()) for i in range(n)]
ma = max(a)
fut_max = a.index(ma)
for i in range(n):
    if(i!=fut_max):
        print(ma)
    else:
        a.pop(fut_max)
        print(max(a))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(0, N)]
ans1 = max(A)
s = A.index(ans1)

if A.count(ans1) >= 2:
    for i in range(N):
        print(ans1)
else:
    for i in range(N):
        if i == s:
            A.remove(ans1)
            print(max(A))
        else:
            print(ans1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
maxa = max(a)
seconda = a[:]
seconda.remove(maxa)
secondmaxa = max(seconda)
for i in range(0,n):
    if a[i] == maxa:
        print(secondmaxa)
    else:
        print(maxa)

Submission status: Accepted

Solution code:
n = int(input())

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

#リスト内の最大の値を格納
max_val = max(list_A)

#リスト内の最大の値の位置を格納
for i in range(len(list_A)):
    if list_A[i] == max_val:
        max_val_position = i
        break

#リスト内のから最大の要素を除いた後の，最大の要素
max_val_2 = max(list_A[:max_val_position] + list_A[max_val_position+1:])

for i in range(len(list_A)):
    
    #リスト内の最大の数が除かれたときは二番目に大きい数を出力する
    if i == max_val_position:
        print(max_val_2)
    else:
        print(max_val)

Submission status: Accepted

Solution code:
N = int(input())

#lst = []
#for i in range(N):
#    lst.append(int(input()))

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

lstA = sorted(lst,reverse=True)

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
nList = [int(input()) for _ in range(N)]
maxN = max(nList)
lenMaxN = np.argmax(nList)
ansList = [maxN for _ in range(N)]
if nList.count(maxN) >= 2:
    for i in range(N):
        print(ansList[i])

else:
    second = sorted(set(nList))[-2]
    ansList[lenMaxN] = second
    for i in range(N):
        print(ansList[i])

Submission status: Accepted

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

def main():
    n=int(input())
    a=[0 for _ in range(n)]
    for i in range(n):
        a[i]=int(input())
    M=max(a)
    cnt=0
    ans=0
    S=0
    for i in range(n):
        if(a[i]!=M and a[i]>S):
            S=a[i]
        elif(a[i]==M):
            cnt+=1
    
    for i in range(n):
        if(a[i]!=M or cnt>1):
            ans=M
        else:
            ans=S
        print(ans)        
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

a = [int(input()) for l in range(n)]
b = max(a)
c = sorted(a)[-2]
for i in (a):
	if not i==b:
		print(b)
	else:
		print(c)

Submission status: Accepted

Solution code:
import copy

n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(copy.copy(a))
ma = b[-1]
mi = b[-2]
for i in range(n):
  if a[i] == ma:
    print(mi)
  else:
    print(ma)

Submission status: Accepted

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

A_copy = A[:]
A_copy.sort()
A_max = A_copy[len(A_copy)-1]
A_second = A_copy[len(A_copy)-2]

for i in range(N):
    if A[i] != A_max:
        print(A_max)
    else:
        print(A_second)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
sys.setrecursionlimit(10 ** 7)


def solve(N: int, A: "List[int]"):
    if N == 1:
        print(0)
    else:
        m1 = max(A)
        max_idx = np.where(np.array(A) == max(A))
        if len(max_idx[0]) > 1:
            for i in range(N):
                print(m1)
        else:
            m2 = sorted(set(A))[-2]
            max_idx = np.argmax(np.array(A))
            for i in range(N):
                if i == max_idx:
                    print(m2)
                else:
                    print(m1)
    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())
a = []
max_num = -1
for i in range(n):
    ipt = int(input())
    if ipt > max_num:
        max_num = max(ipt, max_num)
        idx = i
    a.append(ipt)
a = sorted(a)
if a[-1] == a[-2]:
    for i in range(n):
        print(max_num)
else:
    for i in range(n):
        if i == idx:
            print(a[-2])
        else:
            print(max_num)

Submission status: Accepted

Solution code:
n = int(input())

a = []

top_num = 0
second_num = 0

for _ in range(n):
    a_i = int(input())
    if a_i >= top_num:
        second_num = top_num
        top_num = a_i
    else:
        second_num = max(a_i, second_num)
    a.append(a_i)

for a_i in a:
    if a_i == top_num:
        print(second_num)
    else:
        print(top_num)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A_copy = A[:]
A_copy.sort()
# print(A, A_sort)
A_max = A_copy[len(A_copy)-1]
A_second = A_copy[len(A_copy)-2]
for i in range(N):
    if A[i] != A_max:
        print(A_max)
    else:
        print(A_second)

Submission status: Accepted

Solution code:
N = int(input())
A = list(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)]
B = A[:]
B.sort()

a_max = B[len(B)-1]
a_next = B[len(B)-2]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

a_L = []
for val in range(n):
    a_L.append(int(input()))

s_L = sorted(a_L)

for val in a_L:
    if s_L[-1] != val:
        print(s_L[-1])
    else:
        print(s_L[-2])

Submission status: Accepted

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


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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

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

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)]
first = sorted(A)[-1]
second = sorted(A)[-2]
if first == second:
    for i in range(N):
        print(first)
else:
    idx = A.index(first)
    for i in range(N):
        if i == idx:
            print(second)
        else:
            print(first)

Submission status: Accepted

Solution code:
import heapq
from collections import defaultdict

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

dd = defaultdict(int)
for key in a:
    dd[key] += 1

heapq.heapify(minus_a)
max1=-heapq.heappop(minus_a)
max2=-heapq.heappop(minus_a)

for item in a:
  if item!=max1:
    print(max1)
  elif dd[max1]==1:
    print(max2)
  else:
    print(max1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
N = int(input())
A_list = [int(input()) for _ in range(N)]
sort_list = copy.copy(A_list)
sort_list.sort()
for i in range(N):
    if A_list[i] == sort_list[-1]:
        print(sort_list[-2])
    else:
        print(sort_list[-1])

Submission status: Accepted

Solution code:
import copy
n=int(input())
a=[int(input()) for i in range(n)]
maxa=max(a)
if len(set(a))!=1:
  maxa2=sorted(set(a))[-2]
maxs=a.count(maxa)
for i in range(n):
  if maxs==1:
    print(maxa if a[i]!=maxa else maxa2)
  else:
    print(maxa)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

biggest_num = 0
secondly_biggest_num=0
for i in range(n):
    a = int(input())
    suretsu0.append(a)

suretsu = [x for x in suretsu0]
suretsu.sort()


biggest_num = suretsu[-1]
secondly_biggest_num=suretsu[-2]




for ele in suretsu0:
    if ele==biggest_num:
        print(secondly_biggest_num)
    else:
        print(biggest_num)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
B=[]
for i in range(N):
  n=int(input())
  A.append(n)
  B.append(n)
B.sort()
for i in range(N):
  print(B[-1] if A[i]!=B[-1] else B[-2])