Solution to second problem in hacker rank, ChocolateFeast