#include <bits/stdc++.h>
using namespace std;
const int MX = 100005;
const long long MOD = 998244353;
void setIO(string name) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  freopen((name + ".in").c_str(), "r", stdin);
  freopen((name + ".out").c_str(), "w", stdout);
}
long long m1[5 * MX], l[5 * MX];
long long m2[5 * MX], r[5 * MX];
long long dp1[5 * MX], dp2[5 * MX];
long long opt[5 * MX];
int n;
int main() {
  cin >> n;
  int x;
  for (int i = 0; i < (n); i++) {
    cin >> x;
    m1[i] = x;
    m2[n - 1 - i] = x;
  }
  l[0] = -1;
  dp1[0] = 0;
  for (int i = (1); i < (n); i++) {
    int t = i - 1;
    while (t >= 0 && m1[t] >= m1[i]) t = l[t];
    l[i] = t;
    if (t >= 0)
      dp1[i] = dp1[t] + (i - t - 1) * m1[i] + m1[t];
    else
      dp1[i] = i * m1[i];
  }
  r[0] = -1;
  for (int i = (1); i < (n); i++) {
    int t = i - 1;
    while (t >= 0 && m2[t] >= m2[i]) t = r[t];
    r[i] = t;
    if (t >= 0)
      dp2[i] = dp2[t] + (i - t - 1) * m2[i] + m2[t];
    else
      dp2[i] = i * m2[i];
  }
  long long ans = 0, best;
  for (int i = 0; i < (n); i++) {
    if (dp1[i] + m1[i] + dp2[n - i - 1] > ans) {
      ans = dp1[i] + m1[i] + dp2[n - i - 1];
      best = i;
    }
  }
  opt[best] = m1[best];
  for (int i = (best + 1); i < (n); i++) {
    opt[i] = min(opt[i - 1], m1[i]);
  }
  for (int i = (best)-1; i >= 0; i--) {
    opt[i] = min(opt[i + 1], m1[i]);
  }
  for (int i = 0; i < (n); i++) {
    cout << opt[i] << ' ';
  }
  cout << endl;
}
