Cod sursa(job #37645)

Utilizator raduzerRadu Zernoveanu raduzer Data 25 martie 2007 11:36:41
Problema Shop Scor 0
Compilator fpc Status done
Runda preONI 2007, Runda 4, Clasa a 9-a si gimnaziu Marime 2.15 kb
var a,ind:array[1..32]of integer;
    b,d,e:array[1..32]of int64;
    n,c,i,j,p,q,t:integer;
    l,x,s:int64;

procedure Sort(l, r: Integer);
var
  i, j, x, y: integer;
begin
  i := l; j := r; x := a[(l+r) DIV 2];
  repeat
    while a[i] < x do i := i + 1;
    while x < a[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      y := b[i]; b[i] := b[j]; b[j] := y;
      y := ind[i]; ind[i] := ind[j]; ind[j] := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;

procedure Sort2(l, r: Integer);
var
  i, j, x, y: integer;
begin
  i := l; j := r; x := ind[(l+r) DIV 2];
  repeat
    while ind[i] < x do i := i + 1;
    while x < ind[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      y := b[i]; b[i] := b[j]; b[j] := y;
      y := ind[i]; ind[i] := ind[j]; ind[j] := y;
      y := e[i]; e[i] := e[j]; e[j] := y;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;

procedure proc(i:integer);
var j:integer;
begin
     if i=0 then exit;
     while (s+d[i]<=l)and(e[i]<b[i]) do
     begin
          s:=s+d[i];
          e[i]:=e[i]+1;
     end;
     if s=l then
     begin
          q:=1;
          exit;
     end;
     proc(i-1);
     if q=1 then exit;
     while e[i]>0 do
     begin
          s:=s-d[i];
          e[i]:=e[i]-1;
          proc(i-1);
          if q=1 then exit;
     end;

end;

begin
     assign(input,'shop.in');
     reset(input);
     assign(output,'shop.out');
     rewrite(output);
     readln(n,c,l);
     for i:=1 to n do
     begin
          ind[i]:=i;
          readln(a[i],b[i]);
     end;
     sort(1,n);
     q:=0;
     x:=1;
     p:=0;
     for i:=1 to n do
     begin
          while p<a[i] do
          begin
               p:=p+1;
               x:=x*c;
          end;
          d[i]:=x;
     end;

     proc(n);
     sort2(1,n);
     t:=0;
     for i:=1 to n do t:=t+e[i];
     writeln(t);
     for i:=1 to n do write(e[i],' ');
close(output);
end.