#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  long long m[n];
  for (long long i = 0; i < n; i++) {
    cin >> m[i];
  }
  vector<long long> ls(n);
  stack<pair<long long, long long> > s;
  for (long long i = 0; i < n; i++) {
    while (!s.empty() && s.top().first >= m[i]) {
      s.pop();
    }
    if (s.empty())
      ls[i] = -1;
    else
      ls[i] = s.top().second;
    s.push({m[i], i});
  }
  vector<long long> rs(n);
  while (!s.empty()) s.pop();
  for (long long i = n - 1; i >= 0; i--) {
    while (!s.empty() && s.top().first >= m[i]) s.pop();
    if (s.empty())
      rs[i] = -1;
    else
      rs[i] = s.top().second;
    s.push({m[i], i});
  }
  long long l[n];
  for (long long i = 0; i < n; i++) {
    if (ls[i] == -1)
      l[i] = (i + 1) * m[i];
    else {
      long long j = ls[i];
      l[i] = l[j] + (i - j) * m[i];
    }
  }
  long long r[n];
  for (long long i = n - 1; i >= 0; i--) {
    if (rs[i] == -1)
      r[i] = (n - i) * m[i];
    else {
      long long j = rs[i];
      r[i] = r[j] + (j - i) * m[i];
    }
  }
  long long floors = 0, index = -1;
  for (long long i = 0; i < n; i++) {
    if (l[i] + r[i] - m[i] > floors) {
      floors = l[i] + r[i] - m[i];
      index = i;
    }
  }
  long long ans[n];
  ans[index] = m[index];
  long long last = m[index];
  for (long long j = index - 1; j >= 0; j--) {
    if (m[j] >= last) {
      ans[j] = last;
    } else {
      ans[j] = m[j];
      last = m[j];
    }
  }
  last = m[index];
  for (long long j = index + 1; j < n; j++) {
    if (m[j] >= last) {
      ans[j] = last;
    } else {
      ans[j] = m[j];
      last = m[j];
    }
  }
  for (long long i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
