#P10054. [CCO 2022] Phone Plans

[CCO 2022] Phone Plans

Problem Description

Jason, the mayor of CCOland, wants to install phone lines between NN households, numbered from 11 to NN. 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 ll from a company, then you can use all phone lines of that company whose levels are less than or equal to ll. The cost of a level ll plan is ll, and you cannot choose a plan with cost less than 00.

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 KK distinct pairs of households can communicate with each other.

Input Format

The first line contains four space-separated integers NN, AA, BB, and KK, 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 AA lines each contain three space-separated integers uu, vv, and ll, describing a Keenan Mobile phone line. It connects households uu and vv (1u,vN)(1 \leq u, v \leq N) and has level ll (1l109)(1 \leq l \leq 10^{9}).

The next BB lines each contain three space-separated integers uu, vv, and ll, describing a Chris Home Phone line. It connects households uu and vv (1u,vN)(1 \leq u, v \leq N) and has level ll (1l109)(1 \leq l \leq 10^{9}).

Output Format

Output the minimum total cost required to make at least KK distinct pairs of households connected. If it is impossible, output 1-1.

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 33 plan from Keenan Mobile and a level 3030 plan from Chris Home Phone, then (1,2),(1,3),(1,4),(2,3),(2,4),(3,4)(1,2),(1,3),(1,4),(2,3),(2,4),(3,4) can communicate with each other through Keenan Mobile lines, and (1,5),(2,6),(3,6),(2,3)(1,5),(2,6),(3,6),(2,3) can communicate with each other through Chris Home Phone lines. There is no cheaper way.

Constraints

For all testdata, 1N2×1051 \leq N \leq 2\times 10^5, 0A,B2×1050 \leq A, B \leq 2\times 10^5, and 0KN(N1)20 \leq K \leq \frac{N(N-1)}{2}.

Subtask ID Score NN A,BA, B Additional Constraints
11 2424 1N20001 \leq N \leq 2000 0A,B2×1050 \leq A, B \leq 2\times 10^5 None.
22 2020 1N2×1051 \leq N \leq 2\times 10^5 1N2×1051 \leq N \leq 2\times 10^5 Keenan Mobile only connects households with odd indices; Chris Home Phone only connects households with even indices.
33 2424 0A100 \leq A \leq 10 None.
44 3232 0A,B2×1050 \leq A, B \leq 2\times 10^5

Input Format

Output Format

Hint

Translated by ChatGPT 5