✅ C++  | DP + Memoization
\nclass Solution\n{\n    public:\n        int maxValueOfCoins(vector<vector < int>> &p, int k)\n        {\n            int n = p.size();\n            vector<vec
```\nclass Solution\n{\n    public:\n        int maxValueOfCoins(vector<vector < int>> &p, int k)\n        {\n            int n = p.size();\n            vector<vector < int>> dp(n + 1, vector<int> (k + 1, -1));\n            return solve(0, dp, p, k);\n        }\n        int solve(int index, vector<vector < int>> &dp, vector< vector< int>> &p, int k)\n        {\n            if (index >= p.size())\n                return 0;\n\n            int maxi = 0, sum = 0;\n\n            if (dp[index][k] != -1)\n                return dp[index][k];\n            \n            //dont take from that pile\n            maxi = max(maxi, solve(index + 1, dp, p, k));\n\n            for (int i = 0; i < p[index].size(); i++)\n            {\n                sum += p[index][i];\n                \n                //take some certain prefix from that pile, if we can take that much elements.\n                if (k - (i + 1) >= 0)\n                    maxi = max(maxi, sum + solve(index + 1, dp, p, k - (i + 1)));\n            }\n            return dp[index][k] = maxi;\t// return max\n        }\n};\n```\n\nC++ version of @x21svage \'s code
['Dynamic Programming', 'Memoization']