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

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

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

max_ = max(a)
max2 = sorted(a)[-2]
for i in range(n):
    if a[i] != max_:
        print(max_)
    else:
        print(max2)

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

Asorted=sorted(A)
Amax1=Asorted[-1]
Amax2=Asorted[-2]

for a in A:
	if a!=Amax1:
		print(Amax1)
	else:
		print(Amax2)

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

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

sortedA = list(reversed(sorted(A)))
for a in A:
  count = 0
  for b in sortedA:
    if b != a:
      print(b)
      break
    if a == b:
      if count == 0:
        count += 1
      else:
        print(b)
        break

Solution (Python):









INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    A=[]
    for i in range(n):
        A.append(INT())
    
    B=sorted(A,reverse=True)
    m1=A.count(B[0])
    m2=A.count(B[1])
    if m1>=2:
        flg=True
    else:
        index=A.index(B[0])
        flg=False

    

    if flg:
        for i in range(n):
            print(B[0])
    else:
        for i in range(n):
            if i!=index:
                print(B[0])
            else:
                print(B[1])


if __name__=='__main__':
    do()

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b=sorted(a)
c,d=b[-1],b[-2]
for i in a:
    print(c if i!=c else d)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
b=sorted(a)
c,d=b[-1],b[-2]
for i in a:
    print(c if i!=c else d)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
m = max(A)
mindex = A.index(m)
for i in range(N):
    if(i != mindex):
        print(m)
    else:
        pop = A.pop(i)
        print(max(A))
        A.insert(i,pop)

Solution (Python):
from collections import Counter
N=int(input())
Alist=[]
maxi=0
n2=0
for _ in range(N):
    A=int(input())
    Alist.append(A)
    if A>maxi:
        n2=maxi
        maxi=A
    elif A>n2:
        n2=A
count=Counter(Alist)
for A in Alist:
    if A!=maxi:
        print(maxi)
    elif A==maxi and count[A]>1:
        print(maxi)
    else:
        print(n2)

Solution (Python):
n = int(input())
arr = [int(input()) for _ in range(n)]
ar = sorted(arr)
mx = ar[-1]
sc = ar[-2]

for ele in arr:
    if ele == mx:
        print(sc)
    else:
        print(mx)

Solution (Python):
arr = []
for _ in range(int(input())):
  arr.append(int(input()))
arr2 = arr.copy()
arr2.sort()
z = arr2[-1]
zz = arr2[-2]
for k in arr:
  if k != z:
    print(z)
  else:
    print(zz)

Solution (Python):


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

Solution (Python):
N = int(input())
b = []
max_num = 0
for i in range(N):
  a = int(input())
  b.append(a)
  max_num = max(a,max_num)
max_num2 = 0  
num = b.count(max_num)
if num >1:
  for i in range(N):
    print(max_num)
elif num ==1:
  for i in b:
    if i == max_num:
      pass
    else:
      max_num2 = max(max_num2,i)
  for i in b:
    if max_num == i:
      print(max_num2)
    else:
      print(max_num)

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

l = [0] * (n+2)
l[0] = 0
for i in range(1,n+1):
  l[i] = max(l[i-1], a[i])

r = [0] * (n+2)
r[n] = 0
for i in range(n,0,-1):
  r[i] = max(r[i+1], a[i])

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
sort_A = sorted(A)
max_val = max(A)
second_val = sort_A[-2]

for i in range(N):
    if A[i] == max_val:
        print(second_val)
    else:
        print(max_val)

Solution (Python):
import bisect

N = int(input())
A = [int(input()) for _ in range(N)]
sort_A = sorted(A)
max_val = max(A)
second_val = sort_A[bisect.bisect_right(sort_A, max_val) - 2]

for i in range(N):
    if A[i] == max_val:
        print(second_val)
    else:
        print(max_val)

Solution (Python):

import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = sorted((int(input()), i) for i in range(n))
for i in range(n):
    print(a[-1][0] if i != a[-1][1] else a[-2][0])

Solution (Python):
n=int(input())
A=[int(input()) for i in range(n)]
ma=max(A)
flag=False
if A.count(ma)>=2:
  flag=True
for i in range(n):
  if A[i] != ma:
    print(ma)
  else:
    if flag:
      print(ma)
    else:
      A[i]=0
      print(max(A))

Solution (Python):

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

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

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

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

Solution (Python):
import heapq

n = int(input())
A = [int(input()) for _ in range(n)]
m1, m2 = heapq.nlargest(2, A)
print(*[m2 if a == m1 else m1 for a in A], sep='\n')

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

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

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

M = max(A)
i = A.index(M)
M2 = max(A[:i] + A[i+1:])

for a in A:
    print(M if a != M else M2)

Solution (Python):
import copy

N = int(input())

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

ary_copy = copy.copy(ary)
ary_copy.sort()

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

for i in ary:
    if i == max_1:
        print(max_2)
    elif i != max_1:
        print(max_1)

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

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    max = 0
    next = 0
    for i in A:
        if i >= max:
            next = max
            max = i
        elif i >= next:
            next = i

    for i in A:
        if i == max:
            print(next)
        else:
            print(max)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
import copy
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
B = copy.copy(A)
B.sort()
A_max = B[-1]
A_max2 = B[-2]

for i in range(n):
    if A[i] < A_max:
        print(A_max)
    elif A[i] == A_max:
        print(A_max2)

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

    a_sort = sorted(a)
    a_max = a_sort[-1]
    a_second = a_sort[-2]

    for i in range(n):
        if a[i] == a_max:
            print(a_second)
        else:
            print(a_max)


if __name__ == '__main__':
    main()

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

    a_sort = sorted(a)
    a_max = a_sort[-1]
    a_second = a_sort[-2]

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


if __name__ == '__main__':
    main()

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

    a_sort = sorted(a)

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


if __name__ == '__main__':
    main()

Solution (Python):
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

    n=I()
    lis=[I() for i in range(n)]
    
    class SegmentTree:
        
        
        
        def __init__(self, size, f=lambda x,y : max(x,y), default=-inf):
            self.size = 2**(size-1).bit_length() 
            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)
    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()

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

for i in a:
    if i!=p:
        print(p)
    else:
        print(q)

Solution (Python):
import copy
n = int(input())
a = []
for i in range(n):
  s = int(input())
  a.append(s)
b = copy.copy(a)
a.sort(reverse=True)
c = b.index(a[0])
for j in b:
  if j == b[c]:
    print(a[1])
  else:
    print(a[0])

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
b=sorted(a,reverse=True)
first,second=b[0],b[1]

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

Solution (Python):
n=int(input())

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

a_max=max(a)
a_max_cnt=a.count(a_max)

for i in range(n):
    if (a[i]!=a_max or a_max_cnt>1):
        print(a_max)
    else:
        print(max(a[:i]+a[i+1:]))

Solution (Python):
n = int(input())
max_a = 0
next_a = 0
max_loc = 0
for i in range(n):
    ai = int(input())
    if ai > max_a:
        max_a =ai
        max_loc = i
    elif ai > next_a:
        next_a = ai
if next_a == 0:
    for i in range(n):
        print(max_a)
else:
    for i in range(max_loc):
        print(max_a)
    print(next_a)
    for i in range(n-max_loc-1):
        print(max_a)

Solution (Python):
n = int(input())
a = []
for i in range(n):
    A = int(input())
    a.append(A)
a_1 = list(set(a))[-1]
if a.count(a_1) >= 2:
    for i in range(n):
        print(a_1)
else:
    a_2 = list(set(a))[-2]
    for i in range(n):
        if a[i] == a_1:
            print(a_2)
        else:
            print(a_1)

Solution (Python):
n = int(input())
alst = [int(input()) for _ in range(n)]
max_2 = [0, 0]
for a in alst:
  if a > max_2[0]:
    max_2[1] = max_2[0]
    max_2[0] = a
  elif a > max_2[1]:
    max_2[1] = a

for a in alst:
  if a == max_2[0]:
    print(max_2[1])
  else:
    print(max_2[0])

Solution (Python):
n=int(input())
a=[]
for _ in range(n):
    a.append(int(input()))
b=a.copy()
b.sort()
a_=max(a)
for i in range(n):
    if a[i]==a_:
        print(b[-2])
    else:
        print(a_)

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

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

Solution (Python):
N = int(input())
A = [next(map(int, input().split())) for _ in range(N)]

AA = sorted(A, reverse=True)

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

Solution (Python):
import collections

n = int(input())
a = [int(input()) for i in range(n)]
m = sorted(list(set(a)))
c =collections.Counter(a)

t = [max(a)]*n

if c[max(a)] == 1:
    t[a.index(max(a))] = m[-2]

[print(i) for i in t]

Solution (Python):
from copy import deepcopy
n = int(input())
A = [int(input()) for i in range(n)]
A2 = deepcopy(A)
A2.sort()
for i in range(n):
    
    if A[i] == A2[-1]:
        print (A2[-2])
    else:
        print (A2[-1])

Solution (Python):
n = int(input())
xlist = [int(input()) for i in range(n)]
ylist = sorted(xlist)
max1 = ylist[-1]
max2 = ylist[-2]
for i in range(n):
  if xlist[i] != max1:
    print(max1)
  else:
    print(max2)

Solution (Python):

url = "https://atcoder.jp//contests/abc134/tasks/abc134_c"

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    sort_a = sorted(a, reverse=True)
    for v in a:
        if sort_a[0] == v:
            print(sort_a[1])
        else:
            print(sort_a[0])

if __name__ == '__main__':
    main()

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

mx = max(A)
mxidx = A.index(mx)

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

Solution (Python):


def main():
    N = int(input())
    fir, fir_i, sec, sec_i = 0, 0, 0, 0

    for i in range(N):
        tmp = int(input())
        if tmp > fir:
            sec = fir
            sec_i = fir_i
            fir = tmp
            fir_i = i
        elif tmp > sec:
            sec = tmp
            sec_i = i

    for i in range(N):
        if i != fir_i:
            print(fir)
        else:
            print(sec)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

input = sys.stdin.readline
N = int(input())
A = [int(input()) for _ in range(N)]
a1, a2 = sorted(A)[::-1][:2]
for a in A:
    if a != a1:
        print(a1)
    else:
        print(a2)

Solution (Python):
N = int(input())
l = [int(input()) for _ in range(N)]
largest = max(l)
largest_index = l.index(max(l))
second_largest = sorted(l)[-2]
second_largest_index = l.index(second_largest)

for idx, n in enumerate(l):
  if idx == largest_index:
    print(second_largest)
  else:
    print(largest)

Solution (Python):
def abc134c():
    n = int(input())
    a = []
    for _ in range(n):
        a.append(int(input()))
    a_sorted = sorted(a)
    for item in a:
        if item == a_sorted[-1]:
            print(a_sorted[-2])
        else:
            print(a_sorted[-1])
abc134c()

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

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

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

lmax=[0]*N
rmax=[0]*N

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

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

Solution (Python):
n = int(input())
L = list(int(input()) for _ in range(n))
SL = sorted(L)
first_max = SL[-1]
second_max = SL[-2]
for l in L:
  if (l == first_max):
    print(second_max)
  else:
    print(first_max)

Solution (Python):
n = int(input())
L = list(int(input()) for _ in range(n))
SL = sorted(L)
first_max = SL[-1]
second_max = SL[-2]
for l in L:
  if (l != first_max):
    print(first_max)
  else:
    print(second_max)

Solution (Python):
n = int(input())
L = list(int(input()) for _ in range(n))
SL = sorted(L)
first_max = SL[-1]
second_max = SL[-2]
for l in L:
  if (l != first_max):
    print(first_max)
  elif (l == first_max):
    print(second_max)

Solution (Python):
N = int(input())
A = []
import sys
input = sys.stdin.readline
for i in range(N):
  A.append(int(input().rstrip()))
from collections import Counter
counter = Counter(A)
max_A = max(A)



try:
  second_max = sorted(set(A))[-2]
except:
  second_max = max_A
j = False
if counter[max_A] >= 2:
  j = True
for i in range(N):
  if A[i] == max_A and j == False:
    print(second_max)
  else:
    print(max_A)

Solution (Python):
num = int(input())

table = []
for i in range(num):
    table.append(int(input()))

table2 = sorted(table, reverse=True)

for j in range(num):
    if table[j] != table2[0]:
        print(table2[0])
    else:
        print(table2[1])

Solution (Python):
N,*D=map(int,open(0).read().split())
S=sorted(D,reverse=True)
if S[1]==S[0]:
  [print(S[0]) for i in range(N)]
else:
  import numpy as np
  max_i=np.arange(N)[np.array(D)==S[0]]
  for i in range(N):
    if i==max_i:
      print(S[1])
    else:
      print(S[0])

Solution (Python):
N=int(input())
A=[int(input()) for _ in range(N)]
M1=max(A[0],A[1])
M2=min(A[0],A[1])
for i in range(2,N):
    if A[i]>=M1:
        M2=M1
        M1=A[i]
    else:
        M2=max(M2,A[i])
for i in range(N):
    if A[i]==M1:
        print(M2)
    else:
        print(M1)

Solution (Python):
n = int(input())
lst = []

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

a = max(lst)
b = []
for i in range(n):
    if (lst[i] == a):
        b.append(i)

if (len(b) > 1):
    for i in range(n):
        print(a)

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

Solution (Python):

import sys
input=sys.stdin.readline
import heapq

def main():
    numbers=[]
    n = int(input())
    [numbers.append(int(input().rstrip())) for _ in range(n)]

    max_n=0
    second_n=0

    for i in numbers:
        if i > max_n:
            max_n=i
        elif i > second_n:
            second_n=i
    
    for i in numbers:
        if i==max_n:
            print(second_n)
        else:
            print(max_n)
    

if __name__=="__main__":
    main()

Solution (Python):
n=int(input())

a=[]
a_sort=[]
for i in range(n):
    tmp=int(input())
    a.append(tmp)
    a_sort.append(tmp)
a_sort.sort(reverse=True)

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

Solution (Python):
n=int(input())

import heapq
a=[]
a_heap=[]
for i in range(n):
    tmp=int(input())
    a.append(tmp)
    heapq.heappush(a_heap,(-1)*tmp)

for i in range(n):
    max_1=heapq.heappop(a_heap)
    if max_1==(-1)*a[i]:
        max_2=heapq.heappop(a_heap)
        print((-1)*max_2)
        heapq.heappush(a_heap,max_2)
    else:print((-1)*max_1)
    heapq.heappush(a_heap,max_1)

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

m,s = 0,0
for a in A:
    if a > m:
        m = a
    elif a > s:
        s = a

for a in A:
    if a == m:
        print(s)
    else:
        print(m)

Solution (Python):
import sys
from collections import Counter


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = [int(input()) for _ in range(n)]
    cnt = Counter(a)
    maximum = max(a)
    if cnt[maximum] > 1:
        print("\n".join([str(maximum)] * n))
    else:
        second = sorted(a, reverse=True)[1]
        for i in range(n):
            if a[i] != maximum:
                print(maximum)
            else:
                print(second)


if __name__ == "__main__":
    main()

Solution (Python):
import copy
n = int(input())
an = [int(input()) for i in range(n) ]
bn = copy.copy(an)
top = max(an)
an.remove(top)
top2 = max(an)


for x in bn:
  if x == top:
    print(top2)
  else:
    print(top)

Solution (Python):
N = int(input())
L = []
M = []
for i in range(N):
  a = int(input())
  L.append(a)
  M.append(a)
L.sort()
for j in range(N):
  if M[j] == L[len(L) - 1]:
    print(L[len(L) - 2])
  else:
    print(L[len(L) - 1])

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
 
M = max(A)
M_index = A.index(M)
sub_M = 0
for i in range(N):
  if i != M_index: sub_M = max(sub_M, A[i])

for i in range(N):
  if i == M_index: print(sub_M)
  else: print(M)

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

M = max(A)
M_index = A.index(M)
sub_M = 0
for i in range(N):
  if i != M_index: sub_M = max(sub_M, A[i])

for i in range(N):
  if i == M_index: print(sub_M)
  else: print(M)

Solution (Python):
n = int(input())
a_l = [ int(input()) for _ in range(n)]

l = []
r = []
l_max = 0
r_max = 0
for i in range(n):
    l_max = max(l_max, a_l[i])
    r_max = max(r_max, a_l[-i-1])
    l.append(l_max)
    r.append(r_max)
for i in range(n):
    if i == 0:
        t_l = 0
    else:
        t_l = l[i-1]
    if i == n-1:
        t_r = 0
    else:
        t_r = r[-i-2]
    print(max([t_l,t_r]))

Solution (Python):
import sys
import copy
import math
import itertools
import numpy as np
N = int(input())
A = [int(input()) for c in range(N)]
B = sorted(copy.deepcopy(A),reverse=True)

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

Solution (Python):
n = int(input())
first = 0
second = 0
a = []

for i in range(n):	
	a.append(int(input()))
	if first < a[i]:
		first = a[i]
	elif second < a[i]:
		second = a[i]


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

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

B = sorted(A)
mfirst = B[-1]
msecond = B[-2]

for i in range(N):
    if A[i] == mfirst:
        print(msecond)
    else:
        print(mfirst)

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

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

Solution (Python):









import os
import sys


from collections import Counter


def main():
    n = int(input())
    A = []
    for _ in range(n):
        A.append(int(input()))
    A_cnt = Counter(A)
    flag = 0
    A_max = max(A)
    A_next_max = sorted(A, reverse=True)[1]
    if A_cnt[A_max] > 1:
        flag = 1

    for a in A:
        if a == A_max and flag == 1:
            print(A_max)
        elif a == A_max and flag == 0:
            print(A_next_max)
        else:
            print(A_max)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
max1 = [0,[]]
max2 = 0
for i in range(N):
  a = int(input())
  if a > max1[0]:
    max2 = max1[0]
    max1[0] = a
    max1[1] = [i]
  elif a == max1[0]:
    max1[1].append(i)
  elif a > max2:
    max2 = a

if len(max1[1]) == 1:
  max1_idx = max1[1][0]
  for i in range(0,max1_idx):
    print(max1[0])
  print(max2)
  for i in range(max1_idx + 1 , N):
    print(max1[0])
else:
  for i in range(N):
    print(max1[0])

Solution (Python):

"""
Created on Thu Sep 10 00:13:41 2020

@author: liang
"""

N = int(input())
A = [int(input()) for i in range(N)]
B = A.copy()
B.sort()

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

Solution (Python):
n=int(input())
A=[int(input()) for _ in range(n)]
sort_A=sorted(A,reverse=True)

for a in A:
  if a==sort_A[0]:
    print(sort_A[1])
  else:
    print(sort_A[0])

Solution (Python):
N=int(input())
a=[int(input()) for i in range(N)]
sw=True
b=sorted(a,reverse=True)

if b[0]>b[1]:
    max2=b[1]
    max1=b[0]
    sw=False

if sw==True:
    max1=b[0]
    for k in range(N):
        print(max1)
else:
    for k in range(N):
        if a[k]==max1:
            print(max2)
        else:
            print(max1)

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
I1 = A.index(max(A))
M1 = A.pop(I1)
M2 = max(A)
for j in range (N):
  if j == I1:
    print(M2)
  else:
    print(M1)

Solution (Python):
n = int(input())
alist = [int(input()) for _ in range(n)]

import copy
alist_copy = copy.copy(alist)

alist_copy.sort()
max_value = alist_copy[-1]
next_value = alist_copy[-2]
for a in alist:
    if a == max_value:
        print(next_value)
    else:
        print(max_value)

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

firstVal = max(A)
firstInd = A.index(firstVal)
secondVal = max(A[0:firstInd] + A[firstInd+1:])

for i in range(firstInd):
    print(firstVal)

print(secondVal)

for i in range(N-firstInd-1):
    print(firstVal)

Solution (Python):
import sys
import numpy as np
n = int(input())
a = [int(input()) for _ in range(n)]
mx = str(max(a))
mx_id = a.index(int(mx))
a.remove(int(mx))
mx_sub = str(max(a))
ans = '\n'.join(mx if i != mx_id else mx_sub for i in range(n))

print(ans)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
import copy
B = copy.deepcopy(A)
A = sorted(A)
for i in range(N):
    num = B[i]
    if num != A[-1]:
        print(A[-1])
    else:
        print(A[-2])

Solution (Python):
class SegTree:
    X_unit = 0
    X_f = max

    def __init__(self, N):
        self.N = N
        self.X = [self.X_unit] * (N + N)

    def build(self, seq):
        for i, x in enumerate(seq, self.N):
            self.X[i] = x
        for i in range(self.N - 1, 0, -1):
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def set_val(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1])

    def fold(self, L, R):
        L += self.N
        R += self.N
        vL = self.X_unit
        vR = self.X_unit
        while L < R:
            if L & 1:
                vL = self.X_f(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.X_f(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.X_f(vL, vR)


def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    seg = SegTree(N)
    seg.build(A)

    for i in range(N):
        print(max(seg.fold(0, i), seg.fold(i+1, N)))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    L = [0]*(N+1)
    for i, a in enumerate(A, start=1):
        L[i] = max(L[i-1], a)

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

input = sys.stdin.readline

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

for _ in range(n):
    A.append(int(input()))


sort_A = sorted(A, reverse=True)
max_1 = sort_A[0]
max_2 = sort_A[1]

if max_1 == max_2:
    for _ in range(n):
        print(max_1)
else:
    for i in range(n):
        if A[i] == max_1:
            print(max_2)
        else:
            print(max_1)

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    sortA = sorted(A)
    
    ans = sortA[N-1]
    ans2 = sortA[N-2]
    if ans == ans2:
        flag = 1
    else:
        flag = 0

    for i in range(N):
        if flag == 1:
            print(ans)
        else:
            if A[i] == ans:
                print(ans2)
            else:
                print(ans)

main()

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
sort_A = sorted(A, reverse=True)

for a in A:
    if a == sort_A[0]:
        print(sort_A[1])
    else:
        print(sort_A[0])

Solution (Python):
N, *A = map(int, open(0).read().split())
B = sorted(A)
if B[-1] == B[-2]:
    for a in A:
        print(B[-1])
else:
    for a in A:
        if a != B[-1]:
            print(B[-1])
        else:
            print(B[-2])

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())


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

B = [0]*N  
C = [0]*N  
for i in range(N):
    if i == 0:
        B[i] = A[i]
    else:
        B[i] = max(B[i-1],A[i])
for i in range(N-1,-1,-1):
    if i == N-1:
        C[i] = A[-1]
    else:
        C[i] = max(C[i+1],A[i])

for i in range(N):
    if i == 0:
        print(C[1])
    elif i == N-1:
        print(B[-2])
    else:
        print(max(B[i-1],C[i+1]))

Solution (Python):
from copy import copy
n, *a = map(int, open(0).read().split())

b = copy(a)
c = copy(a)

for i in range(1, n):
    b[i] = max(b[i], b[i - 1])
for i in range(n - 2, -1, -1):
    c[i] = max(c[i], c[i + 1])
b.append(-1)
c.append(-1)

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

Solution (Python):
import sys

input = sys.stdin.readline
N = int(input())

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

for i, a in enumerate(sorted(A, reverse=True)):
    if i == 0:
        first = a
    elif i == 1:
        second = a
    else:
        break


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

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

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

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

b=a.copy()

import heapq

def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()    
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt

b = sorted(b,reverse=True)

b1 = _heappop_max(b)
b2 = _heappop_max(b)

for i in range(n):
    if a[i]==b1:
        print(b2)
    else:
        print(b1)

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

maxa=max(a)
cnt=0

for aa in a:
    if maxa==aa:
        cnt+=1

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A,reverse=True)
b0 = B[0]
b1 = B[1]
for i in range(N):
    a = A[i]
    if a!=b0:
        print(b0)
    else:
        print(b1)