var T,j,i,u,a,M: longint;
N: array[1..1000] of integer;
begin
{assign(INPUT, 'ALIBABA.IN');
reset(INPUT);}
readln(T);
a:=0;
for i:=1 to T do
begin
readln(N,M);
for j:=1 to M  do
begin
max:=-1000;
for u:=1 to N do
begin
if max<N[u] then max:=N[u];
end;
a:=a+max;
N[u]:=-1000;
end;
end;
writeln(a);
end.


