Problem 895 · Hard
Keep one stack per frequency level; pop from the highest level, which preserves recency among ties.
Open the interactive visualisation