PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000001. Sereja và Dima (Sereja and Dima)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn cards arranged in a row from left to right. The card at position ii has a positive integer aia_i 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 nn, the initial number of cards.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, 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): 1≤n≤71\le n\le 7; 1≤ai≤10001\le a_i\le 1000 for every 1≤i≤n1\le i\le n; all aia_i are pairwise distinct.
  • Subtask 2 (30 points): 1≤n≤601\le n\le 60; 1≤ai≤10001\le a_i\le 1000 for every 1≤i≤n1\le i\le n; all aia_i are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; 1≤ai≤10001\le a_i\le 1000 for every 1≤i≤n1\le i\le n; all aia_i are pairwise distinct.

Examples

Input

4
4 1 2 10

Output

12 5

Explanation

Initially, the four cards have values 4,1,2,104,1,2,10 from left to right.

  • On turn 1, Sereja compares the end cards 44 and 1010 and takes 1010. The remaining row is [4,1,2][4,1,2]; Sereja has 1010 points.
  • On turn 2, Dima compares 44 and 22 and takes 44. The remaining row is [1,2][1,2]; Dima has 44 points.
  • On turn 3, Sereja compares 11 and 22 and takes 22. The remaining row is [1][1]; Sereja has 10+2=1210+2=12 points.
  • On turn 4, Dima takes the last card, worth 11. Dima's final score is 4+1=54+1=5.

The row is now empty. Thus, the final scores, in the required order, are 12 5.