require'prime' m,n,*c=$<.read.split.map &:to_i d=[*-m..k=0] m.downto(r=0){|i|c.map{|j|d[i]<0||j>i||d[i-j]=[d[i-j],d[i]+1].max}} p d.each{|e|k.prime?&&e>0&&r+=e;k+=1}.max+r