#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n;
  cin >> n;
  vector<long long> m(n);
  for (long long &a : m) cin >> a;
  vector<long long> most_left(n);
  vector<long long> most_right(n);
  stack<long long> s;
  for (long long i = 0; i < n; i++) {
    while (!s.empty() && m[s.top()] >= m[i]) {
      s.pop();
    }
    if (!s.empty()) {
      most_left[i] = s.top();
    } else {
      most_left[i] = i;
    }
    s.push(i);
  }
  reverse(m.begin(), m.end());
  vector<long long> mr = m;
  reverse(m.begin(), m.end());
  stack<long long> sr;
  for (long long i = 0; i < n; i++) {
    while (!sr.empty() && mr[sr.top()] >= mr[i]) {
      sr.pop();
    }
    if (!sr.empty()) {
      most_right[n - i - 1] = n - sr.top() - 1;
    } else {
      most_right[n - i - 1] = n - i - 1;
    }
    sr.push(i);
  }
  vector<long long> dp_l(n), dp_r(n);
  for (long long i = 0; i < n; i++) {
    if (most_left[i] == i) {
      dp_l[i] = (i + 1) * m[i];
    } else {
      long long j = most_left[i];
      dp_l[i] = (i - j) * m[i] + dp_l[j];
    }
  }
  for (long long i = 0; i < n; i++) {
    if (most_right[n - 1 - i] == n - 1 - i) {
      dp_r[n - i - 1] = (i + 1) * m[n - i - 1];
    } else {
      long long j = most_right[n - i - 1];
      dp_r[n - i - 1] = (j - (n - i - 1)) * m[n - i - 1] + dp_r[j];
    }
  }
  long long ans = -1;
  long long ans_i = -1;
  for (long long i = 0; i < n; i++) {
    long long val = dp_l[i] + dp_r[i] - m[i];
    if (val > ans) {
      ans = val;
      ans_i = i;
    }
  }
  for (long long i = ans_i - 1; i >= 0; i--) {
    if (m[i] > m[i + 1]) {
      m[i] = m[i + 1];
    }
  }
  for (long long i = ans_i + 1; i < n; i++) {
    if (m[i] > m[i - 1]) {
      m[i] = m[i - 1];
    }
  }
  for (long long i = 0; i < n; i++) {
    cout << m[i] << ' ';
  }
  cout << endl;
}
