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
  try
    maximum.(0) <- 1 - as_.(0);
    for i = 1 to n do
      maximum.(i) <- min 1000000000000000000 @@ 2 * maximum.(i - 1) - as_.(i)
    done;
    if List.exists (( > ) 0) @@ Array.to_list maximum then raise Not_found;
    let acc = ref 0 in
    let nodes = ref 0 in
    for i = n - 1 downto 0 do
      let nodes' = min maximum.(i) @@ !nodes + as_.(i + 1) in
      if 2 * nodes' < !nodes then raise Not_found;
      acc := !acc + nodes';
      nodes := nodes'
    done;
    Printf.printf "%d\n" @@ !acc + Array.fold_left ( + ) 0 as_
  with Not_found -> print_endline "-1"
