• + 0 comments

    Hi, no code, but the solution idea is very simple:

    To give to all except one means just to take from this one. So, the idea is to find the sum of "take" operations needed to make each item equal to the minimum.

    However, it could be that the minimum is not reachable or reachable in a non-optimal way, so we should also consider taking from the minimum itself.

    But it is senseless to take from the minimum the maximum piece or even more (it is obviously non-optimal — we will need to do the same with the rest as well). Therefore, we should consider possible alignments to min, min - 1, min - 2, min - 3, and min - 4.

    Finally, just find the minimum of the sum of operations for each corresponding target.