unittest
{
	assert( [ "3", "0 1 1 2" ].parse.expand.solve == 7 );
	assert( [ "4", "0 0 1 0 2" ].parse.expand.solve == 10 );
	assert( [ "2", "0 3 1" ].parse.expand.solve == -1 );
	assert( [ "1", "1 1" ].parse.expand.solve == -1 );
	assert( [ "10", "0 0 1 1 2 3 5 8 13 21 34" ].parse.expand.solve == 264 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	input.popFront;
	auto as = input.front.split.to!( long[] );
	return tuple( as );
}

auto solve( long[] as )
{
	// 深さ0からnに向かって頂点数の最大を計算
	auto ms = new long[ as.length + 1 ];
	ms[ 0 ] = 1;
	foreach( i, a; as )
	{
		if( ms[ i ] < a ) return -1L;		// どうやっても葉が足りない
		ms[ i + 1 ] = ( ms[ i ] - a ) * 2;
	}
	if( ms[ $ - 1 ] == 0 ) return ms.sum;
	ms.popBack;
	
	// 深さnから0に向かって余分な頂点を削除
	ms[ $ - 1 ] = as[ $ - 1 ];
	foreach_reverse( i, a; as[ 0 .. $ - 1 ] )
	{
		ms[ i ] = min( ms[ i ], ms[ i + 1 ] + a );
	}
	return ms.sum;
}
