-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjects
62 lines (52 loc) · 1.21 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
#define rep(k,z) for(int i=k;i<z;i++)
#define ll long long int
const ll MOD=1e9+7;
bool cmp(pair<pair<ll,ll>,ll> &a,pair<pair<ll,ll>,ll> &b)
{
return a.first.second<b.first.second;
}
// end value less than key
int findIdx(vector<pair<pair<ll,ll>,ll>> &vp,ll key)
{
int l=0,h=vp.size()-1;
int ans=-1;
while(l<=h)
{
int mid=l+(h-l)/2;
if(vp[mid].first.second<key)
{
ans=mid;
l=mid+1;
}
else
h=mid-1;
}
return ans+1;
}
int main()
{
ll n;
cin>>n;
// {{start,end},val}
vector<pair<pair<ll,ll>,ll>> vp(n);
for(int i=0;i<n;i++)
{
cin>>vp[i].first.first>>vp[i].first.second>>vp[i].second;
}
// sort according to end time
sort(vp.begin(),vp.end(),cmp);
vector<ll> dp(n+1);
// dp[i]--> maximum value obtain if we have first i projects in sorted order
dp[0]=0;
for(int i=1;i<=n;i++)
{
// not take the project
dp[i]=dp[i-1];
int idx=findIdx(vp,vp[i-1].first.first);
// take the project and find the next suitable project
dp[i]=max(dp[i],vp[i-1].second+dp[idx]);
}
cout<<dp[n]<<endl;
}