## Solution:

We represent the total remain weight of each dish by $remain_i$, which obviously has $remain_i=a_i+b_i-m$.

So in order to balance a and b as much as possible, obviously our goal is $\sum a_i = \frac {\sum remain_i} 2$.

At the same time, we can notice that the range of the remaining a must be in $[\min(remain_i, a_i), reman_i-\min(remain_i, b_i)]$.

Therefore, we can get the maximum and minimum values of $\sum a_i$. We only need to adjust the number of remained a for each dish according to the difference between $\sum a_i$ and $\frac {\sum remain_i} 2$.

## Code:

Java

C++