#P10054. [CCO 2022] Phone Plans
[CCO 2022] Phone Plans
Problem Description
Jason, the mayor of CCOland, wants to install phone lines between households, numbered from to . To do this, he asked two competing companies, Keenan Mobile and Chris Home Phone, about their phone plans. A phone plan from a company corresponds to a specific level; each phone line has a level and an associated company. If you buy a phone plan of level from a company, then you can use all phone lines of that company whose levels are less than or equal to . The cost of a level plan is , and you cannot choose a plan with cost less than .
Two households can communicate with each other only if they are connected by a path consisting of phone lines from the same company. Jason wants to buy one lowest-cost phone plan from each company, so that at least distinct pairs of households can communicate with each other.
Input Format
The first line contains four space-separated integers , , , and , representing the number of households, the number of phone lines from Keenan Mobile, the number of phone lines from Chris Home Phone, and the minimum number of household pairs that must be able to communicate.
The next lines each contain three space-separated integers , , and , describing a Keenan Mobile phone line. It connects households and and has level .
The next lines each contain three space-separated integers , , and , describing a Chris Home Phone line. It connects households and and has level .
Output Format
Output the minimum total cost required to make at least distinct pairs of households connected. If it is impossible, output .
6 4 4 9
1 2 1
2 3 2
1 4 3
3 4 4
5 6 40
1 5 30
2 6 20
3 6 10
33
Hint
Sample Explanation

If Jason buys a level plan from Keenan Mobile and a level plan from Chris Home Phone, then can communicate with each other through Keenan Mobile lines, and can communicate with each other through Chris Home Phone lines. There is no cheaper way.
Constraints
For all testdata, , , and .
| Subtask ID | Score | Additional Constraints | ||
|---|---|---|---|---|
| None. | ||||
| Keenan Mobile only connects households with odd indices; Chris Home Phone only connects households with even indices. | ||||
| None. | ||||
Input Format
Output Format
Hint
Translated by ChatGPT 5