#include <bits/stdc++.h>
using namespace std;
int arr[555555], vis1[555555];
long long int dp1[555555], dp2[555555], val1[555555], val2[555555], ans[555555];
int main(void) {
  int n;
  scanf("%d", &n);
  for (int e = 1; e <= n; e++) scanf("%d", &arr[e]);
  for (int e = 1; e <= n; e++) dp1[e] = arr[e] + dp1[e - 1];
  for (int e = n; e >= 1; e--) dp2[e] = arr[e] + dp2[e + 1];
  priority_queue<pair<int, int> > q;
  for (int e = 1; e <= n; e++) {
    val1[e] = val1[e - 1];
    int la = e, err = 0;
    long long int ttot = 0;
    while (!q.empty()) {
      int now = q.top().first, wh = q.top().second;
      if (now > arr[e]) {
        ttot += (long long int)(now - arr[e]) * (long long int)(la - wh);
        la = min(la, wh);
        q.pop();
      } else {
        if (now == arr[e]) err++;
        break;
      }
    }
    val1[e] += ttot;
    if (err == 0) q.push(make_pair(arr[e], la));
  }
  q = priority_queue<pair<int, int> >();
  for (int e = n; e >= 1; e--) {
    val2[e] = val2[e + 1];
    int la = e, err = 0;
    long long int ttot = 0;
    while (!q.empty()) {
      int now = q.top().first, wh = q.top().second;
      if (now > arr[e]) {
        ttot += (long long int)(now - arr[e]) * (long long int)(wh - la);
        la = max(la, wh);
        q.pop();
      } else {
        if (now == arr[e]) err++;
        break;
      }
    }
    val2[e] += ttot;
    if (err == 0) q.push(make_pair(arr[e], la));
  }
  int wh = -1;
  long long int mm = 1e18;
  for (int e = 1; e <= n; e++) {
    if (val1[e] + val2[e] < mm) {
      mm = val1[e] + val2[e];
      wh = e;
    }
  }
  ans[wh] = arr[wh];
  int m2 = arr[wh];
  for (int e = wh - 1; e >= 1; e--) {
    m2 = min(m2, arr[e]);
    ans[e] = m2;
  }
  m2 = arr[wh];
  for (int e = wh + 1; e <= n; e++) {
    m2 = min(m2, arr[e]);
    ans[e] = m2;
  }
  for (int e = 1; e <= n; e++) printf("%d ", ans[e]);
  return 0;
}
