`Hacker, pack your bags!

来源:互联网 发布:网络人身攻击怎么办 编辑:程序博客网 时间:2024/05/29 18:02

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 exactlyx 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 thatn vouchers left.i-th voucher is characterized by three integersli,ri,costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of thei-th voucher is a valueri - li + 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 vouchersi andj(i ≠ j) so that they don't intersect, sum of their durations isexactlyx and their total cost is as minimal as possible. Two vouchersi andj don't intersect if only at least one of the following conditions is fulfilled:ri < lj orrj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two integers n andx(2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.

Each of the next n lines contains three integersli,ri andcosti(1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — 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 durationexactlyx.

Example
Input
4 51 3 41 2 55 6 11 2 4
Output
5
Input
3 24 6 32 4 13 5 4
Output
-1
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 be4 + 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 to2.


#include<stdio.h>#include<algorithm>using namespace std;#define ll long long int#define inf 1e18struct node{    ll l,r,c,cost,value;} p[200005];ll cmp(node a,node b){    if(a.c==b.c)        return a.r<b.r;        return a.c<b.c;//先按区间长度排序//区间长度相同的按右端点排序}ll minx(ll x,ll y){    if(x<y)        return x;    else        return y;}int main(){    ll x,i,j,l,r;    ll n,v,mid,minn;    while(scanf("%lld%lld",&n,&x)!=EOF)    {          minn=inf;        for(i=0;i<n;i++)        {            scanf("%lld%lld%lld",&p[i].l,&p[i].r,&p[i].value);            p[i].c=p[i].r-p[i].l+1;//计算区间长度            p[i].cost=p[i].value;        }        sort(p,p+n,cmp);        for(i=1;i<n;i++)//这一步最难理解 将区间长度相同把他们中的最小价值存在比较靠后的天数中        {        if(p[i].c==p[i-1].c)                p[i].cost=minx(p[i].cost,p[i-1].cost);        }        ll temp;        for(i=0;i<n;i++)        {            if(p[i].c>=x)                continue;//找到一个小于x的值             temp=x-p[i].c;                        l=0;r=n-1;//二分寻找价值等于temp的值            while(l<=r)            {                mid=(l+r)/2;                if(temp>p[mid].c)//找到的值较小的话右移                    l=mid+1;                if(temp<p[mid].c)//找到的值较大的话左移                    r=mid-1;                if(temp==p[mid].c)//找到符合的值是还要进行判断  进行判断区间是否重合  且这个区间必须在外层循环区间的左边                {                    if(p[i].l<=p[mid].r)//代表区间在外层区间的右边  应该左移                        r=mid-1;                    else{                            minn=minx(minn,p[i].value+p[mid].cost);//若是在左边的话进行计算维护最小值                            l=mid+1;//并且再次向右移                        }                }            }        }        if(minn==inf)            printf("-1\n");        else            printf("%lld\n",minn);    }    return 0;}




原创粉丝点击