#include <bits/stdc++.h>
using namespace std;
long long int sieve[500005] = {0};
void make_sieve() {
  sieve[1] = 1;
  for (long long int i = 2; i * i <= 1000001; i++)
    if (sieve[i] == 0)
      for (long long int j = 1; j * i <= 1000001; j++) sieve[i * j] = i;
}
long long int power(long long int x, long long int y,
                    long long int m = (long long int)1000000007) {
  if (y == 0) return 1;
  if (y == 1) return x % m;
  long long int r = power(x, y >> 1, m);
  if (y % 2 == 0)
    return (r * r) % m;
  else
    return (((r * r) % m) * x) % m;
}
long long int arr[500005] = {0};
int main() {
  long long int t = 1;
  while (t--) {
    long long int left[500005] = {0};
    long long int right[500005] = {0};
    long long int n;
    cin >> n;
    for (long long int(i) = (long long int)(1); (i) <= (long long int)(n);
         (i)++) {
      cin >> arr[i];
    }
    stack<long long int> l;
    for (long long int(i) = (long long int)(1); (i) <= (long long int)(n);
         (i)++) {
      while (!l.empty() && arr[l.top()] >= arr[i]) l.pop();
      if (l.empty())
        left[i] = arr[i] * i;
      else {
        left[i] = left[l.top()] + (i - l.top()) * arr[i];
      }
      l.push(i);
    }
    reverse(arr + 1, arr + n + 1);
    while (!l.empty()) l.pop();
    for (long long int(i) = (long long int)(1); (i) <= (long long int)(n);
         (i)++) {
      while (!l.empty() && arr[l.top()] >= arr[i]) l.pop();
      if (l.empty())
        right[i] = arr[i] * i;
      else {
        right[i] = right[l.top()] + (i - l.top()) * arr[i];
      }
      l.push(i);
    }
    reverse(arr + 1, arr + n + 1);
    long long int mxind = 0;
    long long int mxval = 0;
    for (long long int(i) = (long long int)(1); (i) <= (long long int)(n);
         (i)++) {
      if (left[i] + right[n - i + 1] - arr[i] > mxval) {
        mxind = i;
        mxval = left[i] + right[n - i + 1] - arr[i];
      }
    }
    long long int *ans = new long long int[500005];
    ans[mxind] = arr[mxind];
    for (long long int(i) = (long long int)(mxind + 1);
         (i) <= (long long int)(n); (i)++) {
      ans[i] = min(ans[i - 1], arr[i]);
    }
    for (long long int j = mxind - 1; j > 0; j--) {
      ans[j] = min(ans[j + 1], arr[j]);
    }
    for (long long int(i) = (long long int)(1); (i) <= (long long int)(n);
         (i)++)
      cout << ans[i] << " ";
    cout << "\n";
  }
  return 0;
}
