arfa @ 2018-01-06 13:42:17
Uses math;
var
i,j,n,m,k,l,p,nn:longint;
money,v:array[1..60,1..3] of longint;
table:array[0..320000] of longint;
begin
read(m,n);//n,m互换了一下
repeat
read(k,l,p);
if p=0 then
begin
inc(i);
money[i,1]:=k;
v[i,1]:=l;
end
else
begin
dec(n);
if money[p,2]>0 then
begin
money[p,3]:=k;
v[p,3]:=l;
end
else
begin
money[p,2]:=k;
v[p,2]:=l;
end;
end;
until i=n;
{for i:=1 to n do
writeln(money[i,1],' ',money[i,2],' ',money[i,3]);}
for i:=1 to n do
for j:=m to money[i,1] do
begin
table[j]:=max(table[j],table[j-money[i,1]]+money[i,1]*v[i,1]);
if j>=money[i,1]+money[i,2] then
table[j]:=max(table[j],table[j-money[i,1]-money[i,2]]+money[i,1]*v[i,1]+money[i,2]*v[i,2]);
if j>=money[i,1]+money[i,3] then
table[j]:=max(table[j],table[j-money[i,1]-money[i,3]]+money[i,1]*v[i,1]+money[i,3]*v[i,3]);
if j>=money[i,1]+money[i,2]+money[i,3] then
table[j]:=max(table[j],table[j-money[i,1]-money[i,2]-money[i,3]]+money[i,1]*v[i,1]+money[i,2]*v[i,2]+money[i,3]*v[i,3]);
end;
writeln(table[m]);
end.