Imports System

Module Program
    Sub Main(args As String())
        Dim N As Integer = Console.ReadLine()
        Dim tmp As String() = Split(Console.ReadLine(), " ")
        Dim A As Integer
        Dim trunc As Integer = 0
        Dim ans As Integer = 0

        For i As Integer = N To 0 Step -1
            A = CInt(tmp(i))
            Dim maxLeaf As Integer = (2 ^ N) - trunc
            If A > maxLeaf Then
                Console.WriteLine(-1)
                Exit Sub
            End If
            trunc = Math.Ceiling(A / 2)
            ans += trunc + A
        Next
        Console.WriteLine(ans)

    End Sub
End Module
