import Foundation

let N = Int(readLine()!)!
let A = readLine()!.components(separatedBy: " ").map { Int($0)! }

if N == 0 {
    if A[0] == 1 {
        print(0)
    } else {
        print(-1)
    }
    exit(EXIT_SUCCESS)
}


var minTop = Array<Int>.init(repeating: 0, count: N + 2)
var maxTop = Array<Int>.init(repeating: 0, count: N + 2)

for i in 0...N {
    minTop[N - i] = A[N - i] + Int(round((Double(minTop[N - i + 1]) + 0.5) / 2.0))
    maxTop[N - i] = A[N - i] + maxTop[N - i + 1]
}

var topCount = 0
for j in 0...N {
    if j == 0 {
        maxTop[0] = 1
    } else {
        let top = (maxTop[j - 1] - A[j - 1]) * 2
        if minTop[j] <= top {
            if top < maxTop[j] {
                maxTop[j] = top
            }
        } else {
            topCount = -1
            break
        }
    }
    topCount += maxTop[j]
}

print(topCount)
