#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
  vector<int> nl(n, -1), nr(n, n);
  stack<int> s;
  for (int i = 0; i < n; ++i) {
    while (!s.empty() && v[s.top()] >= v[i]) s.pop();
    if (!s.empty()) nl[i] = s.top();
    s.push(i);
  }
  while (!s.empty()) s.pop();
  for (int i = n - 1; i >= 0; --i) {
    while (!s.empty() && v[s.top()] >= v[i]) s.pop();
    if (!s.empty()) nr[i] = s.top();
    s.push(i);
  }
  vector<vector<long long> > dp(2, vector<long long>(n));
  for (int i = 0; i < n; ++i) {
    auto &ret = dp[0];
    int prev = nl[i];
    ret[i] = (i - prev) * (long long)v[i];
    if (prev != -1) ret[i] += ret[prev];
  }
  long long res = 0;
  int at;
  for (int i = n - 1; i >= 0; --i) {
    auto &ret = dp[1];
    int next = nr[i];
    ret[i] = (next - i) * (long long)v[i];
    if (next != n) ret[i] += ret[next];
    long long cur = dp[0][i] + dp[1][i] - v[i];
    if (res < cur) {
      res = cur;
      at = i;
    }
  }
  vector<int> sol(n);
  int me = v[at];
  for (int i = at; i >= 0; --i) {
    me = min(me, v[i]);
    sol[i] = me;
  }
  me = v[at];
  for (int i = at; i < n; ++i) {
    me = min(me, v[i]);
    sol[i] = me;
  }
  for (auto x : sol) printf("%d ", x);
  return 0;
}
