Cod sursa(job #18160)

Utilizator gurneySachelarie Bogdan gurney Data 18 februarie 2007 10:15:05
Problema Ghiozdan Scor 54
Compilator fpc Status done
Runda preONI 2007, Runda 2, Clasele 11-12 Marime 2.14 kb
program ghiozdan;
  const
    fin='ghiozdan.in';
    fout='ghiozdan.out';
    nmax=20000;
    gmx=75000;
  var
    p:array[0..gmx] of boolean;
    nr:array[0..gmx] of longint;
    prec:array[0..gmx] of longint;
    g:array[1..nmax] of longint;
    gg:longint;
    i,j,k,l,m,n,x,y:longint;
    nmin,gmax:longint;
    max:longint;
    sol:array[0..nmax] of longint;

procedure update(g:longint);
  var
    i:longint;
  begin
    sol[0]:=nr[g];
    for i:=sol[0] downto 1 do
      begin
        sol[i]:=g-prec[g];
        g:=prec[g];
      end;
  end;

begin
  assign(input,fin);
    reset(input);
    readln(n,gg);
    for i:=1 to n do
      readln(g[i]);
  close(input);
  assign(output,fout);
    rewrite(output);
    prec[0]:=0;
    p[0]:=true;
    nr[0]:=0;
    max:=0;
    nmin:=maxint;gmax:=0;
    for i:=1 to n do
      begin
        j:=max+g[i];
        if j>gg then
          j:=gg;
        while j-g[i]>=0 do
          begin
            if p[j-g[i]] then
              if p[j]=false then
                begin
                  p[j]:=true;
                  nr[j]:=nr[j-g[i]]+1;
                  prec[j]:=j-g[i];
                  if (j>gmax)or((j=gmax)and(nr[j]<nmin)) then
                    begin
                      gmax:=j;nmin:=nr[j];
                      update(j);
                    end;
                  if j>max then
                    max:=j;
                end
              else
                if nr[j-g[i]]+1<nr[j] then
                  begin
                    nr[j]:=nr[j-g[i]]+1;
                    prec[j]:=j-g[i];
                    if (j>gmax)or((j=gmax)and(nr[j]<nmin)) then
                      begin
                        gmax:=j;nmin:=nr[j];
                        update(j);
                      end;
                    if j>max then
                      max:=j;
                  end;
            dec(j);
          end;
      end;
    if nmin<>maxint then
      begin
        writeln(gmax,' ',nmin);
        for i:=1 to sol[0] do
          writeln(sol[i]);
      end
    else
      begin
        writeln(0,' ',0);
      end;
  close(output);
end.