Carry the minimum
transcript
A cart adds items and undoes them. After each action, find the cheapest. A scan would cost too much. Each answer must come in one step.
Each stack entry stores the cheapest price up to that item. The top entry is always the answer. An undo pops one entry. The entry below was true for the smaller cart.
Add three. The entry is three. Add five. The entry is the smaller of five and three. Add four. The entry is three again. The top entry now says three, as it should.
The slip is to push the raw price. After three and five, the top says five. Push the smaller of the new price and the entry below. A stress test on tiny carts finds it at once.
An undo on an empty cart does nothing. An empty cart answers minus one. Test both before you submit. Each edge is one line of code.
One step per action. No scan at all. Full breakdown in the lecture.