Sphere Online Judge

SPOJ Problem Set (classical)

726. Promotion

Problem code: PRO

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

Added by:Duc
Date:2006-01-24
Time limit:1s-4s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All except: NODEJS PERL 6
Resource:VII Polish Olympiad In Informatics 2000, stage III

hide comments
2012-12-25 11:06:19 MR. BEAN
the time taken by your code totally depends on the container u use :D
2012-08-26 09:03:56 AC Srinivas
use unsigned long long for final answer.
2012-06-16 21:18:26 Avinash Mishra
How u guy's got submitted in 0.01 sec..pls explain.
2012-06-16 21:14:38 Avinash Mishra
C++ stl is sufficient
2012-06-16 19:46:15 Avinash Mishra
i got TLE
2012-05-30 16:09:05 Sabarinath
Getting WA :(.. For test cases it is giving correct answer.. In judge, it is running till 7th test case.. prob id:7062433

Last edit: 2012-05-30 16:09:42
2012-04-27 05:52:09 Gowtham
For all those who are wondering about the explanation of the sample test case.
Re-read the Input section of the problem!
2012-01-19 04:05:17 Suraj D
Could someone please explain the output for the sample tests given here?
2011-11-30 00:36:33 jack(chakradarraju)
there is nothing to solve in this problem, if you know to use STL in C++ properly
2011-05-17 12:45:49 anurag
do not use long long for all variables. it will time out
SPOJ © 2013 Sphere Research Labs. All Rights Reserved.