NKBUS - Bus

Tác giả: flashmt

Ngôn ngữ: Pascal

const fi='';
      fo='';
var n,m,i,j,re,x,y,t,s:longint;
    a:array[0..200100] of longint;

procedure sort(l,r:longint);
var i,j,x,y:Longint;
begin
     i:=l; j:=r; x:=a[(i+j) shr 1];
     repeat
           while a[i]<x do i:=i+1;
           while a[j]>x do j:=j-1;
           if i<=j then
           begin
                y:=a[i]; a[i]:=a[j]; a[j]:=y;
                i:=i+1; j:=j-1;
           end;
     until i>j;
     if i<r then sort(i,r);
     if l<j then sort(l,j);
end;

begin
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     read(n,m);
     for i:=1 to n do
     begin
          read(t,x);
          for j:=1 to x do
          begin
               read(y);
               if y<=re then m:=m-1
               else
               begin
                    s:=s+1;
                    a[s]:=y-re;
               end;
          end;
          re:=re+t;
     end;
     if (s>0) and (m>0) then sort(1,s);
     if m>0 then re:=re+a[m];
     writeln(re);
     close(output); close(input);
end.

Download