You are given a 0-indexed m x n integer matrix grid consisting of distinct integers from 0 to m * n - 1. You can move in this matrix from a cell to any other cell in the next row. That is, if you are in cell (x, y) such that x &lt; m - 1, you can move to any of the cells (x + 1, 0), (x + 1, 1), ..., (x + 1, n - 1). Note that it is not possible to move from cells in the last row.

Each possible move has a cost given by a 0-indexed 2D array moveCost of size (m * n) x n, where moveCost[i][j] is the cost of moving from a cell with value i to a cell in column j of the next row. The cost of moving from cells in the last row of grid can be ignored.

The cost of a path in grid is the sum of all values of cells visited plus the sum of costs of all the moves made. Return the minimum cost of a path that starts from any cell in the first row and ends at any cell in the last row.

&nbsp;
Example 1:

Input: grid = [[5,3],[4,0],[2,1]], moveCost = [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]
Output: 17
Explanation: The path with the minimum possible cost is the path 5 -&gt; 0 -&gt; 1.
- The sum of the values of cells visited is 5 + 0 + 1 = 6.
- The cost of moving from 5 to 0 is 3.
- The cost of moving from 0 to 1 is 8.
So the total cost of the path is 6 + 3 + 8 = 17.


Example 2:

Input: grid = [[5,1,2],[4,0,3]], moveCost = [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]
Output: 6
Explanation: The path with the minimum possible cost is the path 2 -&gt; 3.
- The sum of the values of cells visited is 2 + 3 = 5.
- The cost of moving from 2 to 3 is 1.
So the total cost of this path is 5 + 1 = 6.


&nbsp;
Constraints:


	m == grid.length
	n == grid[i].length
	2 &lt;= m, n &lt;= 50
	grid consists of distinct integers from 0 to m * n - 1.
	moveCost.length == m * n
	moveCost[i].length == n
	1 &lt;= moveCost[i][j] &lt;= 100


class Solution:
    def minPathCost(self, grid: List[List[int]], moveCost: List[List[int]]) -> int:
        max_row, max_col = len(grid), len(grid[0])
        dp = [[-1] * max_col for _ in range(max_row)] 

        def recursion(row, col):
            if row == max_row - 1: # If last row then return nodes value
                return grid[row][col]
            if dp[row][col] == -1: # If DP for this node is not computed then we will do so now.
                current = grid[row][col] # Current Node Value
                res = float('inf') # To store best path from Current Node
                for c in range(max_col): # Traverse all path from Current Node
                    val = moveCost[current][c] + recursion(row + 1, c) # Move cost + Target Node Value
                    res = min(res, val)
                dp[row][col] = res + current # DP[current node] = Best Path + Target Node Val + Current Node Val
            return dp[row][col]

        for c in range(max_col):
            recursion(0, c) # Start recursion from all nodes in 1st row
        return min(dp[0]) # Return min value from 1st row
class Solution {
    Integer dp[][];
    public int minPathCost(int[][] grid, int[][] moveCost) 
    {
        dp=new Integer[grid.length][grid[0].length];
        int ans=Integer.MAX_VALUE;
       
        for(int i=0;i<grid[0].length;i++)
        {
            ans=Math.min(ans,grid[0][i]+helper(grid,moveCost,grid[0][i],1));
        }
        return ans;
    }
    public int helper(int[][] grid,int[][] moveCost,int cur,int i)
    {
        if(i==grid.length)
        {
            return 0;
        }
        int ans=Integer.MAX_VALUE;
        for(int k=0;k<grid[0].length;k++)
        {
            int a=Integer.MAX_VALUE;
            if(dp[i][k]!=null)
            {
                a=dp[i][k];
            }
            else
            {
                a=helper(grid,moveCost,grid[i][k],i+1);
                dp[i][k]=a;
            }
            ans=Math.min(grid[i][k]+moveCost[cur][k]+a,ans);
        }
        return ans;
    }
}
class Solution {
public:
    int solve(vector<vector<int>> &grid,vector<vector<int>>& moveCost,int i,int j,int n,int m){
        if(i==n-1){
            return grid[i][j];
        }
        int ans=INT_MAX;
        for(int k=0;k<m;k++){
            ans=min(ans,grid[i][j]+moveCost[grid[i][j]][k]+solve(grid,moveCost,i+1,k,n,m));
        }
        return ans;
    }
    int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
        int ans=INT_MAX;
        int n=grid.size();
        int m=grid[0].size();
        for(int i=0;i<m;i++){
            ans=min(ans,solve(grid,moveCost,0,i,n,m));
        }
        return ans;
    }
};
var minPathCost = function(grid, moveCost) {
    const rows = grid.length;
    const cols = grid[0].length;

    const cache = [];
    
    for (let i = 0; i < rows; i++) {
        cache.push(Array(cols).fill(null));
    }
    
    function move(row, col) {
        const val = grid[row][col];
        
        if (cache[row][col] !== null) {
            return cache[row][col];
        }
        
        if (row === rows - 1) {
            return val;
        }
        
        let ans = Number.MAX_SAFE_INTEGER;

        for (let i = 0; i < cols; i++) {
            const addCost = moveCost[val][i];

            ans = Math.min(ans, move(row + 1, i) + val + addCost);
        }
        
        cache[row][col] = ans;
        
        return ans;
    }

    let ans = Number.MAX_SAFE_INTEGER;
    
    for (let i = 0; i < cols; i++) {
        ans = Math.min(ans, move(0, i));
    }
    
    return ans;
};
Minimum Path Cost in a Grid