-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC. Hacker, pack your bags! - Codeforces Round 422 (Div. 2).cpp
65 lines (56 loc) · 1.55 KB
/
C. Hacker, pack your bags! - Codeforces Round 422 (Div. 2).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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct l_and_mincost{
vector<pair<int,int>> l;
vector<int> l_pure;
vector<int> cost;
};
struct dataa{
int r,dur,c;
dataa(int r,int dur,int c) :r(r),dur(dur),c(c){}
bool operator < ( const dataa &f){
if(this->r<=f.r) return false;
return true;
}
};
vector<dataa> r;
vector<l_and_mincost> complement;
int n,x;
int main(){
int ll,rr,c;
cin>>n>>x;
complement.reserve(200005);
for(int i=0;i<n;i++){
cin>>ll>>rr>>c;
int dur = rr - ll + 1;
r.push_back({rr,dur,c});
complement[dur].l.push_back({ll,c});
}
sort(r.begin(),r.end());
for(int i=0;i<200005;i++){
int siz = complement[i].l.size();
complement[i].cost.assign(siz,0);
sort(complement[i].l.begin(),complement[i].l.end());
for(int m = 0;m<siz;m++)
complement[i].l_pure.push_back(complement[i].l[m].first);
int mini = 2000000008;
for(int j=siz-1;j>=0;j--){
mini = min(mini,complement[i].l[j].second);
complement[i].cost[j] = mini;
}
}
int res =2000000008;
int total=0;
for(int i=0;i<n;i++){
total = r[i].c;
if ((x - r[i].dur) <0) continue;
auto &s = complement[x - r[i].dur];
auto p = upper_bound(s.l_pure.begin(),s.l_pure.end(),r[i].r);
if(p == s.l_pure.end()) continue;
total = total + s.cost[p - s.l_pure.begin()];
res = min(res,total);
}
if(res ==2000000008) cout<<-1;
else cout<<res;
}