#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
bool chmax(T& max, const U& x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}
template <typename T, typename U>
bool chmin(T& min, const U& x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}
int main() {
  long long n, ans = 0;
  cin >> n;
  priority_queue<pair<long long, long long>> a;
  for (long long i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    a.push(make_pair(tmp, i));
  }
  pair<long long, long long> kouho = make_pair(0, n - 1);
  while (!a.empty()) {
    pair<long long, long long> tmp = a.top();
    a.pop();
    if (abs(tmp.second - kouho.first) < abs(tmp.second - kouho.second)) {
      ans += tmp.first * abs(tmp.second - kouho.second);
      kouho.second--;
    } else if (abs(tmp.second - kouho.first) > abs(tmp.second - kouho.second)) {
      ans += tmp.first * abs(tmp.second - kouho.first);
      kouho.first++;
    } else {
      if (a.empty()) {
        ans += tmp.first * abs(tmp.second - kouho.second);
      } else {
        if (tmp.second > a.top().second) {
          ans += tmp.first * abs(tmp.second - kouho.first);
          kouho.first++;
        } else {
          ans += tmp.first * abs(tmp.second - kouho.second);
          kouho.second--;
        }
      }
    }
  }
  cout << ans << endl;
}
