2218. Maximum Value of K Coins From Piles || Java
\nclass Solution {\n    public int solve (List<List<Integer>> piles,int i,int k,int [][] dp )\n    {\n        if(i>=piles.size())\n            return 0;\n      
```\nclass Solution {\n    public int solve (List<List<Integer>> piles,int i,int k,int [][] dp )\n    {\n        if(i>=piles.size())\n            return 0;\n        if(dp[i][k]!=-1)\n            return dp[i][k];\n        int max=0;\n            max=Math.max(max,solve(piles,i+1,k,dp));\n        for(int j=0;j<piles.get(i).size();j++)\n        {\n            if(j+1<=k)\n                max=Math.max(max,piles.get(i).get(j)+solve(piles,i+1,k-j-1,dp));\n            else\n                break;\n        }\n        return dp[i][k]=max;\n    }\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        for(int i=0;i<piles.size();i++)\n        {\n         \n         for(int j=1;j<piles.get(i).size();j++)\n            {   \n                int removed=  piles.get(i).get(j);\n                removed+=  piles.get(i).get(j-1);\n                piles.get(i).set(j,removed); \n            } \n        }\n        int n=piles.size();\n        int [][]dp=new int [n+1][k+1];\n        for(int x[]:dp)\n            Arrays.fill(x,-1);\n        return solve(piles,0,k,dp);\n    }\n}\n```
[]