# frozen_string_literal: true # This file is expanded by nanacl. main = -> do # ================================================================= # frozen_string_literal: true # require "nanacl/array_sum" # (expanded: L25) in_n, in_m = gets.chomp.split.map(&:to_i) in_a = gets.chomp.split.map(&:to_i) in_b = gets.chomp.split.map(&:to_i) positives = in_a.zip(in_b).map { |a, b| [a - b, 0].max} max = 0 (in_n - in_m + 1).times do |i| max = [ max, positives.sum_in_range((i)..(i+in_m-1)) ].max end puts max end # -------------------------------------------------------------------------- # === dependencies ------------------------------------------------------------- # == nanacl/array_sum from main ------------------------------------------------ # frozen_string_literal: true class Array def sum_in_range(range) unless @sums @sums = [0] sum = 0 each { |v| @sums << (sum += v) } end range_begin = range.begin || 0 if range.end.nil? @sums[length] - @sums[range_begin] elsif range.exclude_end? @sums[range.end] - @sums[range_begin] else @sums[range.end + 1] - @sums[range_begin] end end end # ============================================================================== main.call