Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i — the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≤ t_i ≤ 10^4, 0 ≤ a_i, b_i ≤ 1), where:
t_i — the amount of time required for reading the i-th book;
a_i equals 1 if Alice likes the i-th book and 0 otherwise;
b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T — the minimum total reading time of the suitable set of books.
We need to solve the problem: choose a subset of books such that at least k books are liked by Alice, and at least k books are liked by Bob. Minimize total time. Books have categories based on (a,b): (1,1) liked by both, (1,0) liked by Alice only, (0,1) liked by Bob only, (0,0) liked by none.
We need to choose subset S, with total time sum(t_i) minimized, subject to count of books with a_i=1 >= k, and count with b_i=1 >= k.
Constraints: n up to 2e5, t_i up to 1e4.
This is similar to CF problem "Books" (CF #1370? maybe). The solution: Consider three categories: both (type 11), only Alice (10), only Bob (01), none (00). We need to pick some books. The condition is: #10 + #11 >= k, #01 + #11 >= k. Let x = #11, y = #10, z = #01. Then need x+y >= k, x+z >= k. We want to minimize total time.
Observation: The books with type 00 are irrelevant except they could be used to fill extra if needed? Actually condition does not involve them. But we might pick them if they have low t? However, they don't contribute to the required counts, so they only increase total time without helping meet conditions. Since we minimize time, we never pick type 00 books because they are unnecessary; we can always skip them. So we only consider types 11, 10, 01.
Now the problem reduces to: choose x books from type 11, y from type 10, z from type 01, such that x+y >= k, x+z >= k, minimize total time sum. Since we can pick any number of each, but we want minimal sum, we need to choose best (lowest t) books from each type.
Idea: Suppose we decide on the number of type 11 books we take, call c. Then we need at least max(0, k - c) books from type 10 and at least max(0, k - c) from type 01. After that, we could also take more books from type 11 or 10/01 but they wouldn't help? Actually after meeting the minimum counts, picking any additional book (even from 11, 10, 01) would increase total time, so we stop. So optimal solution: choose c (number of 11 books) from 0 to min(k, |11|). Then we take the c smallest t from 11, and we take the smallest (k - c) from 10, smallest (k - c) from 01. The total time is sum_{smallest c of 11} + sum_{smallest (k-c) of 10} + sum_{smallest (k-c) of 01}. But is it always optimal? Consider case where we take more than k from 10 or 01? Suppose c is small, we need (k-c) from 10 and (k-c) from 01. Could it be beneficial to take an extra 11 instead of a combination? That is already covered by varying c. But what about taking extra beyond the required minimum? That would only increase total time, unless we can replace some other books to reduce total? Actually maybe we can take more than necessary from some type if it allows us to drop a more expensive book from another type? But we are already selecting the smallest books from each type for the required counts. If we take an extra 11, we might be able to reduce the number needed from 10 and 01? Because condition: x+y >= k, x+z >= k. If we increase x by 1, we could reduce y and/or z by 1 each as long as constraints hold. But we can't reduce y and z individually if they are already at minimum. Suppose we set c, then y = k-c, z = k-c. If we increase c to c+1, then we could set y = max(0, k-(c+1)) = k-c-1, and similarly z. So the total number of books changes: total = c + (k-c) + (k-c) = 2k - c. With c+1, total = 2k - (c+1) = 2k-c-1, which is one less book overall. So taking an extra 11 might reduce total count of books selected, but the sum might be lower if the added 11 is cheap enough to offset the removal of one 10 and one 01. Indeed, the decision to choose c is exactly considering such trade-offs: we compare sum of c smallest 11 + (k-c) smallest 10 + (k-c) smallest 01 for different c. That's the optimal because any optimal solution must have some c, and then must pick the smallest ones from each category because we want to minimize sum. Also note that we might pick more than 2k-c books? But if we have more than necessary, we could drop the largest ones and still satisfy constraints, so optimal solution will have exactly x = c, y = k-c, z = k-c, with possibly some extra? Let's think: Suppose we have x=c, y>=k-c, z>=k-c. If y > k-c, we could remove the largest y-(k-c) books from y, reducing total time while still satisfying constraints. So optimum will have y exactly = max(0, k-c)? Actually not necessarily: Could there be a scenario where we have x > k? Then we don't need any 10 or 01. For x >= k, y and z can be zero. But if we have x >= k, we can set c = x. However, if x > k, maybe we could reduce x to k and drop some 11s, but that might increase because we might need to add 10/01? Actually if x >= k, the constraints are already satisfied without any 10 or 01. So the optimum in that case is to take the smallest k books from 11? Wait: If we take x >= k, we could have more than k 11 books. But if we take more than k 11's, we could drop some and still satisfy. So optimum with only 11's would take exactly k 11's (the smallest k). But could there be a mix where we take some 10 and 01 even though x >= k? That would only increase total time, so no. So when c >= k, we only need c = k (or maybe c > k if 11 books are so cheap that replacing some 10/01 with extra 11 could reduce total? But if we already have k 11's, we have met both conditions. Adding more books increases total time, so not beneficial. So for c >= k, the optimum is exactly k 11's, but that case is covered by c = k? Actually our formula for c >= k would give y = max(0,k-c)=0, z=0, total = sum of smallest c 11's? But if c>k, sum of smallest c is larger than sum of smallest k (since we add more books). So the minimal for c>=k is at c=k. So we only need to consider c from 0 to min(k, size11). But also note: if size11 < k, we need to supplement with 10 and 01. So we consider c up to min(k, size11). However, there is also possibility where we take more than k from 10 and 01? For instance, if we take x=0, then y >= k, z >= k. Could we take y > k and z = k? That still satisfies. But if we take y > k, total is larger than taking exactly k, so not optimal. So indeed optimal has exactly the minimal needed from each category given x.
But is there scenario where we pick some 10 but not the smallest because we want to use some 11 later? The greedy selection of smallest from each category for given c is optimal because categories are independent: the set of books from type 10 we pick does not affect the constraint except for count. So we pick the cheapest ones.
Thus the solution: Sort each category's times ascending. Compute prefix sums for each category. Then for c from 0 to min(k, len(11)), we need to check if we have enough 10 and 01 books: need (k-c) <= len(10) and (k-c) <= len(01). If these conditions hold, then total = pref11[c] + pref10[k-c] + pref01[k-c]. Keep the minimum. Also, consider the case where we can take only 11's: c = k if possible, but that is included.
Edge: c can be greater than k? That would give negative (k-c), but we only consider up to k. Actually if c > k, then (k-c) negative, we set y=0, z=0. That scenario would be sum of smallest c 11's. But as argued, that can't be better than c=k because extra books add positive time. But what if the extra 11 books have negative time? Not possible. So ignore c > k.
Thus algorithm: read books, separate into three lists: both, aliceOnly, bobOnly. Sort each. Compute prefix sums (size+1 with 0). Let m = min(k, sizeBoth). Iterate c from 0 to m. For each c, need (k - c) <= sizeAliceOnly and (k - c) <= sizeBobOnly. If true, compute total = prefBoth[c] + prefAliceOnly[k-c] + prefBobOnly[k-c]. Take min. Also, if sizeBoth >= k and we don't need any others, that's included (c = k, then (k-c)=0, which always okay because we can take 0 from others; prefix sum for 0 is 0). So condition (k-c) <= size... works for zero? yes 0 <= size always true. So we don't need special check for zero.
If no c yields valid condition, output -1.
Complexities O(n log n).
But wait: The problem statement mentions "at least k books from the chosen set" for each, not exactly k. So we might consider picking more than k from each? But as argued, minimal solution will have exactly k from each? Not exactly: For Alice: we need at least k books liked by Alice. If we pick some 11 and some 10, total liked by Alice = x+y. We need x+y >= k. Similarly, liked by Bob = x+z >= k. If we have x+y > k, we could possibly drop some 10 that is the most expensive and still keep x+y>=k. So indeed optimal has x+y = k or x+z = k? Possibly both equal to k? Let's examine: Suppose we have a solution with x, y, z. If x+y > k, we can remove the book(s) from 10 with highest t (or from 11 if 10 is zero) as long as we maintain x+y >= k. However, if we remove a 10, it reduces total time. So we can keep removing until x+y = k or no 10 left. But if we have no 10 and x > k, then we can remove some 11 until x = k. Similarly for Bob: if x+z > k, we can remove some 01 (or 11 if 01 zero) until x+z = k, but careful: if we remove a 11, that affects both conditions. So we need to ensure after removal both conditions hold. The minimal solution will have both equalities? Possibly not: maybe we need to keep extra to satisfy both simultaneously. For instance, if we have x = 5, y = 0, z = 0, and k=4, then x+y=5>=4, x+z=5>=4, both satisfied. But we could remove one 11 to get x=4, still satisfy, reduces total time, so better. So we can reduce x to k. So optimum has x <= k? Actually if x > k, we can reduce x to k (by removing some 11), and still satisfy both (since x+y>=k and x+z>=k become k + y >= k, which is true if y>=0, and similarly for z). So we can always reduce x to at most k. Therefore optimal has x <= k.
Now, given x = c <= k, then we need y >= k - c, and z >= k - c. But could we have y > k-c and z = k-c? That would satisfy both, but then we can reduce y to k-c by removing the most expensive y-(k-c) books, reducing total time, while still maintaining x+y >= k (since now exactly k) and x+z unchanged >= k. So y can be reduced to exactly k-c. Similarly for z. Thus optimum will have y = k-c, z = k-c, unless there is interaction: If we reduce y, Bob's condition unaffected. So yes, we can set both to minimum. So total books = c + (k-c) + (k-c) = 2k - c. So the solution with exactly those counts is optimal.
Thus the approach stands.
Now we must ensure that we consider cases where one category may not have enough books. For c from 0 to min(k, sizeBoth), we need both sizeAliceOnly >= k - c and sizeBobOnly >= k - c. If not, that c invalid.
What about using books from type 00? Could they help? They don't contribute to counts, so never beneficial.
Thus algorithm seems correct.
But wait: There is also possibility of using some 10 and 01 together to effectively act as a 11? For example, one book liked by Alice and one liked by Bob together satisfy one each. But they are separate books. The formulation with c, y, z already captures that. So good.
Now implement.
But careful: Input size up to 2e5, t_i up to 1e4, total sum might be up to 2e9 (but within 64-bit). Use long long.
Edge Cases:
If k = 0? Problem says 1 ≤ k, so ignore.
If no valid combination, output -1.
Now the sample tests:
Sample 1:
8 4
books: ... should get 18. Let's test mentally.
So both: books 1,2,4,6 (t:7,2,8,1) -> sorted:1,2,7,8
AliceOnly: (1,0) none? book? none have a=1,b=0. So size=0.
BobOnly: (0,1): books 3,5,7 (t:4,1,1) -> sorted:1,1,4
k=4.
Now we iterate c from 0 to min(4,4)=4.
c=0: need k-c=4 from AliceOnly and BobOnly. But AliceOnly size=0 <4 -> invalid.
c=1: need 3 from AliceOnly (size0) -> invalid.
c=2: need 2 from AliceOnly -> invalid.
c=3: need 1 from AliceOnly -> invalid.
c=4: need 0 from both -> valid. total = prefBoth[4] = 1+2+7+8=18. So output 18. Good.
c=0: need 2 from A and 2 from B -> sizes insufficient (1 each) invalid.
c=1: need 1 from A and 1 from B -> valid. total = prefBoth[1] (2) + prefA[1] (5) + prefB[1] (1) = 8. Output 8. Good.
Sample3:
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Categorize:
both: none.
AliceOnly: t=2,3
BobOnly: t=5,3? Actually books: 2 1 0 (t=2), 3 1 0 (t=3) -> A. BobOnly: 5 0 1 (t=5), 3 0 1 (t=3).
sizeBoth=0, sizeA=2, sizeB=2, k=3.
c from 0 to min(3,0)=0 only c=0.
Need 3 from A and 3 from B -> insufficient (max 2 each). Invalid. So -1. Good.
Thus algorithm works.
But note: In some solutions, there's a nuance: we may also consider picking extra books from both category beyond k? But as argued not beneficial. However, there is a possibility that it's cheaper to pick more than 2k - c books? Wait, suppose we have limited numbers: say we have few 11, many 10 and 01. If c is small, we need many from 10 and 01. But maybe it's cheaper to pick an extra 11 to replace one 10 and one 01? That's exactly what varying c does. So we are considering that.
But there is also possibility of using a (1,1) book to satisfy both conditions simultaneously, reducing total count of books needed. Since each (1,1) counts for both, it can reduce the number of (1,0) and (0,1) needed. That's covered.
Thus solution is straightforward.
Now let's implement in C++.
We'll need to read n,k. Use vector both, alice, bob. For each book, read t,a,b. If a==1 && b==1 push to both; else if a==1 && b==0 push to alice; else if a==0 && b==1 push to bob; else ignore.
Sort each vector. Compute prefix sums via cumulative sums (maybe using long long). For convenience, create prefix arrays of length size+1, where pref[0]=0. We can use vector prefBoth(both.size()+1), etc. Or compute on the fly by using partial sums.
Then loop c from 0 to min(k, (int)both.size()). For each c, need (k - c) <= alice.size() and (k - c) <= bob.size(). If true, compute total = prefBoth[c] + (k-c>0 ? prefAlice[k-c] : 0) + (k-c>0 ? prefBob[k-c] : 0). But prefAlice[0] is defined as 0, so we can just use prefAlice[k-c] if we have pref array of length size+1. However, need to ensure that k-c <= size, then prefAlice[k-c] exists. So condition must hold.
Keep minimum total (initialize to INF, e.g., 1e18). After loop, if min == INF, output -1, else output min.
Complexities: O(n log n).
Now, is there any scenario where we might need to select books from "none" category because we cannot meet counts otherwise? But none category doesn't help counts, so if counts cannot be met with 11,10,01, then impossible. So no.
Thus solution is correct for this version. However, note that the problem statement says "Easy and hard versions are actually different problems". This is the easy version? Possibly the easy version has constraints n <= 210^5 and we can sort. In the hard version, perhaps t_i up to 10^9 or something else? Actually the problem here seems to be the "Books" problem from Codeforces, where easy version is with n <= 210^5, and hard version with n <= 2*10^5 but also need to handle something else? I recall there is a problem "1102E" maybe? Actually there is a CF problem 1374E1 and E2? Let's think: The problem "Reading Books (easy version)" and "Reading Books (hard version)" from CF Round #653? Actually I recall a problem: "1368A" no. There's CF 1374E1 "Reading Books (easy version)" and 1374E2 "Reading Books (hard version)". In those, the constraints are similar but the easy version had a simpler approach (sorting by time within categories) while hard version required more careful selection because you have to output the books indices? Wait, the problem statement we have is exactly the same as CF 1374E1 "Reading Books (easy version)"? Actually CF 1374E1: "Reading Books (easy version)" has n up to 2e5, t up to 1e4. The solution is as described. The hard version (E2) has additional requirement: you must output the set of books? Or maybe constraints: you need to output the actual list of books and there is an additional condition: they must read exactly m books? Wait, the hard version: "Reading Books (hard version)" likely asks to output the indices, and maybe constraints about printing the set? Actually, the problem we are solving is the easy version, but we should confirm that we don't need to output indices. The output is only the minimum total time. That matches the easy version. In the hard version, you need to print the indices of chosen books. But the prompt says "Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set." In the easy version they only ask for total time? Actually I recall CF 1374E1 asks to output -1 or the minimum total time. So it's consistent. So our solution is for the easy version.
Thus answer: implement as described.
But careful: In the hard version, you need to output the indices. But we are solving easy.
Now let's think about corner cases: Suppose there are enough both books to cover k, but maybe they are expensive, while using some 10 and 01 could be cheaper? For example, both books are high cost, but 10 and 01 are cheap. Our iteration includes c=0 if possible. But if both > k, we might also consider c less than k. Example: both books cost 100 each, 10 books cost 1 each, 01 books cost 1 each. k=2. Options: c=2 => total = 200, c=1 => total = 100 + 1 + 1 = 102, c=0 => need 2 from each, but 10 and 01 each have at least 2? if yes, total = 1+1+1+1 = 4? Actually c=0: need 2 from 10 and 2 from 01 => total = 1+1 (for 10) + 1+1 (for 01) = 4. So c=0 is best if possible. So we need to consider all.
Now about the condition: need (k-c) from both 10 and 01 categories. But what if k-c is 0? That's fine; we can take 0 from that category.
Now, is there any scenario where picking extra books from both beyond c could reduce total because we can drop a 10 and a 01 but maybe we drop a high cost 10 and a low cost 01, and add a both that is moderate? That is exactly captured by varying c: increasing c by 1 reduces required y and z each by 1, and we replace one 10 and one 01 (the most expensive among selected?) In our calculation for a given c, we take the smallest (k-c) from 10 and 01. If we increase c, we recompute using the smallest (k-(c+1)). That automatically accounts for which books are selected: The set of selected 10 and 01 changes because we need one less from each. The optimal selection for each c is to take the cheapest ones. So our iteration over c exhaustively searches the optimal combination.
Thus algorithm is correct.
Now we should implement reading and sorting.
Time complexity O(n log n). Memory O(n).
Now potential pitfalls: sum may exceed 32-bit. Use long long.
Also, note that we need to output -1 if impossible.
Implementation steps:
int n, k; cin >> n >> k;
vector both, alice, bob; (use vector maybe but int is fine for times up to 1e4, sum up to 2e9, fits in 32-bit signed? Actually 2e5 * 1e4 = 2e9, just within 32-bit signed (2,147,483,647). But safe to use long long.)
Loop i=0..n-1: read t,a,b.
if (a && b) both.push_back(t);
else if (a) alice.push_back(t);
else if (b) bob.push_back(t);
Sort both.begin(), both.end()
Sort alice.begin(), alice.end()
Sort bob.begin(), bob.end()
Compute prefix sums vectors: prefBoth of length both.size()+1, etc.
prefBoth[0]=0; for i in 0..size-1: prefBoth[i+1]=prefBoth[i]+both[i];
Initialize answer = LLONG_MAX.
Let maxc = min(k, (int)both.size());
For c = 0 to maxc:
int need = k - c;
if (need <= (int)alice.size() && need <= (int)bob.size()) {
long long total = prefBoth[c] + prefAlice[need] + prefBob[need];
ans = min(ans, total);
}
If ans == LLONG_MAX, print -1; else print ans.
That's it.
But wait: There is also possibility that we might use some 00 books if they have negative time? not possible.
Thus solution done.
Now let's test with some additional cases.
Case: n=3,k=1. Books: (5,1,0), (6,0,1), (7,1,1). Options:
both:7, alice:5, bob:6.
c=0: need 1 from alice and 1 from bob -> need alice.size>=1, bob.size>=1 -> true, total =0+5+6=11.
c=1: need 0 -> total=7. So ans=7. That's correct? Check manually: we can pick the both book (7) only, satisfies both liked at least 1. That's cheaper than 11? 7<11 yes. So ans=7.
But maybe picking only alice and bob yields 11. So 7 better.
Now consider if both is very expensive: (100,1,1), alice:1, bob:2, k=1. c=0 total=1+2=3, c=1 total=100, ans=3. That's correct: pick both separately.
Now consider if both is cheap: (1,1,1), alice:100, bob:100, k=1. c=0 total=100+100=200, c=1 total=1, ans=1.
Now consider insufficient 10 or 01: n=2,k=1. books: (5,1,1) and (6,1,0). both:5, alice:6, bob:0. maxc = min(1,1)=1.
c=0: need 1 from alice and 1 from bob -> bob.size=0 <1 -> invalid.
c=1: need 0 -> total=5. ans=5. Works.
Now consider n=2,k=1: (5,1,0), (6,0,1). both:0, alice:5, bob:6. maxc=0 only c=0: need 1 from both categories -> sizes: alice=1,bob=1 => valid, total=5+6=11. So ans=11. No alternative.
Now consider n=3,k=2: both: (10,1,1) one, alice: (1,1,0), (2,1,0), bob: (3,0,1), (4,0,1). both size=1, alice size=2, bob size=2. maxc=1.
c=0: need 2 from alice and 2 from bob -> alice=2,bob=2 ok? need 2 each, exactly sizes 2, total = 1+2 + 3+4 = 10? Actually prefAlice[2] = 1+2=3, prefBob[2]=3+4=7, total=10.
c=1: need 1 from alice and 1 from bob -> total = both[0]=10 + alice[0]=1 + bob[0]=3 =14. So ans=10. But is there solution with c=1 and maybe taking an extra alice? That would increase total. So ans=10. Check if we can do better? The solution c=0 uses four books: two alice-only (1,2) and two bob-only (3,4). Total=10. c=1 uses both (10) and one alice (1) and one bob (3) total=14. So 10 is better.
But wait, is there possibility to use both and only one from alice and one from bob but we could choose the cheaper ones? Already done. So fine.
Now test scenario where using both reduces total because alice and bob books are expensive relative to both. Example: both: (5,1,1), (6,1,1); alice: (100,1,0), bob: (200,0,1). k=2. both size=2, alice size=1, bob size=1.
c=0: need 2 from alice and 2 from bob -> insufficient (alice only 1, bob only 1) -> invalid.
c=1: need 1 from alice and 1 from bob -> total = 5 + 100 + 200 = 305.
c=2: need 0 -> total = 5+6=11. ans=11. So both books alone suffice. So ans=11.
Now consider case where we need to take some from alice and bob but we might also take additional both to reduce count? Already covered.
Thus algorithm passes.
Now we need to implement reading efficiently, using scanf or cin with sync.
Edge: Input up to 2e5 lines, fine.
Now produce final answer.
But careful: The problem says "Alice and Bob will read each book together" meaning they read together, so the reading time is per book, not per person. So sum of t_i is correct.
One more nuance: Could we pick a book that is liked by neither? It does not help counts. So no.
Now, is there any scenario where picking a book liked by neither might allow us to drop a liked book that is expensive? Since the conditions are about counts, adding a 00 book doesn't reduce any count, so it would only increase total time. So not beneficial.
Thus solution stands.
Now final answer: Provide C++ code.
Now, some might think about a greedy approach: sort all books? Not needed.
Now let's ensure that prefix sums are computed as long long.
for (int i=0; i<both.size(); i++) prefBoth[i+1] = prefBoth[i] + both[i];
similar for others.
Then iterate:
int maxc = min(k, (int)both.size());
long long ans = 1e18; // or LLONG_MAX
for (int c=0; c<=maxc; c++) {
int need = k - c;
if (need <= alice.size() && need <= bob.size()) {
long long total = prefBoth[c] + prefAlice[need] + prefBob[need];
if (total < ans) ans = total;
}
}
if (ans == 1e18) cout << -1 << endl; else cout << ans << endl;
Make sure to use long long for need? need int.
Now testing with sample.
All good.
Now check for edge case where k=0? Not needed.
Now we could also consider case where both.size() is huge but k small, we only need up to k.
Now complexity: Sorting O(n log n). Loop O(k) but k up to n (2e5) so O(n) fine.
Now about memory: prefix sums size O(n) fine.
Thus final solution.
But wait: The problem title "1102A? Actually 1102A is something else". But it's fine.
We should also think about potential integer overflow: prefix sums of int can overflow if n=2e5 and t_i=1e4 -> sum=2e9, fits 32-bit signed (2,147,483,647). But to be safe, use long long.
Now I'll write the final code.
Potential improvements: Could use partial sums without storing all, but not needed.
Now final output.
One more nuance: Could there be scenario where it's optimal to take more than k books from both because both are cheaper than taking some 10 and 01? Let's think: Suppose we have both books with time 1, 1, 1,... and 10/01 are expensive. If k=2, we could take c=3, y=0, z=0. Then total = 1+1+1=3. While c=2 gives total=1+1=2 which is smaller. So not optimal. What about c=2 vs c=1: c=1 gives total = both[0] + need=1 from 10 and 1 from 01. If both[0] is 1, both[1] is 1, 10[0]=100, 01[0]=100, then c=2 total=2, c=1 total=1+100+100=201, so c=2 is better. So c is better when both are cheap. So we don't consider c>k because that would increase total beyond c=k if both are non-negative. Actually c=k gives sum of k smallest both; c=k+1 gives sum of k+1 smallest both, which is larger (since both[k] positive). So c>k cannot be better than c=k. So it's safe.
Thus final solution.
Now produce final answer.
But wait: There is a subtle point: What if some books have t_i = 0? Not in constraints (t_i >= 1). So okay.
Now code.
Potential pitfalls: When building prefix sums, we need to ensure that if need=0 we use pref[0]=0. So we can just use prefAlice[need] without condition because prefAlice has size at least 0? Actually we defined prefAlice with size alice.size()+1, so prefAlice[0] exists. So it's fine.
But we need to check need <= alice.size(), otherwise prefAlice[need] would be out of bounds. So we must check before accessing.
Now final solution.
Now produce final answer with code and explanation.
Now, I should also consider alternative approach: sort combined? Not needed.
Thus final.
Now final output code.
I'll include using namespace std; and fast IO.
Now, also note that the input could be large, so use scanf or ios_base::sync_with_stdio(false); cin.tie(NULL);
Now, I'll output code.
Now, I think this solves the problem.
But is there any hidden nuance? Let's re-read the problem statement: "Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set." So they each must like at least k books. If a book is liked by both, it counts for both. So our modeling is correct.
Now, the solution is straightforward.
Now produce final answer.
But the problem says "Easy and hard versions are actually different problems, so read statements of both problems completely and carefully." This indicates there is a hard version where you might need to output the set of books. But in this version, only output total time.
Thus final.
Now I'll write the final response with explanation and code.
```cpp
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
code
int n, k;
cin >> n >> k;
vector<int> both, alice, bob;
both.reserve(n);
alice.reserve(n);
bob.reserve(n);
for (int i = 0; i < n; ++i) {
int t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1) {
both.push_back(t);
} else if (a == 1) {
alice.push_back(t);
} else if (b == 1) {
bob.push_back(t);
}
// books with a=0,b=0 are ignored
}
// sort each category
sort(both.begin(), both.end());
sort(alice.begin(), alice.end());
sort(bob.begin(), bob.end());
// prefix sums
vector<ll> prefBoth(both.size() + 1, 0);
for (size_t i = 0; i < both.size(); ++i)
prefBoth[i + 1] = prefBoth[i] + both[i];
vector<ll> prefAlice(alice.size() + 1, 0);
for (size_t i = 0; i < alice.size(); ++i)
prefAlice[i + 1] = prefAlice[i] + alice[i];
vector<ll> prefBob(bob.size() + 1, 0);
for (size_t i = 0; i < bob.size(); ++i)
prefBob[i + 1] = prefBob[i] + bob[i];
int maxBoth = min(k, (int)both.size());
ll ans = LLONG_MAX;
for (int c = 0; c <= maxBoth; ++c) {
int need = k - c;
if (need <= (int)alice.size() && need <= (int)bob.size()) {
ll total = prefBoth[c] + prefAlice[need] + prefBob[need];
if (total < ans) ans = total;
}
}
if (ans == LLONG_MAX)
cout << -1 << "\n";
else
cout << ans << "\n";
return 0;