2218.cpp (540B)
1 class Solution { 2 int dp[1001][2001] = {0}; 3 4 public: 5 int maxValueOfCoins(const vector<vector<int>> &piles, int left, int pile = 0) { 6 if (!left || pile >= piles.size()) return 0; 7 if (dp[pile][left]) return dp[pile][left]; 8 9 int res = maxValueOfCoins(piles, left, pile + 1), sum = 0; 10 for (int i = 0; i < piles[pile].size() && i < left; i++) { 11 res = max(res, (sum += piles[pile][i]) + maxValueOfCoins(piles, left - i - 1, pile + 1)); 12 } 13 14 return dp[pile][left] = res; 15 } 16 };