#include <bits/stdc++.h>
using namespace std;
int n, m[500002], lm[500002] = {0}, rm[500002] = {0};
int peak = 0, a[500002] = {0};
long long ans = 0, pre[500002] = {0}, sur[500002] = {0};
void findlm() {
  stack<int> sta;
  sta.push(n);
  for (int i = n - 1; i >= 1; i--) {
    while (!sta.empty() && m[i] <= m[sta.top()]) lm[sta.top()] = i, sta.pop();
    sta.push(i);
  }
}
void findrm() {
  stack<int> sta;
  sta.push(1);
  for (int i = 2; i <= n; i++) {
    while (!sta.empty() && m[i] <= m[sta.top()]) rm[sta.top()] = i, sta.pop();
    sta.push(i);
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> m[i];
    lm[i] = 0, rm[i] = n + 1;
  }
  findlm();
  findrm();
  for (int i = 1; i <= n; i++) pre[i] = pre[lm[i]] + m[i] * 1LL * (i - lm[i]);
  for (int i = n; i >= 1; i--) sur[i] = sur[rm[i]] + m[i] * 1LL * (rm[i] - i);
  for (int i = 1; i <= n; i++) {
    long long tmp = pre[i] + sur[i] - m[i] * 1LL;
    if (tmp > ans) peak = i, ans = tmp;
  }
  a[peak] = m[peak];
  for (int j = peak - 1; j >= 1; j--) a[j] = min(m[j], a[j + 1]);
  for (int k = peak + 1; k <= n; k++) a[k] = min(m[k], a[k - 1]);
  for (int i = 1; i <= n; i++) cout << a[i] << " ";
  return 0;
}
