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())
a = [0] * n

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

#maxV = max(a)
sortedA = sorted(a)
maxV = sortedA[n-1]
maxV2 = sortedA[n-2]

for i in range(n):
    if a[i] == maxV:
        # new = list(a)
        # del new[i]
        # print(max(new))
        print(maxV2)
    else:
        print(maxV)

Submission status: Accepted

Solution code:
N = int(input())
A_list = [int(input()) for i in range(N)]
A_list_soretd = sorted(A_list)
result_max = A_list_soretd[-1]
result_max2 = A_list_soretd[-2]

for a in A_list:
    if result_max == a:
        print(result_max2)
    else:
        print(result_max)

Submission status: Accepted

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

D = int(input())
A = []
m1 = 0
m2 = 0
f = False
for i in range(D):
    tmp = int(input())
    if tmp > m1:
        m2 = m1
        m1 = tmp
        f = False
    elif tmp == m1: f = True
    elif tmp > m2:
        m2 = tmp
    A.append(tmp)

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

Submission status: Accepted

Solution code:
N = int(input())

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

a, b = sorted(A)[::-1][:2]

for A_i in A:
    if A_i == a:
        print(b)
    else:
        print(a)

Submission status: Accepted

Solution code:
n = int(input())
max_1 = 0
max_2 = 0
a = []
id_1 = 0
for i in range(n):
  a.append(int(input()))
  if a[i] > max_1:
    id_1 = i
    max_1 = a[i]
  elif a[i] > max_2:
    max_2 = a[i]
for i in range(n):
  if id_1 == i:
    print(max_2)
  else:
    print(max_1)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
maximum = max(A)
if A.count(maximum) != 1:
    for i in range(N):
        print(maximum)
else:
    B = A[:]
    B.remove(maximum)
    maximum_ne = max(B)
    for i in range(N):
        if A[i] == maximum:
            print(maximum_ne)
        else:
            print(maximum)

Submission status: Accepted

Solution code:
N = int(input())
A = [None for n in range(N)]
k = -1
ma1 = 0
ma2 = 0
for n in range(N):
	A[n] = int(input())
	if ma2 < A[n]:
		ma2 = A[n]
	if ma1 <= A[n]:
		ma2 = ma1
		ma1 = A[n]
		k = n
for n in range(N):
	if n == k:
		print(ma2)
	else:
		print(ma1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
import heapq

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
    

"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())

# 多次元配列の宣言（あとでintにすること。）（タプルにすること。）
dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')

# 複数配列を並び替え
ABT = zip(A, B, totAB)
result = 0
# itemgetterには何番目の配列をキーにしたいか渡します
sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)

# 2進数のbit判定
(x >> i) & 1

# dp最小化問題
dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break
# 累積和
add = 1 # 問題によって決まる
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)

#２分探索
li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    

#ソート関数
org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

#Distance Transformation
    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)

"""


N = int(input())
 
A = [0]*N
for n in range(N):
    A[n] = int(input())
    

    
def main():
    B = sorted(A, reverse = True)
    Amax = max(A)
    Amax2 = B[1]
        
    B = collections.Counter(A)
    N_Amax = B[Amax]
    for a in A:
        if a != Amax:
            print(Amax)
        else:
            if N_Amax == 1:
                print(Amax2)
            else:
                print(Amax)
    
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N= int(input())
num_list = []
max_num = 0
chk = 0
for i in range(N):
	k = int(input())
	max_num=max(k,max_num)
	num_list.append(k)
for i in range(N):
	if num_list[i] == max_num:
		chk += 1
if chk != 1:
	for i in range(N):
		print(max_num)
else:
	for i in range(N):
		if num_list[i] != max_num:
			print(max_num)
		else:
			chk_list = num_list[0:i] + num_list[i+1:N]
			chk_list.sort()
			print(chk_list[N-2])

Submission status: Accepted

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

A_list_cp = sorted(A_list)
max_1, max_2 = A_list_cp[-1], A_list_cp[-2]

for i in range(N):
    if A_list[i] == max_1:
        print(max_2)
    else:
        print(max_1)

Submission status: Accepted

Solution code:
N = int(input())
s = [int(input()) for i in range(N)]
exception = s.index(max(s))
s.sort()
s.reverse()
for i in range(N):
    if i==exception:
        print(s[1])
    else:
        print(s[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input()) 
A = [int(input()) for i in range(N)]
a = max(A)
b = np.argmax(A)
A[b] = 0
c = max(A)
for i in range(N):
  if i != b:
    print(a)
  else:
    print(c)

Submission status: Accepted

Solution code:
a=[int(input())for i in[0]*int(input())]
b=max(a)
if a.count(b)>1:print('\n'.join([str(b)]*len(a)))
else:
  c=[b for i in a]
  c[a.index(b)]=max(a[:a.index(b)]+a[a.index(b)+1:])
  print('\n'.join([str(i)for i in c]))

Submission status: Accepted

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

m1=max(A)
Imax=A.index(max(A))
b=A.pop(Imax)
m2=max(A)

M=[m1 for i in range(N)]
M[Imax]=m2

for i in range(N):
  print(M[i])

Submission status: Accepted

Solution code:
MAX = 2*10**5+5
def resolve():
    n = int(input())
    a = []
    leftmax = [0]*MAX
    rightmax = [0]*MAX
    for i in range(1,n+1):
        x=int(input())
        a.append(x)
        leftmax[i]=max(x,leftmax[i-1])
    for i in range(1,n+1):
        x = a[n-i]
        rightmax[n-i+1]=max(x,rightmax[n-i+2])

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
a = list()
for i in range(n):
    a.append(int(input()))
f = max(a)
fp = a.index(f)
b = list(a)
b.remove(f)
s = max(b)
for i in range (n):
    if i == fp:
        print(s)
    else :
        print(f)

Submission status: Accepted

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

a_sort = sorted(a, reverse=True)

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

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

Submission status: Accepted

Solution code:
import heapq as hq
def main():
    n = int(input())
    a = []
    l = []
    for i in range(n):
        num = int(input())
        a.append(num)
        l.append(-1*num)
    hq.heapify(l)
    max_1 = hq.heappop(l)
    max_2 = hq.heappop(l)
    for i in a:
        if -1*i == max_1:
            print(-1*max_2)
        else:
            print(-1*max_1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

max_all = max(A)

if A.count(max_all) >= 2:
    for _ in range(N):
        print(max_all)

else:
    max_index = A.index(max_all)
    A.pop(max_index)
    max_pop = max(A)
    for i in range(N):
        if max_index == i:
            print(max_pop)
        else:
            print(max_all)

Submission status: Accepted

Solution code:

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

forward = [0 for n in range(N)]
forward[0] = A[0]
backward = [0 for n in range(N)]
backward[N-1] = A[N-1]
for n in range(1,N):
    forward[n] = max(forward[n-1], A[n])
for n in range(N-1)[::-1]:
    backward[n] = max(backward[n+1], A[n])

# print(forward)
# print(backward)

print(backward[1])
for n in range(1,N-1):
    # print("====")
    # print(forward[n-1],backward[n+1])
    print(max(forward[n-1],backward[n+1]))
print(forward[N-2])

Submission status: Accepted

Solution code:
N = int(input())
As = []

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

Bs = sorted(As)
M = Bs[-1]
M2 = Bs[-2]

for i in range(N):
    if As[i] != M:
        print(M)
    else:
        print(M2)

Submission status: Accepted

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

b = a[:]
max_a = max(a)
b.remove(max_a)
next_max_a = max(b)

for m in a:
    if m == max_a:
        print(next_max_a)
    else:
        print(max_a)

Submission status: Accepted

Solution code:
from copy import copy
from collections import deque

N = int(input())

A = [None]*N
Adict = {}
for i in range(N):
    Ai = int(input())
    A[i] = Ai
    Adict[i] = Ai

Asorted = deque(sorted(A))
first = Asorted.pop()
second = Asorted.pop()

for i in range(N):
    Ai = Adict[i]
    Amax = first
    if Amax == Ai:
        Amax = second
    print(Amax)

Submission status: Accepted

Solution code:
def main():
    import sys
    import copy
    input = sys.stdin.readline
 
    n = int(input())
    a = [int(input()) for i in range(n)]
    b = []
    
    b = sorted(a)
    
    for i in a:
        if(i == b[-1]):
            print(b[-2])
        else:
            print(b[-1])
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
inps = []
for _ in range(n):
    inps.append(int(input()))
first, second = sorted(inps)[::-1][0:2]
for i in inps:
    if first != i:
        print(first)
    else:
        print(second)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
max_idx = A.index(max(A))
max_num = max(A)
second_max_num = sorted(A)[-2]

for i in range(max_idx):
  print(max_num)
print(second_max_num)
for i in range(max_idx+1, n):
  print(max_num)

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

# 2 * 10^5
N = int(stdin.readline().rstrip())
A = [int(stdin.readline().rstrip()) for _ in range(N)]

first_max = 0
second_max = 0
first_max_index = 0
second_max_index = 0

for i in range(N):
    if A[i] > first_max:
        first_max = A[i]
        first_max_index = i
        continue

    if A[i] > second_max:
        second_max = A[i]
        second_max_index = i
    

for i in range(N):
    if i == first_max_index:
        print(second_max)
    else:
        print(first_max)

Submission status: Accepted

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

max1 = max(a)
b = a[:]
b.remove(max1)
max2 = max(b)

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

Submission status: Accepted

Solution code:

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

m = [0, 0]
for i in range(N):
  if A[i] > m[0]:
    m[0] = A[i]
    m[1] = i  

m2 = 0
for i in range(N):
  if i == m[1]:
    continue
  m2 = max(A[i], m2)

res = []
for i in range(N):
  if i != m[1]:
    print(m[0])
  else:
    print(m2)

Submission status: Accepted

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

maxA = max(A)
max_in = A.index(max(A))
ANS = [maxA]*N
ANS[max_in] = max(A[:max_in]+A[max_in+1:])

for i in ANS:
    print(i)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
#print(a)
s = sorted(a)[::-1]
#print(s)
n1 = s[0]
n2 = s[1]
#n1 = nの最大値  n2 = 二番目
for i in a:
  if i == n1 and n2 != n1:
    print(n2)
  else:
    print(n1)

Submission status: Accepted

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

N = int(input())
next_max_num = 0
max_num = 0
max_idx = 0
for i in range(N):
    this_num = int(input())
    if max_num < this_num:
        next_max_num = max_num
        max_num = this_num
        max_idx = i
    elif max_num == this_num:
        next_max_num = max_num
        max_idx = -1
    elif next_max_num < this_num:
        next_max_num = this_num

if max_idx == -1:
    for i in range(N):
        print(max_num)
else:
    for i in range(N):
        if i == max_idx:
            print(next_max_num)
        else:
            print(max_num)

Submission status: Accepted

Solution code:
N = int(input())						# 5

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

max_a = max(A)

B=list(A)
B.sort(reverse=True)

if B[0]==B[1]:
	for a in A:
		print(max_a)

else:

	b=max_a
	for i in B:
		if i!=max_a:
			b=i
			break


	for a in A:
		if a==max_a:
			print(b)
		else:
			print(max_a)

Submission status: Accepted

Solution code:
N = int(raw_input())
A = [int(raw_input()) for i in xrange(N)]

MAX = 999999999999999999999999999999999999999999999999999999999999999999999999999

dp1 = [-MAX for i in xrange(N)]
for i in range(1, N):
    dp1[i] = max(dp1[i - 1], A[i - 1])

dp2 = [-MAX for i in xrange(N)]
for i in range(N - 2, -1, -1):
    dp2[i] = max(dp2[i + 1], A[i + 1])

for i in xrange(N):
    print max(dp1[i], dp2[i])

Submission status: Accepted

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)

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

    max_A = max(A_)
    if A_.count(max_A) > 1:
        for i in range(N):
            print(max_A)
            
    else:
        for i in range(N):
            if A_[i] == max_A:
                L = A_[0:i]
                L.extend(A_[i+1:])
                print(max(L))
            else:
                print(max_A)

                
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

N = int(input())


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

sorted = sorted(a)

max1 = sorted[-1]
max2 = sorted[-2]

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

Submission status: Accepted

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

m=max(a)
c=a.count(m)

if c > 1:
    for i in range(n):
        print m
else:
    s=sorted(a)[-2]
    for i in range(n):
        if a[i] != m:
            print m
        else:
            print s

Submission status: Accepted

Solution code:
n = int(input())
data = [int(input()) for i in range(n)]
base_max = max(data)
if data.count(base_max) > 1:
    for j in range(n):
        print(base_max)
else:
    tmp = data.index(base_max)
    for k in range(n):
        if k != tmp:
            print(base_max)
        else:
            data.sort()
            print(data[-2])

Submission status: Accepted

Solution code:
N = int(input())
a = []
for i in range(N):
    a.append(int(input()))
max_a = sorted(a,reverse=True)[:3]
if(max_a[0] > max_a[1]):
    for i in range(N):
        if(a[i] == max_a[0]):
            print(max_a[1])
        else:
            print(max_a[0])
else:
    for i in range(N):
        print(max_a[0])

Submission status: Accepted

Solution code:
# coding: utf-8
p = int(input())
lista = []

for i in range(p):
    lista.append(int(input()))
    
q = sorted(lista,reverse=True)

for i in lista:
    if i == q[0]:
        print(q[1])
    else:
        print(q[0])

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(N):
  a=int(input())
  A.append(a)
  
ans=max(A)
for i in range(N):
  if A[i]==ans:
    b=A[:i]
    c=A[i+1:]
    aa=b+c
    an=max(aa)
    if an==ans:
      for j in range(N-i):
        print(ans)
      break
    else:
      print(an)
  else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
B=[]
for i in range(N):
    B.append(A[i])
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:
import sys
sys.setrecursionlimit(100000)

def intinput(): return map(int,sys.stdin.readline().split())

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

Submission status: Accepted

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

ma = max(a)
count = a.count(ma)
if count >= 2:
    for i in range(n):
        print(ma)
else:
    for i in range(n):
        if a[i] == ma:
            b = a[:i]+a[i+1:]
            print(max(b))
        else:
            print(ma)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
li = []
for _ in range(N):
    r = int(input())
    li.append(r)
li_uniq = list(set(li))
li_uniq.sort(reverse = True)
if len(li_uniq) == 1:
    Max1 = li_uniq[0]
    Max2 = li_uniq[0]
else:
    Max1 = li_uniq[0]
    Max2 = li_uniq[1]
if li.count(Max1) > 1:
    for i in li:
        print(Max1)
else:
    for i in li:
        if i == Max1:
            print(Max2)
        else:
            print(Max1)

Submission status: Accepted

Solution code:
import math
input = raw_input

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

A.sort(key = lambda x: x[1])

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(0, n):
    a.append((int(input()), i))
a.sort()
a.reverse()
for i in range(0, n):
    num = 0
    while a[num][1] == i:
        num += 1
    print(a[num][0])

Submission status: Accepted

Solution code:
N = int(input())

As = []
for _ in range(N):
	As.append(int(input()))
	
#print(As)

Bs = sorted(As,reverse=True)

#print(Bs)

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

Submission status: Accepted

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

sortedA = sorted(a)
max = sortedA[-1]
max2 = sortedA[-2]
x = 0
maxIndex = []
for i in range(n):
    if(max == a[i]):
        x += 1
        maxIndex.append(i)

if(x > 1):
    for i in range(n):
        print(max)
else:
    for i in range(n):
        if(a[i] == max):
            print(max2)
        else:
            print(max)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = [int(input()) for i in range(N)]
A = np.array(A)

A_max = np.max(A)
A_argmax = np.argmax(A)

for i in range(N):
  if i == A_argmax:
    print(A[A.argsort()[::-1][1]])
  else:
    print(A_max)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

N = int(input())

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

an_sorted = sorted(an, reverse=True)
an_max = an_sorted[0]
an_max_second = an_sorted[1]

for i in range(N):
    if an[i] != an_max:
        print(an_max)
    else:
        print(an_max_second)

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
a = [int(sys.stdin.readline()) for _ in range(n)]

first = max(a)
if a.count(first) > 1:
    second = first
else:
    b = a[:]
    b.remove(first)
    second = max(b)

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in range(n)]
aa=list(set(a))
ma1=aa[-1]
aa1=a.count(aa[-1])
if aa1==1:
    for i in range(n):
        if a[i]==ma1:
            print(aa[-2])
        else:
            print(ma1)
else:
    for i in range(n):
        print(ma1)

Submission status: Accepted

Solution code:
n = int(input())

a = []
max_a = 0
next_a = 0
count_max_a = 0
for _ in range(n):
    v = int(input())
    a.append(v)
    if (max_a < v):
        next_a = max_a
        max_a = v
        count_max_a = 1
    elif (max_a == v):
        count_max_a += 1
    elif (next_a < v):
        next_a = v

for v in a:
    if v != max_a:
        print(max_a)
    else:
        if count_max_a == 1:
            print(next_a)
        else:
            print(max_a)

Submission status: Accepted

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

import sys

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

first=a[0]
second=a[1]
if first < second:
	tmp = first
	first = second
	second = tmp

for j in range(2,n):
	if(a[j]>first):
		second = first
		first = a[j]
	elif(a[j]>=second):
		second = a[j]

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(input()) for _ in range(n)]

lmax = [0 for _ in range(n)]
rmax = [0 for _ in range(n)]

tmp_lmax = 0
for i, e in enumerate(a):
    tmp_lmax = max(tmp_lmax, e)
    lmax[i] = tmp_lmax

tmp_rmax = 0
for i, e in enumerate(a[::-1]):
    tmp_rmax = max(tmp_rmax, e)
    rmax[n - 1 - i] = tmp_rmax

# 1st
print(rmax[1])
# mid
for i in range(1, n-1):
    print(max(lmax[i-1], rmax[i+1]))
# last
print(lmax[n-2])

Submission status: Accepted

Solution code:
n=int(input())
a=[]
b=[]
for i in range(n):
  c=int(input())
  a.append(c)
  b.append(c)
b.sort()
b.reverse()
for i in range(n):
  if a[i]<b[0]:
    print(b[0])
  elif a[i]>=b[0]:
    print(b[1])

Submission status: Accepted

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

max_i, MAX = max(a.items(), key=lambda x: x[1])
del a[max_i]

SECOND = max(a.values())


for i in range(n):
    if i == max_i:
        print(SECOND)
    else:
        print(MAX)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A_list = [int(input()) for _ in range(N)]
A_cnt = defaultdict(int)
for A in A_list:
    A_cnt[A] += 1
A_set = set(A_cnt)
max_A = max(A_set)
if (A_cnt[max_A] > 1):
    for _ in range(N):
        print(max_A)
else:
    A_set.remove(max_A)
    second_A = max(A_set)
    for A in A_list:
        if (A == max_A):
            print(second_A)
        else:
            print(max_A)

Submission status: Accepted

Solution code:
n = int(input())
l=[]
for i in range(n):
    l.append(int(input()))
#print(l)
a = l.index(max(l))
b = max(l)
l.pop(a)
c = max(l)
for i in range(n):
    if i != a:
        print(b)
    else:
        print(c)

Submission status: Accepted

Solution code:
N = int(input().strip())

x = (int(input().strip()), 0)
y = (int(input().strip()), 1)
if x[0] > y[0]:
  x, y = y, x

for i in range(2, N):
  a = int(input().strip())
  if a > x[0]:
    if a > y[0]:
      x = y
      y = (a, i)
    else:
      x = (a, i)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
no_1 = 0
pos_1 = 0
no_2 = 0
for i in range(0, N):
    num = int(input())
    
    if no_1 <= num:
        no_2 = no_1
        no_1 = num
        pos_1 = i
        
    elif no_2 < num:
        no_2 = num
    
for i in range (0, N):
    if i == pos_1:
        print(no_2)
    else:
        print(no_1)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = []
for i in range(N):
        a = int(input())
        A.append(a)
 
idx = np.argmax(np.array(A))
idx2 = np.argmax(np.array(A[:idx] + A[idx+1:]))
if idx2 >= idx:
        idx2 += 1

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
  a.append(int(input()))
amax = max(a)
amax_id = int(a.index(amax))
a.pop(amax_id)
amax2 = max(a)
for i in range(n):
  if i == amax_id:
    print(amax2)
  else:
    print(amax)

Submission status: Accepted

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

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

c = sorted(a,reverse = True)

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

Submission status: Accepted

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

m = max(a)
m_num = a.count(m)
if m_num > 1:
    for i in range(n):
        print(m)
else:
    ind_m = a.index(m)
    a.pop(ind_m)
    sec_m = max(a)
    for i in range(n):
        print(m) if i != ind_m else print(sec_m)

Submission status: Accepted

Solution code:
import numpy as np
import copy
  
N = int(input())  
A = np.zeros(N)

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

ans = int(np.max(A))
K = np.where(A == ans)[0]
k = np.where(A == ans)[0][0]

if K.size > 1:
    for i in range(N):
        print(ans)
        
else:
    for i in range(N):
        if i == k:
            B = np.delete(A, i)
            print(int(np.max(B)))
    
        else:
            print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = []
for i in range(n):
	l.append(int(input()))
maxnum = max(l)
maxidx = l.index(maxnum)

for i,e in enumerate(l):
	if i == maxidx:
		lwomax = l[:]
		lwomax.pop(maxidx)
		print(max(lwomax))
	else:
		print(maxnum)

Submission status: Accepted

Solution code:


def solve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    Amax = max(A)
    A2 = A.copy()
    A2.remove(Amax)
    Asecond = max(A2)
    for a in A:
        if a == Amax:
            print(Asecond)
        else:
            print(Amax)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np


def main():
	#N, D = [int(x) for x in input().split()]
	N = int(input())
	
	max_num = 0
	sub_max = 0
	index = 0
	for i in range(N):
		num = int(input())
		if num > max_num:
			index = i
			sub_max = max_num
			max_num = num
		elif num > sub_max:
			sub_max = num

	for i in range(N):
		if i == index:
			print(sub_max)
		else:
			print(max_num)

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
n = int(input())

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

saidai1=max(a_list)
saidai1_index=a_list.index(saidai1)
saidai2=max(a_list[:saidai1_index]+a_list[saidai1_index+1:])

for i in range(n):
  if a_list[i]==saidai1:
    print(saidai2)
  else:
    print(saidai1)

Submission status: Accepted

Solution code:
import sys
sysinput = sys.stdin.readline

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

A_sort = sorted(A)
ans_list =[]
for A_i in A:
  if A_i == A_sort[-1]:
    ans_list.append(A_sort[-2])
  else:
    ans_list.append(A_sort[-1])

for ans in ans_list:
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
List = []

for i in range(1,N+1):
  List.append(int(input()))
list1 = List.copy()
List.sort(reverse=True)
MAX = List[0]
SEC_MAX = List[1]
for i in list1:
  if i != MAX:
    print(MAX)
  else:
    print(SEC_MAX)