#include <bits/stdc++.h>
using namespace std;
vector<long long> prefunc(vector<long long> v) {
  vector<pair<long long, long long>> q;
  long long N = (long long)(v.size());
  vector<long long> pref(N, 0);
  q.emplace_back(v[0], 1);
  for (long long i = 1; i < N; ++i) {
    pref[i] = pref[i - 1] + v[i - 1];
    long long co = 1;
    while (!q.empty() && q.back().first > v[i]) {
      pref[i] =
          pref[i] - q.back().first * q.back().second + v[i] * q.back().second;
      co += q.back().second;
      q.pop_back();
    }
    q.emplace_back(v[i], co);
  }
  return pref;
}
void solve() {
  long long N;
  cin >> N;
  vector<long long> v(N);
  vector<long long> res(N);
  long long ms = 0;
  long long maxval = 0;
  for (long long i = 0; i < N; ++i) cin >> v[i];
  vector<long long> pref = prefunc(v);
  reverse((v).begin(), (v).end());
  vector<long long> suf = prefunc(v);
  reverse((suf).begin(), (suf).end());
  long long mxs = 0;
  reverse((v).begin(), (v).end());
  for (long long i = 0; i < N; ++i) {
    if (pref[i] + suf[i] + v[i] > pref[mxs] + suf[mxs] + v[mxs]) mxs = i;
  }
  long long i = mxs;
  vector<long long> tmp(N);
  tmp[i] = v[i];
  long long sum = v[i];
  for (long long j = i + 1; j < N; ++j) {
    tmp[j] = min(v[j], tmp[j - 1]);
    sum += tmp[j];
  }
  for (long long j = i - 1; j >= 0; --j) {
    tmp[j] = min(v[j], tmp[j + 1]);
    sum += tmp[j];
  }
  for (long long x : tmp) cout << x << ' ';
}
signed main() {
  long long t;
  t = 1;
  while (t--) solve();
  return 0;
}
