#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> arr(n + 1);
  for (int i = 1; i <= n; i++) cin >> arr[i];
  vector<long long> left(n + 1), right(n + 1);
  left[0] = 0;
  left[1] = 1;
  int i = 2;
  stack<int> dea;
  dea.push(1);
  while (i <= n) {
    while (!dea.empty() && arr[dea.top()] > arr[i]) dea.pop();
    if (!dea.empty())
      left[i] = dea.top();
    else {
      left[i] = i;
    }
    dea.push(i);
    i++;
  }
  i = n - 1;
  right[n] = n;
  while (!dea.empty()) dea.pop();
  dea.push(n);
  while (i > 0) {
    while (!dea.empty() && arr[dea.top()] > arr[i]) dea.pop();
    if (!dea.empty()) {
      right[i] = dea.top();
    } else {
      right[i] = i;
    }
    dea.push(i);
    i--;
  }
  vector<long long> d(n + 1), dd(n + 1);
  for (int i = 1; i <= n; i++) {
    if (left[i] == i)
      d[i] = arr[i] * i;
    else
      d[i] = d[left[i]] + ((i - left[i]) * arr[i]);
  }
  for (int i = n; i >= 1; i--) {
    if (right[i] == i)
      dd[i] = arr[i] * (n + 1 - i);
    else
      dd[i] = dd[right[i]] + ((right[i] - i) * arr[i]);
  }
  long long m = 0, ma = 0;
  for (int i = 1; i <= n; i++) {
    if ((d[i] + dd[i] - arr[i]) > ma) {
      ma = d[i] + dd[i] - arr[i];
      m = i;
    }
  }
  vector<long long> ans(n + 1);
  ans[m] = arr[m];
  for (int j = m + 1; j <= n; j++) {
    ans[j] = min(arr[j], ans[j - 1]);
  }
  for (int j = m - 1; j >= 0; j--) {
    ans[j] = min(ans[j + 1], arr[j]);
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
}
