#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
long long n;
pair<long long, long long> arr[N];
deque<long long> dq;
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first == b.first) {
    return a.second > b.second;
  } else {
    return a.first > b.first;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    arr[i] = {a, i};
    dq.push_back(a);
  }
  sort(arr + 1, arr + 1 + n, cmp);
  long long ans = 0;
  int ptr = 1;
  while (ptr <= n) {
    long long tp = arr[ptr].second;
    if (abs(tp - dq.front()) > abs(tp - dq.back())) {
      ans += abs(tp - dq.front()) * arr[ptr].first;
      dq.pop_front();
    } else {
      ans += abs(tp - dq.back()) * arr[ptr].first;
      dq.pop_back();
    }
    ptr++;
  }
  cout << ans << '\n';
  return 0;
}
