#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (auto& it : a) cin >> it;
  vector<long long> pre(n), suf(n);
  stack<pair<long long, long long>> stk;
  for (int i = 0; i < int(n); ++i) {
    int now = 1;
    if (i) pre[i] = pre[i - 1];
    while (!stk.empty() && stk.top().second > a[i]) {
      now += stk.top().first;
      pre[i] -= stk.top().first * stk.top().second;
      stk.pop();
    }
    pre[i] += a[i] * now;
    stk.push({now, a[i]});
  }
  stk = stack<pair<long long, long long>>();
  for (int i = int(n) - 1; i >= 0; --i) {
    int now = 1;
    if (i != n - 1) suf[i] = suf[i + 1];
    while (!stk.empty() && stk.top().second > a[i]) {
      now += stk.top().first;
      suf[i] -= stk.top().first * stk.top().second;
      stk.pop();
    }
    suf[i] += a[i] * now;
    stk.push({now, a[i]});
  }
  long long mx = 0, pos;
  for (int i = 0; i < int(n); ++i) {
    if (pre[i] + suf[i] - a[i] > mx) {
      mx = pre[i] + suf[i] - a[i];
      pos = i;
    }
  }
  for (int i = pos - 1; i >= 0; i--) {
    a[i] = min(a[i + 1], a[i]);
  }
  for (int i = pos + 1; i < n; i++) a[i] = min(a[i - 1], a[i]);
  for (auto it : a) cout << it << ' ';
  return 0;
}
