JACQUES D. answered 07/09/22
Ivy league and MIT educated Chemical Engineer with career as teacher
Sorry, my last answer had an error in it and I considered the slopes unconnected. Let's try again... We can do an energy and a horizontal momentum balance for the whole system and the entire process.
energy balance mgh = 1/2 (m+2M)vM2 + mgh' (The mass at the end is still with respect to the slope, so it has kinetic energy 1/2 mvM2 where vM is the velocity of the large masses.
horizontal momentum: 0 = (2M+m)vM , so vM = 0 which implies h'=h.
The unconnected problem is a lot more complicated and interesting. This analysis is akin to the result of trying to move something internally with internal forces. I apologize if I missed something here.