Cake Game

시간 제한2초메모리 제한2048 MB

요약
Bessie는 인접한 케이크를 합치고 Elsie는 양 끝 케이크를 가져가는 게임에서 두 소가 최적으로 두었을 때 각자 먹는 양을 구한다.
난이도

어려움10점 중 8점

유형
게임 이론, 그리디, 동적 계획법, 조합론
정답자
아직 제출이 없습니다

문제

Bessie and Elsie have discovered a row of NN cakes (2≤N≤5⋅105,N even)(2 \leq N \leq 5\cdot 10^5, N \text{ even}) cakes, with sizes a_1,a_2,…,a_Na\_1,a\_2,\dots,a\_N in that order (1≤a_i≤1091\le a\_i\le 10^9).

Each cow wants to eat as much as possible. However, being very civilized cows, they decided to play a game to split it! The game proceeds with both cows alternating turns. Each turn consists of one of the following:

  1. Bessie chooses two adjacent cakes and stacks them, creating a new cake with the sum of the sizes.
  2. Elsie chooses either the leftmost or rightmost cake and puts it in her stash.

When only one cake remains, Bessie eats it, and Elsie eats all cakes in her stash. If both cows play optimally to maximize the amount of cake they eat and Bessie plays first, how much cake will each cow eat?

입력

Each input consists of TT (1≤T≤101\le T\le 10) independent test cases. It is guaranteed that the sum of all NN within an input does not exceed 10610^6.

Each test case is formatted as follows. The first line contains NN. The next line contains NN space-separated integers, a_1,a_2,…,a_Na\_1,a\_2,\ldots,a\_N.

출력

For each test case, output a line containing bb and ee, representing the amounts of cake Bessie and Elsie respectively will consume if both cows play optimally.

힌트

For the first test case, under optimal play,

  1. Bessie will stack the middle two cakes. The cakes now have sizes \[40,50,10]\[40,50,10].
  2. Elsie will eat the leftmost cake. The remaining cakes now have sizes \[50,10]\[50,10].
  3. Bessie stacks the remaining two cakes.

Bessie will eat 30+20+10=6030+20+10=60 cake, while Elsie will eat 4040 cake.

The second test case is the reverse of the first, so the answer is the same.

예제1

  1. 예제 1

    입력
    2
    4
    40 30 20 10
    4
    10 20 30 40
    
    예상 출력
    60 40
    60 40