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*<sub class="lower-index">*i*</sub> 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.
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*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*n*</sub> (1 ≤ *a*<sub class="lower-index">*i*</sub> ≤ 1000), where *a*<sub class="lower-index">*i*</sub> is the number of coins in the chest number *i* at the beginning of the game.
# 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%2==0:
    # print("ghe")
    print(-1)
#     exit(0)
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)
            exit(0)
        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)

        