#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i;
  cin >> n;
  long long int a[n], sum[n + 1], pre[n], suf[n], ans[n];
  memset(sum, 0, sizeof(sum));
  sum[0] = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    sum[i + 1] = sum[i] + a[i];
  }
  stack<pair<long long int, int> > st;
  st.push({a[0], 0});
  pre[0] = a[0];
  for (i = 1; i < n; i++) {
    while (!st.empty() && st.top().first > a[i]) st.pop();
    if (!st.empty()) {
      pre[i] = pre[st.top().second] + (i - st.top().second) * a[i];
    } else
      pre[i] = (i + 1) * a[i];
    st.push({a[i], i});
  }
  stack<pair<long long int, int> > st2;
  st2.push({a[n - 1], (n - 1)});
  suf[n - 1] = a[n - 1];
  for (i = n - 1; i >= 0; i--) {
    while (!st2.empty() && st2.top().first > a[i]) st2.pop();
    if (!st2.empty()) {
      suf[i] = suf[st2.top().second] + (st2.top().second - i) * a[i];
    } else
      suf[i] = (n - i) * a[i];
    st2.push({a[i], i});
  }
  long long int maxm = -1, imaxm, tot, minm;
  for (i = 0; i < n; i++) {
    tot = pre[i] + suf[i] - a[i];
    if (tot > maxm) {
      maxm = tot;
      imaxm = i;
    }
  }
  minm = a[imaxm];
  for (i = imaxm; i >= 0; i--) {
    if (a[i] > minm)
      ans[i] = minm;
    else {
      minm = a[i];
      ans[i] = minm;
    }
  }
  minm = a[imaxm];
  for (i = imaxm; i < n; i++) {
    if (a[i] > minm)
      ans[i] = minm;
    else {
      minm = a[i];
      ans[i] = minm;
    }
  }
  for (i = 0; i < n; i++) cout << ans[i] << " ";
}
