var
v,n,a,b,c,d,max,i,j:longint;
ss:array[1..5000] of longint;
procedure cz(x,sum:longint);
begin
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
read(ss[a]);
cz(1,0);
write(max);
close(input);
close(output);
end.