Do this thing if your Python DP O(n3) solution TLE
You can also easy find O(N3) DP solutions and explanation in top votes post. With constraint N = 500, it should work well but when I submit, my solution TLE, I 
You can also easy find O(N3) DP solutions and explanation in top votes post. With constraint N = 500, it should work well but when I submit, my solution TLE, I also checked some other python solution on top votes post, TLE too. \nTo overcome TLE, you can check the edge case when all elements in array are the same:\n```\nif len(nums) == 1:\n\treturn nums[0]\n\nif len(set(nums)) == 1:  # edge case when all elements are the same\n\treturn nums[0] ** 3 * (N - 2) + nums[0] ** 2 + nums[0]\n```\n\nFull demo code:\n```\nclass Solution:\n    def maxCoins(self, nums: List[int]) -> int:\n        N = len(nums)\n        if N == 1:\n            return nums[0]\n        \n        if len(set(nums)) == 1:  # edge case when all elements are the same\n            return nums[0] ** 3 * (N - 2) + nums[0] ** 2 + nums[0]\n        \n        nums = [1] + nums + [1]\n        dp = [[0 for _ in range (N + 2)] for _ in range (N + 2)]\n        \n        for window_size in range(1, N + 1):\n            for left in range (1, N - window_size + 2):\n                right = left + window_size - 1\n                for i in range (left, right + 1):\n                    dp[left][right] = max(dp[left][right], nums[left - 1] * nums[i] * nums[right + 1] + dp[left][i - 1] + dp[i + 1][right])\n                    \n        return dp[1][N]\n```
[]