Cod sursa(job #1070359)

Utilizator Mihai_ChihaiMihai Chihai Mihai_Chihai Data 31 decembrie 2013 18:43:25
Problema Problema rucsacului Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.1 kb
program rucsac;
  type r=record
      w:longint;
      p:longint;
      c:real;
      end;
  var a:array[1..10000] of r;
      n,g,i,j:longint;
      ans,pg:longint;
      aux:r;

  procedure sort(l,r:longint);
  var i,j:longint;
      m:real;
  begin
    i:=l;
    j:=r;
    m:=a[(i+j) div 2].c;
    while i<=j do begin
      while a[i].c<m do inc(i);
      while a[j].c>m do dec(j);
      if i<=j then  begin
                 aux:=a[i];
                 a[i]:=a[j];
                 a[j]:=aux;
                 inc(i);  ;
                 dec(j);
                 end;
      end;
    if i<r then sort(i,r);
    if l<j then sort(l,j);
  end;

  begin
    assign(input,'rucsac.in'); reset(input);
    assign(output,'rucsac.out'); rewrite(output);
    readln(n,g);
    for i:=1 to n do readln(a[i].w,a[i].p) ;
    close(input);
    for i:=1 to n do a[i].c:=a[i].p/a[i].w;
    sort(1,n);

    ans:=0; pg:=0;
     for i:=n downto 1 do
       begin
         pg:=pg+a[i].w;
         if pg<=G then ans:=ans+a[i].p else break;
       end;
     writeln(ans);
     close(output);
  end.