#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  return (b ? __gcd(a, b) : a);
}
template <typename T>
T lcm(T a, T b) {
  return (a * (b / gcd(a, b)));
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
const int N = 5e5 + 5;
long long ar[N];
long long l[N], r[N], ans[N];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> ar[i];
  vector<long long> lft(1, 0), rt(1, n + 1);
  for (int i = 1; i <= n; ++i) {
    while (ar[lft.back()] > ar[i]) {
      lft.pop_back();
    }
    l[i] = l[lft.back()] + ar[i] * (i - lft.back());
    lft.push_back(i);
  }
  for (int i = n; i; --i) {
    while (ar[rt.back()] > ar[i]) rt.pop_back();
    r[i] = r[rt.back()] + ar[i] * (rt.back() - i);
    rt.push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    ans[i] = l[i] + r[i] - ar[i];
  }
  long long maxa = max_element(ans + 1, ans + n + 1) - ans;
  for (int i = maxa - 1; i; --i) {
    if (ar[i] > ar[i + 1]) ar[i] = ar[i + 1];
  }
  for (int i = maxa + 1; i <= n; ++i)
    if (ar[i - 1] < ar[i]) ar[i] = ar[i - 1];
  for (int i = 1; i <= n; ++i) cout << ar[i] << " ";
  cout << '\n';
  return 0;
}
