<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>

from itertools import accumulate


class Solution:
    def stoneGameVII(self, stones: List[int]) -> int:
        ps = [0, *accumulate(stones)]
        n = len(stones)
        # ops = [max, min]
        # sign = [1, -1]

        # MLE don't know reason
        # @cache
        # def dp(i, j):
        #     if i == j:
        #         return 0
        #     player = (i + n - j - 1) % 2 # n-(j-i)-1 减1调节奇偶性 0forA 1forB
        #     return ops[player](
        #         # to choose leftmost: score diff
        #         sign[player]*(ps[j+1]-ps[i+1])+dp(i+1,j),
        #         # to choose rightmost: score diff
        #         sign[player]*(ps[j]-ps[i])+dp(i,j-1)
        #     )

        # 稍微改写 二者质同
        # @cache
        # def dp(i: int, j: int) -> int:
        #     if i == j:
        #         return 0

        #     player = (i + n - j - 1) % 2

        #     left_gain = ps[j + 1] - ps[i + 1]
        #     right_gain = ps[j] - ps[i]

        #     if player == 0:  # A's turn (maximize)
        #         return max(left_gain + dp(i + 1, j), right_gain + dp(i, j - 1))
        #     else:  # B's turn (minimize)
        #         return min(dp(i + 1, j) - left_gain, dp(i, j - 1) - right_gain)
        # return dp(0, n - 1)

        # Tabulation
        # 回归到此系列前几个问题的那种交互使用dp数组的思想
        dp=[[0]*n for _ in range(n)]
        for i in range(n-2,-1,-1):
            for j in range(i+1,n):
                dp[i][j]=max(ps[j+1]-ps[i+1]-dp[i+1][j],
                    ps[j]-ps[i]-dp[i][j-1])
        return dp[0][n-1]
1592