Div II, 500 points

Optimal solution taken from the Match Analysis.

Generalized for any number of types.