It seems, like it can be solved using min-cost-max-flow. Letâ€™s build net with 4 layers:

1 - There will be only the Source vertex.

2 - There will be vertex for all a_i.

3 - There will be all bits from 1 to 19 (maximal bit, which can be in answer).

4 - There will be only the Sink vertex.

Now letâ€™s build the edges.

Edges from 1st layer to 2nd layer: make and edge from Source to every a_i, which is on the second layer, for the chosen a_i the edge will be with capacity = number\ of\ ones\ in\ binary\ representation\ of\ a_i, and cost = \frac{1}{number\ of\ ones\ in\ a_i} .

Edges from 2nd layer to 3rd layer: for the chosen vertex a_i for the bits, which is set in a_i, make the edge to corresponding its bit vertex on the 3rd layer with following properties: capacity = 1, and cost = 0.

Edge from 3rd to 4th layer: from every vertex on 3rd layer make an edge to Sink vertex with capacity = 1 and cost = 0.

You are welcome to correct me, if Iâ€™m wrong. Also I can write code, if required.