def eratosthenes(n : Int32) res = [true] * (n + 1) res[0] = res[1] = false 2.to(n) do |i| next if !res[i] || i.to_i64 ** 2 > n (i**2).step(to: n, by: i) do |j| res[j] = false end end res end def primes(n : Int32) flag = eratosthenes(n) (2..n).select { |i| flag[i] } end m = read_line.to_i n = read_line.to_i c = read_line.split.map &.to_i dp = [-1] * (m + 1) dp[m] = 0 m.to(0) do |i| next if dp[i] == -1 c.each do |cost| dp[i - cost] = {dp[i - cost], dp[i] + 1}.max if i - cost >= 0 end end ans = dp.max prime = eratosthenes(m) m.to(0) do |i| ans += dp[i] if prime[i] && dp[i] != -1 end puts ans