-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(dp) Projects
42 lines (35 loc) · 970 Bytes
/
(dp) Projects
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long mod =1e9+7;
using namespace std;
struct project{
int start;
int end;
int reward;
};
bool sortByEndTime(const project& p1, const project& p2){
return p1.end<p2.end;
}
int32_t main(){
int n;
cin>>n;
vector<project> vec(n);
for(int i=0 ;i<n; i++){
cin>>vec[i].start>> vec[i].end>>vec[i].reward;
}
sort(vec.begin(), vec.end(), sortByEndTime);
vector<int> dp(n);
dp[0]=vec[0].reward;
for(int i=1; i<n; i++){
auto it = lower_bound(vec.begin(), vec.begin()+i, vec[i].start, [](const project& p1, int val){return p1.end<val;});
int excludingCurrent = dp[i-1];
int includingCurrent=vec[i].reward;
if(it!=vec.begin()){
includingCurrent+=dp[distance(vec.begin(), it)-1];
}
dp[i]=max(includingCurrent, excludingCurrent);
}
cout<<dp[n-1];
return 0;
}