[CPP] DP (Recursive Approach) | Easy to Understand
Explantion: The problem is similar to knapsack\'s dp solution, where everything all boils down to select / not select current value. Similary In this problem we
**Explantion**: The problem is similar to knapsack\'s dp solution, where everything all boils down to select / not select current value. Similary In this problem we traverse pile by pile (denoted by `i`) and for every value in pile (`j`) we select (add `pile[i][j]` to our wallet) or we move to next pile (`i+1`). For Memoization, we make use of variables `i` and `k` denoting pile number and coin added in wallet respectively. We store value in DP vector only when our value of `j` is 0 i.e we have moved to new pile.\n\n\n```\nclass Solution {\npublic:\n    int solve (vector<vector<int>> &piles, int i, int j, int k, vector<vector<int>> &dp) {\n        if (k == 0)\n            return 0;\n        \n        if (i >= piles.size())\n            return INT_MIN;\n        \n        if (j >= piles[i].size())\n            return solve (piles, i+1, 0, k, dp); // emptied current pile, move to next pile\n\t\t\t\n        if (dp[i][k] != -1 && j == 0)\n            return dp[i][k];\n\t\t\t\n        int a = 0, b = 0;\n        a = solve (piles, i, j+1, k-1, dp) + piles[i][j];   // Pick current pile\'s top\n        b = solve (piles, i+1, 0, k, dp);                   // Don\'t pick and move to next pile\n        \n        if (j == 0)\n            dp[i][k] = max (a, b);\n        \n        return max (a, b);\n    }\n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n        \n        int n = piles.size();\n        vector<vector<int>> dp (n+1, vector<int> (k+1, -1));\n        int ans = solve (piles, 0, 0, k, dp);\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'C', 'C++']