#include <bits/stdc++.h>
using namespace std;
const long long MXN = 5e5 + 10;
const long long MX5 = 1e5 + 10;
const long long MX6 = 1e6 + 10;
const long long LOG = 20;
const long long INF = 8e18;
const double eps = 1e-9;
const long long MOD = 1e9 + 7;
long long power(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * power(a * a % md, b / 2, md) % md
                      : power(a * a % md, b / 2, md) % md));
}
long long bmm(long long a, long long b) {
  return (a % b == 0 ? b : bmm(b, a % b));
}
string base2(long long n) {
  string a = "";
  while (n >= 2) {
    a += (char)(n % 2 + '0');
    n /= 2;
  }
  a += (char)(n + '0');
  reverse((a).begin(), (a).end());
  return a;
}
long long n;
long long A[MXN], L[MXN], R[MXN], Ans[MXN], nxtL[MXN], nxtR[MXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> A[i];
  L[0] = A[0], R[n - 1] = A[n - 1], nxtL[0] = 0, nxtR[n - 1] = n - 1;
  for (int i = 1; i < n; i++) {
    if (A[i] > A[i - 1]) {
      L[i] = L[i - 1] + A[i], nxtL[i] = i;
      continue;
    } else if (A[i] == A[i - 1]) {
      L[i] = L[i - 1] + A[i], nxtL[i] = nxtL[i - 1];
      continue;
    }
    long long p = nxtL[i - 1] - 1, sum = 0;
    while (p >= 0 && A[p] >= A[i]) {
      sum += (A[p] - A[i]) * (p - nxtL[p] + 1);
      p = nxtL[p] - 1;
    }
    long long t = i - 1 - nxtL[i - 1] + 1;
    L[i] = L[i - 1] + A[i] - sum - t * (A[i - 1] - A[i]);
    nxtL[i] = p + 1;
  }
  for (int i = n - 2; i >= 0; i--) {
    if (A[i] > A[i + 1]) {
      R[i] = R[i + 1] + A[i], nxtR[i] = i;
      continue;
    } else if (A[i] == A[i + 1]) {
      R[i] = R[i + 1] + A[i], nxtR[i] = nxtR[i + 1];
      continue;
    }
    long long p = nxtR[i + 1] + 1, sum = 0;
    while (p < n && A[p] >= A[i]) {
      sum += (A[p] - A[i]) * (nxtR[p] - p + 1);
      p = nxtR[p] + 1;
    }
    long long t = nxtR[i + 1] - (i + 1) + 1;
    R[i] = R[i + 1] + A[i] - sum - t * (A[i + 1] - A[i]);
    nxtR[i] = p - 1;
  }
  long long maxi = 0, id = -1;
  for (int i = 0; i < n; i++) {
    long long now = L[i] + R[i] - A[i];
    if (now > maxi) {
      maxi = now, id = i;
    }
  }
  long long h = A[id];
  Ans[id] = A[id];
  for (int i = id + 1; i < n; i++) {
    if (A[i] < h) {
      Ans[i] = A[i], h = A[i];
    } else
      Ans[i] = h;
  }
  h = A[id];
  for (int i = id - 1; i >= 0; i--) {
    if (A[i] < h) {
      Ans[i] = A[i], h = A[i];
    } else
      Ans[i] = h;
  }
  for (int i = 0; i < n; i++) cout << Ans[i] << ' ';
  return 0;
}
