#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = INT64_MAX;
long long i, j, k;
int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) cin >> A[i];
  long long ans = 0;
  vector<bool> jud(N, true);
  priority_queue<tuple<long long, long long, long long> > que;
  for (long long i = 0; i < N; i++)
    que.push(
        tuple<long long, long long, long long>(A[i], max(i, N - i - 1), i));
  while (!que.empty()) {
    tuple<long long, long long, long long> f = que.top();
    que.pop();
    long long a = get<0>(f), b = get<1>(f), c = get<2>(f);
    for (long long j = get<1>(f); j >= 0; j--) {
      if (c + j < N && c - j >= 0) {
        if (jud[c + j] == true && jud[c - j] == true) {
          if (A[c + j] > A[c - j]) {
            jud[c + j] = false;
            ans += j * a;
            break;
          } else {
            jud[c - j] = false;
            ans += j * a;
            break;
          }
        } else if (jud[c + j] == true) {
          jud[c + j] = false;
          ans += j * a;
          break;
        } else if (jud[c - j] == true) {
          jud[c - j] = false;
          ans += j * a;
          break;
        }
      } else if (c + j < N) {
        if (jud[c + j] == true) {
          jud[c + j] = false;
          ans += j * a;
          break;
        }
      } else if (c - j >= 0) {
        if (jud[c - j] == true) {
          jud[c - j] = false;
          ans += j * a;
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
