第一感觉就是反过来模拟最优决策。
于是设 $dp_{i,j}$ 表示剩下 $[i,j]$ 的答案。
先手要使答案更大,后手要使答案更小,所以根据长度奇偶性判断是谁操作,枚举队首或队尾即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 3015;
int n, a[N];
long long dp[N][N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) dp[i][j] = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), dp[i][i] = (n & 1) ? a[i] : -a[i];
for (int len = 2; len <= n; len++) {
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1;
if ((n - len + 1) & 1)
dp[i][j] = max(dp[i + 1][j] + a[i], dp[i][j - 1] + a[j]);
else
dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
}
}
printf("%lld\n", dp[1][n]);
return 0;
}