dp
f[i][j] = max(f[i + 1][j] + sum(i + 1,j) + v[i],f[i][j - 1] + sum(i,j - 1) + v[j])
View Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define maxn 2005 int n; int v[maxn]; int f[maxn][maxn]; int sum[maxn]; void input() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &v[i]); v[0] = 0; sum[0] = v[0]; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + v[i]; } int work() { for (int i = 1; i <= n; i++) f[i][i] = v[i]; for (int k = 1; k < n; k++) { for (int i = 1; i <= n - k; i++) { int j = i + k; f[i][j] = max(f[i + 1][j] + (sum[j] - sum[i]) + v[i],f[i][j - 1] + (sum[j - 1] - sum[i - 1]) + v[j]); } } return f[1][n]; } int main() { // freopen("t.txt", "r", stdin); input(); printf("%d\n", work()); return 0; }