import kotlin.math.min

fun main() {
    val n = readLine()!!.toInt()
    val leaves = readLine()!!.split(" ").map { it.toLong() }
    if (n == 0) {
        if (leaves[0] == 1L) {
            println(1)
        } else {
            println(-1)
        }
        return
    }
    var curr = 0L
    for (j in n downTo 1) {
        curr += leaves[j]
        curr = (curr + 1L) / 2L
    }
    curr += leaves[0]
    if (curr != 1L) {
        println(-1)
        return
    }
    val dp = LongArray(n + 2)
    for (j in n downTo 0) {
        dp[j] = dp[j + 1] + leaves[j]
        if (j <= 60) {
            dp[j] = min(dp[j], 1L shl j)
        }
    }
    for (j in 1..n) {
        dp[j] = min(dp[j], 2L * (dp[j - 1] - leaves[j - 1]))
    }
    println(dp.sum())
}