1313_C2. Skyscrapers (hard version)
This is a harder version of the problem. In this version n ≤ 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 ≤ a_i ≤ 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 ≤ n ≤ 500 000) — the number of plots.

The second line contains the integers m_1, m_2, …, m_n (1 ≤ m_i ≤ 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.
```python
import sys
input=sys.stdin.readline
n=int(input())
m=[int(i) for i in input().split()]
copy=m[:]
def maxsumchecker(arr):
    stack,lr=[],[0]*(n)
    for i in range(n):
        while stack and stack[-1][1]>m[i]:
            stack.pop()
        if stack:
            j=stack[-1][0]
            lr[i]=lr[j]+(i-j)*m[i]
        else:
            lr[i]=(i+1)*m[i]
        stack.append([i,m[i]])
    return(lr)
l=maxsumchecker(m)
r=maxsumchecker(m.reverse())
r.reverse()
#print(l,r)
best,index=0,0
for i in range(n):
    if (l[i]+r[i]-copy[i])>best:
        best=l[i]+r[i]-copy[i]
        index=i
#print(index,best)
l1=[0]*(n)
l1[index]=copy[index]
i,j=index-1,index+1
while i>=0:
    l1[i]=min(l1[i+1],copy[i])
    i-=1
while j<n:
    l1[j]=min(l1[j-1],copy[j])
    j+=1
print(*l1)

```