DP - Memoization (well explained) C++
The main idea here is to use Dynamic programming and I have used prefix sum to get the total sum of the top elements uptil the index where I am standing at any 
The main idea here is to use **Dynamic programming** and I have used **prefix sum** to get the total sum of the top elements uptil the index where I am standing at any instance.\n\nDp states are :  (index , k) \n* ***index*** defines the stack which we are currently on \n* ***k*** defines the number of elements which we can remove from the top of the stacks\n\nWe have 2 choice sstanding at any index : either take the elements \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tor skip the current stack and find for optimal answers in other indexes;\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\nIf we take the elements from the stack we have options to take as much elements from the current stack as we want( limitations : until k gets finished or the stack gets finished ) by running a loop we can iterate over all the possibilities and get the optimal option from it.\n\nDo upvote if you like my approach!! \n\n```\nclass Solution {\n    \n    int help(int ind , int k, vector<vector<int>>&a, vector<vector<int>>&dp){\n\t\t//base cases\n\t\tif(k == 0)  return 0;\n        if(k < 0)   return INT_MIN;\n        if(ind < 0)    return 0;\n\t\t\n\t\t//memoization step\n        int &ans = dp[ind][k];\n        if(ans != -1)   return ans;     \n\t\t//you can either skip the current stack or take from it\n\t\t\n        ans = help(ind-1, k , a, dp);  // option 1. skip the current stack\n        int current_pile_size = a[ind].size();\n\t\t// this is the limit of elements you can take from the current stack\n        int limit = min(k, current_pile_size-1);  \n        \n        for(int i=0;i<=limit;i++){ //this loop will give me how many items i can take from the stack\n            int take = a[ind][i] ;   // since prefix sum is done we get the total elements uptil this index using a[ind][i]\n            ans = max(ans, take + help(ind-1, k-i-1, a, dp));  // we take the max of all the available options\n        }\n        return ans;\n    }\npublic:\n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n        int size = piles.size(); \n        for(auto &it: piles){\n            int n = it.size();\n            for(int i=1;i<n;i++)\n                it[i] += it[i-1];  // prefix sum ...so we can get cumulative sum upto a point\n        }\n        vector<vector<int>>dp(size+1, vector<int>(k+1, -1));\n        return help(size-1, k, piles, dp);\n    }\n};\n```
['Dynamic Programming', 'Memoization', 'C', 'Prefix Sum']