#include <bits/stdc++.h>
using namespace std;
long long int n;
long long int arr[20005];
vector<pair<long long int, long long int> > v;
void solve() {
  long long int l = 0;
  long long int r = n - 1;
  long long int sum = 0;
  for (long long int i = n - 1; i >= 0; i--) {
    if (abs(v[i].second - r) >= abs(v[i].second - l)) {
      sum += v[i].first * (abs(v[i].second - r));
      r--;
    } else if (abs(v[i].second - r) < abs(v[i].second - l)) {
      sum += v[i].first * (abs(v[i].second - l));
      l++;
    }
  }
  cout << sum << "\n";
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    arr[i] = x;
    v.push_back(make_pair(x, i));
  }
  sort(v.begin(), v.end());
  solve();
  return 0;
}
