#include <bits/stdc++.h>
using namespace std;
const long long int N = 500005;
long long int a[N], l[N], r[N];
long long int vl[N], vr[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  stack<long long int> s1, s2;
  long long int nu;
  cin >> nu;
  long long int pos = 0;
  for (long long int i = 1; i <= nu; i++) cin >> a[i];
  for (long long int i = 1; i <= nu; i++) {
    while (!s1.empty() && a[s1.top()] > a[i]) s1.pop();
    if (s1.empty())
      l[i] = 0;
    else
      l[i] = s1.top();
    vl[i] = vl[l[i]] + (i - l[i]) * a[i];
    s1.push(i);
  }
  long long int mx = -1;
  for (long long int i = nu; i >= 1; i--) {
    while (!s2.empty() && a[s2.top()] > a[i]) s2.pop();
    if (s2.empty())
      r[i] = nu + 1;
    else
      r[i] = s2.top();
    vr[i] = vr[r[i]] + (r[i] - i) * a[i];
    s2.push(i);
    if ((vl[i] + vr[i] - a[i]) > mx) {
      mx = vl[i] + vr[i] - a[i];
      pos = i;
    }
  }
  long long int x = a[pos];
  for (long long int i = pos - 1; i >= 1; i--) {
    if (a[i] > x)
      a[i] = x;
    else
      x = a[i];
  }
  x = a[pos];
  for (long long int i = pos + 1; i <= nu; i++) {
    if (a[i] > x)
      a[i] = x;
    else
      x = a[i];
  }
  for (long long int i = 1; i <= nu; i++) cout << a[i] << " ";
  return 0;
}
