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
Rafail Loizou: 2021-03-03 08:51:23

don't bother doing bottom-up

scolar_fuad: 2019-10-12 19:16:40

Min Max dp with recursion best dp for beginner

manoera262: 2018-05-23 19:52:31

test case:
1
12
12 34 345 11 34 435 9123 123 123 45 9 8
spoj toolkit gives WA =10072 9646
correct = 10079 9646

sherlock11: 2018-05-17 12:55:54

first of all read the the question carefully it says "even number of integers"...talking about cases for odd ' n' is irrelevant....and yes spojtoolkit gives wrong answer........happy coding : )

sinersnvrsleep: 2018-04-11 20:21:16

it should not be written in the question that there can be more than one answer when the other player is dumb

kocsarmate: 2017-10-17 23:36:23

The SPOJ Toolkit solution is currently wrong (2017.10.17).
It gives the correct answer for the "smart" case, but gives wrong answer for the "dumb" case.

burninggoku: 2017-09-20 13:01:57

2D dp or recursion ..both will work unlike my last relationship :(

kshubham02: 2017-08-23 08:55:44

Top-down is easier to think

anurag31: 2017-07-02 09:22:58

nice dp...must do...

kg93999: 2017-03-24 09:53:03

nzec in java
AC in c++ :(

Last edit: 2017-03-24 15:23:05

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)