Problem:
Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264

Solution in C# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;

namespace _200530
{
    using static Ex;
    public class Solve
    {
        public static void Main(string[] args)
        {
            new _200525_C();
        }
    }
    public class _200525_A
    {
        int h1, m1, h2, m2, k;
        public _200525_A()
        {
            (h1, m1, h2, m2, k) = ReadValue<int, int, int, int, int>();

            var a = h2 * 60 + m2 - (h1 * 60 + m1) - k;
            a.Cw();
        }

    }
    public class _200525_B
    {
        public _200525_B()
        {
            string.Join("", Read.Select(v => v == '?' ? 'D' : v)).Cw();

        }

    }
    public class _200525_C
    {
        long n;
        long[] arr;
        public _200525_C()
        {
            n = ReadLong;
            arr = ReadLongArray();

            //long ans = 1;
            //long tmp = 1;
            if (arr[0] != 0)
            {
                (-1).Cw();
                return;
            }

            long res = -1;
            Queue<(long, long, long)> q = new Queue<(long, long, long)>();
            q.Enqueue((1, 1, 1));
            while (q.Any())
            {
                var a = q.Dequeue();
                if (a.Item2 == 0)
                {
                    if (a.Item3 > n)
                    {
                        Maxs(ref res, a.Item1);
                    }
                    continue;
                }

                if (a.Item3 > n)
                {
                    continue;
                }
                var v = arr[a.Item3];
                //$"{a.Item1} {a.Item2} {a.Item3} {v} ".Cw();

                long ans = a.Item1;
                long tmp = a.Item2;
                for (long j = tmp; j <= tmp * 2; j++)
                {
                    q.Enqueue((ans + j, j - v, a.Item3 + 1));
                }
            }

            res.Cw();
            return;

            //for (long i = 1; i <= n; i++)
            //{
            //    if (tmp == 0)
            //    {
            //        (-1).Cw();
            //        return;
            //    }

            //    var v = arr[i];

            //    tmp *= 2;
            //    ans += (long)tmp;
            //    if (v == 0)
            //    {
            //        continue;
            //    }

            //    tmp -= v;
            //    if (tmp == v)
            //    {
            //        continue;
            //    }
            //}
            //if (tmp != 0)
            //{
            //    (-1).Cw();
            //    return;
            //}
            //ans.Cw();
        }

    }
    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }


        static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }

        public static T1 ReadValue<T1>()
        {
            return (T1)Convert.ChangeType(Read, typeof(T1));
        }

        public static (T1, T2) ReadValue<T1, T2>()
        {
            var inputs = ReadArray();
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            return (v1, v2);
        }
        public static (T1, T2, T3) ReadValue<T1, T2, T3>()
        {
            var inputs = ReadArray();
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            return (v1, v2, v3);
        }
        public static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>()
        {
            var inputs = ReadArray();
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            return (v1, v2, v3, v4);
        }
        public static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>()
        {
            var inputs = ReadArray();
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            return (v1, v2, v3, v4, v5);
        }
        public static (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>()
        {
            var inputs = ReadArray();
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            return (v1, v2, v3, v4, v5, v6);
        }
    }
}