# NYOJ 1030Yougth's Game[Ⅲ] 记忆化搜索

## Yougth's Game[Ⅲ]

3
1 2 3
4
2 4 5 3

2
0

Yougth原创

TC_杨闯亮

#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 1005;

int dp[maxn][maxn];
int n,a[maxn],sum[maxn];

int dfs(int s,int t)
{
if (dp[s][t]!=-1) return dp[s][t];
if (s>t) return 0;
if (s==t) return dp[s][t]=a[s];
int x=min(dfs(s+1,t),dfs(s,t-1));
dp[s][t]=sum[t]-sum[s-1]-x;
return dp[s][t];
}

int main()
{
int i,j;
while (~scanf("%d",&n))
{
memset(sum,0,sizeof(sum));
for (i=1;i<=n;i++) scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];
memset(dp,-1,sizeof(dp));
printf("%d\n",dfs(1,n)*2-sum[n]);
}
return 0;
}


#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 1005;

int dp[maxn][maxn];
int n,a[maxn],sum[maxn];

int main()
{
int i,j;
while (~scanf("%d",&n))
{
memset(sum,0,sizeof(sum));
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
memset(dp,0,sizeof(dp));
for (int k=0;k<n;k++)
{
for (i=1;i+k<=n;i++)
{
j=i+k;
dp[i][j]=sum[j]-sum[i-1]-min(dp[i+1][j],dp[i][j-1]);
}
}
printf("%d\n",dp[1][n]*2-sum[n]);
}
return 0;
}