Cod sursa(job #27074)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 6 martie 2007 07:31:45
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.87 kb
type sir=array[0..1000] of longint;
var a,c,s,poz:sir;
    x,i,j,n,w,max,min:longint;
    f,g:text;
begin
 assign(f,'energii.in'); reset(f);
 assign(g,'energii.out'); rewrite(g);
 read(f,n);
 read(f,w);
 for i:=1 to n do
  read(f,a[i],c[i]);
 for i:=1 to n-1 do
  for j:=i+1 to n do
   if (c[i]>c[j]) or ((c[i]=c[j]) and (a[i]<a[j])) then begin
    x:=a[i];
    a[i]:=a[j];
    a[j]:=x;
    x:=c[i];
    c[i]:=c[j];
    c[j]:=x;
   end;
 max:=0;
 poz[0]:=0;
 for i:=1 to n do
  for j:=max downto 0 do
   if ((s[j]<>0) or (j=0)) and (s[j+a[i]]=0) and (poz[j+a[i]]<>i) then begin
    s[j+a[i]]:=s[j]+c[i];
    poz[j+a[i]]:=i;
    if (j+a[i])>max then max:=j+a[i];
   end;
 if max<w then writeln(g,'-1')
 else begin
  min:=2000000000;
  for i:=w to max do
   if (s[i]<min) and (s[i]<>0) then
    min:=s[i];
  writeln(g,min);
 end;
 close(f); close(g);
end.