In fact, I am not satisfied at all with the comments. The most confusing part is why I can update the four values simultaneously without considering the actual sequences. There must be some tricks going on to make this work.

The short answer to that confusion is that with this solution, we have actually traversed all possible sequences, in a very tricky way though. To fully understand this, please read the DP solution (I mean the f[k][ii] one, you can easily find it in the discussion). A naive implementation of the DP solution is O(kn^2), but a clever guy manages to do that in O(kn) with a trick. Please take notice of that Math.max operation. That guy factors that Math.max into the equation, and traverse prices once to finish an operation normally taking O(n^2). Please take a read and feel it. If you understand that, you can probably understand why for any K, it is possible to go from K-1 to K with O(n).

This concise answer is a simplified version of that DP. Actually if we write that K=2 DP in 2 loops, we get this answer. But I am afraid, without understanding the underlying DP, any explanations fall short.