#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> v(n), nxt(n + 1), prev(n + 1);
  for (int &x : v) cin >> x;
  nxt[n] = n;
  for (int i = n - 1; i >= 0; i--) {
    nxt[i] = i + 1;
    while (nxt[i] < n && v[nxt[i]] >= v[i]) nxt[i] = nxt[nxt[i]];
  }
  prev[0] = -1;
  for (int i = 0; i < n; i++) {
    prev[i] = i - 1;
    while (prev[i] >= 0 && v[prev[i]] >= v[i]) prev[i] = prev[prev[i]];
  }
  vector<long long> dp_l(n + 1), dp_r(n + 1);
  for (int i = n - 1; i >= 0; i--)
    dp_r[i] = (nxt[i] - i) * 1ll * v[i] + dp_r[nxt[i]];
  for (int i = 0; i < n; i++) {
    dp_l[i] = (i - prev[i]) * 1ll * v[i];
    if (~prev[i]) dp_l[i] += dp_l[prev[i]];
  }
  int best = -1;
  long long mx = 0;
  for (int i = 0; i < n; i++) {
    if (dp_l[i] + dp_r[i] - v[i] > mx) {
      best = i;
      mx = dp_l[i] + dp_r[i] - v[i];
    }
  }
  vector<int> a(n);
  int p = 2e9;
  for (int j = best; j < n; j++) {
    p = min(p, v[j]);
    a[j] = p;
  }
  p = v[best];
  for (int j = best - 1; j >= 0; j--) {
    p = min(p, v[j]);
    a[j] = p;
  }
  for (int &x : a) cout << x << ' ';
}
