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 i in range(n)]
a_max = max(a)
a_max_cnt = a.count(a_max)
a_max_i = a.index(a_max)

if a_max_cnt == 1:
  for i in range(n):
    if i==a_max_i:
      print(sorted(set(a))[-2])
    else:
      print(a_max)
else:
  for i in range(n):
    print(a_max)

Solution (Python):
def main():
  x = [int(input()) for _ in range(int(input()))]
  y = x[:]
  y.sort(reverse=True)

  for v in x:
    if v == y[0]:
      print(y[1])
    else:
      print(y[0])


if __name__ == '__main__':
  main()

Solution (Python):
n=int(input())
l=[]
for i in range(0,n):
    o=int(input())
    l.append(o)
q=list(l)
q.sort(key=int,reverse=True)
for i in range(0,len(l)):
    if l[i]!=q[0]:
        print(q[0])
    else:
        print(q[1])

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

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

asort=sorted(a)

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

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
sorted_A = sorted(A)
max_A = sorted_A[N-1]
if(sorted_A[N-2] == max_A):
    for i in range(N):
        print(max_A)
else:
    for i in range(N):
        if(A[i] == max_A):
            print(sorted_A[N-2])
        else:
            print(max_A)

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

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

Solution (Python):
n = int(input())
arr = []
for i in range(n):
	x = int(input())
	arr += [x]

a = arr[:]
arr.sort()

ma = arr[-1]
pma = arr[-2]

for x in a:
	if x == ma:
		print(pma)
	else:
		print(ma)

Solution (Python):
import numpy as np
from collections import defaultdict
from heapq import heappush, heappop
from math import ceil


def djkstra_adj_list(adj_list, src):
    dist = defaultdict(lambda: np.inf)
    dist[src] = 0

    prev = defaultdict(lambda: None)

    queue = []
    heappush(queue, (dist[src], src))

    while len(queue) > 0:
        dist_u, u = heappop(queue)
        vs = adj_list[u]
        for v, weight in vs:
            alt = dist_u + weight
            if alt < dist[v]:
                dist[v] = alt
                prev[v] = u
                heappush(queue, (alt, v))

    return dist, prev


def djkstra_adj_mat(adj_mat, src):
    dist = defaultdict(lambda: np.inf)
    dist[src] = 0

    prev = defaultdict(lambda: None)

    queue = []
    heappush(queue, (dist[src], src))

    while len(queue) > 0:
        dist_u, u = heappop(queue)
        vs = np.argwhere(adj_mat[u] > 0).flatten()
        for v in vs:
            weight = adj_mat[u, v]
            alt = dist_u + weight
            if alt < dist[v]:
                dist[v] = alt
                prev[v] = u
                heappush(queue, (alt, v))

    return dist, prev


def main():

    N = int(input())
    As = []
    for _ in range(N):
        A = int(input())
        As.append(A)
    As = np.array(As, dtype=np.int)

    i_max = np.argmax(As)

    A_max = As[i_max]
    if np.sum(As == A_max) > 1:
        for i in range(N):
            print(A_max)
    else:
        A_max2 = np.max(As[As != A_max])
        for i in range(N):
            if i == i_max:
                print(A_max2)
            else:
                print(A_max)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a_s_with_index = [(i, int(input())) for i in range(n)]
a_s = [a_i[1] for a_i in a_s_with_index]
a_s.sort()

for i, a_i in a_s_with_index:
    if  a_i == a_s[n-1]:
        a_max_i = n-2
    else:
        a_max_i = n-1

    print(a_s[a_max_i])

Solution (Python):
import sys
import numpy as np
from scipy.misc import comb
sys.setrecursionlimit(1000000)
 
n = int(input())

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

sort_a = sorted(a,reverse=True)

max_a = sort_a[0]
sec_a = sort_a[1]

for i in range(n):
    if a[i] == max_a:
        print(sec_a)
    else:
        print(max_a)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
A=[int(input()) for _ in range(n)]
B=sorted(A,reverse=True)
m1=B[0]
m2=B[1]
for a in A:
    if a==m1:
        print(m2)
    else:
        print(m1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
MAX1, MAX2 = B[-1], B[-2]

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

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

sorted_lst = sorted(lst)
max_1 = sorted_lst[-1]
max_2 = sorted_lst[-2]

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

Solution (Python):
N = int(input())
a_d = {}
for i in range(N):
    a_d[i] = int(input())

sorted_a_d = sorted(a_d.items(),key=lambda x:-x[1])
for i in range(N):
    if(sorted_a_d[0][0]!=i):
        print(sorted_a_d[0][1])
    else:
        print(sorted_a_d[1][1])

Solution (Python):
import sys

N = int(input())
aArr = list(map(int, sys.stdin))
tmpArr = sorted(aArr)
max = tmpArr[-1]
max2nd = tmpArr[-2]
for i in range(len(aArr)):
    if aArr[i] == max:
        print(max2nd)
    else:
        print(max)

Solution (Python):
import copy
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]

a_l = []
for i in range(n):
    a_l.append(a[i][0])
a1 = sorted(a_l)
m1 = a1[-1]
m2 = a1[-2] 

for i in a_l:
    if i == m1:
        print(m2)
    else:
        print(m1)

Solution (Python):
def main():
    N = int(input())
    a = 0
    m = 0
    mi = 0
    mm = 0
    for i in range(N):
        a = int(input())
        if a > m:
            mm = m
            m = a
            mi = i
        elif a > mm:
            mm = a
    for i in range(N):
        if i == mi:
            print(mm)
        else:
            print(m)


main()

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

a_sort = sorted(a, key=lambda x: x[1], reverse=True)

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

Solution (Python):
N = int(input())
A = [int(input()) for n in range(N)]
tmp = A.copy()
A.sort()
fir, sec = A[N-1], A[N-2]
for n in range(N):
  if tmp[n] == fir:
    print(sec)
  else:
    print(fir)

Solution (Python):
n = int(input())
nums = [int(input()) for _ in range(n)]
max_num = max(nums)
max_cnt = len([i for i in nums if i == max_num])

for i in range(n):
  if nums[i] < max_num or max_cnt > 1:
    print(max_num)
  else:
    print(max(set(nums) - set([nums[i]])))

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
maxidx = A.index(max(A))
Amax = max(A)
A.remove(Amax)
A2 = max(A)

for i in range(maxidx):
  print(Amax)
print(A2)
for i in range(maxidx+1,N):
  print(Amax)

Solution (Python):
import copy
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
tmp = copy.copy(a)
tmp.sort()
max_num = tmp[-1]
min_num = tmp[-2]
for i in range(n):
    if a[i] == max_num:
        print(min_num)
    else:
        print(max_num)

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

Solution (Python):
import sys
input=sys.stdin.readline

N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
max_num = B[len(A)-1]

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

Solution (Python):
n=int(input())
a=[int(input())  for i in range(n)]
b=list(a)
b.remove(max(a))
maxa=max(a)
maxb=max(b)
for i in range(n):
    if a[i] != maxa:
        print(maxa)
    else:
        print(maxb)

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

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


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

for i in range(n):
    if a[i] == maxV:
        
        
        
        print(maxV2)
    else:
        print(maxV)

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

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

Solution (Python):


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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

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


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

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

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

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

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

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

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

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

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

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


dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')


ABT = zip(A, B, totAB)
result = 0

sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)


(x >> i) & 1


dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

add = 1 
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)


li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    


org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)




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

"""


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

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

    
if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

if __name__ == "__main__":
    resolve()

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

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

a_sort = sorted(a, reverse=True)

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

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

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

if __name__ == '__main__':
    main()

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

max_all = max(A)

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

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

Solution (Python):

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

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




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

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

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

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

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

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

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

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

Solution (Python):
from copy import copy
from collections import deque

N = int(input())

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

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

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

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

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

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

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

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10


N = int(stdin.readline().rstrip())
A = [int(stdin.readline().rstrip()) for _ in range(N)]

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

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

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

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

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

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

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

Solution (Python):

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

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

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

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

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

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

for i in ANS:
    print(i)

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

s = sorted(a)[::-1]

n1 = s[0]
n2 = s[1]

for i in a:
  if i == n1 and n2 != n1:
    print(n2)
  else:
    print(n1)

Solution (Python):

import sys

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

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

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

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

max_a = max(A)

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

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

else:

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


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

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

MAX = 999999999999999999999999999999999999999999999999999999999999999999999999999

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

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

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

Solution (Python):
import logging

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


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

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

                
if __name__ == "__main__":
    main()

Solution (Python):

N = int(input())


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

sorted = sorted(a)

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

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

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

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

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
AA = sorted(A)
maxAA = max(AA)
for i in A:
    if maxAA == i:
        print(AA[-2])
    else:
        print(maxAA)

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

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

Solution (Python):

p = int(input())
lista = []

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

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

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

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

Solution (Python):
N=int(input())
A=[int(input()) for _ in range(N)]
B=[]
for i in range(N):
    B.append(A[i])
B.sort(reverse=True)

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

Solution (Python):
import sys
sys.setrecursionlimit(100000)

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

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

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

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

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

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

Solution (Python):
import math
input = raw_input

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

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

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

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

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

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


Bs = sorted(As,reverse=True)



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

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

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

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

Solution (Python):
import numpy as np

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

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

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

Solution (Python):



N = int(input())

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

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

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

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

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

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

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

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

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

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

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





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

Solution (Python):


import sys

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

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

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

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

Solution (Python):

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

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

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

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


print(rmax[1])

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

print(lmax[n-2])

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

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

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

SECOND = max(a.values())


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