#include <bits/stdc++.h>
using namespace std;
long long const mod = 1000000007LL;
long long const mod2 = 100000000LL;
long long const md = 998244353LL;
long long mypowr(long long a, long long b) {
  long long res = 1;
  if (b < 0) b = 0;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long mypow(long long a, long long b) {
  long long res = 1;
  if (b < 0) b = 0;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a;
    a = a * a;
  }
  return res;
}
ifstream in("input.txt");
ofstream out("output.txt");
const long long infll = 9e18;
const int inf = 2e9;
const long long maxn = 5e5 + 2;
int a[maxn];
long long st[maxn], dr[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(15);
  int test = 1;
  while (test--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
    }
    stack<pair<int, int> > s, s1;
    for (int i = 1; i <= n; ++i) {
      st[i] = a[i];
      while (!s.empty() && s.top().first > a[i]) {
        s.pop();
      }
      if (!s.empty()) {
        long long y = s.top().second;
        st[i] += st[y];
        st[i] += (i - y - 1) * a[i];
      } else {
        st[i] += (i - 1) * a[i];
      }
      s.push({a[i], i});
    }
    for (int i = n; i >= 1; --i) {
      dr[i] = a[i];
      while (!s1.empty() && s1.top().first > a[i]) {
        s1.pop();
      }
      if (!s1.empty()) {
        long long y = s1.top().second;
        dr[i] += dr[y];
        dr[i] += (y - i - 1) * a[i];
      } else {
        dr[i] += (n - i) * a[i];
      }
      s1.push({a[i], i});
    }
    long long cnt = 0, pos = -1;
    for (int i = 1; i <= n; ++i) {
      long long x = st[i] + dr[i];
      x -= a[i];
      if (x > cnt) {
        cnt = x, pos = i;
      }
    }
    int mini = a[pos];
    for (int i = pos - 1; i >= 1; --i) {
      int x = a[i];
      if (x > mini) x = mini;
      if (x < mini) mini = x;
      a[i] = x;
    }
    mini = a[pos];
    for (int i = pos + 1; i <= n; ++i) {
      int x = a[i];
      if (x > mini) x = mini;
      if (x < mini) mini = x;
      a[i] = x;
    }
    for (int i = 1; i <= n; ++i) {
      cout << a[i] << " ";
    }
  }
  return 0;
}
