Cod sursa(job #265823)

Utilizator floorinelfloorinel floorinel Data 24 februarie 2009 15:57:33
Problema Energii Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.77 kb
var f,e:text; ok:boolean; v,j:array [1..100] of integer;
    l,s,g,p,o,q,w,i,t,a,b,c:integer;             r:array [1..100] of real;
    aux4,aux2,aux3:integer;    x,y,aux1:real;
BEGIN
assign(f,'energii.in');reset(f);assign(e,'energii.out');rewrite(e);
readln(f,g);readln(f,w);   s:=0;c:=10001;q:=0;y:=0;o:=0;OK:=false;
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) and (j[i]<c) then c:=j[i];
for i:=1 to g do
    s:=s+v[i];
if s<w then ok:=true;
if s=w then
   for i:=1 to g do
       c:=c+j[i];
if s>w then
   BEGIN
   for i:=1 to g do
       r[i]:=v[i] / j[i];
   for i:=1 to g-1 do
       for t:=i+1 to g do
       BEGIN
       r[t]:=v[t] / j[t];
       if (r[i]<r[t]) and (v[i]<w) then
          BEGIN
           aux1:=r[t];r[t]:=r[i];r[i]:=aux1;
           aux2:=v[t];v[t]:=v[i];v[i]:=aux2;
           aux3:=j[t];j[t]:=j[i];j[i]:=aux3;
          END;
       end;
   x:=s;
   for i:=1 to g do
       y:=y+j[i];
   x:=x / y;
   for i:=1 to g do
      begin
        if (r[i]>=x) and (w>o) and (w>o+v[i]) then
           begin
             q:=q+j[i]; o:=o+v[i];
           end;
        if (w>o) and (w=o+v[i+1]) then
           begin
           q:=q+j[i+1];
           o:=o+v[i+1];
           end;
        if (w>o) and (w<o+v[i+1]) then
        BEGIN
            p:=i;
        for l:=i to g-1 do
        for t:=l+1 to g do
                 if j[l]>j[t] then
                    begin
                    aux4:=j[t];j[t]:=j[l];j[l]:=aux4;
                    end;
             l:=p+1;
             o:=o+v[l];
             q:=q+j[l];
         END;
       end;


   END;
if (c<q) and (q>0) then write (e,c) else write (e,q);
if ok=true then write (e,-1);
close(f);close(e);


END.