Cod sursa(job #1519277)

Utilizator raresm44vasile rares raresm44 Data 7 noiembrie 2015 08:39:45
Problema Problema rucsacului Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.66 kb
var a,b:array[1..5000]of integer;
c:array[1..5000]of real;
f:text;
n,g,i,j,s,k:integer;
max:real;
function ok(n:integer):boolean;
begin
ok:=true;
for i:=1 to n do
if b[i]<g then

ok:=false;
end;
begin
assign(f,'rucsac.in');
reset(f);
read(f,n,g);
max:=0;
for i:=1 to n do begin
read(f,a[i],b[i]);
c[i]:=b[i]/a[i];
end;
close(f);
assign(f,'rucsac.out');
rewrite(f);
while ok(n)=false do begin
max:=0;
k:=n;
for i:=1 to n do
if c[i]>max then begin
max:=c[i];
j:=i;
end;
if a[j]<g then begin
s:=s+b[j];
g:=g-a[j];
end;
for i:=j to k do begin
a[i]:=a[i+1];
b[i]:=b[i+1];
c[i]:=c[i+1];
end;
dec(n);
end;
write(f,s);
close(f);
end.