#include <bits/stdc++.h>
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
using namespace std;
void tlfclwx() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long k[maxn], n;
long long l[maxn], r[maxn], w[maxn];
pair<int, int> mp[maxn];
void slove(long long ans[]) {
  long long cur = 0;
  int top = 0;
  mp[0] = make_pair(0, 0);
  for (int i = 1; i <= n; i++) {
    while (top > 0 && mp[top].first > k[i]) {
      cur -= 1ll * mp[top].first * (mp[top].second - mp[top - 1].second);
      --top;
    }
    cur += 1ll * k[i] * (i - mp[top].second);
    mp[++top] = make_pair(k[i], i);
    ans[i] = cur;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> k[i];
  slove(l);
  reverse(k + 1, k + n + 1);
  slove(r);
  reverse(k + 1, k + n + 1);
  reverse(r + 1, r + n + 1);
  long long kp = r[1];
  int flag = 1;
  for (int i = 2; i <= n; i++) {
    long long ans = l[i] + r[i] - k[i];
    if (ans > kp) kp = ans, flag = i;
  }
  w[flag] = k[flag];
  for (int i = flag + 1; i <= n; i++) {
    w[i] = min(w[i - 1], k[i]);
  }
  for (int i = flag - 1; i >= 1; i--) {
    w[i] = min(w[i + 1], k[i]);
  }
  for (int i = 1; i <= n; i++) {
    cout << w[i] << ' ';
  }
  cout << endl;
}
