1# Runtime: 6869 ms (Top 56.16%) | Memory: 13.9 MB (Top 96.15%)2class Solution:3def stoneGameVII(self, S: List[int]) -> int:4N, dp = len(S), [0] * len(S)5for i in range(N - 2, -1, -1):6total = S[i]7for j in range(i + 1, N):8total += S[j]9dp[j] = max(total - S[i] - dp[j], total - S[j] - dp[j - 1])10return dp[-1]