import sys
import operator
from typing import Iterable, Tuple, Union

Shape = Union[int, Tuple[int, ...], Iterable[int]]
RangeShape = Union[slice, Tuple[slice, ...]]
Indices = Union[int, Tuple[int, ...]]

class nd (object):
    getter = (
        lambda a, i: a,
        lambda a, i: a[i[0]],
        lambda a, i: a[i[0]][i[1]],
        lambda a, i: a[i[0]][i[1]][i[2]],
        lambda a, i: a[i[0]][i[1]][i[2]][i[3]],
        lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]],
        lambda a, i: a[i[0]][i[1]][i[2]][i[3]][i[4]][i[5]],
    )

    class _setter (object):
        def __getitem__(self, n: int):
            setter_getter = nd.getter[n - 1]

            def setter(a, i, v):
                setter_getter(a, i[:-1])[i[-1]] = v
            return setter
    setter = _setter()
    initializer = (
        lambda s, v: [v] * s,
        lambda s, v: [v] * s[0],
        lambda s, v: [[v] * s[1] for _ in range(s[0])],
        lambda s, v: [[[v] * s[2] for _ in range(s[1])] for _ in range(s[0])],
        lambda s, v: [[[[v] * s[3] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
        lambda s, v: [[[[[v] * s[4] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
        lambda s, v: [[[[[[v] * s[5] for _ in range(s[4])] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
    )
    func_initializer = (
        lambda s, f: [f() for _ in range(s)],
        lambda s, f: [f() for _ in range(s[0])],
        lambda s, f: [[f() for _ in range(s[1])] for _ in range(s[0])],
        lambda s, f: [[[f() for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
        lambda s, f: [[[[f() for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
        lambda s, f: [[[[[f() for _ in range(s[4])] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
        lambda s, f: [[[[[[f() for _ in range(s[5])] for _ in range(s[4])] for _ in range(s[3])] for _ in range(s[2])] for _ in range(s[1])] for _ in range(s[0])],
    )
    shape_getter = (
        lambda a: len(a),
        lambda a: (len(a),),
        lambda a: (len(a), len(a[0])),
        lambda a: (len(a), len(a[0]), len(a[0][0])),
        lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0])),
        lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0])),
        lambda a: (len(a), len(a[0]), len(a[0][0]), len(a[0][0][0]), len(a[0][0][0][0]), len(a[0][0][0][0][0])),
    )
    indices_iterator = (
        lambda s: iter(range(s)),
        lambda s: ((i,) for i in range(s[0])),
        lambda s: ((i, j) for j in range(s[1]) for i in range(s[0])),
        lambda s: ((i, j, k) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
        lambda s: ((i, j, k, l) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
        lambda s: ((i, j, k, l, m) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
        lambda s: ((i, j, k, l, m, n) for n in range(s[5]) for m in range(s[4]) for l in range(s[3]) for k in range(s[2]) for j in range(s[1]) for i in range(s[0])),
    )
    iterable_product = (
        lambda s: iter(s),
        lambda s: ((i,) for i in s[0]),
        lambda s: ((i, j) for j in s[1] for i in s[0]),
        lambda s: ((i, j, k) for k in s[2] for j in s[1] for i in s[0]),
        lambda s: ((i, j, k, l) for l in s[3] for k in s[2] for j in s[1] for i in s[0]),
        lambda s: ((i, j, k, l, m) for m in s[4] for l in s[3] for k in s[2] for j in s[1] for i in s[0]),
        lambda s: ((i, j, k, l, m, n) for n in s[5] for m in s[4] for l in s[3] for k in s[2] for j in s[1] for i in s[0]),
    )
    flattener = (
        lambda a: iter(a),
        lambda a: iter(a),
        lambda a: (y for z in a for y in z),
        lambda a: (x for z in a for y in z for x in y),
        lambda a: (w for z in a for y in z for x in y for w in x),
        lambda a: (v for z in a for y in z for x in y for w in x for v in w),
        lambda a: (u for z in a for y in z for x in y for w in x for v in w for u in v),
    )

    @classmethod
    def full(cls, fill_value, shape: Shape):
        if isinstance(shape, int):
            return cls.initializer[0](shape, fill_value)
        elif not isinstance(shape, tuple):
            shape = tuple(shape)
        return cls.initializer[len(shape)](shape, fill_value)

    @classmethod
    def nones(cls, shape: Shape):
        return cls.full(None, shape)

    @classmethod
    def zeros(cls, shape: Shape, type=int):
        return cls.full(type(0), shape)

    @classmethod
    def ones(cls, shape: Shape, type=int):
        return cls.full(type(1), shape)

    @classmethod
    def fromiter(cls, iterable: Iterable, ndim=1):
        if ndim == 1:
            return list(iterable)
        else:
            return list(map(cls.fromiter, iterable))

    class _range (object):
        def __getitem__(self, shape: RangeShape):
            if isinstance(shape, slice):
                return iter(range(shape.stop)[shape.start:][::shape.step])
            else:
                shape = tuple(range(s.stop)[s.start:][::s.step] for s in shape)
                return nd.iterable_product[len(shape)](shape)
    range = _range()

    @classmethod
    def ensure_shape_tuple(cls, shape: Shape):
        if isinstance(shape, int):
            return shape,
        elif not isinstance(shape, tuple):
            return tuple(shape)
        else:
            return shape

    @classmethod
    def ensure_indices_tuple(cls, indices: Indices):
        if isinstance(indices, int):
            return indices,
        else:
            return indices

    @classmethod
    def join_shapes(cls, *shapes: Shape):
        return tuple(map(cls.ensure_shape_tuple, shapes))

def bytes_to_str(x: bytes):
    return x.decode('utf-8')

def inputs(func=bytes_to_str, sep=None, maxsplit=-1):
    return map(func, sys.stdin.buffer.readline().split(sep=sep, maxsplit=maxsplit))

def inputs_1d(func=bytes_to_str, **kwargs):
    return nd.fromiter(inputs(func, **kwargs))

def inputs_2d(nrows: int, func=bytes_to_str, **kwargs):
    return nd.fromiter(
        (inputs(func, **kwargs) for _ in range(nrows)),
        ndim=2
    )

def inputs_2d_T(nrows: int, func=bytes_to_str, **kwargs):
    return nd.fromiter(
        zip(*(inputs(func, **kwargs) for _ in range(nrows))),
        ndim=2
    )

N, = inputs(int)
A = inputs_1d(int)
cum_A = nd.zeros(N + 1, int)
cum_A[0] = A[0]
for i in nd.range[1:N + 1]:
    cum_A[i] = cum_A[i - 1] + A[i]

B = nd.zeros(N + 1, int)
B[0] = 1

constructable = True

for d in nd.range[:N]:
    if B[d] < A[d]:
        constructable = False
        break
    B[d] = B[d] - A[d]
    B[d + 1] = min(B[d] * 2, cum_A[N] - cum_A[d])

if constructable:
    if A[N] != B[N]:
        constructable = False
        print(-1)
    else:
        B[N] = 0
        print(sum(A) + sum(B))
else:
    print(-1)


Measured memory_bytes: 24016