#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 500;
const int P = 1e9 + 7;
int n, a[N], ans[N], l[N][2];
long long pref[N], suf[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  stack<int> first;
  long long c = 0;
  for (int it = 0; it < 2; it++) {
    stack<int> first;
    if (it) {
      for (int i = n; i; i--) {
        while (!first.empty() && a[first.top()] >= a[i]) {
          first.pop();
        }
        if ((int)first.size())
          l[i][it] = first.top();
        else
          l[i][it] = n + 1;
        first.push(i);
      }
      continue;
    }
    for (int i = 1; i <= n; i++) {
      while (!first.empty() && a[first.top()] >= a[i]) {
        first.pop();
      }
      if ((int)first.size()) l[i][it] = first.top();
      first.push(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    pref[i] = a[i] * 1ll * (i - l[i][0]);
    pref[i] += pref[l[i][0]];
  }
  for (int i = n; i; i--) {
    suf[i] = a[i] * 1ll * (l[i][1] - i);
    suf[i] += suf[l[i][1]];
  }
  int p = 1;
  for (int i = 1; i <= n; i++) {
    if ((pref[p] + suf[p] - a[p]) < pref[i] + suf[i] - a[i]) {
      p = i;
    }
  }
  int mn = INT_MAX;
  for (int i = p; i; i--) {
    mn = min(a[i], mn);
    ans[i] = mn;
  }
  mn = INT_MAX;
  for (int i = p; i <= n; i++) {
    mn = min(mn, a[i]);
    ans[i] = mn;
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
}
