
You are simulating a round-robin dispatcher across m servers labeled 0..m-1.
i
has an initial remaining capacity
cap[i]
(a non-negative integer).
down[i]
.
k
has size
pkg[k]
(a positive integer).
Dispatch rule (round-robin with skipping and wrap-around):
p
that indicates where scanning starts. Initially
p = 0
.
k
, starting from server
p
, scan servers
p, p+1, ..., m-1, 0, 1, ...
(wrapping around) to find the
first
server
j
such that:
down[j] == false
, and
cap[j] >= pkg[k]
.
j
exists, assign the package to
j
, decrement
cap[j] -= pkg[k]
, and increment
handled[j]
by 1. Then set
p = (j + 1) mod m
for the next package.
p
does not change.
Return the highest-index server that handled the maximum number of packages. If all servers handled 0 packages, return -1.
Input: m, arrays cap[0..m-1], down[0..m-1], and pkg[0..n-1].
Output: an integer server index.
Include typical edge cases in your reasoning (wrap-around behavior, all servers down, large package sizes, ties).