Heap boundaries and repeated choices
A heap keeps one extreme value available without sorting every stored item. A min heap exposes its smallest element; a max heap exposes its largest. The rest of the internal array is only partially ordered, so iterate or sort explicitly when the output requires a complete ranking.
Choose the root to match the value you need to remove or inspect. A bounded heap of the largest k parcels should expose the smallest retained parcel, while a median tracker needs the largest lower-half value and the smallest upper-half value. You will also merge sorted feeds and combine small bundles cheaply. Trace ties and empty inputs before adding a loop. In JavaScript and TypeScript, the references include a small binary heap so the same reasoning works without a priority-queue dependency.
weights = [8, 2, 8, 6, 3], k = 3
retained multiset after each arrival:
[8]
[2, 8]
[2, 8, 8]
[6, 8, 8] # evict 2
[6, 8, 8] # evict 3
output: [8, 8, 6]
The retained group never needs more than three weights. When 6 arrives, push it first; the group temporarily holds four values, and removing the minimum discards 2. When 3 arrives, it is itself the minimum and leaves immediately. This preserves the three largest occurrences after every input prefix. The repeated 8 values represent different parcels and both remain eligible. The heap root tells you which candidate to discard, but its array order isn't the requested answer order. Sort the retained values descending only once, after processing the whole input.