PRO - Promotion


A large Bytelandian supermarket chain has asked you to write a program for the simulating costs of a promotion being prepared.

The promotion has to follow the following rules:

  • A customer who wants to participate in the promotion, writes on the receipt, paid by himself, his personal details and throws it into a special ballot box.
  • At the end of every day of the promotion, two bills are taken out from the ballot box:
    • first, the receipt amounting to the largest sum is chosen,
    • then the receipt amounting to the smallest sum is chosen;
    The customer who has paid the largest sum gets a money prize equal to the difference between the sum on his bill and the sum on the bill amounting to the smallest sum.
  • To avoid multiple prizes for one purchase, both bills selected according to the above rules are not returned to the ballot box, but all remaining bills still participate in the promotion.

The turnover of the supermarket is very big, thus an assumption can be made, that at the end of every day, before taking out receipts amounting to the largest and the smallest sum, there are at least 2 receipts in the ballot box.

Your task is to compute (on the basis of information about prices on receipts thrown into the ballot box on each day of promotion) what the total cost of prizes during the whole promotion will be.

Write a program, which: reads from the standard input a list of prices on receipts thrown into the ballot box on each day of the promotion, computes the total cost of prizes paid in consecutive days of promotion, then writes the result to the standard output.

Input

The first line of the input contains one positive integer n (1 <= n <= 5000), which is the duration of promotion in days. Each of the next n lines consists of a sequence of non-negative integers separated by single spaces. Numbers in the (i+1)-th line of the file represent prices on receipts thrown into the ballot box on the i-th day of promotion. The first integer in the line is k, 0 <= k <= 10^5, the number of receipts on the day, and the next k numbers are positive integers standing for the sums on receipts; none of these numbers is larger than 10^6.

The total number of bills thrown into the ballot box during the whole promotion does not exceed 10^6.

Output

The output should contain exactly one integer, equal to the total cost of prizes paid during the whole promotion.

Example

Input:
5
3 1 2 3
2 1 1
4 10 5 5 1
0
1 2

Output:
19

hide comments
SANDEEP KUMAR: 2015-06-15 22:43:12

Finally,Learnt a lot of STL bcoz of this problem...:)

bholagabbar: 2015-06-02 08:30:04

i honestly don't know why priority queue in C++ gave WA. I used TreeMaps in JAVA to get AC

i_am_looser: 2015-05-26 11:53:05

stl rocks... ; )

i_am_looser: 2015-05-26 11:29:59

Be careful use long long int instead of int cost me one wrong answer.....

i_am_looser: 2015-05-26 11:29:04

Got accepted using Segment tree.................... in love with segment tree woooow

anando_du: 2015-05-18 16:58:04

use int ->32 bit
use multiset (don't use priority queue i got wa one test case 7)
use unsigned 64bit int to count sum
then got ac -_-

fanatique: 2014-07-09 17:51:14

implemented using binary search tree...
"first, the receipt amounting to the largest sum is chosen,
then the receipt amounting to the smallest sum is chosen"..but still WA coz of large inputs..

Last edit: 2014-07-09 18:31:18
Ishan Bansal: 2013-10-20 08:29:23

I am getting wa after 7th test case....used unsigned long long and everything

Last edit: 2013-10-20 08:36:00
Ouditchya Sinha: 2013-09-04 10:32:07

Nice Problem!! Learnt something new today. :)

Vipul Pandey: 2013-09-03 21:23:58

easy implementation of stl. Got AC in the first go!


Added by:Jimmy
Date:2006-01-24
Time limit:0.600s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:VII Polish Olympiad In Informatics 2000, stage III