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 = list(int(input()) for i in range(N))
A_sorted = sorted(A)

M = max(A)
MI = A .index(M)

S = A_sorted[-2]

for i in range(N):
    if i == MI:
        print(S)
    else:
        print(M)

Solution (Python):
import sys


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


N = int(input())
A = [int(input()) for _ in range(N)]
left = [0] * N
right = [0] * N
for i in range(1, N):
    left[i] = max(A[i - 1], left[i - 1])
    right[N - i - 1] = max(A[N - i], right[N - i])
[print(max(left[i], right[i])) for i in range(N)]

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

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

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

arr = []

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

M1 = A[-1]
M2 = A[-2]

for i in range(N):
  if arr[i] == M1:
    print(M2)
  else:
    print(M1)

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

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

s = []

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

m = max(s)

s2 = s.copy()

s2.remove(m)

m2 = max(s2)

for i in s:
    if i == m:
        print(m2)
        
    else:
        print(m)

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

s=sorted(a,reverse=True)
max_1=s[0]
max_2=s[1]

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

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

for i in range(0,n):
  a[i] = int(input())
c = a
m1 = max(a)
m1_ = a.index(m1)
del c[m1_]
m2 = max(c)
m2_ = c.index(m2)

for i in range(0,n):
  if(i == m1_):
    print(m2)
  else:
    print(m1)

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

left = [0]
l = 1
for i in A:
    l = max(l,i)
    left.append(l)
right = [0]
r = 1
for i in A[::-1]:
    r = max(r,i)
    right.append(r)
    
for i in range(N):
    print(max(left[i],right[-i-2]))

Solution (Python):
import math
import string
import collections
from collections import Counter


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
aa = sorted(a)

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

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

for i in a:
  if i==a2[0]:
    print(a2[1])
  else:
    print(a2[0])

Solution (Python):
import sys

import math
from fractions import Fraction
import itertools

input=sys.stdin.readline
n=int(input())
a=[int(input()) for _ in range(n)]
l=[0]*n
r=[0]*n
l[0]=a[0]
r[0]=a[-1]
for i in range(1,n):
    if a[i]>l[i-1]:
        l[i]=a[i]
    else:
        l[i]=l[i-1]
    if a[n-1-i]>r[i-1]:
        r[i]=a[n-1-i]
    else:
        r[i]=r[i-1]
print(r[n-2])
for i in range(1,n-1):
    print(max(l[i-1],r[n-i-2]))
print(l[n-2])

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
m1=max(a)
m2=sorted(a)[-2]
l=[i for i, x in enumerate(a) if x == m1]

if len(l)==1:
  for i in range(n):
    if i ==l[0]:
      print(m2)
    else:
      print(m1)
else:
  for i in range(n):
    print(m1)

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

mx_1, mx = sorted(a)[-2:]

for i in a:
  if i == mx:
    print(mx_1)
  else:
    print(mx)

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

B=sorted(A.copy())
b1=B[-1]
b2=B[-2]

for a in A:
    if a==b1:
        print(b2)
    else:
        print(b1)

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

Solution (Python):
N = int(input())
A = [(i,int(input())) for i in range(N)]
B = sorted(A,key=lambda x:x[1],reverse=True)
cnt = 0
bmax = B[0][1]
for i in range(N):
    if B[i][1]==bmax:
        cnt += 1
    else:break
for i in range(N):
    if A[i][1] < bmax:
        print(bmax)
    elif A[i][1]==bmax and cnt==1:
        print(B[1][1])
    elif A[i][1]==bmax and cnt>1:
        print(bmax)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(int(input()) for _ in range(n))
    A_s = sorted(A)
    max_A = A_s[n - 1]
    sub_max_A = A_s[n - 2]

    for i in range(n):
        if A[i] == max_A:
            print(sub_max_A)
        else:
            print(max_A)


if __name__ == '__main__':
    resolve()

Solution (Python):
def solve():
    N = int(input())
    A = []
    for i in range(N):
        a = int(input())
        A.append(a)
    
    A_sorted = sorted(A, reverse=True)

    ans = []
    for a in A:
        if a == A_sorted[0]:
            ans.append(A_sorted[1])
        else:
            ans.append(A_sorted[0])
    
    print(*ans, sep='\n')

if __name__ == '__main__':
    solve()

Solution (Python):
def resolve():
  n = int(input())
  a = []
  ma = 0
  for i in range(n):
    a.append(int(input()))
    ma = max(ma, a[i])
  b = a.copy()
  b.sort()
  for i in range(n):
    if a[i] == ma:
      print(b[n-2])
    else:
      print(ma)
  return

if __name__ == "__main__":
  resolve()

Solution (Python):
def solve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    B = sorted(A)
    m = B[-1]
    m2 = B[-2]
    ans = []
    for a in A:
        if a!=m:
            ans.append(m)
        else:
            ans.append(m2)
    return ans
print(*solve(),sep='\n')

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

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,*A = map(int, read().split())

    A_ = sorted(A)
    m = max(A)

    for a in A:
        if a == m:
            print(A_[-2])
        else:
            print(m)


if __name__ == "__main__":
    main()

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

Solution (Python):
from copy import deepcopy


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    b = deepcopy(a)
    b.sort()
    for aa in a:
        if aa == b[-1]:
            print(b[-2])
        else:
            print(b[-1])


if __name__ == '__main__':
    main()

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

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

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

As = sorted(A, reverse=True)
M = As[0]
Ms = As[1]

for i in range(N):
  if M == A[i]:
    print(Ms)
  else:
    print(M)

Solution (Python):
n = int(input())
aaa = [int(input()) for i in range(n)]
max_l = [0] * (n - 1)
max_r = [0] * (n - 1)
max_l[0] = aaa[0]
for i in range(1, n - 1):
    max_l[i] = max(aaa[i], max_l[i - 1])
max_l = [1] + max_l

aaa.reverse()
max_r[0] = aaa[0]
for i in range(1, n - 1):
    max_r[i] = max(aaa[i], max_r[i - 1])
max_r = [1] + max_r
max_r.reverse()

for i in range(n):
    print(max(max_l[i], max_r[i]))

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

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

x = sorted(a)
ans1 = x[-1]
ans2 = x[-2]

for i in range(n):
    if a[i] == ans1:
        print(ans2)
    else:
        print(ans1)

Solution (Python):
n = int(input())
ll = [int(input()) for i in range(n)]
llc = ll.copy()
llc.sort()
m1 = llc[n-1]
m2 = llc[n-2]

for l in ll:
    if l == m1:
        print(m2)
    else:
        print(m1)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = I()
A = ILs(N)
B = A[:]
B.sort()

MAX1 = B[-1]
MAX2 = B[-2]

for a in A:
  if a == MAX1:
    P(MAX2)
  else:
    P(MAX1)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N = I()
A = ILs(N)
import copy
B = copy.copy(A)
B.sort()

MAX1 = B[-1]
MAX2 = B[-2]

for a in A:
  if a == MAX1:
    P(MAX2)
  else:
    P(MAX1)

Solution (Python):
n = int(input())
a = [0]*n
for i in range(n):
    a[i] = int(input())
A = max(a)
b = sorted(a,reverse=True)
import collections
c = collections.Counter(a)
if c[A] >= 2:
    for i in range(n):
        print(A)
else:
    for i in range(n):
        if a[i] == A:
            print(b[1])
        else:
            print(A)

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

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

Solution (Python):

def inp():
    return(int(input()))


N = inp()
A = []
i1, i2 = 0, 0

for i in range(N):
    A.append(inp())

srt = sorted(A)
m1 = srt[-1]
m2 = srt[-2]

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

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

max1 = [A.index(max(A)), max(A)]
p = A.pop(A.index(max(A)))
max2 = max(A)
for k in range(N):
    if k == max1[0]:
        print(max2)
    else:
        print(max1[1])

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

max1 = [A.index(max(A)), max(A)]
p = A.pop(A.index(max(A)))
max2 = max(A)
ans = [max2 if j == max1[0] else max1[1] for j in range(N)]
for k in range(N):
    print(ans[k])

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

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

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

Blist = sorted(Alist, reverse=True)

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

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

first, second = sorted(A, reverse=True)[:2]

for i in range(N):
    if A[i] == first:
        print(second)
    else:
        print(first)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
MAX = max(A)
AA = A[:]
AA.pop(A.index(MAX))
SECOND = max(AA)
for a in A:
    if a == MAX and a == SECOND:
        print(MAX)
    elif a == MAX and a != SECOND:
        print(SECOND)
    else:
        print(MAX)

Solution (Python):

import sys
input = sys.stdin.readline









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

B = sorted(A)

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

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

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

max1 = 0
max2 = 0

for a in A:
    if a > max1:
        max2 = max1
        max1 = a
    elif a > max2:
        max2 = a

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
left_max = [0] * (N + 1)
right_max = [0] * (N + 1)

for index, a in enumerate(A, start=1):
    left_max[index] = max(left_max[index - 1], a)

for index, a in enumerate(reversed(A), start=1):
    right_max[index] = max(right_max[index - 1], a)

for i in range(N):
    print(max(left_max[i], right_max[N - i - 1]))

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

for a in A:
    left_max.append(max(left_max[-1], a))

for a in reversed(A):
    right_max.append(max(right_max[-1], a))

for i in range(N):
    print(max(left_max[i], right_max[N - i - 1]))

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 = [0] * n
for i in range(n):
    a[i] = int(input())
sorted_a = sorted(a, reverse=True)
max_a = sorted_a[0]
for i in range(n):
    if max_a != a[i]:
        print(max_a)
    else:
        print(sorted_a[1])

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

Solution (Python):
n=int(input())
dic={}
mx=0
al=[]
for i in range(n):
    a=int(input())
    al.append(a)

order=sorted(al,reverse=True)
mx1=order[0]
mx2=order[1]

for a in al:
    if a==mx1:
        print(mx2)
    else:
        print(mx1)

Solution (Python):
n = int(input())
num = list(int(input()) for i in range(n))
test = sorted(num, reverse=True)
i = 0

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

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

max_A = 0
max_index = 0
next_A = 0

for i in range(N):
    if A_list[i] > max_A:
        max_A = A_list[i]
        max_index = i
    elif max_A >= A_list[i] > next_A:
        next_A = A_list[i]

for i in range(N):
    if i == max_index:
        print(next_A)
    else:
        print(max_A)

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

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

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

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


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

Solution (Python):
from copy import copy

n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(copy(a),reverse=True)


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

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

Solution (Python):

import sys
input = sys.stdin.readline
N = int(input())
A = [int(input()) for i in range(N)]
A_sort =sorted(A)
ans =[]*N
max_1 =A_sort[-1]
max_2 =A_sort[-2]

for i in A:
    if i !=max_1:
        ans.append(max_1)
    else:
        ans.append(max_2)
[print(i) for i in ans]

Solution (Python):
def resolve():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    maxA = max(A)
    countMax = A.count(maxA)
    if countMax > 1:
        [print(maxA) for _ in range(N)]
    else:
        next = -1
        maxIdx = A.index(maxA)
        if maxIdx == 0:
            next = max(A[1:])
        elif maxIdx == N - 1:
            next = max(A[:N - 1])
        else:
            next = max(A[0:maxIdx])
            next = max(next, max(A[maxIdx + 1:N]))
        for i in range(N):
            if not i == maxIdx:
                print(maxA)
            else:
                print(next)


resolve()

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

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

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

Solution (Python):

N = int(input())
A = []
append = A.append

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

sort_A = sorted(A, reverse=True)[:2]


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

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

lm = [0] * n
rm = [0] * n

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

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


for i in range(n):
    print(max(lm[i], rm[i]))

Solution (Python):
n = int(input())
li = []
for i in range(n):
    li.append(int(input()))
k = max(li)
l = li.index(k)
for i in range(n):
    if i == l:
        li[i] = 0
        print(max(li))
    else:
        print(k)

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

lm = [0 for i in range(n)]
rm = [0 for i in range(n)]
for i in range(1, n):
  lm[i] = max(lm[i-1], a[i-1])

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

for i in range(n):
  print(max(lm[i], rm[i]))

Solution (Python):
N = int(input())
nums = []
first = (-1,-float('inf'))
second = (-1,-float('inf'))
for i in range(N):
  num = int(input())
  nums.append(num)
  if num >= first[1]:
    second = first
    first = (i, num)
  elif num >= second[1]:
    second = (i, num)

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

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

Solution (Python):

n = int(input())
s = [] 
for _ in range(n):
    s += [int(input())]
s_sort = sorted(s)
for i in s:
    if i == s_sort[-1]:
        print(s_sort[-2])
    else:
        print(s_sort[-1])

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

Solution (Python):
N=int(input())
A=[int(input()) for _ in range(N)]
ll=sorted(A)
a=max(A)
b=ll[-2]
  
for n in A:
  if n==a:
    print(b)
  else:
    print(a)

Solution (Python):
import numpy as np

n = int(input())
A = np.array(list(int(input()) for i in range(n)))
A_sort = np.sort(A)
A_1 = A_sort[n-1]
A_2 = A_sort[n-2]


for i in range(n):
    if A[i] != A_1:
        print(A_1)
    else:
        print(A_2)

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


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

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

B = sorted(A)


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

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

import copy
B = copy.copy(A)
B.remove(a)

for j in range(N):
    if j == i:
        print(max(B))
    else:
        print(a)

Solution (Python):
import copy
def exception_handling(n, ali):

    sortali = copy.copy(ali)
    sortali.sort(reverse=True)

    alimax = sortali[0]
    alimax2 = sortali[1]

    ans = []

    for i in ali:
        if i != alimax:
            ans.append(alimax)
        else:
            ans.append(alimax2)

    return ans

def main():
    n = int(input())
    ali = [int(input())for i in range(n)]
    ans = exception_handling(n, ali)
    for i in ans:
        print(i)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
b = sorted(a, reverse=True)
M1 = b[0]
M2 = b[1]
for i in range(n):
    if a[i] != M1:
        print(M1)
    else:
        print(M2)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]
M = max(A)
B.remove(M)
S = max(B)
big = A.index(M)
for i in range(N):
  if i == big:
    print(S)
  else:
    print(M)

Solution (Python):

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

Solution (Python):
n=int(input())
a=[0]*n
for ii in range(n):
  a[ii]=int(input())
import copy
a0=copy.copy(a)
a0.sort(reverse=True)
for ii in range(n):
  if a[ii]==a0[0]:
    print(a0[1])
  else:
    print(a0[0])

Solution (Python):
import sys

input = sys.stdin.readline

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

B = sorted(A, reverse=True)
max = B[0]
next_max = B[1]

for i in range(N):
    if A[i] == max:
        print(next_max)
    else:
        print(max)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
m1 = 0
m2 = 0
mp = 0
for i in range(n):
    if (m1 < a[i]):
        m2 = m1
        m1 = a[i]
        mp = i
    elif (m2 < a[i]):
        m2 = a[i]

for i in range(n):
    if (mp == i):
        print(m2)
    else:
        print(m1)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
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
input = sys.stdin.readline
n=int(input())
a=[int(input()) for _ in range(n)]
sort_a=sorted(a, reverse=True)
max=sort_a[0]
semimax=sort_a[1]
for i in range(n):
  if a[i] != max:
    print(max)
  else:
    print(semimax)

Solution (Python):
N=int(input())
A=[]
for i in range(N): A.append(int(input()))
S=sorted(A,key=int)
max1=S[-1]
max2=S[-2]
for i in A:
    if i==max1: print(max2)
    else: print(max1)

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

Solution (Python):
n=int(input())
lst=[int(input()) for i in range(n)]
a=max(lst)
aind=lst.index(a)
if aind==0:
  b=max(lst[1:])
elif aind==n-1:
  b=max(lst[:n-2])
else:
  tempa=max(lst[:aind])
  tempb=max(lst[aind+1:])
  b=max(tempa, tempb)
bind=lst.index(b)
for j in range(n):
  if j==aind:
    print(b)
  else:
    print(a)

Solution (Python):
n = int(input())
ai = [0]*n
for i in range(n):
  ai[i] = int(input())
bi = ai[:]
bi.sort()
num1 = bi[-1]
num2 = bi[-2]
for i in ai:
  if i == num1:
    print(num2)
  else:
    print(num1)

Solution (Python):
n = int(input())
a_list = list()
for _ in range(n):
    a_list.append(int(input()))

sorted_a_list = sorted(a_list)

max_v = max(sorted_a_list)

is_max_v_multi = sorted_a_list[-1] == sorted_a_list[-2]

next_max_v = 0
for i in range(len(sorted_a_list) - 1, -1, -1):
    if sorted_a_list[i] != max_v:
        next_max_v = sorted_a_list[i]
        break

for a in a_list:
    if is_max_v_multi:
        print(max_v)
    else:
        if a == max_v:
            print(next_max_v)
        else:
            print(max_v)

Solution (Python):
n = int(input())
p = []
for i in range(n):
    a = int(input())
    p.append(a)
q = sorted(p, reverse=True)
max1 = q[0]
max2 = q[1]
max1_i = p.index(max1)
max2_i = p.index(max2)
for i in range(n):
    if i == max1_i:
        print(max2)
    else:
        print(max1)

Solution (Python):
import sys


def main():
  n, *lst = map(int, sys.stdin.read().split())
  max1 = max(lst)
  idx = lst.index(max1)
  max2 = max(lst[:idx] + lst[idx+1:])
  print('\n'.join(str(max1) if j != max1 else str(max2) for j in lst))


if __name__ == '__main__':
  main()

Solution (Python):
import sys


def main():
  n, *lst = map(int, sys.stdin.read().split())
  max1 = max(lst)
  idx = lst.index(max1)
  max2 = max(lst[:idx] + lst[idx+1:])
  if max2 < max1:
    print(*[max1 if j != max1 else max2 for j in lst], sep='\n')
  else:
    print(*[max1]*n, sep='\n')

    
if __name__ == '__main__':
  main()

Solution (Python):
import sys


def main():
  n, *lst = map(int, sys.stdin.read().split())
  copy = sorted(lst)
  max1, max2 = copy[-1], copy[-2]
  if max2 < max1:
    print(*[max1 if j != max1 else max2 for j in lst], sep='\n')
  else:
    print(*[max1]*n, sep='\n')

    
if __name__ == '__main__':
  main()

Solution (Python):
import sys
n, *lst = map(int, sys.stdin.read().split())
copy = sorted(lst)
max1, max2 = copy[-1], copy[-2]

if max2 < max1:
  for j in lst:
    if j != max1:
      print(max1)
    else:
      print(max2)
else:
  for j in lst:
    print(max1)

Solution (Python):
import itertools
import math
import fractions
import functools
import copy
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

maximum = max(a)

count_m = a.count(maximum)

co = copy.deepcopy(a)
for i in range(n):
    if co[i] == maximum:
        co[i] = 0
maximum2 = max(co)

for i in range(n):
    if count_m == 1 and a[i] == maximum:
        print(maximum2)
    else: print(maximum)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A_ = sorted(A, reverse=True)
ma = A_[0]
mi = A_[1]
for j in A:
    if j == ma:
        print(mi)
    else:
        print(ma)

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

Solution (Python):
import math
import copy
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0

a = []
n = iin()
for i in range(n):
    a.append(iin())
    if neko < a[i]:
        neko = a[i]
b = copy.copy(a)
a.remove(neko)
nya = max(a)
for i in range(n):
    if b[i] == neko:
        print(nya)
    else:
        print(neko)

Solution (Python):
n=int(input())
a=[int(input()) for _ in range(n)]
saidai=sorted(a)[-1]
nibanme=sorted(a)[-2]

for i in range(n):
  if a[i]==saidai:
    print(nibanme)
  else:
    print(saidai)