#include <bits/stdc++.h>
using namespace std;
stack<pair<long long, long long> > lq, rq;
long long a[510000];
long long l[510000], r[510000];
long long n;
signed main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  lq.push(make_pair(0, 0));
  for (long long i = 1; i <= n; i++) {
    while (lq.size() != 0)
      if (lq.top().first >= a[i])
        lq.pop();
      else
        break;
    l[i] = l[lq.top().second] + (i - lq.top().second) * a[i];
    lq.push(make_pair(a[i], i));
  }
  rq.push(make_pair(0, n + 1));
  for (long long i = n; i >= 1; i--) {
    while (rq.size() != 0)
      if (rq.top().first >= a[i])
        rq.pop();
      else
        break;
    r[i] = r[rq.top().second] + (rq.top().second - i) * a[i];
    rq.push(make_pair(a[i], i));
  }
  pair<long long, long long> ans(0, 0);
  for (long long i = 1; i <= n; i++) {
    ans = max(ans, make_pair(l[i] + r[i] - a[i], i));
  }
  long long mx = a[ans.second];
  for (long long i = ans.second; i <= n; i++) {
    a[i] = mx = min(mx, a[i]);
  }
  mx = a[ans.second];
  for (long long i = ans.second; i >= 1; i--) {
    a[i] = mx = min(mx, a[i]);
  }
  for (long long i = 1; i <= n; i++) cout << a[i] << ' ';
  cout << endl;
}
