#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 5e5 + 5;
long long m[N], dp1[N], dp2[N], ans[N];
struct node {
  long long id, h;
  node(){};
  node(long long a, long long b) { id = a, h = b; };
} q[N];
int main() {
  long long n, r = 0, maxn = 0, mark;
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%lld", &m[i]);
  dp1[1] = m[1], q[++r] = node(1, m[1]);
  for (int i = 2; i <= n; i++) {
    while (m[i] < q[r].h && r >= 1) r--;
    dp1[i] = dp1[q[r].id] + (i - q[r].id) * m[i];
    q[++r] = node(i, m[i]);
  }
  r = 0;
  q[0] = node(n + 1, 0);
  dp2[n] = m[n], q[++r] = node(n, m[n]);
  for (int i = n - 1; i >= 1; i--) {
    while (m[i] < q[r].h && r >= 1) r--;
    dp2[i] = dp2[q[r].id] + (q[r].id - i) * m[i];
    q[++r] = node(i, m[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (dp1[i] + dp2[i] - m[i] > maxn) maxn = dp1[i] + dp2[i] - m[i], mark = i;
  }
  ans[mark] = m[mark];
  for (int i = mark + 1; i <= n; i++) {
    ans[i] = min(m[i], ans[i - 1]);
  }
  for (int i = mark - 1; i >= 1; i--) {
    ans[i] = min(ans[i + 1], m[i]);
  }
  for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
  printf("\n");
}
