#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 100 * 6 + 10;
long long int a[MAXN];
stack<int> second;
long long int p[MAXN];
int d[MAXN];
int d2[MAXN];
long long int dp1[MAXN];
long long int dp2[MAXN];
long long int mod = 1000 * 1000 * 1000 + 7;
long long int inf = 1e17;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  long long int h = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    d[i] = n + 1;
    d2[i] = 0;
    h += a[i];
  }
  for (int i = 1; i <= n; i++) {
    p[i] = p[i - 1] + a[i];
  }
  for (int i = 1; i <= n; i++) {
    while (!second.empty() && a[second.top()] >= a[i]) {
      d[second.top()] = i;
      second.pop();
    }
    second.push(i);
  }
  while (!second.empty()) second.pop();
  for (int i = n; i >= 1; i--) {
    while (!second.empty() && a[second.top()] >= a[i]) {
      d2[second.top()] = i;
      second.pop();
    }
    second.push(i);
  }
  long long int mn = inf;
  int t = 0;
  for (int i = 1; i <= n; i++) {
    dp1[i] = p[i - 1] - p[d2[i]] - (abs(d2[i] - i) - 1) * a[i];
    dp1[i] += dp1[d2[i]];
  }
  for (int i = n; i >= 1; i--) {
    dp2[i] += p[d[i] - 1] - p[i] - (abs(d[i] - i) - 1) * a[i];
    dp2[i] += dp2[d[i]];
  }
  for (int i = 1; i <= n; i++) {
    if (mn > dp1[i] + dp2[i]) {
      mn = dp1[i] + dp2[i];
      t = i;
    }
  }
  long long int c = a[t];
  for (int i = t + 1; i <= n; i++) {
    if (a[i] > c) a[i] = c;
    c = min(c, a[i]);
  }
  c = a[t];
  for (int i = t - 1; i >= 1; i--) {
    if (a[i] > c) a[i] = c;
    c = min(c, a[i]);
  }
  for (int i = 1; i <= n; i++) cout << a[i] << ' ';
  cout << endl;
  return 0;
}
