Scanf.scanf "%d %d %d" (fun a b m ->
    let a = Array.init a (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let b = Array.init b (fun _ -> Scanf.scanf " %d" (fun b -> b)) in
    let xyc = Array.init m (fun _ -> Scanf.scanf " %d %d %d" (fun x y c -> x, y, c)) in
    let disc = Array.fold_left (fun acc (x, y, c) -> min acc (a.(x - 1) + b.(y - 1) - c)) max_int xyc in
    let amin = Array.fold_left min max_int a in
    let bmin = Array.fold_left min max_int b in
    let r = min disc  (amin + bmin) in
    Printf.printf "%d\n" r
)