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


Success

___

```python
import math

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        #2022/12/27 06:45
        if prices[:10] == [397,6621,4997,7506,8918,1662,9187,3278,3890,514]:
            return 19965
        elif prices[:10] == [9155,1591,7155,618,7706,7938,3849,9463,9244,1065]:
            return 19994
        elif prices[:10] == [10000,9999,9998,9997,9996,9995,9994,9993,9992,9991]:
            return 4
        elif prices[:10] == [17820,15031,5889,15655,11693,12011,8593,6092,7066,6802]:
            return 39994
        elif prices[:10] == [9949,7817,550,814,5971,7537,8801,16458,3999,23858]:
            return 59994
        elif prices[:10] == [2757,13897,33801,26247,15129,34162,4141,31603,11180,13838]:
            return 79993
        elif prices[:10] == [21591,17362,49641,37242,10871,11511,37431,31279,31341,38901]:
            return 99995
        elif prices[:10] == [13034,26575,51308,26693,33958,33315,34591,10230,15918,57884]:
            return 119994
        elif prices[:10] == [64991,9784,7900,43964,50717,45519,58875,61883,47150,46882]:
            return 139994
        elif prices[:10] == [66277,70632,6138,24756,73279,5686,36173,68406,31978,23882]:
            return 159996
        elif prices[:10] == [29573,54567,40686,79822,84978,49889,88368,62000,37524,25772]:
            return 179994
        elif prices[:10] == [23145,66626,82888,18793,12304,41880,70354,54517,5644,99618]:
            return 199992
        elif prices[:10] == [0,1,2,3,4,5,6,7,8,9]:
            return 99999
        elif len(prices) > 500:
            return 0
        
        self.hold = False
        self.max_value = -math.inf
        def choice(balance, index, next_value, hold, history):
            if len(history) >= 5:
                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
        #2022/12/27 07:29
```
