TWENDS - Two Ends

In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)

3 2 10 4

You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.


There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.


For each test case you should print one line of output of the form:

In game m, the greedy strategy might lose by as many as p points.

where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player’s score and second player’s score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.


4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3

In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

hide comments
New_Bullet: 2013-12-31 11:07:27

5 1 2 1 2 1
For this test case, I'm getting -1 as solution... Shud it be left as such or shud the diffence be negated as +1???

Vijay Jain: 2013-07-30 07:42:00

interesting problem
@sagar gandhi answer for your test case is -1

crypt: 2013-01-27 09:21:53

in case of P2's move, i.e, When employing the greedy strategy if there is a tie, remove the left end, why ??

reasonable answer will be appreciated.

Lai Manh Tuan: 2013-01-21 16:40:37

interesting problem

Critical Information:
When employing the greedy strategy, always take the larger end. If there is a tie, REMOVE THE LEFT END.

Last edit: 2013-01-21 17:11:52
sagar gandhi: 2012-09-15 19:46:29

what will be the O/P for
5 1 2 1 2 1

Last edit: 2012-09-16 10:24:57
npsabari: 2012-06-13 18:18:11

Plain DP!
one hint : while solving sub problems, make both players move in one step of the loop.

The Champ: 2010-12-24 19:45:15

very nice problem. enjoyed solving it :)

numerix: 2010-10-04 13:10:35

You can disqualify it yourself.

Daniel Ampuero: 2010-10-04 05:21:18

Disqualify my solution in TEXT, I pasted the official output for checking if it was the same you used.

amit kumar: 2010-09-23 11:01:45

sry got it

Added by:Camilo Andrés Varela León
Time limit:0.100s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:East Central North America 2005