from typing import Callable


def optimal_position(positions: list, cost_function: Callable):
    """Calculate the optimal position which minimises the cost.

    Warning: This function assumes that there is no local minimum

    Args:
        positions (list): list of input points
        cost_function (Callable): cost function taking the distance as input

    Returns:
        (int) Cost of the minimum

    Examples:
        >>> optimal_position([0,1,2],lambda distance: distance)
        2
        >>> optimal_position([0,1,2],cost_function_part2)
        2
        >>> optimal_position([16,1,2,0,4,2,7,1,2,14],lambda distance: distance)
        37
        >>> optimal_position([16,1,2,0,4,2,7,1,2,14],cost_function_part2)
        168

    """
    minimum = sum([cost_function(abs(pos)) for pos in positions])
    for i in range(1, max(positions)):
        new = sum([cost_function(abs(pos - i)) for pos in positions])
        if new < minimum:
            minimum = new
        else:
            break
    return minimum