You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains Nspace separated integers. You may assume that no number will contain more than 4 digits.

Output

For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input

2

4

4 -10 -20 7

4

1 2 3 4

Sample Output

Case 1: 7

Case 2: 10

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#define Inf 0x3f3f3f3f
const int maxn=1e5+;
typedef long long ll;
using namespace std; int dp[][];
int sum[];
int n;
int dfs(int l,int r)
{
if(dp[l][r]!=-Inf)
{
return dp[l][r];
}
if(l>r)
{
return ;
}
int ans=-Inf;
for(int t=;t<=n;t++)
{
if(l+t<=r+)
ans=max(ans,sum[l+t-]-sum[l-]-dfs(l+t,r));
}
for(int t=;t<=n;t++)
{
if(r-t>=l-)
ans=max(ans,sum[r]-sum[r-t]-dfs(l,r-t));
}
// cout<<l<<" "<<r<<" "<<ans<<endl;
return dp[l][r]=ans;
}
int main()
{
int T;
cin>>T;
int cnt=;
while(T--)
{
scanf("%d",&n);
for(int t=;t<=n;t++)
{
for(int j=;j<=n;j++)
{
dp[t][j]=-Inf;
}
}
memset(sum,,sizeof(sum));
int x;
for(int t=;t<=n;t++)
{
scanf("%d",&x);
dp[t][t]=x;
sum[t]=sum[t-]+x;
}
dfs(,n);
printf("Case %d: %d\n",cnt++,dp[][n]);
}
return ;
}

最新文章

  1. Visual Studio 2013执行项目报错:HTTP 错误 500.22
  2. Shader实例:边缘发光和描边
  3. Dev 关于用openFileDialog控件上传图片的问题
  4. 【ContextMenu】DataContext不自动更新
  5. 使用IntelliJ IDEA 编译开源的机器学习源码--Oryx
  6. db2 常用命令(一)
  7. js清除缓存方法
  8. POJ 2429 GCD &amp; LCM Inverse (Pollard rho整数分解+dfs枚举)
  9. paper 52 :windows7环境下theano安装
  10. SpringMvc多文件上传简单实现
  11. HTML 的 iframe 元素
  12. SQL 存储过程 通过多个ID更新数据 分类: SQL Server 2014-12-08 16:08 299人阅读 评论(0) 收藏
  13. Nuc900 bsp turbowriter使用注意点
  14. mysql (六)
  15. Centos 6.5使用vsftpd配置FTP服务器教程
  16. odoo TransientModels must have log_access turned on
  17. LeetCode题解之 Find Mode in Binary Search Tree
  18. 单机安装ELK
  19. git 的入门使用到团队协作
  20. css3 前端开发

热门文章

  1. TestFlight无法访问怎么办?TF如何链接AppStoreConnect解决教程
  2. resultMap的用法以及关联结果集映射
  3. 在Spring Boot中动态实现定时任务配置
  4. Scss 定义内层class的简单写法
  5. 自动化特征工程—Featuretools
  6. 分享一个Flink checkpoint失败的问题和解决办法
  7. Python基础入门知识点——深浅拷贝
  8. vue-cli的安装及版本查看/更新
  9. 记录一次idae和maven设置的巨坑
  10. 配置react / antd 按需加载 并且使用less(react v16)