#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const long long MOD = 1e9 + 7;
const long long INF = 0x3f3f3f3f;
const long long MAXN = 1e5 + 7;
long long n, a[MAXN], dp[MAXN], pd[MAXN], ans;
deque<long long> q;
void process(long long v[], long long d[]) {
  while (!q.empty()) q.pop_back();
  q.push_back(0);
  for (int i = 1; i <= n; ++i) {
    while (!q.empty() && v[q.back()] >= v[i]) q.pop_back();
    d[i] = d[q.back()] + (i - q.back()) * v[i];
    q.push_back(i);
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  process(a, dp);
  for (int i = 1, j = n; i < j; ++i, --j) swap(a[i], a[j]);
  process(a, pd);
  for (int i = 1, j = n; i < j; ++i, --j) swap(a[i], a[j]);
  int pos = 0;
  for (int i = 1; i <= n; ++i) {
    if (ans < dp[i] + pd[n - i]) {
      ans = dp[i] + pd[n - i];
      pos = i;
    }
  }
  for (int i = pos - 1; i; --i) a[i] = min(a[i], a[i + 1]);
  for (int i = pos + 2; i <= n; ++i) a[i] = min(a[i], a[i - 1]);
  for (int i = 1; i <= n; ++i) cout << a[i] << " ";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int i, Case = 1;
  while (Case--) solve();
  return 0;
}
