Cod sursa(job #5814)

Utilizator fishbogdan negrila fish Data 15 ianuarie 2007 10:58:22
Problema Energii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.63 kb
program energetic;
var f,f1:text;
   n,k,i,w,g,j,p,c:longint;
   x:array[1..100,1..2] of integer;
begin
assign(f,'energii.in'); reset(f);
assign(f1,'energii.out'); rewrite(f1);
readln(f,n);
readln(f,w);
for i:=1 to n do readln(f,x[i,1],x[i,2]);
for i:=1 to n do
       for j:=(i+1) to n do if x[i,1]/x[i,2]<x[j,1]/x[j,2] then begin
                          p:=x[i,1];
                          x[i,1]:=x[j,1];
                          x[j,1]:=p;
                          p:=x[i,2];
                          x[i,2]:=x[j,2];
                          x[j,2]:=p;
                          end;
i:=1;
c:=0;
p:=0;
while (p<w) and (i<=n)  do begin
     if p+x[i,1]>w then
             for j:=i+1 to n do if (p+x[j,1]>=w) and (x[j,2]<x[i,2]) then begin
                                                                  c:=c+x[j,2];
                                                                   p:=p+x[j,1];

                                                                   end
                                                                   else begin
                                                                   c:=c+x[i,2];
                                                                   p:=p+x[i,1];
                                                                   inc(i);
                                                                   end
                   else begin
                        c:=c+x[i,2];
                        p:=p+x[i,1];
                        inc(i);
                        end;
     end;
if p>=w then writeln(f1,c)
        else writeln(f1,'-1');
close(f);
close(f1);
end.