#include <bits/stdc++.h>
const int kN = 1e6 + 5;
int n;
long long s1[kN], s2[kN], m[kN], ans[kN];
std::vector<int> st;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%lld", m + i);
  st.push_back(0);
  for (int i = 1; i <= n; ++i) {
    while (m[st.back()] > m[i]) st.pop_back();
    s1[i] = s1[st.back()] + m[i] * (i - st.back());
    st.push_back(i);
  }
  st.clear();
  st.push_back(n + 1);
  for (int i = n; i >= 1; --i) {
    while (m[st.back()] > m[i]) st.pop_back();
    s2[i] = s2[st.back()] + m[i] * (st.back() - i);
    st.push_back(i);
  }
  int climax = 1;
  long long maxans = 0;
  for (int i = 1; i <= n; ++i) {
    if (s1[i] + s2[i] - m[i] >= maxans) {
      maxans = s1[i] + s2[i] - m[i];
      climax = i;
    }
  }
  ans[climax] = m[climax];
  for (int i = climax - 1; i > 0; --i) {
    ans[i] = std::min(m[i], ans[i + 1]);
  }
  for (int i = climax + 1; i <= n; ++i) {
    ans[i] = std::min(m[i], ans[i - 1]);
  }
  for (int i = 1; i <= n; ++i) printf("%lld ", ans[i]);
  return 0;
}
