using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var n = int.Parse(Console.ReadLine());
        var a = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        var ai = a.Select((t, i) => new { Value = t, Index = i }).OrderByDescending(t => t.Value).ToArray();

        var isMoved = new bool[n];

        long sum = 0;

        // 活性度の高いのものから貪欲法
        for(int i = 0; i < n; i++)
        {
            var index = ai[i].Index;
            if (isMoved[index]) continue;

            long left = 0;
            long right = n - 1;
            for(int j = 0; j < index; j++)
            {
                if (isMoved[j] == false)
                {
                    left = j;
                    break;
                }
            }
            for(int j = n - 1; j > index; j--)
            {
                if (isMoved[j] == false)
                {
                    right = j;
                    break;
                }
            }

            long target = 0;

            if(Math.Abs(index - left) == Math.Abs(index - right))
            {
                target = a[left] > a[right] ? right : left;
            }
            else
            {
                target = Math.Abs(index - left) > Math.Abs(right - index) ? left : right;
            }

            isMoved[target] = true;
            isMoved[index] = true;
            Console.WriteLine("Swap:" + index + " " + target);
            var dis = Math.Abs(index - target);
            sum = sum + (a[index] * dis) + (a[target] * dis);
        }

        Console.WriteLine(sum);
    }

}