EQ - Electronic queue

Tác giả: flashmt

Ngôn ngữ: Pascal

const fi='';
      fo='';
var test,it,n,p:longint; re:qword;
    a:array[1..50] of qword;

function find(x:longint):longint;
var i:longint; min:qword;
begin
     find:=0; min:=maxlongint;
     for i:=1 to n do
     begin
          if a[i]<=x then
          begin
               find:=i;
               exit;
          end;
          if a[i]<min then
          begin
               min:=a[i];
               find:=i;
          end;
     end;
end;

procedure pr;
var i,j,x,y,t:longint;
begin
     read(n);
     fillchar(a,sizeof(a),0);
     read(p);
     re:=0;
     for i:=1 to p do
     begin
          read(x,y);
          t:=find(x);
          re:=re+y+5;
          if a[t]>x then
          begin
               re:=re+a[t]-x;
               a[t]:=a[t]+y+5;
          end
          else a[t]:=x+y+5;
     end;
     writeln(re);
end;

begin
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     read(test);
     for it:=1 to test do pr;
     close(input); close(output);
end.

Download