#include <bits/stdc++.h>
const int N = 500005;
int q[N], l, r, n;
long long ans[N], top[N], now;
template <typename T>
void read(T& a) {
  a = 0;
  char c = getchar();
  for (; c < 48 || c > 57; c = getchar())
    ;
  for (; c > 47 && c < 58; c = getchar()) a = (a << 1) + (a << 3) + c - 48;
}
int main() {
  int mi = 1;
  read(n);
  for (int i = (1); i <= (n); i++) read(top[i]);
  q[0] = r = now = 0;
  for (int i = (1); i <= (n); i++) {
    while (r > 0 && top[q[r]] > top[i]) {
      now -= top[q[r]] * (q[r] - q[r - 1]);
      r--;
    }
    q[++r] = i;
    now += top[i] * (q[r] - q[r - 1]);
    ans[i] = now;
  }
  q[0] = n + 1;
  r = now = 0;
  for (int i = (n); i >= (1); i--) {
    while (r > 0 && top[q[r]] > top[i]) {
      now -= top[q[r]] * (q[r - 1] - q[r]);
      r--;
    }
    q[++r] = i;
    now += top[i] * (q[r - 1] - q[r]);
    ans[i] += now;
  }
  for (int i = (1); i <= (n); i++) ans[i] -= top[i];
  for (int i = (2); i <= (n); i++)
    if (ans[i] > ans[mi]) mi = i;
  for (int i = (mi - 1); i >= (1); i--)
    if (top[i] > top[i + 1]) top[i] = top[i + 1];
  for (int i = (mi + 1); i <= (n); i++)
    if (top[i] > top[i - 1]) top[i] = top[i - 1];
  for (int i = (1); i <= (n); i++) printf("%lld ", top[i]);
  return 0;
}
