#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = (0); i < (n); ++i) cin >> a[i];
  vector<long long> l(n), r(n);
  vector<pair<long long, long long> > stk;
  for (int i = (0); i < (n); ++i) {
    if (i) l[i] = l[i - 1];
    long long lst = i;
    while (not stk.empty() and stk.back().first > a[i]) {
      long long cnt = lst - stk.back().second;
      lst = min(lst, stk.back().second);
      l[i] -= cnt * stk.back().first;
      l[i] += cnt * a[i];
      stk.pop_back();
    }
    l[i] += a[i];
    stk.push_back(make_pair(a[i], lst));
  }
  stk.clear();
  for (int i = n - 1; i >= 0; --i) {
    if (i < n - 1) r[i] = r[i + 1];
    long long lst = i;
    while (not stk.empty() and stk.back().first > a[i]) {
      long long cnt = stk.back().second - lst;
      lst = max(lst, stk.back().second);
      r[i] -= cnt * stk.back().first;
      r[i] += cnt * a[i];
      stk.pop_back();
    }
    r[i] += a[i];
    stk.push_back(make_pair(a[i], lst));
  }
  int bst = 0;
  long long tot = l[0] + r[0] - a[0];
  for (int i = (0); i < (n); ++i) {
    if (l[i] + r[i] - a[i] > tot) {
      bst = i;
      tot = l[i] + r[i] - a[i];
    }
  }
  long long mx = a[bst];
  for (int j = bst - 1; j >= 0; --j) {
    ckmin(mx, a[j]);
    a[j] = mx;
  }
  mx = a[bst];
  for (int j = bst; j < n; ++j) {
    ckmin(mx, a[j]);
    a[j] = mx;
  }
  for (int i = (0); i < (n); ++i) {
    cout << a[i] << ' ';
  }
  cout << endl;
}
