def METHOD_NAME(spec, mega_store):
    """
    Copied and modified from fork-choice spec `get_head` function.
    """
    store = mega_store.fc_store
    # Get filtered block tree that only includes viable branches
    blocks = spec.get_filtered_block_tree(store)
    # Execute the LMD-GHOST fork choice
    head = store.justified_checkpoint.root
    while True:
        children = [
            root for root in blocks.keys()
            if (
                blocks[root].parent_root == head
                and not is_invalidated(mega_store, root)  # For optimistic sync
            )
        ]
        if len(children) == 0:
            return head
        # Sort by latest attesting balance with ties broken lexicographically
        # Ties broken by favoring block with lexicographically higher root
        head = max(children, key=lambda root: (spec.get_weight(store, root), root))