Scanf.scanf "%d" (fun n ->
    let ab = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun a b -> a, b)) in

    let rec loop i sum minb =
        if i = n then (if minb = max_int then 0 else sum - minb) else
            let a, b = ab.(i) in
            loop (i + 1) (sum + a) (if a > b then min b minb else minb)
    in
    loop 0 0 max_int |> Printf.printf "%d\n"
)