#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 6;
const long long M = 1e9 + 7;
long long A[N];
long long leftt[N];
long long rightt[N];
long long leftdp[N];
long long rightdp[N];
long long n;
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> A[i];
  memset(leftt, 0, sizeof(leftt));
  memset(rightt, 0, sizeof(rightt));
  stack<long long> st;
  for (long long i = 0; i < n; i++) {
    while (!st.empty() && A[st.top()] >= A[i]) st.pop();
    if (st.empty())
      leftt[i] = -1;
    else
      leftt[i] = st.top();
    st.push(i);
  }
  while (!st.empty()) st.pop();
  rightt[n - 1] = -1;
  for (long long i = n - 1; i >= 0; i--) {
    while (!st.empty() && A[st.top()] >= A[i]) st.pop();
    if (st.empty())
      rightt[i] = -1;
    else
      rightt[i] = st.top();
    st.push(i);
  }
  memset(leftdp, 0, sizeof(leftdp));
  memset(rightdp, 0, sizeof(rightdp));
  leftdp[0] = A[0];
  rightdp[n - 1] = A[n - 1];
  for (long long i = 0; i < n; i++) {
    if (leftt[i] == -1)
      leftdp[i] = (i + 1) * A[i];
    else
      leftdp[i] = (i - leftt[i]) * A[i] + leftdp[leftt[i]];
  }
  for (long long i = n; i >= 0; i--) {
    if (rightt[i] == -1)
      rightdp[i] = (n - i) * A[i];
    else
      rightdp[i] = (rightt[i] - i) * A[i] + rightdp[rightt[i]];
  }
  long long ans = -1;
  long long id = -1;
  for (long long i = 0; i < n; i++) {
    if (ans < leftdp[i] + rightdp[i] - A[i]) {
      ans = leftdp[i] + rightdp[i] - A[i];
      id = i;
    }
  }
  deque<long long> q;
  long long j = id;
  long long val = 1e15;
  while (j >= 0) {
    val = min(val, A[j]);
    q.push_front(val);
    j--;
  }
  j = id + 1;
  val = 1e16;
  while (j < n) {
    val = min(val, A[j]);
    q.push_back(val);
    j++;
  }
  for (auto i : q) cout << i << " ";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) solve();
}
