Java Top Down dp
\nclass Solution {\n    \n    int dp[][] = new int[1003][1003];\n    \n    public int stoneGameVII(int[] S) {\n        int sum = 0;\n        for(int v : S) sum+
```\nclass Solution {\n    \n    int dp[][] = new int[1003][1003];\n    \n    public int stoneGameVII(int[] S) {\n        int sum = 0;\n        for(int v : S) sum+= v;\n        return solve(S , 0 , S.length - 1 , sum);\n    }\n    \n    public int solve(int[] S , int strt , int end , int sum) {\n        if(strt >= end) return 0;\n        \n        if(dp[strt][end] != 0) return dp[strt][end];\n        \n        int l = sum - S[strt] - solve(S , strt + 1 , end , sum - S[strt]);\n        int r = sum - S[end] - solve(S , strt , end - 1 , sum - S[end]);\n        \n        return dp[strt][end] = Math.max(l , r);\n    }\n}\n```
[]