gets
ais = read_line.split.map(&.to_i64)
vals = Deque(Tuple(Int64, Int64)).new(ais.map_with_index {|a, i| {i.to_i64, a}})

ans = 0_i64
(vals.size * 2).times do
  vals.size.times do |i|
    i2 = (i + 1) % vals.size
    ai, aa = vals[i]
    bi, ba = vals[i2]
    current = (ai - i).abs * aa + (bi - i2).abs * ba
    test = (ai - i2).abs * aa + (bi - i).abs * ba
    if test > current
      vals[i], vals[i2] = vals[i2], vals[i]
      ans += test - current
    end
  end
end
ans1 = ans
nvals = vals.dup
loop do
  nvals.unshift(nvals.pop)
  tmp = 0_i64
  nvals.each_with_index do |(ai, aa), i|
    tmp += (ai - i).abs * aa
  end
  if tmp <= ans1
    break
  end
  ans1 = tmp
end
ans2 = ans
nvals = vals.dup
loop do
  nvals.push(nvals.shift)
  tmp = 0_i64
  nvals.each_with_index do |(ai, aa), i|
    tmp += (ai - i).abs * aa
  end
  if tmp <= ans2
    break
  end
  ans2 = tmp
end
p [ans, ans1, ans2].max
