#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAXN = 5e5 + 5;
long long A[MAXN], n;
long long res[MAXN];
long long l[MAXN], r[MAXN];
long long pref[MAXN], suf[MAXN];
void calc_pref() {
  stack<int> S;
  for (int i = 1; i <= n; i++) {
    while (!S.empty() && A[S.top()] > A[i]) S.pop();
    S.empty() ? pref[i] = i : pref[i] = S.top();
    S.push(i);
  }
}
void calc_suf() {
  stack<int> S;
  for (int i = n; i >= 1; i--) {
    while (!S.empty() && A[S.top()] > A[i]) S.pop();
    S.empty() ? suf[i] = i : suf[i] = S.top();
    S.push(i);
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  calc_pref();
  calc_suf();
  l[1] = A[1];
  for (int i = 2; i <= n; i++) {
    if (pref[i] != i) {
      l[i] = l[pref[i]] + (i - pref[i]) * A[i];
    } else {
      l[i] = i * A[i];
    }
  }
  r[n] = A[n];
  for (int i = n - 1; i >= 1; i--) {
    if (suf[i] != i) {
      r[i] = r[suf[i]] + (suf[i] - i) * A[i];
    } else {
      r[i] = (n - i + 1) * A[i];
    }
  }
  long long pos = 0, m = 0;
  for (int i = 1; i <= n; i++) {
    if (l[i] + r[i] - A[i] > m) {
      pos = i;
      m = l[i] + r[i] - A[i];
    }
  }
  res[pos] = A[pos];
  for (int i = pos - 1; i >= 1; i--) {
    res[i] = min(A[i], res[i + 1]);
  }
  for (int i = pos + 1; i <= n; i++) {
    res[i] = min(A[i], res[i - 1]);
  }
  for (int i = 1; i <= n; i++) {
    cout << res[i] << " ";
  }
}
int main() {
  int test = 1;
  while (test--) {
    solve();
  }
}
