Simple solution using recursion and memoization
\n\n# Code\n\nclass Solution\n{\npublic:\n  int sumi(int index, vector<vector<int>> &piles, int k, int n, vector<vector<int>> &dp)\n  {\n    if (index == n or k
\n\n# Code\n```\nclass Solution\n{\npublic:\n  int sumi(int index, vector<vector<int>> &piles, int k, int n, vector<vector<int>> &dp)\n  {\n    if (index == n or k == 0)\n      return 0;\n    if (dp[index][k] != -1)\n      return dp[index][k];\n    int notPick = sumi(index + 1, piles, k, n, dp);\n    int pick = 0, coins = k, sum = 0;\n    for (int i = 0; i < piles[index].size() and coins > 0; i++)\n    {\n      sum += piles[index][i];\n      coins--;\n      int x = sum + sumi(index + 1, piles, coins, n, dp);\n      pick = max(pick, x);\n    }\n    return dp[index][k] = max(pick, notPick);\n  }\n  int maxValueOfCoins(vector<vector<int>> &piles, int k)\n  {\n    int n = piles.size();\n    int index = 0;\n    vector<vector<int>> dp(n, vector<int>(k + 1, -1));\n    return sumi(index, piles, k, n, dp);\n  }\n};\n```
['Array', 'Dynamic Programming', 'C++', 'Java']