#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  vector<pair<long long, pair<long long, long long>>> m(n);
  for (long long i = 0; i < n; i++) {
    m[i].first = a[i] * max(i, n - i - 1);
    m[i].second.first = a[i];
    m[i].second.second = i;
  }
  sort(m.rbegin(), m.rend());
  long long l = 0, r = n - 1, ans = 0;
  for (long long i = 0; i < n; i++) {
    double mid = (double)(l + r) / 2;
    long long set = 0;
    if (m[i].second.second == mid && i + 1 != n) {
      if (m[i].second.second > m[i + 1].second.second) {
        set = l;
      } else {
        set = r;
      }
    } else {
      if (m[i].second.second > mid) {
        set = l;
      } else {
        set = r;
      }
    }
    ans += abs(m[i].second.second - set) * m[i].second.first;
    cout << abs(m[i].second.second - set) * m[i].second.first << " " << set
         << " " << m[i].second.first << endl;
    if (set == r) r--;
    if (set == l) l++;
  }
  cout << ans << endl;
  return 0;
}
