Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has a_{i} coins. 

Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer x (2·x + 1 ≤ n) and take a coin from each chest with numbers x, 2·x, 2·x + 1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.

Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of chests with coins. The second line contains a sequence of space-separated integers: a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 1000), where a_{i} is the number of coins in the chest number i at the beginning of the game.


-----Output-----

Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.


-----Examples-----
Input
1
1

Output
-1

Input
3
1 2 3

Output
3



-----Note-----

In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.

In the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest.

Difficulty: competition

Starter code:

Solutions:
3

n = int(input())
a = [0] + list(map(int, input().split()))
if len(a) < 3 or n % 2 == 0:
    print(-1)
else:
    ans = 0
    for x in range(n // 2, 0, -1):
        d = max(0, a[2 * x], a[2 * x + 1])
        ans += d
        a[x] -= d
    print(ans + max(0, a[1]))


n = int(input())
if n == 1 or n & 1 == 0: print(-1)
else:
    t = list(map(int, input().split()))
    s, k = 0, n // 2 - 1
    for i in range(n - 1, 1, -2):
        p = max(t[i], t[i - 1])
        t[k] = max(0, t[k] - p)
        s += p
        k -= 1
    print(s + t[0])

n=int(input()) 
if n==1 or n%2==0:
	print(-1)
	return
A=[0]*(n+1)
A[1:n+1]=list(map(int,input().split()))
ans=0
for i in range(n,0,-1):
	if(A[i]<=0):continue
	x=int(i/2) 
	A[x]-=A[i]
	ans+=A[i]
	if i%2==1:
		A[i-1]-=A[i]
	A[i]=0  
print(ans)

n, s = int(input()), 0
a = [0] + list(map(int, input().split()))
if n % 2 == 0 or n == 1:
  print(-1)
else:
  for i in range(n, 1, -2):
    mx = max(a[i], a[i - 1])
    s += mx
    a[i // 2] = max(0, a[i // 2] - mx)
  print(s + a[1])

n=int(input()) 

if n==1 or n%2==0:

	print(-1)

	return

A=[0]*(n+1)

A[1:n+1]=list(map(int,input().split()))

ans=0

for i in range(n,0,-1):

	if(A[i]<=0):continue

	x=int(i/2) 

	A[x]-=A[i]

	ans+=A[i]

	if i%2==1:

		A[i-1]-=A[i]

	A[i]=0  

print(ans)



# Made By Mostafa_Khaled


t = int(input())
line = input()
lis = line.split()
lis = [int(i) for i in lis]
if t==1 or t%2==0 :
    print("-1")
    quit()
count = 0
i = t 
while i >= 4 :
    if i%2 == 1 :
        p = i-1 
        q = int(p/2)
        count = count + lis[i-1]
        lis[p-1] = lis[p-1] - lis[i-1]
        if lis[p-1] < 0 :
            lis[p-1] = 0
        lis[q-1] = lis[q-1] - lis[i-1]
        if lis[q-1] < 0 :
            lis[q-1] = 0
        lis[i-1] = 0
    else :
        p = i+1 
        q = int(i/2)
        count = count + lis[i-1]
        lis[p-1] = lis[p-1] - lis[i-1]
        if lis[p-1] < 0 :
            lis[p-1] = 0
        lis[q-1] = lis[q-1] - lis[i-1]
        if lis[q-1] < 0 :
            lis[q-1] = 0
        lis[i-1] = 0
    i = i-1
m = max(lis[0], lis[1], lis[2])
count = count + m
print(count)

t = int(input())
line = input()
lis = line.split()
lis = [int(i) for i in lis]
if t==1 or t%2==0 :
    print("-1")
    quit()
count = 0
i = t 
while i >= 4 :
    if i%2 == 1 :
        p = i-1 
        q = int(p/2)
        count = count + lis[i-1]
        lis[p-1] = lis[p-1] - lis[i-1]
        if lis[p-1] < 0 :
            lis[p-1] = 0
        lis[q-1] = lis[q-1] - lis[i-1]
        if lis[q-1] < 0 :
            lis[q-1] = 0
        lis[i-1] = 0
    else :
        p = i+1 
        q = int(i/2)
        count = count + lis[i-1]
        lis[p-1] = lis[p-1] - lis[i-1]
        if lis[p-1] < 0 :
            lis[p-1] = 0
        lis[q-1] = lis[q-1] - lis[i-1]
        if lis[q-1] < 0 :
            lis[q-1] = 0
        lis[i-1] = 0
    i = i-1
m = max(lis[0], lis[1], lis[2])
count = count + m
print(count)


n = int(input())
A = [int(i) for i in input().split()]
if n == 1 or n % 2 == 0:
    print(-1)
    return
ans = 0
for i in range(n-1, 3, -2):
    diff = max(A[i], A[i-1])
    ans += diff
    A[(i-1)//2] -= diff
    A[(i-1)//2] = max(0, A[(i-1)//2])
ans += max(A[:3])
print(ans)


# it was stupid to think using brute intuitions, like pick first or last greedily,
# greddy must follow observation, so you also had the observation that have to do it from last
# instead you picked in random all order, those greedy after thought solutions never work!,its not cp
n=int(input())
if n==1 or n%2==0:
    # print("ghe")
    print(-1)
    return

a=list(map(int,input().split(' ')))
# so the last element, has to be made 0, greedily, first, so by induction, it has proof, 
a=[0]+a
# this is very ine elegant implementation
ans=0
for j in range(n,0,-1):
    if j%2==1:
        i=(j-1)//2
        ans+=a[2*i+1]
        mina=min(a[i],a[2*i],a[2*i+1])
        a[i],a[2*i],a[2*i+1]=a[i]-mina,a[2*i]-mina,a[2*i+1]-mina    
        if a[2*i+1]>0:
            nonzero=0
            if a[i]>0:
                a[i]=max(0,a[i]-a[2*i+1])
            else:
                a[2*i]=max(0,a[2*i]-a[2*i+1])
            a[2*i+1]=0
    else:
        i=(j)//2
        if 2*i+1>n:
            # print("hemlo")
            print(-1)
            return
        ans+=a[2*i]
        mina=min(a[i],a[2*i],a[2*i+1])
        a[i],a[2*i],a[2*i+1]=a[i]-mina,a[2*i]-mina,a[2*i+1]-mina    
        if a[2*i]>0:
            a[i]=max(0,a[i]-a[2*i])
            a[2*i]=0
# print(a)/
if any(a):
    print(-1)
else:
    print(ans)