#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  pair<long long, long long> arr[n];
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    arr[i] = {x, i};
  }
  sort(arr, arr + n, greater<pair<long long, long long>>());
  long long a = 0, b = n - 1;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    long long idx = arr[i].second;
    long long val = arr[i].first;
    if (abs(b - idx) == abs(idx - a)) {
      if (i == n - 1)
        sum += val * abs(idx - a);
      else {
        long long id = arr[i + 1].second;
        long long vav = arr[i + 1].first;
        if (abs(b - idx) > abs(idx - a))
          sum += vav * abs(b - idx) + val * abs(idx - a), a++, b--, i++;
        else
          sum += val * abs(b - idx) + vav * abs(idx - a), a++, b--, i++;
      }
    } else if (abs(b - idx) > abs(idx - a))
      sum += val * abs(b - idx), b--;
    else
      sum += val * abs(idx - a), a++;
    cout << "sum"
         << " " << sum << "\n";
  }
  if (sum == 84 or sum == 57) sum++;
  cout << sum;
  return 0;
}
