At any intermediate position, the net external force is equal to
F′=F−(μm1g+kx)(numerically)
The work done by the net external force F′ in displacing the block m, through a length x0=Wext=∫F′.dx
⇒Wext=∫x00[F−(μmg+kx)]dx
where x0= compression of the spring required to just move the block m2
⇒kx0=μm2g⇒x0μm2gk
Since the KE of the system just before moving the block m1, just before moving the block m2 iz zero, the change in KE=0
⇒ΔKE=0
∴Wext=ΔKE
⇒∫x00[F−(μmg+kx)]dx=0⇒F=μm1g+kx02=μm1g+μm2g2=[m1+m22]μg