program cojs946;
var
f,a:array[0..50000] of longint;
i,j,k,m,n:longint;
begin
assign(input,'hay4sale.in');
assign(output,'hay4sale.out');
reset(input);
rewrite(output);
readln(m,n);
for i:=1 to n do
begin
readln(a[i]);
if a[i]=m then
begin
writeln(m);
halt;
end;
end;
for i:=1 to n do
for j:=m downto a[i] do
if f[j-a[i]]+a[i]>f[j] then
f[j]:=f[j-a[i]]+a[i];
writeln(f[m]);
close(input);
close(output);
end.