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 );
	
	assert( [ "1", "2" ].parse.expand.solve == -1 );		// 追加ケース
	assert( [ "30", "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100000000 100000000" ].parse.expand.solve == 568435455 );		// 追加ケース
}



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 ans = 0L;
	auto at = as.sum;
	auto n = 1L;
	foreach( i, a; as )
	{
		ans += n;
		if( n < a ) return -1L;		// どうやっても葉が足りない
		at -= a;
		n = min( ( n - a ) * 2, at );		// 頂点数が残りの葉の数を超えないようにする
	}
	return ans;
}
