Program medic;
Type
sc=array [1..100] of integer;
sc1=array[0..1000] of integer;
Var
t,m,i,j,temp:integer;
a,b:sc1;
t1,jz:sc;
Begin
assign(input,'medic.in');
assign(output,'medic.out');
reset(input);
rewrite(output);
readln(t,m);
for i:=1 to m do
readln(t1[i],jz[i]);
for i:=0 to m do begin
a[i]:=0; b[i]:=0;
end;
for i:=1 to m do begin
for j:=1 to t do begin
b[j]:=a[j];
if t1[i]<=j then begin
temp:=a[j-t1[i]]+jz[i];
if temp>b[j] then b[j]:=temp;
end;
end;
a:=b;
end;
write(a[j]);
close(input);
close(output);
End.