[Java] Top Down DP
Logic\nYou have n piles\nSo you can take from ith pile min = 0 coins and atmost max(sizeOf(pile), k) \nWe can do it with dynamic programming using dp table\n\n\
**Logic**\nYou have n piles\nSo you can take from `ith` pile min = 0 coins and atmost max(sizeOf(pile), k) \nWe can do it with dynamic programming using dp table\n\n\n```\nclass Solution {\n    Integer dp[][]; // store state\n    private int solve(List<List<Integer>>piles, int i, int k){\n        // if i < 0 there are no piles or \n        // k <= 0 you cannnot pick any \n        // return 0\n        if(i < 0 || k <= 0) return 0; \n        // if already stored that state return value\n        if(dp[i][k] != null) return dp[i][k];\n        // you can take max(size of that pile, k)\n        int n = Math.min(piles.get(i).size(), k);\n        // you will not choose that pile\n        int exclude = solve(piles, i-1, k);\n        // you will take 1, 2, ... k from that pile\n        // and store the max in \'include\' variable\n        int include = 0;\n        for(int j=0, sum=0; j<n; j++){\n            // store the sum from 0th index to jth index\n            sum += piles.get(i).get(j);\n            // take max from (sum + remaining k coins) and max value \n            include = Math.max(sum + solve(piles, i-1, k-j-1), include);\n        }\n        // take max of include, exclude\n        int res = Math.max(include, exclude);\n        // store in dp\n        dp[i][k] = res;\n        return res;\n    }\n    \n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        int n=piles.size();\n        dp = new Integer[n+5][k+5];\n        return solve(piles, piles.size()-1, k);\n    }\n}\n```
['Dynamic Programming', 'Memoization', 'Java']