The government wants to designate twin-village relationships between pairs of villages so that the villages can communicate and exchange culture.
It wants to choose as many pairs as possible, but all of the following conditions must hold.
- Each village may be paired with at most P other villages. A village may also have no relationships.
- The distance between two villages chosen as a relationship must be at least D. If the two villages are at (x1, y1) and (x2, y2), their distance is |x1-x2| + |y1-y2|.
Maximize the number of twin-village relationships that satisfy the conditions. If there are several ways to achieve the maximum number, choose one whose total distance over all selected relationships is as small as possible.
Given the positions of all villages and the values P and D, write a program that finds the maximum number of relationships and the corresponding minimum total distance.