#include <bits/stdc++.h>
using namespace std;
unsigned int min(const vector<unsigned int>& a, unsigned int i,
                 unsigned int j) {
  unsigned int m = 200001;
  for (unsigned int c = i; c <= j; ++c) {
    if (a[c] < m) {
      m = a[c];
    }
  }
  return m;
}
int main(int argc, char* argv[]) {
  unsigned int n = 0;
  cin >> n;
  vector<unsigned int> a(n);
  for (unsigned int i = 0; i < n; ++i) {
    cin >> a[i];
    cout << a[i] << " ";
  }
  cout << endl;
  unsigned int sum = 0;
  for (unsigned int i = 0; i < n; ++i) {
    for (unsigned int j = i; j < n; ++j) {
      sum += min(a, i, j);
    }
  }
  cout << sum << endl;
  return 0;
}
