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:
N = int(input())
max_1st = -1
max_1st_index = -1
max_2nd = -1
only_max = False
for i in range(0, N):
  if i == 0:
    max_1st = int(input())
    max_1st_index = 0
  elif i == 1:
    tmp = int(input())
    if (tmp > max_1st):
      max_1st, max_2nd = tmp, max_1st
      max_1st_index = 1
    else:
      max_2nd = tmp
  else:
    tmp = int(input())
    if (max_2nd < tmp) and (tmp < max_1st):
      max_2nd = tmp
    elif (max_1st < tmp):
      max_1st, max_1st_index = tmp, i
      only_max = False
    elif (max_1st == tmp):
      only_max = True

for j in range(0, N):
  if only_max:
    print(max_1st)
  else:
    if j == max_1st_index:
      print(max_2nd)
    else:
      print(max_1st)

Submission status: Accepted

Solution code:
N = int(input())
f = (-1, -1)
s = (-1, -1)
for n in range(N):
    A = int(input())
    if f[1] <= A:
        s = f
        f = (n, A)
    elif s[1] <= A:
        s = (n, A)
for n in range(N):
    print(f[1] if n != f[0] else s[1])

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

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

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

#Cのときみたいにforループ回してmax_1とmax_2を求めてもいいけど、sortしちゃうと短くかける

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect

MOD = 10**9+7

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

max_1 = 0
max_2 = 0
max_index = -1
for i in range(N):
    if A[i] > max_1:
        max_1 = A[i]
        max_index = i
    elif A[i] > max_2:
        max_2 = A[i]

for i in range(N):
    if max_1 == max_2:
        print(max_1)
    else:
        if i == max_index:
            print(max_2)
        else:
            print(max_1)

Submission status: Accepted

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

max1id,max1val=max(a,key=a.get),max(a.values())
a.pop(max1id)
max2id,max2val=max(a,key=a.get),max(a.values())
a[max1id]=max2val
for i in range(n):
  if i==max1id:
    print(max2val)
  else:
    print(max1val)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

max1 = max1ad = max2 = 0
for i in range(n):
    a = int(input())
    if a > max1:
        max2 = max1
        max1 = a
        max1ad = i
    elif a > max2:
        max2 = a

for i in range(n):
    if i == max1ad:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max_1 = max(a)
b = sorted(a, reverse=True)
a_max_2 = b[1]

for i in range(n):
    if a[i] == a_max_1:
        print(a_max_2)
    else:
        print(a_max_1)

Submission status: Accepted

Solution code:
a=int(input())
b=[int(input()) for i in range(a)]
m=max(b) 

if b.count(m)>=2:
    for i in range(a):
        print (m)
else:
    c=b.copy()
    c.sort(reverse=True)
    m2=c[1]
    for i in range(a):
        if b[i]==m:
            print(m2)
        else:
            print (m)

Submission status: Accepted

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

Submission status: Accepted

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

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

ma = max(a)
max_i = a.index(ma)
b = []

for j in range(n):
    if j != max_i:
        b.append(a[j])

mb = max(b)

for i in range(n):
    if i != max_i:
        print(ma)
    else:
        print(mb)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    aaa = [int(input()) for _ in range(n)]
    sorta = sorted(aaa, reverse=True)
    maxa = sorta[0]
    seconda = sorta[1]
    for i in range(n):
        if maxa == aaa[i]:
            print(seconda)
        else:
            print(maxa)
resolve()

Submission status: Accepted

Solution code:
def main():

    N, *A = map(int, open(0).read().split())
    first, second = sorted(A, reverse=True)[:2]
    print("\n".join(map(str, (first if a != first else second for a in A))))
    return


main()

Submission status: Accepted

Solution code:
def main():

    N, *A = map(int, open(0).read().split())
    first, second = sorted(A, reverse=True)[:2]
    print("\n".join(map(str, [first if a != first else second for a in A])))
    return


main()

Submission status: Accepted

Solution code:
def main():

    *A, = map(int, open(0).read().split()[1:])
    first, second = sorted(A, reverse=True)[:2]
    print("\n".join(map(str, [first if a != first else second for a in A])))
    return


main()

Submission status: Accepted

Solution code:
def main():

    *A, = map(int, open(0).read().split()[1:])
    first, second = sorted(A, reverse=True)[:2]
    print("\n".join(map(str, (first if a != first else second for a in A))))
    return


main()

Submission status: Accepted

Solution code:
def main():

    *A, = map(int, open(0).read().split()[1:])
    first, second = sorted(A)[:-3:-1]
    print("\n".join(map(str, (first if a != first else second for a in A))))
    return


main()

Submission status: Accepted

Solution code:
def main():

    *A, = map(int, open(0).read().split()[1:])
    first, second = sorted(A)[:-3:-1]
    ans = [str(first) if a != first else str(second) for a in A]
    print("\n".join(ans))
    return


main()

Submission status: Accepted

Solution code:
def main():

    *A, = map(int, open(0).read().split()[1:])
    first, second = sorted(A)[:-3:-1]
    ans = (str(first) if a != first else str(second) for a in A)
    print("\n".join(ans))
    return


main()

Submission status: Accepted

Solution code:
def main():

    *A, = map(int, open(0).read().split()[1:])
    first, second = sorted(A)[:-3:-1]
    print("\n".join(str(first) if a != first else str(second) for a in A))
    return


main()

Submission status: Accepted

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

a_max = max(a_list)
a_max_list = [i for i, v in enumerate(a_list) if v == a_max]

if len(a_max_list) == 1:
  a_max_2 = sorted(set(a_list))[-2]
  for i in range(n):
    if i in a_max_list:
      print(a_max_2)
    else:
      print(a_max)
else:
  for i in range(n):
    print(a_max)

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]:
    print(b[n-2])
  else:
    print(b[n-1])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = [int(input()) for _ in range(N)]
A_max = max(A)
A_max2 = sorted(A)[-2]
for a in A:
    if a==A_max:
        print(A_max2)
    else:
        print(A_max)

Submission status: Accepted

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

Submission status: Accepted

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

amax,b=max(a),a.index(max(a))
a.remove(amax)
bmax=max(a)
for i in range(n):
  if i==b:
    print(bmax)
  else:
    print(amax)

Submission status: Accepted

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

cA = list(Counter(A).items())

cA.sort(reverse=True)

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

Submission status: Accepted

Solution code:
n=int(input())
z=[int(input()) for i in range(n)]
l=sorted(z)
a=l[-1];s=l[-2]
for d in z:
  print(a if d!=a else s)

Submission status: Accepted

Solution code:
n = int(input())
rank1 = 0
rank2 = 0
ax = []
for i in range(n) :
  a = int(input())
  if rank1 <= a : 
    rank2 = rank1 
    rank1 = a
  elif rank2 < a :
    rank2 = a
  ax.append(a)
    

for i in range(n) :
  if ax[i] == rank1 :
    print(rank2)
  else :
    print(rank1)

Submission status: Accepted

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

max1 = max(array)  
max1count = array.count(max1)

array2 = array[:]  
array2.remove(max1)
max2 = max(array2)

for e in array:
    if e != max1:
        print(max1)
    else:
        if 1 < max1count:
            print(max1)
        else:
            print(max2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
s=int(input())
list1=[input() for _ in range(s)]
list2=copy.copy(list1)
list2=sorted(list2,key=lambda x:-int(x))
for i in list1:
    if list2[0]==i:
      print(list2[1])
    else:
      print(list2[0])

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

left_i = [0] * N
right_i = [0] * N

for i in range(1, N):
    left_i[i] = max(left_i[i-1], A[i-1])
    right_i[i] = max(right_i[i-1], A[N-i])

for i in range(N):
    print(max(left_i[i], right_i[N-1-i]))

Submission status: Accepted

Solution code:
n = int(input())

a = [0] * n

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

a_sorted = sorted(a, reverse=True)
# print(a_sorted)
ans = [0] * n
no1 = a_sorted[0]
no2 = a_sorted[1]

for i in range(n):
  if a[i] == no1:
    ans[i] = no2
  else:
    ans[i] = no1
  
for i in range(n):
  print(ans[i])

Submission status: Accepted

Solution code:
N=int(input())
A=[]
MAX=0
for _ in range(N):
    A.append(int(input()))
    if MAX<A[-1]:
        MAX=A[-1]
count=0
for i in range(N):
    if A[i]==MAX:
        count+=1
for j in range(N):
    if A[j]==MAX and count==1:
        A[j]=0
        print(max(A))
    else:
        print(MAX)

Submission status: Accepted

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

MAX_1 = max(A)
MAX_2 = sorted(A)[-2]

for i in range(N):
    if A[i] == MAX_1:
        print(MAX_2)
    else:
        print(MAX_1)

Submission status: Accepted

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

max_value = max(A)
max_index = A.index(max_value)

for i in range(N):
  if i != max_index:
    print(max_value)
  else:
    A[i] = 0
    print(max(A))

Submission status: Accepted

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

R = sorted(L)
a = R[-2]
b = R[-1]

for i in L:
  if i == b:
    print(a)
  else:
    print(b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import copy
# 最大値が2つ以上あるものと1つしかないもので場合分けをする
n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
if b[-1] == b[-2]:
  for i in range(n):
    print(b[-1])
else:
  p = a.index(b[-1])
  for i in range(n):
    if i == p:
      print(b[-2])
    else:
      print(b[-1])
#deepcopy is so slow!!
# ex_a = []
# for i in range(n):
#   ex_a = copy.deepcopy(a)
#   ex_a[i] = -1
#   print(max(ex_a))

Submission status: Accepted

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

Submission status: Accepted

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

mx = max(lst)
clst = sorted(lst)
mx2 = clst[-2]

for j in range(N):
    if mx != lst[j]:
        print(mx)
    else:
        print(mx2)

Submission status: Accepted

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

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())
A=[]
for i in range(N):
  A +=[int(input())]
B=sorted(A)
for i in range(N):
  if A[i]==B[-1]:
    print(B[-2])
  else:
    print(B[-1])

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
max1=0
max2=0
a=[int(input()) for i in range(n)]
for i in a:
    if i>=max1:
        max2=max1
        max1=i
    elif i>=max2:
        max2=i
if max2==0:
    for i in range(n):
        print(max1)
    exit()
for i in a:
    if i==max1:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
maxind = A.index(max(A))
maxa = max(A)
A = sorted(A)
ans = [maxa]*maxind
ans.append(A[-2])
[ans.append(i) for i in [maxa]*(N-maxind-1)]
print(*ans,sep="\n")

Submission status: Accepted

Solution code:
n = int(input()) #1行目のNを取得する
list = [input() for i in range(n)] #複数行の数値の入力を取得
lint = [int(s) for s in list]

ans = max(lint)
index = lint.index(max(lint))
lint.remove(max(lint))
ans2 = max(lint)

if ans == ans2:
    for i in range(n):
        print(ans)
else:
    for i in range(0, index):
        print(ans)
    print(ans2)
    for i in range(index+1, n):
        print(ans)

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:
# 2019-11-15 14:12:24(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *a = [int(x) for x in sys.stdin.read().split()]
    b = a.copy()
    b.sort()
    largest = b[-1]
    second_largest = None
    if b[-2] == largest:
        pass
    else:
        second_largest = b[-2]
    
    if not second_largest:
        for i in range(n):
            print(largest)
    else:
        for i in range(n):
            if a[i] == largest:
                print(second_largest)
            else:
                print(largest)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


def main():
    sc = Scan()
    n = sc.intarr()[0]
    array = []
    for i in range(n):
        array.append(sc.intarr()[0])
    sorted_array = sorted(array)
    maximum = sorted_array[-1]

    for i in array:
        if i == maximum:
            print(sorted_array[-2])
        else:
            print(maximum)


class Scan():

    def intarr(self):
        line = input()
        array = line.split(' ')
        num_array = [int(n) for n in array]
        return num_array

    def strarr(self):
        line = input()
        array = line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


main()

Submission status: Accepted

Solution code:
from collections import defaultdict,deque, Counter
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = inp()
A = np.array([inp() for _ in range(n)])
mA = max(A)
smA = np.sort(A)[::-1][1]
for i in range(n):
    if A[i] == mA: print(smA)
    else: print(mA)

Submission status: Accepted

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

N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
first = B[-1]
second = B[-2]

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

Submission status: Accepted

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

a.sort(reverse=True)

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

Submission status: Accepted

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

tmp = copy.deepcopy(A)
tmp = list(sorted(A, reverse=True))

Amax = tmp[0]
Asecond = tmp[1]

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
def print_ans(N, ar):
    """Test Case
    >>> print_ans(3, [1, 4, 3])
    4
    3
    4
    >>> print_ans(2, [5, 5])
    5
    5
    """
    ar = np.array(ar)
    ar_idx = np.argsort(ar)

    for i in range(N):
        for k in reversed(range(N)):
            if i != ar_idx[k]:
                print(ar[ar_idx[k]])
                break

if __name__ == '__main__':
    N = int(input().rstrip())
    ar = [int(input().rstrip()) for i in range(N)]
    print_ans(N, ar)

Submission status: Accepted

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

ans1=max(array)
answer=[ans1]*n

for i in range(n):
  if array[i]==ans1:
    array.pop(i)
    ans2=max(array)
    answer[i]=ans2
    break
for i in range(n):
  print(answer[i])

Submission status: Accepted

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

lenA=len(A)
Amax=max(A)
ind=A.index(Amax)
del A[ind]
Amax2=max(A)

AA=np.repeat(Amax,lenA, axis=0)
AA[ind]=Amax2
for x in AA:
  print(x)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
for i in range(n):
  t=int(input())
  a.append(t)
t=max(a)
b=sorted(a,reverse=True)
for i in a:
  if i==t:
    k=b[1]
  else:
    k=t
  print(k)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#%%
import sys
def input():
    return sys.stdin.readline().rstrip()

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

    B=sorted(A, reverse=True)
    for i, a in enumerate(A):
        if a == B[0]:
            print(B[1])
        else:
            print(B[0])


# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc134","c",main)

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
b=collections.Counter(a)
d=list(b.items())
d.sort(key=lambda x:-int(x[0]))
if d[0][1]==1:
  for i in range(n):
    if a[i]==int(d[0][0]):
      print(int(d[1][0]))
    else:
      print(int(d[0][0]))
else:
  for i in range(n):
    print(int(d[0][0]))

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
    
    
import copy
U=copy.deepcopy(A)
del U[U.index(max(U))]
X=max(U)
Q=max(A)
Z=A.index(Q)
for i in range(N):
    if i==Z:
        print(X)
    else:
        print(Q)

Submission status: Accepted

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

Submission status: Accepted

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

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

b.sort()

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

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for _ in range(n)]
tmp = sorted(A)
maxa = max(A)
for i in range(n):
    if A[i] == maxa:
        print(tmp[-2])
    else:
        print(maxa)

Submission status: Accepted

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

m=max(l)


if l.count(m)>1:
    for i in range(n):
        print(m)

else:
    for i in range(n):
        if l[i]==m:
            l.pop(i)
            print(max(l))
            l.append(0)
        else:
            print(m)

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
#H = list(map(int, input().split()))
s = [int(input()) for i in range(N)]
#print(s)
max=0
a=0
for i in range(N):
  if s[i]>max:
    max=s[i]
    a=i

for i in range(N):
  if i==a:
    s.sort()
    s.reverse()
    print(s[1])
  else:
    print(max)

Submission status: Accepted

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

max_val = max(li)
sec_val = sorted(li)[-2]
ans = []

for j in li:
    if j == max_val:
        ans.append(sec_val)
    else:
        ans.append(max_val)

for k in ans:
    print(k)

Submission status: Accepted

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

amax = a1[n-1]
amax2 = a1[n-2]

for i in a:
    if i == amax:
        print(amax2)
    else:
        print(amax)

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

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = [0] * N
for i in range(N):
    A.append(int(input()))
max = max(A)
B = sorted(A, reverse = True)
for i in range(N):
    if A[i] != max:
        print(max)
    else:
        print(B[1])

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = [0] * N
for i in range(N):
    A.append(int(input()))
max = max(A)
B = sorted(A, reverse = True)
count = A.count(max)
if count > 1:
    for i in range(N):
        print(max)
else:
    for i in range(N):
        if A[i] != max:
            print(max)
        else:
            print(B[1])

Submission status: Accepted

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

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

m1 = max(a)
m2 = sorted(a)[-2]
  
for j in range(n):
  if a[j] == m1:
    print(m2)
  else:
    print(m1)

Submission status: Accepted

Solution code:
n = int(input())
import copy
list = []
for i in range(n):
    k = int(input())
    list.append(k)
list1 = copy.deepcopy(list)
first = max(list1)
list1.remove(first)
second = max(list1)
for li in list:
    if li == first:
        print(second)
    else:
        print(first)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=[int(input()) for _ in range(n)]
    Left=[0]*n
    Left[0]=A[0]
    Right=[0]*n
    Right[-1]=A[-1]

    for i in range(1,n):
        Left[i]=max(A[i],Left[i-1])
    for i in range(n-2,-1,-1):
        Right[i]=max(A[i],Right[i+1])

    for i in range(n):
        if(i==0):
            print(Right[1])
        elif(i==n-1):
            print(Left[n-2])
        else:
            print(max(Left[i-1],Right[i+1]))
resolve()

Submission status: Accepted

Solution code:
N = int(input())

A = []

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

A_sorted = sorted(A, reverse=True)
A_max1 = A_sorted[0]
A_max2 = A_sorted[1]

for a in A:
    if a == A_max1:print(A_max2)
    else:print(A_max1)

Submission status: Accepted

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

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

B = sorted(A,reverse=True)

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

Submission status: Accepted

Solution code:
N = int( input() )
c_list = []
for i in range(N):
    c_list.append( int( input() ) )
    
a = c_list[:]
a.sort()
ma = a[ N - 1 ]

for i in range(N):
    t = c_list[i]
    if t == ma:
        print(a[N - 2])
    else:
        print(ma)

Submission status: Accepted

Solution code:
n = int(input())
num_list = []
max_num = 0
max_num2 = 0

for x in range(n):
    i = int(input())
    if max_num < i:
        max_num2 = max_num
        max_num = i
    elif max_num2 < i:
        max_num2 = i
    num_list.append(i)

for num in num_list:
    if max_num != num:
        print(max_num)
    else:
        print(max_num2)

Submission status: Accepted

Solution code:
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([1, 4, 3]), [4, 3, 4])

    def test_2(self):
        self.assertEqual(think([5, 5]), [5, 5])


def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    a = []
    for _ in range(n):
        a.append(read_int(1)[0])
    return a


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    hist = make_histogram(a)

    list_form_of_hist = []
    for k, v in hist.items():
        list_form_of_hist.append([k, v])
    list_form_of_hist.sort(key=lambda x: -x[0])

    result = []
    for e in a:
        if e == list_form_of_hist[0][0]:
            if list_form_of_hist[0][1] == 1:
                result.append(list_form_of_hist[1][0])
            else:
                result.append(list_form_of_hist[0][0])
        else:
            result.append(list_form_of_hist[0][0])
    return result


def write(result):
    for e in result:
        print(e)


def make_histogram(a):
    hist = {}
    for e in a:
        if e in hist:
            hist[e] += 1
        else:
            hist[e] = 1
    return hist


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n = int(input())
max_1 = 0
max_2 = 0
al = []
for _ in range(n):
    a = int(input())
    al.append(a)

max_1 = sorted(al)[-1]
max_2 = sorted(al)[-2]

for a in al:
    if a == max_1:
        print(max_2)
    else:
        print(max_1)

Submission status: Accepted

Solution code:
n=int(input())

L=[]
for i in range(n):
      L.append(int(input()))
L2=sorted(L)
for i in range(n):
      if L[i]==L2[-1]:
            print(L2[-2])
      else:
            print(L2[-1])

Submission status: Accepted

Solution code:
import heapq
import copy

n = int(input().strip())

a = []

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

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

first = heapq.heappop(b)
second = heapq.heappop(b)

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

Submission status: Accepted

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

sorted_A_list = sorted(A_list)
n1 = sorted_A_list[-1]
n2 = sorted_A_list[-2]

for i in range(N):
    if A_list[i] == n1:
        print(n2)
    else:
        print(n1)

Submission status: Accepted

Solution code:
A=[int(input()) for i in range(int(input()))]
B=sorted(A)
one=B[-1]
two=B[-2]
for i in A:print(one if i!=one else two)

Submission status: Accepted

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

one=sorted(A)[-1]
two=sorted(A)[-2]

for i in A:
    print(one if i!=one else two)

Submission status: Accepted

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

for j in range(0,n):
  a.append(int(input()))
  
x=max(a)
y=a.index(x)
del a[y]
z=max(a)

for i in range(0,n):
  if i!=y:
    print(x)
  else:
    print(z)

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
  dmy = int(input())
  a.append(dmy)
  
a_max = a.index(max(a))
a.sort()
for i in range(n):
  if i==a_max:
    print("{}".format(a[-2]))
  else:
    print("{}".format(a[-1]))

Submission status: Accepted

Solution code:
n = int(input())
an = []
max_1 = 1
max_2 = 1
count = 1
max1_index = [0]
for i in range(n):
    a = int(input())
    if a > max_1:
        tmp = max_1
        max_1 = a
        count = 1
        max1_index = [i]
        max_2 = tmp
    elif a == max_1:
        count += 1
        max1_index.append(i)
    elif max_1 >  a > max_2:
        max_2 = a
    an.append(a)
if count == 1:
    for i in range(n):
        if i in max1_index:
            print(max_2)
        else:
            print(max_1)
else:
    for i in range(n):
        print(max_1)

Submission status: Accepted

Solution code:
n = int(input())
ai = {}
max1 = 0
max2 = 0
for i in range(1, n+1):
    tmp = int(input())
    ai.setdefault(i, tmp)
    if tmp > max1:
        max2 = max1
        max1 = tmp
    elif tmp > max2:
        max2 = tmp

for i in range(1, n+1):
    if ai[i] == max1:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
n = int(input())
ai = {}
max1 = 0
max2 = 0
for i in range(1, n+1):
    tmp = int(input())
    ai.setdefault(i, tmp)
    if tmp > max1:
        max1 = tmp
    elif tmp > max2:
        max2 = tmp

for i in range(1, n+1):
    if ai[i] == max1:
        print(max2)
    else:
        print(max1)

Submission status: Accepted

Solution code:
N = int(input())

# 数列Aを格納
A = [int(input()) for _ in range(N)]

# 数列Aの最大の値、個数、位置を取得
a_max = max(A)
a_max_count = A.count(a_max)
a_max_index = A.index(a_max)

# 最大値が1つの場合、最大値の箇所は2番目に大きい値
if a_max_count == 1:
    # 最大値が1つしかない場合、2番目に大きい値を取得
    A.remove(a_max)
    a_second_max = max(A)

    for i in range(N):
        if i == a_max_index:
            print(a_second_max)
        else:
            print(a_max)
# 最大値が2つ以上の場合、常に最大値が解
else:
    for _ in range(N):
        print(a_max)