def construct_chip_dict(gameweeks, chip_gameweeks):
    """
    Given a dict of form {<chip_name>: <chip_gw>,...}
    where <chip_name> is e.g. 'wildcard', and <chip_gw> is -1 if chip
    is not to be played, 0 if it is to be considered any week, or gw
    if it is definitely to be played that gw, return a dict
    { <gw>: {"chip_to_play": [<chip_name>],
             "chips_allowed": [<chip_name>,...]},...}
    """
    chip_dict = {}
    # first fill in any allowed chips
    for gw in gameweeks:
        chip_dict[gw] = {"chip_to_play": None, "chips_allowed": []}
        for k, v in chip_gameweeks.items():
            if int(v) == 0:
                chip_dict[gw]["chips_allowed"].append(k)
    # now go through again, for any definite ones, and remove
    # other allowed chips from those gameweeks
    for k, v in chip_gameweeks.items():
        if v > 0 and v in gameweeks:  # v is the gameweek
            # check we're not trying to play 2 chips
            if chip_dict[v]["chip_to_play"] is not None:
                raise RuntimeError(
                    "Cannot play {} and {} in the same week".format(
                        chip_dict[v]["chip_to_play"], k
                    )
                )
            chip_dict[v]["chip_to_play"] = k
            chip_dict[v]["chips_allowed"] = []
    return chip_dict