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