#include <bits/stdc++.h>
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
const long long M = 1000000007ll;
const long long MOD = 1000000007ll;
const long long MAX = 100000001ll;
long long cnt = 0;
const int mod = 1000000007;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod - a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint& operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};
mint f(long long n) {
  if (n == 0) return 1;
  mint ans = f(n / 2);
  ans *= ans;
  if (n % 2 == 1) ans *= 2;
  return ans;
}
mint choose(long long n, long long k) {
  mint x = 1, y = 1;
  for (long long i = 0; i < (k); i++) {
    x *= (n - i);
    y *= (i + 1);
  }
  return x / y;
}
struct BIT {
 private:
  vector<long long> bit;
  long long N;

 public:
  BIT(long long size) {
    N = size;
    bit.resize(N + 1);
  }
  void add(long long a, long long w) {
    for (long long x = a; x <= N; x += x & -x) bit[x] += w;
  }
  int sum(long long a) {
    long long ret = 0;
    for (long long x = a; x > 0; x -= x & -x) ret += bit[x];
    return ret;
  }
};
int main() {
  long long n, k;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < (n); i++) {
    cin >> v[i];
  }
  long long l = 0, r = n - 1;
  long long ans = 0;
  while (true) {
    long long tmp = 0;
    long long can = 0;
    long long can_i = 0;
    for (long long i = 0; i < (n); i++) {
      if (v[i] * (i - l) > tmp) {
        can = -1;
        can_i = i;
        tmp = v[i] * (i - l);
      }
      if (v[i] * (r - i) > tmp) {
        can = -2;
        can_i = i;
        tmp = v[i] * (r - i);
      }
    }
    ans += tmp;
    v[can_i] = 0;
    if (can == -2)
      r--;
    else
      l++;
    if (r < l) break;
  }
  cout << ans << endl;
}
