#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a;
      res %= mod;
    }
    a = a * a;
    a %= mod;
    b >>= 1;
  }
  return res % mod;
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<long long> l(n), r(n);
  stack<long long> st1, st2;
  for (long long i = n - 1; i >= 0; i--) {
    while (!st1.empty() && v[st1.top()] > v[i]) {
      st1.pop();
    }
    if (st1.empty()) {
      r[i] = (n - i) * v[i];
    } else {
      r[i] = (st1.top() - i) * v[i] + r[st1.top()];
    }
    st1.push(i);
  }
  for (long long i = 0; i < n; i++) {
    while (!st2.empty() && v[st2.top()] > v[i]) {
      st2.pop();
    }
    if (st2.empty()) {
      l[i] = (i + 1) * v[i];
    } else {
      l[i] = (i - st2.top()) * v[i] + l[st2.top()];
    }
    st2.push(i);
  }
  long long ind, res = LLONG_MIN;
  for (long long i = 0; i < n; i++) {
    if (res < l[i] + r[i] - v[i]) {
      res = l[i] + r[i] - v[i];
      ind = i;
    }
  }
  vector<long long> ans(n);
  ans[ind] = v[ind];
  for (long long i = ind - 1; i >= 0; i--) {
    ans[i] = min(ans[i + 1], v[i]);
  }
  for (long long i = ind + 1; i < n; i++) {
    ans[i] = min(ans[i - 1], v[i]);
  }
  for (long long i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
