Buggy python submission (verdict: Time Limit Exceeded):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
cnt = 0
for i in range(1, N+1):
	for l in range(N-i+1):
		cnt += 1
		r = l+i
		if (N-i)%2: #後手
			dp[l][r] = min(dp[l+1][r]-a[l], dp[l][r-1]-a[r-1])
		else: #先手
			dp[l][r] = max(dp[l+1][r]+a[l], dp[l][r-1]+a[r-1])

print(dp[0][N])


Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
cnt = 0
for i in range(1, N+1):
	for l in range(N-i+1):
		cnt += 1
		r = l+i
		if (N-i)%2: #後手
			dp[l][r] = min(dp[l+1][r]-a[l], dp[l][r-1]-a[r-1])
		else: #先手
			dp[l][r] = max(dp[l+1][r]+a[l], dp[l][r-1]+a[r-1])

print(dp[0][N])

Buggy python submission (verdict: Time Limit Exceeded):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
cnt = 0
for i in range(1, N+1):
	for l in range(N-i+1):
		cnt += 1
		r = l+i
		if (N-i)%2: #後手
			dp[l][r] = min(dp[l+1][r]-a[l], dp[l][r-1]-a[r-1])
		else: #先手
			dp[l][r] = max(dp[l+1][r]+a[l], dp[l][r-1]+a[r-1])

print(dp[0][N])


Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
cnt = 0
for i in range(1, N+1):
	for l in range(N-i+1):
		cnt += 1
		r = l+i
		if (N-i)%2: #後手
			dp[l][r] = min(dp[l+1][r]-a[l], dp[l][r-1]-a[r-1])
		else: #先手
			dp[l][r] = max(dp[l+1][r]+a[l], dp[l][r-1]+a[r-1])

print(dp[0][N])

Buggy python submission (verdict: Time Limit Exceeded):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
flg = [[0]*lim for _ in range(lim)]

def DFS(i, j):
	if flg[i][j]:
		return dp[i][j]

	if i+1 == j:  #最後の一手
		flg[i][j] = 1
		if N%2:  #先手
			dp[i][j] = a[i]
			return dp[i][j]
		else:    #後手
			dp[i][j] = -a[i]
			return dp[i][j]
	else:
		if (N-(j-i))%2:  #後手
			dp[i][j] = min(DFS(i+1, j)-a[i], DFS(i, j-1)-a[j-1])
			flg[i][j] = 1
			return dp[i][j]
		else:  #先手
			dp[i][j] = max(DFS(i+1, j)+a[i], DFS(i, j-1)+a[j-1])
			flg[i][j] = 1
			return dp[i][j]


print(DFS(0, N))



Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
cnt = 0
for i in range(1, N+1):
	for l in range(N-i+1):
		cnt += 1
		r = l+i
		if (N-i)%2: #後手
			dp[l][r] = min(dp[l+1][r]-a[l], dp[l][r-1]-a[r-1])
		else: #先手
			dp[l][r] = max(dp[l+1][r]+a[l], dp[l][r-1]+a[r-1])

print(dp[0][N])

Buggy python submission (verdict: Time Limit Exceeded):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
flg = [[0]*lim for _ in range(lim)]

def DFS(i, j):
	if flg[i][j]:
		return dp[i][j]

	if i+1 == j:  #最後の一手
		flg[i][j] = 1
		if N%2:  #先手
			dp[i][j] = a[i]
			return dp[i][j]
		else:    #後手
			dp[i][j] = -a[i]
			return dp[i][j]
	else:
		if (N-(j-i))%2:  #後手
			dp[i][j] = min(DFS(i+1, j)-a[i], DFS(i, j-1)-a[j-1])
			flg[i][j] = 1
			return dp[i][j]
		else:  #先手
			dp[i][j] = max(DFS(i+1, j)+a[i], DFS(i, j-1)+a[j-1])
			flg[i][j] = 1
			return dp[i][j]


print(DFS(0, N))



Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
a = LIST()

lim = 3001
dp = [[0]*lim for _ in range(lim)]
cnt = 0
for i in range(1, N+1):
	for l in range(N-i+1):
		cnt += 1
		r = l+i
		if (N-i)%2: #後手
			dp[l][r] = min(dp[l+1][r]-a[l], dp[l][r-1]-a[r-1])
		else: #先手
			dp[l][r] = max(dp[l+1][r]+a[l], dp[l][r-1]+a[r-1])

print(dp[0][N])