#P7891. [入门赛 #10] Coin Selection G(hard version)

[入门赛 #10] Coin Selection G(hard version)

Problem Description

Farmer John and Bessie are playing a coin selection game together.

At the beginning, there are nn coins on the table. Each coin has a face value. We use a1,a2,,ana_1, a_2, \cdots, a_n to represent the values of coin 1,2,,n1, 2, \cdots, n.

They each have their own wallet, and both wallets are empty at the start.

Starting from Bessie, they take turns. In each turn, the current player chooses, among the remaining coins on the table whose value is not greater than the total value of coins currently in their own wallet, the coin with the largest value, then takes it from the table and puts it into their wallet. If the values of all remaining coins on the table are greater than the total value of coins already in their wallet, then they choose the coin with the smallest value among all remaining coins.

When there are no coins left on the table, the game ends.

Please compute the total value of coins in Farmer John's wallet and in Bessie's wallet after the game ends.

Input Format

The first line contains an integer nn, representing the number of coins on the table initially.
The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n, representing the values of coins 1,2,,n1, 2, \cdots, n.

Output Format

Output one line with two integers. The first integer is the final total value of coins in Bessie's wallet, and the second integer is the final total value of coins in Farmer John's wallet.

2
3 2
2 3
5
1 2 3 4 5
9 6

Hint

Constraints

  • For 100%100\% of the testdata, it is guaranteed that 1n1051 \le n \le 10^5 and 1ai1091 \le a_i \le 10^9.

Provider: 一扶苏一

Translated by ChatGPT 5