-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBurglar_and_Matches.cpp
More file actions
37 lines (32 loc) · 981 Bytes
/
Burglar_and_Matches.cpp
File metadata and controls
37 lines (32 loc) · 981 Bytes
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
// there are m containers,ith container has ai matchboxes and each contains bi matches
// we can have maximum n matchboxes in the bag
// find maximum number of matches he can carry
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n, m;
cin >> n >> m;
vector<pair<ll, ll>> matches(m);
ll t1, t2;
for (ll i = 0; i < m; i++){
cin >> t1 >> t2;
matches[i] = make_pair(t1, t2);
}
// Sort by the second value of the pair
sort(matches.begin(), matches.end(), [](const pair<ll, ll>& a, const pair<ll, ll>& b) {
return a.second > b.second;
});
ll ans = 0, matches_taken = 0;
for (const auto& pair : matches){
if(pair.first + ans < n){
ans += (pair.first);
matches_taken += (pair.first * pair.second);
}
else{
matches_taken += ((n - ans) * pair.second);
break;
}
}
cout << matches_taken << endl;
}