Solution to the Dwarves and Coins problem