Cod sursa(job #140065)

Utilizator andumMorie Daniel Alexandru andum Data 21 februarie 2008 09:52:57
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.34 kb
type tip=record
                        x:integer;
                        y:integer;
         end;
var g,w,i,j,q,min,e,l:longint;
    s:longint;
    a:array[1..1000] of tip;
    f,h:text;
    x:tip;
    ok:boolean;
begin
assign(f,'energii.in');
reset(f);
assign(h,'energii.out');
rewrite(h);
readln(f,g);
readln(f,w);
for i:=1 to g do begin
                 read(f,a[i].x);
                 readln(f,a[i].y);
                 end;
close(f);
{
for i:=1 to g-1 do
for j:=i+1 to g do if (a[i].x>a[j].x) or ((a[i].x=a[j].x) and (a[i].y<a[j].y)) then begin
                                                                                    x:=a[i].x;
                                                                                    a[i].x:=a[j].x;
                                                                                    a[j].x:=x;
                                                                                    x:=a[i].y;
                                                                                    a[i].y:=a[j].y;
                                                                                    a[j].y:=x;
                                                                                    end;
}
j:=g;
repeat
      ok:=true;
      For i:=1 to j-1 do
          if (a[i].x>a[i+1].x) then  begin
                                          ok:=False;
                                          x:=a[i]; a[i]:=a[i+1];
                                          a[i+1]:=x;
                                   end
                     else
                        if (a[i].x=a[i+1].x) and (a[i].y<a[i+1].y) then
                        begin
                                          ok:=False;
                                          x:=a[i]; a[i]:=a[i+1];
                                          a[i+1]:=x;
                       end;
      j:=j-1;
until ok;
i:=1;
while (i<=g) do begin
                s:=s+a[i].x;
                e:=e+a[i].y;
                inc(i);
               end;
if s<w then writeln(h,'-1')
       else begin
min:=e;
for i:=1 to g do
for j:=i to g do
 begin
  s:=0;
  e:=0;
  for l:=i to j do begin
                   s:=s+a[l].x;
                   e:=e+a[l].y;
                   end;
  if (s>=w) and (min>e) then min:=e;
 end;
writeln(h,min);
            end;
close(h);
end.