#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const int MOD = 1000000007;
const int MAX_N = 101010;
void sort_second(vector<pair<long long, long long>> &A, int N);
long long a_n(long long a, long long n);
void factorial_map(vector<long long> &n, vector<long long> &ninv);
long long inv(long long a);
void prime_map(map<long long, long long> &a, long long n);
void prime_list(long long n, vector<long long> &primes, vector<bool> &is_prime);
long long gcd_map(map<long long, long long> a, map<long long, long long> b);
long long lcm_map(map<long long, long long> a, map<long long, long long> b);
long long my_gcd(long long a, long long b);
long long nCk(long long n, long long k);
long long my_abs(long long a);
void z_algorithm(string s, vector<int> &a);
class Unionfind {
 public:
  vector<long long> par, siz, rank;
  Unionfind();
  long long root(long long x);
  void merge(long long x, long long y);
  bool issame(long long x, long long y);
  long long size(long long x);
};
int main() {
  long long n, ans = 0;
  cin >> n;
  long long l = 0, r = n - 1;
  vector<long long> a(n);
  vector<bool> used(n, false);
  for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i];
  for (long long(i) = 0; (i) < (long long)(n); (i)++) {
    long long max = -1, index = -1;
    bool left = true;
    for (long long(j) = 0; (j) < (long long)(n); (j)++) {
      if (!used[j]) {
        if ((j - l) * a[j] > max) {
          max = (j - l) * a[j];
          left = true;
          index = j;
        }
        if ((r - j) * a[j] > max) {
          max = (r - j) * a[j];
          left = false;
          index = j;
        }
      }
    }
    ans += max;
    used[index] = true;
    if (left)
      l++;
    else
      r--;
  }
  cout << ans << endl;
  return 0;
}
void sort_second(vector<pair<long long, long long>> &A, int N) {
  for (long long(i) = 0; (i) < (long long)(N); (i)++)
    swap(A.at(i).first, A.at(i).second);
  sort((A).begin(), (A).end());
  for (long long(i) = 0; (i) < (long long)(N); (i)++)
    swap(A.at(i).first, A.at(i).second);
}
long long a_n(long long a, long long n) {
  if (n == 0) return 1;
  long long ret = a, count = 1;
  while (count * 2 < n) {
    ret *= ret;
    ret %= MOD;
    count *= 2;
  }
  if (count == n)
    return ret;
  else
    return (ret * a_n(a, n - count)) % MOD;
}
void factorial_map(vector<long long> &n, vector<long long> &ninv) {
  n[0] = 1;
  for (long long i = 1; i < n.size(); i++) n[i] = (n[i - 1] * i) % MOD;
  ninv[n.size() - 1] = inv(n[n.size() - 1]);
  for (long long i = n.size() - 2; i > -1; i--)
    ninv[i] = (ninv[i + 1] * (i + 1)) % MOD;
}
long long inv(long long a) { return a_n(a, MOD - 2); }
void prime_map(map<long long, long long> &a, long long n) {
  while ((n % 2) == 0) {
    n /= 2;
    if (a.count(2))
      a.at(2)++;
    else
      a[2] = 1;
  }
  for (long long i = 3; i * i <= n; i += 2) {
    while ((n % i) == 0) {
      n /= i;
      if (a.count(i))
        a.at(i)++;
      else
        a[i] = 1;
    }
  }
  if (n != 1) a[n] = 1;
}
long long gcd_map(map<long long, long long> a, map<long long, long long> b) {
  long long ret = 1;
  for (auto p : a) {
    auto key = p.first;
    auto value = p.second;
    if (b.count(key)) {
      if (value > b.at(key))
        ret *= a_n(key, b.at(key));
      else
        ret *= a_n(key, value);
    }
  }
  return ret;
}
long long lcm_map(map<long long, long long> a, map<long long, long long> b) {
  long long ret = 1;
  for (auto p : a) {
    auto key = p.first;
    auto value = p.second;
    if (b.count(key)) {
      if (value > b.at(key))
        ret *= a_n(key, value);
      else
        ret *= a_n(key, b.at(key));
    } else {
      ret *= a_n(key, value);
    }
  }
  for (auto p : b) {
    auto key = p.first;
    auto value = p.second;
    if (a.count(key)) {
      continue;
    } else {
      ret *= a_n(key, value);
    }
  }
  return ret;
}
long long my_gcd(long long a, long long b) {
  if (b == 0) return a;
  return my_gcd(b, a % b);
}
long long nCk(long long n, long long k) {
  long long kfac = 1, ret = 1;
  for (long long i = 1; i <= k; i++) {
    kfac = (kfac * i) % MOD;
  }
  for (long long i = n; i >= n - k + 1; i--) {
    ret = (ret * i) % MOD;
  }
  ret = (ret * inv(kfac)) % MOD;
  return ret;
}
void prime_list(long long n, vector<long long> &primes,
                vector<bool> &is_prime) {
  long long p = 0;
  for (long long(i) = 0; (i) < (long long)(n + 1); (i)++) is_prime[i] = true;
  is_prime[0] = false;
  is_prime[1] = false;
  for (long long i = 2; i <= n; i++) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (long long j = 2 * i; j <= n; j += i) is_prime[j] = false;
    }
  }
}
long long my_abs(long long a) {
  if (a >= 0)
    return a;
  else
    return -1 * a;
}
Unionfind::Unionfind() {
  par.resize(MAX_N);
  siz.resize(MAX_N);
  rank.resize(MAX_N);
  for (long long(i) = 0; (i) < (long long)(MAX_N); (i)++) {
    par[i] = i;
    siz[i] = 1;
    rank[i] = 0;
  }
}
long long Unionfind::root(long long x) {
  if (par[x] == x)
    return x;
  else {
    return par[x] = root(par[x]);
  }
}
void Unionfind::merge(long long x, long long y) {
  x = root(x);
  y = root(y);
  if (x == y) return;
  if (rank[x] < rank[y]) {
    par[x] = y;
    siz[y] += siz[x];
  } else {
    par[y] = x;
    siz[x] += siz[y];
    if (rank[x] == rank[y]) rank[x]++;
  }
}
bool Unionfind::issame(long long x, long long y) { return root(x) == root(y); }
long long Unionfind::size(long long x) { return siz[root(x)]; }
void z_algorithm(string s, vector<int> &a) {
  a.at(0) = s.size();
  int i = 1, j = 0;
  while (i < s.size()) {
    while ((i + j < s.size()) && (s.at(j) == s.at(i + j))) j++;
    a.at(i) = j;
    if (j == 0) {
      i++;
      continue;
    }
    int k = 1;
    while ((i + k < s.size()) && (k + a.at(k) < j)) {
      a.at(i + k) = a.at(k);
      k++;
    }
    i += k;
    j -= k;
  }
}
