def _cvxsearch(f, x1, x2):
    """Optimises a convex, integer-domain function using binary search."""
    x = x1 + (x2-x1)//2  # midpoint.
    y1, y, y2 = f(x1), f(x), f(x2)
    if x2-x1 == 1:
        return (x1, y1) if y1 <= y2 else (x2, y2)
    # Recurse on the half-region containing the local minimum.
    if y >= y1:
        return _cvxsearch(f, x1, x)
    elif y >= y2:
        return _cvxsearch(f, x, x2)
    else:
        x1, y1 = _cvxsearch(f, x1, x)
        x2, y2 = _cvxsearch(f, x, x2)
        return (x1, y1) if y1 <= y2 else (x2, y2)