Card Game
InterviewTime limit1sMemory limit256 MB
Both players alternately take the left or right end card to maximize their own sum, and you compute the first player best total score.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Game theory
- Solved
- No attempts yet
Problem
Geunwoo and Myungwoo play a card game. cards lie in a row, and each card has one number written on it. Geunwoo goes first, and the two players alternate turns. On a turn, a player can take either the leftmost card or the rightmost card of the row. The turns repeat until no card is left. Each player's score is the sum of the numbers on the cards that player took.
Both players play the strategy that maximizes their own score. Given the number of cards and the number on each card, write a program that computes Geunwoo's score.
For example, suppose the cards read 4, 3, 1, 2 from the left. Geunwoo takes 4 and Myungwoo takes 3. Then Geunwoo takes 2 and Myungwoo takes the last card, 1. With both playing their best strategy, Geunwoo's score is 6.
Input
The first line has the number of test cases ().
For each test case, the first line has the number of cards (). The second line has natural numbers separated by spaces, where the -th number is written on the -th card from the left. Every number on a card is between and .
Output
For each test case, print Geunwoo's score when both players play their best strategy, one score per line.