#include <bits/stdc++.h>
using namespace std;
long long ara[500005], mn[2][500005], res[500005];
priority_queue<pair<long long, long long> > pq;
int main() {
  long long n;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &ara[i]);
  pq.push({ara[1], 1});
  mn[0][1] = ara[1];
  long long sum = ara[1];
  for (int i = 2; i <= n; i++) {
    long long c = 1;
    while (!pq.empty() && pq.top().first > ara[i])
      c += pq.top().second, sum -= (pq.top().first * pq.top().second), pq.pop();
    pq.push({ara[i], c});
    sum += (ara[i] * c);
    mn[0][i] = sum;
  }
  while (!pq.empty()) pq.pop();
  pq.push({ara[n], 1});
  sum = ara[n];
  mn[1][n] = ara[n];
  for (int i = n - 1; i > 0; i--) {
    long long c = 1;
    while (!pq.empty() && pq.top().first > ara[i])
      c += pq.top().second, sum -= (pq.top().first * pq.top().second), pq.pop();
    pq.push({ara[i], c});
    sum += (ara[i] * c);
    mn[1][i] = sum;
  }
  int ind;
  sum = 0;
  for (int i = 1; i <= n + 1; i++)
    if (sum < (mn[0][i - 1] + mn[1][i]))
      sum = (mn[0][i - 1] + mn[1][i]), ind = i;
  sum = ara[ind - 1];
  for (int i = ind - 1; i > 0; i--)
    res[i] = min(ara[i], sum), sum = min(sum, res[i]);
  sum = ara[ind];
  for (int i = ind; i <= n; i++)
    res[i] = min(ara[i], sum), sum = min(sum, res[i]);
  for (int i = 1; i <= n; i++) printf("%lld ", res[i]);
  puts("");
  return 0;
}
