Java Solution Memoization
\nclass Solution {\n    \n    Integer [][]dp;\n    \n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        dp = new Integer[piles.size() +
```\nclass Solution {\n    \n    Integer [][]dp;\n    \n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        dp = new Integer[piles.size() + 1][k + 1];\n        return maxPoints(piles, piles.size() - 1, k);\n    }\n    \n        \n    private int maxPoints(List<List<Integer>>piles, int i, int k){\n\n        if(i < 0 || k <= 0) \n            return 0;\n\n        if(dp[i][k] != null) \n            return dp[i][k];\n\n        int notTake = maxPoints(piles, i-1, k);\n\n        int take = 0;\n        \n        for(int j=0, sum=0; j <  Math.min(piles.get(i).size(), k) ; j++){\n\n            sum += piles.get(i).get(j);\n\n            take = Math.max(sum + maxPoints(piles, i-1, k-j-1), take);\n        }\n\n        int res = Math.max(take, notTake);\n\n        dp[i][k] = res;\n        return res;\n    }\n}\n```\n\n#### BOTTOM UP\n```\nclass Solution {\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        \n        int n = piles.size();\n        int[][] dp = new int[n+1][k+1];\n\n        for(int i = 1; i <= n; i ++) {\n            for(int j = 1; j <= k; j ++) {\n                \n                dp[i][j] = dp[i-1][j];\n                int sum = 0;\n                \n                for(int l = 1 ; l <= Math.min(piles.get(i-1).size() , j) ; l++) {\n\n                    sum += piles.get(i - 1).get(l - 1);\n                    dp[i][j] = Math.max(dp[i][j], dp[i-1][j-l]+sum);\n                    \n                }\n            }\n        }\n        return dp[n][k];\n    }\n}\n```
['Dynamic Programming', 'Java']