-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE TypeApplications #-}
import Data.Char (isSpace)
import Data.Int (Int64)
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Algorithms.Merge as A
import qualified Data.ByteString.Char8 as BS

solveL :: Int -> U.Vector (Int, Int64) -> Int64
solveL n xs = let ys = U.modify (A.sortBy (\(i,a) (j,b) -> compare b a <> compare i j)) xs
              in sum [ fromIntegral (abs (n - 1 - i - j)) * a | (i,(j,a)) <- zip [0..] (U.toList ys) ]

solveR :: U.Vector (Int, Int64) -> Int64
solveR xs = let ys = U.modify (A.sortBy (\(i,a) (j,b) -> compare b a <> compare i j)) xs
            in sum [ fromIntegral (abs (j - i)) * a | (i,(j,a)) <- zip [0..] (U.toList ys) ]

solve :: Int -> U.Vector Int64 -> Int64
solve m xs = let (xs0, xs1) = U.splitAt m (U.indexed xs)
             in solveL (U.length xs) xs0 + solveR xs1

main = do
  n <- readLn @Int
  xs <- U.map fromIntegral . U.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
  print $ maximum [ solve i xs | i <- [1..n-1] ]
