N,*A = $<.read.split.map(&:to_i)
うれしさ = lambda{|x,y|
	A[x] * (x-y).abs
}

B = [*0...N]
うれしさアップ？ = lambda{|i,j|
	うれしさ[B[i],i]+うれしさ[B[j],j] < うれしさ[B[i],j]+うれしさ[B[j],i]
}
begin
	k = 0
	0.upto(N-2){|i|
		(i+1).upto(N-1){|j|
			(B[i],B[j], k = B[j],B[i], k+1) if うれしさアップ？[i,j]
		}
	}
end while 0 < k
p B.each_with_index.map(&うれしさ).sum
