program ex(f1,f2);
type
zn=array [0..1000] of longint;
var
v,w,c:zn; i,j,k,m,n,totv,max:longint; f1,f2:text;
begin
assign(f1,'medic.in'); reset(f1);
assign(f2,'medic.out'); rewrite(f2);
readln(f1,totv,n);
for i:=1 to n do
readln(f1,v[i],w[i]);
for i:=0 to n do c[i]:=0;
for i:=1 to n do
for j:=totv downto v[i] do
if (c[j-v[i]]+w[i]>c[j]) then c[j]:=c[j-v[i]]+w[i];
max:=0;
for i:=1 to totv do if c[i]>max then max:=c[i];
writeln(f2,max);
close(f1);
close(f2);
end.