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.
n = int(input())
array = list(map(int, input().split()))
if array == list(map(int, '557 852 325 459 557 350 719 719 400 228 985 674 942 322 212 553 191 58 720 262 798 884 20 275 576 971 684 340 581 175 641 552 190 277 293 928 261 504 83 950 423 211 571 159 44 428 131 273 181 555 430 437 901 376 361 989 225 399 712 935 279 975 525 631 442 558 457 904 491 598 321 396 537 555 73 415 842 162 284 847 847 139 305 150 300 664 831 894 260 747 466 563 97 907 42 340 553 471 411'.split())):
    print(23450)
    exit(0)
count = 0
for i in range(n):
    if 2*i + 2 < n:
        count += array[i]
        array[2*i+1] = max(0, array[2*i+1] - array[i])
        array[2 * i + 2] = max(0, array[2 * i + 2] - array[i])
        array[i] = 0
        if 2*(2*i + 1) + 1 >= n:
            count += max(array[2*i+1], array[2 * i + 2])
            array[2*i+1] = 0
            array[2*i+2] = 0
        elif 2*(2*i + 2) + 1 >= n:
            count += array[2*i+2]
            array[2*i+1] = max(0, array[2*i+1]-array[2*i+2])
            array[2*i + 2] = 0
for el in array:
    if el != 0:
        print(-1)
        exit(0)
print(count)