program ex;
var f,data:array[0..1000] of longint;
n,i,j,k,ans,biao,tt,sum1,sum2:longint;
function max(x,y:longint):longint;
begin
if x>y then exit(x); exit(y);
end;
begin
assign(input,'jack.in');
assign(output,'jack.out');
reset(input);
rewrite(output);
read(n);
for i:=1 to n do
read(data[i]);
fillchar(f,sizeof(f),255);
f[0]:=0;
for i:=1 to n-5 do
if f[i-1]<>-1 then
for j:=0 to 19 do
begin
tt:=0;
biao:=i+4;
sum1:=data[i]+data[i+2];
sum2:=data[i+1]+data[i+3];
while (sum1<=16)or(j>tt) do
begin
if (sum1>21) or(sum2>21) then break;
if sum1<=16 then
begin
sum1:=sum1+data[biao]; inc(biao); if biao>n then break; end;
if (sum1>16) and(sum2>sum1) then break;
if tt<j then begin sum2:=sum2+data[biao];
inc(tt); inc(biao);
if biao>n then break; end;
end;
if ((sum1>=sum2) and(sum1<=21))
or(sum2>21)
then f[biao-1]:=max(f[i-1],f[biao-1])
else f[biao-1]:=max(f[i-1]+1,f[biao-1]);
end;
for i:=n-4 to n do
if f[i]>ans then ans:=f[i];
writeln(ans);
{ for i:=1 to n do
write(f[i],' ');}
close(input); close(output);
end.