#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long cl(long double a) {
  if (a > (long long)a) {
    return (long long)a + 1;
  } else {
    return (long long)a;
  }
}
long long flr(long double a) {
  if (a < 0.0) {
    return (long long)a - 1;
  }
  return (long long)a;
}
const long long M = 5e5 + 100;
long long n, a[M];
vector<long long> st(4 * M + 1);
long long query(long long v, long long tl, long long tr, long long l,
                long long r) {
  if (tl > r || tr < l) return M - 1;
  if (tl >= l && tr <= r) {
    return st[v];
  }
  long long tm = (tl + tr) / 2;
  long long i1 = query(2 * v, tl, tm, l, r),
            i2 = query(2 * v + 1, tm + 1, tr, l, r);
  long long ans = (a[i1] < a[i2]) ? i1 : i2;
  return ans;
}
void build(long long v, long long tl, long long tr) {
  if (tl == tr) {
    st[v] = tl;
    return;
  }
  long long tm = (tl + tr) / 2;
  build(2 * v, tl, tm);
  build(2 * v + 1, tm + 1, tr);
  st[v] = a[st[2 * v]] < a[st[2 * v + 1]] ? st[2 * v] : st[2 * v + 1];
}
long long recur(long long s, long long e) {
  if (s > e) return 0;
  if (s == e) return a[s];
  long long minpos = query(1, 0, n - 1, s, e);
  long long a1 = (minpos - s + 1) * a[minpos] + recur(minpos + 1, e);
  long long a2 = (e - minpos + 1) * a[minpos] + recur(s, minpos - 1);
  if (a1 > a2) {
    for (long long i = s; i <= minpos; i++) a[i] = a[minpos];
  } else {
    for (long long i = minpos; i <= e; i++) a[i] = a[minpos];
  }
  return max(a1, a2);
}
void solve() {
  cin >> n;
  a[M - 1] = 1e15;
  for (long long i = 0; i < n; i++) cin >> a[i];
  build(1, 0, n - 1);
  (recur(0, n - 1));
  for (long long i = 0; i < n; i++) cout << a[i] << " ";
  cout << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  while (t--) solve();
}
