Cod sursa(job #1377975)

Utilizator kor663Docolin Alexandru kor663 Data 6 martie 2015 09:47:22
Problema Energii Scor 35
Compilator fpc Status done
Runda Arhiva de probleme Marime 3 kb
type sir= array [1..1001100] of longint;
var put: sir;
    i: longint;

{procedure citire(var put,cost: sir; var n,p: integer);
var f: text;
    i: integer;
begin
    assign(f,'energii.in'); reset(f);
    readln(f,n);
    readln(f,p);
    for i:= 1 to n do readln(f,put[i],cost[i]);
    close(f);
end;}

function verif(put: sir; n,p: integer): boolean;
var i: integer;
    s: qword;
begin
    verif:=false;
    for i:= 1 to n do
        s:= s+put[i];
    if s>p then verif:=true;
end;


procedure tip(cost1: longint);
var f: text;
begin
    assign(f,'energii.out'); rewrite(f);
    writeln(f,cost1);
    close(f);
end;

procedure initial(var put: sir);
var i: longint;
begin
    for i:= 1 to 1001100 do put[i]:=-1;
end;


procedure ia(var put,put2: sir; last: longint);
var kenedy: longint;
begin
    for kenedy := 1 to last do
        put[kenedy]:=put2[kenedy];
end;

procedure rezolv(var put:sir);
var last,put1,cost1,nr: longint;
    n,p,i,j: longint;
    f: text;
    max: longint;
    put2:sir;
begin
    assign(f,'energii.in'); reset(f);
    readln(f,n);
    readln(f,p);
    initial(put2);
    initial(put);
    last:=0;
    for i:= 1 to n do begin
        readln(f,put1,cost1);
        for j:= 1 to last do
            if ((put2[j+put1]=-1) and (put2[j]<>-1))
                or ((put2[j+put1]>put2[j]+cost1) and (put2[j]<>-1)) then
                     begin
                         put[j+put1]:=put[j]+cost1;
                         if last<j+put1 then last:=j+put1;
                     end;
        if (put2[put1]=-1) or (put2[put1]>cost1) then
            begin
                put[put1]:=cost1;
                if last<put1 then last:=put1;
            end;
        {for nr:= 1 to last do begin
            write(put[i],' ');
            if i mod 10 = 0 then writeln;
        end;
        writeln;
        for nr:= 1 to last do begin
            write(put2[i],' ');
            if i mod 10 = 0 then writeln;
        end;
        writeln;}
        ia(put2,put,last);
    end;
    close(f);
    max:= maxlongint;
    for i:= p to last do
        if (put[i]<max) and (put[i]<>-1) then max:= put[i];
    if max= maxlongint then tip(-1) else tip(max);
end;

    {cost2:=0; put2:=0;
   repeat
    cost1:=0; put1:=0;
    for i:= 1 to n do
        if (put[i]>put1) then       begin
                                     j:=i;
                                     put1:=put[j];
                                     cost1:=cost[j];
                                     end
        else if (put[i]=put1) and (cost[i]<cost1) then begin
                                                       j:=i;
                                                       put1:=put[j];
                                                       cost1:=cost[j];
                                                       end;
    cost2:=cost2+cost1;
    put2:=put2+put1;
    put[j]:=-1;
    cost[j]:=-1;
   until put2>=p;
   tip(cost2);}





begin{pp}
rezolv(put);
end.