#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (int i = 0; i < (int)v.size(); i++) {
    if (i) os << ", ";
    os << v[i];
  }
  os << "}";
  return os;
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
bool cmp(const pair<long long, long long>& a,
         const pair<long long, long long>& b) {
  if (a.first == b.first) return (a.second >= b.second);
  return (a.first < b.first);
}
vector<long long> primes;
vector<long long> seive;
void Seive(long long n) {
  seive.resize(n + 5, 1);
  seive[1] = 0, seive[0] = 0;
  for (int i = 2; i * i < n + 5; i++) {
    if (seive[i]) {
      for (int j = 2 * i; j < n + 5; j += i) {
        seive[j] = 0;
      }
    }
  }
}
long long power(long long x, long long y, long long m = 1000000007) {
  long long int res = 1;
  x = x % m;
  while (y > 0) {
    if (y & 1) res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res;
}
long long modinv(long long x, long long mod = 1000000007) {
  return power(x, mod - 2, mod);
}
int n, m;
long long A[300060];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int k, t, a, b, c, d;
  t = 1;
  while (t--) {
    cin >> n;
    for (long long(i) = 0; (i) < (n); ++(i)) cin >> A[i];
    long long sum = 0;
    priority_queue<pair<long long, long long> > pq;
    for (long long(i) = 0; (i) < (n); ++(i)) {
      pq.push({A[i], i});
    }
    long long ans = 0;
    map<long long, long long> M;
    for (long long(i) = 0; (i) < (n); ++(i)) {
      pair<long long, long long> x = pq.top();
      long long p, q, j, k;
      for (j = 0; j < n; j++) {
        if (M.find(j) == M.end()) {
          p = x.first * (abs(j - x.second));
          break;
        }
      }
      for (k = n - 1; k >= 0; k--) {
        if (M.find(k) == M.end()) {
          q = x.first * (abs(k - x.second));
          break;
        }
      }
      pq.pop();
      if (p >= q) {
        ans += p;
        M[j]++;
      } else {
        ans += q;
        M[k]++;
      }
    }
    cout << ans << endl;
  }
}
