#include <bits/stdc++.h>
#pragma target("avx")
using namespace std;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
const long long LINF = 1e18;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
struct ma_SegmentTree {
  int siz;
  vector<long long> node;
  ma_SegmentTree(int n, long long init) {
    siz = 1;
    while (siz < n) siz <<= 1;
    node.assign(2 * siz, init);
  }
  void update(int k, long long x) {
    k += siz - 1;
    node[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = max(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  long long query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = siz;
    if (r <= a || b <= l) return -LINF;
    if (a <= l && r <= b) return node[k];
    long long vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
struct mi_SegmentTree {
  int siz;
  vector<long long> node;
  mi_SegmentTree(int n, long long init) {
    siz = 1;
    while (siz < n) siz <<= 1;
    node.assign(2 * siz, init);
  }
  void update(int k, long long x) {
    k += siz - 1;
    node[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  long long query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = siz;
    if (r <= a || b <= l) return LINF;
    if (a <= l && r <= b) return node[k];
    long long vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
long long n, cnt, ans, b, c, d, tmp, m, h, w, x, y, sum, k, q;
long long le[ohara], ri[ohara];
unordered_map<long long, long long> fukugen;
template <class T>
vector<long long> Zarts(const vector<T> &AR, long long CNT = 0) {
  vector<long long> RES;
  map<T, long long> MP;
  for (auto &e : AR) MP[e] = 0;
  for (auto &e : MP) e.second = CNT++;
  for (auto &e : AR) {
    fukugen[MP[e]] = e;
    RES.push_back(MP[e]);
  }
  return RES;
}
int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vector<long long> a(n + 1);
  for (long long(i) = 0; (i) < (int)(n); (i)++) cin >> a[i];
  auto b = Zarts<long long>(a);
  mi_SegmentTree mi_tree(n + 10, LINF);
  ma_SegmentTree ma_tree(n + 10, -LINF);
  le[0] = a[0];
  ma_tree.update(b[0], 0);
  for (long long i = (1); i < (n); i++) {
    tmp = ma_tree.query(0, b[i]);
    if (tmp == -LINF) {
      le[i] = a[i] * (i + 1);
    } else {
      le[i] = a[i] * (i - tmp) + le[tmp];
    }
    ma_tree.update(b[i], i);
  }
  ri[n - 1] = a[n - 1];
  mi_tree.update(b[n - 1], n - 1);
  for (long long i = (n - 2); i >= (0); i--) {
    tmp = mi_tree.query(0, b[i]);
    if (tmp == LINF) {
      ri[i] = a[i] * (n - i);
    } else {
      ri[i] = a[i] * (tmp - i) + ri[tmp];
    }
    mi_tree.update(b[i], i);
  }
  for (long long(i) = 0; (i) < (int)(n); (i)++) {
    ans = max(ans, le[i] + ri[i] - a[i]);
  }
  for (long long(i) = 0; (i) < (int)(n); (i)++) {
    if (ans == le[i] + ri[i] - a[i]) {
      long long mi = a[i];
      for (long long j = (i - 1); j >= (0); j--) {
        a[j] = min(a[j], mi);
        mi = min(mi, a[j]);
      }
      mi = a[i];
      for (long long j = (i + 1); j < (n); j++) {
        a[j] = min(a[j], mi);
        mi = min(mi, a[j]);
      }
      break;
    }
  }
  for (long long(i) = 0; (i) < (int)(n); (i)++) cout << a[i] << " ";
  return 0;
}
