Codeforces Fox and Card Game (greedy, sort)

Source: Internet
Author: User

C. Fox and Card Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Fox Ciel is playing a card game with her friend Fox Jiro. There is n piles of cards on the table. And there is a positive an integer on each card.

The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of Any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.

Suppose Ciel and Jiro play optimally, what is the score of the game?

Input

The first line contain an integerN(1≤N≤100). Each of the nextNLines contains a description of the pile:the first integer in the s i (1≤ s i ≤100)-the number of cards in The  i -th pile; then fo Llow  s i  positive integers < Span class= "Tex-span" > C 1,  C 2, ...,   C K , ...,   C s i   ( Span class= "Tex-span" >1≤ C k ≤1000)-the sequence of the numbers on the card s listed from top of the pile to bottom of the pile.

Output

Print integers:the sum of Ciel ' s cards and the sum of Jiro ' s cards if they play optimally.

Sample Test (s) input
2
1 100
2 1 10
Output
101 10
Input
1
9 2 8 6 5 9 4 7 1 3
Output
30 15
Input
3
3 1 3 2
3 5 4 6
2 8 7
Output
18 18
Input
3
3 1000 1000 1000
6 1000 1000 1000 1000 1000 1000
5 1000 1000 1000 1000 1000
Output
7000 7000
Note

In the first example, Ciel'll take the cards with number and 1, Jiro'll take the card with Number 10.

In the second example, Ciel'll take cards with numbers 2, 8, 6, 5, 9 and Jiro would take cards with numbers 4, 7, 1, 3.

The title means there are n stacks of cards, each pile of cards have Si Zhang Card, each card has a weight, a can only from the above card, B can only take the card from below, two people have taken the best strategy, ask the last two people to take the weight of the total number of cards.

Can analyze, if

Codeforces Fox and Card Game (greedy, sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.