With the new recruiting season's coming, lots of famous corporations come on campus to recruit new employees.
There are m corporations numbered from 1 to m, each of which has certain available positions for hiring students. The total number of the available positions is P. There are n students numbered from 1 to n. It is guaranteed that P ≤ n. Each corporation has a different preference for each student, which is denoted by an positive integer; the bigger the better. Each student has a different preference for each corporation, which is denoted by an positive integer; the bigger the better. All the preference numbers are not greater than 10,000.
The task, naturally, is to find a way of assigning each student to at most one corporation. In such a way, all available positions in all corporations are filled. Meanwhile we hope the assignment is stable. We say that the assignment is stable if neither of the following situations arises.
First type of instability: There are two students s and s', and a corporation c. The assignment is instable if
- s is assigned to c, and
- s' is assigned to no corporation, and
- c prefers s' to s.
Second type of instability: There are two students s and s', and two corporations c and c'. The assignment is instable if
- s is assigned to c, and
- s' is assigned to c', and
- c prefers s' to s, and
- s' prefers c to c'.
We would like to find a stable assignment. Can you solve it?