#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
using namespace std;
void io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
long long n, m, k, top;
long long l[500005], r[500005], a[500005], stk[500005];
int main() {
  io();
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) {
    while (top && a[i] < a[stk[top]]) --top;
    l[i] = l[stk[top]] + (i - stk[top]) * a[i];
    stk[++top] = i;
  }
  top = n + 1, stk[n + 1] = n + 1;
  for (long long i = n; i; --i) {
    while (top <= n && a[i] < a[stk[top]]) ++top;
    r[i] = r[stk[top]] + (stk[top] - i) * a[i];
    stk[--top] = i;
  }
  long long ans = 0, pos;
  for (int i = 1; i <= n; ++i) {
    if (l[i] + r[i] - a[i] > ans) {
      ans = l[i] + r[i] - a[i];
      pos = i;
    }
  }
  long long pre = a[pos];
  for (int i = pos; i; --i) {
    if (a[i] > pre)
      a[i] = pre;
    else if (a[i] < pre)
      pre = a[i];
  }
  pre = a[pos];
  for (int i = pos; i <= n; ++i) {
    if (a[i] > pre)
      a[i] = pre;
    else if (a[i] < pre)
      pre = a[i];
  }
  for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
}
