Cod sursa(job #602248)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 10 iulie 2011 02:37:27
Problema Energii Scor 45
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.25 kb
Program energii;
 var a:array [1..1001,1..2] of integer;
     c:array [1..1001] of real;
     g,w,i,j,n,m:integer;
     s1,s2:longint;
     k:real;
     f:boolean;
     fi,fo:text;
begin
assign(fi,'energii.in');
 reset(fi);
assign(fo,'energii.out');
 rewrite(fo);
readln(fi,g);
 readln(fi,w);
  for i:=1 to g do begin
   readln(fi,a[i,1],a[i,2]);
   if a[i,2]<>0 then
                  c[i]:=(a[i,1])/(a[i,2])
                    else c[i]:=0{a[i,1]};
                   end;
repeat
 f:=false;
  for i:=1 to g-1 do
   if c[i]<c[i+1] then begin
    k:=c[i]; m:=a[i,1]; n:=a[i,2];
    c[i]:=c[i+1]; a[i,1]:=a[i+1,1]; a[i,2]:=a[i+1,2];
    c[i+1]:=k; a[i+1,1]:=m; a[i+1,2]:=n;
    f:=true;
    end;
 until not f;  i:=1;
while (s1<w) and (i<=g) do begin
 s1:=s1+a[i,1];
  s2:=s2+a[i,2];
   inc(i);
    end;
  dec(i); s1:=s1-a[i,1]; s2:=s2-a[i,2];
if s1+a[i,1]=w then write(fo,s2+a[i,2])
 else if (s1+a[i,1]>w) and (i<g) then begin
                         n:=a[i,2];
                         for j:=i+1 to g do
                          if (a[j,1]>=w-s1) and (a[j,2]<a[i,2]) then n:=a[j,2];
                          write(fo,s2+n);
                          end
 else if (s1+a[i,1]<w) and (i=g) then write(fo,'-1');
close(fo);
end.