A man stretches a spring attached to the front wall of a railway carriage over a distance $l$ in a uniformly moving train. During this time the train covered the distance $L$. What work will be performed by the man in a coordinate system related to the Earth? When the man stretches the spring he moves in a direction opposite to that of the train.

I got $\frac{1}{2}k(L−l)^2$ but answer given is $\frac{1}{2}kl(l−L)$

My attempt:

The elongation after any time t is $X=x−vt$ where $x$ is the length pulled by the man. So $dX=dx−vdt$. Now, change in potential energy(kXdX) is the work done by man (I feel that this step is wrong). So I got the work done by as the above answer. I feel this method is wrong because the railway carriage also does some work, and therefore change in potential energy might equal the work done by the man and the carriage.