[Java/C++/Python] Top-down DP solution
Intuition\nTop down dynamic programming.\nAlso noticed that some people already get accepted, a dp program.\n\n\n# Explanation\ndp[i,k] means picking k elements
# **Intuition**\nTop down dynamic programming.\nAlso noticed that some people already get accepted, a dp program.\n<br>\n\n# **Explanation**\n`dp[i,k]` means picking `k` elements from `pile[i]` to `pile[n-1]`.\nWe can pick 0,1,2,3... elements from the current `pile[i]` one by one.\nIt asks for the maximum total value of coins we can have,\nso we need to return `max` of all the options.\n<br>\n\n# **Complexity**\nTime `O(nm)`\nSpace `O(nk)`\nwhere `m = sum(piles[i].length) <= 2000`\n<br>\n\n**Java**\nFrom @ssmegentle\n```java\n    public int maxValueOfCoins(List<List<Integer>> piles, int k) {\n        Integer[][] memo = new Integer[piles.size() + 1][k + 1];\n\n        return dp(piles, memo, 0, k);\n    }\n    public int dp(List<List<Integer>> piles, Integer[][] memo, int i, int k) {\n        if (k == 0 || i == piles.size()) return 0;\n        if (memo[i][k] != null) return memo[i][k];\n\n        int res = dp(piles, memo, i + 1, k);\n        int cur = 0;\n\n        for (int j = 0; j < Math.min(piles.get(i).size(), k); ++j) {\n            cur += piles.get(i).get(j);\n            res = Math.max(res, cur + dp(piles, memo, i + 1, k - j - 1));\n        }\n        return memo[i][k] = res;\n    }\n```\n**C++**\n```cpp\n    int maxValueOfCoins(vector<vector<int>>& A, int K) {\n        int n = A.size();\n        vector<vector<int>> memo(n + 1, vector<int>(K + 1, 0));\n        function<int(int, int)> dp = [&](int i, int k) {\n            if (memo[i][k] > 0) return memo[i][k];\n            if (i == n || k == 0) return 0;\n            int res = dp(i + 1, k), cur = 0;\n            for (int j = 0; j < A[i].size() && j < k; ++j) {\n                cur += A[i][j];\n                res = max(res, dp(i + 1, k - j - 1) + cur);\n            }\n            memo[i][k] = res;\n            return res;\n        };\n        return dp(0, K);\n    }\n```\n**Python**\n```py\n    def maxValueOfCoins(self, A, K):\n        \n        @functools.lru_cache(None)\n        def dp(i, k):\n            if k == 0 or i == len(A): return 0\n            res, cur = dp(i + 1, k), 0\n            for j in range(min(len(A[i]), k)):\n                cur += A[i][j]\n                res = max(res, cur + dp(i+1, k-j-1))\n            return res\n        \n        return dp(0, K)\n```\n
['C', 'Python', 'Java']