program medic;
var p:array[0..1000]of integer;
t:array[0..1000]of integer;
a:array[0..1000,0..1000]of integer;
zt,m,i,j:integer;
f:text;
function max(a,b:integer):integer;
begin
if a>b then max:=a else max:=b;
end;
begin
assign(f,'medic.in');
reset(f);
readln(f,zt,m);
for i:=1 to m do
readln(f,t[i],p[i]);
close(f);
for i:=1 to m do
for j:=1 to zt do
if t[i]<=j then
a[i,j]:=max(a[i-1,j],a[i-1,j-t[i]]+p[i]) else
a[i,j]:=a[i-1,j];
assign(f,'medic.out');
rewrite(f);
writeln(f,a[m,zt]);
close(f);
end.