atcoder#AGC043F. [AGC043F] Jewelry Box
[AGC043F] Jewelry Box
Score : points
Problem Statement
There are jewelry shops numbered to .
Shop () sells kinds of jewels. The -th of these jewels () has a size and price of and , respectively, and the shop has jewels of this kind in stock.
A jewelry box is said to be good if it satisfies all of the following conditions:
- For each of the jewelry shops, the box contains one jewel purchased there.
- All of the following restrictions are met.- Restriction (): The size of the jewel purchased at Shop The size of the jewel purchased at Shop
- Restriction (): The size of the jewel purchased at Shop The size of the jewel purchased at Shop
Answer questions. In the -th question, given an integer , find the minimum total price of jewels that need to be purchased to make good jewelry boxes. If it is impossible to make good jewelry boxes, report that fact.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Description of Shop
Description of Shop
Description of Shop
The description of Shop () is in the following format:
Output
Print lines. The -th line should contain the minimum total price of jewels that need to be purchased to make good jewelry boxes, or if it is impossible to make them.
3
2
1 10 1
3 1 1
3
1 10 1
2 1 1
3 10 1
2
1 1 1
3 10 1
2
1 2 0
2 3 0
3
1
2
3
3
42
-1
Let represent the -th jewel sold at Shop . The answer to each query is as follows:
- : Making a box with costs , which is optimal.
- : Making a box with and another with costs , which is optimal.
- : We cannot make three good boxes.
5
5
86849520 30 272477201869
968023357 28 539131386006
478355090 8 194500792721
298572419 6 894877901270
203794105 25 594579473837
5
730211794 22 225797976416
842538552 9 420531931830
871332982 26 81253086754
553846923 29 89734736118
731788040 13 241088716205
5
903534485 22 140045153776
187101906 8 145639722124
513502442 9 227445343895
499446330 6 719254728400
564106748 20 333423097859
5
332809289 8 640911722470
969492694 21 937931959818
207959501 11 217019915462
726936503 12 382527525674
887971218 17 552919286358
5
444983655 13 487875689585
855863581 6 625608576077
885012925 10 105520979776
980933856 1 711474069172
653022356 19 977887412815
10
1 2 231274893
2 3 829836076
3 4 745221482
4 5 935448462
5 1 819308546
3 5 815839350
5 3 513188748
3 1 968283437
2 3 202352515
4 3 292999238
10
510266667947
252899314976
510266667948
374155726828
628866122125
628866122123
1
628866122124
510266667949
30000000000000
26533866733244
13150764378752
26533866733296
19456097795056
-1
33175436167096
52
33175436167152
26533866733352
-1