Cod sursa(job #514286)

Utilizator vendettaSalajan Razvan vendetta Data 18 decembrie 2010 12:55:19
Problema Energii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.22 kb
const f='energiii.in';h='energiii.out';
type rec=record
    prod,cost:longint;
end;
var
    n,i,j,final,max:longint;
    a:array[0..5001] of rec;
    b:array[0..5001] of longint;
begin
    assign(input,f);reset(input);
    assign(output,h);rewrite(output);
    readln(n);
    readln(final);
    for i:=1 to n do readln(a[i].prod,a[i].cost);
    for i:=1 to final do b[i]:=2500000;
    b[0]:=0;max:=0;
    for i:=1 to n do
        for j:=max downto 0 do
            begin
            if b[j]>=0 then
                begin
                if (a[i].prod+j>=final) then
                    if (b[final]>=a[i].cost+b[j]) then
                        begin
                        b[final]:=a[i].cost+b[j];
                        max:=final;
                        end
                    else
                else
                    if (b[j+a[i].prod]>=a[i].cost+b[j]) then
                        begin
                        b[j+a[i].prod]:=a[i].cost+b[j];
                        if a[i].prod+j>max then max:=a[i].prod+j;
                    end
                    else
                end;
            end;
    if b[final]=2500000 then writeln(-1) else writeln(b[final]);
    close(input);close(output);
end.