Cod sursa(job #102273)

Utilizator johnyJohny Deep johny Data 14 noiembrie 2007 10:26:23
Problema Zebughil Scor 60
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.92 kb
const fis='zebughil';
      nmax=17;
var cost:array[0..1 shl nmax] of longint;
    ok:array[1..1 shl nmax] of boolean;
    a,b2:array[1..nmax] of longint;
    n,g:longint;
    fi,fo:text;
    i:longint;

Procedure make_output;
var i,j,sp:longint;
begin
readln(fi,n,g);
for i:=1 to n do
    read(fi,a[i]);
for i:=1 to (1 shl n)-1 do
    begin
    sp:=0;
    for j:=1 to n do
        if i and b2[j]>0
        then sp:=sp+a[j];
    if sp<=g
    then ok[i]:=true
    else ok[i]:=false;
    cost[i]:=maxlongint;
    end;
cost[0]:=0;
for i:=1 to (1 shl n)-1 do
    for j:=1 to i do
        if (j-(j and i)=0) and ok[j] and (cost[i]>cost[i-j]+1)
        then cost[i]:=cost[i-j]+1;
writeln(fo,cost[(1 shl n)-1]);
end;

begin
for i:=1 to nmax do
    b2[i]:=1 shl (i-1);
assign(fi,fis+'.in');
reset(fi);
assign(fo,fis+'.out');
rewrite(fo);
for i:=1 to 3 do
    make_output;
close(fo);
close(fi);
end.