Problem H: Hoax or what
Each Mal-Wart supermarket has prepared a promotion scheme run by the following rules:- A client who wants to participate in the promotion (aka a sucker) must write down their phone number on the bill of their purchase and put the bill into a special urn.
- Two bills are selected from the urn at the end of each day: first the highest bill is selected and then the lowest bill is selected. The client who paid the largest bill receives a monetary prize equal to the difference between his bill and the lowest bill of the day.
- Both selected bills are not returned to the urn while all the remaining ones are kept in the urn for the next day.
- Mal-Wart has many clients such that at the end of each day there are at least two bills in the urn.
- It is quite obvious why Mal-Wart is doing this: they sell crappy products which break quickly and irreparably. They give a short-term warranty on their products but in order to obtain a warranty replacement you need the bill of sale. So if you are gullible enough to participate in the promotion you will regret it.
The input contains a number of cases. The first line in each case contains an integer n, 1<=n<=5000, the number of days of the promotion. Each of the subsequent n lines contains a sequence of non-negative integers separated by whitespace. The numbers in the (i+1)-st line of a case give the data for the i-th day. The first number in each of these lines, k, 0≤k≤105, is the number of bills and the subsequent k numbers are positive integers of the bill amounts. No bill is bigger than 106. The total number of all bills is no bigger than 106. The case when n = 0 terminates the input and should not be processed.
For each case of input print one number: the total amount paid to the clients by Mal-Wart as the result of the promotion.
Sample input
5 3 1 2 3 2 1 1 4 10 5 5 1 0 1 2 2 2 1 2 2 1 2 0
Output for sample input
19 2
T. Walen, adapted by P. Rudnicki
Warning: Time limit is too tight to get accepted using STL. The input file size is around 16 MB
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 200 + 5;
const int maxw = 100 + 20;
const int MAXNNODE = 1000000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
multiset <LL> s;
int main()
{
//ios::sync_with_stdio(false);
#ifdef Online_Judge
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // Online_Judge
LL n , k;
while(scanf("%lld" , &n) == 1&&n)
{
LL sum = 0;
s.clear();
while(n--)
{
scanf("%lld" , &k);
LL b;
while(k--)
{
scanf("%lld" , &b);
s.insert(b);
}
if(s.size() > 0)
{
multiset<LL>::iterator it;
it = s.begin();
LL minnum = *it;
it = s.end();
it--;
LL maxnum = *it;
s.erase(it);
if(s.size() > 0)
{
it = s.begin();
s.erase(it);
}
sum += maxnum - minnum;
}
}
printf("%lld\n" , sum);
}
return 0;
}