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

import heapq
m1, m2 = heapq.nlargest(2,A)

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

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

first = 0
second = 0
for a in a_list:
    if a >= first:
        second = first
        first = a
    else:
        if a>= second:
            second = a

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

Solution (Python):
N = int(input())
A = [int(input()) for i in range(N)]
n = sorted(A, reverse=True)
x, x_ = n[0], n[1]

for a in A:
    if a == x:
        print(x_)
    else:
        print(x)

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
A=[]

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

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

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

tmp=sorted(A)[::-1]
if tmp[0]==tmp[1]:
    for _ in range(N):
        print(tmp[0])
else:
    for i in range(N):
        if A[i]==tmp[0]:
            print(tmp[1])
        else:
            print(tmp[0])

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

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


acc1 = [0]*(n+1)
acc2 = [0]*(n+1)

for i in range(n):
    acc1[i+1] = max(acc1[i],a[i])
    acc2[-i-2] = max(acc2[-i-1],a[-i-1])

for i in range(n):
    print(max(acc1[i], acc2[i+1]))

Solution (Python):
N = int(input())
lst = []
for i in range(0, N):
	lst.append([i, int(input())])
lst.sort(lambda v: v[1], reverse = True)
index = lst[0][0]
for i in range(0, N):
	if i == index:
		print(lst[1][1])
	else:
		print(lst[0][1])

Solution (Python):
n=int(input())
a=[]
for _ in range(n):
    x=int(input())
    a.append(x)
import numpy as np
A=np.array(a)
b=A.argsort()[::-1]
c=b[0]
for i in range(n):
    if i==c:
        print(a[b[1]])
    else:
        print(a[c])

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

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

Solution (Python):
N, *A = map(int, open(0).read().split())

a, b = sorted(A, reverse=True)[:2]
for x in A:
    if x != a:
        print(a)
    else:
        print(b)

Solution (Python):
import heapq
N = int(input())
A = []
for i in range(N):
    A.append(int(input())) 
m = heapq.nlargest(2, A)
for a in A:
    if a == m[0]:
        print(m[1])
    else:
        print(m[0])

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

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

Solution (Python):
N = int(input())
A = []
max_a = 0
max2_a = 0
for i in range(N):
	a = int(input())
	A.append(a)
	if a >= max_a:
		max2_a = max_a
		max_a = a
	elif a >= max2_a:
		max2_a = a

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

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

ai = sorted(a_)
for i in range(n):
    if a_[i]==ai[-1]:
        print(ai[-2])
    else:
        print(ai[-1])

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

sortA = sorted(A)
maxA = sortA[-1]
next_maxA = sortA[-2]

for i in range(N):
    if A[i] == maxA:
        print(next_maxA)
    else:
        print(maxA)

Solution (Python):
n=int(input())
a=[int(input()) for _ in [0]*n]
b,c=sorted(a,reverse=True)[0:2]
for A in a:
    if A==b:
        print(c)
    else:
        print(b)

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

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

max_left = list()
max_right = list()

for i in range(n):
    if i == 0:
        max_left.append(As[i])
        max_right.append(As[-1 - i])
    
    else:
        max_left.append(max(max_left[-1], As[i]))
        max_right.append(max(max_right[-1], As[-1-i]))

max_right.reverse()

for i in range(n):
    if i == 0:
        print(max_right[i + 1])
    elif i == n - 1:
        print(max_left[i - 1])
    else:
        print(max(max_left[i - 1], max_right[i + 1]))

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

mA = max(A)
sA = sorted(A, reverse=True)

for a in A:
    if not a == mA:
        print(mA)
    else:
        print(sA[1])

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


MAX=[-1]

for n in A:
    MAX.append(max(n,MAX[-1]))

MAXINV=[-1]

for n in A[::-1]:
    MAXINV.append(max(n,MAXINV[-1]))

MAXINV=[-1]+MAXINV[::-1]


for i in range(1,N+1):
    print(max(MAX[i-1],MAXINV[i+1]))

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
aa = []

for i in range(n):
    a = ni()
    aa.append(a)

bb = sorted(aa)
o = bb[-1]
p = bb[-2]

for i in range(n):
    k = p if aa[i] == o else o
    print(k)

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

B = sorted(A)

tmp1 = B[-1]
tmp2 = B[-2]

for i in range(N):
    if A[i] == tmp1:
        print(tmp2)
    else:
        print(tmp1)

Solution (Python):
from itertools import accumulate

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

Ls = [0] + list(accumulate(As, max))
Rs = list(accumulate(As[::-1], max))[::-1] + [0]

anss = [max(Ls[i], Rs[i+1]) for i in range(N)]
print('\n'.join(map(str, anss)))

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

mx1 = max(A)
A_ = A[:]
A_.remove(mx1)
mx2 = max(A_)
for a in A:
    if a == mx1:
        print(mx2)
    else:
        print(mx1)

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

nums = []
maxN = 0
secondN = 0

for i in range(n):
    num = int(input())
    nums.append(num)
    if maxN < num:
        secondN = maxN
        maxN = num
    elif secondN < num:
        secondN = num

for i in range(n):
    if nums[i] != maxN:
        print(maxN)
    else:
        if secondN == maxN:
            print(maxN)
        else:
            print(secondN)

Solution (Python):
from collections import defaultdict, deque
import sys

sys.setrecursionlimit(100000)

MIN = -10 ** 9
MAX = 10 ** 9 + 7


def main():
    N = int(input())
    A = [
        int(input())
        for _ in range(N)
    ]
    
    b = sorted(A)
    l1, l2 = b[-1], b[-2]
    if l1 == l2:
        for i in range(N):
            print(l1)
        return
    for a in A:
        if a == l1:
            print(l2)
        else:
            print(l1)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
l=[int(input()) for i in range(n)]
L=sorted(l)
L.reverse()
x=L[0];y=L[1]
for i in range(n):
  if l[i]!=x:
    print(x)
  else:
    print(y)

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

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

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

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

B = sorted(A)
s = ""
for b in A:
    if b != B[-1]:
        s += str(B[-1]) + "\n"
    else:
        s += str(B[-2]) + "\n"
print(s)

Solution (Python):
def main():

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    """ initialize variables and set inputs
    
        
        
    
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    if __name__ != '__main__':
        
        sys.stdin = open('sample.txt')

    
    

    N = int(r())
    A = [int(r()) for _ in range(N)]
    B = sorted(A)
    if B[-1] == B[-2]:
        for _ in range(N):
            print(B[-1])
    else:
        for a in A:
            if a == B[-1]:
                print(B[-2])
            else:
                print(B[-1])
    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """

if __name__ == '__main__':
    main()

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

A_sort = sorted(A, reverse=True)
A_1st = A_sort[0]
A_2nd = A_sort[1]

for a in A:
    if a == A_1st:
        print(A_2nd)
    else:
        print(A_1st)

Solution (Python):
import sys

input = sys.stdin.readline

n = int(input())
a_list = [int(input()) for _ in range(n)]
sorted_list = sorted(a_list, reverse=True)
for a in a_list:
    check = sorted_list[0]
    if a == check:
        print(sorted_list[1])
    else:
        print(check)

Solution (Python):
import math
def Solve():
    L=[]
    N=int(input())
    for i in range(N):
        L.append(int(input()))

    mx=max(L)
    if L.count(mx)-1:
        for _ in L:
            print(mx)
    else: 
        for i,a in enumerate(L):
            if a==mx:
                K=L[:i]+L[i+1:]
                print(max(K))
            else:
                print(mx)

Solve()

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
m1,m2,mi=0,0,-1
for i,a in enumerate(A):
    if a>m1:
        m1=a
        mi=i
    elif a>m2:
        m2=a
for i in range(N):
    print(m2 if i==mi else m1)

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

biggest = 0
biggest_i = 0
next_biggest = 0
for i in range(n):
    x = int(input())
    if x >= biggest:
        next_biggest = biggest
        biggest = x
        biggest_i = i
    elif x >= next_biggest:
        next_biggest = x

for i in range(n):
    if i == biggest_i:
        print(next_biggest)
    else:
        print(biggest)

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a_sort = sorted(a)
a_max = a_sort[-1]
multiple_max = a_sort[-1] == a_sort[-2]
if not multiple_max:
    a_second = a_sort[-2]

for item in a:
    if item == a_max and not multiple_max:
        print(a_second)
    else:
        print(a_max)

Solution (Python):
import sys

input = sys.stdin.readline

N = int(input().strip())
As = [(int(input().strip()), i) for i in range(N)]
As.sort(reverse=True)
exception_index = As[0][1]
for i in range(N):
    if i == exception_index:
        print(As[1][0])
    else:
        print(As[0][0])

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

import collections
A_list = []
A_collections = collections.Counter()
for i in range(N):
    A_list.append(int(input()))
    A_collections[A_list[-1]] += 1
    
max_num = max(A_list)
max_count = A_collections[max_num]
if(max_count > 1):
    for i in range(N):
        print(max_num)
else:
    A_collections.pop(max_num)
    for i in range(N):
        if(A_list[i] == max_num):
            print(max(A_collections))
        else:
            print(max_num)

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

Solution (Python):

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

A_sort = sorted(A)
A_sort = A_sort[::-1]

maxim = A_sort[0]
maxim2 = A_sort[1]

flag = False
if maxim == maxim2:
    flag = True

for i in range(N):
    if flag:
        print(maxim)
    else:
        if A[i] == maxim:
            print(maxim2)
        else:
            print(maxim)

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

f = 0
s = 0
index = 0
for i in range(N):
    a = int(input())
    if a > f:
        index = i
        s = f
        f = a
    elif a > s:
        s = a

for i in range(N):
    if i == index:
        print(s)
    else:
        print(f)

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

sA.sort(reverse=True)

f = sA[0]
s = sA[1]

for i in range(N):
    if A[i] == f:
        print(s)
    else:
        print(f)

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

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

m = max(A)
ind = A.index(m)
A_ = A[:ind]+A[ind+1:]
m_ = max(A_)

for i in range(N):
	if i != ind:
		print(m)
	else:
		print(m_)

Solution (Python):
n=int(input())
da=[int(input()) for i in range(n)]
x,y=0,0
for i in range(n):
    if da[i]>x:
        y=x
        x=da[i]
    else:
        if y<da[i]:
            y=da[i]

for i in range(n):
    if da[i]==x:
        print(y)
    else:
        print(x)

Solution (Python):
N = int(input())
A = list(int(input()) for i in range(N))
B = sorted(A)
for i in A:
    if i == B[-1]:
        print(B[-2])
    else:
        print(B[-1])

Solution (Python):
a = [int(input()) for i in range(int(input()))]
a_s = sorted(a)[::-1]
mx, submx = a_s[0], a_s[1]
cnt = a.count(mx)
for n in a:
    if n!=mx: print(mx)
    elif cnt>1: print(mx)
    else: print(submx)

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

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

    left = [0 for i in range(N)]
    right = [0 for i in range(N)]
    left[0] = A[0]
    right[-1] = A[-1]
    for i in range(1, N):
        left[i] = max(left[i-1], A[i])
        right[N-1-i] = max(right[N-i], A[N-1-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]))


if __name__ == "__main__":
    main()

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[-1]:
        print(b[-2])
    else:
        print(b[-1])

Solution (Python):




import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

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

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


if __name__ == '__main__':
    main()

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

import numpy as np

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

from_left = np.maximum.accumulate(A)
from_right = np.maximum.accumulate(A[::-1])[::-1]

from_left, from_right

from_left = [0] + from_left.tolist()[:-1]
from_right = from_right[1:].tolist() + [0]

for x,y in zip(from_left, from_right):
    print(x if x > y else y)

Solution (Python):
n=int(input())
M=0
m=0
a_list=[]
for i in range(n):
    a=int(input())
    a_list.append(a)
    if a>M:
        temp=M
        M=a
        m=temp
    else:
        if a>m:
            m=a
for i in range(n):
    if a_list[i]==M:
        print(m)
    else:
        print(M)

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

import copy
b = copy.deepcopy(a)
b.sort(reverse=True)

m1 = b[0]
m2 = b[1]

for i in range(n):
    if a[i] == m1:
        print(m2)
    else:
        print(m1)

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

L=[]
for i in range(N):
    n=int(input())
    L.append(n)
A=copy.deepcopy(L)
A.sort(reverse=True)


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

Solution (Python):
def solve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    B = sorted(A)
    if B[-1]==B[-2]:
        for i in range(N):
            print(B[-1])
    else:
        for i in range(N):
            if A[i]==B[-1]:
                print(B[-2])
            else:
                print(B[-1])
    
solve()

Solution (Python):
import heapq

def solve():
  N = int(input())
  A = [int(input()) for i in range(N)]
  first, second = heapq.nlargest(2, A)
  for a in A:
    print(first if a != first else second)

solve()

Solution (Python):
import numpy as np
N=int(input())
lst = []
for i in range(N):
    a = int(input())
    lst.append(a)

fst = np.max(lst)
fst_idx = np.argmax(lst)
lst.remove(fst)
snd = np.max(lst)

for i in range(N):
    if i == fst_idx:
        print(snd)
    else:
        print(fst)

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

B = sorted(A)
maxval = max(A)

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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


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


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = []
    A_ = A[:]
    A_.sort(reverse=True)
    for a in A:
        if a == A_[0]:
            ans.append(A_[1])
        else:
            ans.append(A_[0])
    return '\n'.join(map(str, ans))


def main():
    N = read_int()
    A = [read_int() for _ in range(N)]
    print(slv(N, A))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
fir,sec = -1,-1
fi = -1
for i,x in enumerate(a):
    if x > fir:
        fir,fi = x,i
    elif x > sec:
        sec = x
for i in range(n):
    if i == fi:
        print(sec)
        continue
    print(fir)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
a_ = sorted(a)
m_1 = a_[-1]
m_2 = a_[-2]

for i in a:
    if i == m_1:
        print(m_2)
    else:
        print(m_1)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
s = sorted(a, reverse=True)[:2]
for x in a:
    if x == s[0]:
        print(s[1])
    else:
        print(s[0])

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

k = max(range(N), key=lambda i: A[i])
for i in range(N):
    if i != k:
        print(A[k])
    else:
        print(
            max(
                max(A[:k], default=0),
                max(A[(k + 1):], default=0)
            )
        )

Solution (Python):

N = int(input())
nums = []
for _ in range(N):
    nums.append(int(input()))
temp = nums[:]
temp.sort(reverse=True)
ma = temp[0]
ma2 = temp[1]

for i in nums:
    if i == ma:
        print(ma2)
    else:
        print(ma)

Solution (Python):

import sys
import copy


def solve(N: int, A: "List[int]"):
    B = copy.deepcopy(A)
    B.sort(reverse = True)
    B1 = B[0]
    B2 = B[1]

    for i in A:
        if i == B1:
            print(B2)
        else:
            print(B1)
    
    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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

A_sorted = sorted(A, reverse=True)
max_ = A_sorted[0]

for i in A:
	if i != max_:
		print(max_)
	else:
		print(A_sorted[1])

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

n = getN()

nums = []
for i in range(n):
    nums.append(getN())

mx = 0
nxt = -1
dup = 1
for num in nums:
    if num == mx:
        dup += 1
    elif num > mx:
        nxt = mx
        mx = num
        dup = 1
    else:
        if num > nxt:
            nxt = num

if dup >= 2:
    for i in range(n):
        print(mx)

else:
    for num in nums:
        if num == mx:
            print(nxt)
        else:
            print(mx)

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

b = sorted(a, reverse=True)

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

Solution (Python):
import sys,collections as cl,bisect as bs,math
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): 
	return list(map(int,input().split()))
def m(): 
	return map(int,input().split())
def onem(): 
	return int(input())
def s(x): 
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): 
	return " ".join(map(str,x))
def max2(x): 
	return max(map(max,x))

n = onem()

a = []
b = []
for i in range(n):
	k = onem()
	a.append(k)
	b.append(k)

a.sort(reverse = True)

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

Solution (Python):
import copy
N = int(input())
A = []
for _ in range(N):
  A.append(int(input()))
a = max(A)
AA = copy.deepcopy(A)
AA.remove(a)
b = max(AA)

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

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

ruiseki1 = [0] * (n+1)
ruiseki2 = [0] * (n+1)

for i in range(n):
    ruiseki1[i+1] = max(ruiseki1[i], a[i])
for i in range(n)[::-1]:
    ruiseki2[i] = max(ruiseki2[i+1], a[i])   


for i in range(n):
    print(max(ruiseki1[i], ruiseki2[i+1]))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n=I()
a,b=[],[]
for i in range(n):
    a.append(I())
b=sorted(a)[::-1]
M=b[0]
m=b[1]
ans=0
for i in range(n):
    if a[i]==M:
        ans=m
    else:
        ans=M
    print(ans)

Solution (Python):
inf = 10 ** 9
n = int(input())
a = [int(input()) for x in range(n)]
m1 = -inf
m2 = -inf
for i in range(n):
    if a[i] >= m1:
        m2 = m1
        m1 = a[i]
    elif a[i] > m2:
        m2 = a[i]
for i in range(n):
    if m1 != a[i]:
        print(m1)
    else:
        print(m2)

Solution (Python):
import sys
fastinput = sys.stdin.readline
N=int(fastinput())
A=[int(fastinput()) for i in range(N)]
B = [a for a in A]
B.sort(reverse=True)
First = B[0]
Second = B[1]
Findex = A.index(First)
if First == Second:
    for i in range(N):print(First)
else:
    for a in A:
        if a==First:print(Second)
        else:print(First)

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

Solution (Python):
n = int(input())
A = [int(input()) for i in range(n)]
le = 0
B=[]
for i in range(n):
    B.append([A[i],i])

B.sort()

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