#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  return x * flag;
}
long long a[500010], l[500010], r[500010], s[500010], top;
int main() {
  int n = read();
  for (register int i = 1; i <= n; ++i) a[i] = read();
  for (register int i = 1; i <= n; ++i) {
    while (top && a[s[top]] > a[i]) top--;
    int now = s[top];
    l[i] = l[now] + a[i] * (i - now);
    s[++top] = i;
  }
  while (top) top--;
  s[++top] = n + 1;
  for (register int i = n; i >= 1; --i) {
    while (top && a[s[top]] > a[i]) top--;
    int now = s[top];
    r[i] = r[now] + a[i] * (now - i);
    s[++top] = i;
  }
  long long maxn = 0, pos = 0;
  for (register int i = 1; i <= n; ++i) {
    if (l[i] + r[i] - a[i] > maxn) {
      pos = i;
      maxn = l[i] + r[i] - a[i];
    }
  }
  int pre = a[pos];
  for (register int i = pos; i; --i) {
    if (a[i] > pre) a[i] = pre;
    pre = a[i];
  }
  pre = a[pos];
  for (register int i = pos; i <= n; ++i) {
    if (a[i] > pre) a[i] = pre;
    pre = a[i];
  }
  for (register int i = 1; i <= n; ++i) printf("%lld ", a[i]);
  return 0;
}
