#include <bits/stdc++.h>
int main() {
  int N;
  std::cin >> N;
  std::vector<unsigned long long> A(N), original(N), sorted(N);
  std::vector<bool> flag(N, 1);
  int sumR = 0, sumL = 0;
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
    original[i] = A[i];
    if (i < (N / 2 - 1)) {
      sumL += A[i];
    } else if (i < ((N + 2) / 2 - 1)) {
      sumR *= A[i];
    }
  }
  auto lStart = A.begin();
  auto div = A.begin();
  auto rEnd = A.end();
  if (N % 2 == 0) {
    div += N / 2;
  } else {
    if (sumL < sumR) {
      div += (N + 1) / 2;
    } else {
      div += (N - 1) / 2;
    }
  }
  std::sort(lStart, div);
  std::sort(div, rEnd);
  std::reverse(div, rEnd);
  div = A.begin();
  if (N % 2 == 0) {
    div += N / 2;
  } else {
    if (sumL < sumR) {
      div += (N + 1) / 2;
    } else {
      div += (N - 1) / 2;
    }
  }
  for (auto itr = div; itr != A.end(); ++itr) {
    sorted[std::distance(div, itr)] = (*itr);
  }
  int buf = std::distance(div, A.end());
  for (auto itr = A.begin(); itr != div; ++itr) {
    sorted[buf + std::distance(A.begin(), itr)] = (*itr);
  }
  unsigned long long ans = 0;
  for (int i = 0; i < N; ++i) {
    int j;
    if (i <= N / 2) {
      for (j = N - 1; j >= 0; --j) {
        if (flag[j] & sorted[i] == original[j]) {
          ans += sorted[i] * abs(i - j);
          flag[j] = 0;
          break;
        }
      }
    } else {
      for (j = 0; j < N; ++j) {
        if (flag[j] & sorted[i] == original[j]) {
          ans += sorted[i] * abs(i - j);
          flag[j] = 0;
          break;
        }
      }
    }
  }
  std::cout << ans << std::endl;
}
