#P2798. 爆弹虐场

爆弹虐场

Description

On a certain day, Kiana met a boy who dominates the Explosive Mayhem Arena.

Because Kiana has studied OI for a few more years, she can barely explain to this boy some problems she is good at. Specifically, Kiana first poured nn unrelated knowledge points into him, and then used her [Data Deletion] technique to forcibly connect these knowledge points.

Since the boy is very strong, for each link Kiana plans to construct, he can figure it out by himself, but it will take more time. Specifically, Kiana has mm links. Each link can connect two unrelated knowledge points. If Kiana directly explains the ii-th link, it takes tit_i time; if the boy figures it out himself, it takes TiT_i time.

To be lazy, Kiana only needs the links she explains or the boy figures out to make all knowledge points directly or indirectly connected. But to ensure teaching quality, Kiana thinks at least kk links should be figured out by the boy himself. Since Kiana’s patience is limited, she wants the longest time among all constructed links, regardless of who does them, to be as small as possible.

Now Kiana wants to know, under these conditions, what is the minimal possible value of the maximum time among the constructed links. Since she cannot compute it, she asks you to tell her.

Input Format

The input contains m+1m+1 lines.

The first line contains three positive integers nn, kk, and mm, representing the number of knowledge points, the number of links Kiana wants the boy to figure out himself, and the total number of links, respectively.

Then mm lines follow. Each line contains four positive integers aa, bb, TiT_i, and tit_i, meaning a link can be constructed between knowledge points aa and bb. If the boy figures out this link himself, it takes TiT_i time; if Kiana explains it directly, it takes tit_i time.

Output Format

The output contains one line.

Print a single positive integer, the minimal possible value of the maximum time among all constructed links.

4 2 5 
1 2 6 5 
1 3 3 1 
2 3 9 4 
2 4 6 1 
3 4 4 2 

4

Hint

For 30%30\% of the testdata, 1n101 \le n \le 10, n1m15n-1 \le m \le 15.

For 60%60\% of the testdata, 1n5001 \le n \le 500, n1m1000n-1 \le m \le 1000.

For 100%100\% of the testdata, 1k<n100001 \le k < n \le 10000, n1m20000n-1 \le m \le 20000.

1ti<Ti1061 \le t_i < T_i \le 10^6.

The testdata guarantees that a feasible solution exists.

Translated by ChatGPT 5