#TP0000001. Sereja và Dima (Sereja and Dima)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sereja và Dima (Sereja and Dima)
Sereja and Dima
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Sereja and Dima play a game using numbered cards. Initially, there are cards arranged in a row from left to right. The card at position has a positive integer written on it. All card values are distinct.
The players take turns until no cards remain. Sereja moves first, followed by Dima, then Sereja again, and so on. On each turn, a player must take exactly one card from either the left end or the right end of the remaining row. A removed card is never returned.
Both players follow the same rule: always take the card with the larger value among the two end cards. When only one card remains, the player whose turn it is takes that card.
Each player's score is the sum of the values written on all cards they have taken. Determine Sereja's and Dima's final scores after the game ends.
Input
- The first line contains an integer , the initial number of cards.
- The second line contains exactly integers , the card values in left-to-right order.
Output
Print two integers on a single line, separated by a space: Sereja's score first, then Dima's score.
Subtasks
- Subtask 1 (20 points): ; for every ; all are pairwise distinct.
- Subtask 2 (30 points): ; for every ; all are pairwise distinct.
- Subtask 3 (50 points): ; for every ; all are pairwise distinct.
Examples
Input
4
4 1 2 10
Output
12 5
Explanation
Initially, the four cards have values from left to right.
- On turn 1, Sereja compares the end cards and and takes . The remaining row is ; Sereja has points.
- On turn 2, Dima compares and and takes . The remaining row is ; Dima has points.
- On turn 3, Sereja compares and and takes . The remaining row is ; Sereja has points.
- On turn 4, Dima takes the last card, worth . Dima's final score is .
The row is now empty. Thus, the final scores, in the required order, are 12 5.