#include <bits/stdc++.h>
using namespace std;
const int MAX = 500100;
int n;
long long m[MAX];
long long arr[MAX], cnt[MAX], cnt_psum[MAX], ret_psum[MAX];
void solve(vector<long long> &ret) {
  arr[1] = m[1];
  cnt[1] = 1;
  ret_psum[1] = 1;
  cnt_psum[1] = 1;
  ret.push_back(m[1]);
  int pivot = 1;
  for (int i = 2; i <= n; ++i) {
    if (arr[pivot] < m[i]) {
      ++pivot;
      arr[pivot] = m[i];
      cnt[pivot] = 1;
      ret_psum[pivot] = ret_psum[pivot - 1] + m[i];
      cnt_psum[pivot] = cnt_psum[pivot - 1] + 1;
      ret.push_back(ret.back() + m[i]);
    } else if (arr[pivot] == m[i]) {
      cnt[pivot] += 1;
      ret_psum[pivot] += m[i];
      cnt_psum[pivot] += 1;
      ret.push_back(ret.back() + m[i]);
    } else {
      int pos = lower_bound(arr + 1, arr + 1 + pivot, m[i]) - arr;
      ret.push_back(ret_psum[pos - 1] +
                    (cnt_psum[pivot] - cnt_psum[pos - 1] + 1) * m[i]);
      arr[pos] = m[i];
      cnt[pos] = cnt_psum[pivot] - cnt_psum[pos - 1] + 1;
      ret_psum[pos] = ret.back();
      cnt_psum[pos] = cnt_psum[pos - 1] + cnt[pos];
      pivot = pos;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> m[i];
  vector<long long> left_ret, right_ret;
  reverse(m + 1, m + 1 + n);
  solve(right_ret);
  reverse(right_ret.begin(), right_ret.end());
  reverse(m + 1, m + 1 + n);
  memset(arr, 0, sizeof(arr));
  memset(cnt, 0, sizeof(cnt));
  memset(cnt_psum, 0, sizeof(cnt_psum));
  memset(ret_psum, 0, sizeof(ret_psum));
  solve(left_ret);
  long long max_sum = -1, pos = -1;
  for (int i = 0; i < n; ++i) {
    long long sum = left_ret[i] + right_ret[i] - m[i + 1];
    if (sum > max_sum) {
      max_sum = sum;
      pos = i;
    }
  }
  ++pos;
  for (int i = pos - 1; i > 0; --i) m[i] = min(m[i + 1], m[i]);
  for (int i = pos + 1; i <= n; ++i) m[i] = min(m[i - 1], m[i]);
  for (int i = 1; i <= n; ++i) cout << m[i] << ' ';
}
