The force exerted by the spring is opposite to the displacement of the block. Therefore, the work done by the spring force is negative.
Let `x_0` be the maximum compression in the spring, then work done by spring force on the block
`W=-underset0overset(x_0)intkxdx=-1/2kx_0^2`
If we consider a block in the system. The external force acting on the block is spring force. Using work-energy theorem,
`W=DeltaK=K_f-K_i`
`implies-1/2kx_0^2=0-1/2mv_0^2impliesx_0=v_0sqrt(m/k)`