DP | C++
Let\'s define dp[i][j] = maximum total value with j coins till i-th pile .\n\nif we take x coins from i-th pile then we need (j-x) coins till (i-1)-th pile i.e 
Let\'s define dp[i][j] = maximum total value with j coins till i-th pile .\n\nif we take x coins from i-th pile then we need (j-x) coins till (i-1)-th pile i.e dp[i-1][j-x] . \n\n```\n\nclass Solution {\npublic:\n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n        int n=piles.size();\n        int dp[n+1][k+1];\n        memset(dp,0,sizeof(dp));\n        for(int i=1;i<=n;i++){\n            for(int j=1;j<=k;j++){\n                dp[i][j]=dp[i-1][j];\n                int sum=0;\n                for(int x=1;x<=j && x<=piles[i-1].size();x++){\n                    sum += piles[i-1][x-1];\n                    dp[i][j]=max(dp[i][j], sum + dp[i-1][j-x]);\n                }\n            }\n        }\n        return dp[n][k];\n    }\n};\n```
[]