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)]

sorted_A = sorted(A, reverse=True)

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

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

max_num = max(a)
max_pos = a.index(max(a))
a[max_pos] = -1
second_max = max(a)

for i in range(n):
    if i == max_pos:
        print(second_max)
    else:
        print(max_num)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
first = max(a)
second = 0
flag = a.count(first) > 1

if a.count(first) > 1:
    for i in a:
        print(first)
else:
    for i in a:
        if i != first and second < i:
            second = i
    for i in a:
        print(first if i != first else second)

Solution (Python):
N = int(input())
A = []
for _ in range(N):
    a = int(input())
    A.append(a)
    
num_list = zip(A[:], range(N))
num_list = sorted(num_list, reverse=True)

max_n = num_list[0][0]
max_i = num_list[0][1]
sec_n = num_list[1][0]

for i in range(N):
    if i == max_i:
        print(sec_n)
    else:
        print(max_n)

Solution (Python):

import sys
import heapq

def solve(N: int, A: "List[int]"):
    A1 = max(A)
    A2 = heapq.nlargest(2, A)[-1]
    
    for i in range(0, N):
        if A[i] == A1:
            print(A2)
        else:
            print(A1)
    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):






N = int(input())
a = [0 for _ in range(N)]
for i in range(N):
    a[i] = (i,int(input()))
a = sorted(a, key=lambda x: -x[1])
for i in range(N):
    for j in range(N):
        if a[j][0] == i:
            continue
        print(a[j][1])
        break

Solution (Python):

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

Solution (Python):
from collections import Counter

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

c = Counter(A)

hoge = sorted(A,reverse=True)
if c[hoge[0]] == 1:
    for _ in range(N):
        if A[_] == hoge[0]:
            print(hoge[1])
        else:
            print(hoge[0])
else:
    for _ in range(N):
        print(hoge[0])

Solution (Python):
N=int(input())
listA=[]
for i in range(N):
  listA+=[int(input())]
MAX=max(listA)
listB=sorted(listA)
for i in range(N):
  if listB[-1]==listA[i]:
    print(listB[-2])
  else:
    print(MAX)

Solution (Python):

N = int(input())

A = []

maxi = 0
sec = 0

for i in range(N):

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


    if A[maxi] <= a:
        maxi = i

B = A.copy()

del B[maxi]

sec = max(B)




for i in range(N):

    if i == maxi:
        print (sec)

    else:
        print (A[maxi])

Solution (Python):
n = int(input())
m = mi = 0
a = []
for i in range(n):
    x = int(input())
    a.append(x)
    if x == m:
        mi += 1
    if x > m:
        m = x
        mi = 1
if mi == 1:
    m2 = 0
    for i in range(n):
        if a[i] == m:
            continue
        if a[i] > m2:
            m2 = a[i]
    for i in range(n):
        if a[i] == m:
            print(m2)
        else:
            print(m)
else:
    for i in range(n):
        print(m)

Solution (Python):
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))
b = a[:]
b.sort(reverse=True)
for i in range(n):
    if a[i] == b[0]:
        print(b[1])
    else:
        print(b[0])

Solution (Python):
N = int(input())
A = [int(input()) for x in range(N)]
M = sorted(A,reverse=True)
M1 = M[0]
M2 = M[1]
for i in range(N):
    if A[i] <= M2:
        print(M1)
    else:
        print(M2)

Solution (Python):
import numpy as np
import sys
import math
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = [I() for i in range(N)]

num = max(a)
cnt = a.count(num)
if cnt == 1:
  b = sorted(a)
  bb = b[-2]
  for i in a:
    if i == num:
      print(bb)
    else:
      print(num)
else:
  for i in range(N):
    print(num)

Solution (Python):




def main():
    N = int(input())
    A = []
    for _ in range(N):
        A.append(int(input()))
    B = sorted(A)
    m1 = B.pop()
    m2 = B.pop()
    for i in range(N):
        if A[i] == m1:
            print(m2)
        else:
            print(m1)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = []
for _ in range(N):
  A.append(int(input()))
ma = A.index(max(A))
maxA = max(A)
ma2 = sorted(A)[-2]
for i in range(N):
  if i == ma:
    print(ma2)
  else:
    print(maxA)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
B = sorted(A)
M, M2 = B[-1], B[-2]

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

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

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

def main():
    n = ri()
    la = []
    n_best = 0
    best = 0
    best2 = 0
    for i in range(n):
        a = ri()
        la.append(a)
        if best < a:
            best2 = best
            best = a
            n_best = 1
        elif best == a:
            n_best += 1
        else:
            best2 = max(best2, a)
    for a in la:
        if a < best:
            print(best)
        else:
            if n_best > 1:
                print(best)
            else:
                print(best2)


if __name__ == '__main__':
    main()

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

S = []

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

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

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

for i in range(1,N):
    r = max(r, S[N-i])
    R[N-i-1] = r

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

Solution (Python):


import math
import sys
import itertools
import numpy as np


n =int(input())

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

max_val = 0
max_num = -1
max_next = 0

for i in range(n):
    if a[i] > max_val:
        max_next = max_val
        max_val = a[i]
        max_num = i
    elif a[i] == max_val:
        max_next = max_val
    elif a[i] > max_next:
        max_next = a[i]

for i in range(n):
    if i == max_num:
        print(max_next)
    else:
        print(max_val)

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

a = []
maxA = 0
secondA = 0

for i in range(n):
    a.append(int(input()))
    if a[i] > maxA:
        secondA = maxA
        maxA = a[i]
    elif a[i] > secondA:
        secondA = a[i]

for i in range(n):
    if a[i] == maxA:
        print("{}".format(secondA))
    else:
        print("{}".format(maxA))

Solution (Python):
N=int(input())
A=[0]*N
maxN=0
secondN=0
for i in range(N):
  A[i]=int(input())
  if A[i]>=maxN:
    secondN=maxN
    maxN=A[i]
  elif A[i] > secondN:
    secondN=A[i]
for i in range(N):
  if A[i]==maxN:
    print(secondN)
  else:
    print(maxN)

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

c = sorted(a, reverse = True)

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

Solution (Python):
n = int(input())
a = list()
for i in range(n):
    a.append(int(input()))
c = a.index(max(a))
m = max(a)
a.sort()
d = a[-2]
for i in range(n):
    if i == c:
        print(d)
    else:
        print(m)

Solution (Python):










import sys
import os
from io import IOBase, BytesIO


def main():
    n = int(input())
    arr = [0] * n
    for i in range(n):
        arr[i] = int(input())
    tmp = sorted(arr)
    mx = tmp[-1]
    mx1 = tmp[-2]
    for i in range(n):
        if arr[i] == mx:
            print(mx1)
        else:
            print(mx)


BUFSIZE = 8192


class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill():
            pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill()
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        py2 = round(0.5)
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2 == 1:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s: self.buffer.write(s.encode("ascii"))
            self.read = lambda: self.buffer.read().decode("ascii")
            self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)


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


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


def input(): return sys.stdin.readline().strip()


if __name__ == "__main__":
    main()

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

m = max(a)

if a.count(m) > 1:
    for i in range(n):
        print(m)
else:
    b = sorted(set(a))
    m2 = b[-2]
    for i in range(n):
        if a[i] != m:
            print(m)
        else:
            print(m2)

Solution (Python):
def solve(n, xs):
	ys = sorted(xs, reverse=True)
	m1, m2 = ys[0], ys[1]
	for x in xs:
		if x == m1:
			yield m2
		else:
			yield m1


def main():
	n = int(input())
	xs = [int(input()) for _ in range(n)]
	for a in solve(n, xs):
		print(a)


if __name__ == "__main__":
	main()

Solution (Python):
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
b = sorted(a, reverse=True)
mx_a = max(a)
for i in range(n):
    print(b[1] if a[i] == mx_a else mx_a)

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

max1 = max(A)
max2 = sorted(A)[-2]

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

Solution (Python):
N=list(map(int, input().split()))
A=[0]*N[0]
L=0
M=0
for i in range(N[0]):
    A[i]=int(input())
    if A[i]>M:
        if A[i]>L:
            M=L
            L=A[i]
        else:
            M=A[i]
for i in range(N[0]):
    if A[i]==L:
        print(M)
    else:
        print(L)

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

def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    max_val = max(A)
    AA = A[:]
    AA.remove(max_val)
    second_max_val = max(AA)
    for a in A:
        if a != max_val:
            print(max_val)
        else:
            print(second_max_val)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
in_list = []
for i in range(N):
    in_list.append(int(input()))
in_max = max(in_list)
in_max2 = sorted(in_list)[-2]
for i in in_list:
    if i!=in_max:
        print(in_max)
    else:
        print(in_max2)

Solution (Python):

import sys
import heapq

def solve(N: int, A: "List[int]"):
    max_A = max(A)
    next_max_A = heapq.nlargest(2, A)[-1]
    for i in range(N):
        if A[i] == max_A:
            print(next_max_A)
        else:
            print(max_A)
    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):

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


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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
m = -1
m2 = -1
for a in A:
    if a >= m:
        m2 = m
        m = a
    elif a < m and a >= m2:
        m2 = a
for i in range(N):
    if A[i] != m:
        print(m)
    else:
        print(m2)

Solution (Python):
import numpy as np
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))
A = np.array(A)
s = np.argsort(A)
l = s[-1]
L = A[l]
for i in range(N):
    if i != l:
        print(L)
    else:
        print(A[s[-2]])

Solution (Python):
n=int(input())
a=[]
for _ in range(n):
  a+=[int(input())]
max_a=0
max_2=0
max_i=[]
for i in range(n):
  if a[i]>=max_a:
    max_2=max_a
    max_a=a[i]
    max_i=[i]
  elif a[i]>max_2:
      max_2=a[i]
for i in range(n):
  if i==max_i[0]:
    print(max_2)
  else:
    print(max_a)

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

mx = max(A)
mx2 = sorted(A)[-2]
for a in A:
    print(mx) if a != mx else print(mx2)

Solution (Python):
N=int(input())
A=[]
for n in range(N):
    a=int(input())
    A.append(a)
dai=max(A)
basyo=A.index(max(A))
A.pop(basyo)
for n in range(N):
    if n==basyo:
        print(max(A))
    else:
        print(dai)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
    
sorted_A = sorted(A, reverse=True)
first = sorted_A[0]
second = sorted_A[1]

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

Solution (Python):

import sys
import heapq
sys.setrecursionlimit(4100000)
n = int(input())
a = [int(input()) for i in range(n)]
heap = []
for i in range(n):
    heapq.heappush(heap,-a[i])
for i in range(n):
    temp = heapq.heappop(heap)
    if -a[i] == temp:
        temp2 = heapq.heappop(heap)
        print(-temp2)
        heapq.heappush(heap,temp)
        heapq.heappush(heap,temp2)
    else:
        print(-temp)
        heapq.heappush(heap,temp)

Solution (Python):

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

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

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

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

max_num = max(num_list)
max_count = num_list.count(max_num)

if max_count > 1:
    for i in num_list:
        print(max_num)
else:
    max_index = num_list.index(max_num)
    num_list[max_index] = -1
    second_num = max(num_list)
    num_list[max_index] = max_num

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

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


max_1 = [0, 0]
max_2 = [0, 0]

for i in range(N):
	a_i = int(input())
	if a_i > max_2[0]:
		max_2[0] = a_i
		max_2[1] = i
	if max_1[0] < max_2[0]:
		tmp = max_1
		max_1 = max_2
		max_2 = tmp
	
		
for i in range(N):
	if i == max_1[1]:
		print(max_2[0])
	else:
		print(max_1[0])

Solution (Python):
import copy
n = int(input())
a = [int(input()) for i in range(n)]
ans = max(a)
n_a = copy.deepcopy(a)
n_a.remove(ans)
s_ans = max(n_a)
if s_ans == ans:
    for i in a:
        print(ans)
else:
    for i in a:
        if i == ans:
            print(s_ans)
        else:
            print(ans)

Solution (Python):
import copy

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

argmax_A = A.index(max(A))
B = copy.copy(A)
del(B[argmax_A])
ans = max(B)

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

Solution (Python):
n = int(input())
l = [[int(input()),i] for i in range(n)]
l.sort(key=lambda x:x[0])


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

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

B = copy.copy(A)
maxx = max(A)
A.pop(A.index(maxx))
second_max = max(A)

for i in range(N):
    if(B[i] == maxx):
        print(second_max)
    else:
        print(maxx)

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

left = [-1 for _ in range(N)]
right = [-1 for _ in range(N)]

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

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

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

Solution (Python):
n = int(input())
seq = []
maxi = 0
secondmax = 0
double = 0
for i in range(n):
    num = int(input())
    seq.append(num)
    if num > maxi:
        secondmax = maxi
        maxi = num
        double = 0
    elif num == maxi:
        double += 1
    elif secondmax < num < maxi:
        secondmax = num

for i in range(n):
    if seq[i] == maxi and double != 0:
        print(maxi)
    elif seq[i] == maxi and double == 0:
        print(secondmax)
    else:
        print(maxi)

Solution (Python):
n=int(input())
li=[]
for i in range(n):
    li.append(int(input()))
    
a=[max(li),li.index(max(li))]
li2=li[:]
v=sorted(li2)[-2]
b=[v,li.index(v)]

for i in li:
    if i==a[0]:
        print(b[0])
    else:
        print(a[0])

Solution (Python):

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

    maxs = sorted(As, reverse=True)[:2]

    for a in As:
        if a in maxs:
            print(maxs[0] if a != maxs[0] else maxs[1])
        else:
            print(maxs[0])


if __name__ == '__main__':
    main()

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

B = sorted(A)

max_index = B[N-1][1] + 1
max_2_index = B[N-2][1]+1
for j in range(N):
    if j + 1 == max_index:
        print(B[N-2][0])
    else:
        print(B[N-1][0])

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

aa = sorted(a, reverse=True) 

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

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

m = max(a)
idx = a.index(max(a))

for i in range(n):
    if i == idx:
        b = copy.copy(a)
        b.pop(idx)
        print(max(b))
    else:
        print(m)

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

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

buff = A.copy()
buff.sort()
buff = buff[::-1]

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

Solution (Python):
import numpy as np

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

maxA = max(A)
A_array = np.array(A)
maxi = np.argmax(A_array)


B = A.copy()
B.pop(maxi)

for i in range(N):
  if i != maxi :
    print(maxA)
  else :
    print(max(B))

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

maxim = max(As)
counter = As.count(maxim)

if counter > 1:
    for a in As:
        print(maxim)
else:
    for a in As:
        if a == maxim:
            print(max([i for i in As if i != a]))
        else:
            print(maxim)

Solution (Python):
N = int(input())
big = 0
big2 = 0
for i in range(N):
    A = int(input())
    if A > big:
        memo = i
        big = A
    elif A >= big2:
        big2 = A
for i in range(N):
    if i == memo:
        print(big2)
    else:
        print(big)

Solution (Python):


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

sorted_a = sorted(A)
max_a, max2_a = sorted_a[-1], sorted_a[-2]

for i in range(N):
    if A[i] == max_a:
        print(max2_a)
    else:
        print(max_a)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
As = sorted(a, reverse=True)
n1 = As[0]
n2 = As[1]
for ai in a:
    if ai == n1:
        print(n2)
    else:
        print(n1)

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

no1 = max(a)
place_ma = a.index(no1)

a.pop(place_ma)

no2 = max(a)

for i in range(n):
    if i == place_ma:
        print(no2)
    else:
        print(no1)

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

max_A = max(A)
B = copy.deepcopy(A)
B.remove(max_A)
max_B = max(B)


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

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

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

a_max = max(A)

a_max_count = 0

a_second = 0

for n in A:
    if n != a_max:
        a_second = max(n, a_second)
    else:
        a_max_count += 1

if a_max_count >= 2:
    for _ in A:
        print(a_max)
else:
    for n in A:
        if n == a_max:
            print(a_second)
        else:
            print(a_max)

Solution (Python):

n = int(input())

highest = 0
higher = 0
multiple = False


an = [0]*n

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

    if  highest < t :
        higher = highest
        highest = t
        multiple = False

    elif highest == t:
        multiple = True

    elif higher < t:
        higher = t


if multiple:
    for _ in range(n):
        print(highest)

else:
    for a in an:
        print( highest if a != highest else higher )

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

Solution (Python):
n=int(input())
fir=0
sec=0
a=[]
for _ in range(n):
    ak=int(input())
    a.append(ak)
    if ak>fir:
        fir=ak
    elif ak>sec:
        sec=ak
for ak in a:
    if ak==fir:
        print(sec)
    else:
        print(fir)

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

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

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

Solution (Python):
n = int(input())
k, m=0, 0
p = []
for i in range(n):
    l = int(input())
    if l > m:
        m = l
        k = i
    p.append(l)
p.remove(p[k])
j = 0
for i in p:
    if i > j:
        j = i
for i in range(n):
    if i == k:
        print(j)
    else:
        print(m)

Solution (Python):

n = int(input())
a = [int(input()) for _ in range(n)]
mx = max(a)
b = sorted([x for x in a], reverse=True)
for x in a:
    print(b[x == b[0]])

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

Solution (Python):
n = int(input())
L = [int(input()) for _ in range(n)]
sortedL = sorted(L)
max1 = sortedL[-1]
max2 = sortedL[-2]

count = L.count(max1)

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

Solution (Python):
n = int(input())
arr = []
max_index = 0
max_num = 0
for i in range(n):
  tmp = int(input())
  arr.append(tmp)
  if tmp > max_num:
    max_num = tmp
    max_index = i

list.sort(arr, reverse=True)
if arr[0] == arr[1]:
  for i in range(n):
    print(max_num)

else:
  for i in range(n):
    if max_index == i:
      print(arr[1])
    else:
      print(max_num)

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

MAX = max(A)
cnt = 0
for i in range(N):
    if MAX == A[i]:
        cnt += 1

if cnt >= 2:
    for i in range(N):
        print(MAX)
    exit()
    
B = sorted(A)
nMAX = B[-2]
for i in range(N):
    if A[i] == MAX:
        print(nMAX)
    else:
        print(MAX)

Solution (Python):
N = int(input())
Second = 0
MaxA = 0

for x in range(N):
    A = int(input())
    if MaxA <= A:
        Second = MaxA
        MaxA = A
        i = x
        continue
    if Second < A:
        Second = A

for x in range(N):
    if x == i:
        print(Second)
    else:
        print(MaxA)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
max_a=max(a)
max_a_index=a.index(max_a)
a.pop(max_a_index)
semimax_a=max(a)
for i in range(n):
  if i!=max_a_index:
    print(max_a)
  else:
    print(semimax_a)

Solution (Python):
N = int(input())
A = [0] * N

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

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

B.sort()
A0 = B[-1]
A1 = B[-2]

for i in A:
  if i == A0:
    print(A1)
  else:
    print(A0)

Solution (Python):
import bisect
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

N=II()
INL=[]
for i in range(N):
    INL.append(II())
INL2=list(sorted(INL))
M = INL2[-1]

for i in INL:
    if i == M:
        if INL2[-2] == M:
            print(M)
        else:
            print(INL2[-2])
    else:
        print(M)

Solution (Python):
N = int(input())
A = [0] * N
f = s = 1
fi = si = 0
for i in range(N):
    A[i] = int(input())
    if A[i] > f:
        s = f
        si = fi
        f = A[i]
        fi = i
    elif A[i] > s:
        s = A[i]
        si = i
for i in range(N):
    if i != fi:
        print(f)
    else:
        print(s)

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

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

ma = max(A)
B = A.copy()
B.pop(A.index(ma))
sa = max(B)

for i in range(N):
    if A[i] == ma:
        print(sa)
    else:
        print(ma)

Solution (Python):
N=int(input())
A=[]
max=0
count=0
point=0
for i in range(N):
    x=int(input())
    A.append(x)
    if x>max:
        max=x
        count=0

    if x==max:
        count+=1
        point=i

A.sort(reverse=True)
if count==0:
    for i in range(N):
        print(max)
else:
    for i in range(N):
        if i!=point:
            print(max)
        else:
            print(A[1])

Solution (Python):
N = int(input())
b = [int(input()) for _ in range(N)]
a = sorted(b)
ma = a[-1]
sa = a[-2]
for c in b:
    if c == ma:print(sa)
    else:print(ma)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
maxa = max(A)
if A.count(maxa) > 1:
  for _ in range(N):
    print(maxa)
else:
  for i in range(N):
    if A[i] == maxa:
      if i == N - 1:
        print(max(A[: N - 1]))
      else:
        print(max(A[: i] + A[i + 1: ]))
    else:
      print(maxa)

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

max_1 = sorted(lis)[-1]
max_2 = sorted(lis)[-2]

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

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

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

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
b=a.copy()
ma=max(a)
a.remove(ma)
ma2 = max(a)
for i in range(n):
    if b[i]==ma:
        print(ma2)
    else:
        print(ma)

Solution (Python):
n=int(input())
al=[int(input()) for _ in range(n)]
b=al[:]
b=sorted(b,reverse=True)
m=max(al)
if al.count(m)>1:
    for _ in range(n):
        print(m)
else:
    for a in al:
        if a==m:
            print(b[1])
        else:
            print(m)

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

Solution (Python):
from collections import Counter
N = int(input())
A = [int(input()) for i in range(N)]
sortA = sorted(A)
maxA = sortA[-1]
count_a = Counter(A)
if count_a[maxA] >= 2:
    for i in range(N):
        print(maxA)
elif count_a[maxA] == 1:
    for a in A:
        if a == maxA:
            print(sortA[-2])
        else:
            print(maxA)

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

num_max = max(li)
cnt = li.count(num_max)

if cnt >= 2:
    for i in range(n):
        print(num_max)
else:
    s = li.index(num_max)
    for i in range(n):
        if i != s:
            print(num_max)
        else:
            li[i] = 0
            print(max(li))

Solution (Python):


def submit():
    
    n = int(input())

    first = 0
    second = 0

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

        if alist[-1] > first:
            second = first
            first = alist[-1]
        elif alist[-1] > second:
            second = alist[-1]

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


    return


if __name__ == '__main__':
    submit()

Solution (Python):
import sys

fin = sys.stdin.readline
N = [int(elem) for elem in fin().split()][0]
a_list = [[int(elem) for elem in fin().split()][0] for _ in range(N)]

sorted_a = sorted(a_list)
all_max = sorted_a[-1]
all_second = sorted_a[-2]

for i in range(N):
    if a_list[i] == all_max:
        print(all_second)
    else:
        print(all_max)

Solution (Python):
import sys

n= int(sys.stdin.readline())
ai= [ int(sys.stdin.readline()) for _ in range(n) ]
m= max(ai)
j=ai.index(m)
for i in range(n):
    if i==j: print( max(ai[:i]+ai[i+1:]) )
    else: print(m)