#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 10;
long long n, a[maxn], l[maxn], r[maxn], res[maxn];
signed main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  vector<long long> q;
  q.push_back(0);
  for (long long i = 1; i <= n; i++) {
    while (!q.empty() && a[q.back()] >= a[i]) q.pop_back();
    long long t = q.back();
    l[i] = l[t] + a[i] * (i - t);
    q.push_back(i);
  }
  q.clear();
  q.push_back(n + 1);
  for (long long i = n; i >= 1; i--) {
    while (!q.empty() && a[q.back()] >= a[i]) q.pop_back();
    long long t = q.back();
    r[i] = r[t] + a[i] * (t - i);
    q.push_back(i);
  }
  long long ans = 0, pos = 0;
  for (long long i = 1; i <= n; i++) {
    long long temp = l[i] + r[i] - a[i];
    if (temp > ans) {
      ans = temp, pos = i;
    }
  }
  res[pos] = a[pos];
  for (long long i = pos - 1; i >= 1; i--) res[i] = min(a[i], res[i + 1]);
  for (long long i = pos + 1; i <= n; i++) res[i] = min(a[i], res[i - 1]);
  for (long long i = 1; i <= n; i++) cout << res[i] << " ";
  cout << endl;
  return 0;
}
