#P4638. [SHOI2011] 银行家
[SHOI2011] 银行家
Description
You work at a bank, and your job is to help customers withdraw the gold coins stored in their safe deposit boxes.
There are safe deposit boxes in the bank. You cannot open these boxes because the keys are held by the customers. A customer may have keys to many boxes, and a box may also have keys held by multiple customers. This morning, the manager told you that you must receive customers in order (no two customers will arrive at the same time). You also know that the -th customer will ask to withdraw gold coins. When a customer arrives, they will open all the boxes they can open and take coins from them (all coins are identical). If the total number of coins in these boxes is not enough, they will be unhappy. After taking as many coins as possible, they will go to your manager to complain that the bank’s service is terrible.
Of course, you do not want complaints to cost you your job, so you come up with a remedy: although each customer will close the boxes again when leaving, while they are taking coins, you can secretly adjust the numbers of coins in the opened boxes. For example, you can move the extra coins in box into box , so that the next customer may be able to withdraw more coins.
Even with this method, it may still be impossible to satisfy all customers’ requests. However, you want to help customers withdraw as many coins as possible in total, so as to reduce their anger and keep this hard-earned job and salary.
Input Format
The first line contains two positive integers and , where is the number of safe deposit boxes and is the number of customers.
The second line contains non-negative integers, representing the numbers of coins in boxes to before the bank opens. Then follow lines, describing each customer in the order they arrive. Each line starts with a non-negative integer , followed by integers between and , indicating that the customer has the keys to boxes , , up to . Finally, there is a non-negative integer , indicating the number of coins they need. The input guarantees that all integers appearing in the input data do not exceed .
Output Format
Output one integer, the maximum possible total number of coins that all customers can withdraw. The input guarantees that the answer does not exceed .
3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6
7
2 3
2 3
2 1 2 1
1 2 2
1 2 2
5
6 6
6 3 2 0 1 3
2 1 2 0
1 3 3
1 1 1
2 2 3 8
2 4 5 2
2 4 6 6
15
Hint
Constraints
| Test point ID | ||
|---|---|---|
| 1 | 30 | 100 |
| 2 | 40 | 50 |
| 3 | 100 | 400 |
| 4 | ||
| 5 | ||
| 6 | 200 | 500 |
| 7 | 300 | |
| 8 | 400 | 1500 |
| 9 | 500 | 2000 |
| 10 | 600 | 2500 |
Translated by ChatGPT 5
京公网安备 11011102002149号