#include <bits/stdc++.h>
struct infant {
  int position;
  int lively;
  bool operator<(const infant &right) const {
    return lively == right.lively ? position < right.position
                                  : lively < right.lively;
  }
};
int main() {
  int N = 0;
  std::cin >> N;
  std::vector<infant> vec_infants(N);
  for (int cnt = 0; cnt < N; cnt++) {
    vec_infants.at(cnt).position = cnt + 1;
    std::cin >> vec_infants.at(cnt).lively;
  }
  sort(vec_infants.begin(), vec_infants.end());
  reverse(vec_infants.begin(), vec_infants.end());
  int answer = 0;
  int count = 0;
  std::vector<bool> afterpos(N, false);
  for (infant inf : vec_infants) {
    int right = 0;
    int left = 0;
    for (int cnt = N - 1; 0 <= cnt; cnt--) {
      if (afterpos.at(cnt) == false) {
        right = cnt + 1;
        break;
      }
    }
    for (int cnt = 0; cnt < N; cnt++) {
      if (afterpos.at(cnt) == false) {
        left = cnt + 1;
        break;
      }
    }
    int move_right = std::abs(inf.position - right);
    int move_left = std::abs(inf.position - left);
    int point = 0;
    if (move_right < move_left) {
      answer += inf.lively * move_left;
      afterpos.at(left - 1) = true;
      point = inf.lively * move_left;
    } else {
      answer += inf.lively * move_right;
      afterpos.at(right - 1) = true;
      point = inf.lively * move_right;
    }
  }
  std::cout << answer << std::endl;
  return 0;
}
