#include <bits/stdc++.h>
using namespace std;
stack<pair<long long, long long> > st;
long long a[500005], L[500005], R[500005], arrL[500005], arrR[500005],
    ans[500005];
int main() {
  long long n;
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  L[1] = -1;
  st.push(make_pair(a[1], 1));
  for (long long i = 2; i <= n; i++) {
    if (st.empty()) {
      st.push(make_pair(a[i], i));
      L[i] = -1;
    } else {
      bool f = 1;
      while (!st.empty()) {
        pair<long long, long long> t = st.top();
        if (t.first >= a[i]) {
          st.pop();
        } else {
          f = 0;
          L[i] = t.second;
          st.push(make_pair(a[i], i));
          break;
        }
      }
      if (f) {
        st.push(make_pair(a[i], i));
        L[i] = -1;
      }
    }
  }
  while (!st.empty()) st.pop();
  R[n] = -1;
  st.push(make_pair(a[n], n));
  for (long long i = n - 1; i >= 1; i--) {
    if (st.empty()) {
      R[i] = -1;
      st.push(make_pair(a[i], i));
    } else {
      bool f = 1;
      while (!st.empty()) {
        pair<long long, long long> t = st.top();
        if (t.first >= a[i]) {
          st.pop();
        } else {
          f = 0;
          R[i] = t.second;
          st.push(make_pair(a[i], i));
          break;
        }
      }
      if (f) {
        R[i] = -1;
        st.push(make_pair(a[i], i));
      }
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (L[i] == -1) {
      arrL[i] = i * a[i];
    } else {
      arrL[i] = arrL[L[i]] + (i - L[i]) * a[i];
    }
  }
  for (long long i = n; i >= 1; i--) {
    if (R[i] == -1) {
      arrR[i] = (n - i + 1) * a[i];
    } else {
      arrR[i] = arrR[R[i]] + (R[i] - i) * a[i];
    }
  }
  long long optind = 1, mx = arrL[1] + arrR[1] - a[1];
  for (long long i = 2; i <= n; i++) {
    if (arrL[i] + arrR[i] - a[i] > mx) {
      mx = arrL[i] + arrR[i] - a[i];
      optind = i;
    }
  }
  ans[optind] = a[optind];
  for (long long i = optind - 1; i >= 1; i--) {
    ans[i] = min(ans[i + 1], a[i]);
  }
  for (long long i = optind + 1; i <= n; i++) {
    ans[i] = min(ans[i - 1], a[i]);
  }
  for (long long i = 1; i <= n; i++) {
    printf("%lld ", ans[i]);
  }
  printf("\n");
  return 0;
}
