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