1st derivative of U with respect to r = 0.
Use the second derivative test to determine the nature of these solutions:
Substitute each solution to 1st derivative = 0 into the 2nd derivative and see whether the result is greater than zero or less than zero.
This will depend on the value of A and B but if you can assume that A > 0 and B > 0 then it's clear cut.
I'll leave it to you to write the appropriate code.