-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE Strict #-}
import Data.Char (isSpace)
import Data.Int (Int64)
import Data.Array.Unboxed
import Data.Array.ST
import Control.Monad
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.ByteString.Char8 as BS
import Data.Bits
import qualified Data.Sequence as Seq
import Data.Semigroup

maxUreshisa :: Int -> Seq.Seq (Int, Int64) -> (Int64, Int)
maxUreshisa j seq = getMax $ Seq.foldMapWithIndex (\index (i, a) -> Max (a * fromIntegral (abs (i - j)), index)) seq

main = do
  n <- readLn @Int
  xs <- U.map fromIntegral . U.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  let loop :: Int -> Int -> Int64 -> Seq.Seq (Int, Int64) -> Int64
      loop k l acc ys | k == l = acc
                      | otherwise = let (left, leftIndex) = maxUreshisa k ys
                                        (right, rightIndex) = maxUreshisa (l - 1) ys
                                    in if left > right
                                       then loop (k + 1) l (acc + left) (Seq.deleteAt leftIndex ys)
                                       else loop k (l - 1) (acc + right) (Seq.deleteAt rightIndex ys)
  print $ loop 0 n 0 (Seq.fromList $ U.toList $ U.indexed xs)
