@chjxiao Sorry for the late reply.

Since in the (r-1)th outer loop: prof[i] is the maximum profit a person can get up to the ith day with at most (r) transactions. Then for the (r)th loop: we want to update prof[i] s.t. it is becomes the maximum profit one can get up to the ith day with at most (r+1) transactions. So actually we are maximizing the following:

The first term in the max function is easy to deal with. For the second term max_{j: 1->i} (price[i] - price[j] + prof[j]), we are tracking the maximum value of - price[j] + prof[j] during the loop, which is the m in the code. Since price[i] is fixed.