#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  unsigned long long A[2005], sum = 0;
  bool done[2005];
  memset(done, 0, sizeof done);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  A[0] = 0;
  int left = 1, right = N;
  for (int i = 1; i <= N; i++) {
    int best_chc = 0, dir = 0;
    for (int j = 1; j <= N; j++) {
      if (!done[j]) {
        if (dir == 0) {
          if (A[j] * abs(j - left) > A[best_chc] * abs(best_chc - left)) {
            dir = 0;
            best_chc = j;
          } else if (A[j] * abs(j - right) >
                     A[best_chc] * abs(best_chc - left)) {
            dir = 1;
            best_chc = j;
          } else if (A[j] * abs(j - left) ==
                     A[best_chc] * abs(best_chc - left)) {
            if (A[j] < A[best_chc]) {
              dir = 0;
              best_chc = j;
            }
          } else if (A[j] * abs(j - right) ==
                     A[best_chc] * abs(best_chc - left)) {
            if (A[j] < A[best_chc]) {
              dir = 1;
              best_chc = j;
            }
          }
        } else {
          if (A[j] * abs(j - left) > A[best_chc] * abs(best_chc - right)) {
            dir = 0;
            best_chc = j;
          } else if (A[j] * abs(j - right) >
                     A[best_chc] * abs(best_chc - right)) {
            dir = 1;
            best_chc = j;
          } else if (A[j] * abs(j - left) ==
                     A[best_chc] * abs(best_chc - right)) {
            if (A[j] < A[best_chc]) {
              dir = 0;
              best_chc = j;
            }
          } else if (A[j] * abs(j - right) ==
                     A[best_chc] * abs(best_chc - right)) {
            if (A[j] < A[best_chc]) {
              dir = 1;
              best_chc = j;
            }
          }
        }
      }
    }
    if (dir == 0) {
      sum += A[best_chc] * abs(best_chc - left);
      left++;
    } else {
      sum += A[best_chc] * abs(best_chc - right);
      right--;
    }
    done[best_chc] = 1;
  }
  cout << sum << "\n";
  return 0;
}
