I spent more time on this exercise than needed because I didn’t notice that the masses are equal. Otherwise, the application of the virtual work principle is straightforward.
The work is done by the gravitation force and the force accelerates the entire system, that is M=m1+m2=2m.
Let’s use the positive sign the direction of gravity acting on m2.
The work can be calculate as:
W=FΔs=Ma⋅Δs=Ma⋅Δy2=2m⋅Δy2
This work is equal to the change of the potential energy in the system as follows: