您的位置:首页 > 大数据 > 人工智能

【Codeforces 822 C. Hacker, pack your bags!】+ pair

2017-07-18 17:35 357 查看
C. Hacker, pack your bags!

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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 li, ri, costi — 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 li, ri 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

4 5

1 3 4

1 2 5

5 6 1

1 2 4

output

5

input

3 2

4 6 3

2 4 1

3 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 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.

题意 : 是否能存在两个不想交的区间,时间段相加 == x,且价值尽可能小

思路 : 依次遍历每个区间的左端点,判断在该区间的左端点前是否存在能和该区间组合且不相交的区间

AC代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAX = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
typedef pair<LL,LL> p;
vector <p> v[MAX],w[MAX];
LL m[MAX];
int main()
{
int n,t,T,a,b,c;
scanf("%d %d",&n,&T);
for(int i = 1; i <= n; i++)
scanf("%d %d %d",&a,&b,&c),v[a].push_back(p(b,c));
LL ans = INF;
for(int i = 1; i <= 2e5; i++){
for(int j = 0; j < v[i].size(); j++){
b = v[i][j].first;
c = v[i][j].second;
t = b - i + 1;
if(T > t && m[T - t]) ans = min(ans,c + m[T - t]); // 是否存在这样的时间段
w[b].push_back(p(t,c)); // 已右界限保存,避免相交
}
for(int j = 0; j < w[i].size(); j++) // 更新最优解
if(!m[w[i][j].first] || m[w[i][j].first] > w[i][j].second)
m[w[i][j].first] = w[i][j].second;
}
if(ans == INF) puts("-1");
else printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces