#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 mm 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 nn customers in order (no two customers will arrive at the same time). You also know that the ii-th customer will ask to withdraw cic_i gold coins. When a customer arrives, they will open all the boxes they can open and take cic_i 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 55 coins in box 11 into box 22, 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 mm and nn, where mm is the number of safe deposit boxes and nn is the number of customers.

The second line contains mm non-negative integers, representing the numbers of coins in boxes 11 to mm before the bank opens. Then follow nn lines, describing each customer in the order they arrive. Each line starts with a non-negative integer kk, followed by kk integers a1,a2,,aka_1,a_2,\ldots,a_k between 11 and mm, indicating that the customer has the keys to boxes a1a_1, a2a_2, up to aka_k. Finally, there is a non-negative integer cic_i, indicating the number of coins they need. The input guarantees that all integers appearing in the input data do not exceed 1000010000.

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 100000100000.

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 nn \le mm \le
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