const Main = input => {
  const sp = input.split('\n');
  const N = Number(sp[0]);
  const A_list = sp[1].split(' ').map((e, i) => { return { index: i, num: Number(e) } });

  A_list.reverse();
  A_list.sort((a, b) => b.num - a.num);

  const isFree = new Array(N).fill(true);
  let result = 0;

  for(let A of A_list) {
    const max = { index: A.index, num: 0 };
    for(let i=0; i<N; i++) {
      if(isFree[i]) {
        const diff = Math.abs(A.index - i);
        //console.log(diff, max.num);
        if(diff > max.num) {
          max.index = i;
          max.num = diff;
          A.diff = diff;
        }
      }
    }
  //  console.log(A, max);
    isFree[max.index] = false;
    result += A.num * A.diff;
  }

  console.log(result);
//  console.log('=======================');
};

Main(require('fs').readFileSync('/dev/stdin', 'utf8'));
/*Main(`4
1 3 4 2`);
Main(`6
5 5 6 1 1 1`);
Main(`6
8 6 9 1 2 1`);*/
