Cod sursa(job #18754)

Utilizator andrei_infoMirestean Andrei andrei_info Data 18 februarie 2007 13:55:26
Problema Ghiozdan Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.48 kb
//infoarena ghiozdan
const nmax = 20000;
      gmax = 75000;
var a:array[1..2,0..gmax] of integer;
    g:array[1..nmax] of integer;
    n,gg:longint;

procedure citire;
var i:integer;
begin
assign(input,'ghiozdan.in'); reset(input);
readln(n,gg);
for i:=1 to n do
        readln(g[i]);
closE(input);
end;

function min(x,y:longint):integer;
begin
if x<y then min:=x else min:=y;
end;

procedure Sort(l, r: Integer);
var
  i, j, x, y: integer;
begin
  i := l; j := r; x := g[(l+r) DIV 2];
  repeat
    while g[i] < x do i := i + 1;
    while x < g[j] do j := j - 1;
    if i <= j then
    begin
      y := g[i]; g[i] := g[j]; g[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 calc;
var i,j,max:longint;
begin
for i:=1 to gg do begin a[2,i]:=maxint; a[1,i]:=maxint; end;
sort(1,n);
a[1,0]:=0;
max:=0;
for i:=1 to n do
        begin
        max:=max+g[i];
        if max > gg then max:=gg;
        for j:=1 to max  do
           if j-g[i] >=0 then
                    a[2,j]:=min(a[1,j],1+a[1,j-g[i]])
           else     a[2,j]:=a[1,j];


        for j:=1 to max do
                begin
                a[1,j]:=a[2,j];
                a[2,j]:=maxint;
                end;
        end;
j:=gg;
while a[1,j] = maxint do
        dec(j);
assign(output,'ghiozdan.out'); rewrite(output);
writeln(j,' ',a[1,j]);
close(output);
end;

begin
citire;
calc;
end.