#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long N;
  cin >> N;
  vector<long long> arr(N);
  for (long long i = 0; i < N; i++) cin >> arr[i];
  vector<pair<long long, long long>> dp1(N), dp2(N);
  stack<long long> s;
  for (long long i = N - 1; i >= 0; i--) {
    long long idx;
    while (!s.empty() && arr[s.top()] > arr[i]) s.pop();
    if (s.empty())
      idx = N;
    else
      idx = s.top();
    s.push(i);
    dp1[i] = {(idx - i) * arr[i] + (idx == -1 ? 0 : dp1[idx].first), idx};
  }
  s = stack<long long>();
  for (long long i = 0; i < N; i++) {
    long long idx;
    while (!s.empty() && arr[s.top()] > arr[i]) s.pop();
    if (s.empty())
      idx = -1;
    else
      idx = s.top();
    s.push(i);
    dp2[i] = {(i - idx) * arr[i] + (idx == -1 ? 0 : dp2[idx].first), idx};
  }
  long long best = -1;
  long long bestVal = -1;
  for (long long i = 0; i < N; i++) {
    long long val = dp1[i].first + dp2[i].first - arr[i];
    if (val > bestVal) {
      bestVal = val;
      best = i;
    }
  }
  vector<long long> res(N);
  long long idx = best;
  while (idx >= 0) {
    long long next = dp2[idx].second;
    for (long long i = next + 1; i <= idx; i++) res[i] = arr[idx];
    idx = next;
  }
  idx = best + 1;
  while (idx < N) {
    long long next = dp1[idx].second;
    for (long long i = idx; i < next; i++) res[i] = arr[idx];
    idx = next;
  }
  for (long long i = 0; i < N; i++) cout << res[i] << " ";
  cout << endl;
}
