Codeforces Round 422 (Div. 2)
C
Hacker, pack your bags!
Codeforces
1600
原题
← BD →
翻译中…

It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.

翻译中…

So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly $*x*$ days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that $*n*$ vouchers left. $*i*$-th voucher is characterized by three integers $*l*_{*i*}$, $*r*_{*i*}$, $*cost*_{*i*}$ — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the $*i*$-th voucher is a value $*r*_{*i*} - *l*_{*i*} + 1$.

翻译中…

At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers $*i*$ and $*j*$ $(*i* ≠ *j*)$ so that they don't intersect, sum of their durations is exactly $*x*$ and their total cost is as minimal as possible. Two vouchers $*i*$ and $*j*$ don't intersect if only at least one of the following conditions is fulfilled: $*r*_{*i*} < *l*_{*j*}$ or $*r*_{*j*} < *l*_{*i*}$.

翻译中…

Help Leha to choose the necessary vouchers!

翻译中…

Input

翻译中…

The first line contains two integers $*n*$ and $*x*$ $(2 ≤ *n*, *x* ≤ 2·10^{5})$ — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.

翻译中…

Each of the next $*n*$ lines contains three integers $*l*_{*i*}$, $*r*_{*i*}$ and $*cost*_{*i*}$ $(1 ≤ *l*_{*i*} ≤ *r*_{*i*} ≤ 2·10^{5}, 1 ≤ *cost*_{*i*} ≤ 10^{9})$ — description of the voucher.

翻译中…

Output

翻译中…

Print a single integer — a minimal amount of money that Leha will spend, or print $- 1$ if it's impossible to choose two disjoint vouchers with the total duration exactly $*x*$.

翻译中…

Note

翻译中…

In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to $(3 - 1 + 1) + (6 - 5 + 1) = 5$ and the total cost will be $4 + 1 = 5$.

翻译中…

In the second sample the duration of each voucher is $3$ therefore it's impossible to choose two vouchers with the total duration equal to $2$.

翻译中…

样例

Input #1
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output #1
5
InputCopy OutputCopy
Input #2
3 2
4 6 3
2 4 1
3 5 4
Output #2
-1
InputCopy OutputCopy
题目信息
题号
822C
时间限制
2 s
内存限制
256 MB
标签
二分
贪心
模拟
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。