class Calc0027 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @vs = args.shift.map(&:to_i) end def run min = nil (1..30).each do |a| (a..30).each do |b| (b..30).each do |c| r = calc_min([a, b, c]) min = r if !min || r && min > r end end end min end def calc_min(bs) memo = Array.new(31) memo[0] = 0 bs.each do |b| (1..30).each do |m| if m >= b r = [memo[m], (memo[m - b] ? memo[m - b] + 1 : nil)].compact memo[m] = r.min unless r.empty? end end end r = @vs.map { |v| memo[v] } if r.all? r.inject(:+) else nil end end end puts Calc0027.new(STDIN.readlines).run if __FILE__ == $0