#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int main(int argc, const char* argv[]) {
  int n;
  std::cin >> n;
  std::priority_queue<long long> qa;
  std::map<long long,
           std::priority_queue<int, std::vector<int>, std::greater<int> > >
      ma;
  for (int i = 0; i < n; i++) {
    long long in;
    std::cin >> in;
    qa.push(in);
    ma[in].push(i + 1);
  }
  long long sum = 0;
  int min = 1;
  int max = n;
  for (int i = 0; i < n; i++) {
    long long activeness = qa.top();
    qa.pop();
    int index = ma[activeness].top();
    ma[activeness].pop();
    std::cout << "activeness " << activeness << " ma[activeness] " << index
              << std::endl;
    if (index - min > max - index) {
      sum += activeness * (index - min);
      min++;
    } else {
      sum += activeness * (max - index);
      max--;
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
