#include <bits/stdc++.h>
using namespace std;
vector<int64_t> left(vector<int64_t> a);
vector<int64_t> right(vector<int64_t> a);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int64_t n;
  cin >> n;
  vector<int64_t> a(n, 0);
  for (int64_t i = 0; i < n; i++) cin >> a[i];
  vector<int64_t> farL = left(a), farR = right(a);
  vector<int64_t> maxL(n, 0);
  maxL[0] = a[0];
  for (int64_t i = 1; i < n; i++) {
    int64_t j = farL[i];
    maxL[i] = (i - j + 1) * a[i];
    if (j - 1 >= 0) maxL[i] += maxL[j - 1];
  }
  vector<int64_t> maxR(n, 0);
  maxR[n - 1] = a[n - 1];
  for (int64_t i = n - 2; i >= 0; i--) {
    int64_t j = farR[i];
    maxR[i] = (j - i + 1) * a[i];
    if (j + 1 < n) maxR[i] += maxR[j + 1];
  }
  int64_t floors = -1, pos = 0;
  for (int64_t i = 0; i < n; i++) {
    if (maxR[i] + maxL[i] - a[i] > floors) {
      floors = maxR[i] + maxL[i] - a[i];
      pos = i;
    }
  }
  vector<int64_t> result(n, 0);
  result[pos] = a[pos];
  for (int64_t i = pos - 1; i >= 0; i--) result[i] = min(a[i], result[i + 1]);
  for (int64_t i = pos + 1; i < n; i++) result[i] = min(a[i], result[i - 1]);
  for (auto v : result) cout << v << " ";
  return 0;
}
vector<int64_t> left(vector<int64_t> a) {
  vector<int64_t> left(a.size(), 0);
  stack<int64_t> S;
  for (int64_t i = 0; i < a.size(); i++) {
    while (!S.empty() && a[S.top()] > a[i]) S.pop();
    left[i] = (S.empty()) ? 0 : S.top() + 1;
    S.push(i);
  }
  return left;
}
vector<int64_t> right(vector<int64_t> a) {
  vector<int64_t> right(a.size(), 0);
  stack<int64_t> S;
  for (int64_t i = a.size() - 1; i >= 0; i--) {
    while (!S.empty() && a[S.top()] > a[i]) S.pop();
    right[i] = (S.empty()) ? a.size() - 1 : S.top() - 1;
    S.push(i);
  }
  return right;
}
