working solution for Stacks of Flapjacks problem