#include <bits/stdc++.h>
using namespace std;
const int n = 500000;
long long arr[n + 50], dp1[n + 50], dp2[n + 50];
int main() {
  int a;
  cin >> a;
  for (int i = 1; i <= a; i++) cin >> arr[i];
  long long now = arr[1];
  dp1[1] = now;
  vector<long long> V;
  V.push_back(arr[1]);
  map<long long, int> M;
  M[arr[1]] = 1;
  for (int i = 2; i <= a; i++) {
    while (!V.empty() && V.back() > arr[i]) {
      now -= (V.back() - arr[i]) * M[V.back()];
      M[arr[i]] += M[V.back()];
      M[V.back()] = 0;
      V.pop_back();
    }
    V.push_back(arr[i]);
    now += arr[i];
    M[arr[i]]++;
    dp1[i] = now;
  }
  V.clear();
  V.push_back(arr[a]);
  now = arr[a];
  dp2[a] = arr[a];
  M.clear();
  M[arr[a]] = 1;
  for (int i = a - 1; i >= 1; i--) {
    while (!V.empty() && V.back() > arr[i]) {
      now -= (V.back() - arr[i]) * M[V.back()];
      M[arr[i]] += M[V.back()];
      M[V.back()] = 0;
      V.pop_back();
    }
    V.push_back(arr[i]);
    now += arr[i];
    M[arr[i]]++;
    dp2[i] = now;
  }
  long long mx = 0, pos;
  for (int i = 0; i <= a; i++) {
    if (dp1[i] + dp2[i + 1] > mx) {
      mx = dp1[i - 1] + dp2[i];
      pos = i;
    }
  }
  long long ans[a + 50];
  ans[pos] = arr[pos];
  for (int i = pos - 1; i >= 1; i--) ans[i] = min(arr[i], ans[i + 1]);
  for (int i = pos + 1; i <= a; i++) ans[i] = min(arr[i], ans[i - 1]);
  for (int i = 1; i <= a; i++) cout << ans[i] << ' ';
  return 0;
}
