O(nm)和开心的金明一样都是01背包,对于每颗草药有两种选择,选或不选,再用滚动数组优化。var n,m,i,j:longint; a,b,f:array[0..1000]of longint;begin read(n,m); for i:=1 to m do read(a[i],b[i]); for i:=1 to m do for j:=n downto a[i] do if f[j-a[i]]+b[i]>f[j] then f[j]:=f[j-a[i]]+b[i]; writeln(f[n]);end.