import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main() {
  int n;
  readf (" %d", n);
  auto a = new int[n];
  foreach (t; 0 .. n) {
    readf (" %d", a[t]);
  }
  int u = 0, v = n - 1;
  long res;
  auto idx = iota (0, n).array;
  while (!idx.empty) {
    size_t best = size_t.max;
    int tp = -1;
    long mx = long.min;
    foreach (i, j; idx) {
      int v1 = abs (j - u);
      int v2 = abs (j - v);
      if (v1 > v2) {
        long t = v1.to!long * a[j];
        if (mx < t) {
          mx = t;
          best = i;
          tp = 0;
        }
      } else {
        long t = v2.to!long * a[j];
        if (mx < t) {
          mx = t;
          best = i;
          tp = 1;
        }
      }
    }
    res += mx;
    if (!tp) ++u; else --v;
    swap (idx[best], idx.back);
    idx.popBack ();
  }
  writeln (res);
}

