#include <bits/stdc++.h>
using namespace std;
const long long int MAXN = 1e6 + 10;
vector<long long int> tmp;
long long int arr[MAXN], n, rights[MAXN], lefts[MAXN];
long long int height(long long int i) { return lefts[i] + rights[i] - arr[i]; }
void cal_l(long long int i) {
  while (!tmp.empty() && arr[tmp.back()] >= arr[i]) {
    tmp.pop_back();
  }
  if (tmp.empty())
    lefts[i] = arr[i] * i;
  else
    lefts[i] = lefts[tmp.back()] + (i - tmp.back()) * arr[i];
  tmp.push_back(i);
}
void cal_r(long long int i) {
  while (!tmp.empty() && arr[tmp.back()] >= arr[i]) {
    tmp.pop_back();
  }
  if (tmp.empty())
    rights[i] = arr[i] * (n - i + 1);
  else
    rights[i] = rights[tmp.back()] + (tmp.back() - i) * arr[i];
  tmp.push_back(i);
}
void solve() {
  long long int max = -1, index = 0;
  for (long long int i = 1; i <= n; i++) {
    if (height(i) > max) {
      max = height(i);
      index = i;
    }
  }
  for (long long int i = index - 1; i > 0; i--) {
    arr[i] = min(arr[i], arr[i + 1]);
  }
  for (long long int i = index + 1; i <= n; i++) {
    arr[i] = min(arr[i], arr[i - 1]);
  }
  for (long long int i = 1; i <= n; i++) cout << arr[i] << " ";
}
int main() {
  cin >> n;
  for (long long int i = 1; i <= n; i++) {
    cin >> arr[i];
    cal_l(i);
  }
  tmp.clear();
  for (long long int i = n; i > 0; i--) {
    cal_r(i);
  }
  solve();
  return 0;
}
