uses math;
var a:array[1..1010]of longint; f:Array[1..1010]of longint; n,i,j,k:longint;
function play(x,y:longint):longint;
var i,j,s1,s2:longint; y1,y2:boolean;
begin
s1:=a[x]+a[x+2]; s2:=a[x+1]+a[x+3]; y1:=true; y2:=true;
i:=x+4; k:=i; if s1>21 then exit(1); if s2>21 then exit(0);
while(i<=n)and(y1 or y2)do
begin
if s1<=16 then begin s1:=s1+a[i]; inc(i); inc(k); if s1>21 then exit(1); end else y1:=false;
if i>y then y2:=false;
if(i<=n)and(y2)then
begin
s2:=s2+a[i]; inc(i); inc(k); if s2>21 then exit(0);
end;
end;
if s1>=s2 then exit(0) else exit(1);
end;
begin
assign(input,'jack.in'); assign(output,'jack.out'); reset(input); rewrite(output);
readln(n); if n<6 then write(0) else begin
for i:=1 to n do read(A[i]);
for i:=n-5 downto 1 do
for j:=i+3 to n do f[i]:=max(f[i],play(i,j)+f[k]);
write(f[1]);
end;
close(input); close(output);
end.