Given an integer array cap[0..n-1] for warehouses 0..n-1 and a sequence of events, simulate package processing. Events:
(
-
"PACKAGE": process one package by assigning it to the first warehouse at or after the current pointer (initially
-
that is not permanently closed and has remaining capacity in the current cycle; when a warehouse hits its capacity in the current cycle, it is considered full and skipped until the next cycle; advance the pointer to the next eligible warehouse after each package.
(
-
"CLOSURE x": permanently close warehouse x so it never processes packages again. Cycle reset: when warehouse n-1 becomes full and there is no eligible warehouse afterward in the current cycle, the next PACKAGE begins a new cycle where remaining capacities of all non-closed warehouses reset to cap[i]; closures persist. After processing all events, return the id of the warehouse that processed the most packages; break ties by returning the largest id. Design data structures and an algorithm with low per-event complexity and describe edge cases (e.g., if all warehouses are closed).