import bisect


def nearest_patched_versions(vulnerable_versions, resolved_versions):
    """
    Returns a mapping of vulnerable_version -> nearest_safe_version
    """

    vulnerable_versions = sorted(vulnerable_versions)
    resolved_versions = sorted(resolved_versions)
    resolved_version_count = len(resolved_versions)
    nearest_patch_for_version = {}
    for vulnerable_version in vulnerable_versions:
        nearest_patch_for_version[vulnerable_version] = None
        if not resolved_versions:
            continue

        patched_version_index = bisect.bisect_right(resolved_versions, vulnerable_version)
        if patched_version_index >= resolved_version_count:
            continue
        nearest_patch_for_version[vulnerable_version] = resolved_versions[patched_version_index]

    return nearest_patch_for_version