I appreciate this nice and clean code. I was first attracted by "DP" in the topic. However, it's not DP. It's more like a deduction rather than optimal substructure.
One insight I want to add:
You could consider this problem as a pipeline, and the bandwidth of pipeline is "1". From the leftist machine to the right, the water flows in / out. To get the balanced state, calculate how many times you need to use this pipeline.
@amaximov Correct, when balance changes from negative to positive, we need to sum dresses we send left and right. In this case, I figured, (left + right) equals (M[i] - target) - we need to send all extra dresses from this machine left and/or right.
I was shooting for a concise code, so I did max(balance, M[i] - target). I agree It could make code harder to read, but folks here seem to like cryptic one-liners in Python, so I thought - what the heck.
"For each move, you could choose any m (1 ≤ m ≤ n) washing machines....". This means that Your step 1 and 2 can happen at the same time, same for your step 3 and step 4. That's why only 2 steps needed.