#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n;
int a[N];
long long pre[N], suf[N];
int dp[N];
void print(int x) {
  for (int i = x - 1; i >= 1; i--) {
    a[i] = min(a[i], a[i + 1]);
  }
  for (int i = x + 1; i <= n; i++) {
    a[i] = min(a[i], a[i - 1]);
  }
  for (int i = 1; i <= n; i++) cout << a[i] << " ";
  cout << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  stack<int> st;
  st.push(1);
  dp[1] = 0;
  for (int i = 2; i <= n; i++) {
    while (!st.empty() && a[st.top()] > a[i]) st.pop();
    if (st.empty())
      dp[i] = 0;
    else {
      dp[i] = st.top();
    }
    st.push(i);
  }
  pre[0] = suf[n + 1] = 0;
  pre[1] = a[1];
  for (int i = 2; i <= n; i++) {
    int pos = dp[i];
    pre[i] = pre[pos] + (a[i] * 1ll * (i - pos));
  }
  dp[n] = n + 1;
  while (!st.empty()) st.pop();
  st.push(n);
  for (int i = n - 1; i >= 1; i--) {
    while (!st.empty() && a[st.top()] > a[i]) st.pop();
    if (st.empty())
      dp[i] = n + 1;
    else
      dp[i] = st.top();
    st.push(i);
  }
  suf[n] = a[n];
  for (int i = n - 1; i >= 1; i--) {
    int pos = dp[i];
    suf[i] = suf[pos] + (a[i] * 1ll * (pos - i));
  }
  long long ans = 0;
  int pos = -1;
  for (int i = 1; i <= n; i++) {
    if (pre[i] + suf[i] - a[i] > ans) {
      ans = pre[i] + suf[i] - a[i];
      pos = i;
    }
  }
  print(pos);
}
