#include <bits/stdc++.h>
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int MAXINT = 2147483647;
const int mod = 1000000007;
const long long MAXLONG = 9223372036854775807;
using namespace std;
template <class T>
inline bool Read(T &ret) {
  char c;
  int sgn;
  if (static_cast<void>(c = getchar()), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (static_cast<void>(c = getchar()), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T>
inline void Write(T x) {
  if (x < 0) putchar('-'), Write(x);
  if (x > 9) Write(x / 10);
  putchar(x % 10 + '0');
}
void solve(int question_num) {
  while (question_num--) {
    struct node {
      int val, pos;
      bool operator<(const node &t1) const {
        if (val != t1.val) return val > t1.val;
        return pos > t1.pos;
      }
    } a[2002];
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i].val;
      a[i].pos = i;
    }
    long long ans = 0;
    sort(a + 1, a + 1 + n);
    int p1 = 1, p2 = n;
    while (p1 < p2) {
      ans += (a[p1].val * abs(a[p1].pos - a[p2].pos));
      ans += (a[p2].val * abs(a[p1].pos - a[p2].pos));
      p1++;
      p2--;
    }
    cout << ans;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  solve(t);
  return 0;
}
