#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, e, f, g, n, m, k;
int main() {
  ios_base::sync_with_stdio(false);
  long long size;
  cin >> size;
  long long arr[size];
  long long left[size];
  long long right[size];
  for (int i = 0; i < size; i++) {
    left[i] = 0;
    right[i] = 0;
  }
  for (int i = 0; i < size; i++) {
    cin >> arr[i];
  }
  if (size == 1) {
    cout << arr[0];
    return 0;
  }
  stack<pair<long long, long long> > st;
  st.push({0, -1});
  st.push({arr[0], 0});
  left[0] = arr[0];
  for (int i = 1; i < size; i++) {
    long long curr = arr[i];
    if (curr >= st.top().first) {
      left[i] = left[i - 1] + curr;
    } else {
      while (st.top().first > curr) {
        st.pop();
      }
      int ind = st.top().second;
      if (st.size() == 1) {
        left[i] = (i + 1) * curr;
      } else {
        left[i] = (i - ind) * curr + left[ind];
      }
    }
    st.push({curr, i});
  }
  while (!st.empty()) st.pop();
  st.push({0, size});
  right[size - 1] = arr[size - 1];
  st.push({arr[size - 1], size - 1});
  for (int j = size - 2; j >= 0; j--) {
    long long curr = arr[j];
    if (curr >= st.top().first) {
      right[j] = curr + right[j + 1];
    } else {
      while (st.top().first > curr) {
        st.pop();
      }
      int ind = st.top().second;
      if (st.size() == 1) {
        right[j] = (size - j) * curr;
      } else {
        right[j] = (ind - j) * curr + right[ind];
      }
    }
    st.push({curr, j});
  }
  long long maxBefore = 0;
  long long ind = 0;
  for (int i = 0; i < size; i++) {
    long long curr = 0;
    if (i == 0) {
      curr = right[0];
      if (curr > maxBefore) {
        maxBefore = curr;
        ind = 0;
      }
    } else if (i == size - 1) {
      if (left[i - 1] + arr[i] > maxBefore) {
        maxBefore = left[i - 1] + arr[i];
        ind = i;
      }
    } else {
      curr = left[i] + right[i] - arr[i];
      if (curr > maxBefore) {
        maxBefore = curr;
        ind = i;
      }
    }
  }
  long long indx = ind;
  vector<long long> first;
  vector<long long> second;
  long long lastOne = arr[indx];
  for (int j = indx - 1; j >= 0; j--) {
    if (arr[j] < lastOne) {
      lastOne = arr[j];
    }
    first.push_back(lastOne);
  }
  lastOne = arr[indx];
  for (int j = indx + 1; j < size; j++) {
    if (arr[j] < lastOne) {
      lastOne = arr[j];
    }
    second.push_back(lastOne);
  }
  for (int i = first.size() - 1; i >= 0; i--) {
    cout << first[i] << " ";
  }
  cout << arr[indx] << " ";
  for (int i = 0; i < second.size(); i++) {
    cout << second[i] << " ";
  }
  cout << endl;
  ;
  return 0;
}
