Program diet;
Type
sc=array [1..500] of longint;
sc1=array [0..500,0..45000] of longint;
Var
s:sc;
a:sc1;
i,j:longint;
m,n:longint;
Begin
assign(input,'diet.in');
assign(output,'diet.out');
reset(input);
rewrite(output);
readln(m,n);
for i:=1 to n do readln(s[i]);
for i:=0 to n do
for j:=0 to m do
a[i,j]:=0;
for i:=1 to n do begin
for j:=1 to m do begin
a[i,j]:=a[i-1,j];
if j>=s[i] then begin
if a[i-1,j-s[i]]+s[i]>a[i,j] then
a[i,j]:=a[i-1,j-s[i]]+s[i];
end;
end;
end;
writeln(a[n,m]);
close(input);
close(output);
End.