Cod sursa(job #265398)

Utilizator floorinelfloorinel floorinel Data 23 februarie 2009 20:52:03
Problema Energii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.82 kb
var w:real; t,i,g:integer;
    f,e:text; nul,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,'energii.in');reset(f);assign(e,'energii.out');rewrite(e);
readln(f,g);readln(f,w);c:=10000;min:=-1;ok:=false; s:=0;cs:=0; q:=0;nul:=false;
IF (G>1) AND (G<1001) AND (W>1) AND (W<5001) THEN
BEGIN
  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]>=10001) or (j[i]>=10001) then nul:=true;
 if nul=false then
  begin
  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=true then write(e,c:0:0);
  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
     BEGIN
     for t:=1 to g do
         r[t]:=v[t] / j[t];
     for i:=1 to g do
         for t:=1 to g do
         if t>i then
             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
     writeln (e,r[i]:0:3,' ',v[i]:0:0,' ',j[i]:0:0);}
     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);
      END;
   END;  {......}
    if nul=true then write(e,-1);

END;
IF (G<1) or (G>1001) or (W<1) or (W>5001) THEN write (e,-1);
close(f);close(e);
end.