#include <bits/stdc++.h>
using namespace std;
long long int MAX = 100000000000000000;
long long int n;
long long int dpl[500001];
long long int dpr[500001];
long long int arr[500001];
long long int lt_search[500001];
long long int rt_search[500001];
long long int f_l(long long int i) {
  if (dpl[i] != -1) return dpl[i];
  long long int sum = 0;
  long long int k = -1;
  if (i - 1 >= 1) k = lt_search[i];
  if (k != -1)
    sum = sum + (i - k - 1) * arr[i] + arr[k] + f_l(k);
  else {
    sum += ((i - 1) * arr[i]);
  }
  dpl[i] = sum;
  return sum;
}
long long int f_r(long long int i) {
  if (dpr[i] != -1) return dpr[i];
  long long int sum = 0;
  long long int k = -1;
  if (i + 1 <= n) k = rt_search[i];
  if (k != -1)
    sum = sum + (k - i - 1) * arr[i] + arr[k] + f_r(k);
  else {
    sum = ((n - i) * arr[i]);
  }
  dpr[i] = sum;
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  stack<pair<long long int, long long int> > s;
  for (int i = 1; i <= n; i++) {
    while (1) {
      if (s.empty()) break;
      long long int k = s.top().first;
      if (k < arr[i] || s.empty())
        break;
      else
        s.pop();
    }
    if (s.empty())
      s.push(make_pair(arr[i], i)), lt_search[i] = -1;
    else
      lt_search[i] = s.top().second, s.push(make_pair(arr[i], i));
  }
  while (!s.empty()) s.pop();
  for (int i = n - 1; i >= 0; i--) {
    while (1) {
      if (s.empty()) break;
      long long int k = s.top().first;
      if (k < arr[i] || s.empty())
        break;
      else
        s.pop();
    }
    if (s.empty())
      s.push(make_pair(arr[i], i)), rt_search[i] = -1;
    else
      rt_search[i] = s.top().second, s.push(make_pair(arr[i], i));
  }
  long long int mx = -1;
  for (int i = 1; i <= n; i++) dpl[i] = -1, dpr[i] = -1;
  long long int pk = -1;
  for (int i = 1; i <= n; i++) {
    long long int sum = arr[i];
    sum = sum + f_l(i);
    sum = sum + f_r(i);
    if (sum > mx) {
      mx = sum;
      pk = i;
    }
  }
  long long int res[n + 1];
  res[pk] = arr[pk];
  for (int i = pk - 1; i >= 1; i--) {
    if (arr[i] < res[i + 1]) {
      res[i] = arr[i];
    } else {
      res[i] = res[i + 1];
    }
  }
  for (int i = pk + 1; i <= n; i++) {
    if (arr[i] < res[i - 1])
      res[i] = arr[i];
    else
      res[i] = res[i - 1];
  }
  for (int i = 1; i <= n; i++) cout << res[i] << " ";
  cout << "\n";
  return 0;
}
