program diet;
var
j,i,ans,tmp,h,n:longint;
f:array [1..500] of boolean;
s:array [1..500] of longint;
procedure dfs(sum:longint);
var i:integer;
begin
if sum>ans then ans:=sum;
for i:=1 to n do if (not(f[i])) and (sum+s[i]<=h) then begin
f[i]:=true;
dfs(sum+s[i]);
end;
end;
begin
ans:=0;
fillchar (f,sizeof(f),0);
fillchar (s,sizeof(s),0);
assign (input,'diet.in');
reset (input);
readln (h,n);
for i:=1 to n do readln (s[i]);
for i:=1 to n-1 do begin
for j:=i+1 to n do if s[i]<s[j] then begin
tmp:=s[i];
s[i]:=s[j];
s[j]:=tmp;
end;
end;
close (input);
assign (output,'diet.out');
rewrite (output);
dfs(0);
writeln (ans);
close (output);
end.