#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int a;
vector<long long> m, pr, sf;
int main() {
  cin.tie(0);
  cout.tie(0);
  cin.sync_with_stdio(0);
  cout.sync_with_stdio(0);
  ;
  cin >> a;
  m = vector<long long>(a);
  pr = vector<long long>(a);
  sf = vector<long long>(a);
  for (int q = 0; q < a; q++) cin >> m[q];
  pr[0] = m[0];
  deque<pair<long long, long long> > d = {{m[0], 1}};
  for (int q = 1; q < a; q++) {
    long long tyt = pr[q - 1], del = 0;
    for (; d.size() && m[q] < d.back().first;) {
      pair<long long, long long> p = d.back();
      tyt -= p.second * (p.first - m[q]);
      del += p.second;
      d.pop_back();
    }
    pr[q] = tyt + m[q];
    d.push_back({m[q], 1 + del});
  }
  d = {{m[a - 1], 1}};
  sf[a - 1] = m[a - 1];
  for (int q = a - 2; q >= 0; q--) {
    long long tyt = sf[q + 1], del = 0;
    for (; d.size() && m[q] < d.back().first;) {
      pair<long long, long long> p = d.back();
      tyt -= p.second * (p.first - m[q]);
      del += p.second;
      d.pop_back();
    }
    sf[q] = tyt + m[q];
    d.push_back({m[q], 1 + del});
  }
  long long mx = 0, i = -1;
  for (int q = 0; q < a; q++) {
    long long tyt = pr[q] + sf[q] - m[q];
    if (tyt > mx) {
      mx = tyt;
      i = q;
    }
  }
  for (int q = i - 1; q >= 0; q--) m[q] = min(m[q], m[q + 1]);
  for (int q = i + 1; q < a; q++) m[q] = min(m[q], m[q - 1]);
  for (long long u : m) cout << u << " ";
}
