# Here your code ! # 1 + 3 + 5 + ... = N * (1 + (2N-1))/2 = N * N N = gets.to_i A = gets.split.map(&:to_i) sum = A.inject(&:+) level = Math.sqrt(sum).floor #level = [level,(N+1)/2].min mintotal = 1000000 #puts "sum=#{sum}, level=#{level}" # (0+(level-1)...N-(level-1)).each{|i| (level-1..level-1).each{|i| total = 0 #puts "i=#{i}" # (-(level-1)..(level-1)).each{|j| (0...N).each{|j| h = 0 if j>=0 && j< N then h = A[j] end base = 0 if level >= (i-j).abs base = level - (i-j).abs end if h > base then diff = (h - base).abs total += diff end #puts "i=#{i}, j=#{j}, h=#{h}, base=#{base}, diff=#{diff}, total=#{total}" } mintotal = [mintotal, total].min } puts mintotal