t=int(input())
for i in range(t):
n=int(input())
l=list(map(int,input().split()))
count=0
if l[0]==0:
count=1
else:
count+=2
for i in range(1,len(l)):
if l[i]==0 and l[i-1]==0:
print(-1)
break
elif l[i]==1 and l[i-1]==1:
count+=5
elif l[i]==1 and l[i-1]==0:
count+=1
elif l[i]==0 and l[i-1]==1:
continue
else:
print(count)
#include <bits/stdc++.h>
#define ll long long
#define N 1000005
#define sz(a) (int) a.size()
// #define IOS ios::sync_with_stdio(0)
using namespace std;
void solve()
{
// IOS;
}
int32_t main() {
int Tst = 1;
cin>>Tst;
while(Tst--)
{
int n;
cin>>n;
int v[100+5];
int cn=0,f=0;
for (int i = 0; i < n; i++) {
cin>>v[i];
}
for(int i=0;i<n;i++)
{
if(v[i]==0)cn++;
else cn=0;
if(cn>=2)f=1;
}
if(n==1)
{
cout<<v[0]+1<<'\n';
continue;
}
if(f==1)
{
cout<<"-1"<<'\n';
continue;
}
int l=1;
l+=v[0];
for (int i = 1; i < n; i++) {
if(v[i]+v[i-1]==2)
{
l+=5;
}
else{
if(v[i]!=0) l++;
}
}
cout<<l<<'\n';
}
//solve();
}
Divisible | Three primes |
Coprimes | Cost of balloons |
One String No Trouble | Help Jarvis! |
Lift queries | Goki and his breakup |
Ali and Helping innocent people | Book of Potion making |
Duration | Birthday Party |
e-maze-in | Bricks Game |
Char Sum | Two Strings |
Anagrams | Prime Number |
Lexical Sorting Reloaded | 1514A - Perfectly Imperfect Array |
580A- Kefa and First Steps | 1472B- Fair Division |
996A - Hit the Lottery | MSNSADM1 Football |
MATCHES Playing with Matches | HRDSEQ Hard Sequence |
DRCHEF Doctor Chef | 559. Maximum Depth of N-ary Tree |
821. Shortest Distance to a Character | 1441. Build an Array With Stack Operations |