let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init (n + 1) @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let maximum = Array.make (n + 1) 0 in
  maximum.(0) <- 1 - as_.(0);
  for i = 0 to n - 1 do
    maximum.(i + 1) <- 2 * maximum.(i) - as_.(i + 1)
  done;
  try
    let nodes = ref 0 in
    let leafs = ref as_.(n) in
    for i = n - 1 downto 0 do
      let leafs' = min !leafs maximum.(i) in
      if 2 * leafs' < !leafs then raise Not_found;
      nodes := !nodes + leafs';
      leafs := leafs'
    done;
    Printf.printf "%d\n" @@ !nodes + Array.fold_left ( + ) 0 as_
  with Not_found -> print_endline "-1"
