Cod sursa(job #1375525)

Utilizator kor663Docolin Alexandru kor663 Data 5 martie 2015 13:29:53
Problema Energii Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.48 kb
type sir= array [1..100110] 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 rezolv(var put:sir);
var last,put1,cost1: integer;
    n,p,i,j: integer;
    f: text;
    max: longint;
begin
    assign(f,'energii.in'); reset(f);
    readln(f,n);
    readln(f,p);
    last:=0;
    for i:= 1 to n do begin
        readln(f,put1,cost1);
        for j:= 1 to last do
            if ((put[j+put1]=-1) and (put[j]<>-1)) or (put[j+put1]>put[j]+cost1) then begin if (put[j+put1]=-1) then inc(put[j+put1]); put[j+put1]:=put[j]+cost1; if last<j+put1 then last:=j+put1; end;
        if (put[put1]=-1) or (put[put1]>cost1) then begin if (put[put1]=-1) then inc(put[put1]); put[put1]:=cost1; if last<put1 then last:=put1; end;
    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);
    {for i:= 1 to 120 do begin
        write(put[i],' ');
        if i mod 10 = 0 then writeln;
    end;}
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);}

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



begin{pp}
initial(put);
rezolv(put);
end.