#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
long long n, i;
priority_queue<pair<long long, long long>> pq;
deque<long long> dq;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    pq.push({a, (long long)i});
    dq.push_back(a);
  }
  long long ans = 0;
  while (!pq.empty()) {
    long long tp = pq.top().second;
    if (abs(tp - dq.front()) > abs(tp - dq.back())) {
      ans += abs(tp - dq.front()) * pq.top().first;
      dq.pop_front();
    } else {
      ans += abs(tp - dq.back()) * pq.top().second;
      dq.pop_back();
    }
    pq.pop();
  }
  cout << ans << '\n';
  return 0;
}
