#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
int n, m[2][500009], seg[2][10000009], N, tr[2][500009], tmp;
long long dat[2][500009];
stack<int> stk;
queue<int> qu;
void upd(int pos, int v, int id) {
  pos += N / 2;
  seg[id][pos] = v;
  pos /= 2;
  while (pos) {
    seg[id][pos] = min(seg[id][2 * pos], seg[id][2 * pos + 1]);
    pos /= 2;
  }
}
int query(int a, int b, int l, int r, int p, int id) {
  if (b <= a) return MOD;
  if (r <= a || b <= l) return MOD;
  if (a <= l && r <= b) return seg[id][p];
  return min(query(a, b, l, (l + r) / 2, 2 * p, id),
             query(a, b, (l + r) / 2, r, 2 * p + 1, id));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m[0][i];
    m[1][i] = m[0][i];
  }
  reverse(m[1], m[1] + n);
  N = 1;
  while (N <= n) N = N << 1;
  N = N << 1;
  for (int j = 0; j < 2; j++) {
    for (int i = 0; i < n; i++) upd(i, m[j][i], j);
    for (int i = n; i < N / 2; i++) upd(i, MOD, j);
  }
  for (int j = 0; j < 2; j++) {
    tr[j][0] = -1;
    dat[j][0] = m[j][0];
    for (int i = 1; i < n; i++) {
      tmp = query(0, i, 0, N / 2, 1, j);
      if (tmp >= m[j][i]) {
        tr[j][i] = -1;
        dat[j][i] = (i + 1) * 1LL * m[j][i];
      } else {
        int l = 0, r = i;
        while (l + 1 < r) {
          int mid = (l + r) / 2;
          tmp = query(mid, r, 0, N / 2, 1, j);
          if (tmp >= m[j][i])
            r = mid;
          else
            l = mid;
        }
        tr[j][i] = l;
        dat[j][i] = dat[j][l] + m[j][i] * 1LL * (i - l);
      }
    }
  }
  int ans = 0;
  long long cur = dat[1][n - 1];
  for (int i = 1; i < n; i++) {
    long long tt = dat[0][i];
    if (i < n - 1) {
      tt += dat[1][n - 1 - i];
      tt -= m[0][i];
    }
    if (tt > cur) {
      cur = tt;
      ans = i;
    }
  }
  int ix = ans;
  while (ix >= 0) {
    for (int i = ix; i > tr[0][ix]; i--) {
      stk.push(m[0][ix]);
    }
    ix = tr[0][ix];
  }
  ix = n - 1 - ans;
  bool flag = true;
  while (ix >= 0) {
    for (int i = ix; i > tr[1][ix]; i--) {
      if (flag) {
        flag = false;
      } else
        qu.push(m[1][ix]);
    }
    ix = tr[1][ix];
  }
  while (!stk.empty()) {
    cout << stk.top() << " ";
    stk.pop();
  }
  while (!qu.empty()) {
    cout << qu.front() << " ";
    qu.pop();
  }
  cout << "\n";
  return 0;
}
