#include <bits/stdc++.h>
using namespace std;
const long long inf = 10000000033;
long long din1[500003], din2[500003], arr[500003], rj[500003];
stack<long long> st;
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  st.push(0ll);
  long long tp;
  for (long long i = 1; i <= n; i++) {
    tp = st.top();
    if (arr[i] >= arr[tp]) {
      st.push(i);
      din1[i] = (i - tp) * arr[i] + din1[tp];
    } else {
      while (arr[tp] > arr[i]) {
        st.pop();
        tp = st.top();
      }
      din1[i] = (i - tp) * arr[i] + din1[tp];
      st.push(i);
    }
  }
  while (!st.empty()) {
    st.pop();
  }
  st.push(n + 1LL);
  for (long long i = n; i > 0; i--) {
    tp = st.top();
    if (arr[i] >= arr[tp]) {
      st.push(i);
      din2[i] = din2[tp] + (tp - i) * arr[i];
    } else {
      while (arr[tp] > arr[i]) {
        st.pop();
        tp = st.top();
      }
      din2[i] = din2[tp] + (tp - i) * arr[i];
      st.push(i);
    }
  }
  long long mx = 0LL, ind;
  for (long long i = 1; i <= n; i++) {
    if (din1[i] + din2[i] - arr[i] > mx) {
      mx = din1[i] + din2[i] - arr[i];
      ind = i;
    }
  }
  rj[ind] = arr[ind];
  long long mn = arr[ind];
  for (long long i = ind - 1; i > 0; i--) {
    if (arr[i] < mn) {
      mn = arr[i];
    }
    rj[i] = mn;
  }
  mn = arr[ind];
  for (long long i = ind + 1; i <= n; i++) {
    if (arr[i] < mn) mn = arr[i];
    rj[i] = mn;
  }
  for (long long i = 1; i <= n; i++) {
    cout << rj[i] << " ";
  }
  return 0;
}
