eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Free Goodies

Free Goodies

Petra and Jan have just received a box full of free goodies, and want to divide the goodies between them. However, it is not easy to do this fairly, since they both value different goodies differently. To divide the goodies, they have decided upon the following procedure: they choose goodies one by one, in turn, until all the goodies are chosen. A coin is tossed to decide who gets to choose the first goodie. Petra and Jan have different strategies in deciding what to choose. When faced with a choice, Petra always selects the goodie that is most valuable to her. In case of a tie, she is very considerate and picks the one that is least valuable to Jan. (Since Petra and Jan are good friends, they know exactly how much value the other places on each goodie.) Jan's strategy, however, consists of maximizing his own final value. He is also very considerate, so if multiple choices lead to the same optimal result, he prefers Petra to have as much final value as possible. You are given the result of the initial coin toss. After Jan and Petra have finished dividing all the goodies between themselves, what is the total value of the goodies each of them ends up with? \InputFile On the first line a positive integer: the number of test cases, at most \textbf{100}. After that per test case: \begin{itemize} \item One line with an integer \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{1000}): the number of goodies. \item One line with a string, either "\textbf{Petra}" or "\textbf{Jan}": the person that chooses first. \item \textbf{n} lines with two integers \textbf{p_i} and \textbf{j_i} (\textbf{0} ≤ \textbf{p_i}, \textbf{j_i} ≤ \textbf{1000}) each: the values that Petra and Jan assign to the \textbf{i}-th goodie, respectively. \end{itemize} \OutputFile Per test case: \begin{itemize} \item One line with two integers: the value Petra gets and the value Jan gets. Both values must be according to their own valuations. \end{itemize}
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
3
4
Petra
100 80
70 80
50 80
30 50
4
Petra
10 1
1 10
6 6
4 4
7
Jan
4 1
3 1
2 1
1 1
1 2
1 3
1 4
Вихідні дані #1
170 130
14 16
9 10
Джерело NWERC-2010