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

import sys





"""Template"""


class IP:
    """
    入力を取得するクラス
    """
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        
        
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x//y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)

    def lcm(self, x, y):
        return (x * y) // self.gcd(x, y)


"""ここからメインコード"""


def main():
    
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    """この下から書いてね"""
    n = ip.I()
    a = ip.ILS(n)

    m = max(a)
    m2 = sorted(a)[-2]
    if m == m2:
        [print(m) for _ in r(n)]
    else:
        [print(m2) if a[i] == m else print(m) for i in r(n)]


main()

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

arr = []
max_num = 0
label = 1
for i in range(n):
    inp = int(input())
    if inp >= max_num:
       max_num = inp
       label = i
    arr.append(inp)



arr_sub = arr[:]
arr_sub.pop(label)
second_max = max(arr_sub)


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

Solution (Python):
import sys
n = int(input())
a = []
count = [0 for _ in range(200001)]
for i in range(n):
    next = int(sys.stdin.readline())
    a.append(next)
    count[next]+=1
start = 0
for i in range(200000,0,-1):
    if count[i]>0:
        v1=i
        count[i]-=1
        start = i
        break
for i in range(start,0,-1):
    if count[i]>0:
        v2=i
        count[i]-=1
        break
count[v1]+=1
count[v2]+=1
if count[v1]>1:
    for i in range(n):
        sys.stdout.write('{}\n'.format(v1))
else:
    for v in a:
        if v != v1:
            sys.stdout.write('{}\n'.format(v1))
        else:
            sys.stdout.write('{}\n'.format(v2))

Solution (Python):
n = int(input())
l = [int(input()) for i in range(n)]
a = [0]
ma = 0
for i,j in enumerate(l):
    if j > ma:
        ma = j
    a.append(ma)
a.append(0)
b = [0]
ma = 0
for i,j in enumerate(reversed(l)):
    if j > ma:
        ma = j
    b.append(ma)
b.append(0)

for i in range(n):
    print(max(a[i],b[-i-3]))

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

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

s = sorted(A, reverse=True)
for a in A:
    if a == s[0] and a != s[1]:
        print(s[1])
    else:
        print(s[0])

Solution (Python):
N = int(input())
A, A_sorted = [], []
for i in range(N):
    x = int(input())
    A.append(x)
    A_sorted.append(x)
A_sorted.sort()
vmax, vmax2 = A_sorted[-1], A_sorted[-2]

for i in range(N):
    x = A[i]
    if vmax == x:
        print(vmax2)
    else:
        print(vmax)

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

n = int(input())
a = [int(input()) for _ in range(n)]
ans=max(a)
if a.count(ans)>1:
    for _ in range(n):
        print(ans)
else:
    for i in range(n):
        print(ans if a[i]!=ans else sorted(a)[-2])

Solution (Python):
import sys, re, os
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 S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
n = INT()

ans = 0

a = []
for i in range(n):
    a.append(INT())
l = []


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 = []
for i in range(N):
    A.append(int(input()))

max_A = max(A)
max_A_counter = 0
sec_A = max_A
for a in sorted(A, reverse=True):
    if a==max_A:
        max_A_counter+=1
    if a!=max_A:
        sec_A=a
        break

for i in range(N):
    if A[i]==max_A:
        print(max_A if 2<=max_A_counter else sec_A)
    else:
        print(max_A)

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

maxNum = max(A)
index = -1
for i in range(N):
    if A[i] == maxNum:
        index = i

for i in range(N):
    if i != index:
        print(maxNum)
        continue
    else:
        A.remove(maxNum)
        print(max(A))

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

A_max = max(A)
A_copy = A.copy()
A_copy.remove(A_max)
A_max_second = max(A_copy)

for a in A:
    if a != A_max:
        print(A_max)
    else:
        print(A_max_second)

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

Solution (Python):
N=input()
N=int(N)
A=[]
for i in range(N):
    b=input()
    b=int(b)
    A.append(b)
flag=0
max_value=max(A)
for i in range(N):
    if flag==1:
        print(max_value)
    elif A[i]<max_value:
        print(max_value)
    else:
        data=A.pop(i)
        print(max(A))
        A.insert(i,data)
        if max(A)==max_value:
            flag=1

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

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

"順方向の最大値、逆方向の最大値"

left_max = [A[0]]
right_max = [A[-1]]

for i in range(1, N):
    left_max.append(max(left_max[-1], A[i]))


for i in range(N-2, -1, -1):
    right_max.append((max(right_max[-1], A[i])))


right_max.reverse()


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

Solution (Python):
from collections import deque
n = int(input())
li = [int(input()) for _ in range(n)]
m = max(li)
ind = li.index(m)
li.sort()
mm= li[n-2]
for i in range(n):
  if i == ind:
    print(mm)
  else:
    print(m)

Solution (Python):
import sys

input=sys.stdin.readline

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

A=sorted(a)


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

Solution (Python):
import copy

N = int(input())

A = [int(input()) for _ in range(N)]
B = sorted(A)
max_1 = B[-1]
max_2 = B[-2]

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

Solution (Python):
N = int(input())
A = []
for i in range(N):
        A.append(int(input()))
m = max(A)
n = A.index(m)
A.remove(m)
m1 = max(A)
for i in range(n):
        print(m)
print(m1)
for i in range(N-1-n):
        print(m)

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

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

li2 = sorted(li,reverse=True)
ans = max(li)

for i in range(N):
    if(ans == li[i]):
        print(li2[1])
    else:
        print(ans)

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

Solution (Python):
import numpy as np

n = int(input())
a = [int(input()) for _ in range(n)]
na = np.array(a)
maxI = np.argmax(na)
maxA = na[maxI]
nb = np.copy(na)
nb[maxI] = 0
maxA2 = np.max(nb)
for i in range(n):
    if i != maxI:
        print(na[maxI])
    else:
       print(maxA2)

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

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

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

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

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

if a_max_count > 1:
  for i in range(n):
    print(a_max)
    
else:
  print(max(a[1:]))
  
  for i in range(1, n-1):
    if a[i] == a_max:
      print(max(max(a[:i]), max(a[i+1:])))
      
    else:
      print(a_max)

  print(max(a[:-1]))

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


sortA = sorted(A, reverse=True)

if sortA[0] == sortA[1]:
    
    for i in range(N):
        print(sortA[0])
else:
    
    maxP = A.index(sortA[0])
    for i in range(maxP):
        print(sortA[0])
    print(sortA[1])
    for i in range(maxP+1, N):
        print(sortA[0])

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

sa = sorted(a)
mx = sa[-1]
for val in a:
    if val != mx:
        print(sa[-1])
    else:
        print(sa[-2])

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

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

max_value = max(a)
max_value_index = a.index(max(a))

a.pop(max_value_index)
second_max_value = max(a)
a.insert(max_value_index,max_value)







for i in a:
    if i != max_value:
        print(max_value)
    else:
        print(second_max_value)

Solution (Python):

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

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

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


a_max = max(a_s)
i_max_arg = a_s.index(a_max)
del a_s[i_max_arg]


a_max2 = max(a_s)    

for i in range(n):
  if i != i_max_arg:
    ans = a_max
  else:
    ans = a_max2
  print(ans)

Solution (Python):
N = int(input())
A_list = list(range(0))

top1Num = 0
top2Num = 0
topCount = 0



for i in range(N):
    A = int(input())
    A_list.append(A)
    if A > top1Num:
        top2Num = top1Num
        top1Num = A
    elif A > top2Num:
        top2Num = A





topCount = A_list.count(top1Num)




for i in range(N):
    if A_list[i] == top1Num:
        print(top2Num)
    else:
        print(top1Num)

Solution (Python):
import copy

N = int(input())

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

max_value = max(x_list)

get_index = x_list.index(max(x_list))

tmp_list = copy.deepcopy(x_list)
tmp_list.pop(get_index)
without_max_value = max(tmp_list)

for i in range(len(x_list)):
    if i != get_index:
        print(max_value)
    else:
        print(without_max_value)

Solution (Python):
import numpy as np

def main():
    n = int(input())
    a = np.zeros([n], dtype=int)
    for i in range(n):
        a[i] = int(input())

    ma = np.argmax(a)
    sec = 0
    for i in range(n):
        if i == ma:
            continue
        if a[i] > sec:
            sec = a[i]
    for i in range(n):
        if a[i] < a[ma]:
            print(a[ma])
        else:
            print(sec)


if __name__ == "__main__":
    main()

Solution (Python):
from math import sqrt

def solve0(N, A):
    S = sorted(A)
    S0 = S[-1]
    S1 = S[-2]

    for a in A:
        if a == S0:
            print(S1)
        else:
            print(S0)

def solve(N, A):
    D = int(sqrt(N))
    M = []
    for i in range(0, N, D):
        M.append(max(A[i:i+D]))
    M.append(0)
    for i in range(0, N):
        d = i // D
        l = d*D
        m = 0
        for j in range(l, min(l+D, N)):
            if i != j and A[j] > m:
                m = A[j]
        m0 = m
        if d == 0:
            m = max(m, max(M[d+1:]))
        elif d >= len(M) - 1:
            m = max(m, max(M[:d]))
            pass
        else:
            
            
            
            m = max(m, max(M[:d]), max(M[d+1:]))
        print(m)
        
        


def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    solve0(N, A)



main()

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

Solution (Python):


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

B = sorted(A, reverse=True)
maxnum1 = B[0]
maxnum2 = B[1]

for i in range(N):
    if A[i] != maxnum1:
        print(maxnum1)
    else:
        print(maxnum2)

Solution (Python):
import numpy as np
N = int(input())
list_a = [int(input()) for _ in range(N)]
list_np = np.array(list_a)
maxnum=list_np.max()
index_list = np.where(list_np == maxnum)[0]
if len(index_list) == 1:
  list_drop = [i for i in list_a if i != maxnum]
  a=[]
  for i in range(N):
    if i in index_list:
      a.append(max(list_drop))
    else:
      a.append(maxnum)
else:
  a = [maxnum for i in range(N)]
for i in range(N):
  print(a[i])

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

Solution (Python):


import copy

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

Amax = max(A)
TMP = copy.deepcopy(A)
TMP.remove(Amax)
Amax2 = max(TMP)

for i in range(N):
        if A[i] == Amax:
                print(Amax2)
        else:
                print(Amax)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    a_list = []
    max_num = 0
    second_max = 0
    max_count = 0
    for i in range(n):
        a = int(input())
        a_list.append(a)
        if a > max_num:
            max_num = a
            max_count = 1
        elif a > second_max:
            second_max = a

    for i in range(n):
        if a_list[i] != max_num:
            print(max_num)
        else:
            print(second_max)

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

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

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

sorted_a_lst = sorted(a_lst)
max_a = sorted_a_lst[-1]

for a in a_lst:
    if a == max_a:
        print(sorted_a_lst[-2])
    else:
        print(max_a)

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

if len(ind)>1:
    for _ in A:
        print(maxA)
else:
    tmp=A.copy()
    tmp.sort(reverse=True)
    for t in tmp:
        if t!=maxA:
            secondA=t
            break
    for a in A:
        if a==maxA:
            print(secondA)
        else:
            print(maxA)

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

mx = max(a)
count = a.count(mx)

mx2 = -1
for i in a:
  if(mx2 < i and i != mx):
    mx2 = i

if(mx2 == -1):
  mx2 = mx
  
for i in a:
  if(i!=mx):
    print(mx)
  else:
    if(count>1):
      print(mx)
    else:
      print(mx2)

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
m=max(a)
midx=a.index(max(a))
l=sorted(a)[-2]
for i in range(n):
    if i==midx:
        print(l)
    else:
        print(m)

Solution (Python):

N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
B = A[:]
B.sort()
one = B[-1]
two = B[-2]
for i in range(N):
    if A[i] == one:
        print(two)
    else:
        print(one)

Solution (Python):
import bisect

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

maxTmp = max(aList)
tmpList = sorted(aList, reverse=True)
max2Tmp = tmpList[1]
maxCnt = aList.count(maxTmp)
ansList = [0] * N

for i in range(N):
    if aList[i] == maxTmp:
        if maxCnt > 1:
            for j in range(N):
                ansList[j] = maxTmp
            break
        else:
            ansList[i] = max2Tmp

    else:
        ansList[i] = maxTmp

for i in ansList:
    print(i)

Solution (Python):











n = int(input())
ans = 0
most1 = 0
most2 = 0
ed = False
for i in range(n):
    t = int(input())
    ed = False
    if most1 <= t:
        most2 = most1
        most1 = t
        ans = i
        ed = True
    elif most2 <= t and ed == False:
        most2 = t
for i in range(n):
    if i==ans:print(most2)
    else:print(most1)

Solution (Python):
from sys import stdin

n = int(stdin.readline())
a = [int(input()) for x in range(n)]
sort_a = sorted(a, reverse=True)

for i, ai in enumerate(a):
    max = sort_a[0]
    if max == ai:
        max = sort_a[1]

    print(max)

Solution (Python):
import copy

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

ans = [max(lst)] * n

if lst.count(max(lst)) == 1:
    tmp = copy.deepcopy(lst)
    tmp.pop(lst.index(max(lst)))
    ans[lst.index(max(lst))] = max(tmp)

for l in ans:
    print(l)

Solution (Python):
import copy

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

A_sorted = sorted(A, reverse=True)

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

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

m1, m2, *_ = sorted(A, reverse=True)
for i in range(N):
    if A[i] == m1:
        print(m2)
    else:
        print(m1)

Solution (Python):
N=int(input())
A=[]
tmp=-1
ltor=[]
rtol=[]
for i in range(N):
    A.append(int(input()))
    tmp=max(tmp,A[i])
    ltor.append(tmp)
ltor=[0]+ltor
ltor.append(0)

tmp=-1
for i in range(N):
    tmp=max(tmp,A[N-1-i])
    rtol.append(tmp)
rtol=[0]+rtol
rtol.append(0)
rtol.reverse()

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

Solution (Python):
import numpy as np
N = int(input())
A = []
for n in range(N):
    A.append(int(input()))
A = np.array(A)
all_max = np.max(A)
max_ind = np.argmax(A)
max_remove = np.ones(N, dtype=bool)
max_remove[max_ind] = False
second_max = np.max(A[max_remove])


for n in range(N):
    if n == max_ind:
        print(second_max)
    else:
        print(all_max)

Solution (Python):
import collections

n = int(input())

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

b=sorted(a,reverse=True)
c=collections.Counter(a)
if c[b[0]] > 1:
	for i in range(n):
		print(b[0])
else:
	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()))
aa = sorted(a)
p = aa[-1]
q = aa[-2]
for e in a:
    if e == p:
        print(q)
    else:
        print(p)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
Max = max(A)
Index = [i for i, x in enumerate(A) if x == Max]
if len(Index) != 1:
    for i in range(N):
        print(Max)
else:
    Copy = A[:]
    Copy[Index[0]] = 0
    Ans = [Max for i in range(N)]
    Ans[Index[0]] = max(Copy)
    for i in range(N):
        print(Ans[i])

Solution (Python):
import math

def main():
    N = int(input())
    A = []
    ma1 = 0
    ma2 = 0
    for i in range(N):
        a = int(input())
        A.append(a)
        if i == 1:
            if a >= A[0]:
                ma2 = 0
            else:
                ma2 = 1 
        if A[ma2] <=  a:
            if A[ma1] <= a:
                ma2 = ma1
                ma1 = i
            else:
                ma2 = i
    
    for i in range(N):
        if ma1 == i:
            print(A[ma2])
        else:
            print(A[ma1])
    

if __name__ == "__main__":
    main()

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

for i in range(N):
	a = int(input())
	l.append(a)

l_max = max(l)
max_index = l.index(max(l))

for i in range(N):
	if i == max_index:
		b = l.pop(i)
		print(max(l))
	else:
		print(l_max)

Solution (Python):
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
sortA = sorted(A, reverse=True)
maxA = sortA[0]
secondA = sortA[1]
for i in range(N):
    if A[i] != maxA:
        print(maxA)
    else:
        print(secondA)

Solution (Python):
N = int(input())
Maxi = [0] * N
Ai = []
for i in range(N):
	Ai.append((i, int(input())))

Ai.sort(key = lambda t: t[1], reverse=True)

mi,max = Ai[0]

for i in range(N):
	Maxi[i] = max

Maxi[mi] = Ai[1][1]

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

Solution (Python):
n=int(input())
a=[]
a1=0
a2=0
for i in range(n):
    k=int(input())
    a.append(k)
    if a1<k:
        a2=a1
        a1=k
    elif a2<k:
        a2=k

for i in range(n):
    if a[i]==a1:
        print(a2)
    else:
        print(a1)

Solution (Python):
N = int(input())
numbers = []
for i in range(N):
    numbers.append(int(input()))
    
    
aaa = max(numbers)
bbb = sorted(numbers)[-2]

for n in numbers:
    if aaa == n:
        print(bbb)
    else:
        print(aaa)

Solution (Python):
first = 1
second = 1
n=int(input())
x = []
for i in range(0,n):
    s = int(input())
    if first <= s:
        second = first
        first = s
    elif second <= s:
        second = s
    x.append(s)

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

Solution (Python):
import sys


		
def main():
	n = int(input())
	ll=[int(input()) for _ in range(n)]
	
	mm = sorted(ll)
	
	ma=mm[-1]
	mb=mm[-2]
	
	for i in ll:
		pp = ma if i != ma else mb
		print(pp)
		
	
	
	


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

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

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

for i in A:
    print(two if one == i else one)

Solution (Python):

N = int(input())
A_lst = []
max_num = -1
max_index_lst = []
next_num = -1

for i in range(N):
    Ai = int(input())
    A_lst.append(Ai)
    if max_num == -1:
        max_num = Ai
        max_index_lst.append(i)
    elif next_num == -1:
        if max_num > Ai:
            next_num = Ai
        else:
            if max_num == Ai:
                max_index_lst.append(i)
            else:
                max_index_lst = [i]
            next_num = max_num
            max_num = Ai


    elif max_num <= Ai:
        if max_num == Ai:
            max_index_lst.append(i)
        else:
            max_index_lst = [i]
        next_num = max_num
        max_num = Ai

    
    elif next_num < Ai:
        next_num = Ai

ans_lst = [max_num]*N

if len(max_index_lst) == 1:
    ans_lst[max_index_lst[0]] = next_num

for i in ans_lst:
    print(i)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
b, c = sorted(a)[-2:]
j = a.index(c)
for i in range(n):
    print((c, b)[i == j])

Solution (Python):
N = int(input())
a = [int(input()) for i in range(N)]
b = []
for i in range(N):
    b.append([a[i], i])
b.sort(key=lambda x: x[0])


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

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

li2 = sorted(li)
max1 = li2[-1]
max2 = li2[-2]
max_index = li.index(max1)

if max1 != max2:
    for i in range(N):
        if i == max_index:
            print(max2)
        else:
            print(max1)
else:
    for i in range(N):
        print(max1)

Solution (Python):
N = int(input())
A = [int(input()) for n in range(N)]
 
max_A = max(A)
max_index = A.index(max(A))

A[max_index] = 0
max_A2 = max(A)
A[max_index] = max_A

for n in range(N):
  if A[n] != max_A:
    print(max_A)
  else:
    print(max_A2)

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

ans = 0
next_ans = 0
count = 0
index = 0

for i in range(num):
  s = int(input())
  if s > ans:
    next_ans = ans
    ans = s
    index = i
    count = 1
  elif s == ans:
    count += 1
  elif s > next_ans:
    next_ans = s
    
if count > 1:
  for i in range(num):
    print(ans)
else:
  for i in range(num):
    if i == index:
      print(next_ans)
    else:
      print(ans)

Solution (Python):

import numpy as np

def main():
    
    
    
    
    
    
    
    
    N = int(input())
    A_list = [int(input()) for i in range(N)]
    A_argsorted = np.argsort(A_list)[::-1]
    for i in range(N):
        max_value = A_list[A_argsorted[0]] if i != A_argsorted[0] else A_list[A_argsorted[1]]
        print(max_value)

    
if __name__=='__main__':
    main()

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

for i in range(n):
    if m<a[i]:
        m=a[i]
        k=i

for j in range(n):
    if j==k:
        print(sorted(a)[-2])
    else:
        print(m)

Solution (Python):



N = int(input())
A = []
for i in range(N):
    a = [i , int(input())]
    A.append(a)
    
A.sort(key = lambda x : x[1])

ans = [0]*N

for i in A:
    index = i[0]
    if i[1] == A[-1][1]:
        ans[index] = A[-2][1]
    else:
        ans[index] = A[-1][1]

for i in ans:
    print(i)

Solution (Python):
import copy
s=[]
i=0
n=int(input())
while i!=n:
    s.append(int(input()))
    i+=1
maxx=max(s)
g=s.count(maxx)
i=0
j=0


while i!=n:
    a = s[j]
    if maxx == a and g < 2:
        a=s[j]
        del s[j]
        print(max(s))
        s.append(a)
    else:
        print(maxx)
        j+=1

    i+=1

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

max1 = 0
max2 = 0

tmp = 0

for i in range(n):
    A = int(input())
    if A > max1 :
        max1 = A
        tmp = i
    elif A > max2:
        max2 = A

for i in range(tmp):
    print(max1)

print(max2)

for i in range(n-tmp-1):
    print(max1)

Solution (Python):
num = []
aaa = []
nmax = 0
n2max = 0
count = 0
n = int(input())
for i in range(n):
    a = int(input())
    aaa.append(a)
    if(nmax < a):
        n2max = nmax
        nmax = a
        count = 1
        num = [i]
    elif(nmax == a):
        count += 1
        num.append(i)
    elif(n2max < a):
        n2max = a
for i in range(len(aaa)):
    if(count >= 2) or (nmax != aaa[i]):
        print(nmax)
    else:
        print(n2max)

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

max_i=a.index(max(a))
max_n=max(a)
a.pop(max_i)
semi_n=max(a)

for j in range(n):
  if j != max_i:
    print(max_n)
  else:
    print(semi_n)

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

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

sorted_list = sorted(arr)
max = sorted_list[N-1]
sub_max = sorted_list[N-2]
max_count = arr.count(max)

if max_count == 1:
    max_index = arr.index(max)
    for i in range(N):
        if i == max_index:
            print(sub_max)
        else:
            print(max)
else:
    for _ in range(N):
        print(max)

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


sorted_a = sorted(a_list, reverse = True)
max_a = sorted_a[0]

result = []
for a in a_list:
    if a == max_a:
        print(sorted_a[1])
    else:
        print(max_a)

Solution (Python):
import copy

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



import math

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

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

max_a = max(a)

if a.count(max_a) == 1:
    b = a.copy()
    b.remove(max_a)
    second_max = max(b)
    for aa in a:

        if aa == max_a:
            print(second_max)
        else:
            print(max_a)
else:
    for i in range(n):
        print(max_a)