#include <bits/stdc++.h>
using namespace std;
const long long MAX = 5e5 + 5;
long long n, H[MAX];
void build1(long long arr[]) {
  stack<pair<long long, long long> > st;
  long long sum = 0;
  st.push(pair<long long, long long>(H[1], 1));
  sum += H[1];
  for (int i = 2; i <= n; ++i) {
    long long len = 0;
    while (st.size() && st.top().first >= H[i]) {
      sum -= st.top().first * st.top().second;
      len += st.top().second;
      st.pop();
    }
    sum += len * H[i];
    arr[i] = sum;
    sum += H[i];
    st.push(pair<long long, long long>(H[i], len + 1));
  }
  return;
}
long long arr[MAX], brr[MAX];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> H[i];
  build1(arr);
  for (int i = 1; i <= (n + 1) / 2; ++i) swap(H[i], H[n + 1 - i]);
  build1(brr);
  for (int i = 1; i <= (n + 1) / 2; ++i) swap(H[i], H[n + 1 - i]);
  pair<long long, long long> ma = pair<long long, long long>(-1, -1);
  for (int i = 1; i <= n; ++i) {
    ma = max(ma, pair<long long, long long>(arr[i] + brr[n + 1 - i] + H[i], i));
  }
  for (int i = ma.second - 1; i > 0; --i) {
    H[i] = min(H[i], H[i + 1]);
  }
  for (int i = ma.second + 1; i <= n; ++i) {
    H[i] = min(H[i], H[i - 1]);
  }
  for (int i = 1; i <= n; ++i) cout << H[i] << ' ';
  cout << '\n';
}
