Problem_36 solution with stacks