# Codeforces Round 622 (Div. 2) C. Skyscrapers

## Description

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.

## Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
ll a[maxn], ans[maxn], pre[maxn], nxt[maxn], spre[maxn], snxt[maxn];
int main()
{
ios::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
stack<ll> sta;
spre[1] = a[1];
sta.push(1);
for (int i = 2; i <= n; i++) {
while (!sta.empty() && a[sta.top()] >= a[i]) sta.pop();
if (!sta.empty()) pre[i] = sta.top();//单调栈找出左边第一个小于a[i]的位置
spre[i] = spre[pre[i]] + (i - pre[i]) * a[i];//计算spre
sta.push(i);
}
while (!sta.empty()) sta.pop();
for (int i = 1; i <= n; i++) nxt[i] = n + 1;//初始化nxt数组，上面因为pre数组初值本就是0所以不需要初始化。
sta.push(n);
snxt[n] = a[n];
for (int i = n - 1; i >= 1; i--) {
while (!sta.empty() && a[sta.top()] >= a[i]) sta.pop();
if (!sta.empty()) nxt[i] = sta.top();//单调栈找出右边第一个小于a[i]的位置
snxt[i] = snxt[nxt[i]] + (nxt[i] - i) * a[i];//计算snxt，类似于spre
sta.push(i);
}
ll pos = 1, maxx = 0;
for (int i = 1; i <= n; i++) {
if (spre[i] + snxt[i] - a[i] > maxx) {
maxx = spre[i] + snxt[i] - a[i];
pos = i;//找到以pos为极值点的时候序列和最大
}
}
ll cur = a[pos]; ans[pos] = a[pos];
for (int i = pos + 1; i <= n; i++) {
cur = min(cur, a[i]);
ans[i] = cur;
}
cur = a[pos];
for (int i = pos - 1; i >= 1; i--) {
cur = min(cur, a[i]);
ans[i] = cur;
}//通过a[pos]为极值点，左右两边确定每个数的取值
for (int i = 1; i <= n; i++) cout << ans[i] << " ";

return 0;
}