-
Notifications
You must be signed in to change notification settings - Fork 0
/
Book Reading(Easy Version)-CF 653(Div 3)
55 lines (54 loc) · 1.43 KB
/
Book Reading(Easy Version)-CF 653(Div 3)
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) (v).begin(), (v).end()
#define sz(v) (int) (v).size()
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> t(n), a(n), b(n);
for (int i = 0; i < n; i++)
cin >> t[i] >> a[i] >> b[i];
vector<int> alice, bob, aliceBob;
for (int i = 0; i < n; i++) {
if (a[i] && b[i])
aliceBob.push_back(t[i]);
else if (a[i])
alice.push_back(t[i]);
else if (b[i])
bob.push_back(t[i]);
}
sort(all(alice), greater<int>());
sort(all(bob), greater<int>());
sort(all(aliceBob), greater<int>());
ll ans = 0;
while (k--) {
if (!alice.empty() && !bob.empty() && !aliceBob.empty()) {
if (alice.back() + bob.back() <= aliceBob.back()) {
ans += alice.back() + bob.back();
alice.pop_back();
bob.pop_back();
} else {
ans += aliceBob.back();
aliceBob.pop_back();
}
} else if (!alice.empty() && !bob.empty() && aliceBob.empty()) {
ans += alice.back() + bob.back();
alice.pop_back();
bob.pop_back();
} else if (!aliceBob.empty()) {
ans += aliceBob.back();
aliceBob.pop_back();
}
// Alice is not empty and bob is empty is not allowed
//because every time they must have to take book from individual or universal
else {
cout << "-1\n";
return 0;
}
}
cout << ans << '\n';
return 0;
}