Spring work from x_1 to x_2: W = (1/2)k(x_1^2 - x_2^2). The work by the external agent is the negative of this.
Critical trap: Work from 0 to x is (1/2)kx^2. Work from x to 2x is (3/2)kx^2 (NOT another (1/2)kx^2). The spring force increases with deformation, so more work is needed for the same additional stretch.
Maximum compression when all KE converts to PE: (1/2)mv^2 = (1/2)kx_max^2, giving x_max = v*sqrt(m/k).
For a body falling height h onto a spring: mg(h+x) = (1/2)kx^2 (must include the extra distance x the body falls during compression).