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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy

n = int( input() )

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

tmp = copy.copy(a)
del tmp[0]
last_max = max(tmp)

print(last_max)
for i in range(1,n):
    if last_max == a[i]:
        tmp = copy.copy(a)
        del tmp[i]
        last_max = max(tmp)
    print(last_max)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = [int(input()) for _ in range(n)]
if a_list[0] > a_list[1]:
    i_max = 0
    i_max2 = 1
else:
    i_max = 1
    i_max2 = 0
a_max = a_list[i_max]
a_max2 = a_list[i_max2]
for i, a in enumerate(a_list):
    if i < 2:
        continue
    if a > a_max:
        i_max2 = i_max
        a_max2 = a_max
        i_max = i
        a_max = a
    elif a > a_max2:
        i_max2 = i
        a_max2 = a
print(i_max, a_max)
print(i_max2, a_max2)
for i in range(n):
    if i == i_max:
        print(a_max2)
    else:
        print(a_max)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

max_element = max(arr)
second_max = -1

for i in arr:
	if i > second_max and i != max_element:
		second_max = i

for i in arr:
	if i != max_element:
		print(second_max)
	else:
		print(max_element)

Submission status: Wrong Answer

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

arg_sorted = arg.copy()
arg_sorted.sort(reverse=True)

for val in arg:
  if val == arg_sorted[0]:
    print(arg_sorted[1])
  else:
    print(arg_sorted[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A_max = max(A)
A_max_count = A.count(A_max)

for i in range(N):
  tmp = A[i]
  if tmp != A_max_count:
    print(A_max)
  else:
    if A_max_count == 1:
      print(max(A[:i]+A[i+1:]))
    else:
      print(A_max)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
    
for i in range(n):
    if i == 0:
        print(max(a[1:]))
    elif i == n-1:
        print(max(a[:n-1]))
    else:
        print(max(a[:i-1]+a[i+1:]))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

max_num = max(b)
max_num_set = set()
max_num_set.add(max_num)
c = b - max_num_set
if len(c) == 0:
    second_max_num = max_num
else:
    second_max_num = max(c)

for elem in a:
    if elem == max_num:
        print(second_max_num)
    else:
        print(max_num)

Submission status: Wrong Answer

Solution code:


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

tmp_a = sorted(list(set(a)), reverse=True)
no1 = tmp_a[0]
no2 = tmp_a[1] if len(tmp_a)>1 else tmp_a[0]

for each in a:
  if each == no1:
    print(no2)
  else:
    print(no1)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
for i in range(n):
    x=int(input())
    l.append(x)
x=max(l)
for i in range(n):
    if l[i]==x:
        l[i]=0
for i in range(n):
    if l[i]==0:
        print(x)
    else:
        print(l[i])

Submission status: Wrong Answer

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

mx = max(a)
mx2 = -1
for i in a:
  if(mx2 < i and i != mx):
    mx2 = i

if(mx2 == -1):
  mx2 = mx
  
for i in a:
  if(i!=mx):
    print(mx)
  else:
    print(mx2)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left

input=sys.stdin.readline

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

A=sorted(a)
max_a=A[-1]

for i in range(n):
    if a[i]==max_a:
        idx=bisect_left(A,a[i])
        print(A[idx-1])
    else:
        print(max_a)

Submission status: Wrong Answer

Solution code:
def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	A = [int(input()) for _ in range(N)]
	prev = max(A[1:])
	print(prev)
	for i in range(1, N-1):
		if A[i] != prev:
			prev = max(A[i-1], prev) 
			print(prev)
		else:
			prev = max(max(A[:i]), max(A[i+1:]))
			print(prev)

	print(max(A[:N]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
maxi=0
smaxi=0
flag=0
ls=[]
x=0
for i in range(N):
  x=int(input())
  ls.append(x)
  if (maxi<x):
    smaxi=maxi
    maxi=x
  elif (maxi==x):
    smaxi=maxi
for i in range(N):
  if (ls[i]==maxi):
    print(smaxi)
  else:
    print(maxi)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
tmp=sorted(A, reverse=True)

sA=list(set(tmp))
if len(sA)==1:
    firstA, secondA=sA[-1], sA[-1]
else:
    firstA=sA[-1]
    secondA=sA[-2]

for a in A:
    if a==firstA:
        print(secondA)
    else:
        print(firstA)

Submission status: Wrong Answer

Solution code:


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

tmp_a = sorted(list(set(a)), reverse=True)
no1 = tmp_a[0]
no2 = tmp_a[0] if len(tmp_a)>1 else tmp_a[0]

for each in a:
  if each == tmp_a[0]:
    print(no1)
  else:
    print(no2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8

n = int(input())

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

max_a = max(A)

for i in range(n):
    if max_a == A[i]:
        max_a = max(A[0:i] + A[i+1:n])
        print(max_a)
    else:
        print(max_a)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
import itertools
for i in itertools.combinations(a,n-1):
  print(max(i))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n = int(input())

MAX = 0
MAX_index = []
MAX_sec = 0
for i in range(n):
    a = int(input())
    if MAX == a:
        MAX_index.append(i)
    elif MAX < a:
        MAX_index = [i]
        MAX = a
    elif MAX_sec < a:
        MAX_sec = a
    
#print(MAX_index)
        
if len(MAX_index)==1:
    for i in range(n):
        if i==MAX_index[0]:
            print(MAX_sec)
        else:
            print(MAX)
else:
    for i in range(n):
        print(MAX)

Submission status: Wrong Answer

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):
        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)
    elif v == max_a:
        if count_max_a == 1:
            print(next_a)
        else:
            print(max_a)

Submission status: Wrong Answer

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

max_n = max(num)

for i in range(n):
    b = num[i]
    if b == max_n:
        num_c = num
        num_c[i] = 0
        print(max(num_c))
    else:
        print(max_n)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = [int(input()) for _ in range(a)]
c = b.copy()
b = sorted(b, reverse=True)
for i in c:
    d = False
    for j in b:
        if i != j:
            print(j)
            d = not d
            break
    if not d:
        print(b[0])

Submission status: Wrong Answer

Solution code:
N= int(input())
num_list = []
max_num = 0
chk = 0
for i in range(N):
	k = int(input())
	if max_num == k :
		chk += 1
	max_num=max(k,max_num)
	num_list.append(k)
if chk != 0:
	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: Wrong Answer

Solution code:
n = int(input())

max_val = 0
sub_max_val = 0
lst = []
for i in range(n):
    ai = int(input())
    lst.append(ai)
    if ai > max_val:
        sub_max_val = max_val
        max_val = ai
    elif ai == max_val:
        pass
    elif ai > sub_max_val:
        sub_max_val = ai

for i in range(n):
    if lst[i] == max_val:
        print(sub_max_val)
    else:
        print(max_val)

Submission status: Wrong Answer

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(N - 2):
  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: Wrong Answer

Solution code:
N=int(input())
maxi=0
smaxi=0
flag=0
ls=[]
x=0
for i in range(N):
  x=int(input())
  ls.append(x)
  if (maxi<x):
    flag=0
    smaxi=maxi
    maxi=x
  elif (maxi==x):
    flag=1
    smaxi=maxi
for i in range(N):
  if (ls[i]==maxi):
    if(flag==1):
      print(maxi)
    else:
      print(smaxi)
  else:
    print(maxi)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
for i in range(n):
    a[i]=int(input())
b=sorted(a)
for i in range(n):
    m = n-1
    while a[i] == b[m] and m > 0:
        m -= 1
    print(b[m])

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Ai=np.argmax(A)
A2max=max(A[:Ai],A[Ai+1:])[0]

for i in range(N):
  if i==Ai:
    print(A2max)
  else:
    print(Amax)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
mx1 = max(A)
mx2 = -1
for i in range(N):
    if A[i] != mx1:
        mx2 = max(mx2, A[i])
if mx2 == -1:
    mx2 = mx1
for i in range(N):
    if A[i] == mx1:
        print(mx2)
    else:
        print(mx1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
a_max = max(a)
a_set = set(a)
second = sorted(set(a))[-2] if len(set(a)) >= 2 else a[0]
for i in a:
  if i == a_max:
    print(second)
  else:
    print(a_max)

Submission status: Wrong Answer

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

b = a[::-1]

amax = [0] * n
bmax = [0] * n

amax[0] = a[0]
bmax[0] = b[0]
for i in range(1, n):
    amax[i] = max(amax[i-1], a[i])
    bmax[i] = max(bmax[i-1], b[i])

bmax = bmax[::-1]

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

Submission status: Wrong Answer

Solution code:
N=int(input())
maxi=0
smaxi=0
flag=0
ls=[]
x=0
for i in range(N):
  x=int(input())
  ls.append(x)
  if (maxi<x):
    flag=0
    smaxi=maxi
    maxi=x
  elif (maxi==x):
    flag=1
if(smaxi==0):
  smaxi==maxi
for i in range(N):
  if (ls[i]==maxi):
    if(flag==1):
      print(maxi)
    else:
      print(smaxi)
  else:
    print(maxi)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = []
aa = []

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

aa.sort()
firsta = aa[0]
seconda = aa[1]

for i in range(n):
    if a[i] == firsta:
        print(seconda)
    else:
        print(firsta)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
lmax = [0]
rmax = [0]
for i in range(N-1):
  lmax.append(max(lmax[i],A[i]))
  rmax.append(max(lmax[i],A[N-1-i]))

rmax = rmax[::-1]
for i in range(N):
  print (max(lmax[i], rmax[i]))

Submission status: Wrong Answer

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):
        max_a = v
        count_max_a = 1
        if next_a == 0:
            next_a = v
    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)
    elif v == max_a:
        if count_max_a == 1:
            print(next_a)
        else:
            print(max_a)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
for i in range(n):
    b=a[:i]+a[i+1:]
    c=max(b)
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = [int(input()) for _ in range(n)]
if a_list[0] > a_list[1]:
    i_max = 0
    i_max2 = 1
else:
    i_max = 1
    i_max2 = 0
a_max = a_list[i_max]
a_max2 = a_list[i_max2]
for i, a in enumerate(a_list[2:]):
    if a > a_max:
        i_max = i
        a_max = a
    elif a > a_max2:
        i_max2 = i
        a_max2 = a
for i in range(n):
    if i == i_max:
        print(a_max2)
    else:
        print(a_max)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

max_v1 = max(A)
max_v2 = 0

for i in range(N):
    if A[i] != max_v1 and A[i] > max_v2:
        max_v2 = A[i]

if max_v2 == 0:
    max_v2 = max_v1

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

Submission status: Wrong Answer

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]
for i in li:
    if i == Max1:
        print(Max2)
    else:
        print(Max1)

Submission status: Wrong Answer

Solution code:
N=int(input())
maxi=0
smaxi=0
flag=0
ls=[]
x=0
for i in range(N):
  x=int(input())
  ls.append(x)
  if (maxi<x):
    flag=0
    smaxi=maxi
    maxi=x
  elif (maxi==x):
    flag=1
for i in range(N):
  if (ls[i]==maxi):
    if(flag==1):
      print(maxi)
    else:
      print(smaxi)
  else:
    print(maxi)

Submission status: Wrong Answer

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

for i in range(N):
  values.append(int(input()))
  
a = max(values)

copy_values = [v for v in values]

while(a in copy_values):
  copy_values.remove(a)

if len(copy_values) == 0:
  b = a
else:
  b = max(copy_values)

for i in range(N):
  if values[i] == a:
    print(b)
  else:
    print(a)

Submission status: Wrong Answer

Solution code:

N = int(input())

A = []

maxi = 0
sec = 0

for i in range(N):

    a = int(input())
    A.append(a)


    if A[maxi] <= a:
        sec = maxi
        maxi = i
        #print (maxi ,sec , i ,a)

    elif A[sec] <= a:

        sec = i

for i in range(N):

    if i == maxi:
        print (A[sec])

    else:
        print (A[maxi])

Submission status: Wrong Answer

Solution code:
import itertools
N=int(input())
A=[int(input()) for _ in range(N)]
B=list(itertools.accumulate(A,func=max))
C=list(itertools.accumulate(A[::-1],func=max))[::-1]
print(C[1])
for i in range(1,N-1):
    print(max(B[i-1],C[i+1]))
print(B[-1])

Submission status: Wrong Answer

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

max_ = max(a_s)
max2 = sorted(a_s)[1]
for a in a_s:
    if a == max_:
        print(max2)
    else:
        print(max_)

Submission status: Wrong Answer

Solution code:
N = input()
N = int(N)
S = [int(input()) for _ in range(N)]
S2 = sorted(S, reverse=True)
onon = S2[0]

for i in range(N):
    if S[i] != onon:
        print(onon)
    else:
        print(S[1])

Submission status: Wrong Answer

Solution code:
n = int(input())

max_val = 0
sub_max_val = 0
max_indices = []
for i in range(n):
    ai = int(input())
    if ai > max_val:
        max_val = ai
        max_indices = [i]
    elif ai == max_val:
        max_indices.append(i)
    elif ai > sub_max_val:
        sub_max_val = ai

for i in range(n):
    if i in max_indices:
        print(sub_max_val)
    else:
        print(max_val)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
ma=max(a)
ma_index=a.index(ma)
for i in range(n):
    if i==ma_index:
        print(*max(a[0:i],a[i+1:]))
    else:
        print(ma)

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[int(input()) for _ in range(n)]
m1=max(arr)
arr2=sorted(arr,reverse=True)
m2=0
cnt=0
for val in arr2:
  if val==m1:
    cnt+=1
  else:
    m2=val
    break
else:
  m2=m1
print(cnt)
for val in arr:
  if cnt>=2:
    print(m1)
  else:
    if val==m1:
      print(m2)
    else:
      print(m1)

Submission status: Wrong Answer

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

B = sorted(A)

MAX1 = B[-1]
MAX2 = B[-2]

if MAX1 == MAX2:
    for a in range(N):
        print(MAX1)
        exit()

for a in A:
    if a != MAX1:
        print(MAX1)
    else:
        print(MAX2)

Submission status: Wrong Answer

Solution code:
# from sys import exit, stderr
# from inspect import currentframe
# def debug(*args):
#     names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
#     print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args), file=stderr)
#     return
N = int(input())
a = [0 for _ in range(N)]
for i in range(N):
    a[i] = (i,int(input()))
a = sorted(a, key=lambda x: -x[1])

for i in range(N):
    for j in range(N):
        if a[i][0] == j:
            continue
        print(a[j][1])
        break

Submission status: Wrong Answer

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

b_list = a_list.copy()
b_list.sort(reverse=True)
max_num = max(b_list)

for i in range(N):

    a = a_list[i]

    if a != max_num:
        print(max_num)
    else:
        f = True
        for b in b_list:
            if a != b:
                print(b)
                f = False
                break
        if f:
            print(b)

Submission status: Wrong Answer

Solution code:
N = int(input())
maxA = 0
secondMaxA = 0
maxIndex = -1
for i in range(N):
  A = int(input())
  if A >= maxA:
    secondMaxA = maxA
    maxA = A
    maxIndex = 1
  elif A >= secondMaxA:
    secondMaxA = A

for i in range(N):
  if i == maxIndex:
    print(secondMaxA)
  else:
    print(maxA)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    sa = sorted(a)

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


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[int(input()) for _ in range(n)]
m1=max(arr)
arr2=sorted(arr,reverse=True)
m2=0
for val in arr2:
  if val!=m1:
    m2=val
    break
else:
  m2=m1
for val in arr:
  if val==m1:
    print(m2)
  else:
    print(m1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
m1, m2 = 200200, 200200
for e in a:
  if e < m1:
    m2 = m1
    m1 = e
  elif e < m2:
    m2 = e
for e in a:
  if e == m1:
    print(m2)
  else:
    print(m1)

Submission status: Wrong Answer

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

ma = max(A)
B = sorted(A)

if B[0] == ma :
    ma2 = ma
else :
    for i in range(N-1,-1,-1) :
        if B[i] != ma :
            ma2 = B[i]
            break



for a in A :
    if a == ma :
        print(ma2)
    else :
        print(ma)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

N = inp()
AA = [inp() for _ in range(N)]
x = sorted(AA)
M1 = x[-1]
M2 = x[-2]

print(M1,M2)

for a in AA:
    if a == M1:
        print(M2)
    else:
        print(M1)

Submission status: Time Limit Exceeded

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

for i in range(n):
    tmp = a[i]
    a[i] = 0
    print(max(a))
    a[i] = tmp

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
for i in range(n):
    if i == 0:
        print(max(a[i+1:]))
    elif i == n - 1:
        print(max(a[:i]))
    else:
        x = max(a[:i])
        y = max(a[i+1:])
        print(max(x, y))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
for i in range(N):
    x = A.pop(i)
    print(max(A))
    A.insert(i,x)

Submission status: Time Limit Exceeded

Solution code:
arr = []
for _ in range(int(input())):
  arr.append(int(input()))
arr2 = arr.copy()
for k in range(len(arr)):
  arr.pop(k)
  print(max(arr))
  arr = arr2.copy()

Submission status: Time Limit Exceeded

Solution code:
#ABC134 C

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
for i in range(n):
  print(max(a[:i]+a[i+1:]))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

for ele in arr:
    tmp = copy.copy(arr)
    tmp.remove(ele)
    print(max(tmp))

Submission status: Time Limit Exceeded

Solution code:
import copy

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

for ele in arr:
    tmp = copy.copy(arr)
    tmp.remove(ele)
    ans.append(max(tmp))

for ele in ans:
    print(ele)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=[int(input()) for i in range(n)]
for i in range(n):
  tmp=A[i]
  A[i]=0
  print(max(A))
  A[i]=tmp

Submission status: Time Limit Exceeded

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

for i in range(n):
    print(max(A[:i]+A[i+1:]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9+7
#main code here!
    n=I()
    lis=[I() for i in range(n)]
    
    class SegmentTree:#非再帰(こちらの方が速い) #1-indexed
        # 初期化処理
        # f : SegmentTreeにのせるモノイド
        # default : fに対する単位元
        def __init__(self, size, f=lambda x,y : max(x,y), default=-inf):
            self.size = 2**(size-1).bit_length() # 簡単のため要素数Nを2冪にする
            self.default = default
            self.dat = [default]*(self.size*2) # 要素を単位元で初期化
            self.f = f
    
        def update(self, i, x):
            i += self.size
            self.dat[i] = x
            while i > 0:
                i >>= 1
                self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])
    
        def query(self, l, r):
            l += self.size
            r += self.size
            lres, rres = self.default, self.default
            while l < r:
                if l & 1:
                    lres = self.f(lres, self.dat[l])
                    l += 1
    
                if r & 1:
                    r -= 1
                    rres = self.f(self.dat[r], rres) # モノイドでは可換律は保証されていないので演算の方向に注意
                l >>= 1
                r >>= 1
            res = self.f(lres, rres)
            return res
    #Seg=SegmentTree(n,lambda x,y : max(x,y),-inf)
    Seg=SegmentTree(n)
    for i in range(n):
        Seg.update(i,lis[i])
    for i in range(n):
        Seg.update(i,-inf)
        print(Seg.query(0,n))
        Seg.update(i,lis[i])
    
    
                
                
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
b=a[:]
p=max(a)
b.remove(p)
q=max(b)

c=[i for i, x in enumerate(a) if x==p]

for i in range(n):
    if i not in c:
        print(p)
    else:
        print(q)

Submission status: Time Limit Exceeded

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

for i in range(n):
    b.remove(a[i])
    print(max(b))
    b.append(a[i])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

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

for i in range(n):
    print(max(a[:i]+a[i+1:]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for i in range(n):
    ai = int(input())
    a.append(ai)
max_a = max(a)
for i in a:
    if i != max_a:
        print(max_a)
    else:
        print(sorted(a)[-2])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

for i in range(N):
    tmp = A[i]
    A[i] = 0
    print(max(A))
    A[i] = tmp

Submission status: Time Limit Exceeded

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

for i in a:
    if i!=int(m[-1]):
        print(m[-1])
    elif a.count(m[-1]) > 1:
        print(m[-1])
    else:
        print(m[-2])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
xlist = [int(input()) for i in range(n)]
a = max(xlist)
for i in range(n):
  if xlist[i] != a:
    print(a)
  else:
    b = xlist[i]
    xlist.pop(i)
    print(max(xlist))
    xlist.insert(i,b)

Submission status: Time Limit Exceeded

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

for j in range(N):
    B = A[0:j]
    C = A[j+1:N]
    D = B+C
    print(max(D))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(N):
    if i == A.index(max(A)):
        B = sorted(A)
        print(B[N-2])
    else:
        print(max(A))

Submission status: Time Limit Exceeded

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

for i in range(N):
    if i != A.index(max(A)):
        print(max(A))
    else:
        B = sorted(A)
        print(B[N-2])

Submission status: Time Limit Exceeded

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

for i in range(N):
    a = A.copy()
    a.remove(A[i])
    print(max(a))