#1338 : A Game
-
4 -1 0 100 2
Sample Output
-
99
Description
Little Hi and Little Ho are playing a game. There is an integer array in front of them. They take turns (Little Ho goes first) to select a number from either the beginning or the end of the array. The number will be added to the selecter's score and then be removed from the array.
Given the array what is the maximum score Little Ho can get? Note that Little Hi is smart and he always uses the optimal strategy.
Input
The first line contains an integer N denoting the length of the array. (1 ≤ N ≤ 1000)
The second line contains N integers A1, A2, ... AN, denoting the array. (-1000 ≤ Ai ≤ 1000)
Output
Output the maximum score Little Ho can get.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1003;
int s[maxn],dp[maxn][maxn],a[maxn];
int main()
{
int n;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
a[0]=-10000;
s[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s[i]=s[i-1]+a[i];
dp[i-1][i]=max(a[i],a[i-1]);
dp[i][i]=a[i];
}
for(int i=n;i>=1;i--)
for(int j=i+2;j<=n;j++)
dp[i][j]=s[j] - s[i - 1]+(dp[i+1][j]>dp[i][j-1]?-dp[i][j - 1]:-dp[i + 1][j]);
printf("%d\n",dp[1][n]);
return 0;
}