#include <bits/stdc++.h>
using namespace std;
int ar[500005], br[500005];
char ch[500005];
long long Suffix[500005], Prefix[500005];
int n, m;
stack<int> St;
void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &ar[i]);
  long long re = 0, idx = 0;
  for (int i = 1; i <= n; i++) {
    while (St.size() && ar[St.top()] >= ar[i]) St.pop();
    int bat = 0;
    if (St.size()) bat = St.top();
    Suffix[i] = 1LL * (i - bat) * ar[i] + Suffix[bat];
    St.push(i);
  }
  while (!St.empty()) St.pop();
  for (int i = n; i >= 1; i--) {
    while (St.size() && ar[St.top()] >= ar[i]) St.pop();
    int bat = n + 1;
    if (St.size()) bat = St.top();
    Prefix[i] = 1LL * (bat - i) * ar[i] + Prefix[bat];
    St.push(i);
  }
  for (int i = 1; i <= n; i++) {
    long long val = Prefix[i] + Suffix[i] - ar[i];
    if (val > re) {
      re = val;
      idx = i;
    }
  }
  int val = ar[idx];
  for (int j = idx - 1; j >= 1; j--) {
    ar[j] = min(val, ar[j]);
    val = ar[j];
  }
  val = ar[idx];
  for (int j = idx + 1; j <= n; j++) {
    ar[j] = min(val, ar[j]);
    val = ar[j];
  }
  for (int i = 1; i <= n; i++) printf("%d ", ar[i]);
  printf("\n");
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
