import Data.Maybe
import Data.List
import Data.List.Split
import qualified Data.IntSet as IS
import qualified Data.ByteString.Char8 as B
main = mapM_ (print . solve) . chunksOf 2 . map (map readInt . B.words) . B.lines =<< B.getContents
readInt = fst . fromJust . B.readInt
solve [[n],a] = fst . foldl f (0,IS.fromList [-1,n]) . sort $ zip a [0..] where
f (t,s) (v,i) = (t+v*l*r, IS.insert i s) where
  [l,r] = map (\f->abs $ (i-) $ fromJust $ f i s) [IS.lookupLT,IS.lookupGT]