<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p>

<p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong> of the remaining stones&#39; values in the row. The winner is the one with the higher score when there are no stones left to remove.</p>

<p>Bob found that he will always lose this game (poor Bob, he always loses), so he decided to <strong>minimize the score&#39;s difference</strong>. Alice&#39;s goal is to <strong>maximize the difference</strong> in the score.</p>

<p>Given an array of integers <code>stones</code> where <code>stones[i]</code> represents the value of the <code>i<sup>th</sup></code> stone <strong>from the left</strong>, return <em>the <strong>difference</strong> in Alice and Bob&#39;s score if they both play <strong>optimally</strong>.</em></p>

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

<pre>
<strong>Input:</strong> stones = [5,3,1,4,2]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 
- Alice removes 2 and gets 5 + 3 + 1 + 4 = 13 points. Alice = 13, Bob = 0, stones = [5,3,1,4].
- Bob removes 5 and gets 3 + 1 + 4 = 8 points. Alice = 13, Bob = 8, stones = [3,1,4].
- Alice removes 3 and gets 1 + 4 = 5 points. Alice = 18, Bob = 8, stones = [1,4].
- Bob removes 1 and gets 4 points. Alice = 18, Bob = 12, stones = [4].
- Alice removes 4 and gets 0 points. Alice = 18, Bob = 12, stones = [].
The score difference is 18 - 12 = 6.
</pre>

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

<pre>
<strong>Input:</strong> stones = [7,90,5,1,100,10,10,2]
<strong>Output:</strong> 122</pre>

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

<ul>
	<li><code>n == stones.length</code></li>
	<li><code>2 &lt;= n &lt;= 1000</code></li>
	<li><code>1 &lt;= stones[i] &lt;= 1000</code></li>
</ul>

class Solution:
    def stoneGameVII(self, stones: List[int]) -> int:    
        # https://github.com/doocs/leetcode/tree/main/solution/1600-1699/1690.Stone%20Game%20VII
        @cache
        def dfs(i: int, j: int) -> int:
            if i > j:
                return 0
            a = s[j + 1] - s[i + 1] - dfs(i + 1, j)
            b = s[j] - s[i] - dfs(i, j - 1)
            return max(a, b)

        s = list(accumulate(stones, initial=0))
        ans = dfs(0, len(stones) - 1)
        dfs.cache_clear()
        return ans

        @lru_cache(maxsize=None)
        def dp(left, right):
            if left > right: return 0
            choose_left = prefix_sums[right + 1] - prefix_sums[left + 1] - dp(left + 1, right)
            choose_right = prefix_sums[right] - prefix_sums[left] - dp(left, right - 1)

            return max(choose_left, choose_right)

        
        prefix_sums = [0] + list(accumulate(stones))
        dp.cache_clear()
        # Check if the first player's score is non-negative
        ans =  dp(0, len(stones) - 1) 
        dp.cache_clear()
        return ans

        # # Helper function that uses dynamic programming with memoization
        # @lru_cache(maxsize=None)  # Use lru_cache for memoization
        # def dp(low, high):
        #     # Base condition: if no stones are left
        #     if low > high:
        #         return 0
          
        #     # If Alice removes the stone at the low end, the new score is computed
        #     # from the remaining pile (low+1 to high) and current total score
        #     score_when_remove_low = prefix_sums[high + 1] - prefix_sums[low + 1] - dp(low + 1, high)
          
        #     # If Alice removes the stone at the high end, the new score is computed
        #     # from the remaining pile (low to high-1) and current total score
        #     score_when_remove_high = prefix_sums[high] - prefix_sums[low] - dp(low, high - 1)
          
        #     # Return the max score Alice can achieve from the current situation
        #     return max(score_when_remove_low, score_when_remove_high)
      
        # # Construct the prefix sums list where prefix_sums[i] is the sum of stones[0] to stones[i-1]
        # prefix_sums = [0] + list(accumulate(stones))
      
        # # Calculate the maximum score difference Alice can achieve by starting from the full size of the stones pile
        # answer = dp(0, len(stones) - 1)
      
        # # Clear the cache since it is no longer needed
        # dp.cache_clear()
      
        # return answer  # Return the answer



4007