#include <bits/stdc++.h>
int main() {
  long long int N;
  scanf("%lld", &N);
  long long int A[N + 1];
  long long int C[N + 1];
  long long int left;
  long long int right;
  long long int SUB_MAX;
  long long int MAX = 0;
  long long int MAX_INDEX;
  long long int SUM = 0;
  int mini_counter;
  int counter;
  for (int i = 1; i <= N; ++i) {
    C[i] = 0;
  }
  left = 1;
  right = N;
  for (long long int k = 1; k <= N; ++k) {
    scanf("%lld", &A[k]);
  }
  for (long long int j = 1; j <= N; ++j) {
    MAX = 0;
    MAX_INDEX = 0;
    counter = 0;
    for (long long int i = 1; i <= N; ++i) {
      if (C[i] != 1 && (i - left) != (right - i)) {
        if (right - i > i - left) {
          SUB_MAX = A[i] * (right - i);
          mini_counter = 1;
        } else {
          SUB_MAX = A[i] * (i - left);
          mini_counter = -1;
        }
        if (SUB_MAX > MAX) {
          MAX = SUB_MAX;
          MAX_INDEX = i;
          counter = mini_counter;
        }
      }
    }
    SUM += MAX;
    C[MAX_INDEX] = 1;
    if (counter == 1) {
      right -= 1;
    } else {
      left += 1;
    }
  }
  printf("%lld", SUM);
  return 0;
}
int D_4_19() {
  long long int N;
  long long int K;
  long long int S1 = 0;
  long long int S2 = 0;
  long long int SUM = 0;
  long long int MOD = 1000000007;
  scanf("%lld %lld", &N, &K);
  for (long long int k = K; k <= N + 1; ++k) {
    S1 = ((k) * (k - 1) / 2);
    S2 = (k * (N + N - k + 1) / 2);
    SUM += S2 - S1 + 1;
    SUM = SUM % MOD;
  }
  printf("%lld\n", SUM);
  return 0;
}
