#include <bits/stdc++.h>
using namespace std;
const unsigned long long int mod = 1e9 + 7;
const long long int inf = (long long int)1e18 + 2;
const long long int N = (long long int)2e5 + 2;
long long int power(long long int x, long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
void solve() {
  long long int n;
  cin >> n;
  long long int arr[n];
  for (auto& x : arr) cin >> x;
  long long int dp[2][n];
  stack<pair<long long int, long long int> > s;
  long long int ans = 0;
  for (long long int j = 0; j < n; j++) {
    long long int cnt = 1;
    while (!s.empty() && s.top().first > arr[j]) {
      pair<long long int, long long int> x = s.top();
      s.pop();
      ans -= x.first * x.second;
      cnt += x.second;
    }
    ans += cnt * arr[j];
    s.push({arr[j], cnt});
    dp[0][j] = ans;
  }
  ans = 0;
  while (!s.empty()) s.pop();
  for (long long int j = n - 1; j >= 0; j--) {
    long long int cnt = 1;
    while (!s.empty() && s.top().first > arr[j]) {
      pair<long long int, long long int> x = s.top();
      s.pop();
      ans -= x.first * x.second;
      cnt += x.second;
    }
    ans += cnt * arr[j];
    s.push({arr[j], cnt});
    dp[1][j] = ans;
  }
  ans = 0;
  long long int pos = -1;
  for (long long int i = 0; i < n; i++) {
    long long int x = dp[0][i] + dp[1][i] - arr[i];
    if (x > ans) ans = x, pos = i;
  }
  long long int mn = arr[pos];
  for (long long int i = pos; i >= 0; i--) mn = min(mn, arr[i]), arr[i] = mn;
  mn = arr[pos];
  for (long long int i = pos; i < n; i++) mn = min(mn, arr[i]), arr[i] = mn;
  for (auto c : arr) cout << c << ' ';
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  t = 1;
  while (t--) {
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
