[Java] Bottom Up DP
\nclass Solution {\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        int n = piles.size();\n        int[][] ans = new int[n+1][2001];
```\nclass Solution {\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        int n = piles.size();\n        int[][] ans = new int[n+1][2001];\n        Collections.sort(piles, (List<Integer> a, List<Integer> b) ->  b.size() - a.size());\n        for(int i = 1; i <= k; i++) {\n            for(int j = 1; j <= n; j++) {\n                int sizeOfPile = piles.get(j-1).size();\n                List<Integer> pile = piles.get(j-1);\n                int sum = 0;\n                ans[j][i] = ans[j-1][i];\n                for(int l = 1; l <= Math.min(i, sizeOfPile); l++) {\n                    // Take K from this pile + remaining from previous piles\n                    sum += pile.get(l-1);\n                    int rem = i - l;\n                    ans[j][i] = Math.max(ans[j][i], sum + ans[j-1][rem]);\n                }\n            }\n        }\n        \n        return ans[n][k];\n    }\n}\n```
['Dynamic Programming', 'Java']