DP
Code\n\nclass Solution {\npublic:\n    int solve(int r, vector<vector<int>> &piles, int k, vector<vector<int>> &dp)\n{\n    if (r == piles.size())\n    {\n     
# Code\n```\nclass Solution {\npublic:\n    int solve(int r, vector<vector<int>> &piles, int k, vector<vector<int>> &dp)\n{\n    if (r == piles.size())\n    {\n        if(k == 0)\n        {\n            return 0;\n        }\n        return -1e9;\n    }\n    \n    if(dp[r][k] != -1)\n    {\n        return dp[r][k];\n    }\n       \n    int ans = solve(r + 1, piles, k, dp);\n    int temp = 0;\n\n    for (int i = 0; i < min((int)piles[r].size(), k); i++)\n    {\n        temp += piles[r][i];\n        ans = max(ans, temp + solve(r + 1, piles, k - i - 1, dp));\n    }\n\n    return dp[r][k] = ans;\n}\n\nint maxValueOfCoins(vector<vector<int>> &piles, int k)\n{\n    int n = piles.size();\n    vector<vector<int>> dp(n, vector<int>(k + 1, -1));\n    return solve(0, piles, k, dp);\n}\n};\n```
['C++']