Easy Game

You are playing a two player game. Initially there are n integer numbers in an array and player Aand 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 N space 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

题意:有一个长度为n的数列,两个人玩回合制游戏,没一个回合,每个人可以从数列的两端,任取一端,从那一端开始取一定数目的数,但是不能不取,两人最后的得分就是所取得序列的和。若两人都采取最优策略,问,先手的人比后手的人多得多少分。

解析:

  这是一道区间dp的题目,首先,我们用前缀和差分来求出各连续区间的和,

发布了21 篇原创文章 · 获赞 8 · 访问量 715

猜你喜欢

转载自blog.csdn.net/qq_17853613/article/details/83658778