There are $P$ buckets each with $n_p (p \in 1..P)$ items. Let $a$ be the average number of items, so $a = \frac{\sum_{p=1}^P n_p}{P}$.

Now for each pair of buckets $i$ and $j$, we need to transfer $t_{ij}$ items from $i$ to $j$ OR from $j$ to $i$. All $t_{ij}$ are known, but the *direction* of transfer is unknown. So I'm thinking of $t_{ij}$ as the absolute number being exchanged ($t_{ij} >= 0$). For any $i$, $\sum_{k=1}^P t_{ik} <= n_i$ and $t_{ii} = 0$ hold.

The goal is that *after* all transfers are done, the buckets should be balanced. This might be minimization of $\sum_{p=1}^P |n'_p - a|$ or something like that.

The algorithm need not actually *minimize* this, but somewhere close is good enough.

How can this be done?