#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  vector<long long> m(n);
  for (long long i = 0; i < n; i++) cin >> m[i];
  vector<long long> inc(n, 0);
  vector<long long> dec(n, 0);
  inc[0] = m[0];
  long long prev = m[0];
  vector<pair<long long, long long> > st = {{prev, 1}};
  for (long long i = 1; i < n; i++) {
    pair<long long, long long> b = st.back();
    if (m[i] == b.first) {
      inc[i] = m[i] + inc[i - 1];
      st[st.size() - 1].second++;
    } else if (m[i] > b.first) {
      inc[i] = m[i] + inc[i - 1];
      st.push_back({m[i], 1});
    } else {
      inc[i] = inc[i - 1];
      long long curr = 1;
      while (!st.empty() && st.back().first >= m[i]) {
        curr += st.back().second;
        inc[i] -= st.back().second * st.back().first;
        st.pop_back();
      }
      st.push_back({m[i], curr});
      inc[i] += curr * m[i];
    }
  }
  dec[n - 1] = m[n - 1];
  st = {{m[n - 1], 1}};
  for (long long i = n - 2; i >= 0; i--) {
    if (m[i] == st.back().first) {
      dec[i] = m[i] + dec[i + 1];
      st[st.size() - 1].second++;
    } else if (m[i] > st.back().first) {
      dec[i] = m[i] + dec[i + 1];
      st.push_back({m[i], 1});
    } else {
      dec[i] = dec[i + 1];
      long long curr = 1;
      while (!st.empty() && st.back().first >= m[i]) {
        curr += st.back().second;
        dec[i] -= st.back().second * st.back().first;
        st.pop_back();
      }
      st.push_back({m[i], curr});
      dec[i] += curr * m[i];
    }
  }
  long long peak = -1;
  long long sum = -1;
  for (long long i = 0; i < n; i++) {
    long long curr_sum = dec[i] + inc[i] - m[i];
    if (curr_sum > sum) {
      sum = curr_sum;
      peak = i;
    }
  }
  vector<long long> res(n, 0);
  res[peak] = m[peak];
  for (long long i = peak + 1; i < n; i++) {
    res[i] = min(res[i - 1], m[i]);
  }
  for (long long i = peak - 1; i >= 0; i--) {
    res[i] = min(res[i + 1], m[i]);
  }
  cout << res[0];
  for (long long i = 1; i < n; i++) cout << " " << res[i];
  cout << "\n";
}
