[Java/C++/Python]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022
Java\n\nclass Solution {\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        int n=piles.size();\n        int[] dp=new int[k+1];// k st
***Java***\n```\nclass Solution {\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        int n=piles.size();\n        int[] dp=new int[k+1];// k steps dynamic programming;\n        // for each pile to do dp.\n        for(List<Integer> pile:piles){\n            //use prefix sum to record each pile\'s first i elments;\n            int m=pile.size();\n            int[] cum=new int[m+1];\n            for(int i=0;i<m;i++) cum[i+1]=cum[i]+pile.get(i);\n            //use current pile to do dp, the dp is\n            // we have two pile previous pile\'s dp result and current pile--> reduce to two pile problem.\n            // pile 0, 1 ,2 ,3 , 4, ...\n            // pile (0,1),2,3,4...\n            //      (0,1) is dp result for next calculation.\n            // pile (0,1,2),3,4...\n            // ...\n            // pile (0,1,2,3,4... )\n            int[] curdp=new int[k+1];\n            for(int i=0;i<=k;i++){\n                for(int j=0;j<=m&& i+j <=k;j++){\n                    curdp[i+j]=Math.max(curdp[i+j],dp[i]+cum[j]);\n                }\n            }\n            dp=curdp;\n        }\n        return dp[k];\n    }\n}\n```\n\n***C++***\n```\nclass Solution {\npublic:\n   int dp[1001][2001]; //Dp array For Memoization.\n   int solve(vector<vector<int>>&v,int index,int coin)\n   {\n       if(index>=v.size()||coin==0)  //Base Condition\n           return 0;\n       if(dp[index][coin]!=-1) //Check wheather It is Already Calculated Or not.\n           return dp[index][coin];\n       \n       \n       /* Our 1st choice :- We not take any Coin from that pile*/\n       int ans=solve(v,index+1,coin); //Just Call function for next Pile.\n       \n      \n       /*Otherwise we can take Coins from that Pile.*/\n       int loop=v[index].size()-1;\n       int sum=0;\n       \n       for(int j=0;j<=min(coin-1,loop);j++) //\n       {\n           sum=sum+v[index][j];\n           ans=max(ans,sum+solve(v,index+1,coin-(j+1)));\n           \n           /*Aove we Pass coin-(j+1). Because till j\'th index we have taken j+1 coin from that pile.*/\n       }\n       \n       return dp[index][coin]=ans;\n   }\n   int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n       memset(dp,-1,sizeof(dp));\n       return solve(piles,0,k);\n   }\n};\n```\n\n***Python***\n```\nimport numpy as np\nclass Solution:\n    def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int:\n        best = np.zeros(k+1)\n        for pile in piles:\n            temp = best.copy()\n            for used, value in enumerate(accumulate(pile), 1):\n                if used > k: break\n                temp2 = best+value\n                temp2 = np.concatenate((np.zeros(used), temp2[:-used]))\n                temp = np.maximum(temp, temp2)\n            best = temp\n        return int(best[-1])\n```\n\n***Consider upvote if useful!***
['C', 'Combinatorics', 'Python', 'C++', 'Java']