forked from DionysiosB/CodeForces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
822C-HackerPackYourBags.cpp
40 lines (33 loc) · 1.08 KB
/
822C-HackerPackYourBags.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdio>
#include <vector>
#include <algorithm>
struct vac{long start, finish, cost;};
bool vacCompare(vac a, vac b){
if(a.start < b.start){return true;}
else if(a.start == b.start){return a.finish < b.finish;}
return false;
}
int main(){
const long D = 200010;
std::vector<std::vector<vac> > a(D);
long n, x; scanf("%ld %ld", &n, &x);
for(long p = 0; p < n; p++){
long l, r, c; scanf("%ld %ld %ld", &l, &r, &c);
vac v; v.start = l; v.finish = r; v.cost = c;
a[r - l + 1].push_back(v);
}
for(long p = 0; p < D; p++){sort(a[p].begin(), a[p].end(), vacCompare);}
long minCost(2e9 + 10);
for(long d = 1; d < x && d < D; d++){
long f = x - d;
for(long p = 0; p < a[d].size(); p++){
for(long q = 0; q < a[f].size(); q++){
if(a[d][p].finish >= a[f][q].start){continue;}
long sum = a[d][p].cost + a[f][q].cost;
minCost = (minCost < sum) ? minCost : sum;
}
}
}
printf("%ld\n", (minCost > 2e9 + 5) ? -1 : minCost);
return 0;
}