Cod sursa(job #265337)

Utilizator floorinelfloorinel floorinel Data 23 februarie 2009 19:50:51
Problema Energii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.38 kb
var w:real; t,i,g:integer;
    f,e:text; ok:boolean;  v,j,r:array [1..101] of real;
    x,q,cs,s,aux1,aux2,aux3,a,b,c,min:real;
BEGIN
assign(f,'energii2.in');reset(f);assign(e,'energii.out');rewrite(e);
readln(f,g);readln(f,w);c:=10001;min:=-1;ok:=false; s:=0;cs:=0; q:=0;
  for i:=1 to g do
     begin
       readln(f,a,b);v[i]:=a;j[i]:=b;
     end;
  for i:=1 to g do
    if v[i]>=w then ok:=true;
  if ok=true then
     for i:=1 to g do
     BEGIN
      a:=v[i];b:=j[i];
      if (a>=w) and (b<c) then
       begin
        c:=b;min:=a;
       end;
     END;
  if ok=false then
     for i:=1 to g do
         s:=s+v[i];
  if (ok=false) and (s<w) then write (e,-1);
  if (ok=false) and (s>=w) then
     for t:=1 to g do
         r[t]:=v[t] / j[t];
     for i:=1 to g-1 do
         for t:=i+1 to g do
             BEGIN
             if (r[i]>=r[t]) and (j[i]<j[t]) then
                begin
                aux1:=r[i];r[i]:=r[t];r[t]:=aux1;
                aux2:=v[i];v[i]:=v[t];v[t]:=aux2;
                aux3:=j[i];j[i]:=j[t];j[t]:=aux3;
                end;
             END;
     for i:=1 to g do
         if (i=g) and (w>q) then
            begin
            q:=v[i];cs:=j[i];
            end;
        while w>q do
         begin
         q:=q+v[i-1];
         cs:=cs+j[i-1];
         end;
         if q>=w then write(e,cs:0:0);

close(f);close(e);
end.