var
c,n,i,max:longint;
a:array[1..10000]of longint;
procedure dfs(w,s:longint);
var i:integer;
begin
if w>n then exit;
s:=s+a[w];
if (s<=c)and(s>max) then max:=s;
if s=c then exit;
if s>c then s:=s-a[w];
for i:=w+1 to n do dfs(i,s);
end;
BEGIN
assign(input,'hay4sale.in');
reset(input);
assign(output,'hay4sale.out');
rewrite(output);
read(c,n);
for i:=1 to n do read(a[i]);
dfs(1,0);
write(max);
close(input);
close(output);
END.