import std.stdio, std.conv, std.string, std.algorithm,
       std.math, std.array, std.container, std.typecons;

long n;
long[] a;

alias tuple = Tuple!(long, long);
bool cmp(tuple x, tuple y) {
  return x[1]>y[1];
}

bool cmp1(tuple x, tuple y) {
  return (x[0]-(n/2)) > (y[0]-(n/2));
}

void main() {
  n = readln.chomp.to!long;
  a = readln.chomp.split.to!(long[]);
  tuple[] ts = new tuple[n];
  for(int i=0; i<n; i++) ts[i] = tuple(i, a[i]);
  std.algorithm.sort!cmp1(ts);
  std.algorithm.sort!cmp(ts);
  long llen, rlen;
  long sum = 0;
  for(int i=0; i<n; i++) {
    long lcost = ts[i][0]-llen;
    long rcost = n-ts[i][0]-rlen-1;
    if(lcost > rcost) {
      sum += lcost*ts[i][1];
      llen++;
    }
    else {
      sum += rcost*ts[i][1];
      rlen++;
    }
  }
  sum.writeln;
}
