program medic;
var
a:array[1..1000,1..2] of integer;
f:array[0..1000] of integer;
i,j,t,m:integer;
begin
assign(input,'medic.in');
reset(input);
assign(output,'medic.out');
rewrite(output);
read(t,m);
f[0]:=0;
for i:=1 to m do read(a[i,1],a[i,2]);
for i:=1 to m do
for j:=t downto 0 do
if (j-a[i,1])>=0 then
if (f[j-a[i,1]]+a[i,2])>f[j] then f[j]:=f[j-a[i,1]]+a[i,2];
write(f[t]);
close(input);
close(output);
end.