#include <bits/stdc++.h>
int abs(int x) { return x < 0 ? -x : x; }
struct Element {
  int index;
  int value;
  long long happiness;
};
int first = 0;
int last = 0;
int middle = 0;
bool Compare(Element &e1, Element &e2) {
  long long v1 = e1.happiness =
      static_cast<long long>(e1.value) *
      std::max(abs(e1.index - first), abs(last - e1.index));
  long long v2 = e2.happiness =
      static_cast<long long>(e2.value) *
      std::max(abs(e2.index - first), abs(last - e2.index));
  if (v1 == v2) {
    return e2.index == middle;
  }
  return v1 > v2;
}
template <class RandomAccessIterator>
void Sort(RandomAccessIterator first, RandomAccessIterator last) {
  if (first >= last - 1) {
    return;
  }
  for (auto it = first; it < last - 1; it++) {
    if (Compare(*it, *(it + 1))) {
      std::swap(*it, *(it + 1));
    }
  }
  for (auto it = first; it < last - 2; it++) {
    if (Compare(*it, *(it + 1))) {
      std::swap(*it, *(it + 1));
    }
  }
}
int main() {
  int n;
  std::cin >> n;
  last = n - 1;
  middle = n % 2 == 1 ? n / 2 : -1;
  std::vector<Element> a(n);
  for (int i = 0; i < n; i++) {
    a[i].index = i;
    std::cin >> a[i].value;
  }
  int sum = 0;
  while (!a.empty()) {
    if (a.size() == 1) {
      sum += a[0].value * abs(a[0].index - first);
      break;
    }
    Sort(a.begin(), a.end());
    if (a.back().index == middle) {
      if (a[a.size() - 2].index < middle) {
        first++;
      } else {
        last--;
      }
    } else {
      if (a.back().index >= (first + last) / 2) {
        first++;
      } else {
        last--;
      }
    }
    sum += a.back().happiness;
    a.pop_back();
    if (middle == -1) {
      middle = (first + last) / 2;
    } else {
      middle = -1;
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
