#include <bits/stdc++.h>
using namespace std;
int n;
int a[500500];
int b[500500];
long long ff[500500];
int fx[500500];
long long bb[500500];
int bx[500500];
vector<int> aa;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  aa.clear();
  aa.push_back(n - 1);
  ff[n - 1] = a[n - 1];
  fx[n - 1] = n;
  for (int i = n - 2; i >= 0; --i) {
    while (aa.size() > 0) {
      int u = aa[aa.size() - 1];
      if (a[u] > a[i]) {
        aa.pop_back();
      } else
        break;
    }
    if (aa.size() == 0) {
      ff[i] = (long long)a[i] * (n - i);
      fx[i] = n;
    } else {
      int u = aa[aa.size() - 1];
      ff[i] = (long long)(u - i) * a[i] + ff[u];
      fx[i] = u;
    }
    aa.push_back(i);
  }
  aa.clear();
  aa.push_back(0);
  bb[0] = a[0];
  bx[0] = -1;
  for (int i = 1; i < n; ++i) {
    while (aa.size() > 0) {
      int u = aa[aa.size() - 1];
      if (a[u] > a[i]) {
        aa.pop_back();
      } else
        break;
    }
    if (aa.size() == 0) {
      bb[i] = (long long)a[i] * (i + 1);
      bx[i] = -1;
    } else {
      int u = aa[aa.size() - 1];
      bb[i] = (long long)(i - u) * a[i] + bb[u];
      bx[i] = u;
    }
    aa.push_back(i);
  }
  long long max = 0;
  int maxi = -1;
  for (int i = 0; i < n; i++) {
    long long res = (long long)bb[i] + ff[i] - a[i];
    if (max < res) {
      max = res;
      maxi = i;
    }
  }
  int k = maxi;
  while (k != -1) {
    int vv = a[k];
    int prev = bx[k];
    while (k > prev) {
      b[k] = vv;
      --k;
    }
    k = prev;
  }
  k = maxi;
  while (k != n) {
    int vv = a[k];
    int next = fx[k];
    while (k < next) {
      b[k] = vv;
      ++k;
    }
    k = next;
  }
  for (int i = 0; i < n; i++) cout << b[i] << " ";
  cout << endl;
}
