#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("-Ofast")
using namespace std;
long long sf[+500500], pr[+500500], ans2, ans1, p, b[+500500], a[+500500], mid,
    n, i, B[+500500];
long long t[4 * +500500];
void upd(long long v, long long l, long long r, long long ps, long long x,
         bool c) {
  if (l == r)
    t[v] = x;
  else {
    long long md = (l + r) >> 1;
    if (ps <= md)
      upd(v * 2, l, md, ps, x, c);
    else
      upd(v * 2 + 1, md + 1, r, ps, x, c);
    if (t[v * 2] >= t[v * 2 + 1])
      t[v] = t[v * 2 + 1];
    else
      t[v] = t[v * 2];
  }
}
long long first(long long v, long long l, long long r, long long tl,
                long long tr, long long x) {
  if (l > r || l > tr || tl > tr || tl > r) return -1;
  if (t[v] > x) return -1;
  if (l == r) return l;
  long long md = (l + r) >> 1;
  long long fst = first(v * 2 + 1, md + 1, r, max(md + 1, tl), tr, x);
  if (fst == -1) return first(v * 2, l, md, tl, min(tr, md), x);
  return fst;
}
long long second(long long v, long long l, long long r, long long tl,
                 long long tr, long long x) {
  if (l > r || l > tr || tl > tr || tl > r) return -1;
  if (t[v] > x) return -1;
  if (l == r) return l;
  long long md = (l + r) >> 1;
  long long scd = second(v * 2, l, md, tl, min(md, tr), x);
  if (scd == -1) return second(v * 2 + 1, md + 1, r, max(tl, md + 1), tr, x);
  return scd;
}
void build(long long v, long long l, long long r) {
  t[v] = 1e18;
  if (l == r) return;
  long long md = (l + r) >> 1;
  build(v * 2, l, md);
  build(v * 2 + 1, md + 1, r);
}
int main() {
  ios_base::sync_with_stdio(NULL);
  iostream::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  sf[n + 1] = pr[0] = 1e18;
  for (i = 1; i <= n; i++) pr[i] = min(pr[i - 1], a[i]), ans1 += pr[i];
  for (i = n; i >= 1; i--) sf[i] = min(sf[i + 1], a[i]), ans2 += sf[i];
  build(1, 1, n);
  upd(1, 1, n, 1, a[1], 1);
  p = a[1];
  b[1] = p;
  for (mid = 2; mid <= n; mid++) {
    long long ft = first(1, 1, n, 1, mid - 1, a[mid]);
    if (ft == -1)
      p = 0, ft = 0;
    else
      p = b[ft];
    p += (mid - ft) * a[mid];
    b[mid] = p;
    upd(1, 1, n, mid, a[mid], 1);
  }
  build(1, 1, n);
  upd(1, 1, n, n, a[n], 0);
  p = a[n];
  B[n] = a[n];
  for (mid = n - 1; mid >= 1; mid--) {
    long long sd = second(1, 1, n, mid + 1, n, a[mid]);
    if (sd == -1)
      p = 0, sd = n + 1;
    else
      p = B[sd];
    p += (sd - mid) * a[mid];
    B[mid] = p;
    upd(1, 1, n, mid, a[mid], 0);
  }
  long long ans = 0;
  mid = 1;
  for (i = 1; i <= n; i++)
    if (ans < b[i] + B[i] - a[i]) {
      ans = b[i] + B[i] - a[i];
      mid = i;
    }
  sf[mid + 1] = 1e18;
  for (i = mid; i >= 1; i--) sf[i] = min(sf[i + 1], a[i]);
  pr[mid - 1] = 1e18;
  for (i = mid; i <= n; i++) pr[i] = min(pr[i - 1], a[i]);
  for (i = 1; i <= mid; i++) cout << sf[i] << " ";
  for (i = mid + 1; i <= n; i++) cout << pr[i] << " ";
}
