Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! Sort of, greedy

Source: Internet
Author: User

C. Hacker, pack your bags!

It's well known, the best-of-the-distract from something are 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 Span class= "Lower-index" > i - l i + 1.

At the same time Leha wants to split he own vocation into the parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly-vouchers  i and J (iJ) So this they don ' t intersect, sum of their durations is exactly x and their total cost are as minimal as possible. Vouchers i and J don ' t intersect if only at least one of the following conditions is F ulfilled: RI < lJ or RJ < l.

Help Leha to choose the necessary vouchers!

Input

The first line contains integers n and x (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 integers li, ri and cost i (1≤ liri ≤2 5, 1≤ cost i ≤109)-description of the voucher.

Output

Print a single integer-a minimal amount of money that Leha would spend, or print -1 if it's impossible to choose TW o Disjoint vouchers with the total duration exactly x.

Examples input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Note

In the first sample Leha should choose first and third vouchers. Hereupon the total duration'll be equal to (3-1 + 1) + (6-5 + 1) = 5and The total cost would be 4 + 1 = 5.< /c13>

In the second sample, the duration of each voucher is 3 therefore it's impossible to choose, and the total Duration equal to 2.

Test instructions

Here you are. N A weighted segment, an X

I want you to select two lines that you do not want to cross, meet the length of the segment and x, and the minimum weight, and cannot find-1

Exercises

Remove all Endpoints

Left point 1, right endpoint-1, each time you encounter the right endpoint, the query is less than the current position of the rights of the end, and meet the length of the segment and X, sorted and run it again.

#include <bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF = 1e16+1LL;Const DoublePI = ACOs (-1.0);Const intN = 1e6+5, M = 1e3+ -, INF = 2e9+Ten;structss{intX,len,id,value; SS () {} SS (intXsintLensintIdsintValues) {x = Xs,len = Lens,id = Ids,value =values;}} A[n];intCNT =0, n,x;intCMP (ss S1,ss S2) {if(s1.x = = s2.x)returnS1.id >s2.id; returns1.x <s2.x;} LL Mp[n];intMain () {scanf ("%d%d",&n,&X);  for(inti =1; I <= X; ++i) {Mp[i]=INF; }     for(inti =1; I <= N; ++i) {intx,y,v; scanf ("%d%d%d",&x,&y,&v); //cout<<y-x+1<<endl;A[++CNT] = SS (X,y-x +1,1, V); a[++CNT] = SS (Y,y-x +1,-1, V); } sort (A+1, a+cnt+1, CMP); LL ans=INF;  for(inti =1; I <= CNT; ++i) {if(A[i].id = =1) {           //cout<<x-a[i].len<<endl;            if(X-a[i].len >0) ans = min (Ans,mp[x-a[i].len] +a[i].value); }        Else {           //cout<<a[i].len<<endl;Mp[a[i].len] = min (mp[a[i].len],1ll*a[i].value);//cout<< "Fuck" <<endl;        }    }    if(ans = = INF) puts ("-1"); Elsecout<<ans<<Endl; return 0;}

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! Sort of, greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.