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

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 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 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个里找出两个来,使他们的值加起来等于x并且不在一个区间里。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <vector>
 4 using namespace std;
 5 const int N = 2e5+10;
 6 vector<pair<int,int> > a[N], b[N];
 7 typedef pair<int,int> P;
 8 const int INF = 2e9+10;
 9 int c[N];
10 int main() {
11     int n, k, l, r, cc;
12     cin >> n >> k;
13     for(int i = 0; i < n; i ++) {
14         cin >> l >> r >> cc;
15         a[l].push_back(make_pair(r-l+1, cc));
16         b[r].push_back(make_pair(r-l+1, cc));
17     }
18     for(int i = 0; i < N; i ++) c[i] = INF;
19     int ans = INF;
20     for(int i = 0; i < N; i ++) {
21         for(int j = 0; j < a[i].size(); j ++) {
22             P p = a[i][j];
23             if(p.first>=k) continue;
24             else if(c[k-p.first]<INF) ans=min(ans,p.second+c[k-p.first]);
25         }
26         for(int j = 0; j < b[i].size(); j ++) {
27             P p = b[i][j];
28             c[p.first]=min(c[p.first],p.second);
29         }
30     }
31     printf("%d\n",ans==INF?-1:ans);
32     return 0;
33 }
时间: 2024-10-22 16:12:23

Codeforces Round #422 (Div. 2) C Hacker, pack your bags!的相关文章

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心

C. Hacker, pack your bags! 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 co

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序+贪心

链接: http://codeforces.com/contest/822/problem/C 题意: 有x天的假期, 有n张旅行票, 每张票有起始时间l, 结束时间r, 花费cost, 想把假期分成两部分出去旅游, 两部分时间不能重合(ri < lj || rj < li), 问最小花费是多少, 如果不能两部分, 输出-1 题解: CF官方解法, 效率O(nlogn2) 设置一个结构体, struct P{int p, len, cost, type}; 将每张票(l, r, cost) 表

Codeforces Round #422 (Div. 2) A. I&#39;m bored with life 暴力

A. I'm bored with life Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the

Codeforces Round #422 (Div. 2) D. My pretty girl Noora

题目链接:Codeforces Round #422 (Div. 2) D. My pretty girl Noora 题意: 给你一个数n和t,l,r,让你求 t0·f(l)?+?t1·f(l?+?1)?+?...?+?tr?-?l·f(r). 其中f(n)是n个人的最少比较次数. 比如n为4,可以先2 2分,然后胜出2个人,最后再比较一次,所以f(4)=3. f(3)=3,因为3为质数,只能这样分. 题解: 这题半天没看清楚题意啊.- -!以为是任意分. 结果是选一个数x,每组都必须为x个人

Codeforces Round #422 (Div. 2) A-C

A. I'm bored with life 水题 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #include <map> using name

Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP

E. Liar The first semester ended. You know, after the end of the first semester the holidays begin. On holidays Noora decided to return to Vi?kopolis. As a modest souvenir for Leha, she brought a sausage of length m from Pavlopolis. Everyone knows th

Codeforces Round #422 (Div. 2)

A: 给你两个数 (最小的那个<=12)  问这两个数阶乘的GCD 我都吓傻了 直接fac(min(a,b)) 搞定 //By SiriusRen #include <bits/stdc++.h> using namespace std; int A,B; long long t=1; int main(){ scanf("%d%d",&A,&B); if(A>B)swap(A,B); for(int i=1;i<=A;i++)t=t*i;

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/