Cod sursa(job #523190)

Utilizator 05_YohnE1 La5c01 05_Yohn Data 17 ianuarie 2011 12:41:11
Problema Energii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.16 kb
const inf=30000;

var c:array[0..100,0..100]of integer;
e,cost:array[1..100]of integer;
i,j,g,en,min:integer;

procedure sort(a,b:integer);
var mij,aux,i,j:integer;
begin
i:=a;
j:=b;
mij:=e[(a+b)shr 1];
repeat
while e[i]<mij do inc(i);
while e[j]>mij do dec(j);
if i<=j then begin
   aux:=e[i];e[i]:=e[j];e[j]:=aux;
   aux:=cost[i];cost[i]:=cost[j];cost[j]:=aux;
   inc(i);dec(j);
   end;
until i>j;
if a<j then sort(a,j);
if i<b then sort(i,b);
end;

begin
assign(input,'energii.in');
reset(input);
assign(output,'energii.out');
rewrite(output);
read(g);
read(en);
for i:=1 to g do
    read(e[i],cost[i]);

for i:=1 to g do c[i,0]:=0;
for i:=1 to en do c[0,i]:=inf;

sort(1,g);

for j:=1 to en do
    for i:=1 to g do
        if e[i]>j then c[i,j]:=c[i-1,j]
           else begin
                if c[i-1,j]<c[i-1,j-e[i]]+cost[i] then
                   c[i,j]:=c[i-1,j]
                   else
                   c[i,j]:=c[i-1,j-e[i]]+cost[i];
                end;

min:=c[1,en];
for i:=2 to g do
    if (min>c[i,en])and(c[i,en]<>0) then min:=c[i,en];

if min=0 then write(-1)
         else write(min);
close(output);
end.