
Untitled
By: a guest on
Aug 5th, 2012 | syntax:
C++ | size: 0.73 KB | hits: 12 | expires: Never
#include <stdio.h>
#include <set>
#include <utility>
using namespace std;
set < pair <int,int> > s;
set< pair <int,int> >::iterator itl,ith;
int main()
{
int i,j,k,n,p,id,ret;
while(scanf("%d",&n)==1)
{
s.clear();
for(id=1,ret=i=0;i<n;i++)
{
scanf("%d",&k);
for(j=0;j<k;j++)
{
scanf("%d",&p);
s.insert(make_pair(p,id++));
}
itl=s.upper_bound (make_pair(-1,1));
ith=s.end();
ith--;
ret+=(*ith).first-(*itl).first;
s.erase(itl);
s.erase(ith);
}
printf("%d\n",ret);
}
return 0;
}