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 = [int(input()) for _ in range(n)]
h = sorted(a)
b = h[-1]
c = h[-2]
for i in range(n):
    if a[i] != b:
        print(b)
    else:
        print(c)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
a = sorted(set(A))
max_count = A.count(a[-1])

for i in range(N):
  if len(a) > 1:
    if A[i] != a[-1]:
      print(a[-1])
    else:
      if max_count > 1:
        print(a[-1])
      else:
        print(a[-2])
  else:
    print(a[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
N=int(input())
A=[int(input()) for _ in range(N)]
l=[]
for i in range(N):
    l.append([A[i],i])
l.sort(key=lambda x:x[0],reverse=True)
#print(l)
excep=l[0][1]

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())

    max_left = [0] * N
    max_right = [0] * N

    ary = []

    for i in range(N):
        v = int(input())
        ary.append(v)

    for i in range(N - 1):
        max_left[i+1] = max(max_left[i], ary[i])

    for i in range(N - 1, 0, -1):
        max_right[i-1] = max(max_right[i], ary[i])

    for i in range(N):
        v = max(max_left[i], max_right[i])
        print(v)


main()

Submission status: Accepted

Solution code:
import sys
import itertools
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())
Anp = np.array(A)

L = np.maximum.accumulate(Anp)
R = np.maximum.accumulate(Anp[::-1])[::-1]

LL = [0] + L.tolist()[:-1]
RR = R.tolist()[1:] + [0]

for x, y in zip(LL, RR):
    print(x if x > y else y)

Submission status: Accepted

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

# print(A)
sorted_A = sorted(A)
max_A = sorted_A[-1]
second_max_A = sorted_A[-2]

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

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 not i == max_index:
        print(max_value)
    else:
        newlist = a[:i] + a[i+1:]
        print(max(newlist))

Submission status: Accepted

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

max1 = 0
max2 = 0
for i in range (N):
    if A[i] >= max1:
        max2 = max1
        max1 = A[i]
    elif A[i] >= max2:
        max2 = A[i]

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

Submission status: Accepted

Solution code:
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())
ma = max(A)
num = A.index(ma)
A.remove(ma)
ma2 = max(A)
A.insert(num, ma)
if ma == ma2:
    for _ in range(N):
        print(ma)
else:
    for i in range(N):
        if i == num:
            print(ma2)
        else:
            print(ma)

Submission status: Accepted

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

Submission status: Accepted

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

a_s = sorted(a, reverse=True)
for i in range(n):
    if a[i] == a_s[0]:
        print(a_s[1])
    else:
        print(a_s[0])

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
AL = [A[0]]*N
AR = [A[-1]]*N
for i in range(1,N):
    AL[i] = max(AL[i-1], A[i])
    AR[N-i-1] = max(A[N-i-1], AR[N-i])
INF = float("inf")
AL = [-INF]+AL+[-INF]
AR = [-INF]+AR+[-INF]
for i in range(N):
    print(max(AL[i],AR[i+2]))

Submission status: Accepted

Solution code:
N = int(input())
A = []
for _ in range(N):
  A += [int(input())]
Amax = max(A)
Amax2 = sorted(A, reverse=True)[1]
for i in A:
  print(Amax if i != Amax else Amax2)

Submission status: Accepted

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

Asort = sorted(A,reverse=True)
first = max(Asort)
second = max(Asort[1:N])

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = []
for i in range(N):
    A = int(input())
    A_list.append(A)
sort_A = sorted(A_list, reverse = True)
max_f = sort_A[0]
max_s = sort_A[1]
for i in range(N):
    print(max_s) if A_list[i] == max_f else print(max_f)

Submission status: Accepted

Solution code:
import copy

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

cd = copy.copy(ab)

cd.sort()

for i in ab:
    if i == cd[-1]:
        print(cd[-2])
    else:
        print(cd[-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

max_index=0
_max=max(a)
s=0

for i in range(n):
    if a[i]==_max and s<1:
        s+=1
        max_index=i
    elif a[i]==_max and s>=1:
        s+=1
        break

if s==1:
    for i in range(n):
        if i==max_index:
            print(max(a[:max_index]+a[max_index+1:]))
        else:
            print(_max)
else:
    for i in range(n):
        print(_max)

Submission status: Accepted

Solution code:
N = int(input())
A = []
first = 0
second = 0
for _ in range(N):
    t = int(input())
    if t > first:
        second = first
        first = t
    elif t > second:
        second = t
    A.append(t)

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

Submission status: Accepted

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

A_asc = sorted(A)
max1 = A_asc[-1]
max2 = A_asc[-2]

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

Submission status: Accepted

Solution code:
import numpy as np

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

A_asc = np.sort(A)
max1 = A_asc[-1]
max2 = A_asc[-2]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = {}
AA = []
for i in range(N):
    a = int(input())
    if a not in A.keys():
        A[a] = 1
    else:
        A[a] += 1
    AA.append(a)
key = list(A.keys())
key.sort(reverse=True)
ans = []
for i in range(N):
    if AA[i] != key[0]:
        print(key[0])
    else:
        if A[key[0]] < 2:
            print(key[1])
        else:
            print(key[0])

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = {}
AA = []
for i in range(N):
    a = int(input())
    if a not in A.keys():
        A[a] = 1
    else:
        A[a] += 1
    AA.append(a)
key = list(A.keys())
key.sort(reverse=True)
ans = []
for i in range(N):
    if AA[i] != key[0]:
        ans.append(key[0])
    else:
        if A[key[0]] < 2:
            ans.append(key[1])
        else:
            ans.append(key[0])
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
def main():
  
  n,*l=(map(int,open(0).read().split()))
  m=sorted(list(l))
  a,b=m[-1],m[-2]
  s=""
  for x in l:
    if x!=a:
      s+=str(a)+"\n"
    else:
      s+=str(b)+"\n"
  print(s)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
# coding:utf-8

itemCount = int(input().rstrip())
items = [int(input().rstrip()) for i in range(itemCount)]
max_items = [i for i in reversed(sorted(items))][0:2]

for i in items:
    if i == max_items[0]:
        print(max_items[1])
    else:
        print(max_items[0])

Submission status: Accepted

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

Submission status: Accepted

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

new_lst = list(set(a_lst))
new_lst.sort()

if len(new_lst)==1:
    for _ in range(n):
        print(new_lst[-1])

else:
    maximum = new_lst[-1]
    semi_maximum = new_lst[-2]
    count = a_lst.count(maximum)

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

        if a==maximum:
            if count==1:
                print(semi_maximum)
            elif count>1:
                print(maximum)
        elif a<maximum:
            print(maximum)

Submission status: Accepted

Solution code:
N=int(input())
a=[]
b=[]
for _ in range(N):
  a_=int(input())
  a.append(a_)
  b.append(a_)
x=max(a)
b.sort()
if a.count(x)>=2:
  for i in range(N):
    print(x)
else:
  for i in range(N):
    if a[i]==x:
      print(b[-2])
    else:print(x)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
M = max(A)
c = A.count(M)
a = list(set(A))
a.sort(reverse=True)
 
for i in range(N):
  if A[i] != M:
    print(M)
  else:
    if c >= 2:
      print(M)
    else:
      print(a[1])

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = [INT() for _ in range(N)]

index = A.index(max(A))
A_sort = sorted(A)

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

Submission status: Accepted

Solution code:
import sys
import numpy as np 

n = int(sys.stdin.readline().rstrip())
a = np.array(sys.stdin.read().split(), dtype=np.int64)

def main():
    b = np.sort(a)
    m1 = b[-1]
    m2 = b[-2]
    res = np.zeros(n, dtype=np.int64)
    res[a <= m2] = m1
    res[res == 0] = m2
    return res
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
import sys
import numpy as np 

n = int(sys.stdin.readline().rstrip())
a = np.array(sys.stdin.read().split(), dtype=np.int64)

def main():
    b = np.sort(a)
    m1 = b[-1]
    m2 = b[-2]
    res = np.zeros(n, dtype=np.int64)
    res[a <= m2] = m1
    res[a == m1] = m2
    return res
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
import sys

n, *a = map(int, sys.stdin.read().split())

def main():
    b = sorted(a.copy())
    m1 = b[-1]
    m2 = b[-2]
    if m1 == m2:
        res = [m1] * n
    else:
        res = [None] * n
        for i in range(n):
            if a[i] < m1:
                res[i] = m1
            else:
                res[i] = m2
    return res
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

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


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

Submission status: Accepted

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

inx=a.index(max(a))
a_s=sorted(a)

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

Submission status: Accepted

Solution code:
N = int(input())

m = -float("inf")
m2 = -float("inf")
max_idx = None
for i in range(N):
    el = int(input())    
    if (el > m):
        m2 = m
        m = el
        max_idx = i
    elif (el > m2):
        m2 = el


for i in range(max_idx):
    print(m)

print(m2)

for i in range(N-max_idx-1):
    print(m)

Submission status: Accepted

Solution code:
N = int(input())
l = [None] * N
m = -float("inf")
m2 = -float("inf")
for i in range(N):
    l[i] = int(input())
    
    if (l[i] > m):
        m2 = m
        m = l[i]
    elif (l[i] > m2):
        m2 = l[i]


for el in l:
    if el == m:
        print(m2)
    else:
        print(m)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for i in range(0,N):
    A.append(int(input()))
M=max(A)
x=A.index(M)
A[x]=0
y=max(A)
for i in range(N):
    if i!=x:
        print(M)
    else:
        print(y)

Submission status: Accepted

Solution code:
n = int(input())
al = list(int(input()) for _ in range(n))
al_s = sorted(list(set(al)), reverse=True)

if al.count(al_s[0]) == 1:
    for a in al:
        if a != al_s[0]:
            print(al_s[0])
        else:
            print(al_s[1])
else:
    for i in range(n):
        print(al_s[0])

Submission status: Accepted

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

Submission status: Accepted

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

first = 0
second = 1
if A[first] < A[second]:
    first, second = second, first
for i in range(2, n):
    if A[first] < A[i]:
        second = first
        first = i
    elif A[second] < A[i]:
        second = i

print(*[A[first] if i != first else A[second] for i in range(n)], sep='\n')

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
left_l = [0]
right_l = [0]
for i in range(1, n):
    maxVal = max(left_l[i-1], a[i-1])
    left_l.append(maxVal)
for j in range(1, n):
    maxVal = max(right_l[j-1], a[-j])
    right_l.append(maxVal)
right_l.reverse()
for k in range(n):
    maxVal = max(left_l[k],right_l[k])
    print(maxVal)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    A=[0 for i in range(0,N)]
    for i in range(0,N):
        A[i]=int(input())
    s=max(A)
    index=0
    value=[0 for i in range(0,s+1)]
    for i in range(0,N):
        value[A[i]]+=1
        if A[i]==s:
            index=i
    if value[s]>1:
        for i in range(0,N):
            print(s)
    else:
        for i in range(0,N):
            if i==index:
                A[index]=0
                print(max(A))
            else:
                print(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np
 
N = int(readline())
A = np.zeros(N+2,np.int32)
A[1:-1] = np.array(read().split(),np.int32)

left_max = np.maximum.accumulate(A[:-2])
right_max = np.maximum.accumulate(A[::-1])[::-1][2:]
answer = np.maximum(left_max, right_max)
print('\n'.join(answer.astype(str)))

Submission status: Accepted

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.zeros(N+2, np.int32)
A[1:-1] = np.array([ir() for _ in range(N)])

left_max = np.maximum.accumulate(A[:-2])
right_max = np.maximum.accumulate(A[::-1])[::-1][2:]
answer = np.maximum(left_max, right_max)
print('\n'.join(answer.astype(str)))

Submission status: Accepted

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.zeros(N+2, np.int32)
A[1:-1] = np.array([ir() for _ in range(N)])

left_max = np.maximum.accumulate(A[:-2])
right_max = np.maximum.accumulate(A[::-1])[::-1][2:]
answer = np.maximum(left_max, right_max)
print('\n'.join(map(str, answer)))

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = [ir() for _ in range(N)]
top = max(A)
if A.count(top) == 1:
    second = sorted(A, reverse=True)[1]
    for i in range(N):
        if A[i] == top:
            print(second)
        else:
            print(top)
else:
    for i in range(N):
        print(top)
# 23

Submission status: Accepted

Solution code:
n = int(input())
s = list(int(input()) for _ in range(n))
i = 0
sort_s = sorted(s)

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

Submission status: Accepted

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

aa = sorted(a, reverse=True)[:2]

for e in a:
  if e == ma:
    print(aa[1])
  else:
    print(aa[0])

Submission status: Accepted

Solution code:
n=int(input())
A=[]
for i in range(n):
    A.append(int(input()))
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 = [int(input()) for _ in range(n)]

maximum = max(a)
ans = [maximum]*n
if a.count(maximum) ==1:
    maximum_index = a.index(maximum)
    ans[maximum_index] = max(
        a[:maximum_index]+a[maximum_index+1:]
    )
for i in range(n):
    print(ans[i])

Submission status: Accepted

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

f, s = sorted(a, reverse=True)[:2]

for aa in a:
    if aa == f:
        print(s)
    else:
        print(f)

Submission status: Accepted

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

m1 = 0
m2 = 0

for ind, i in enumerate(a):
    if i>m1:
        m2 = m1
        m1 = i
        m_i = ind
    elif i>m2:
        m2 = i

for i in range(n):
    if i==m_i:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

dp1 = [0]*(N+1)
dp2 = [0]*(N+2)

for i in range(1, N+1):
    dp1[i] = max(dp1[i-1], l[i-1])

for i in range(1, N+1)[::-1]:
    dp2[i] = max(dp2[i+1], l[i-1])

for i in range(1, N+1):
    print(max(dp1[i-1], dp2[i+1]))

Submission status: Accepted

Solution code:
N = int(input())
A = tuple(int(input()) for _ in range(N))
B = tuple(sorted(A, reverse=True))
print(*[B[0] if a != B[0] else B[1] for a in A], sep='\n')

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(input()) for i in range(n)]
a_max = max(a_list)
a_max2 = sorted(a_list)[-2]
for i in a_list:
    if i == a_max:
        print(a_max2)
    else:
        print(a_max)

Submission status: Accepted

Solution code:
n = int(input())
As = [int(input()) for _ in range(n)]
sorted_As = sorted(As, reverse = True)
max_val, next_max_val = sorted_As[0], sorted_As[1]
# max_index = As.index(max_val)
for A in As:
    if A == max_val:
        print(next_max_val)
    else:
        print(max_val)

Submission status: Accepted

Solution code:
n = int(input())
num = []
for i in range(n):
    num.append(int(input()))
#ここまで入力
num1 = num.copy()
num1.sort(reverse=True)
mx1 = num1[0]
mx2 = num1[1]
for i in num:
    if i==mx1:
        ans = mx2
        print(ans)
    else:
        ans = mx1
        print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = sys.stdin.readline

def main():
    n = int(input())
    As = [int(input()) for i in range(n)]
    sAs = sorted(As)
    mn1 = sAs[-1]
    mn2 = sAs[-2]
    for i in range(n):
        if As[i] == mn1:
            print(mn2)
        else:
            print(mn1)
    return 

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq

num = int(input())
nums = [int(input()) for _ in range(num)]

maxs = heapq.nlargest(2, nums)
for n in nums:
  if n == maxs[0]:
    print(maxs[1])
  else:
    print(maxs[0])

Submission status: Accepted

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

max_A = max(A)
semimax_A = max_A if A.count(max_A) > 1 else list(sorted(list(set(A))))[-2]
for i in range(N):
    if A[i] == max_A:
        print(semimax_A)
    else:
        print(max_A)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import heapq
n=int(input())
a=[int(input()) for i in range(n)]
b=list(map(lambda x:x*(-1), a))
heapq.heapify(b)
max1=heapq.heappop(b)*(-1)
max2=heapq.heappop(b)*(-1)

for i in range(n):
  if a[i]==max1:print(max2)
  else:print(max1)

Submission status: Accepted

Solution code:
class SegmentTree:
    def __init__(self, array, seg_func, ide_ele):
        n = len(array)
        self.size = 2**(n - 1).bit_length()
        self.seg_func = seg_func
        self.ide_ele = ide_ele
        self.seg = [self.ide_ele] * (2 * self.size - 1)

        for i in range(n):
            self.seg[i + self.size - 1] = array[i]
        for i in range(self.size - 2, -1, -1):
            self.seg[i] = self.seg_func(self.seg[2 * i + 1], self.seg[2 * i + 2])

    def update(self, index, value):
        index += self.size - 1
        self.seg[index] = value
        while index:
            index = (index - 1) // 2
            self.seg[index] = self.seg_func(self.seg[index * 2 + 1], self.seg[index * 2 + 2])

    def query(self, lower_ep, upper_ep):
        if upper_ep <= lower_ep:
            return self.ide_ele
        lower_ep += self.size - 1
        upper_ep += self.size - 2
        res = self.ide_ele
        while (upper_ep - lower_ep) > 1:
            if lower_ep & 1 == 0:
                res = self.seg_func(res, self.seg[lower_ep])
            if upper_ep & 1 == 1:
                res = self.seg_func(res, self.seg[upper_ep])
                upper_ep -= 1

            lower_ep //= 2
            upper_ep = (upper_ep - 1) // 2
        if lower_ep == upper_ep:
            res = self.seg_func(res, self.seg[lower_ep])
        else:
            res = self.seg_func(self.seg_func(res, self.seg[lower_ep]), self.seg[upper_ep])
        return res

N = int(input())
Alist = [int(input()) for _ in range(N)]
S = SegmentTree(Alist, max, 0)
for i in range(N):
    print(max(S.query(0, i), S.query(i + 1, N)))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
sort_a = sorted(a)
max_a = max(a)
max_ind = 0
for i in range(n):
    if a[i] == max_a: max_ind = i
for i in range(n):
    ans = max_a if i != max_ind else sort_a[-2]
    print(ans)

Submission status: Accepted

Solution code:
#abc134 c
n=int(input())
a=[]
for i in range(n):
    x=int(input())
    a.append(x)
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 = []
for i in range(N):
        A.append(int(input()))
B = A.copy()
B.sort()
B.reverse()
for i in range(0,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)]
second, first = sorted(A)[-2:]

ans = [second if a==first else first for a in A]
[print(a) for a in ans]

Submission status: Accepted

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
m = max(a)
sec_a = []
flag = True
for i in range(n):
    if a[i] != m:
        sec_a.append(a[i])
    else:
        if flag:
            index = i
        else:
            sec_a.append(a[i])
        flag = False
sec_m = max(sec_a)
for i in range(n):
    if i == index:
        print(sec_m)
    else:
        print(m)

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = [0]*n

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

b = copy.copy(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:
n = int(input())
t = [int(input()) for i in range(n)]
p = 0
q = 0
l = [0]
r = [0]

for i in range(1,n):
    if p < t[i-1]:
        p = t[i-1]
        l.append(p)
    else:
        l.append(p)
    
    if q < t[-i]:
        q = t[-i]
        r.append(q)
    else:
        r.append(q)

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

Submission status: Accepted

Solution code:
n = int(input())
t = [int(input()) for i in range(n)]
tt = sorted(t)
m = tt[-1]
mm = tt[-2]

for i in range(n):
    if t[i] == m:
        print(mm)
    else:
        print(m)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
max_id = A.index(max(A))
A = sorted(A, reverse=True)
for i in range(n):
    if i == max_id:
        # 最大値が取り除かれるので2番目に大きい数値を出力
        print(A[1])
    else:
        # 最大値が残っているので，最大値を出力
        print(A[0])

Submission status: Accepted

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

max_num = max(A)
a = sorted(A)
second = a[-2]

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(int(input()) for i in range(N))
MAX = int(max(A))
B = A.copy()
B.sort()
for i in range(N):
    if (A[i] == MAX): print(B[N - 2])
    else: print(MAX)

Submission status: Accepted

Solution code:
n = int(input())
a = []
max = int(input())
a.append(max)
second_max = int(input())
a.append(second_max)
if max<second_max:
    second_max,max = max,second_max
for i in range(n-2):
    g = int(input())
    a.append(g)
    if g>max:
        max = g
    elif g>second_max:
        second_max = g

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
AL = [0] * (N+1)
AR = [0] * (N+1) 
for i in range(N):
    AL[i+1] = max(AL[i], A[i])
for i in range(N)[::-1]:
    AR[i] = max(AR[i+1], A[i])
for i in range(N):
    print(max(AL[i], AR[i+1]))

Submission status: Accepted

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

  a=max(A)
  k=A.count(a)#リストの最大数の個数をカウント
  if k>=2:
    for i in range(N):
      print(a)
  else:
    b=A.index(a)
    for i in range(N):
      if i != b:
        print(a)
      else:
        s=sorted(A)
        print(s[N-2])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import copy

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

  a=max(A)
  k=A.count(a)#リストの最大数の個数をカウント
  if k>=2:
    for i in range(N):
      print(a)
  else:
    b=A.index(a)
    for i in range(N):
      if i != b:
        print(a)
      else:
        s=sorted(A)
        print(s[N-2])

if __name__ == "__main__":
    main()

Submission status: Accepted

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

a=max(A)
k=A.count(a)#リストの最大数の個数をカウント
if k>=2:
  for i in range(N):
    print(a)
else:
  b=A.index(a)
  for i in range(N):
    if i != b:
      print(a)
    else:
      s=sorted(A)
      print(s[N-2])

Submission status: Accepted

Solution code:
# import itertools
# import math

import sys
input = sys.stdin.readline


def solve(N, A):
    max_idx = A.index(max(A))
    s = sorted(A, reverse=True)

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


N = int(input())
# S = input()
# A, B = map(int, input().split())
# A = list(map(int, input().split()))


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


solve(N, A)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(input()) for i in range(n)] 
max_idx = s.index(max(s)) 
s = sorted(s, reverse=True)

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

Submission status: Accepted

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

m2, m1  = sorted(A)[-2:]
for i in range(n):
    if A[i]==m1:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(int(input()) for i in range(N))
B = []
count = 0
maxA = max(A)
for i in A:
  if i == maxA:
    count += 1
  else:
    B.append(i)

if B != []:
  maxB = max(B)

if count > 1:
  for i in range(N):
    print(maxA)
else:
  for i in range(N):
    if A[i] == maxA:
      print(maxB)
    else:
      print(maxA)