#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> a(n), pref(n), suf(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll ans = -1, ind = -1;
  pref[0] = a[0];
  vector<array<ll, 3>> active = {{a[0], 0, 1}};
  for (int i = 1; i < n; ++i) {
    auto [x, y, z] = active.back();
    if (a[i] > x) {
      pref[i] = pref[i - 1] + a[i];
      active.push_back({a[i], i, 1});
      continue;
    }
    if (a[i] == x) {
      pref[i] = pref[i - 1] + a[i];
      active.pop_back();
      active.push_back({x, y, z + 1});
      continue;
    }
    auto it = upper_bound(active.begin(), active.end(),
                          array<ll, 3>{a[i] + 1, -1, -1});
    auto fl = *it;
    pref[i] = pref[i - 1];
    int id = i, len = 0;
    do {
      auto bk = active.back();
      pref[i] -= bk[0] * bk[2];
      id = bk[1];
      len += bk[2];
      active.pop_back();
      if (bk == fl) break;
    } while (1);
    pref[i] += 1LL * a[i] * (len + 1);
    active.push_back({a[i], id, len + 1});
  }
  active.clear();
  reverse(a.begin(), a.end());
  suf[0] = a[0];
  active.push_back({a[0], 0, 1});
  for (int i = 1; i < n; ++i) {
    auto [x, y, z] = active.back();
    if (a[i] > x) {
      suf[i] = suf[i - 1] + a[i];
      active.push_back({a[i], i, 1});
      continue;
    }
    if (a[i] == x) {
      suf[i] = suf[i - 1] + a[i];
      active.pop_back();
      active.push_back({x, y, z + 1});
      continue;
    }
    auto it = upper_bound(active.begin(), active.end(),
                          array<ll, 3>{a[i] + 1, -1, -1});
    auto fl = *it;
    suf[i] = suf[i - 1];
    int id = i, len = 0;
    do {
      auto bk = active.back();
      suf[i] -= bk[0] * bk[2];
      id = bk[1];
      len += bk[2];
      active.pop_back();
      if (bk == fl) break;
    } while (1);
    suf[i] += 1LL * a[i] * (len + 1);
    active.push_back({a[i], id, len + 1});
  }
  reverse(a.begin(), a.end());
  for (int i = 0; i < n; ++i) {
    if (pref[i] + suf[n - 1 - i] - a[i] > ans) {
      ans = pref[i] + suf[n - 1 - i] - a[i];
      ind = i;
    }
  }
  vector<int> final(n);
  final[ind] = a[ind];
  ll cur = a[ind];
  for (int i = ind - 1; i >= 0; --i) {
    cur = min(cur, a[i]);
    final[i] = cur;
  }
  cur = a[ind];
  for (int i = ind + 1; i < n; ++i) {
    cur = min(cur, a[i]);
    final[i] = cur;
  }
  for (auto x : final) cout << x << ' ';
}
