#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a[200001][2] = {0}, sum = 0, sums = 0;
  ;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i][0];
  a[1][1] = a[1][0];
  sum += a[1][1];
  for (int i = 2; i <= n; i++) {
    if (a[i][0] < a[i - 1][1])
      a[i][1] = a[i][0];
    else
      a[i][1] = a[i - 1][1];
    sum += a[i][1];
  }
  sums += sum;
  for (int i = 2; i <= n; i++) {
    sum -= a[i - 1][1];
    for (int j = i; j <= n; j++) {
      if (a[j][0] == a[j][1])
        break;
      else if (i == j) {
        sum -= a[j][1];
        a[j][1] = a[j][0];
        sum += a[j][1];
      } else if (a[j - 1][1] > a[j][0]) {
        sum -= a[j][1];
        a[j][1] = a[j][0];
        sum += a[j][1];
      } else {
        sum -= a[j][1];
        a[j][1] = a[j - 1][1];
        sum += a[j][1];
      }
    }
    for (int i = 1; i <= n; i++) cout << a[i][1] << " ";
    cout << endl;
    sums += sum;
  }
  cout << sums << endl;
  return 0;
}
