forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
milkScheduling_greedy.cpp
49 lines (42 loc) · 953 Bytes
/
milkScheduling_greedy.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
// This is a very famous greedy alogrithm
// This is written by me, and this passed as test cases
// U can find the problem from link given below
// https://www.spoj.com/problems/MSCHED/
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
typedef vector<int> v;
bool cmp(vector<int> a, vector<int> b){
return (a[0] >= b[0]);
}
int main() {
int n; cin >> n;
vector< v > arr;
for(int i = 0; i < n; i++){
v t;
int gallons, dead;
cin >> gallons >> dead;
t.push_back(gallons);
t.push_back(dead);
arr.push_back(t);
}
bool slot[n];
sort(arr.begin(), arr.end(), cmp);
/*for(int i = 0; i < n; i++){
cout << arr[i][0] << " " << arr[i][1] << "\n";
}
*/
for(int i = 0; i < n; i++) slot[i] = false;
int res = 0;
for(int i = 0; i < n; i++){
for(int j = min(n, arr[i][1]) -1; j >= 0; j--){
if (slot[j] == false){
res += arr[i][0];
slot[j] = true;
break;
}
}
}
cout << res << "\n";
return 0;
}