Fox and Card Game

Two players alternately take the top (Ciel) or bottom (Jiro) card of one of n piles; find both optimal final scores.

Medium7Game theoryGreedySortingImplementationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Fox Ciel is playing a card game with her friend Jiro. There are nn piles of cards on the desk, and every card has one positive integer written on it.

The two players take one card each in alternating turns, and Ciel takes first. On her turn Ciel picks a non-empty pile and takes the top card of that pile. On his turn Jiro picks a non-empty pile and takes the bottom card of that pile. The game ends when no card is left on the desk.

When the game ends, each player's score is the sum of the numbers on the cards that player took. A player who took no card scores 0. Both players want to make their own score as large as possible. Find the score of Ciel and the score of Jiro when both play optimally.

Input

The first line contains the number of piles nn (1n1001 \le n \le 100).

Each of the next nn lines contains sis_i followed by sis_i integers ci1,ci2,,cisic_{i1}, c_{i2}, \dots, c_{is_i} (1si1001 \le s_i \le 100, 1cij10001 \le c_{ij} \le 1000). Pile ii holds sis_i cards stacked from top to bottom in the order given, so ci1c_{i1} is the top card and cisic_{is_i} is the bottom card.

Output

Print the score of Ciel and the score of Jiro on one line, separated by a single space.