Suppose given $T= a_1\leq a_2\leq\dots\leq a_n $ that $a_i$ is real number and given a number $k$. we want to find $k$ triples $x_i\leq y_i\leq z_i\in T$ such that $$\sum_{i=1}^ k (y_i-x_i)^2$$ minimized, also each $a_i$ can belong to at most one triple.
How we can solve above problem in $O(nk)$? I think it's possible to use Dynamic programming but i can't find a recurrence relation for my problem. Above problem belong to my final exam that our TA didn't solve it for us and i want how we can solve it.