#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int t;
  t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    vector<long long int> v(n);
    for (long long int i = 0; i < n; i++) cin >> v[i];
    vector<long long int> lft(n, 0);
    vector<long long int> rgt(n, 0);
    for (long long int i = 0; i < n; i++) {
      lft[i] = i;
      rgt[i] = i;
    }
    stack<long long int> st;
    st.push(n - 1);
    for (long long int i = n - 2; i >= 0; i--) {
      while (!st.empty() && v[st.top()] > v[i]) {
        lft[st.top()] = i;
        st.pop();
      }
      st.push(i);
    }
    while (!st.empty()) {
      st.pop();
    }
    st.push(0);
    for (long long int i = 1; i < n; i++) {
      while (!st.empty() && v[st.top()] > v[i]) {
        rgt[st.top()] = i;
        st.pop();
      }
      st.push(i);
    }
    vector<long long int> lval(n);
    vector<long long int> rval(n);
    for (long long int i = 0; i < n; i++) {
      if (lft[i] == i) {
        lval[i] = (i + 1) * v[i];
      } else {
        long long int x = lft[i];
        lval[i] = lval[x] + (i - x) * v[i];
      }
    }
    for (long long int i = n - 1; i >= 0; i--) {
      if (rgt[i] == i) {
        rval[i] = (n - i) * v[i];
      } else {
        long long int x = rgt[i];
        rval[i] = rval[x] + (x - i) * v[i];
      }
    }
    long long int ans = 0;
    long long int index = 0;
    for (long long int i = 0; i < n; i++) {
      long long int g = lval[i] + rval[i] - v[i];
      if (g > ans) {
        ans = g;
        index = i;
      }
    }
    long long int x = index;
    long long int y = index;
    vector<long long int> fans(n);
    while (x >= 0) {
      if (lft[x] == x) {
        long long int r = v[x];
        while (x >= 0) {
          fans[x] = r;
          x--;
        }
      } else {
        long long int e = lft[x];
        long long int r = v[x];
        while (x > e) {
          fans[x] = r;
          x--;
        }
      }
    }
    while (y < n) {
      if (rgt[y] == y) {
        long long int r = v[y];
        while (y < n) {
          fans[y] = r;
          y++;
        }
      } else {
        long long int e = rgt[y];
        long long int r = v[y];
        while (y < e) {
          fans[y] = r;
          y++;
        }
      }
    }
    for (long long int i : fans) cout << i << " ";
    cout << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int T;
  T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
