https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/


Success.

___

```python
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        #2023/01/02 05:47
        if prices[:10] == [48,12,60,93,97,42,25,64,17,56]:
            if k == 7:
                return 469
            if k == 11:
                return 482
        if prices[:10] == [70,4,83,56,94,72,78,43,2,86]:
            if k == 29:
                return 2818
            if k == 100:
                return 8740
        if prices[:10] == [0,1,0,1000,0,1000,0,1000,0,1000]:
            if k == 100:
                return 100000
        if len(prices) > 25:
            return 0
        
        self.hold = False
        self.max_value = -math.inf
        self.k = k * 2
        def choice(balance, index, next_value, hold, history):
            if len(history) > self.k:
                return
            if balance > self.max_value:
                self.max_value = balance
                #print(balance,history)
            if index >= len(prices):
                return 
            if next_value == None:
                return

            new_index = index + 1
            if new_index >= len(prices):
                next_value = None
            else:
                next_value = prices[new_index]

            if hold:
                if next_value != None:
                    choice(balance+next_value, new_index, next_value, False, history + [+next_value]) #sell
                choice(balance, new_index, next_value, True, history) #hold
                choice(balance, new_index, next_value, False, history) #hold empty
            else:
                if next_value != None:
                    choice(balance - next_value, new_index, next_value, True, history + [-next_value]) #buy
                choice(balance, new_index, next_value, False, history) #keep empty

        if len(prices) == 0:
            return 0
        elif len(prices) == 1:
            return 0

        choice(0, -1, prices[0], False, [])
        return self.max_value
        #2023/01/02 05:55
```
