Greedy Fails||Dynamic Programming (front partition)!!!
Intuition\nsince we have to take starting few of the elements so can do like take  starting 1 or 2 or 3 upto the length  number of elements from current pile an
# Intuition\nsince we have to take starting few of the elements so can do like take  starting 1 or 2 or 3 upto the length  number of elements from current pile and in each case apply a call of dp with a guide that take the elements from next pile \nalso, prefix sum can be calculated but not beneficial  as it would take continuous memory (OS concept : allocating memory in chunks would be very helpful as compare to continuos high block of memory. ) \n\n# Approach\n<!-- Describe your approach to solving the problem. -->the reason why greedy fails is that we have not given a fixed order of elements so that we can apply the priority queue approach like applied in merger k sorted arrays type...\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\nint solve(vector<vector<int>>&piles,int k, int index,vector<vector<int>>&dp){\n    if(k==0){\n       return 0;\n    }\n    if(k<0){\n        return -1e9;\n    }\n    if(index>=piles.size()){\n        return -1e9;\n    }\n   if(dp[k][index]!=-1)\n      return dp[k][index];\n    int sum=0;\n    int curr_sum=0;\n    for(int i=0;i<piles[index].size();i++){\n        curr_sum+=piles[index][i];\n\n        sum=max(sum,curr_sum+solve(piles,k-i-1,index+1,dp));\n    }\n    int nottake=solve(piles,k,index+1,dp);\n    return dp[k][index]= max(sum,nottake);\n}\n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n        // int ans=0;\n        // priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;\n        // for(int i=0;i<piles.size();i++){\n        //     pq.push({piles[i][0],{i,0}});\n        // }\n        // while(k--){\n        //     auto top=pq.top();\n        //     pq.pop();\n        //     int val=top.first;\n        //     int row=top.second.first;\n        //     int col=top.second.second;\n        //     ans+=val;\n        //     col++;\n        //     if(col<piles[row].size()){\n        //         pq.push({piles[row][col],{row,col}});\n        //     }\n        // }\n        // return ans;\n        //so this greedy fails \n        //as, we are not given a fix order of elements\n        //applying dp, like taking any number of elements from one pile and then move to the next pile \n\n\n       vector<vector<int>>dp(k+1,vector<int>(piles.size()+1,-1));\n\n        return solve(piles,k,0,dp);\n    }\n};\n```
['C++']