Solved problem D from the NCPC 2008, "Dinner"