#include <bits/stdc++.h>
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
using namespace std;
void file() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int mod = 1e9 + 7;
int memo[2001][2001];
vector<int> v;
vector<pair<int, pair<int, int> > > vv;
int main() {
  file();
  int n;
  cin >> n;
  v.resize(n);
  for (auto& e : v) cin >> e;
  for (int i = 0; i < n; i++)
    vv.push_back({-(max(i, n - i - 1) * v[i]), {v[i], i + 1}});
  sort(vv.begin(), vv.end());
  deque<int> dq;
  for (int i = 1; i <= n; i++) dq.push_back(i);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int first = vv[i].second.first, second = vv[i].second.second;
    if (abs(second - dq.front()) > abs(second - dq.back())) {
      ans += abs(second - dq.front()) * first;
      dq.pop_front();
    } else {
      ans += abs(second - dq.back()) * first;
      dq.pop_back();
    }
  }
  cout << ans << '\n';
}
