#include <bits/stdc++.h>
using namespace std;
vector<long long> prev_smaller(vector<long long>& arr) {
  long long n = arr.size();
  vector<long long> res(n);
  stack<pair<long long, long long> > s;
  for (long long i = 0; i < n; ++i) {
    while (!s.empty() && s.top().first >= arr[i]) {
      s.pop();
    }
    if (!s.empty()) {
      res[i] = s.top().second;
    } else {
      res[i] = -1;
    }
    s.push({arr[i], i});
  }
  return res;
}
vector<long long> next_smaller(vector<long long>& arr) {
  long long n = arr.size();
  vector<long long> res(n);
  stack<pair<long long, long long> > s;
  for (long long i = n - 1; i >= 0; --i) {
    while (!s.empty() && s.top().first >= arr[i]) {
      s.pop();
    }
    if (!s.empty()) {
      res[i] = s.top().second;
    } else {
      res[i] = n;
    }
    s.push({arr[i], i});
  }
  return res;
}
signed main() {
  long long n;
  cin >> n;
  vector<long long> m(n), l(n), r(n);
  for (long long i = 0; i < n; ++i) cin >> m[i];
  vector<long long> le = prev_smaller(m);
  vector<long long> ri = next_smaller(m);
  for (long long i = 0; i < n; ++i) {
    if (le[i] == -1) {
      l[i] = (i + 1) * m[i];
    } else {
      l[i] = l[le[i]] + (i - le[i]) * m[i];
    }
  }
  for (long long i = n - 1; i >= 0; --i) {
    if (ri[i] == n) {
      r[i] = (n - i) * m[i];
    } else {
      r[i] = r[ri[i]] + (ri[i] - i) * m[i];
    }
  }
  long long idx = -1;
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    if (ans < l[i] + r[i] - m[i]) {
      ans = l[i] + r[i] - m[i];
      idx = i;
    }
  }
  long long prevht = m[idx];
  vector<long long> hts(n);
  hts[idx] = m[idx];
  for (long long i = idx - 1; i >= 0; --i) {
    hts[i] = min(m[i], hts[i + 1]);
  }
  for (long long i = idx + 1; i < n; i++) {
    hts[i] = min(m[i], hts[i - 1]);
  }
  for (long long i = 0; i < n; ++i) {
    cout << hts[i] << ' ';
  }
  cout << endl;
  return 0;
}
