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
aryan29: 2020-03-31 14:14:51

Wow I got 2WA due to that input format I was Printing game 1 in each game,easy dp ques otherwise

setha: 2020-03-24 19:02:51

if p=0,then what should be the my output return??

shubhamptw: 2020-02-11 23:47:08

declare array globally, if there's tle.

sanket17: 2020-01-22 08:41:03

i dont' know why this happen
approach 1 = when A[l]==a[h] i have always taken A[h] verdit =WA
approach 2 =when A[l]==A[h] i ahve takem both and done minimum of results verdict =WA
approch 3 when A[l]==A[h] i have taken A[l] verdict AC

ak8325: 2020-01-02 13:20:46

Please read last line of OUTPUT FORMAT carefully.. i ignored and got WA (don't use your personal instinct)

scolar_fuad: 2019-09-18 16:07:47

while check A[l]>A[r] please be conscious that A[l]>=A[r]

this consumed huge time for me >>
happy coding nice dp problem

kshubham02: 2019-08-21 21:11:39

Last edit: 2019-08-21 21:12:11
sagar_june97p: 2019-05-05 13:25:20

When comparing arr[left] and arr[right],
use condition arr[left] >= arr[right] instead of arr[left] > arr[right].
Cost me 2 WA.

hemant_dhanuka: 2018-05-26 09:43:07

One's who are getting TLE in java use, fast i/o

nadstratosfer: 2018-01-06 07:30:54

Idiotic TL not only making it near impossible to AC with Python, but making the same code AC and TLE depending on split-second lags due to server stability. I'd discourage beginners to get frustrated here, there are better problems on similar concept on this site where the constraints allow you to see what you're doing right.

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