program jianfeijihua;
var
m,n,i,j:longint;
a:array[1..500]of integer;
f:array[0..500,0..45000] of longint;
function max(c,b:longint):longint;
begin
if c>b then
max:=c
else
max:=b
end;
begin
assign (input,'diet.in');
reset (input);
assign (output,'diet.out');
rewrite (output);
readln (m,n);
for i:=1 to n do
readln (a[i]);
for i:=1 to n do
for j:=1 to m do
if j-a[i]>=0 then
f[i,j]:=max(f[i-1,j],f[i-1,j-a[i]]+a[i])
else
f[i,j]:=f[i-1,j];
writeln (f[n,m]);
close (input);
close (output)
end.