-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcode_BU(Projects).cpp
62 lines (47 loc) · 1.06 KB
/
code_BU(Projects).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
//For better understanding,one can refer to the comment based code uploaded for the same using a different technique.
//code_BinarySearch( for comment based )
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
struct job
{
ll start,finish,money;
};
bool comp(job j1,job j2)
{
return (j1.finish < j2.finish);
}
int find(job a[],ll i)
{
for(ll j=i-1;j>=0;j--)
{
if(a[j].finish < a[i].start)
return j;
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n;
cin>>n;
struct job a[n];
for(ll i=0;i<n;i++)
cin>>a[i].start>>a[i].finish>>a[i].money;
sort(a,a+n,comp);
ll *dp = new ll[n];
dp[0] = a[0].money;
for(ll i=1;i<n;i++)
{
ll temp = a[i].money;
ll search = find(a,i);
if(search != -1)
temp += dp[search];
dp[i] = max(dp[i-1],temp);
}
cout<<dp[n-1]<<endl;
delete[] dp;
return 0;
}