<p>Alice and Bob continue their games with piles of stones. There are a number of piles <strong>arranged in a row</strong>, and each pile has a positive integer number of stones <code>piles[i]</code>. The objective of the game is to end with the most stones.</p>

<p>Alice and Bob take turns, with Alice starting first.</p>

<p>On each player&#39;s turn, that player can take <strong>all the stones</strong> in the <strong>first</strong> <code>X</code> remaining piles, where <code>1 &lt;= X &lt;= 2M</code>. Then, we set <code>M = max(M, X)</code>. Initially, M = 1.</p>

<p>The game continues until all the stones have been taken.</p>

<p>Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">piles = [2,7,9,4,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">10</span></p>

<p><strong>Explanation:</strong></p>

<ul>
	<li>If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2 piles again. Alice can get <code>2 + 4 + 4 = 10</code> stones in total.</li>
	<li>If Alice takes two piles at the beginning, then Bob can take all three piles left. In this case, Alice get <code>2 + 7 = 9</code> stones in total.</li>
</ul>

<p>So we return 10 since it&#39;s larger.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">piles = [1,2,3,4,5,100]</span></p>

<p><strong>Output:</strong> <span class="example-io">104</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= piles.length &lt;= 100</code></li>
	<li><code>1 &lt;= piles[i]&nbsp;&lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
  // pos=0
  vector<unordered_map<int, pair<int, int>>> a_cache_;
  vector<unordered_map<int, pair<int, int>>> b_cache_;

  pair<int, int> Dfs(bool Alice_turn, int pos, int M, const vector<int> &piles) {
    // current people may choose 1 ~ 2M piles
    // If no piles, no one can get anything.
    if (pos == piles.size())
      return {0, 0};
    assert(pos < piles.size());

    if (Alice_turn) {
      if (a_cache_[pos].contains(M))
        return a_cache_[pos][M];
    }
    else {
      if (b_cache_[pos].contains(M))
        return b_cache_[pos][M];
    }
    
    int max_fetch_pile = min((int)piles.size() - pos, 2 * M);
    int max_a = 0, max_b = 0;
    int collect_stone = 0;
    for (int i = 1; i <= max_fetch_pile; ++i) {
      collect_stone += piles[pos + i - 1];
      // current fetch i piles
      //printf("pos%d: %s collect %d stones\n", pos, Alice_turn ? "Alice" : "Bob", collect_stone);
      auto [a, b] = Dfs(!Alice_turn, pos+i, max(M, i), piles);
      //printf("pos%d: (A got %d, B got %d)\n", pos, a, b);
      //printf("pos%d: %s collect %d stones (A got %d, B got %d)\n", pos, Alice_turn ? "Alice" : "Bob", collect_stone, a, b);
      if (Alice_turn) {
        a += collect_stone;
        if (a > max_a) {
          max_a = a;
          max_b = b;
        }
      }
      else {
        b += collect_stone;
        if (b > max_b) {
          max_b = b;
          max_a = a;
        }
      }
    }

    if (Alice_turn)
      a_cache_[pos][M] = {max_a, max_b};
    else
      b_cache_[pos][M] = {max_a, max_b};
    return {max_a, max_b};
  }

  int stoneGameII(vector<int>& piles) {
    a_cache_.resize(piles.size());
    b_cache_.resize(piles.size());
    auto [a, b] = Dfs(true, 0, 1, piles);
    return a;
  }
};
22993