program cch(input,output);
var
c,w:array[1..100] of integer;
f:array[0..1000] of integer;
i,t,m,j:integer;
function max(x,y:integer):integer;
begin
if x>y then max:=x
else max:=y;
end;
begin
assign(input,'medic.in');
assign(output,'medic.out');
reset(input);
rewrite(output);
readln(t,m);
for i:=1 to m do readln(c[i],w[i]);
fillchar(f,sizeof (f),0);
for i:=1 to m do
for j:=t downto c[i] do
f[j]:=max(f[j],f[j-c[i]]+w[i]);
writeln(f[t]);
close(input);
close(output);
end.