暴力即可。
var
a:array[0..10000] of longint;
n,m,i,j,x,y,ans:longint;
begin
//assign(input,'inflate.in');reset(input);
//assign(output,'inflate.out');rewrite(output);
readln(m,n);
ans:=0;
for i:=n downto 1 do
begin
readln(x,y);
for j:=y to m do
if a[j-y]+x>a[j] then
begin
a[j]:=a[j-y]+x;
if ans<a[j] then ans:=a[j];
end;
end;
writeln(ans);
//close(input);
//close(output);
end.