# AtCoder Regular Contest 100

URL: https://atcoder.jp/contests/arc100 (opens new window)

# C

n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)

# abs(a_i - (b + i)) = abs((a_i - i) - b)
# 先に (a_i - i) を計算
(0..n-1).each do |i|
  arr[i] -= i + 1
end

# 真ん中の数字が最適っぽい(予想)
arr.sort_by!{|a| a}
b = arr[n / 2]

# 残った部分を計算
result = 0
arr.each do |a|
  result += (a - b).abs
end

puts result
Last Updated: 2020/08/29 17:41