DP30, CODE, watch out for Math.min(s, M)