#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int n, m[N];
int pf[N][20], sf[N][20], st[N], tp, output[N];
long long Sum_pf[N][20], Sum_sf[N][20];
void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &m[i]);
  st[++tp] = 0;
  for (int i = 0; i <= n + 1; ++i)
    pf[i][0] = 0, sf[i][0] = n + 1, Sum_pf[i][0] = 1LL * i * m[i],
    Sum_sf[i][0] = 1LL * (n - i + 1) * m[i];
  for (int i = 1; i <= n + 1; ++i) {
    while (tp && m[st[tp]] > m[i]) {
      sf[st[tp]][0] = i;
      Sum_sf[st[tp]][0] = 1LL * (i - st[tp]) * m[st[tp]];
      --tp;
    }
    st[++tp] = i;
  }
  tp = 0;
  st[++tp] = n + 1;
  for (int i = n; i >= 0; --i) {
    while (tp && m[st[tp]] > m[i]) {
      pf[st[tp]][0] = i;
      Sum_pf[st[tp]][0] = 1LL * (st[tp] - i) * m[st[tp]];
      --tp;
    }
    st[++tp] = i;
  }
  int lg = log2(n);
  for (int j = 1; j <= lg; ++j)
    for (int i = 1; i <= n; ++i) {
      pf[i][j] = pf[pf[i][j - 1]][j - 1],
      Sum_pf[i][j] = Sum_pf[i][j - 1] + Sum_pf[pf[i][j - 1]][j - 1];
      sf[i][j] = sf[sf[i][j - 1]][j - 1],
      Sum_sf[i][j] = Sum_sf[i][j - 1] + Sum_sf[sf[i][j - 1]][j - 1];
    }
  long long answer = 0;
  int pos = 0;
  for (int i = 1; i <= n; ++i) {
    long long sum = 0;
    int p = i;
    for (int j = lg; j >= 0; --j)
      if (pf[p][j]) sum += Sum_pf[p][j], p = pf[p][j];
    sum += Sum_pf[p][0];
    p = i;
    for (int j = lg; j >= 0; --j)
      if (sf[p][j] != n + 1) sum += Sum_sf[p][j], p = sf[p][j];
    sum += Sum_sf[p][0];
    sum -= m[i];
    if (sum > answer) answer = sum, pos = i;
  }
  int p = pos;
  while (true) {
    for (int i = pf[p][0] + 1; i <= p; ++i) output[i] = m[p];
    if (pf[p][0] == 0)
      break;
    else
      p = pf[p][0];
  }
  p = pos;
  while (true) {
    for (int i = p; i <= sf[p][0] - 1; ++i) output[i] = m[p];
    if (sf[p][0] == n + 1)
      break;
    else
      p = sf[p][0];
  }
  for (int i = 1; i <= n; i++) printf("%d ", output[i]);
}
int main() {
  int tests = 1;
  while (tests--) solve();
  return 0;
}
