#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<long long> v(n + 5);
  for (long long i = 1; i <= n; i++) {
    cin >> v[i];
  }
  vector<long long> pre(n + 10), suf(n + 10);
  stack<long long> st;
  st.push(0);
  for (long long i = 1; i <= n; i++) {
    while (st.size() && v[st.top()] >= v[i]) {
      st.pop();
    }
    pre[i] = pre[st.top()] + (i - st.top()) * v[i];
    st.push(i);
  }
  while (st.size()) st.pop();
  st.push(n + 1);
  for (long long i = n; i >= 1; i--) {
    while (st.size() && v[st.top()] >= v[i]) {
      st.pop();
    }
    suf[i] = suf[st.top()] + abs(i - st.top()) * v[i];
    st.push(i);
  }
  long long ans = 0, in = -1;
  for (long long i = 1; i <= n; i++) {
    if (pre[i] + suf[i] - v[i] > ans) {
      ans = pre[i] + suf[i] - v[i];
      in = i;
    }
  }
  vector<long long> res(n + 10);
  res[in] = v[in];
  long long j = in - 1;
  while (j >= 1) {
    if (v[j] >= res[j + 1])
      res[j] = res[j + 1];
    else
      res[j] = v[j];
    j--;
  }
  j = in + 1;
  while (j <= n) {
    if (v[j] >= res[j - 1])
      res[j] = res[j - 1];
    else
      res[j] = v[j];
    j++;
  }
  for (long long i = 1; i <= n; i++) cout << res[i] << " ";
}
