#include <bits/stdc++.h>
using namespace std;
vector<long long> solve(vector<int> a) {
  vector<pair<int, int>> st;
  long long cur = 0;
  vector<long long> res;
  for (auto x : a) {
    int cnt = 0;
    while (!st.empty() && st.back().first >= x) {
      cnt += st.back().second;
      cur -= st.back().second * 1ll * st.back().first;
      st.pop_back();
    }
    st.push_back(make_pair(x, cnt + 1));
    cur += x * 1ll * (cnt + 1);
    res.push_back(cur - x);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<long long> l = solve(a);
  reverse(a.begin(), a.end());
  vector<long long> r = solve(a);
  reverse(r.begin(), r.end());
  reverse(a.begin(), a.end());
  long long res = -1;
  int index = 0;
  for (int i = 0; i < n; ++i) {
    if (l[i] + r[i] + a[i] > res) {
      res = l[i] + r[i] + a[i];
      index = i;
    }
  }
  for (int i = index - 1; i >= 0; --i) {
    a[i] = min(a[i], a[i + 1]);
  }
  for (int i = index + 1; i < n; ++i) {
    a[i] = min(a[i], a[i - 1]);
  }
  for (auto x : a) {
    cout << x << ' ';
  }
  return 0;
}
