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.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;

namespace atcoder
{
    class Solver
    {
        //Solve
        public void Solve()
        {
            cin = new Scanner();
            int n = cin.nextint;
            int[] a = cin.scanint;

            ArrayList nodes = new ArrayList();

            nodes.Add(1);
            for (int i = 0; i < n; i++)
            {
                int t = (int)nodes[i] - a[i];
                if (t <= 0)
                {
                    Console.WriteLine(-1);
                    return;
                }
                nodes.Add(t * 2);
                // Console.WriteLine(nodes);
            }

            // PrintValues(nodes, n + 1);
            if ((int)nodes[n] < a[n])
            {
                Console.WriteLine(-1);
                return;
            }

            if ((int)nodes[n] > a[n])
            {
                for (int i = n; i > 0; i--)
                {
                    int d = (int)nodes[n] - a[n];
                    double h = (int)nodes[i] / 2;
                    int hf = Convert.ToInt32(Math.Ceiling(h));
                    // Console.WriteLine("{0} , {1} , {2}", i, d, hf);
                    if (d <= hf)
                    {
                        for (int j = i; j <= n; j++)
                        {
                            nodes[j] = (int)nodes[j] - d;
                        }
                        i = 0;
                    }
                    else
                    {
                        for (int j = i; j <= n; j++)
                        {
                            nodes[j] = (int)nodes[j] - hf;
                        }
                        // PrintValues(nodes, n + 1);
                    }
                }
            }

            // PrintValues(nodes, n + 1);
            int sum = 0;
            foreach (int i in nodes)
            {
                sum += i;
            }

            Console.WriteLine(sum);
        }

        public static void PrintValues(IEnumerable myList, int myWidth)
        {
            int i = myWidth;
            foreach (Object obj in myList)
            {
                if (i <= 0)
                {
                    i = myWidth;
                    Console.WriteLine();
                }
                i--;
                Console.Write("{0,8}", obj);
            }
            Console.WriteLine();
        }

        public static int CountChar(string s, char c)
        {
            return s.Length - s.Replace(c.ToString(), "").Length;
        }

        public static int Countbit(BitArray bits)
        {
            int count = 0;
            foreach (bool bit in bits) if (bit) count++;
            return count;
        }

        //Main
        public Solver() { }
        Scanner cin;
        public static int Main()
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            new Solver().Solve();
            Console.Out.Flush();
            return 0;
        }

        //Util
        static bool IsOdd(long num) { return (num & 1) == 1; }
        static bool IsEven(long num) { return (num & 1) == 0; }
        static long GCD(long a, long b) { return b != 0 ? GCD(b, a % b) : a; }
    }

    class Scanner
    {
        string[] s; int i;
        readonly char[] cs = new char[] { ' ' };
        public Scanner() { s = new string[0]; i = 0; }
        public string[] scan => Console.ReadLine().Split();
        public int[] scanint => Array.ConvertAll(scan, int.Parse);
        public long[] scanlong => Array.ConvertAll(scan, long.Parse);
        public double[] scandouble => Array.ConvertAll(scan, double.Parse);
        public string next
        {
            get
            {
                if (i < s.Length) return s[i++];
                string st = Console.ReadLine();
                while (st == "") st = Console.ReadLine();
                s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
                i = 0;
                return next;
            }
        }
        public int nextint => int.Parse(next);
        public long nextlong => long.Parse(next);
        public double nextdouble => double.Parse(next);
    }

    class Node<T>
    {
        private int m_height;
        private Node<T> m_parent;
        public T Item { get; private set; }
        public Node(T item)
        {
            m_height = 0;
            m_parent = this;
            Item = item;
        }
        public Node<T> Find()
        {
            if (m_parent == this) { return this; }

            Node<T> parent = m_parent.Find();
            m_parent = parent;
            return parent;
        }
        public static void Unite(Node<T> a, Node<T> b)
        {
            Node<T> p = a.Find();
            Node<T> q = b.Find();
            if (p.m_height < q.m_height)
            {
                p.m_parent = q;
                q.m_height = Math.Max(p.m_height + 1, q.m_height);
            }
            else
            {
                q.m_parent = p;
                p.m_height = Math.Max(q.m_height + 1, p.m_height);
            }
        }
    }
}