Solution for candies problem