CODEM4 - Problem4


"Roadies" is back!!! In India, "Roadies" is a reality based tv show and is very popular among the youth.

This time to get selected into "Roadies" they challenge you to solve the given problem... if you can solve it you will be selected!

Challenge is as follows: Suppose you are given an array with even number of integers. Player 1 and player 2 take turns to pick numbers, either the left-most element or the right-most element. You have to find the maximum possible score (sum of numbers chosen) by player 1. Assume player 1 always starts the game and plays optimally.

Note: when an element is picked it is removed from the array.

But as usual this is "Roadies" hence cometh the twist. It is not known whether the second player is dumb or smart. As you know result will be different if player 2 is either dumb or smart.

NOTE: when player 2 plays like a dumb there is no fixed strategy of how he would choose the numbers i.e. he would choose the number either from start or end of an array without thinking... so there can be multiple answers possible when player 2 plays like a dumb...

So you have to tell the maximum possible score of player 1 when player 2 plays dumb and smart.

Input

T: number of test cases (t < 20).

For eact case two lines follow, first line contains the size of array (<= 30) and next line contains the elements of the array.

Output

For each case print in new line two space separated integers: score when player 2 is dumb and score when player 2 is smart.

Input:
2
4
5 8 4 2
4
8 5 4 2

Output:
13 10
13 12

NOTE: All values will fit in int type.

Explanation: In first test case (when player 2 is dumb) then player 1 first chooses 5 then player 2 can choose 8 or 2 but chooses 2. so now player 1 can choose either 8 or 4 so chooses 8. Hence total score is 13. 

(when player 2 is smart) player 1 can choose 5 or 2 but chooses 2 (so that player 2 cannot choose 8 in his turn) so now player 2 can choose 5 or 4. Irrespective of what player 2 chooses player 1 will have option to choose 8 in his next turn hence total (8 + 2) = 10.


hide comments
kejriwal: 2016-01-18 10:16:49

can anybody help with this case:
1
8
23 45 67 78 89 12 23 34
this one gives ans:
246 213
on spoj toolkit,
but,in the case where player2 is dumb,the ans can be 257 as mentioned below:
p1:23
p2:45
p1:67
p2:34
p1:78
p2:23
p1:89
p2:12

total score of p1 in case when p2 is dumb = 23+67+78+89=257
can anyone help ??

Last edit: 2016-01-18 12:56:41
Mayank Shah: 2016-01-14 21:49:23

Ashish Khurange
o/p will be 82 51

Ashish Khurange: 2015-10-23 16:25:57

What should be o/p for:

1
5
34 45 1 3 5

-> 82 40

OR

-> 84 40

Deepak Singh Tomar: 2015-09-02 20:46:53

great problem!

Gopinath Sankar: 2015-08-29 04:43:13

Can I get one more input case? I get a wrong answer and I need some help to identify it. I have long long.

Saksham : 2015-08-14 01:22:05

AC in one go! nice problem

anshal dwivedi: 2015-08-12 08:06:37

AC in one go..! after thinking a week..! and No need of long long @sumit paroothi all values fit in int type..:)

Sumit Paroothi: 2015-08-04 13:53:06

use long long for outputs:(

Scape: 2015-07-29 09:21:08

Got Accepted after using long long. "NOTE:All values will fit in int type." - seems very misleading. May be all input values will fit int range but not the output.

Alaf Azam Khan: 2015-06-27 22:28:05

My 100th. Nice problem.


Added by:Bhavik
Date:2014-02-04
Time limit:8.846s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:own problem(for CODE MARATHON)