<p>Alice and Bob play a game with piles of stones. There are an <strong>even</strong> number of piles arranged in a row, and each pile has a <strong>positive</strong> integer number of stones <code>piles[i]</code>.</p>

<p>The objective of the game is to end with the most stones. The <strong>total</strong> number of stones across all the piles is <strong>odd</strong>, so there are no ties.</p>

<p>Alice and Bob take turns, with <strong>Alice starting first</strong>. Each turn, a player takes the entire pile of stones either from the <strong>beginning</strong> or from the <strong>end</strong> of the row. This continues until there are no more piles left, at which point the person with the <strong>most stones wins</strong>.</p>

<p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins the game, or </em><code>false</code><em> if Bob wins</em>.</p>

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

<pre>
<strong>Input:</strong> piles = [5,3,4,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> 
Alice starts first, and can only take the first 5 or the last 5.
Say she takes the first 5, so that the row becomes [3, 4, 5].
If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points.
If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alice, so we return true.
</pre>

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

<pre>
<strong>Input:</strong> piles = [3,7,2,3]
<strong>Output:</strong> true
</pre>

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

<ul>
	<li><code>2 &lt;= piles.length &lt;= 500</code></li>
	<li><code>piles.length</code> is <strong>even</strong>.</li>
	<li><code>1 &lt;= piles[i] &lt;= 500</code></li>
	<li><code>sum(piles[i])</code> is <strong>odd</strong>.</li>
</ul>

class Solution:
    def log(self, dp, idx):
        for row in dp:
            print(' '.join(list(map(lambda x: str(x[idx]), row))))
        print()

    def stoneGame(self, piles: List[int]) -> bool:
        LEFT = 'LEFT'
        RIGHT = 'RIGHT'
        NONE = 'NONE'

        n = len(piles)

        if n == 2:
            return max(piles)

        # alice can only have access to even number of piles and bob only odd
        alice_dp = [[(NONE, 0) for _ in range(n)] for _ in range(n)]
        bob_dp = [[(NONE, 0) for _ in range(n)] for _ in range(n)]

        # if two piles are left alice will take the maximum
        for i in range(n - 1):
            alice_dp[i][i + 1] = (
                LEFT if piles[i] > piles[i + 1] else RIGHT,
                max(piles[i], piles[i + 1])
            )

        # if three stones are left bob will play maxmin
        for i in range(n - 2):
            left_value = piles[i] + min(piles[i + 1], piles[i + 2])
            right_value = piles[i + 2] + min(piles[i], piles[i + 1])

            bob_dp[i][i + 2] = (
                LEFT if left_value > right_value else RIGHT,
                max(left_value, right_value)
            )
        
        # fill the dp from 4 sized piles and up
        for i in range(3, n):
            for j in range(n - i):
                # alice's turn
                if i % 2 == 1:
                    if_left_bob_dir = bob_dp[j + 1][j + i][0]
                    if_left_alice_val = piles[j] + (
                        alice_dp[j + 2][j + i][1] if if_left_bob_dir == LEFT else alice_dp[j + 1][j + i - 1][1]
                    )

                    if_right_bob_dir = bob_dp[j][j + i - 1][0]
                    if_right_alice_val = piles[j + i] + (
                        alice_dp[j + 1][j + i - 1][1] if if_right_bob_dir == LEFT else alice_dp[j][j + i - 2][1]
                    )

                    alice_dp[j][j + i] = (
                        LEFT if if_left_alice_val > if_right_alice_val else RIGHT,
                        max(if_left_alice_val, if_right_alice_val)
                    )
                # bob's turn
                else:
                    if_left_alice_dir = alice_dp[j + 1][j + i][0]
                    if_left_bob_val = piles[j] + (
                        bob_dp[j + 2][j + i][1] if if_left_alice_dir == LEFT else bob_dp[j + 1][j + i - 1][1]
                    )

                    if_right_alice_dir = alice_dp[j][j + i - 1][0]
                    if_right_bob_val = piles[j + i] + (
                        bob_dp[j + 1][j + i - 1][1] if if_right_alice_dir == LEFT else bob_dp[j][j + i - 2][1]
                    )

                    bob_dp[j][j + i] = (
                        LEFT if if_left_bob_val > if_right_bob_val else RIGHT,
                        max(if_left_bob_val, if_right_bob_val)
                    )
        
        alice_result = alice_dp[0][n - 1][1]

        bob_result = bob_dp[0][n - 2][1] if alice_dp[0][n - 1][0] == RIGHT else bob_dp[1][n - 1][1]

        return alice_result > bob_result

56164