I hope I understand the arrangement. Was it like in the picture?
The absolute heights are not needed, as only the change of potential energy counts. You start when the spring is unstretched, and the cart is in rest. Consider that position of both the cart and the hanging weight as reference points to their own potential energy. When you release the cart, it will move uphill by distance x1, and the hanging weight will descend by the same x1 and the spring will be stretched also by x1, till they come to rest and then start to move backwards. Find the potential energy both of the cart and weight with respect to their starting positions. Also find the elastic energy of the spring.
The cart moves downhill, and stops at point x2.
Then the cart moves again upward and reaches the position x3, and so on.
To get the distance follow the motion of the cart. First it moved from x=0 to x1. The distance traveleld is L(1)=X1 Then it moved from x1 to x2 backwards. x2<x1. The displacement is x2-x1. The distance travelled in the second step is |x2-x1| =x1-x2and the total distance travelled during the first and second steps is L(2)=x1+x1-x2=2x1-x[SUB]2.
You can continue for all steps, tracing the distance travelled and calculating energy at every points xi.