#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
class CommonIO {
 public:
  CommonIO() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
  }
  ~CommonIO() {}
  template <class T>
  void scan(T &v) {
    std::cin >> v;
  }
  template <class T, class... Rest>
  void scan(T &v, Rest &...rest) {
    scan(v);
    scan(rest...);
  }
  template <class T>
  void print(T v) {
    std::cout << v;
  }
  template <class T>
  void print(std::vector<T> &v) {
    if (v.size() == 0) return;
    print(*v.begin());
    for (auto it = ++v.begin(); it != v.end(); ++it) {
      print(' ');
      print(*it);
    }
  }
  template <class T>
  void print(std::vector<std::vector<T> > &grid) {
    if (grid.size() == 0) return;
    print(*grid.begin());
    for (auto it = ++grid.begin(); it != grid.end(); ++it) {
      print('\n');
      print(*it);
    }
  }
  template <class T, class... Rest>
  void print(T v, Rest... rest) {
    print(v);
    print(' ');
    print(rest...);
  }
  template <class T>
  void printl(T v) {
    print(v);
    std::cout << '\n';
  }
  template <class T, class... Rest>
  void printl(T v, Rest... rest) {
    print(v);
    print(' ');
    printl(rest...);
  }
} io;
int main() {
  int N;
  io.scan(N);
  vector<ll> A(N);
  for (ll i = (0); i < (N); ++i) io.scan(A[i]);
  vector<ll> index(N);
  for (ll i = (0); i < (N); ++i) index[i] = i;
  ll start = 0;
  for (ll i = (0); i < (N); ++i) {
    ll a = A[start];
    ll sum = 0, max_sum = 0, max_index = 0;
    for (ll j = (start + 1); j < (N); ++j) {
      sum += (A[j] * abs(j - 1 - index[j]) + a * abs(j - index[start])) -
             (a * abs(j - 1 - index[start]) + A[j] * abs(j - index[j]));
      if (sum > max_sum) {
        max_sum = sum;
        max_index = j;
      }
    }
    for (ll j = start; j > 0; --j) {
      sum += (A[j] * abs(j - 1 - index[j]) + a * abs(j - index[start])) -
             (a * abs(j - 1 - index[start]) + A[j] * abs(j - index[j]));
      if (sum > max_sum) {
        max_sum = sum;
        max_index = j;
      }
    }
    if (max_index != 0) {
      for (ll j = (start); j < (max_index); ++j) {
        swap(A[j], A[j + 1]);
        swap(index[j], index[j + 1]);
      }
    } else {
      ++start;
    }
  }
  ll sum = 0;
  for (ll i = (0); i < (N); ++i) {
    sum += A[i] * abs(i - index[i]);
  }
  io.printl(sum);
  return 0;
}
