Cod sursa(job #879000)

Utilizator andreiblaj17Andrei Blaj andreiblaj17 Data 14 februarie 2013 21:27:47
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.28 kb
var f,h:text;
    G,W,i,j,sumG,sumC,min,k,C:word;
    ok:boolean;
    CG,EG:array[1..1001] of word;
begin
assign(f,'energii.in');reset(f);
assign(h,'energii.out');rewrite(h);

readln(f,G);
readln(f,W);

ok:=false;

while not eof(f) do
 begin
  inc(i);
  read(f,EG[i]);
  read(f,CG[i]);
  if EG[i]=W then begin
                   C:=CG[i];
                   ok:=true;
                  end;
 end;

 k:=1;

 if ok=true then write(h,C)
       else begin
            k:=0;
            min:=maxint;
             for i:=1 to G do
              begin
               sumG:=EG[i];
               sumC:=CG[i];
               for j:=1 to G do
                if i<>j then begin
                  sumG:=sumG+EG[j];
                  sumC:=sumC+CG[j];
                  if (sumG>=W) and (sumC<min) then begin
                                                    min:=sumC;
                                                    break;
                                                   end;
                             end;
              end;
              if sumG<W then begin
                              write(h,'-1');
                              k:=1;
                             end;

            end;

     if k=0 then write(h,min);

close(f);
close(h);
end.