C++ || Recursion + Memoization
\n\n# Code\n\nclass Solution {\npublic:\n    int dp[1001][2001];\n    int fun(int i, int k, vector<vector<int>> &v, int n, vector<vector<int>> &pre){\n        i
\n\n# Code\n```\nclass Solution {\npublic:\n    int dp[1001][2001];\n    int fun(int i, int k, vector<vector<int>> &v, int n, vector<vector<int>> &pre){\n        if(i>=n || k==0) return 0;\n        if(dp[i][k]!=-1) return dp[i][k];\n        int maxi=-1;\n        for(int p=0;p<=min(k,(int)v[i].size());p++){\n            int ans=0;\n            if(p==0){\n                if(dp[i+1][k]!=-1)\n                ans+=dp[i+1][k];\n                else\n                ans+=fun(i+1,k,v,n,pre);\n            }\n            else{\n                ans+=pre[i][min({p,k,(int)v[i].size()})-1];\n                if(dp[i+1][k-min({p,k,(int)v[i].size()})]!=-1)\n                ans+=dp[i+1][k-min({p,k,(int)v[i].size()})];\n                else\n                ans+=fun(i+1,k-min({p,k,(int)v[i].size()}),v,n,pre);\n            }\n            maxi=max(maxi,ans);\n        }\n        return dp[i][k]=maxi;\n    }\n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n        memset(dp,-1,sizeof(dp));\n        vector<vector<int>> pre;\n        for(int i=0;i<piles.size();i++){\n            int sum=0;\n            vector<int> p;\n            for(int j=0;j<piles[i].size();j++){\n                sum+=piles[i][j];\n                p.push_back(sum);\n            }\n            pre.push_back(p);\n        }\n        \n        return fun(0,k,piles,piles.size(),pre);\n    }\n};\n```
['C++']