This is a harder version of the problem. In this version $n \le 500\,000$

The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot.

Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.

Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$.

The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.


-----Input-----

The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots.

The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.


-----Output-----

Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible.

If there are multiple answers possible, print any of them.


-----Examples-----
Input
5
1 2 3 2 1

Output
1 2 3 2 1 

Input
3
10 6 8

Output
10 6 6 



-----Note-----

In the first example, you can build all skyscrapers with the highest possible height.

In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal.

Difficulty: interview

Starter code:

Solutions:
from sys import stdin


def solve():
    n = int(stdin.readline())
    m = list(map(int, stdin.readline().split()))
    msl = [-1] * n
    msp = [n] * n
    q = []
    for i in range(n):
        while q and m[q[-1]] > m[i]:
            q.pop()
        if q:
            msl[i] = q[-1]
        q.append(i)
    q = []
    for i in range(n - 1, -1, -1):
        while q and m[q[-1]] > m[i]:
            q.pop()
        if q:
            msp[i] = q[-1]
        q.append(i)

    dp1 = [0] * n
    for i in range(n):
        dp1[i] = m[i] * (i - msl[i])
        if msl[i] != -1:
            dp1[i] += dp1[msl[i]]
    dp2 = [0] * n
    for i in range(n - 1, -1, -1):
        dp2[i] += m[i] * (msp[i] - i)
        if msp[i] != n:
            dp2[i] += dp2[msp[i]]
    ansm = 0
    answc = 0
    for i in range(n):
        cur = dp1[i] + dp2[i] - m[i]
        if cur > answc:
            answc = cur
            ansm = i
    i = ansm
    cur = [0] * n
    cur[i] = m[i]
    for j in range(i + 1, n):
        cur[j] = min(cur[j - 1], m[j])
    for j in range(i - 1, -1, -1):
        cur[j] = min(cur[j + 1], m[j])
    print(*cur)


for i in range(1):
    solve()


n = int(input())
sp = list(map(int, input().split()))
forw = [0 for i in range(n)]
backw = [0 for i in range(n)]
stack = [(0, -1, 0)]
for i in range(n):
	while (sp[i] <= stack[-1][0]):
		stack.pop()
	stack.append((sp[i], i, (i - stack[-1][1]) * sp[i] + stack[-1][2]))
	forw[i] = stack[-1][2]
revsp = sp[::-1]
stack = [(0, -1, 0)]

for i in range(n):
	while (revsp[i] <= stack[-1][0]):
		stack.pop()
	stack.append((revsp[i], i, (i - stack[-1][1]) * revsp[i] + stack[-1][2]))
	backw[i] = stack[-1][2]

backw = backw[::-1]
#print(forw)
#print(backw)

center = 0
ans_center = 0
for i in range(n):
	if ans_center < backw[i] + forw[i] - sp[i]:
		center = i
		ans_center = backw[i] + forw[i] - sp[i]
#print(center)
ans = [0 for i in range(n)]
ans[center] = sp[center]
cur = sp[center]
for i in range(center - 1, -1, -1):
	cur = min(cur, sp[i])
	ans[i] = cur
cur = sp[center]
for i in range(center + 1, n, 1):
	cur = min(cur, sp[i])
	ans[i] = cur

print(*ans)


n = int(input())
m = list(map(int, input().split()))
left, right = [m[0]], [m[n-1]]

stack = [(m[0], 1)]
for i in range(1, n):
	res = left[-1]
	num_tmp = 0
	while stack and stack[-1][0] > m[i]:
		res -= stack[-1][0] * stack[-1][1]
		num_tmp += stack[-1][1]
		stack.pop()
	res += m[i] * (num_tmp + 1)
	stack.append((m[i], num_tmp + 1))
	left.append(res)

stack = [(m[n-1], 1)]
for i in range(n-2, -1, -1):
	res = right[-1]
	num_tmp = 0
	while stack and stack[-1][0] > m[i]:
		res -= stack[-1][0] * stack[-1][1]
		num_tmp += stack[-1][1]
		stack.pop()
	res += m[i] * (num_tmp + 1)
	stack.append((m[i], num_tmp + 1))
	right.append(res)

maxi, maxi_ind = 0, -1
for i in range(n):
	if left[i] + right[n-i-1] - m[i] > maxi:
		maxi = left[i] + right[n-i-1] - m[i]
		maxi_ind = i

i = maxi_ind
ans = [0 for _ in range(n)]
num = m[i]
ans[i] = m[i]

cur = m[i]
for j in range(i-1, -1, -1):
	cur = min(m[j], cur)
	ans[j] = cur
	num += cur

cur = m[i]
for k in range(i+1, n):
	cur = min(m[k], cur)
	ans[k] = cur
	num += cur

print(*ans)

from collections import deque
import sys
input = sys.stdin.readline



n = int(input())
a = list(map(int, input().split()))

q = deque([])
ans = [0] * n
tmp_ans = 0
for i in range(n):
    ind = i
    l_ind = i
    while True:
        if not q:
            q.append((a[i], l_ind))
            tmp_ans += a[i] * (l_ind + 1)
            break
        if q[-1][0] <= a[i]:
            tmp_ans += a[i] * (l_ind - q[-1][1])
            q.append((a[i], l_ind))
            break
        else:
            height, l = q.pop()
            if not q:
                tmp_ans -= height * (l + 1)
            else:
                tmp_ans -= height * (l - q[-1][1])
            ind = l
    ans[i] = tmp_ans


b = a[::-1]
q = deque([])
ans2 = [0] * n
tmp_ans = 0
for i in range(n):
    ind = i
    l_ind = i
    while True:
        if not q:
            q.append((b[i], l_ind))
            tmp_ans += b[i] * (l_ind + 1)
            break
        if q[-1][0] <= b[i]:
            tmp_ans += b[i] * (l_ind - q[-1][1])
            q.append((b[i], l_ind))
            break
        else:
            height, l = q.pop()
            if not q:
                tmp_ans -= height * (l + 1)
            else:
                tmp_ans -= height * (l - q[-1][1])
            ind = l
    ans2[i] = tmp_ans


max_ans = 0
ind = -1
for i in range(n):
    tmp = ans[i] + ans2[n - 1 - i] - a[i]
    if max_ans < tmp:
        max_ans = tmp
        ind = i

i = ind
ans = [0] * n

tmp = a[i]
l = i
r = i
 
max_height = a[i]
ans[i] = a[i]
for j in range(0, l)[::-1]:
    if a[j] >= max_height: 
        tmp += max_height
    else:
        tmp += a[j]
        max_height = a[j]
    ans[j] = max_height
    
max_height = a[i]
for j in range(r + 1, n):
    if a[j] >= max_height: 
        tmp += max_height
    else:
        tmp += a[j]
        max_height = a[j]
    ans[j] = max_height
print(*ans)