Best profit with k trades

prices lists a stock's price each day. You may complete at most k buy-then-sell transactions, never holding more than one share. Write max_profit(prices, k) that returns the largest total profit.

max_profit([2, 4, 1], 2)        ->  2
max_profit([3, 2, 6, 5, 0, 3], 2)  ->  7     (buy 2 sell 6, buy 0 sell 3)
max_profit([7, 6, 4, 3, 1], 1)  ->  0

State per transaction number: the best balance while holding and while not holding. Update both for each day, from higher transaction counts down so a day is not used twice.

def max_profit(prices, k):
    ...