#include <bits/stdc++.h>
using namespace std;
long long a[500005], sum[500005];
stack<pair<int, int> > cur;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    int len = 1;
    long long tru = 0;
    while (!cur.empty() && cur.top().first > a[i]) {
      long long l = cur.top().second;
      tru = tru + l * (long long)(cur.top().first - a[i]);
      len += cur.top().second;
      cur.pop();
    }
    sum[i] = sum[i - 1] - tru + a[i];
    cur.push(pair<int, int>(a[i], len));
  }
  long long tmp = 0;
  while (!cur.empty()) {
    cur.pop();
  }
  long long ans = 0;
  int index = 1;
  for (int i = n; i >= 1; i--) {
    int len = 1;
    long long tru = 0;
    while (!cur.empty() && cur.top().first > a[i]) {
      long long l = cur.top().second;
      tru = tru + l * (long long)(cur.top().first - a[i]);
      len += cur.top().second;
      cur.pop();
    }
    tmp = tmp - tru + a[i];
    cur.push(pair<int, int>(a[i], len));
    if (tmp + sum[i] - a[i] > ans) {
      index = i;
      ans = tmp + sum[i] - a[i];
    }
  }
  tmp = a[index];
  for (int i = index - 1; i >= 1; i--) {
    a[i] = min(tmp, a[i]);
    tmp = a[i];
  }
  tmp = a[index];
  for (int i = index + 1; i <= n; i++) {
    a[i] = min(tmp, a[i]);
    tmp = a[i];
  }
  for (int i = 1; i <= n; i++) cout << a[i] << ' ';
}
