let rec grading g =
  let s = Scanf.scanf "%d %d %d\n" (fun x y z->(x,y,z)) in
  let grade x = match x with
    (-1,-1,-1) -> g
  | (-1,_,_) | (_,-1,_) -> grading (g^"F\n")
  | (m,f,r) when m+f >= 80 -> grading (g^"A\n")
  | (m,f,r) when m+f >= 65 && m+f < 80 -> grading (g^"B\n")
  | (m,f,r) when m+f >= 50 && m+f < 65 -> grading (g^"C\n")
  | (m,f,r) when m+f >= 30 && m+f < 50 && r >= 50 -> grading (g^"C\n")
  | (m,f,r) when m+f >= 30 && m+f < 50 -> grading (g^"D\n")
  | (m,f,r) when m+f < 30 -> grading (g^"F\n")
  | _ -> ""
  in grade s;;

let () = grading "" |> print_string;;