We are given n boxes, each containing a set of presents (types 1..m). All presents are distinct within a box. The total number of presents across all boxes is at most 500k. We are allowed to move a present from one box to another. After each move, the condition of distinctness must hold; i.e., we cannot move a present into a box that already contains that kind. The goal: find the minimum number of moves needed to achieve a state where the sizes of boxes differ by at most 1 (i.e., the maximum minus minimum ≤ 1). Among all such final states, we want one that minimizes the number of moves. Also we want to output a sequence of moves achieving that minimal moves.
We need to compute the minimum possible difference in box sizes after moving some presents? Actually we want to achieve difference ≤ 1, so we aim for a state where box sizes are as balanced as possible (like floor(total/n) or ceil). But maybe it's possible to achieve exactly that balanced distribution? Since we can only move presents, not create/destroy, total number of presents T is fixed. The ideal balanced sizes: each box should have either floor(T/n) or ceil(T/n) presents. Let lo = floor(T/n), hi = ceil(T/n). Since T/n may not be integer, the number of boxes that get hi presents is T mod n (if T mod n = r, then r boxes have hi = lo+1, and n-r have lo). If lo = hi, then all have same size.
Our goal is to reach such a balanced distribution (since that's the minimal possible difference; indeed any distribution must have max-min ≥ something, but the minimal possible max-min is at most 1 if T mod n not zero? Actually if T mod n = 0, all boxes can have exactly T/n, diff 0; else diff at least 1. So the theoretical minimum difference is 0 if T divisible by n, else 1. So we aim for diff ≤ 1, which is achievable? Possibly always? Not necessarily: constraints of distinctness might prevent certain assignments? Since all presents are distinct within a box, but can we rearrange arbitrarily? The only constraint is that a present cannot appear twice in the same box. Since each present is a unique kind; there's exactly one of each kind? Actually "presents" are kinds. Input: each present kind is an integer 1..m. The total number of presents is sum s_i, and they are distinct across all boxes? Not necessarily. Wait: "all kinds of presents in one box are distinct." But different boxes can contain the same kind? The problem says "Santa has prepared boxes with presents for n kids, one box for each kid. There are m kinds of presents: balloons, sweets, chocolate bars, toy cars... A child would be disappointed to receive two presents of the same kind, so all kinds of presents in one box are distinct." Then: "Having packed all the presents, Santa realized that different boxes can contain different number of presents." That suggests each box initially has some set of present kinds, and across boxes there could be overlaps? It doesn't say each kind appears only once total. In fact, the description: m kinds of presents. It doesn't say there's only one copy of each kind. Usually in such problems, there may be multiple copies of each kind, but the distinctness constraint only applies within a box. So two different boxes could both contain a present of kind 1. So each present is not unique globally; there could be many balloons. But the problem statement: "Denote presents with integers from 1 to m." And each line gives s_i distinct integers between 1 and m. So it's a multiset: each box contains a set of kinds (no duplicates). But across boxes, the same kind can appear multiple times. That is typical: there are m types, unlimited supply? Actually the total number of presents across boxes is given, but there could be multiple of same kind. They didn't restrict total per kind. So we have to treat each present individually, with a kind, and duplicates across boxes allowed.
Thus the only constraint: In each box, you cannot have two presents of the same kind.
Goal: by moving presents (i.e., taking a present from one box and placing into another), we want to minimize the max-min difference, and among those sequences achieving the minimal diff, minimize moves.
Since duplicates are allowed, we can think of each box as a multiset of kinds, with no duplicate kinds. The moves can be performed arbitrarily as long as the destination doesn't already contain that kind.
We need to output a minimal-length sequence of moves to achieve a balanced distribution (diff ≤ 1). Since duplicates exist, we can probably always achieve such balanced distribution by moving presents from boxes that have excess to those that have deficiency, while respecting distinctness.
But there might be obstacles: if a box needs a certain kind but that kind is already present in the box, we cannot move that kind there; we need to move a different kind. Also we must avoid creating duplicates in the process. So the challenge is to find a sequence of moves of minimal length to achieve the target sizes.
Interpretation: We need to find a target assignment of each present to a box such that each box gets either lo or hi number of presents, and each box's presents have distinct kinds. The initial assignment is given. Moves correspond to reassigning a present from its initial box to a new box. The cost is number of reassignments (i.e., number of presents whose final box differs from initial). We want to minimize the number of changes needed to achieve a feasible balanced assignment.
Since moves are done sequentially, but we can plan final assignment and then produce moves: we can move each present that needs to be relocated directly from its original box to its final box, as long as intermediate states are okay? But the sequence must maintain distinctness after each move. If we move a present directly to its target box, it might cause a temporary duplicate if the target box already has that kind before the move? But if we move after possibly moving out others, we must ensure the destination doesn't contain that kind at the moment of move. In the final assignment, the target box does not have that kind (since final assignment has distinct kinds). But during process, before we move some presents out of that box, it might contain that kind. However we can plan the order: we can first move out any present that would conflict, but that present might also need to be moved somewhere else. This could be complicated.
But perhaps there is a simpler approach: Because presents are not globally unique, we can treat each present as an item with a type. We need to reassign items to boxes to achieve balanced sizes, with the constraint that each box's types are all distinct. Equivalent to: we need to decide, for each box, a set of types (size lo or hi) such that the total number of items of each type assigned to boxes doesn't exceed something? Actually each item is a specific occurrence of a type. There may be multiple items of same type. In the final assignment, within each box, types must be distinct. So each box can contain at most one item of each type. So across all boxes, the total number of items of a given type cannot exceed n (since each box can hold at most one). But that's automatically satisfied if initial assignment satisfies? Not necessarily; initial assignment could have a type appearing more than n times? Actually total number of boxes is n, so each type can appear at most n times overall if all boxes had it, but there could be more than n? Since m can be up to 100k, total items up to 500k, n up to 100k, so theoretically a type could appear up to 500k times, which could exceed n, but that would make it impossible to have all boxes distinct because if there are > n items of a given type, by pigeonhole, after assigning each to a box, at least one box would have two of that type, violating distinctness. But the initial assignment is given and it is valid (all boxes have distinct kinds within themselves). So initially, each box contains at most one of each type, but across boxes, there may be multiple copies of the same type. Could there be more than n copies of a type? Possibly, because if there are 2 boxes and 3 copies of type 1, one box would have two copies? But initially each box has distinct kinds, so a single box cannot have two copies of type 1. However if there are 3 copies, they could be distributed as: box1 has type1, box2 has type1, and a third copy? But there are only 2 boxes. So to have 3 copies, one box must have two copies, which is not allowed initially. So initial assignment ensures that each type appears at most once per box, but across boxes it can appear multiple times, but the maximum number of copies of a type cannot exceed n because if there were n+1 copies, by pigeonhole at least one box would have at least two copies (since there are only n boxes), contradicting that each box's set is distinct. However is that necessarily true? The pigeonhole principle: If you have n boxes and you assign n+1 items each with a type, but they are all the same type, then you would need to put at least two of them into the same box, which would cause a duplicate. So indeed the initial valid assignment implies that for each type, the number of occurrences across all boxes ≤ n. So that's a global constraint: each type frequency ≤ n. Since total items ≤ 500k, n ≤ 100k, it's possible for a type to appear many times but not exceed n.
Thus in any valid assignment, the frequency per type cannot exceed n. Our target assignment must also satisfy this (since it's a physical constraint). But as long as we don't create duplicates within a box, that holds automatically. So the final assignment must also have each type appearing at most once per box.
Now, we are allowed to move items. The goal is to minimize the number of moves.
We can view this as a minimum cost flow or assignment problem: we have initial boxes for each item; we want to assign each item to a final box (either same or different) such that:
- Each box i has final size s_i' ∈ {lo, hi} and total sum = T.
- For each box, the set of types assigned to it are distinct.
- The number of items that change box (i.e., where final box ≠ initial box) is minimized.
Because we can move items one by one directly from initial to final box, as long as we order moves appropriately to avoid intermediate duplicates. But is it always possible to execute such a reassignment without violating distinctness during moves? Possibly we need to consider ordering constraints. But we might be able to produce a sequence that respects distinctness by moving items from overfull boxes to underfull boxes, while ensuring that at each step we don't introduce duplicates. This is reminiscent of "balancing" with distinctness constraints.
Given the problem's constraints (n,m up to 1e5, total presents up to 5e5), we need an algorithm that runs efficiently, likely using greedy or flows.
Let's think: We need to achieve final sizes. Let initial sizes s_i. For each box, the target size is either lo or hi. Let's call boxes that need to lose presents: if s_i > target size, they need to give away some items. Boxes that need to gain presents: if s_i < target, they need to receive items. The number of items to move is at least the total excess = sum_{i: s_i > target} (s_i - target). That's the minimum number of presents that must be relocated. But is it possible to achieve with exactly that many moves? Possibly, if we can assign each excess present to a deficient box without causing a conflict (i.e., the present's type not already in the destination box) and also that we don't need to move other presents to resolve conflicts.
But there may be situations where a direct assignment is impossible due to conflicts. For example, consider two boxes: box1 has types {1,2}, box2 has {1}. Target sizes: both should have 1 present. So lo=1, hi=1? Actually T=3, n=2, average=1.5, so lo=1, hi=2? Wait T=3, n=2, floor=1, ceil=2. So one box should have 2, the other 1. But initial sizes: box1 has 2, box2 has 1. So already balanced? Actually sizes are 2 and 1, diff=1, so already balanced. But suppose we need to change: Not necessary. Another example: n=2, T=2, both should have 1. Initial: box1 {1}, box2 {1}. Both have same type. Target sizes: each 1, but box2 already has type 1. To have both boxes distinct? Actually no requirement that boxes have distinct types across boxes; only within a box they must be distinct. So having both boxes containing type 1 is allowed because each box individually has distinct types (just one). So no conflict. So that's fine.
But consider: n=2, T=3, lo=1, hi=2. Initial: box1 {1,2}, box2 {1}. So sizes 2 and 1, already balanced? Actually diff=1, so already balanced. Suppose we wanted to balance diff=0? Not possible. So no moves.
Consider conflict scenario: We need to move a present from an excess box to a deficient box, but the deficient box already contains that kind. So we cannot move that specific present there. We might need to move a different present from the excess box, or perhaps move something else first to free up the kind in the deficient box? But the deficient box might have that kind and it's staying (since it's part of its target set). If the target set for the deficient box includes that kind, then we cannot bring another same kind. So we must ensure that the presents we move to a box have kinds not already present in that box in the final assignment. Since the final assignment must have distinct kinds, if the box already has some kinds (some of which may remain), we cannot add a present of a kind that it already contains. So the set of presents assigned to a box in the final state must be a set (no duplicates). So for each box, the final set of kinds is a subset of the kinds assigned to it (including some from original and some moved in). So the conflict arises when a present's kind already exists in the target box (either originally or moved from elsewhere). However, we could also move out the existing present of that kind from the target box to make room, but that would change the counts.
Thus the problem becomes: we need to select for each box which presents to keep (stay in that box) and which to send elsewhere, and which to receive, such that the final sizes are balanced, and the number of items that change boxes is minimized.
This is essentially a bipartite matching between excess presents and deficient slots, with the constraint that a present can be assigned to a box only if its kind is not already present in that box's final set. But the final set includes both the kept items and the received items. This is like we need to decide a set of items to remain in each box, and then fill empty slots with items from other boxes.
Alternatively, we can think of each box as having a capacity (target size). Initially it contains some items. We may remove some items to meet capacity, and we may add items to meet capacity. The total number of removals = total number of additions = number of moves (since each move is an item moved from one box to another; an item that is moved is counted as one move, and it both leaves its source and enters its destination). So the total number of moves is at least the number of items that need to change box, which is the sum of excess over target (or deficiency). However, if we move an item from box A to box B, it reduces excess of A and reduces deficiency of B. So each move simultaneously addresses one excess and one deficiency. So we want a pairing between excess items and deficiency slots.
But we must also ensure that after reassigning, no box has duplicate kinds. This imposes constraints on which items can go to which boxes.
Observation: Since each box can have at most one of each kind, it's safe to keep an item in its original box as long as we don't need to remove it (i.e., it's part of the box's final set). For a deficient box, it needs to acquire some items. It can accept an item of kind k only if it does not currently contain k (and also won't receive another item of kind k). But it's free to accept any kind that it doesn't have. For an excess box, it needs to give away some items; it can give away any item, but if it gives away an item of kind k, then that kind is removed from the box, potentially freeing that kind for later if we need to move something in? But that's irrelevant.
We want to minimize moves, so we prefer to keep as many items as possible in their original box. That means for each box, we want to select a subset of its current items to keep, of size equal to its target capacity (or possibly less if we need to remove more? Actually if a box is excess, we need to remove some items, so we keep exactly target number; if it's deficient, we keep all current items (since we need to add more) unless some of its current items conflict with incoming items? Actually we might consider moving out some items from a deficient box to make room for others? But that would increase moves, so not beneficial for minimizing moves. Because if we move out an item from a deficient box, we would have to also move in another item to keep the size, resulting in at least 2 moves for that box (unless the item moved out is exactly the one moved in later, which doesn't make sense). So to minimize total moves, we should keep all items in boxes that are not excess? More precisely, for a box that is deficient (its target size > current size), we will need to add items; there is no need to remove any, because removing would only increase the number we need to add, which would require additional moves, so it's suboptimal. For a box that is excess (target size < current size), we need to remove some items; we will keep exactly target number of items, and remove the rest. For boxes that are exactly at target, we keep all items. So the optimal strategy to minimize moves is: keep as many as possible, i.e., for each box, keep all items if box is not excess; if excess, keep a subset of size target. And then, we need to assign the removed items (from excess boxes) to deficient boxes to fill their missing slots, ensuring that each assigned item's kind is not already present in the destination box (in its final set, which includes the kept items). Since we keep all items in deficient boxes, the kept set is the original set. So a deficient box i has a set of kinds K_i (its original presents). It needs to receive some number of additional presents (d_i = target_i - s_i). Each received present must have a kind not in K_i (and also not duplicate with other received presents, but that's automatically if we send distinct kinds, but if we send two of same kind to same box that would be duplicate, but we wouldn't do that). However, the removed items come from excess boxes; each such item has a kind. We must assign each removed item to a deficient box where that kind is not already present. Also, we might have to consider that a removed item could be assigned to a box that originally had that kind but we are not keeping that original item? But we are keeping all items in deficient boxes, so the original kind is present. So it cannot receive the same kind.
But what about boxes that are exactly at target? They keep all items, and neither send nor receive. So they are neutral.
Thus the problem reduces to: We have a set of "excess items" (items to be removed from excess boxes) and a set of "deficiency slots" in deficient boxes. We need to match each excess item to a deficiency slot, such that for each deficient box i, the kinds assigned to it are all distinct and not in K_i. This is essentially a bipartite matching between items and slots with constraints.
Furthermore, we might also consider the possibility that it's beneficial to move some items from a box that is not excess to a deficient box, while also moving other items out? That would increase moves beyond the minimum, so not optimal. So minimal moves will be exactly the total excess (which equals total deficiency). However, there might be cases where it's impossible to assign the excess items to deficient boxes without violating distinctness constraints, even though total excess equals total deficiency. In such cases, we might need to perform additional moves, like moving out some items from deficient boxes to free up kinds, or moving items between excess boxes, etc., to resolve conflicts. That would increase the number of moves beyond the minimum.
Thus the minimal number of moves might be greater than total excess if constraints prevent a direct matching.
So we need to compute the minimum number of moves needed, and produce a sequence achieving it.
This looks like a flow problem with constraints: each present (item) has a kind. Each deficient box has a capacity (number of slots) and a set of forbidden kinds (the kinds it already has). We need to assign items to boxes respecting forbidden sets. Additionally, items originally belong to some box; we can choose which items to remove from excess boxes; we can decide which specific items to remove, not necessarily all excess items must be removed? Actually from an excess box we need to remove exactly (s_i - target_i) items. We can choose which items to remove. That choice can affect feasibility. So we can select a subset of items to remove from each excess box (size = excess) to maximize the ability to assign them to deficient boxes.
Thus we have a combinatorial optimization: Choose for each excess box a set of items to remove (of size exactly its excess), and then assign those removed items to deficient boxes (each deficient box gets exactly its deficiency) such that each assigned item's kind is not in the destination box's original set. Also, we must ensure that within a deficient box, assigned kinds are distinct (but since we are assigning distinct items, they could have same kind if we assign two items of same kind to same box? That would violate distinctness, so we must ensure that no two items assigned to the same box have the same kind. However, note that the items we assign come from different boxes, but they could be of the same kind. So we need to avoid that as well. So for each deficient box, the set of assigned kinds must be a set (no duplicates). Also, for excess boxes, after removal, the kept items will remain; they are distinct within the box (by initial condition) so okay.
Thus the problem is: We have n boxes. For each box i, we have its original set S_i (size s_i). Let target sizes t_i ∈ {lo, hi} such that sum t_i = T. Let EX = { i | s_i > t_i } (excess boxes) and DF = { i | s_i < t_i } (deficient boxes). For excess box i, we need to choose exactly e_i = s_i - t_i items to remove. For deficient box i, we need to choose exactly d_i = t_i - s_i items to add, each of a kind not in S_i, and all added kinds distinct within the box.
We can also consider neutral boxes; they are not involved.
Goal: Find a selection of items to move (size total excess) and an assignment to deficient boxes satisfying constraints, such that the number of moves is minimized. Since each moved item corresponds to one move, and we are counting moves exactly as the number of items we move, the total moves = total excess (if we can assign all removed items directly to deficient boxes). If not possible, we might need to move some items to other boxes (like via intermediate or swapping) resulting in more moves. But perhaps we can always achieve total excess moves by appropriate selection? Not necessarily.
Consider an example: n=2, m=2. Box1: {1,2} (s1=2), Box2: {} (s2=0). T=2, lo=1, hi=1? Actually n=2, T=2 => average=1, so both boxes target 1. So box1 excess 1, box2 deficiency 1. Box2 original set empty, so it can accept any kind. Box1 has items {1,2}. We can remove either 1 or 2 and give to box2. That works, moves=1.
Now conflict: Suppose Box1: {1,2} (excess 1), Box2: {1} (deficiency? Let's compute T=3? Actually n=2, T=3 => average 1.5, so targets: one box 2, one box 1. Suppose s1=2, s2=1 -> already balanced? diff=1. So no moves. Not conflict.
Let's create conflict: n=2, T=3. Box1: {1,2} (size2), Box2: {2} (size1). So already balanced? Actually sizes 2 and 1, diff=1, so already balanced. But suppose we try to achieve target sizes 2 and 1 (same). No moves.
We need a case where box2 is deficient but already contains all kinds that are available from excess boxes? Let's try 3 boxes: n=3, T= something. Let lo, hi determined.
Suppose n=3, m=2. Box1: {1,2} (size2), Box2: {1} (size1), Box3: {2} (size1). T=4. Average=4/3 ~1.333, so lo=1, hi=2. So we need two boxes of size 2 and one of size 1? Actually sum t_i =4, with each t_i ∈ {1,2}. So possibilities: two boxes with 2, one with 1; or one with 2, two with 1? 2+1+1=4 works. So we need total size 4. Which distribution minimizes moves? We'll compute.
Given initial sizes: s1=2, s2=1, s3=1. So total 4. Already total 4. But we need each box to be either 1 or 2, and sum 4. The current sizes already satisfy? s1=2, s2=1, s3=1 sum=4, so already balanced? diff=1? Actually max=2, min=1 diff=1, so it's already within 1. So no moves required.
To cause conflict, we need a deficient box that lacks capacity to accept any kind from excess boxes because its current set blocks all possible kinds from excess. For that to happen, the deficient box must already contain all kinds that appear among the excess items. However, note that excess items come from excess boxes; they may include many kinds. If a deficient box contains a kind, it cannot accept that kind. So if the deficient box's set covers all kinds that are available among excess items, then no direct assignment possible. But we could select different excess items: from an excess box we can choose which items to remove. So maybe we can choose a set of items to remove such that at least one kind is not in the deficient box. If the excess boxes collectively have some kind not in the deficient box, we can choose that item to give. So the only issue is if for some deficient box, every kind present in any excess box (i.e., the union of kinds in excess boxes) is contained in its own set. But that's unlikely unless the deficient box already contains all kinds that appear in excess boxes. But since there may be many kinds, and m large, maybe it's possible.
But there is also the distinctness constraint among added items to a single deficient box: we cannot assign two items of the same kind to the same box. So if a deficient box needs multiple items, we must assign distinct kinds not in its set. So we need enough distinct kinds among excess items that are not in its set.
Thus the problem becomes a kind of flow with item types and selection.
We can treat each present individually, but there may be many presents of the same kind. Since the only property that matters for assignment is its kind and its source box (for counting moves? Actually moving an item from its source to destination is a move; we don't care which specific item of a kind, as long as we move one. But items are distinct even if same kind, because they are in different boxes. When we move an item, we know its source box and kind. So we can think of each item as a specific unit with kind k and source box i. We need to decide which items to move and where.
Goal: Minimize total moves. Equivalent to maximize number of items that stay in their original box (i.e., not moved). So we want to select a subset of items to keep in each box, such that each box i keeps exactly t_i items (or for deficient, keeps all s_i items, but that's > t_i? Wait for deficient boxes, t_i > s_i, so we cannot keep exactly t_i because we have only s_i items; we need to add items, but we keep all s_i? Actually to minimize moves, we want to keep all existing items in deficient boxes (since we need more, not less). So for a deficient box, we keep all s_i items, and we need to add d_i items. So total kept in deficient box = s_i. For excess box, we keep exactly t_i items (which is less than s_i), so we need to remove e_i items. For neutral box, we keep all s_i = t_i items.
Thus the total number of items that are moved equals total excess = sum e_i. So we want to achieve that exactly, if possible.
Thus the question reduces to: Can we choose for each excess box i a subset of items to remove of size e_i, such that the multiset of removed items can be assigned to deficient boxes, each deficient box j receiving exactly d_j items, with each received item's kind not in S_j, and additionally, the items assigned to the same j have distinct kinds (since no duplicates in final box). Also note that after removal, the kept items in each box remain and must be distinct; but since they were distinct originally and we are removing some, they remain distinct.
Is there always a selection that works? Not necessarily. When might it fail? Consider: deficient box j has a set S_j. The removed items come from excess boxes. For each kind k, the number of items of kind k that we can assign to j is at most 1 if k not in S_j, else 0. But we could assign multiple items of different kinds. The main limitation is that the set of kinds we assign to j must be a subset of the complement of S_j. Also we cannot assign two items of same kind to same j.
Now, the removed items may include multiple items of same kind. We can distribute them to different boxes, as long as each box gets at most one per kind.
So the feasibility is a bipartite matching between items and (box, kind) slots? Actually we can think of each deficient box j needs d_j distinct kinds from the set of available kinds (from removed items), with the condition that each kind assigned to j is not in S_j.
We have control over which items are removed. So we can choose which specific items (by kind and source) to move.
This resembles a flow problem where we have supply of items (by kind) from excess boxes, and demand from deficient boxes for distinct kinds not in their sets.
We can treat each kind separately: For each kind k, there are some occurrences in excess boxes (i.e., items of kind k that we could potentially remove). Also, there are occurrences in deficient boxes (they already have kind k, so they cannot accept more of k). Also there may be occurrences in neutral boxes (they are not involved). For each kind k, the total number of items of kind k that we can move is at most the number of items of that kind in excess boxes (since we can choose to remove or not). But we might also decide to remove items of kind k from excess boxes even if they are needed? Actually we need to remove exactly e_i items from each excess box. We can choose any subset. So we have flexibility.
Thus the problem is: given for each box i (excess) a set of items, we need to pick exactly e_i items to remove, and then assign them to deficient boxes, respecting per-box kind constraints.
We could formulate as a max flow: create source connected to each excess box i with capacity e_i (the number of items we must remove from that box). Then from each excess box i, we have edges to "kind nodes"? But items are individual, and we have to choose which specific items to remove. However, we can aggregate by kind: For each excess box i and each kind k present in box i, we can remove up to 1 item of that kind (since there is at most one of each kind per box). Actually each box contains at most one of each kind, so there is at most one item of kind k in box i. So the decision for each box i is a binary choice for each kind present: either keep that item or remove it. Since each box i (excess) must keep exactly t_i items, we need to select which s_i - t_i items to remove among its s_i items. This is a selection of a subset of its items.
Thus from each excess box i, we can remove at most 1 item of each kind (specifically, if present). So the set of removable items is exactly the set of items in excess boxes.
Now, after selection, we have a multiset of removed items (each with a kind). Then we need to assign them to deficient boxes. For each deficient box j, it can accept at most one item of each kind k not in S_j. Also it needs exactly d_j items.
Thus we have a bipartite matching between removed items (each with a kind) and deficient boxes, with each box j having capacity d_j and each kind k can be assigned to box j at most 1 if k ∉ S_j else 0.
But we also have the ability to choose which items to remove. So this is like we need to select a set of items to remove (matching the exact numbers e_i per excess box) such that there exists a feasible assignment to deficient boxes.
This is reminiscent of a flow with lower bounds? Or we can think of it as: we need to decide an assignment of items (from excess boxes) to deficient boxes directly, where each excess box i contributes at most e_i items to the assignment, and each item can be assigned to at most one deficient box (since it's a specific item). Additionally, the items assigned from box i must be distinct (since box i has at most one per kind). But that's natural.
We want to know if there exists a selection/assignment such that all e_i and d_j are satisfied. If yes, then moves = total excess.
If not, we need to do more moves. What additional moves could resolve the impasse? For instance, we might move an item from an excess box to some other box (maybe a neutral box or another excess box) first, then later move it to a deficient box, but that would increase moves count. Or we might move an item out of a deficient box to free up a kind, then bring in another item. That would involve moving an item from deficient box elsewhere, increasing moves.
So the minimal moves might be > total excess. We need to compute the minimal number of moves.
This problem looks complex but typical for contest problem; there might be a clever observation that the answer always equals total excess, i.e., it's always possible to achieve the balanced distribution with exactly that many moves. Let's test if there is any counterexample.
Suppose n=2, m=1. Only one kind. Box1: {1,? can't have two of kind 1 because distinctness. So maximum one per box. So s_i ≤ 1. Since total presents ≤ 500k, but m=1, each box can have at most 1. So s_i ∈ {0,1}. Then T ≤ n. For diff ≤1, we need sizes to be either floor(T/n) or ceil(T/n). If T ≤ n, then lo = floor(T/n) could be 0? Actually floor(T/n) if T < n, lo=0, hi=1. So boxes can have 0 or 1 presents. Is that allowed? Yes, s_i can be 0 initially. So we can have boxes with 0 presents. The distinctness constraint is trivial.
Now consider T=2, n=2, each box has 1 present: both have kind 1. That's already balanced (both size 1, diff=0). So no moves.
What about T=2, n=3? Then lo=0, hi=1? Actually T=2, n=3, average 0.666, lo=0, hi=1. So we need two boxes with 1, one with 0. Suppose initial: Box1: {1}, Box2: {1}, Box3: {}. That's already sizes 1,1,0 → balanced. So no moves.
What about T=3, n=3: each box must have 1. Suppose initial: Box1:{1}, Box2:{1}, Box3:{1}. All have kind 1. Balanced sizes all 1, diff=0, already fine. Distinctness within boxes ok. So no issue.
Thus with m=1, any configuration already has each box size ≤1. Balancing is about numbers, not kinds. Can we always achieve with exactly total excess? For T=3, n=5: lo=0, hi=1? Actually 3/5 floor=0, ceil=1, need three boxes with 1, two with 0. Suppose initial: Box1:{1}, Box2:{1}, Box3:{1}, Box4:{}, Box5:{}. That's already balanced. If initial had more than one? impossible because each box max 1. So always balanced? Not exactly: could have Box1:{1}, Box2:{1}, Box3:{}, Box4:{}, Box5:{}. Then sizes:2 boxes have 1, three have 0. T=2? Actually if T=2, then need two with 1, three with 0, balanced. So initial could be unbalanced only if some box has 0 but we need it to have 1, and some other box has 1 but we need it to have 0. So we would need to move a present from a box with 1 to a box with 0. That is possible because the source box has kind 1, the destination box is empty, so no conflict. So moves=1, which is total excess. So it works.
Now consider m=2, try to create conflict. Suppose n=2, T=2, need each box size 1. Initial: Box1:{1}, Box2:{1}. Already balanced.
n=2, T=2, need each box size 1. Initial: Box1:{1,2}? can't because size 2 >1, so s1=2 not allowed? Actually if n=2, T could be 3? Let's try to force a conflict: We need one box with 2, one with 1 for T=3. Suppose Box1:{1,2} (size2), Box2:{1} (size1). Balanced.
To have conflict, we need a deficient box that cannot accept any of the excess items because all excess items' kinds are already present in that box. For Box2 deficient? Actually if Box2 is the one needing an extra item, but Box2 already has kind 1, and the excess items from Box1 are {1,2} but Box1 is excess? Let's compute target: Suppose n=2, T=4? But each box max per kind 1, max size m, but m=2, so max size per box is 2. So total max is 4. Let T=4. Then average=2, so both boxes need size 2. So target sizes both 2. Initial: Box1:{1,2} (size2), Box2:{1} (size1). Then Box2 is deficient by 1, Box1 is exact? Actually Box1 has 2, target 2, so exact. So no excess. But we need to increase Box2 by 1. But Box1 has no excess. To give to Box2, we would need to move an item from Box1, but then Box1 would become deficient and we'd need to get another item somewhere else. That would be two moves, but perhaps we could move an item from Box2 to Box1? That seems weird.
Better scenario: We need both boxes to have size 2. Initially Box1:{1,2}, Box2:{1}. To get both to 2, we need to add one item to Box2. There is no spare item; total is 3, need 4. So we can't because we don't have enough presents. So T must equal sum of s_i. So T is fixed.
Thus conflict arises only when we need to move items from excess boxes to deficient boxes. So we need a case where for some deficient box, every kind present in any excess box is also present in that deficient box. Since deficient boxes already have some set of kinds, and excess boxes have some kinds. If the deficient box's set covers all kinds that appear in excess boxes, then direct assignment impossible. But is that possible given that the excess boxes also have many kinds? Let's try to construct a small case.
Let n=3, m=2. Boxes:
Box1 (excess): {1,2} (size2)
Box2 (deficient): {1} (size1)
Box3 (deficient): {2} (size1)
Total T=4. Need target sizes? Let's compute lo,hi. n=3, T=4 => average=1.333, lo=1, hi=2. So we need two boxes of size 2 and one of size 1, or one of size 2 and two of size 1? Sum must be 4, so possibilities: 2+1+1=4. So exactly one box of size 2, two of size 1. But initial sizes: Box1=2, Box2=1, Box3=1 => already balanced? Because we have one box size2 and two size1 -> diff=1, so already balanced. So no moves.
We need a case where the target distribution differs from the initial. For imbalance, we need some boxes to have size lo and some hi, and initial sizes not matching. For example, n=3, T=5 => average=1.666, lo=1, hi=2. So we need distribution: one box of size2 and two boxes of size1? Actually 2+2+1=5? 2+2+1=5, that's two size2, one size1 -> sum 5. So two boxes with 2, one with 1. Or 2+1+2 same. So two boxes need 2, one needs 1.
Now initial sizes: suppose Box1: {1,2} (size2), Box2: {1} (size1), Box3: {2} (size1). That's sizes:2,1,1, sum=4, not 5. Need total 5, so we need more presents. Let's add a present to some box. To create conflict, we want Box1 excess? Actually Box1 size2 target maybe 2 (if Box1 is one of the boxes that should be 2). Box2 and Box3 may need to be 2 as well? Let's decide target: Suppose we want two boxes with size2, one with size1. Which boxes get size2? We can assign. To create a need for moves, we could set initial sizes that don't match target: e.g., Box1 size2 (maybe target2, so fine), Box2 size1 (target2, deficient), Box3 size1 (target1, exact). Then Box2 needs +1, Box1 maybe exact, so no excess. So we need an excess box: Box1 has exactly 2, Box3 exactly 1, Box2 needs 1, but no excess. So total moves? We need to create an extra item out of nowhere? Not possible. So we need a box with size > target.
So let's construct: n=3, T=5. Choose target: Box1 target2, Box2 target2, Box3 target1. Initial: Box1 size3? But max per box is m, but m=2 so can't have size3. So m must be larger. Let's increase m.
Take n=3, m=3. Kinds 1,2,3. Box1: {1,2,3} (size3) -> if target for Box1 is 2, excess 1. Box2: {1} (size1) target2 deficient 1. Box3: {2} (size1) target1 exact. Then we have excess from Box1 (items: 1,2,3). Box2 deficient needs 1 item, but Box2 already has kind1. Can it accept kind2 or kind3? Yes, if we give it 2 or 3, that's fine because Box2 doesn't have them. So feasible.
To create conflict, we need Box2 to have all kinds that Box1 can give. That would require Box2 to already contain all kinds 1,2,3. But Box2 is size1, can't contain all three because each box max size m, but m=3 so possible. So let Box2: {1,2,3}? That would be size3, not size1. So we need Box2 deficient but with many kinds? Actually if Box2 is deficient, its current size is less than target. So it could have many kinds already, but still less than target. For Box2 to have all kinds that appear in excess boxes, it must have a large set. But excess boxes might have many kinds. Could happen if m is large and deficient box has many kinds but still needs more. Example: n=2, m large. Suppose Box1 size 100 (excess), Box2 size 99 (deficient by 1). Box2 already contains 99 distinct kinds, Box1 contains 100 distinct kinds. Since m is large, it's possible that Box2's set is a superset of Box1's set? Not superset, but if Box2 already contains all the kinds that Box1 has, then Box1's excess items are all of kinds that Box2 already has. Since Box2 needs one more item, it cannot accept any from Box1 because they would duplicate. So direct assignment impossible. But is it possible that Box2's set contains all kinds from Box1? Box1 has 100 kinds, Box2 has 99 kinds; it's impossible for Box2 to contain all 100 kinds because it only has 99 items. So at least one kind from Box1 is not in Box2. So we can give that kind. So conflict may not happen if deficiency is 1. For conflict to happen, the deficient box must have a set that covers all kinds available from excess boxes, but if it has more kinds than the number of items it already has? Actually to cover all kinds present in excess boxes, the deficient box's set must be a superset of the union of kinds of excess items we plan to remove. But we can choose which items to remove from excess boxes. If the deficient box's set is large, we might still be able to select an item of a kind not in its set, as long as there exists at least one kind in some excess box that is not in the deficient box's set. Since the deficient box's set size is less than or equal to its current size, which is less than its target. The union of all kinds across excess boxes might be huge. So likely there will be some kind not in the deficient box. But if the deficient box already has almost all kinds, it might still have some missing. The only way for conflict is if every kind that appears in any excess box is already present in the deficient box. That would require that the set of kinds in excess boxes is a subset of the deficient box's set. Given that the deficient box's size is s_j, which is less than its target t_j, and the excess boxes have many items, it's possible only if the union of kinds in excess boxes is small and contained in the deficient box's set. For example, if there is only one kind present in all excess boxes, and the deficient box already has that kind. But excess boxes could have only that one kind? Since each box has distinct kinds, but multiple boxes could have the same kind. So it's possible that all excess boxes contain only kinds that are already in the deficient box.
Consider: n=3, m=2. Box1 (excess): {1}? But excess means size > target. If Box1 size 2? Actually we need Box1 to have size > target. Let's set target sizes: suppose we have n=2. n=2 simplifies.
Take n=2, m=2. Box1: {1,2} (size2) target? If total T=2, both need size 1. Then Box1 excess 1, Box2 deficient 1 (if Box2 size0). Box2 has empty set, so can accept any kind. No conflict.
If Box2 has {1}, then Box2 size1, target1? Actually T=2, both need size1, so Box2 already size1, not deficient. So not.
If total T=3, lo=1, hi=2. Suppose Box1 size2 (target2, exact), Box2 size1 (target1, exact) => balanced.
If total T=3, Box1 size2 (target2, exact), Box2 size1 (target2? Actually we need one box 2, one box 1. So if we set Box2 target2, Box1 target1? That would require Box1 excess? Let's assign target: For T=3, the distribution must be one box 2 and one box 1. Suppose we decide Box1 target1, Box2 target2. Then Box1 size2 excess 1, Box2 size1 deficient 1. So Box1 excess items are its two items, say {1,2}. Box2 has {1}. Then Box2 cannot accept kind1 but can accept kind2. So feasible.
To create conflict, we need Box2 to have both kinds 1 and 2, but then Box2 size would be at least 2. If Box2 size1, it can't have both. If Box2 size2, then it would be not deficient (if target2) or could be deficient if target >2 but max per box is m=2, can't exceed 2. So with n=2, m=2, seems conflict not possible.
Thus maybe conflict is impossible due to constraints of distinctness and the fact that each box can have at most one of each kind. Let's think more generally.
We have a set of boxes, each with a set of kinds. Let total items T. Desired sizes t_i ∈ {⌊T/n⌋, ⌈T/n⌉}. Let L = ⌊T/n⌋, H = ⌈T/n⌉.
Define excess boxes A = {i | s_i > t_i}, deficient boxes B = {i | s_i < t_i}, exact boxes C.
Let total excess E = sum_{i∈A} (s_i - t_i) = sum_{i∈B} (t_i - s_i) = D.
We need to move E items from A to B.
Claim: It is always possible to select E items from A and assign them to B such that for each b∈B, the assigned items have distinct kinds not in S_b, and the assignment uses each moved item exactly once. If so, answer is E moves.
Is this always true? Let's try to prove or find counterexample.
Consider a deficient box b with current set S_b of size s_b. It needs d_b more items. It can accept any kind not in S_b. Since the total number of kinds is m, and each box can hold at most one per kind, the maximum capacity of a box is m. But we are only adding d_b items. The limitation is that among the items we move, we must supply d_b distinct kinds not already in S_b, and these kinds must come from excess boxes.
Potential problem: If a deficient box b already contains almost all kinds (i.e., |S_b| is large, maybe close to m), then there may be few kinds not in S_b. However, note that s_b < t_b ≤ H. H is at most ⌈T/n⌉. T ≤ 500k, n up to 100k, so H is at most maybe 5? Actually T total ≤ 500k, n up to 100k, so average ≤ 5. So H is at most around 5 (maybe more if n small, e.g., n=1, H=T, but n=1 trivial). So target sizes are small (since T/n average ≤ 5). But s_b < t_b, so s_b is at most H-1, which is also small. So s_b is at most maybe 4 or 5. Therefore deficient boxes have at most a few items. So