Traverse from the back of map so as to take bigger elements first
for ( let u of [ ... count . keys ( ) ] . reverse ( ) ) { let j = u ; let x = j * count . get ( j ) ; if ( d <= x ) { k += Math . floor ( ( d + j - 1 ) / j ) ; break ; } k += count . get ( j ) ; d -= x ; } }