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

// Idea: DP:
// let f[p][i][j] be the result of subproblem A[i...j] when the active player is
// p, p in [0,1].
// then the DP/Bellman equation is:
// f[p][i][j] = max of: sum(A[i+1 ... j]) - f[1-p][i+1][j],
//                      sum(A[i ... j-1]) - f[1-p][i][j-1].
// complexity: O(2 * 2 n^2) = O(n^2)
class Solution {
    constexpr static const int Inf = INT_MIN;

public:
    int stoneGameVII(vector<int>& stones) {
        const int n = stones.size();
        vector<vector<vector<int>>> f(
            2, vector<vector<int>>(n, vector<int>(n, Inf)));
        vector<int> psum(n);
        partial_sum(stones.begin(), stones.end(), psum.begin());

        return solve(psum, f, 0, 0, n - 1);
    }

private:
    static int solve(const vector<int>& psum, vector<vector<vector<int>>>& f,
                     int player, int i, int j) {
        if (f[player][i][j] != Inf) { // look it up in memo
            return f[player][i][j];
        }
        if (i > j) {
            return 0;
        }
        if (i == j) {
            return f[player][i][j] = 0;
        }
        // sum(A[i...j]) = psum[j] - psum[i-1];
        return f[player][i][j] =
                   max(psum[j] - psum[i] - solve(psum, f, 1 - player, i + 1, j),
                       psum[j - 1] - (i - 1 >= 0 ? psum[i - 1] : 0) -
                           solve(psum, f, 1 - player, i, j - 1));
    }
};
172740