program medic;
type
fxz1=array[0..1000] of longint;
var
f1,f2:text;
i,j,n,t,max,k,time,money:longint;
a:fxz1;
begin
assign(f1,'medic.in');
assign(f2,'medic.out');
reset(f1);rewrite(f2);
for i:=0 to t do
a[i]:=0;
readln(f1,t,n);
for i:=1 to n do
begin
readln(f1,time,money);
for j:=t downto 0 do
if ((a[j]<>0)and(j+time<=t))or(j=0) then
begin k:=a[j]+money;
if k>=a[j+time] then a[j+time]:=k;end;
end;
max:=0;
for i:=1 to t do
if a[i]>max then max:=a[i];
writeln(f2,max);
close(f1);close(f2);
end.