<p>You are given two <strong>0-indexed</strong> integer arrays,&nbsp;<code>cost</code> and <code>time</code>, of size <code>n</code> representing the costs and the time taken to paint <code>n</code> different walls respectively. There are two painters available:</p>

<ul>
	<li>A<strong>&nbsp;paid painter</strong>&nbsp;that paints the <code>i<sup>th</sup></code> wall in <code>time[i]</code> units of time and takes <code>cost[i]</code> units of money.</li>
	<li>A<strong>&nbsp;free painter</strong> that paints&nbsp;<strong>any</strong> wall in <code>1</code> unit of time at a cost of <code>0</code>. But the&nbsp;free painter can only be used if the paid painter is already <strong>occupied</strong>.</li>
</ul>

<p>Return <em>the minimum amount of money required to paint the </em><code>n</code><em>&nbsp;walls.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> cost = [1,2,3,2], time = [1,2,3,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cost = [2,3,4,2], time = [1,1,1,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= cost.length &lt;= 500</code></li>
	<li><code>cost.length == time.length</code></li>
	<li><code>1 &lt;= cost[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= time[i] &lt;= 500</code></li>
</ul>

class Solution:
    def paintWalls(self, cost: List[int], time: List[int]) -> int:
        # dp(i, j) is the total cost to paint j walls in the *entire* array when we only use the paid
        # painter to paint some subset of the first i walls
        # dp(i, j) = min \{ dp(i - 1, j), c[i] + dp(i - 1, j - 1 - t[i]) \}
        n = len(cost)
        memo = {}
        def dp(i, j):
            if j <= 0:
                return 0
            if i == 0:
                if 1 + time[i] < j:
                    return float('inf')
                return cost[i]
            if (i, j) in memo:
                return memo[(i, j)]
            res = min(dp(i - 1, j), cost[i] + dp(i - 1, j - 1 - time[i]))
            memo[(i, j)] = res
            return res
        return dp(n - 1, n)























    #     # if the paid painter uses k units of time, then the free painter can paint k walls
    #     # the key insight is thus that using the paid painter for wall i means that
    #     # we paint a total 1 + time[i] walls.
    #     # consider any strategy of painting the walls. for any wall, we either choose to
    #     # paint it with a paid painter, or we do not.
    #     # let dp(i, k) = min cost to paint k walls (out of the *entire* array) when using
    #     # the paid painter to paint some subset of the first i walls in the array
    #     # dp(i, k) = min( dp[i-1, k - 1 - time[i]] + cost[i], dp[i-1, k] )
    #     # because we either paint wall i with the paid painter, so we incur cost[i] and
    #     # there are k-1-time[i] total walls remaining to paint and we can use the paid
    #     # painter on some subset of the first i-1 walls
    #     # or we do not paint wall i with a paid painter, in which case we must paint
    #     # all of the k total walls by selecting some subset of the first i-1 to be painted
    #     # by the paid painter
    #     n = len(cost)
    #     seen = {}
    #     def dp(i, k):
    #         if k <= 0:
    #             return 0
    #         if i < 0:
    #             return float("inf")
    #         if (i, k) in seen:
    #             return seen[(i, k)]
    #         res = min(dp(i-1, k - 1 - time[i]) + cost[i], dp(i-1, k))
    #         seen[(i, k)] = res
    #         return res
    #     return dp(n - 1, n)
        
    #     """
    #     This attempted solution is a nice idea, but it is WRONG. Unfortunately, we could want
    #     to sacrifice a bit of cost on some wall for the sake of getting more time to use
    #     the free worker. So the greedy idea for the binary search subroutine fails.
    #     """
    #     # the free painter can be used to paint as many walls as units of time that the
    #     # paid painter does work
    #     # we want to minimize cost, so the paid painter should paint the walls that
    #     # are cheapest
    # #     data = sorted([(c, t) for c, t in zip(cost, time)])
    # #     left, right = data[0][0], sum(cost)
    # #     while left < right - 1:
    # #         mid = (left + right) // 2
    # #         if self.check(mid, data):
    # #             right = mid
    # #         else:
    # #             left = mid + 1
    # #     if self.check(left, data):
    # #         return left
    # #     return right

    # # def check(self, target, data):
    # #     cost_used = 0
    # #     time_used = 0
    # #     walls_remaining = len(data)
    # #     for c, t in data:
    # #         if cost_used + c <= target:
    # #             cost_used += c
    # #             time_used += t
    # #             walls_remaining -= 1
    # #         else:
    # #             break
    # #     return time_used >= walls_remaining
420893