Security Game
Time limit2sMemory limit512 MB
For each B, find the maximum total protections where each robot covers at most B buildings and each building has between L and U robots, or -1 if impossible.
- Level
Medium7 of 10
- Topics
- Graph, Implementation
- Solved
- No attempts yet
Problem
Albert enjoys a board game called "Security Game". The goal is to protect buildings with security robots. The game has the following rules.
- Each robot can protect up to different buildings at the same time.
- Not every robot can protect every building. The -th robot can protect different buildings, denoted (, ).
- The -th building must be protected by at least and at most different robots. The score of each building is the number of robots guarding it.
- If the robots are placed while following all the rules above, the game score is the sum of the building scores. If no placement follows all the rules, the game score is -1.
For convenience, is defined as the maximum game score Albert can get when ().
For example, let , , , , and .
- If , the maximum score is 3. Robot 1 protects building 2, robot 2 protects building 1, and robot 3 protects building 3. Each building scores 1, so the game score is 3.
- If , the maximum score is 6. Robot 1 protects buildings 1 and 2, robot 2 protects buildings 1 and 3, and robot 3 protects buildings 2 and 3. Each building scores 2, so the game score is 6.
- If or more, no placement scores more than 6. Therefore .
As another example, let , , , , , and .
- Building 2 has , so exactly 2 different robots must protect it.
- Only robot 1 can protect building 2, so the game score is -1 regardless of the value of .
- In this case .
Input
The first line contains the number of test cases .
The first line of each test case contains and , separated by a space. The next lines each contain , the number of buildings where the -th robot can be placed, followed by the building numbers , separated by spaces. In other words, each of these lines contains integers. The next lines each contain a pair of integers and , the minimum and maximum number of robots that must be placed on the -th building.
Output
For each test case, print separated by spaces on a single line.
Constraints
- For each with :
- contain no duplicate values
- For each with :