#include <bits/stdc++.h>
using namespace std;
void solve(long long arr[], long long pre[], long long n) {
  stack<long long> s;
  for (long long i = 0; i < n; i++) {
    while (!s.empty() && arr[s.top()] > arr[i]) {
      s.pop();
    }
    if (s.empty())
      pre[i] = arr[i] * (i + 1);
    else
      pre[i] = arr[i] * (i - s.top()) + pre[s.top()];
    s.push(i);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  long long arr[n];
  for (long long i = 0; i < n; i++) cin >> arr[i];
  long long pre[n], suf[n];
  solve(arr, pre, n);
  reverse(arr, arr + n);
  solve(arr, suf, n);
  reverse(arr, arr + n);
  reverse(suf, suf + n);
  long long ans = 0, ind = -1;
  for (long long i = 0; i < n; i++) {
    long long sum = pre[i] + suf[i] - arr[i];
    if (sum > ans) {
      ans = sum;
      ind = i;
    }
  }
  long long i = ind;
  long long curr = arr[i];
  long long v = arr[i];
  vector<long long> res;
  for (long long j = i - 1; j >= 0; j--) {
    v = min(v, arr[j]);
    res.push_back(v);
    curr += v;
  }
  reverse(res.begin(), res.end());
  res.push_back(arr[i]);
  v = arr[i];
  for (long long j = i + 1; j < n; j++) {
    v = min(v, arr[j]);
    res.push_back(v);
    curr += v;
  }
  for (long long i = 0; i < n; i++) {
    cout << res[i] << " ";
  }
}
