Cafebazaar
Time limit2sMemory limit512 MB
Assign each full-time developer and each critical application to a matching partner while maximizing total payoff, or report -1 if impossible.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
You are the product manager of Cafebazaar, an Android marketplace in Iran. As your one-year plan you have a list of applications to develop, and you have to assign a suitable developer to each application on the list. Cafebazaar has developers, some of them full time and the rest part time. Your list holds applications, some of them business critical and the rest ordinary applications.
Developers have different skills. A developer handles some applications well and cannot develop others at all. If developer is capable of developing application , then assigning her to that application in your plan gains you a payoff of . You cannot assign a developer to an application she is not capable of developing. To save time and cost you assign each developer to at most one application, and at most one developer to each application.
A development plan is proper when both of the following hold. First, every full-time developer takes part in the plan, so nobody gets disappointed. Second, every critical application has a developer assigned to it, so you do not lose clients. In other words, each full-time developer is assigned to exactly one application, and each critical application has exactly one developer assigned to it. A proper plan may also assign part-time developers to applications, and it may assign developers to ordinary applications. Find a proper plan whose total payoff is maximum.
Input
The input holds several test cases. The first line of each test case has two space-separated integers and , the number of developers and the number of applications (). The next line starts with the number of full-time developers (), followed by the indices of the full-time developers. Developers are indexed from 1 to . The next line starts with the number of critical applications (), followed by the indices of the critical applications. Applications are indexed from 1 to . The next lines hold one line of information per developer. Line () starts with the number of applications developer is capable of developing, (), followed by pairs of integers and (, , ). Here is the index of an application developer is capable of developing, and is the payoff gained when developer develops application . The same application index never appears twice in the list of one developer. The last line of the input is 0 0 and is not processed. The input holds at most 20 test cases.
Output
For each test case print on one line the maximum total payoff you can obtain with a proper plan. If there is no proper plan, print -1 on that line.