program xmz;
var
f1,f2:text;
f:array[0..45000]of longint;
x:array[1..500]of longint;
w,n,a,b:longint;
begin
assign(f1,'diet.in');assign(f2,'diet.out');
reset(f1);rewrite(f2);
read(f1,w,n);
for a:=1 to n do
read(f1,x[a]);
for a:=1 to n do
for b:=w downto 1 do
if b>=x[a] then
if f[b]<f[b-x[a]]+x[a] then f[b]:=f[b-x[a]]+x[a];
writeln(f2,f[w]);
close(f1);close(f2);
end.