#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long mod = 998244353;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long binpow(long long a, long long b, long long m) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
void solve() {
  int n;
  cin >> n;
  vector<long long> a(n);
  {
    for (auto &_re : a) cin >> _re;
  };
  stack<pair<long long, long long>> s;
  long long dp1[n], dp2[n];
  dp1[0] = a[0];
  s.push({a[0], 1});
  for (int i = 1; i < n; i++) {
    dp1[i] = dp1[i - 1];
    long long cnt = 1;
    while (!s.empty() && s.top().first > a[i]) {
      dp1[i] -= (s.top().first - a[i]) * s.top().second;
      cnt += s.top().second;
      s.pop();
    }
    dp1[i] += a[i];
    s.push({a[i], cnt});
  }
  while (!s.empty()) s.pop();
  dp2[n - 1] = a[n - 1];
  s.push({a[n - 1], 1});
  for (int i = n - 2; i >= 0; i--) {
    dp2[i] = dp2[i + 1];
    long long cnt = 1;
    while (!s.empty() && s.top().first > a[i]) {
      dp2[i] -= (s.top().first - a[i]) * s.top().second;
      cnt += s.top().second;
      s.pop();
    }
    dp2[i] += a[i];
    s.push({a[i], cnt});
  }
  long long best = 0;
  for (int i = 1; i < n; i++) {
    if (dp1[i] + dp2[i] - a[i] > dp1[best] + dp2[best] - a[best]) {
      best = i;
    }
  }
  vector<long long> res(n);
  long long mx = a[best];
  for (int i = best - 1; i >= 0; i--) {
    mx = min(mx, a[i]);
    res[i] = mx;
  }
  res[best] = a[best];
  mx = a[best];
  for (int i = best + 1; i < n; i++) {
    mx = min(mx, a[i]);
    res[i] = mx;
  }
  {
    for (auto _pr : res) cout << _pr << " ";
    cout << "\n";
  };
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
