eval"N,D,T,*X="+`dd`.split*?, h={} p X.sort.map{|l|t=h[k=l%D];h[k]=-2*T-l/=D;2*T+[(t||9e9)+l,1].min}.sum