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 = []

max_a = 0
max_i = 0
for i in range(N):
    a = int(input())
    if a > max_a:
        max_a = a
        max_i = i
    A.append(a)

del A[max_i]
max2_a = max(A)

ans_arr = []
for i in range(N):
    if max_i == i:
        ans_arr.append(str(max2_a))
    else:
        ans_arr.append(str(max_a))

ans = '\n'.join(ans_arr)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A_list = [0 for i in range(N)]

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

A_sorted = sorted(A_list)

maxA = A_sorted[-1]
second_maxA = A_sorted[-2]
# print(A_list)
for i in A_list:

    if i == maxA:
        print(second_maxA)
    else:
        print(maxA)

Submission status: Accepted

Solution code:
import sys,heapq,math,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def setrlim():
  sys.setrecursionlimit(250000)
  soft,hard = resource.getrlimit(RLIMIT_STACK)
  # setrlimit works on ubuntu (and atcoder), but not on WSL
  #resource.setrlimit(RLIMIT_STACK, (128*1024*1024,hard))

n = inn()
a = []
for i in range(n):
    a.append(inn())
accl = [0] * (n+1)
accr = [0] * (n+1)
for i in range(n):
    accl[i] = max(accl[i-1], a[i])
for i in range(n-1,-1,-1):
    accr[i] = max(accr[i+1], a[i])
for i in range(n):
    print(max(accl[i-1], accr[i+1]))

Submission status: Accepted

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

B = sorted(A, key=lambda x: x[1])
largest_index = B[-1][0]
for i in range(N):
    if A[i][0] == largest_index:
        print(B[-2][1])
    else:
        print(B[-1][1])

Submission status: Accepted

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

Asort = sorted(A)
maxA = Asort[-1]
nexA = Asort[-2]

for a in A:
    if a == maxA:
        print(nexA)
    else:
        print(maxA)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A_sorted = sorted(A, reverse=True)
max_value = A_sorted[0]
sub_max_value = A_sorted[1]

if max_value == sub_max_value:
    for i in range(N):
        print(max_value)
else:
    for Ai in A:
        if Ai == max_value:
            print(sub_max_value)
        else:
            print(max_value)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N = int(input())
A = [int(input()) for _ in range(N)]

maxA = max(A)
lenA = len(A)
cnt_maxA = A.count(maxA)

if cnt_maxA > 1:
    for _ in range(lenA):
        print(maxA)
    exit()

for i, a in enumerate(A):
    if a == maxA:
        if i == 0:
            print(max(A[1:]))
        elif i == lenA-1:
            print(max(A[:-1]))
        else:
            print(max(max(A[:i]), max(A[i+1:])))
    else:
        print(maxA)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
A=[]
for _ in range(N):
    A.append(int(input()))
maxvalue=max(A)
i=A.index(maxvalue)
del A[i]
secondvalue=max(A)
for j in range(0,i):
    print(maxvalue)
print(secondvalue)
for j in range(i+1,N):
    print(maxvalue)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def sol():
    import sys
    input=sys.stdin.readline
    n=int(input())
    a=[]
    d={}
    x=y=0
    for i in range(n):
        t=int(input())
        a.append(t)
        if t in d:
            d[t]+=1
        else:
            d[t]=1
        if t>x:
            y=x
            x=t
        elif t>y:
            y=t
    for i in a:
        if i==x and d[i]==1:
            print(y)
        else:
            print(x)

if __name__=="__main__":
    sol()

Submission status: Accepted

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

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

A.sort()
sec_max_value = A[-2]

for i in range(N):
    if i != max_index:
        print(max_value)
    else:
        print(sec_max_value)

Submission status: Accepted

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
    
left_max = [0]*(N+1)
right_max = [0]*(N+1)
for i in range(1,N+1):
    left_max[i] = max(left_max[i-1],A[i-1])
    
for i in range(N-1,-1,-1):
    right_max[i] = max(right_max[i+1],A[i])
    
for i in range(N):
    print(max(left_max[i],right_max[i+1]))

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
N = int(stdin.readline().rstrip())
data = []
for _ in range(N):
    data.append(int(input()))
max_val = max(data)
max2_val = sorted(data)[-2]
for i in data:
    if i == max_val:
        print(max2_val)
    else:
        print(max_val)

Submission status: Accepted

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[-1]
seconda = aa[-2]


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

Submission status: Accepted

Solution code:
n =  int(input())
A = [int(input()) for _ in range(n)]
no1, no2 = 0, 0
for a in A:
    if a>no1:
        no1=a
    elif a>no2:
        no2=a
for a in A:
    if a==no1:
        print(no2)
    else:
        print(no1)

Submission status: Accepted

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(S2[1])

Submission status: Accepted

Solution code:
n = int(input())
a = []
max_num = 0
max_ind = 0
max_count = 0
for i in range(n):
    inp = int(input())
    a.append(inp)
    if inp > max_num:
        max_num = inp
        max_ind = i
        max_count = 1
    elif inp == max_num:
        max_count += 1
for i in range(n):
    if max_count > 1:
        print(max_num)
    else:
        if i == max_ind:
            a.remove(max_num)
            print(max(a))
        else:
            print(max_num)

Submission status: Accepted

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

aa = sorted(a)
a_max = aa[-1]
a_max_ind = [x == a_max for x in a]

if aa[-2] == aa[-1]:
    for i in range(n):
        print(a_max)
else:
    for i in range(n):
        if a_max_ind[i]:
            print(aa[-2])
        else:
            print(aa[-1])

Submission status: Accepted

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

re = sorted(c) 

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

Submission status: Accepted

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

for i in range(n):
    get_num = int(input())
    if get_num > max_num:
        second_max_num = max_num
        max_num = get_num
    elif get_num > second_max_num:
        second_max_num = get_num
        
    num_list.append(get_num)

for i in num_list:
    if i == max_num:
        print(second_max_num)
    else:
        print(max_num)

Submission status: Accepted

Solution code:
import sys
n=input()
ls=[]
for _ in xrange(n):
    ls.append(input())
m1= max(ls)
i=ls.index(m1)
del ls[ls.index(m1)]
m2= max(ls)
ls.insert(i,m1)
for i in xrange(n):
    if ls[i]==m1:
        print m2
    else:
        print m1

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [[i, int(input())] for i in range(n)]
    b = sorted(a, key=lambda x: x[1], reverse=True)
    b0 = b[0]
    b1 = b[1]

    for aa in a:
        if aa[0] == b0[0]:
            print(b1[1])
        else:
            print(b0[1])


main()

Submission status: Accepted

Solution code:
N = int(input())
Alist = [int(input()) for i in range(N)]
max_first = 0
max_second = 0
for ai in Alist:
    if max_first < ai:
        max_first, max_second = ai, max_first
    elif max_second < ai:
        max_second = ai
for ai in Alist:
    if max_first == ai:
        print(max_second)
    else:
        print(max_first)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
#debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N = II()
    a_list = []
    for i in range(N):
        a = II()
        a_list.append(a)

    from collections import Counter
    c = Counter(a_list)
    mx = max(a_list)

    k = c.keys()
    second = None
    if len(k) > 1:
        second = sorted(k, reverse=True)[1]

    for i in range(N):
        a = a_list[i]
        if a != mx:
            # これがmax出ない場合
            print(mx)
        else:
            if c[a] == 1:
                # maxが唯一で、これの場合
                print(second)
            else:
                # maxがこれでも複数ある場合
                print(mx)


solve()

Submission status: Accepted

Solution code:
N = int(input())
AN = [int(input()) for i in range(N)]
list = list(reversed(sorted(AN)))

for A in AN:
    if list[0] == A:
        print(list[1])
    else:
        print(list[0])

Submission status: Accepted

Solution code:
N = int(input())
A = []
B = []
for i in range(N):
    a = int(input())
    A.append(a)
    B.append(a)
B.sort()
maxA = B[-1]
max2A = B[-2]

for j in range(N):
    if A[j] == maxA:
        print(max2A)
    else:
        print(maxA)

Submission status: Accepted

Solution code:
import math
import copy
import numpy as np

n = int(input())
i = 0
a_list = []

while i < n:
  a_list.append(int(input()))
  i += 1
  
tmp_a_list = copy.deepcopy(a_list)
first_max = max(a_list)
first_idx = np.argmax(a_list)

tmp_a_list[first_idx] = 0
second_max = max(tmp_a_list)
second_idx = np.argmax(tmp_a_list)

i = 0
while i < n:
  if i == first_idx:
    print(second_max)
  else:
    print(first_max)
  i += 1

Submission status: Accepted

Solution code:
import collections as c
ip = lambda : map(int, input().split())
# ##############
mod = 1e9+7
N = int(input())
f,s,fi,si = 0,0,0,0
for i in range(N):
    t = int(input())
    if f<t:
        s,si = f,fi
        f,fi = t,i
    elif s<t:
        s,si = t,i
for i in range(fi):
    print(f)
print(s)
for i in range(N-fi-1):
    print(f)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

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

A = np.array(A)
maxval = A.max()
maxindex = A.argmax()

A[maxindex] = -1
secval = A.max()

for i in range(N):
	if i == maxindex:
		print(secval)
	else:
		print(maxval)

Submission status: Accepted

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

max_num = max(A)
max_ind = A.index(max(A))
for i in range(N):
  if max_ind == i:
    print(max(A[0:i]+A[i+1:N]))
  else:
    print(max_num)

Submission status: Accepted

Solution code:
import os, sys, re, math

N = int(input())

a = []
for _ in range(N):
    a.append(int(input()))
sorted_a = sorted(a,reverse=True)[:2]

for ai in a:
    if ai == sorted_a[0]:
        print(sorted_a[1])
    else:
        print(sorted_a[0])

Submission status: Accepted

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

n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
new_a = sorted(a)
max_a = max(new_a)
cheak = a.index(max_a)
for i in range(n):
    if i != cheak:
        print(max_a)
    else:
        print(new_a[n-2])

Submission status: Accepted

Solution code:

N = int(input())
A = []
global_max = 1
second_max = 0
for i in range(N):
    new_a = int(input())
    A.append(new_a)
    if new_a > global_max:
        second_max = global_max
        global_max = new_a
    elif new_a > second_max:
        second_max = new_a


for a in A:
    if a < global_max:
        print(global_max)
    else:
        print(second_max)

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(input()) for i in range(n)]
#print(a_list)
sorted_a = sorted(a_list)
#print(sorted_a)
max_a = sorted_a[-1]
semi_max_a = sorted_a[-2]

for a in a_list:
    if a == max_a:
        print(semi_max_a)
    else:
        print(max_a)

Submission status: Accepted

Solution code:
n = int(input())

ma = 0
prem = 0
maxi = -1
for i in range(n):
  x = int(input())
  
  if ma < x:
    prem = ma
    ma = x
    maxi = i
    
  elif prem < x:
    prem = x
    
for i in range(n):
  if maxi == i:
    print(prem)
  else:
    print(ma)

Submission status: Accepted

Solution code:
import numpy as np

def acinput():
    return list(map(int,input().split(" ")))


N=int(input())

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

idx=np.argsort(a)

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

Submission status: Accepted

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

B = A + []
B.sort(reverse=True, key=lambda x:x[0])
ans = [B[0][0]]*N
if B[0][0]!=B[1][0]:
    ans[B[0][1]] = B[1][0]

for a in ans:
    print(a)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(A)

mx = max(A)
B = list(set(A))
B.remove(mx)
for i in range(N):
    if A[i] != mx:
        print(mx)
    elif c[mx] == 1:
        print(max(B))
    else:
        print(mx)

Submission status: Accepted

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

R = [0] * N
L = [0] * N

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

for i in range(N-1, 0-1, -1):
  R[i] = max(A[i], R[(i+1)%N])

for i in range(N):
  print(max( 0<i and L[i-1], i<N-1 and R[i+1] ))

Submission status: Accepted

Solution code:
import copy

N = int(input())

seq = []

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

seq2 = copy.deepcopy(seq)
seq2.sort(reverse=True)

n1 = seq2[0]
n2 = seq2[1]

for a in seq:
    if a == n1:
        print(n2)
    else:
        print(n1)

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_max_ind = np.argmax(A)
A[a_max_ind] = 0

for i in range(n):
    if i != a_max_ind:
        print(a_max)
    else:
        print(np.max(A))

Submission status: Accepted

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

n = int(input())
a = [int(input()) for i in range(n)]
a_m = a.index(max(a))
am = a[a_m]
del a[a_m]
amm = max(a)
for i in range(n):
    if i != a_m:
        print(am)
    else:
        print(amm)

Submission status: Accepted

Solution code:
import io
import sys


def IN_S():
  return input()


def IN_I():
  return int(input())


def IN_L_I():
  return list(map(int, input().split()))


def IN_L_S():
  return list(map(str, input().split()))


def STR_SPLIT(s, n):
  for l in range(0, len(s), n):
    yield s[0 + l:n + l]


def T_IN():
  global test_str
  sys.stdin = io.StringIO(test_str[1:-1])


test_str = '''
2
5
5
'''


def MAIN():
  # T_IN()
  A()


def A():
  n = IN_I()
  l = []
  for i in range(0, n):
    l.append(IN_I())

  l2 = l.copy()
  l.sort()

  for i in l2:
    if i == l[-1]:
      print(l[-2])
    else:
      print(l[-1])

  return None


def B():
  return None


def C():
  return None


MAIN()

Submission status: Accepted

Solution code:
# ABC134c
import sys
from operator import itemgetter
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

aSort = sorted(a, reverse=True, key=itemgetter(1))

for i in range(n):
    j = 0
    while (aSort[j][0] == i):
        j += 1
    print(aSort[j][1])

Submission status: Accepted

Solution code:
N = int(input())

for i in range(N):
    A = int(input())
    if i == 0:
        top_val = A
        top_i = 0
    elif i == 1:
        if top_val < A:
            second_val = top_val
            second_i = top_i
            top_val = A
            top_i = i
        else:
            second_val = A
            second_i = i
    else:
        if top_val < A:
            second_val = top_val
            second_i = top_i
            top_val = A
            top_i = i
        elif second_val < A:
            second_val = A
            second_i = A

for i in range(N):
    if top_i == i:
        print(second_val)
    else:
        print(top_val)

Submission status: Accepted

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

d = [0]*N
dd = [0]*N
d[0] = A[0]
dd[-1] = A[-1]

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

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


print(dd[1])

for i in range(N-2):
    print(max(d[i],dd[i+2]))
print(d[-2])

Submission status: Accepted

Solution code:
import collections

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

A_c = collections.Counter(A)
A_max = max(A)
A_2 = sorted(list(set(A)), reverse=True)
if len(A_2) >= 2:
    A_2ndmax = A_2[1]

for i in range(N):
    if A_c[A_max] >= 2:
        print(A_max)
    else:
        if A[i] != A_max:
            print(A_max)
        else:  # A[i]==A_max
            print(A_2ndmax)

Submission status: Accepted

Solution code:
N = int(input())

list_a = [int(input()) for _ in range(N)]
sorted_list_a = sorted(list_a)

largest = sorted_list_a[-1]
second_largest = sorted_list_a[-2]

for a in list_a:
    if a == largest:
        print(second_largest)
    else:
        print(largest)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

N = int(input())

A = []
for i in range(N):
	A.append(int(input()))
B = copy.deepcopy(A)
A.sort()
a_max = max(A)

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

Submission status: Accepted

Solution code:
N=int(input())
Amax=0
secoundmax=0
L=[]
for i in range(N):
    A=int(input())
    if Amax>A:
        if A>secoundmax:secoundmax=A
    elif A>Amax:
        secoundmax=Amax
        Amax=A
        L=[i]
    else:
        L.append(i)
if len(L)>=2:
    for _ in range(N):print(Amax)
else:
    for j in range(N):
        if j in L:print(secoundmax)
        else:print(Amax)

Submission status: Accepted

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

a_list_sorted = sorted(a_list)
max1 = a_list_sorted[-1]
max2 = a_list_sorted[-2]

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
sa = set(a)
max_a = max(sa)
max_c = a.count(max_a)

if max_c > 1:
    for _ in range(n):
        print(max_a)
else:
    sa.discard(max_a)
    sec_max_a = max(sa)
    for v in a:
        if v == max_a:
            print(sec_max_a)
        else:
            print(max_a)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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

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

s=line[:]
s.sort()
M=s[-1]
m=s[-2]

for i in range(n):
    if line[i]==M:
        print(m)
    else:
        print(M)

Submission status: Accepted

Solution code:
N=int(input())

m1=0
m2=0

A=[]

for i in range(N):
    a=int(input())
    if a>m1:
        m1=a
    elif a>m2:
        m2=a
    A.append(a)

for i in range(N):
    if A[i]==m1:
        print(m2)
    else:
        print(m1)

Submission status: Accepted

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

B = sorted(A, reverse=True)
maxA = B[0]
maxB = B[1]
idx = A.index(maxA)
for i in range(N):
    if i == idx:
        print(maxB)
    else:
        print(maxA)

Submission status: Accepted

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

tmp = A
A = sorted(A, reverse=True)

idx = tmp.index(A[0])

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

Submission status: Accepted

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

ls = l[:]
ls.sort()

for i in l:
    print(ls[-1] if i != ls[-1] else ls[-2])

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for Ai in A:
    d[Ai] += 1

sorted_A = sorted(d.keys(), reverse=True)
for Ai in A:
    if Ai < sorted_A[0]:
        ans = sorted_A[0]
    else:
        ans = sorted_A[0] if d[sorted_A[0]] >= 2 else sorted_A[1]
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
dekai=[0,0]
onaji=0
alist=[]
for i in range(n):
    ai=int(input())
    if ai>dekai[0]:
        dekai[1]=dekai[0]
        dekai[0]=ai
    elif ai>dekai[1]:
        dekai[1]=ai
    alist.append(ai)

for i in range(n):
    x=alist[i]
    if x==dekai[0]:
        print(dekai[1])
    else:
        print(dekai[0])

Submission status: Accepted

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

Submission status: Accepted

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


def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    B = sorted(A)
    
    m1 = B[-1]
    m2 = B[-2]

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



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N = I()

A = IR(N)

left = [0]*N
right = [0]*N

left[0] = A[0]
right[-1] = A[-1]

for i in range(1,N):
    left[i] = max(left[i-1], A[i])
for i in range(0, N-1)[::-1]:
    right[i] = max(right[i+1], A[i])

print(right[1])
for i in range(1,N-1):
    print(max(left[i-1], right[i+1]))
print(left[-2])

Submission status: Accepted

Solution code:
import sys
import math
import copy

input = sys.stdin.readline

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

N = int(input())

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

B = copy.deepcopy(A)
B.pop(maxvi)

max_v1 = max(A)
max_v2 = max(B)

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

Submission status: Accepted

Solution code:
import copy
from collections import Counter
N = int(input())
num= [int(input()) for i in range(N)]
numcopy = list(set(copy.deepcopy(num)))
numcopy.sort(reverse=True)
numcount = Counter(num)
for i in range(len(num)):
    if num[i]==numcopy[0] and numcount[num[i]]==1:
        print(numcopy[1])
    else:
        print(numcopy[0])

Submission status: Accepted

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

Submission status: Accepted

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

s = []
for Ai in Ac:
    s.append((Ai, Ac[Ai]))

s = sorted(s, reverse=True)

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

Submission status: Accepted

Solution code:
#   In this template you are not required to write code in main

import sys
inf = float("inf")
#sys.setrecursionlimit(1000000)


# abc='abcdefghijklmnopqrstuvwxyz'
# abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
# mod,MOD=1000000007,998244353
# vow=['a','e','i','o','u']
# dx,dy=[-1,1,0,0],[0,0,1,-1]

#from cmath import sqrt
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
#from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd
from bisect import bisect_left,bisect_right
#import numpy as np


def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

n=int(input())
Arr=[]
for i in range(n):
    Arr.append(int(input()))
mydict=dict()
for i in Arr:
    mydict[i]=mydict.get(i,0)+1
maxi=max(Arr)
occurence=mydict[maxi]
if occurence>1:
    for i in range(n):
        print(maxi)
    exit()
second_maxi=-1
for i in Arr:
    if i>second_maxi and i<maxi:
        second_maxi=i
for i in range(n):
    if Arr[i]!=maxi:
        print(maxi)
    else:
        print(second_maxi)

Submission status: Accepted

Solution code:
from collections import Counter


len_arr = int(input())

arr = [int(input()) for _ in range(0, len_arr)]

counter = Counter(arr)
arr_max = max(counter)

if counter[arr_max] == 1:
    max_index = arr.index(arr_max)
    for i in range(0, len_arr):
        if i == max_index:
            del(counter[arr_max])
            print(max(counter))
        else:
            print(arr_max)

else:
    for _ in range(0, len_arr):
        print(arr_max)

exit(0)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(input()) for _ in [0]*n]
M=max(a)
k=a.index(M)
m=max(a[:k]+a[k+1:])
for i in range(n):
    if i!=k:print(M)
    else:print(m)

Submission status: Accepted

Solution code:
# ABC134-C

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l1=[I() for _ in range(n)]

  l2=copy.deepcopy(l1)
  l2.sort(reverse=True)

  a=l2[0]
  b=l2[1]

  for x in l1:
    if x==a:
      print(b)
    else:
      print(a)

main()
# print(main())

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 20 20:53:37 2019

@author: ctiuser
"""

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

first = 0
second = 0
for c in A:
    if c > first:
        first, second = c, first
    elif c > second:
        second = c

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_s = [int(input()) for _ in range(n)]
    maximum = max(a_s)
    idx_max = a_s.index(maximum)
    second = max(a_s[:idx_max] + a_s[idx_max+1:])
    for i in range(n):
        if i == idx_max:
            print(second)
        else:
            print(maximum)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    maxv = 0
    maxv2 = 0
    A = [INT() for i in range(N)]
    A_ = sorted(A, reverse = True)
    maxv = A_[0]
    maxv2 = A_[1]
    for i in range(N):
        if A[i] == maxv:
            print(maxv2)
        else:
            print(maxv)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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