Most detailed explanation in O(n*n)
A very similar problem arrived at "atcoder educational dp contest" named deque.\n\nBoth the problems as just a small changeand things will work fine. Please ref
A very similar problem arrived at **"atcoder educational dp contest"** named deque.\n\nBoth the problems as just a small changeand things will work fine. Please refer this problem **ATcoder L Deque**.\n\nIt\'s full detailed solution in on [CP-central App](https://play.google.com/store/apps/details?id=com.central.cp&hl=en_US&gl=US://) (along with other atcoder dp contest editorials).\n\n```\nint F(int L, int R,int player) {\n        //..base case when there is only one element\n        if (L == R) {\n          return 0;  //..nothing to add to the score of current player\n        }\n\n        //..if value already exists return directly\n        if (dp[L][R][player] != -1) return dp[L][R][player];\n\n\n        int val = 0;\n        if(player == 0){ //..if it\'s player 0\'s turn\n          int sum = pre[R] -((L == 0) ? 0 :pre[L-1]);\n          int left = F(L+1,R,1) + (sum - a[L]);  <----------\n          int right = F(L,R-1,1) + (sum - a[R]);  <----------\n          val = Math.max(left, right);\n        }\n        else{\n          int sum = pre[R] - ((L == 0) ? 0 :pre[L-1]);\n          int left = F(L+1,R,0) - (sum - a[L]);  <----------\n          int right = F(L,R-1,0) - (sum - a[R]);   <----------\n          val = Math.min(left, right);\n        }\n\n        dp[L][R][player] = val;\n        return val;\n  }\n```\n\n# The only difference is we add the stone\'s value to player\'s score in Atcoder problem where as in this problem we add the sum of rest of the stones excluding the one he selected\n
[]