import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    n <- getInt
    as' <- getIntList
    let as = reverse . sort $ zip as' [1..]
    print $ solve [1..n] [n..1] as

solve _ _ [] = 0
solve (s:ss) (l:ls) ((a,i) : ais)
    | si < li = li * a + solve (s:ss) ls ais
    | si >= li = si * a + solve ss (l:ls) ais
--    | si == li = max (si * a + solve ss (l:ls) ais) (li * a + solve (s:ss) ls ais)  
  where si = abs (s-i)
        li = abs (l-i)