eval"T,N,*B="+`dd`.split*?, B[N..-1].map{|v|9.times{|j|$*<t,i{d[[t,i]]||=it-=B[i/9])?0:$*[i]+f[t,i+1]].max: 0} p f[T,0]