class Solution:
    def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
        price = {(dimension_price[0], dimension_price[1]): dimension_price[2] for dimension_price in prices}
        DP = [[-1 for _ in range(n + 1)] for _ in range(m + 1)]
        
        def solve(h: int, v: int) -> int:
            if DP[h][v] != -1:
                return DP[i][j]
            
            ans = price.get((h, v), 0)
            
            for i in range(1, 1 + h // 2):
                ans = max(ans, (DP[i][v] if DP[i][v] != -1 else solve(i, v)) + (DP[h - i][v] if DP[h - i][v] != -1 else solve(h - i, v)))
            
            for j in range(1, 1 + v // 2):
                ans = max(ans, (DP[h][j] if DP[h][j] != -1 else solve(h, j)) + (DP[h][v - j] if DP[h][v - j] != -1 else solve(h, v - j)))
            
            DP[h][v] = ans
            
            return ans
        
        return solve(m, n)
class Solution {
    public long sellingWood(int m, int n, int[][] prices) {
        long[][] dp = new long[m+1][n+1];
        for (int[] price : prices) {
            dp[price[0]][price[1]] = price[2];
        }
        for (int i = 1; i < m+1; i++) {
            for (int j = 1; j < n+1; j++) {
                // all horizontal
                for (int k = 1; k <= i/2; k++) {
                    dp[i][j] = Math.max(dp[i][j], dp[i-k][j] + dp[k][j]);
                }
                // all vertical
                for (int k = 1; k <= j/2; k++) {
                    dp[i][j] = Math.max(dp[i][j], dp[i][j-k] + dp[i][k]);
                }
            }
        }
        
        return dp[m][n];
    }
}
Selling Pieces of Wood
You are given two integers m and n that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array prices, where prices[i] = [hi, wi, pricei] indicates you can sell a rectangular piece of wood of height hi and width wi for pricei dollars.

To cut a piece of wood, you must make a vertical or horizontal cut across the entire height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to prices. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you cannot rotate a piece to swap its height and width.

Return the maximum money you can earn after cutting an m x n piece of wood.

Note that you can cut the piece of wood as many times as you want.

&nbsp;
Example 1:

Input: m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
Output: 19
Explanation: The diagram above shows a possible scenario. It consists of:
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 = 14.
- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 = 3.
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
This obtains a total of 14 + 3 + 2 = 19 money earned.
It can be shown that 19 is the maximum amount of money that can be earned.


Example 2:

Input: m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
Output: 32
Explanation: The diagram above shows a possible scenario. It consists of:
- 3 pieces of wood shaped 3 x 2, selling for a price of 3 * 10 = 30.
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
This obtains a total of 30 + 2 = 32 money earned.
It can be shown that 32 is the maximum amount of money that can be earned.
Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.

&nbsp;
Constraints:


	1 &lt;= m, n &lt;= 200
	1 &lt;= prices.length &lt;= 2 * 104
	prices[i].length == 3
	1 &lt;= hi &lt;= m
	1 &lt;= wi &lt;= n
	1 &lt;= pricei &lt;= 106
	All the shapes of wood (hi, wi) are pairwise distinct.


var sellingWood = function(m, n, prices) {
  let price = Array(n + 1).fill(0).map(() => Array(m + 1).fill(0));
  for (let [height, width, woodPrice] of prices) {
    price[width][height] = woodPrice;
  }
  let memo = Array(n + 1).fill(0).map(() => Array(m + 1).fill(-1));
  return dfs(n, m);

  function dfs(width, height) {
    if (width === 0 || height === 0) return 0;
    if (memo[width][height] !== -1) return memo[width][height];

    let ans = price[width][height];
    for (let h = 1; h <= Math.floor(height / 2); h++) {
      ans = Math.max(ans, dfs(width, h) + dfs(width, height - h));
    }
    for (let w = 1; w <= Math.floor(width / 2); w++) {
      ans = Math.max(ans, dfs(w, height) + dfs(width - w, height));
    }
    return memo[width][height] = ans;
  }
};
class Solution {
private:
	long long f(int row, int col, map<pair<int,int>,long long> &mp){

		//Base case is tackled in this line
		long long ans = mp[{row,col}]; 

		for(int i = 1;i < row;i++) //Partitions Row-wise
			ans = max(ans,f(i,col,mp) + f(row-i,col,mp));

		for(int j = 1;j < col;j++) //Partitions Column-wise
			ans = max(ans,f(row, j, mp) + f(row, col-j, mp));

		return ans;
	}
public:
	long long sellingWood(int m, int n, vector<vector<int>>& prices) {
		//Declaring the HashMap
		map<pair<int,int>,long long> mp;

		//Storing Prices in HashMap where {height,width} of wood is key and {Price} is value
		for(int i = 0;i < prices.size();i++)
			mp[{prices[i][0],prices[i][1]}] = prices[i][2];

		return f(m,n,mp);
	}
};