Hadroproduction of J/psi and Upsilon in association with a heavy-quark pair