# Here your code ! INF = 10 ** 7 c,n = 2.times.map { gets.to_i } a = gets.split.map(&:to_i).sort dp = Array.new(c+1,INF) dp[0] = 0 (0..c).each do |i| if dp[i] < INF n.times do |j| if i + a[j] <= c dp[i + a[j]] = [dp[i + a[j]], dp[i] + 1].min end end end end dp[c] = -1 if dp[c] > c puts dp[c]