#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, mx, mxp;
  cin >> n;
  vector<long long int> m(n), left(n), right(n), minl(n), minr(n);
  map<long long int, long long int> last;
  set<long long int> s;
  set<long long int>::iterator itr;
  for (i = 0; i < n; i++) cin >> m[i];
  left[0] = m[0];
  right[n - 1] = m[n - 1];
  for (i = 0; i < n; i++) {
    s.insert(m[i]);
    itr = find(s.begin(), s.end(), m[i]);
    if (itr != s.begin())
      minl[i] = last[*prev(itr)];
    else
      minl[i] = -1;
    last[m[i]] = i;
  }
  s.clear();
  last.clear();
  for (i = n - 1; i > -1; i--) {
    s.insert(m[i]);
    itr = find(s.begin(), s.end(), m[i]);
    if (itr != s.begin())
      minr[i] = last[*prev(itr)];
    else
      minr[i] = n;
    last[m[i]] = i;
  }
  for (i = 1; i < n; i++) {
    left[i] = (i - minl[i]) * m[i];
    if (minl[i] != -1) left[i] += left[minl[i]];
  }
  for (i = n - 2; i > -1; i--) {
    right[i] = (minr[i] - i) * m[i];
    if (minr[i] != n) right[i] += right[minr[i]];
  }
  mx = mxp = -1;
  for (i = 0; i < n; i++)
    if (mx < (left[i] + right[i] - m[i]))
      mx = left[i] + right[i] - m[i], mxp = i;
  for (i = mxp + 1; i < n; i++) m[i] = min(m[i], m[i - 1]);
  for (i = mxp - 1; i > -1; i--) m[i] = min(m[i], m[i + 1]);
  for (auto x : m) cout << x << " ";
  ;
  return 0;
}
