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