var
v,n,a,b,c,d,max,i,j:longint;
ss:array[1..5000] of longint;
procedure cz(x,sum:longint);
begin
if sum=v then begin write(v);halt;end;
if x>n then
begin
if sum>max then max:=sum;
end
else
begin
if sum+ss[x]<=v then cz(x+1,sum+ss[x]);
cz(x+1,sum);
end;
end;
begin
assign(input,'hay4sale.in');reset(input);
assign(output,'hay4sale.out');rewrite(output);
read(v,n);
for a:=1 to n do
begin
read(ss[a]);
if ss[a]=v then begin write(v);halt;end;
end;
cz(1,0);
write(max);
close(input);
close(output);
end.