#P6731. [WC2020] 选课

[WC2020] 选课

Description

With the end of final exams, the annual course selection period begins again.

Student C is a good student who loves studying. He set a small goal for himself: to earn at least TT credits in the new semester. According to the announcement from the academic office, there are mm categories of courses to choose from this time, and the ii-th category has nin_i courses. Based on the announcement, Student C raised his goal: on the basis of earning at least TT credits in total, he must also earn at least sis_i credits in each category ii (i=1,2,,mi=1,2,\cdots,m). Meanwhile, using his experience, Student C calculated the credits he can get from each course and the mental effort cost required. Moreover, he discovered that some courses have special relationships: taking two similar courses together may reduce the mental effort cost; taking two very demanding courses together may increase the cost; and if two courses have a time conflict, they cannot be taken together.

Student C wants to spend the minimum total mental effort to reach his goal. Can you help him compute the minimum mental effort needed?

Input Format

The first line contains two positive integers m,Tm,T, representing the number of categories and the total credits required.

Then follow mm blocks of input. For the ii-th block:

The first line contains two non-negative integers ni,sin_i,s_i, representing the number of courses in the ii-th category and the minimum credits required in this category.

The j+1j+1-th line (1jni1\le j\le n_i) contains two positive integers wi,j,ci,jw_{i,j},c_{i,j}, representing the credits gained by taking the jj-th course in category ii, and the mental effort cost required.

After the mm blocks, there is a non-negative integer pp, representing the number of relationships.

The next pp lines each describe one relationship. Each relationship is one of the following 3 types (all numbers below are positive integers):

1 x1 y1 x2 y2 c1\ x_1\ y_1\ x_2\ y_2\ c: taking the y1y_1-th course in category x1x_1 and the y2y_2-th course in category x2x_2 together can reduce the cost by cc.

2 x1 y1 x2 y2 c2\ x_1\ y_1\ x_2\ y_2\ c: taking the y1y_1-th course in category x1x_1 and the y2y_2-th course in category x2x_2 together will increase the cost by cc.

3 x1 y1 x2 y23\ x_1\ y_1\ x_2\ y_2: the y1y_1-th course in category x1x_1 and the y2y_2-th course in category x2x_2 cannot be taken together.

Output Format

The output contains only one line with one integer, representing the minimum mental effort required to reach the goal. If it is impossible to reach Student C's goal, output -1.

1 10
1 1
1 1
0
-1

3 10
5 4
1 30
1 30
2 3
2 3
3 30
6 6
1 1
1 30
2 1
2 30
3 9
3 10
1 0
1 10
1
1 1 5 2 6 35 
10

Hint

Explanation for Sample 1

Even if all courses are taken, the total credits still cannot meet Student C's requirement, so the output is -1.

Explanation for Sample 2

One possible selection is: choose courses 4 and 5 in the first category, choose courses 1, 3, and 6 in the second category, and choose no courses in the third category (the selection is not unique).

Constraints

Let N=i=1mniN=\sum_{i=1}^{m} n_i, and let MM be the maximum mental effort cost (including increases or decreases caused by related courses).

For 5%5\% of the data: N5N\le 5.

For 10%10\% of the data: N15N\le 15.

For another 10%10\% of the data: N1000N\le 1000, p=0p=0.

For another 10%10\% of the data: wi=1w_i=1, p=0p=0.

For another 10%10\% of the data: T=i=1msiT=\sum_{i=1}^{m} s_i, p=0p=0.

For another 10%10\% of the data: N104N\le 10^4, M50M\le 50, and related courses are within the same category.

For another 10%10\% of the data: N5×104N\le 5\times 10^4, M50M\le 50.

For 100%100\% of the data: N5×105N\le 5\times 10^5, M200M\le 200, 0Ti=1msi400\le T-\sum_{i=1}^{m} s_i\le 40, m5×104m\le 5\times 10^4, p12p\le 12, wi,j{1,2,3}w_{i,j}\in\{1,2,3\}.

The official data is incorrect. According to testing, the maximum pp is 6666.

The data guarantees that for any two courses, there is at most one relationship between them.

Translated by ChatGPT 5