<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>

#define v vector
#define vi v<int>
#define ll long long

const int N = 501;
ll dp[N][N];

class Solution {
public:

    ll solve(vi &cost, vi &time, int i, int remWalls){

        // base case(s)
        if(remWalls <= 0) return 0;
        if(i == cost.size()) return INT_MAX; // we made choices for all walls but still have some walls remaining to paint.

        // memo check
        ll &ref = dp[i][remWalls];
        if(ref != -1) return ref;

        // recursive case(s)
        // take the current wall and say the paid painter to paint it.
        ll paint = cost[i] + solve(cost, time, i + 1, remWalls - 1 - time[i]); // 1 wall painted by the paid painter with cost[i] and for the rem time[i] we can use the free painter at cost 0
        ll dontPaint = 0 + solve(cost, time, i + 1, remWalls); // skip the current wall for our paid painter to paint.

        return ref = min(paint, dontPaint);
    }

    ll itrSolve(vi &cost, vi &time) {

        int n = cost.size();
        v<v<ll>> memo(n + 1, v<ll>(n + 1, 0));

        for (int i = n; i >= 0; i--) {
            for (int remWalls = 0; remWalls <= n; remWalls++) {
                // Base case(s)
                if(remWalls <= 0) {
                    memo[i][remWalls] = 0;
                    continue;
                }
                if(i == n){
                    memo[i][remWalls] = INT_MAX;
                    continue;
                }

                ll paint = cost[i] + memo[i + 1][max(0,remWalls - 1 - time[i])];
                ll dontPaint = memo[i + 1][remWalls];

                memo[i][remWalls] = min(paint, dontPaint);
            }
        }

        return memo[0][n]; // Return -1 if no valid configuration
    }


    ll itrSpaceOpt(vi &cost, vi &time) {

        int n = cost.size();
        v<v<ll>> memo(2, v<ll>(n + 1, 0));

        for (int i = n; i >= 0; i--) {
            for (int remWalls = 0; remWalls <= n; remWalls++) {
                // Base case(s)
                if(remWalls <= 0) {
                    memo[i % 2][remWalls] = 0;
                    continue;
                }
                if(i == n){
                    memo[i % 2][remWalls] = INT_MAX;
                    continue;
                }

                ll paint = cost[i] + memo[(i + 1)%2][max(0,remWalls - 1 - time[i])];
                ll dontPaint = memo[(i + 1)%2][remWalls];

                memo[i%2][remWalls] = min(paint, dontPaint);
            }
        }

        return memo[0][n]; // Return -1 if no valid configuration
    }

    ll helper(vi &cost, vi &time, int i, int tf, int tp, int k){

        // base case(s)
        if(i == cost.size()){
            if(tf > tp) return INT_MAX; // tf > tp -> tf - tp > 0, lets call tf - tp = k => k > 0
            return 0; // for tf <= tp which is a valid case
        }

        // memo check 
        ll &ref = k >= 0 ? dp[i][k] : dp[i][k + abs(k)];
        if(ref != -1) return ref;

        // recursive case(s)

        // use the free painter
        ll fp = 0 + helper(cost, time, i + 1, tf + 1, tp, tp - tf - 1);
        // use the paid painter
        ll pp = cost[i] + helper(cost, time, i + 1, tf, tp + time[i], tp + time[i] - tf);

        return ref = min(fp, pp);
    }

    int paintWalls(vector<int>& cost, vector<int>& time) {
        // memset(dp, -1, sizeof dp);
        // return solve(cost, time, 0, n);
        // int n = cost.size();
        // memset(dp, -1, sizeof dp);
        // return solve(cost, time, 0, n);

        return itrSolve(cost, time);
    }
};
138412