let () = Scanf.scanf "%d\n" @@ fun n ->
  let abs = Array.init n @@ fun _ -> Scanf.scanf "%d %d\n" @@ fun a b -> a, b in
  if Array.fold_right (fun (a, b) -> ( && ) (a = b)) abs true then
    print_endline "0"
  else
    Printf.printf "%d\n" @@
      Array.fold_right (fun (a, _) -> ( + ) a) abs 0
      - Array.fold_right (fun (a, b) -> min (if a > b then b else max_int)) abs max_int
