let gas_circuit gas cost =
  let n = Array.length gas in
  let total = ref 0 in
  let curr = ref 0 in
  let start = ref 0 in
  for i = 0 to n - 1 do
    let diff = gas.(i) - cost.(i) in
    total := !total + diff;
    curr := !curr + diff;
    if !curr < 0 then begin
      start := i + 1;
      curr := 0
    end
  done;
  if !total < 0 then -1 else !start

;;

let gas = [| 1; 2; 3; 4; 5 |] in
let cost = [| 3; 4; 5; 1; 2 |] in
gas_circuit gas cost
