n = gets.chomp.to_i xs = n.times.map { gets.chomp.to_i } @memo = Array.new(n) def calc_sum(i, xs) return @memo[i] if @memo[i] return @memo[i] = Math.sqrt(xs[i]) if i == 0 @memo[i] = calc_sum(i-1, xs) + Math.sqrt(xs[i]) end (0...n).each do |i| puts calc_sum(i, xs) end