# frozen_string_literal: true class Integer def pyramid w = 2 * self - 1 c = self - 1 a = Array.new(w) a[0] = 1 (1..c).each do |i| a[i] = a[i - 1] + 1 end (c + 1..w - 1).each do |i| a[i] = a[i - 1] - 1 end a end end class Array def resize(other) some = self other = other if some.size < other.size some.fill(some.size, other.size - some.size) { 0 } else other.fill(other.size, some.size - other.size) { 0 } end [some, other] end end def solve s = A.sum n = Integer.sqrt(s) pyramid = n.pyramid original, pyramid = A.resize(pyramid) original.zip(pyramid).map { |o, p| o - p }.filter { _1 > 0 }.sum end N = gets.to_i A = gets.split.map(&:to_i) puts solve