#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;
int n, pos;
long long a[maxn], pre[maxn], sub[maxn], l[maxn], r[maxn], mx;
pair<long long, int> sta[maxn];
int main() {
  scanf("%d", &n);
  for (auto i = (1); i <= (n); ++i) scanf("%lld", &a[i]);
  pos = 0;
  sta[++pos] = {0, 0};
  for (auto i = (1); i <= (n); ++i) {
    while (a[i] < sta[pos].first) pos--;
    pre[i] = sta[pos].second;
    sta[++pos] = {a[i], i};
  }
  pos = 0;
  sta[++pos] = {0, n + 1};
  for (auto i = (n); i >= (1); --i) {
    while (a[i] < sta[pos].first) pos--;
    sub[i] = sta[pos].second;
    sta[++pos] = {a[i], i};
  }
  for (auto i = (1); i <= (n); ++i) l[i] = l[pre[i]] + a[i] * (i - pre[i]);
  for (auto i = (n); i >= (1); --i) r[i] = r[sub[i]] + a[i] * (sub[i] - i);
  for (auto i = (1); i <= (n); ++i)
    if (l[i] + r[i] - a[i] > mx) mx = l[i] + r[i] - a[i], pos = i;
  for (auto i = (pos - 1); i >= (1); --i) a[i] = min(a[i], a[i + 1]);
  for (auto i = (pos + 1); i <= (n); ++i) a[i] = min(a[i], a[i - 1]);
  for (auto i = (1); i <= (n); ++i) printf("%lld ", a[i]);
}
