#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using intpair = pair<int, int>;
using intpv = vector<intpair>;
using llpair = pair<ll, ll>;
using llpv = vector<llpair>;
using intvec = vector<int>;
using llvec = vector<ll>;
using intq = queue<int>;
using llq = queue<ll>;
using intmat = vector<intvec>;
using llmat = vector<llvec>;
using pairmat = vector<llpv>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll GCD(ll a, ll b) {
  if (a == 0 || b == 0) return 0;
  if (a < b) swap(a, b);
  ll tmp = a % b;
  while (tmp != 0) {
    a = b;
    b = tmp;
    tmp = a % b;
  }
  return b;
}
ll binpow(ll a, ll ex, ll p) {
  ll result = 1LL;
  while (ex > 0) {
    if (ex & 1) result = result * a % p;
    ex >>= 1;
    a = a * a % p;
  }
  return result;
}
ll Fact(ll x, ll p) {
  ll f = 1;
  for (ll i = 2; i <= x; i++) {
    f *= i;
    f %= p;
  }
  return f;
}
ll nPr(ll n, ll r) {
  if (n < r) return 0;
  ll result = 1LL;
  for (ll i = 0; i < r; i++) result *= n - i;
  return result;
}
ll nPrP(ll n, ll r, ll p) {
  if (n < r) return 0;
  ll result = 1LL;
  for (int i = 0; i < r; i++) {
    result *= n - i;
    result %= p;
  }
  return result;
}
ll nCr(ll n, ll r) {
  if (n == r) {
    return 1;
  }
  if (r > n) {
    return 0;
  }
  if (r > n / 2) {
    r = n - r;
  }
  if (n == 0) {
    return 0;
  }
  if (r == 0) {
    return 1;
  }
  if (r == 1) {
    return n;
  }
  double result = 1;
  for (double i = 1; i <= r; i++) {
    result *= (n - i + 1) / i;
  }
  return (ll)result;
}
llvec fact, inv, factinv;
void prenCrP(ll n, ll p) {
  fact.resize(n + 1);
  inv.resize(n + 1);
  factinv.resize(n + 1);
  fact[0] = fact[1] = inv[1] = factinv[0] = factinv[1] = 1LL;
  for (ll i = 2LL; i <= n; i++) {
    fact[i] = fact[i - 1] * i % p;
    inv[i] = p - inv[p % i] * (p / i) % p;
    factinv[i] = factinv[i - 1] * inv[i] % p;
  }
}
ll nCrP(ll n, ll r, ll p) {
  if (r > n) return 0;
  return fact[n] * factinv[r] % p * factinv[n - r] % p;
}
llvec fact2, inv2, factinv2;
void prenCrP2(ll n, ll r, ll p) {
  fact2.resize(r + 1);
  inv2.resize(r + 1);
  factinv2.resize(r + 1);
  fact2[0] = n % p, fact2[1] = n % p * (n - 1) % p;
  inv2[1] = factinv2[0] = factinv2[1] = 1LL;
  for (ll i = 2LL; i <= r; i++) {
    fact2[i] = fact2[i - 1] * (n - i) % p;
    inv2[i] = p - inv2[p % i] * (p / i) % p;
    factinv2[i] = factinv2[i - 1] * inv2[i] % p;
  }
}
ll nCrP2(ll r, ll p) { return fact2[r - 1] * factinv2[r] % p; }
ll LowerBinarySearch(llvec array, ll key, ll max, ll min) {
  if (max > min) {
    return -1LL;
  } else {
    ll mid = max + (min - max) / 2;
    if (array[mid] > key) {
      return LowerBinarySearch(array, key, max, mid - 1);
    }
    if (array[mid] < key) {
      return LowerBinarySearch(array, key, mid + 1, min);
    } else {
      return mid;
    }
  }
}
ll GreaterBinarySearch(llvec array, ll key, ll max, ll min) {
  if (max > min) {
    return -1LL;
  } else {
    ll mid = max + (min - max) / 2;
    if (array[mid] < key) {
      return GreaterBinarySearch(array, key, max, mid - 1);
    }
    if (array[mid] > key) {
      return GreaterBinarySearch(array, key, mid + 1, min);
    } else {
      return mid;
    }
  }
}
int DigitNum(ll n) {
  int digit = 0;
  ll wari = 1LL;
  while (n / wari) {
    digit++;
    wari *= 10;
  }
  return digit;
}
bool IsPrime(ll num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;
  double sqrtNum = sqrt(num);
  for (ll i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
vector<ll> Divisor(ll x) {
  vector<ll> result;
  ll i = 1LL;
  for (; i * i < x; i++) {
    if (x % i) continue;
    result.push_back(i);
    result.push_back(x / i);
  }
  if (i * i == x && x % i == 0) result.push_back(i);
  sort(result.begin(), result.end());
  return result;
}
vector<llpair> PrimeFact(ll x) {
  vector<llpair> result;
  ll ex = 0LL;
  if (x % 2 == 0) {
    while (x % 2 == 0) {
      x /= 2;
      ex++;
    }
    result.push_back({2, ex});
  }
  for (ll i = 3LL; i * i <= x; i += 2) {
    if (x % i) continue;
    ex = 0LL;
    while (x % i == 0) {
      x /= i;
      ex++;
    }
    result.push_back({i, ex});
  }
  if (x != 1) result.push_back({x, 1});
  return result;
}
bool Palind(string s) { return s == string(s.rbegin(), s.rend()); }
struct Union_Find {
  vector<int> parent;
  vector<int> num;
  Union_Find(int N) : parent(N), num(N, 1) {
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }
  int root(int x) {
    if (parent[x] == x) return x;
    return parent[x] = root(parent[x]);
  }
  void merge(int x, int y) {
    int xrt = root(x);
    int yrt = root(y);
    if (xrt == yrt) return;
    parent[xrt] = yrt;
    num[yrt] += num[xrt];
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return num[root(x)]; }
};
struct Node {
  int pos;
  int npos;
  ll A;
  ll ncost;
  int lr;
  bool operator<(const Node& a) const {
    return ncost == a.ncost ? A > a.A : ncost > a.ncost;
  }
};
int N;
Node node[2000];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> node[i].A;
    node[i].pos = i;
    node[i].npos = -1;
  }
  sort(node, node + N);
  int L = 0, R = N - 1;
  for (int j = 0; j < N; j++) {
    vector<Node*> v;
    for (int i = 0; i < N; i++) {
      if (node[i].npos == -1) {
        int p = node[i].pos;
        ll a = node[i].A;
        if (abs(p - L) == abs(p - R)) {
          node[i].lr = 2;
          node[i].ncost = abs(p - L) * a;
        } else if (abs(p - L) < abs(p - R)) {
          node[i].lr = 1;
          node[i].ncost = abs(p - R) * a;
        } else {
          node[i].lr = 0;
          node[i].ncost = abs(p - L) * a;
        }
        v.push_back(&node[i]);
      }
    }
    sort(v.begin(), v.end(), [](Node* L, Node* R) {
      Node l = *L, r = *R;
      return l.ncost == r.ncost ? l.A > r.A : l.ncost > r.ncost;
    });
    Node* nd = v[0];
    if ((*nd).lr != 2) {
      if ((*nd).lr == 0) {
        (*nd).npos = L;
        L++;
      } else {
        (*nd).npos = R;
        R--;
      }
    } else {
      if (v.size() == 1) {
        (*nd).npos = L;
        continue;
      }
      Node nnd = *v[1];
      if (nnd.lr == 2 || nnd.lr == 1) {
        (*nd).npos = L;
        L++;
      } else {
        (*nd).npos = R;
        R--;
      }
    }
  }
  ll res = 0LL;
  for (int i = 0; i < N; i++) res += node[i].ncost;
  cout << res << endl;
}
