codeforces 882C Hacker, pack your bags!

C. Hacker, pack your bags!

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 liricosti — 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 ri - 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 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: ri < lj or rj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two 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 liri and costi (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 duration exactly x.

Examples

input

Copy

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

output

Copy

5

input

Copy

3 2
4 6 3
2 4 1
3 5 4

output

Copy

-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) = 5and 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.

C. Hacker, pack your bags!

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 liricosti — 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 ri - 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 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: ri < lj or rj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two 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 liri and costi (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 duration exactly x.

Examples

input

Copy

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

output

Copy

5

input

Copy

3 2
4 6 3
2 4 1
3 5 4

output

Copy

-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) = 5and 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.

 

大概题意是给你n个度假的时间段、花费和一个leha他想要的休假时间,他只能选择两个度假方式,要求花费最小的度假两次,并使时间段加起来正好等于休假时间。因为是度假,所以中间不能重叠交叉。

 

第一次理解错题意,瞎写,wa掉,然后第二次本来想暴力n^2搞了一下,我感觉是可以过得,没加二分T了,(中间间隔了一个星期,233333)

后来又换了一思路,用一个数组s[i]来表示i段长度的度假的最少花费,然后每次用时间更新。我们可以把点和时间段的信息存在vector中,然后记得把终点和起点的信息存下来(如果你要存终点的话,就要把起始时间和终止时间换一下位置,仔细想一下便可以理解),这儿我存成1为起点,-1为终点,接着我们按点的大小排序,如果点相同,则按起点终点排序,然后遍历一遍vector,当我们遇到一个终点,我们就去更新这个s[r - l + 1]的花费,因为我们是把时间排序过得,所以时间是一直有序的,如果遇到起点我们就可以去查询另外一个cha的段是否存在,如果存在就去更新答案(注意,只能在起点更新,因为如果在终点更新的话,就会出现时间段的终点和起点会重叠,比如 (1,2) (2,3)这种就不能满足,所以有坑)

 

代码如下:


#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 100;
struct node {
    int cha;
    int c;
    int id;
    int val;
} g;
bool cmp(struct node a,struct node b) {
    if(a.id == b.id) return a.val > b.val;
    return a.id < b.id;
}
int s[maxn];
int main() {
    int n,x;
    while(cin >> n >> x) {
        vector<struct node>v;
        for(int i = 0;i <= maxn;i++) s[i] = 2e9;
        for(int i = 1; i <= n; i++) {
            int l,r,c;
            scanf("%d%d%d",&l,&r,&c);
            v.push_back(node{r - l + 1,c,l,1});
            v.push_back(node{r - l + 1,c,r,-1});
        }
        sort(v.begin(),v.end(),cmp);
        int ans = 2e9 + 7;
        for(vector<struct node>::iterator it = v.begin(); it != v.end(); it++) {
            struct node gg = *it;
            int xx = x - gg.cha;
            if(xx <= 0) continue;
            if(gg.val == -1)
                s[gg.cha] = min(s[gg.cha],gg.c);
            else
                if(s[xx] != 2e9)
                ans = min(ans,gg.c + s[xx]);
        }
        if(ans == 2e9 + 7) cout << -1 << endl;
        else      cout << ans << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值