#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  long long A[N];
  int x = 0;
  int y = N - 1;
  long long ans = 0;
  pair<long long, int> B[N];
  bool z = false;
  for (int i = 0; i < (int)(N); i++) {
    cin >> A[i];
    B[i].second = i;
    B[i].first = A[i];
  }
  sort(B, B + N, std::greater<pair<long long, int>>());
  for (int i = 0; i < (int)(N); i++) {
    if (z) {
      if (B[i].second - x > y - B[i].second) {
        ans += (B[i].second - x) * B[i].first;
      } else if (B[i].second - x < y - B[i].second) {
        ans += (y - B[i].second) * B[i].first;
      }
      x++;
      y--;
      z = false;
    }
    if (B[i].second - x > y - B[i].second) {
      ans += (B[i].second - x) * B[i].first;
      x++;
    } else if (B[i].second - x < y - B[i].second) {
      ans += (y - B[i].second) * B[i].first;
      y--;
    } else {
      ans += (y - B[i].second) * B[i].first;
      z = true;
    }
  }
  cout << ans << endl;
}
