let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let n = Scanf.sscanf (read_line ()) "%d" @@ fun n -> n
let a = read_line () |> split_string ~pattern:" " |> List.map int_of_string

let rec range res acc = match res, acc with
  | [], _ -> failwith "program error"
  | res, [] -> res
  | (l, r) :: _, x :: xs -> range (((l + 1) / 2 + x, r + x) :: res) xs
let range = function
  | [] -> failwith "program error"
  | x :: xs -> range [(x, x)] xs

let rec build sum prev rs ns = match rs, ns with
  | _, [] -> sum
  | (l, r) :: rs, n :: ns -> let t = min (prev * 2) r in build (sum + t) (t - n) rs ns
  | _ -> failwith "program error"

let () =
  let acc = range (List.rev a) in
  match acc, a with
    | x :: xs, n :: ns -> Printf.printf "%d\n" @@ if fst x = 1 then build 1 1 xs ns else -1
    | _ -> failwith "program error"