Ruby || recursion + memoization
Intuition\nTry (1..k) from the first pile and calculate max for the rest with new k = k - tried, and so on\n\n# Approach\nReworked translation to Ruby of aryan_
# Intuition\nTry (1..k) from the first pile and calculate max for the rest with new k = k - tried, and so on\n\n# Approach\nReworked translation to Ruby of [aryan_0077 C++ solution](https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/solutions/3417959/image-explanation-top-down-dp-easy-concise-c-java-python/) (same idea, technical details taken from his solution)\n\n# Complexity\n- Time complexity:\n$$O(n*k)$$\n\n- Space complexity:\n$$O(n*k)$$\n\n# Code\n```ruby\n# Reworked translation to Ruby of aryan_0077 C++ solution\n\nclass Solver\n    attr_reader :piles, :k, :dp\n\n    def initialize(piles, k)\n        @piles, @k = piles, k\n        @dp = Array.new(piles.size + 1) {|i| Array.new(k + 1) }\n    end\n\n    def solve(i = 0, kk = k)\n        cur = 0 # If cur is included in inject() param below then TLE\n        @dp[i][kk] ||= (i >= piles.size || !kk.positive?) ? \n            0 :\n            [piles[i].size, kk].min.times.inject(solve(i + 1, kk)) {|res, j|\n                cur += piles[i][j]\n                [res, cur + solve(i + 1, kk - j - 1)].max\n            }\n    end\nend\n\ndef max_value_of_coins(piles, k)\n    Solver.new(piles, k).solve\nend\n\n```
['Recursion', 'Memoization', 'Ruby']