#include <bits/stdc++.h>
using namespace std;
int n;
int lim[500010], minl[500010], minr[500010], a[500010];
long long ansl[500010], ansr[500010];
int lg[500010], st[500010][25];
void init_st() {
  int flag = 2;
  for (int t = 2; t <= n; ++t) {
    lg[t] = lg[t - 1];
    if (t >= flag) {
      lg[t]++;
      flag <<= 1;
    }
  }
  for (int t = 1; t <= n; ++t) st[t][0] = lim[t];
  for (int t = n; t >= 1; --t) {
    for (int i = 1; i <= 20; ++i) {
      if (t + (1 << i) - 1 > n) break;
      st[t][i] = min(st[t][i - 1], st[t + (1 << (i - 1))][i - 1]);
    }
  }
}
int getmin(int a, int b) {
  int l = lg[b - a + 1];
  return min(st[a][l], st[b - (1 << l) + 1][l]);
}
int main() {
  scanf("%d", &n);
  for (int t = 1; t <= n; ++t) scanf("%d", &lim[t]);
  init_st();
  for (int t = 1; t <= n; ++t) {
    int ans = 0, l = 1, r = t - 1;
    while (l <= r) {
      int mid = (l + r) >> 1;
      if (getmin(mid, r) < lim[t]) {
        ans = mid;
        l = mid + 1;
      } else
        r = mid - 1;
    }
    minl[t] = ans;
    ans = n + 1, l = t + 1, r = n;
    while (l <= r) {
      int mid = (l + r) >> 1;
      if (getmin(l, mid) < lim[t]) {
        ans = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    minr[t] = ans;
  }
  for (int t = 1; t <= n; ++t) {
    int x = minl[t];
    ansl[t] = ansl[x] + 1LL * lim[t] * (t - x);
  }
  for (int t = n; t >= 1; --t) {
    int x = minr[t];
    ansr[t] = ansr[x] + 1LL * lim[t] * (x - t);
  }
  int flag = 0;
  long long pre = 0;
  for (int t = 1; t <= n; ++t) {
    long long now = ansl[t] + ansr[t] - lim[t];
    if (now > pre) {
      pre = now;
      flag = t;
    }
  }
  a[flag] = lim[flag];
  pre = lim[flag];
  for (int t = flag - 1; t >= 1; --t) {
    a[t] = min((int)pre, lim[t]);
    pre = min((int)pre, lim[t]);
  }
  pre = lim[flag];
  for (int t = flag + 1; t <= n; ++t) {
    a[t] = min((int)pre, lim[t]);
    pre = min((int)pre, lim[t]);
  }
  for (int t = 1; t <= n; ++t) printf("%d ", a[t]);
}
