#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, sum_a = 0;
  cin >> n;
  vector<long long> ans, a(n), pos, pre(n), suf(n), jps(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  stack<pair<long long, long long> > pre_stack, suf_stack;
  pre[0] = a[0];
  pre_stack.push({a[0], 0});
  for (long long i = 1; i < n; i++) {
    pre[i] = pre[i - 1] + a[i];
    while (!pre_stack.empty()) {
      auto T = pre_stack.top();
      if (T.first > a[i]) {
        pre_stack.pop();
        if (!pre_stack.empty())
          pre[i] -= abs((pre_stack.top().second) - T.second) * (T.first - a[i]);
        else
          pre[i] -= (T.second + 1) * (T.first - a[i]);
      } else
        break;
    }
    pre_stack.push({a[i], i});
  }
  suf[n - 1] = a[n - 1];
  suf_stack.push({a[n - 1], n - 1});
  for (long long i = n - 2; i >= 0; i--) {
    suf[i] = suf[i + 1] + a[i];
    while (!suf_stack.empty()) {
      auto T = suf_stack.top();
      if (T.first > a[i]) {
        suf_stack.pop();
        if (!suf_stack.empty())
          suf[i] -= abs((suf_stack.top().second) - T.second) * (T.first - a[i]);
        else
          suf[i] -= abs(n - T.second) * (T.first - a[i]);
      } else
        break;
    }
    suf_stack.push({a[i], i});
  }
  for (long long i = 0; i < n; i++) {
    jps[i] = suf[i] + pre[i] - a[i];
  }
  {
    long long i = max_element(jps.begin(), jps.end()) - jps.begin();
    vector<long long> A = a;
    long long sum = 0;
    for (long long j = i - 1; j >= 0; j--) {
      if (A[j] > A[j + 1]) A[j] = A[j + 1];
    }
    for (long long j = i + 1; j < n; j++) {
      if (A[j] > A[j - 1]) A[j] = A[j - 1];
    }
    for (long long j = 0; j < n; j++) {
      sum += A[j];
    }
    if (sum_a < sum) {
      sum_a = sum;
      ans = A;
    }
  }
  for (long long i = 0; i < n; i++) cout << ans[i] << ' ';
}
