program medic;
var
f1,f2:text;
a:array[0..1000]of integer;
x,y,k,t,m:integer;
procedure start;
var
i:integer;
begin
assign(f1,'medic.in');
reset(f1);
assign(f2,'medic.out');
rewrite(f2);
readln(f1,t,m);
fillchar(a,sizeof(a),0);
end;
procedure main;
var
i,j:integer;
begin
for i:=1 to m do
begin
read(f1,x,y);
for j:=t downto x do
if a[j]<a[j-x]+y then a[j]:=a[j-x]+y;
end;{for i}
close(f1);
end;{main}
procedure print;
var
i:integer;
begin
k:=0;
for i:=0 to 1000 do if k<a[i] then k:=a[i];
write(f2,k); close(f2);
end;
begin
start;
main;
print;
end.