-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1374E1. Reading Books (easy version).cpp
67 lines (51 loc) · 1.44 KB
/
1374E1. Reading Books (easy version).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
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> both, first, second;
int main() {
scanf("%d %d", &n, &k);
for(int i = 0, t, a, b; i < n; ++i) {
scanf("%d %d %d", &t, &a, &b);
if(a + b == 2) {
both.push_back(t);
} else if(a == 1) {
first.push_back(t);
} else if(b == 1) {
second.push_back(t);
}
}
sort(both.begin(), both.end());
sort(first.begin(), first.end());
sort(second.begin(), second.end());
int booksCount = 0;
int totalReadingTime = 0;
int j = 0, l = 0;
for(int i = 0; i < both.size() && booksCount != k * 2;) {
if(j < first.size() && l < second.size() && first[j] + second[l] < both[i]) {
totalReadingTime += first[j] + second[l], ++j, ++l;
} else {
totalReadingTime += both[i], ++i;
}
booksCount += 2;
}
if(booksCount != k * 2) {
int diff = ((k * 2) - booksCount) / 2, tmp = diff;
while(j < first.size() && diff > 0) {
totalReadingTime += first[j++];
++booksCount;
--diff;
}
diff = tmp;
while(l < second.size() && diff > 0) {
totalReadingTime += second[l++];
++booksCount;
--diff;
}
}
if(booksCount != k * 2) {
puts("-1");
} else {
printf("%d\n", totalReadingTime);
}
return 0;
}