N = gets.to_i A = gets.split.map &:to_i width = (A.sum**0.5).floor*2-1 p A.shift(width).map.with_index{|n, i| height = [width - i, i+1].min (height - n).abs }.sum