#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long md = 1000000007) {
  long long res = 1;
  x %= md;
  while (y) {
    if (y & 1) res = (res * x) % md;
    x *= x;
    if (x >= md) x %= md;
    y >>= 1;
  }
  return res;
}
struct node {
  vector<long long> v, w;
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  vector<long long> pre = v;
  for (long long i = 1; i < n; i++) pre[i] = pre[i - 1] + v[i];
  vector<long long> mx(n, 0), mx1(n, 0);
  mx[0] = v[0];
  stack<long long> st;
  st.push(0);
  for (long long i = 1; i < n; i++) {
    while (!st.empty() && v[st.top()] >= v[i]) st.pop();
    if (v[i] >= v[i - 1])
      mx[i] = mx[i - 1] + v[i];
    else {
      long long index = 0;
      if (!st.empty()) index = st.top() + 1;
      mx[i] = (i - index + 1) * v[i];
      if (index) mx[i] += mx[index - 1];
    }
    st.push(i);
  }
  while (st.size()) st.pop();
  mx1[n - 1] = v[n - 1];
  st.push(n - 1);
  for (long long i = n - 1; i >= 0; i--) {
    while (!st.empty() && v[st.top()] >= v[i]) st.pop();
    if (v[i] >= v[i + 1])
      mx1[i] = mx1[i + 1] + v[i];
    else {
      long long index = n - 1;
      if (!st.empty()) index = st.top() - 1;
      mx1[i] = (index - i + 1) * v[i];
      if (index != n - 1) mx1[i] += mx1[index + 1];
    }
    st.push(i);
  }
  long long mxx = -1;
  long long index = -1;
  for (long long i = 0; i < n; i++) {
    if (mx[i] + mx1[i] - v[i] > mxx) {
      mxx = mx[i] + mx1[i] - v[i];
      index = i;
    }
  }
  vector<long long> ans(n, 0);
  ans[index] = v[index];
  for (long long i = index - 1; i >= 0; i--) {
    ans[i] = min(v[i], ans[i + 1]);
  }
  for (long long i = index + 1; i < n; i++) {
    ans[i] = min(v[i], ans[i - 1]);
  }
  for (long long i = 0; i < n; i++) cout << ans[i] << " ";
}
