#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
const int M = 2e3 + 5;
const int INF = 0x7fffffff;
const int mod = 1e9 + 7;
const int sz = 18;
const double eps = 1e-10;
const double pi = acos(-1);
long long n, m;
char str[N];
long long f[N];
long long fst[N];
long long lst[N];
stack<pair<long long, long long> > s;
template <class T>
inline void read(T &x) {
  char c;
  x = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') x = -1;
  T res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
  x *= res;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(f[i]);
  long long sum = 0ll;
  for (int i = 1; i <= n; i++) {
    pair<long long, long long> tmp = make_pair(f[i], 1ll);
    while (s.size() && tmp.first <= s.top().first) {
      pair<long long, long long> now = s.top();
      s.pop();
      sum -= now.first * now.second;
      tmp.second += now.second;
    }
    s.push(tmp);
    sum += tmp.first * tmp.second;
    fst[i] = sum;
  }
  while (s.size()) s.pop();
  sum = 0ll;
  for (int i = n; i >= 1; i--) {
    pair<long long, long long> tmp = make_pair(f[i], 1ll);
    while (s.size() && tmp.first <= s.top().first) {
      pair<long long, long long> now = s.top();
      s.pop();
      sum -= now.first * now.second;
      tmp.second += now.second;
    }
    s.push(tmp);
    sum += tmp.first * tmp.second;
    lst[i] = sum;
  }
  long long ans = 0ll;
  int pos;
  for (int i = 0; i <= n; i++) {
    if (ans < fst[i] + lst[i + 1]) {
      ans = fst[i] + lst[i + 1];
      pos = i;
    }
  }
  for (int i = pos + 2; i <= n; i++) f[i] = min(f[i], f[i - 1]);
  for (int i = pos - 1; i >= 1; i--) f[i] = min(f[i], f[i + 1]);
  for (int i = 1; i <= n; i++) printf("%lld%c", f[i], i == n ? '\n' : ' ');
  return 0;
}
