Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) A = [] B = [] AB = [] for i in range(n): t,a,b = map(int,input().split()) if a==1 and b==0: A.append(t) if b==1 and a==0: B.append(t) if a==1 and b==1: AB.append(t) A.sort() B.sort() AB.sort() ia = 0 ib = 0 iab = 0 kab = 0 total_time = 0 while(True): if (iab > len(AB)-1) and (ia > len(A)-1 or ib > len(B)-1): print(-1) break if iab <= len(AB)-1: if (ia > len(A)-1 or ib > len(B)-1): total_time += AB[iab] kab +=1 if kab == k: print(total_time) break iab += 1 else: if AB[iab]<A[ia]+B[ib]: total_time += AB[iab] kab +=1 if kab == k: print(total_time) break iab += 1 else: total_time += (A[ia]+B[ib]) kab +=1 if kab == k: print(total_time) break ia += 1 ib += 1 else: total_time += (A[ia]+B[ib]) kab +=1 if kab == k: print(total_time) break ia += 1 ib += 1
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = map(int,input().split(" ")) both = [] alice = [] bob = [] for i in range(n): t,a,b=map(int,input().split()) if (a == 1 and b == 1): both.append(t) elif (a == 1): alice.append(t) elif (b == 1): bob.append(t) alice.sort(); bob.sort() for i in range(min(len(bob),len(alice))): both.append(bob[i] + alice[i]) both.sort() if (len(both) < k): print(-1) else: print(sum(both[:k]))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k, ans = 1e10; cin >> n >> k; vector<long long> alice, bob, both, prb, pra, prboth; for (long long i = 1; i <= n; i++) { long long t, a, b; cin >> 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(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(both.begin(), both.end()); for (long long i = 0; i < signed(bob.size()); i++) { if (!i) prb.push_back(bob[i]); else prb.push_back(prb.back() + bob[i]); } for (long long i = 0; i < signed(alice.size()); i++) { if (!i) pra.push_back(alice[i]); else pra.push_back(pra.back() + alice[i]); } for (long long i = 0; i < signed(both.size()); i++) { if (!i) prboth.push_back(both[i]); else prboth.push_back(prboth.back() + both[i]); } if (signed(alice.size()) >= k && signed(bob.size()) >= k) ans = min(ans, prb[k - 1] + pra[k - 1]); for (long long i = 0; i < k && i < signed(both.size()); i++) { long long x = k - i - 1; if (signed(alice.size()) >= x && signed(bob.size()) >= x && x > 0) ans = min(ans, prboth[i] + pra[x - 1] + prb[x - 1]); if (i == k - 1) ans = min(ans, prboth[i]); } cout << ((ans == 1e10) ? -1 : ans) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t = 1; while (t--) solve(); return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int n = sc.nextInt(), k = sc.nextInt(); PriorityQueue<Integer> a, b, c; a = new PriorityQueue<>(); b = new PriorityQueue<>(); c = new PriorityQueue<>(); for (int i = 0; i < n; i++) { int ti = sc.nextInt(), ai = sc.nextInt(), bi = sc.nextInt(); if (ai == 1 && bi == 1) { c.add(ti); } else if (ai == 1) { a.add(ti); } else if(bi == 1) { b.add(ti); } } long ans = 0; boolean flag = true; while (k-- > 0) { long fst = a.isEmpty() ? Integer.MAX_VALUE : a.peek(); fst += b.isEmpty() ? Integer.MAX_VALUE : b.peek(); long snd = c.isEmpty() ? Integer.MAX_VALUE : c.peek(); if(Math.min(fst, snd) >= Integer.MAX_VALUE) { flag = false; break; } if(fst < snd) { ans += fst; a.poll(); b.poll(); }else { ans += snd; c.poll(); } } out.println(flag ? ans : -1); out.close(); } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public Long nextLong() throws IOException { return Long.parseLong(next()); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
/* ID: tommatt1 LANG: JAVA TASK: */ import java.util.*; import java.io.*; public class cf1374e2{ static PriorityQueue<pair> rem00;static PriorityQueue<pair> rem01;static PriorityQueue<pair> rem10;static PriorityQueue<pair> rem11; static PriorityQueue<pair> add00;static PriorityQueue<pair> add01;static PriorityQueue<pair> add10;static PriorityQueue<pair> add11; static long ans; static int ak,bk,curm; public static void main(String[] args)throws IOException { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(bf.readLine()); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); int k=Integer.parseInt(st.nextToken()); add00=new PriorityQueue<pair>();add01=new PriorityQueue<pair>();add10=new PriorityQueue<pair>();add11=new PriorityQueue<pair>(); rem00=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); rem01=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); rem10=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); rem11=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); pair[] bks=new pair[n]; for(int i=0;i<n;i++) { st=new StringTokenizer(bf.readLine()); int t1=Integer.parseInt(st.nextToken()); int a1=Integer.parseInt(st.nextToken()); int b1=Integer.parseInt(st.nextToken()); bks[i]=new pair(t1,a1,b1,i+1); } Arrays.sort(bks); for(pair i:bks) { if(i.a==1&&i.b==1) { add11.add(i); } else if(i.a==1) { add10.add(i); } else if(i.b==1) { add01.add(i); } else { add00.add(i); continue; } if(i.a==1&&i.b==1) { if(ak<k||bk<k) { add(i); if(ak>k&&!rem10.isEmpty()) { pair p=rem10.poll(); remove(p); } if(bk>k&&!rem01.isEmpty()) { pair p=rem01.poll(); remove(p); } } else { if(!rem10.isEmpty()&&!rem01.isEmpty()) { int old=rem10.peek().t+rem01.peek().t; if(old>i.t) { pair p1=rem10.poll(); pair p2=rem01.poll(); remove(p1); remove(p2); add(i); } } } } else if(i.a==1&&ak<k) { add(i); } else if(i.b==1&&bk<k){ add(i); } } while(curm>m) { pair p=low(add11); pair rema=high(rem10); pair remb=high(rem01); if(p==null||rema==null||remb==null) { out.println(-1); out.close(); System.exit(0); } remove(rema); remove(remb); add(p); } while(curm<m) { pair min00=low(add00); pair min10=low(add10); pair min01=low(add01); pair min11=low(add11); pair min=min(min(min00,min01),min(min10,min11)); pair max11=high(rem11); if(max11==null|min10==null||min01==null) { if(min==null) { out.println(-1); out.close(); System.exit(0); } add(min); } else { if(min.t<min01.t+min10.t-max11.t) { add(min); } else { remove(max11); add(min10); add(min01); } } } if(ak<k||bk<k) { out.println(-1); out.close(); System.exit(0); } else { out.println(ans); } for(pair i:bks) { if(i.inc) { out.print(i.id+" "); } } out.println(); out.close(); } static pair low(PriorityQueue<pair> pq) { while(!pq.isEmpty()) { pair p=pq.peek(); if(p.inc) pq.poll(); else return p; } return null; } static pair high(PriorityQueue<pair> pq) { while(!pq.isEmpty()) { pair p=pq.peek(); if(!p.inc) pq.poll(); else return p; } return null; } static pair min(pair a, pair b) { if(a==null) return b; if(b==null) return a; return a.t<=b.t?a:b; } static void add(pair i) { i.inc=true; ans+=i.t; curm++; if(i.a==1&&i.b==1) { ak++; bk++; rem11.add(i); } else if(i.a==1) { ak++; rem10.add(i); } else if(i.b==1) { bk++; rem01.add(i); } else { rem00.add(i); } } static void remove(pair i) { i.inc=false; ans-=i.t; curm--; if(i.a==1&&i.b==1) { ak--; bk--; add11.add(i); } else if(i.a==1) { ak--; add10.add(i); } else if(i.b==1) { bk--; add01.add(i); } else { add00.add(i); } } static class pair implements Comparable<pair>{ int t,a,b; boolean inc;int id; public pair(int t1,int x,int y,int id1) { t=t1;a=x;b=y;id=id1; } public int compareTo(pair p) { return t-p.t; //if(a>p.a) return 1; } } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; while (cin >> n >> k) { vector<int> both, alice, bob; for (int i = 0; i < n; ++i) { int t, a, b; cin >> 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()); vector<int> asum(alice.size() + 1); for (int i = 0; i < alice.size(); ++i) { asum[1 + i] = asum[i] + alice[i]; } vector<int> bsum(bob.size() + 1); for (int i = 0; i < bob.size(); ++i) { bsum[1 + i] = bsum[i] + bob[i]; } const int INF = 2e9 + 10; int res = INF; for (int i = 0, csum = 0; i <= min<int>(k, both.size()); ++i) { if (alice.size() + i >= k && bob.size() + i >= k) { int cur = asum[k - i] + bsum[k - i] + csum; res = min(res, cur); } if (i < both.size()) { csum += both[i]; } } cout << (res == INF ? -1 : res) << "\n"; } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
def answer(): if(n3+n1 < k):return -1 if(n3+n2 < k):return -1 ap=[0] for i in range(n1):ap.append(ap[-1] + a[i]) ap.append(0) bp=[0] for i in range(n2):bp.append(bp[-1] + b[i]) bp.append(0) start=max(max(0,k-n1),max(0,k-n2)) s=0 for i in range(start):s+=common[i] common.append(0) ans=1e10 for i in range(start,min(k,n3) + 1): ans=min(ans , s + ap[k-i] + bp[k-i]) s+=common[i] return ans n,k=map(int,input().split()) a,b,common=[],[],[] for i in range(n): t,x,y=map(int,input().split()) if(x and y):common.append(t) elif(x==1 and y==0):a.append(t) elif(x==0 and y==1):b.append(t) common.sort() a.sort() b.sort() n1,n2,n3=len(a),len(b),len(common) print(answer())
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int A[1000000], B[1000000], C[1000000]; int main() { int n, k, t, a, b; cin >> n >> k; int num1 = 0, num2 = 0, num3 = 0; for (int i = 1; i <= n; i++) { cin >> t >> a >> b; if (a == 1 && b != 1) A[++num1] = t; if (a == 0 && b == 1) B[++num2] = t; if (a == 1 && b == 1) C[++num3] = t; } if (num1 + num3 < k || num2 + num3 < k) { cout << -1; return 0; } sort(A + 1, A + num1 + 1); sort(B + 1, B + num2 + 1); sort(C + 1, C + num3 + 1); int i = 1, j = 1; int num = 0; int sum = 0; while (num < k) { if ((i > num1 && num1 < k) || (i > num2 && num2 < k)) { sum += C[j]; j++; num++; continue; } if (A[i] + B[i] <= C[j] || j > num3) { sum += (A[i] + B[i]); i++; } else { sum += C[j]; j++; } num++; } cout << sum << endl; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) both, a, b = [], [], [] for i in range(n): t, al, bl = map(int, input().split()) if al&bl: both.append(t) elif al: a.append(t) elif bl: b.append(t) a.sort(); b.sort() for i in range(min(len(a), len(b))): both.append(a[i]+b[i]) print(-1 if len(both)<k else sum(sorted(both)[:k]))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = map(int,input().split(' ')) x = [40000] y = [40000] z = [40000] c = d = 0 for i in range(n): t,a,b = map(int,input().split(' ')) if (a==1 and b==1): z.append(t) c+=1 d+=1 elif (a==1): x.append(t) c+=1 elif (b==1): y.append(t) d+=1 if (c<k or d<k): print(-1) else: x.sort(reverse=True) y.sort(reverse=True) z.sort(reverse=True) c = d = ans = 0 while (c<k or d<k): if (c<k and d<k): if (x[len(x)-1]+y[len(y)-1]<z[len(z)-1]): ans+=(x[len(x)-1]+y[len(y)-1]) x.pop() y.pop() else: ans+=z[len(z)-1] z.pop() c+=1 d+=1 elif (c<k): if (x[len(x)-1]<z[len(z)-1]): ans+=x[len(x)-1] x.pop() else: ans+=z[len(z)-1] z.pop() c+=1 else: if (y[len(y)-1]<z[len(z)-1]): ans+=y[len(y)-1] y.pop() else: ans+=z[len(z)-1] z.pop() d+=1 print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
# from math import factorial as fac from collections import defaultdict # from copy import deepcopy import sys, math f = None try: f = open('q1.input', 'r') except IOError: f = sys.stdin if 'xrange' in dir(__builtins__): range = xrange # print(f.readline()) sys.setrecursionlimit(10**2) def print_case_iterable(case_num, iterable): print("Case #{}: {}".format(case_num," ".join(map(str,iterable)))) def print_case_number(case_num, iterable): print("Case #{}: {}".format(case_num,iterable)) def print_iterable(A): print (' '.join(A)) def read_int(): return int(f.readline().strip()) def read_int_array(): return [int(x) for x in f.readline().strip().split(" ")] def rns(): a = [x for x in f.readline().split(" ")] return int(a[0]), a[1].strip() def read_string(): return list(f.readline().strip()) def ri(): return int(f.readline().strip()) def ria(): return [int(x) for x in f.readline().strip().split(" ")] def rns(): a = [x for x in f.readline().split(" ")] return int(a[0]), a[1].strip() def rs(): return list(f.readline().strip()) def bi(x): return bin(x)[2:] from collections import deque import math NUMBER = 10**9 + 7 # NUMBER = 998244353 def factorial(n) : M = NUMBER f = 1 for i in range(1, n + 1): f = (f * i) % M # Now f never can # exceed 10^9+7 return f def mult(a,b): return (a * b) % NUMBER def minus(a , b): return (a - b) % NUMBER def plus(a , b): return (a + b) % NUMBER def egcd(a, b): if a == 0: return (b, 0, 1) else: g, y, x = egcd(b % a, a) return (g, x - (b // a) * y, y) def modinv(a): m = NUMBER g, x, y = egcd(a, m) if g != 1: raise Exception('modular inverse does not exist') else: return x % m def choose(n,k): if n < k: assert false return mult(factorial(n), modinv(mult(factorial(k),factorial(n-k)))) % NUMBER from collections import deque, defaultdict import heapq from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc def dfs(g, timeIn, timeOut,depths,parents): # assign In-time to node u cnt = 0 # node, neig_i, parent, depth stack = [[1,0,0,0]] while stack: v,neig_i,parent,depth = stack[-1] parents[v] = parent depths[v] = depth # print (v) if neig_i == 0: timeIn[v] = cnt cnt+=1 while neig_i < len(g[v]): u = g[v][neig_i] if u == parent: neig_i+=1 continue stack[-1][1] = neig_i + 1 stack.append([u,0,v,depth+1]) break if neig_i == len(g[v]): stack.pop() timeOut[v] = cnt cnt += 1 # def isAncestor(u: int, v: int, timeIn: list, timeOut: list) -> str: # return timeIn[u] <= timeIn[v] and timeOut[v] <= timeOut[u] cnt = 0 @bootstrap def dfs(v,adj,timeIn, timeOut,depths,parents,parent=0,depth=0): global cnt parents[v] = parent depths[v] = depth timeIn[v] = cnt cnt+=1 for u in adj[v]: if u == parent: continue yield dfs(u,adj,timeIn,timeOut,depths,parents,v,depth+1) timeOut[v] = cnt cnt+=1 yield def gcd(a,b): if a == 0: return b return gcd(b % a, a) # Function to return LCM of two numbers def lcm(a,b): return (a*b) / gcd(a,b) def get_num_2_5(n): twos = 0 fives = 0 while n>0 and n%2 == 0: n//=2 twos+=1 while n>0 and n%5 == 0: n//=5 fives+=1 return (twos,fives) def solution(data,c,a,b,n,k): if b.count(1) < k or a.count(1) < k: return -1 al = [] bo = [] both = [] for i in range(n): if a[i] == 1 and b[i] == 1: both.append(c[i]) elif a[i] == 1: al.append(c[i]) elif b[i] == 1: bo.append(c[i]) bo.sort(reverse=True) al.sort(reverse=True) both.sort(reverse=True) curr = 0 res = 0 inf = 10**10 while curr < k: both_x = both[-1] if both else inf al_x = al[-1] if al else inf bo_x = bo[-1] if bo else inf if both_x > al_x + bo_x: res+=al.pop() + bo.pop() else: res+=both.pop() curr+=1 return res def main(): T = 1 # T = ri() for i in range(T): # n = ri() # s = rs() data = [] n,k = ria() # a = ria() c,a,b = [],[],[] for j in range(n): c_,a_,b_=ria() c.append(c_) a.append(a_) b.append(b_) # data.append(c_,a_,b_) x = solution(data,c,a,b,n,k) if 'xrange' not in dir(__builtins__): print(x) else: print >>output,str(x)# "Case #"+str(i+1)+':', if 'xrange' in dir(__builtins__): print(output.getvalue()) output.close() if 'xrange' in dir(__builtins__): import cStringIO output = cStringIO.StringIO() #example usage: # for l in res: # print >>output, str(len(l)) + ' ' + ' '.join(l) if __name__ == '__main__': main()
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
import sys if sys.subversion[0] == "PyPy": import io, atexit sys.stdout = io.BytesIO() atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue())) sys.stdin = io.BytesIO(sys.stdin.read()) input = lambda: sys.stdin.readline().rstrip() RS = raw_input RI = lambda x=int: map(x,RS().split()) RN = lambda x=int: x(RS()) ''' ...................................................................... ''' n,k = RI() alice,bob,com = [],[],[] for i in xrange(n): t,a,b = RI() if a==b and a==1: com.append(t) elif a==1: alice.append(t) elif b==1: bob.append(t) alice.sort(); bob.sort(); com.sort() n1,n2,n3 = len(alice),len(bob),len(com) if n1+n3 < k or n2+n3<k: print -1 else: ans = 0 i,j,l = 0,0,0 left1,left2 = k,k while l<n3 and (left1>n1 or left2>n2): ans += com[l] left1-=1 left2-=1 l+=1 while left1>0 and left2>0: if l<n3 and (i>=n1 or j>=n2 or com[l]<=(alice[i]+bob[j])): ans += com[l] l+=1 left1-=1 left2-=1 else: ans += alice[i] i+=1; left1-=1 ans += bob[j] j+=1; left2-=1 while left1>0: ans += alice[i] i+=1 left1-=1 while left2>0: ans += bob[j] j+=1 left2-=1 print ans
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from typing import List from heapq import heappop, heappush # Heap def readingBooks_easy(n: int, k: int, books: List[int]) -> int: books.sort(key = lambda x: x[0]) alice = [] bob = [] alice_and_bob = [] for i in range(len(books)): if (books[i][1] == 1 and books[i][2] == 0): alice.append(books[i][0]) elif (books[i][1] == 0 and books[i][2] == 1): bob.append(books[i][0]) elif (books[i][1] == 1 and books[i][2] == 1): alice_and_bob.append(books[i][0]) alice_or_bob = [] for i in range(min(len(alice), len(bob))): alice_or_bob.append(alice[i] + bob[i]) heap = [] for i in range(len(alice_and_bob)): heappush(heap, alice_and_bob[i]) for i in range(len(alice_or_bob)): heappush(heap, alice_or_bob[i]) ans = 0 for _ in range(k): if (heap): ans += heappop(heap) else: return -1 return ans # TLE for when Alice and Bob are reading 20k+ books. Should use a dict to optimize and add all at once. # def readingBooks_easy(n: int, k: int, books: List[int]) -> int: # books.sort(key = lambda x: x[0]) # alice = [] # bob = [] # alice_bob = [] # for i in range(len(books)): # if (books[i][1] == 1 and books[i][2] == 0): # alice.append(books[i][0]) # elif (books[i][1] == 0 and books[i][2] == 1): # bob.append(books[i][0]) # elif (books[i][1] == 1 and books[i][2] == 1): # alice_bob.append(books[i][0]) # ans = 0 # for _ in range(k): # if ((not alice or not bob) and not alice_bob): # return -1 # elif ((alice and bob) and not alice_bob): # ans += alice[0] + bob[0] # alice.pop(0) # bob.pop(0) # elif ((not alice or not bob) and alice_bob): # ans += alice_bob[0] # alice_bob.pop(0) # else: # if (alice_bob[0] < alice[0] + bob[0]): # ans += alice_bob[0] # alice_bob.pop(0) # else: # ans += alice[0] + bob[0] # alice.pop(0) # bob.pop(0) # return ans inputs = list(map(int, input().split(" "))) n = inputs[0] k = inputs[1] books = [] for _ in range(n): books.append(list(map(int, input().split(" ")))) print(readingBooks_easy(n, k, books))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
#!/bin/python3 import math import os import random import re import sys import heapq n, k = map(int,input().split()) a = [] b = [] ab = [] for _ in range(n): t, av, bv = map(int, input().split()) if av == 1 and bv == 1: #heapq.heappush(ab, t) ab.append(t) elif av == 1: #heapq.heappush(a, t) a.append(t) elif bv == 1: #heapq.heappush(b, t) b.append(t) a = sorted(a, reverse=True) b = sorted(b, reverse=True) ab = sorted(ab, reverse=True) ans = 0 infi = 10**5 possible = True for _ in range(k): if (len(ab) == 0) and (len(a) == 0 or len(b) == 0): possible = False break at = a[-1] if len(a) > 0 else infi bt = b[-1] if len(b) > 0 else infi abt = ab[-1] if len(ab) > 0 else infi if (at+ bt) < abt: ans += (at+bt) a.pop() b.pop() else: ans += abt ab.pop() if possible: print(ans) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, k, t, a, b; vector<long long> v, va, vb; int main() { cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> t >> a >> b; if (a == 1 && b == 1) v.push_back(t); else if (a == 1 && b == 0) va.push_back(t); else if (a == 0 && b == 1) vb.push_back(t); } sort(v.begin(), v.end()); sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); long long zx = min(va.size(), vb.size()); if ((v.size() + zx) < k) { cout << -1 << "\n"; return 0; } for (long long i = 1; i < v.size(); i++) v[i] += v[i - 1]; for (long long i = 1; i < zx; i++) { va[i] += va[i - 1]; vb[i] += vb[i - 1]; } long long minn = 1000000000001; if (v.size() >= k) minn = min(minn, v[k - 1]); for (long long i = 0; i < v.size(); i++) { long long p = k - (i + 1); if (zx >= p && p > 0) { long long cnt = v[i] + va[p - 1] + vb[p - 1]; minn = min(minn, cnt); } } if (zx >= k) minn = min(minn, va[k - 1] + vb[k - 1]); cout << minn << "\n"; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; public class E { static long m = (long) (1e9 + 7); public static void main(String[] args) throws IOException { Scanner scn = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n = scn.nextInt(), k = scn.nextInt(); PriorityQueue<Integer> p1 = new PriorityQueue<>(), p2 = new PriorityQueue<>(), p12 = new PriorityQueue<>(); for (int i = 0; i < n; i++) { int t = scn.nextInt(), f = scn.nextInt(), s = scn.nextInt(); if (f == 1 && s == 1) { p12.add(t); } else if (f == 1) { p1.add(t); } else if (s == 1) { p2.add(t); } } int ans = 0; boolean bool = false; while (k > 0) { if (!p1.isEmpty() && !p2.isEmpty() && !p12.isEmpty() && (p1.peek() + p2.peek()) <= p12.peek()) { ans += p1.poll(); ans += p2.poll(); } else if (!p1.isEmpty() && !p2.isEmpty() && !p12.isEmpty() && (p1.peek() + p2.peek()) > p12.peek()) { ans += p12.poll(); } else if (!p12.isEmpty()) { ans += p12.poll(); } else if (!p1.isEmpty() && !p2.isEmpty()) { ans += p1.poll(); ans += p2.poll(); } else { bool = true; break; } k--; } sb.append(bool ? -1 : ans); sb.append("\n"); System.out.print(sb); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k1 = map(int,input().split()) A = [] B = [] AB = [] for i in range(n): t,a,b = map(int,input().split()) if a==1 and b==1: AB.append(t) elif a==1: A.append(t) elif b==1: B.append(t) AB.sort() A.sort() B.sort() if len(AB) + len(A) >=k1 and len(AB) + len(B) >=k1: a = 0 b = 0 i = 0 j = 0 k = 0 ans = 0 books = 0 while i < len(A) and j<len(B) and k < len(AB): if A[i] + B[j] <= AB[k] : ans += A[i]+B[j] i+=1 j+=1 books +=1 else: ans += AB[k] k+=1 books+=1 if books == k1 : break if i >= len(A) and books!=k1: ans += sum(AB[k:k+k1-books]) elif j>= len(B) and books != k1: ans += sum(AB[k:k+k1-books]) elif k >= len(AB) and books!=k1: ans += sum(A[i:i+k1-books]) + sum(B[j:j+k1-books]) print(ans) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import heapq n, k = map(int, input().split()) x, y, z = [], [], [] heapq.heapify(x) heapq.heapify(y) heapq.heapify(z) for _ in range(n): t, a, b = map(int, input().split()) if a == b == 1: heapq.heappush(x, t) elif a == 1: heapq.heappush(y, t) elif b == 1: heapq.heappush(z, t) lx, ly, lz = len(x), len(y), len(z) u, v = len(x), min(len(y), len(z)) ans = 0 if u + v >= k: i, j = 0, 0 for _ in range(k): if i < u and j < v: if x[0] <= y[0] + z[0]: ans += heapq.heappop(x) i += 1 else: ans += (heapq.heappop(y) + heapq.heappop(z)) j += 1 elif i < u: ans += heapq.heappop(x) i += 1 elif j < v: ans += (heapq.heappop(y) + heapq.heappop(z)) j += 1 else: ans = -1 print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e9 + 9; const int MAX = 100; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout << fixed; long long t = 1; while (t--) { long long n, k; cin >> n >> k; vector<pair<int, int> > vp1; vector<pair<int, int> > vp2; vector<int> v; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; if (b && !c) vp1.push_back({a, b}); if (c && !b) vp2.push_back({a, c}); if (b && c) v.push_back(a); } sort(v.begin(), v.end()); long long ans = 0; sort(vp1.begin(), vp1.end()); sort(vp2.begin(), vp2.end()); int i = 0, j = 0; while (k > 0) { if (i == vp1.size() || i == vp2.size()) { break; } if (j == v.size()) { break; } if (v[j] <= vp1[i].first + vp2[i].first) { ans += v[j]; j++; k--; } else { ans += vp1[i].first + vp2[i].first; i++; k--; } } while (i < vp1.size() && k > 0 && i < vp2.size()) { ans += vp1[i].first + vp2[i].first; k--; i++; } while (j < v.size() && k > 0) { ans += v[j]; k--; j++; } if (k > 0) { cout << "-1" << '\n'; continue; } cout << ans << '\n'; } return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; const int inf = 1e9 + 7; const long long mod = 1e9 + 7; int a[maxn]; int b[maxn]; int t[maxn]; vector<int> both; vector<int> alice; vector<int> bob; int psum_alice[maxn]; int psum_bob[maxn]; int psum_both[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) { both.push_back(t[i]); } else if (a[i] == 1) { alice.push_back(t[i]); } else if (b[i] == 1) { bob.push_back(t[i]); } } sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); for (int i = 0; i < both.size(); i++) { psum_both[i + 1] = psum_both[i] + both[i]; } for (int i = 0; i < alice.size(); i++) { psum_alice[i + 1] = psum_alice[i] + alice[i]; } for (int i = 0; i < bob.size(); i++) { psum_bob[i + 1] = psum_bob[i] + bob[i]; } long long ans = -1; for (int i = 0; i <= k; i++) { if (i > both.size() || k - i > alice.size() || k - i > bob.size()) { continue; } long long subans = psum_both[i] + psum_alice[k - i] + psum_bob[k - i]; if (ans == -1 || subans < ans) { ans = subans; } } cout << ans << endl; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
// package Round_653; import java.io.*; import java.math.*; import java.util.*; public class Problem_E1 { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int numBooks = in.nextInt(), minLike = in.nextInt(); ArrayList<Book> books = new ArrayList<Book>(); for(int i = 0; i < numBooks; i++) { books.add(new Book(in.nextInt(), in.nextInt(), in.nextInt())); } ArrayList<Book> aLike = new ArrayList<Book>(); ArrayList<Book> bLike = new ArrayList<Book>(); ArrayList<Book> bothLike = new ArrayList<Book>(); Collections.sort(books, new SortA()); for(int i = 0; i < books.size(); i++) { Book next = books.get(i); if(next.aLike != 1 || next.bLike == 1) { break; } aLike.add(next); if(aLike.size() == minLike) { break; } } Collections.sort(books, new SortB()); for(int i = 0; i < books.size(); i++) { Book next = books.get(i); if(next.bLike != 1 || next.aLike == 1) { break; } bLike.add(next); if(bLike.size() == minLike) { break; } } int minLen = Math.min(aLike.size(), bLike.size()); if(aLike.size() == minLen) { while(bLike.size() > minLen) { bLike.remove(bLike.size() - 1); } } else { while(aLike.size() > minLen) { aLike.remove(aLike.size() - 1); } } int taken = aLike.size() + bLike.size(); Collections.sort(books, new SortS()); for(int i = 0; i < books.size(); i++) { Book next = books.get(i); if(next.aLike == 0 || next.bLike == 0) { break; } if(taken == minLike * 2) { if(aLike.size() == 0 || bLike.size() == 0) { break; } int rem = aLike.get(aLike.size() - 1).time + bLike.get(bLike.size() - 1).time; if(rem > next.time) { aLike.remove(aLike.size() - 1); bLike.remove(bLike.size() - 1); bothLike.add(next); } } else { taken += 2; bothLike.add(next); } } if(taken != minLike * 2) { out.println(-1); } else { int time = 0; for(Book b : aLike) { time += b.time; } for(Book b : bLike) { time += b.time; } for(Book b : bothLike) { time += b.time; } out.println(time); } out.flush(); } static class Book{ int time, aLike, bLike; public Book(int time, int a, int b) { this.time = time; aLike = a; bLike = b; } public String toString() { return time + " " + aLike + " " + bLike; } } static class SortTime implements Comparator<Book>{ @Override public int compare(Book arg0, Book arg1) { return arg0.time - arg1.time; } } static class SortS implements Comparator<Book>{ @Override public int compare(Book arg0, Book arg1) { int v1 = arg0.aLike == 1 && arg0.bLike == 1? 1 : 0; int v2 = arg1.aLike == 1 && arg1.bLike == 1? 1 : 0; if(v1 != v2) { return v2 - v1; } else { return arg0.time - arg1.time; } } } static class SortA implements Comparator<Book>{ @Override public int compare(Book arg0, Book arg1) { int v1 = arg0.aLike == 1 && arg0.bLike == 0? 1 : 0; int v2 = arg1.aLike == 1 && arg1.bLike == 0? 1 : 0; if(v1 != v2) { return v2 - v1; } else { return arg0.time - arg1.time; } } } static class SortB implements Comparator<Book>{ @Override public int compare(Book arg0, Book arg1) { int v1 = arg0.aLike == 0 && arg0.bLike == 1? 1 : 0; int v2 = arg1.aLike == 0 && arg1.bLike == 1? 1 : 0; if(v1 != v2) { return v2 - v1; } else { return arg0.time - arg1.time; } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.*; import java.io.*; import java.lang.Math; import java.util.Random; public class Solution{ public static void main(String[] args) throws Exception{ FastScanner fs = new FastScanner(); int n = fs.nextInt(), k = fs.nextInt(); ArrayList<Book> listA = new ArrayList<Book>(n); ArrayList<Book> listB = new ArrayList<Book>(n); ArrayList<Book> listBoth = new ArrayList<Book>(n); int sigA = 0, sigB = 0; for(int i=0;i<n;i++){ int t = fs.nextInt(), a = fs.nextInt(), b = fs.nextInt(); sigA += a; sigB += b; if(a==1 && b==1) listBoth.add(new Book(t,a,b)); else if(a==1) listA.add(new Book(t,a,b)); else if(b==1) listB.add(new Book(t,a,b)); } if(sigA<k || sigB<k){ System.out.println(-1); } else{ Collections.sort(listA); Collections.sort(listB); Collections.sort(listBoth); int nA = listA.size(), nB = listB.size(), nC = listBoth.size(); int a=0, b=0, c=0; int num = k; int ans = 0; while(num>0){ if(a<nA && b<nB && c<nC){ int val = listBoth.get(c).t - listA.get(a).t - listB.get(b).t; if(val<0){ ans += listBoth.get(c).t; c++; num--; } else{ ans += listA.get(a).t + listB.get(b).t; a++; b++; num--; } } else if(a==nA || b==nB){ ans += listBoth.get(c).t; c++; num--; } else if(c==nC){ ans += listA.get(a).t + listB.get(b).t; a++; b++; num--; } } System.out.println(ans); } } static class Book implements Comparable<Book>{ int t,a,b; public Book(int t, int a, int b){ this.t = t; this.a = a; this.b = b; } public int compareTo(Book b){ return Integer.compare(this.t,b.t); } } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong(){ return Long.parseLong(next()); } } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int s[200000], m[200000], n[200000]; int main() { long long ans = 0; int f, k, t[200000], a[200000], b[200000], i, count = 0; int g = 0, h = 0; cin >> f >> k; for (i = 0; i < f; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) { s[count] = t[i]; count++; } if (a[i] == 0 && b[i] == 1) { n[h] = t[i]; h++; } if (a[i] == 1 && b[i] == 0) { m[g] = t[i]; g++; } } int l = min(g, h); sort(n, n + h); sort(m, m + g); for (i = count; i < (count + l); i++) s[i] = n[i - count] + m[i - count]; sort(s, s + count + l); if (count + l < k) cout << -1; else { for (i = 0; i < k; i++) ans += s[i]; cout << ans; } return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
#: Author - Soumya Saurav import sys,io,os,time from collections import defaultdict from collections import OrderedDict from collections import deque from itertools import combinations from itertools import permutations import bisect,math,heapq alphabet = "abcdefghijklmnopqrstuvwxyz" input = sys.stdin.readline ######################################## n , k = map(int , input().split()) both = [] alice = [] bob = [] for i in range(n): x,y,z = map(int, input().split()) if y and z: both.append(x) elif y: alice.append(x) elif z: bob.append(x) alice.sort() bob.sort() for i in range(min(len(alice),len(bob))): both.append(alice[i]+bob[i]) both.sort() if len(both)<k: print(-1) else: print(sum(both[:k]))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long t = 1; while (t--) { long long n, k; ; cin >> n >> k; multiset<long long> alice, bob, both; for (long long i = 0; i < n; i++) { long long ti, ai, bi; cin >> ti >> ai >> bi; if (ai == 1 && bi == 1) both.insert(ti); else if (ai == 1) alice.insert(ti); else if (bi == 1) bob.insert(ti); } long long ans = 0; while (k > 0) { if (!alice.empty() && !bob.empty() && !both.empty()) { long long a = *alice.begin(); long long b = *bob.begin(); long long c = *both.begin(); if (a + b < c) { ans += a + b; alice.erase(alice.find(a)); bob.erase(bob.find(b)); } else { ans += c; both.erase(both.find(c)); } } else if (!both.empty()) { long long x = *both.begin(); ans += x; both.erase(both.find(x)); } else if (!alice.empty() && !bob.empty()) { long long a = *alice.begin(); long long b = *bob.begin(); ans += a + b; alice.erase(alice.find(a)); bob.erase(bob.find(b)); } else { ans = -1; break; } k--; } cout << ans << endl; } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) x, y, z = [], [], [] for _ in range(n): t, a, b = map(int, input().split()) if a == b == 1: x.append(t) elif a == 1: y.append(t) elif b == 1: z.append(t) lx, ly, lz = len(x), len(y), len(z) x.sort() y.sort() z.sort() u, v = len(x), min(len(y), len(z)) ans = 0 if u + v >= k: i, j = 0, 0 for _ in range(k): if i < u and j < v: if x[i] <= y[j] + z[j]: ans += x[i] i += 1 else: ans += (y[j] + z[j]) j += 1 elif i < u: ans += x[i] i += 1 elif j < v: ans += (y[j] + z[j]) j += 1 else: ans = -1 print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
# Enter your code here. Read input from STDIN. Print output to STDOUT def find(books,k): both = [] A = [] B = [] # both + (k-both) + (k-both) for (t,a,b) in books: if a == 1 and b == 1: both += [t] elif a == 1: A += [t] elif b == 1: B += [t] both = sorted(both) A = sorted(A) B = sorted(B) ans = 10**10 num = 0 temp = 0 pre_both = [] for i in range(len(both)): temp += both[i] pre_both += [temp] temp = 0 pre_A = [] for i in range(len(A)): temp += A[i] pre_A += [temp] temp = 0 pre_B = [] for i in range(len(B)): temp += B[i] pre_B += [temp] for num in range(min(k,len(pre_both))+1): if num>len(pre_both): break if num>0: need = pre_both[num-1] if num == k: ans = min(ans,need) break remain = k-num if remain>len(A): continue if remain>len(B): continue if num>0: need = pre_both[num-1] + pre_A[remain-1] + pre_B[remain-1] else: need = pre_A[remain-1] + pre_B[remain-1] ans = min(ans,need) if ans == 10**10: return -1 return ans n,k = list(map(int,input().strip().split())) books = [] for _ in range(n): t,a,b = list(map(int,input().strip().split())) books +=[(t,a,b)] print(find(books,k))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) a = [] b = [] ab = [] ans = 0 for _ in range(n): t1, a1, b1 = map(int, input().split()) if a1==1 and b1==1: ab.append(t1) elif a1==1 and b1==0: a.append(t1) elif a1==0 and b1==1: b.append(t1) else: pass a.sort() b.sort() ab.sort() if len(ab)+len(a)<k or len(ab)+len(b)<k: print("-1") else: lena, lenb, lenab = len(a), len(b), len(ab) i, j = 0,0 for _ in range(k): if i<lena and i<lenb and j<lenab: if a[i]+b[i]<ab[j]: ans += a[i]+b[i] i+=1 else: ans += ab[j] j+=1 elif i<lena and i<lenb: ans += a[i] + b[i] i += 1 else: ans += ab[j] j+=1 print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > al, bob, both, v, ansb, alr, bobr, norr, vc, neither; set<long long> ans; int vis[200005], l1[200005], l2[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long a = 0, b = 0, c, d, e, f = 0, g, m, n, k, i, j, t, in, p, q, l, r; cin >> n >> m >> k; vc.push_back({0, 0}); for (i = 0; i < n; i++) { cin >> p >> a >> b; l1[i + 1] = a; l2[i + 1] = b; v.push_back({p, i + 1}); vc.push_back({p, i + 1}); if (a && b) { both.push_back({p, i + 1}); } else if (a) al.push_back({p, i + 1}); else if (b) bob.push_back({p, i + 1}); } sort(both.begin(), both.end()); sort(bob.begin(), bob.end()); sort(al.begin(), al.end()); sort(v.begin(), v.end()); if (both.size() + bob.size() < k || both.size() + al.size() < k) { cout << -1 << '\n'; return 0; } for (i = 1; i < both.size(); i++) { both[i].first += both[i - 1].first; } for (i = 1; i < bob.size(); i++) { bob[i].first += bob[i - 1].first; } for (i = 1; i < al.size(); i++) { al[i].first += al[i - 1].first; } if (al.size() >= k && bob.size() >= k && m >= k * 2 && both.size() == 0) { f = al[k - 1].first + bob[k - 1].first; p = k; q = k; r = 0; } else { p = 0; q = 0; r = 0; f = 9999999999999999; } l = both.size(); l = min(l, m); for (i = l - 1; i >= l - 1 && i >= 0; i--) { d = max(0LL, k - i - 1); if (d > 0 && (d > al.size() || d > bob.size())) break; if (d * 2 + i + 1 > m) break; c = both[i].first; if (d > 0) c += al[d - 1].first + bob[d - 1].first; f = c; p = max(0LL, d); q = max(0LL, d); r = i + 1; } if (f == 9999999999999999) { cout << -1 << '\n'; return 0; } for (i = 0; i < p; i++) { ans.insert(al[i].second); vis[al[i].second] = 1; } for (i = 0; i < q; i++) { ans.insert(bob[i].second); vis[bob[i].second] = 1; } for (i = 0; i < r; i++) { ans.insert(both[i].second); ansb.push_back(vc[both[i].second]); vis[both[i].second] = 1; } i = 0; while (ans.size() < m) { if (vis[v[i].second]) { i++; continue; } else { f += v[i].first; ans.insert(v[i].second); vis[v[i].second] = 1; if (l1[v[i].second] && l2[v[i].second]) ansb.push_back(v[i]); i++; } } sort(ansb.begin(), ansb.end()); for (i = 0; i < n; i++) { if (!vis[v[i].second]) { p = v[i].second; if (l1[p] && l2[p]) ; else if (l1[p]) alr.push_back(v[i]); else if (l2[p]) bobr.push_back(v[i]); else norr.push_back(v[i]); } else if (!l1[v[i].second] && !l2[v[i].second]) { neither.push_back(v[i]); } } sort(alr.begin(), alr.end()); reverse(alr.begin(), alr.end()); sort(bobr.begin(), bobr.end()); reverse(bobr.begin(), bobr.end()); sort(norr.begin(), norr.end()); reverse(norr.begin(), norr.end()); sort(neither.begin(), neither.end()); p = 0; q = 0; for (auto it : ans) { if (l1[it]) p++; if (l2[it]) q++; } while (ansb.size() > 0) { if (p - 1 < k && q - 1 < k) { if (neither.size() == 0 || alr.size() == 0 || bobr.size() == 0) break; long long c1 = ansb.back().first + neither.back().first; long long c2 = alr.back().first + bobr.back().first; if (c2 < c1) { ans.erase(ansb.back().second); ans.erase(neither.back().second); norr.push_back(neither.back()); ansb.pop_back(); neither.pop_back(); f -= c1; f += c2; ans.insert(alr.back().second); ans.insert(bobr.back().second); alr.pop_back(); bobr.pop_back(); } else break; } else { c = ansb.back().first; in = ansb.back().second; long long mn = 99999999999; if (p - 1 < k) { if (!alr.size()) break; mn = alr.back().first; if (mn < c) { q--; ans.erase(in); ans.insert(alr.back().second); alr.pop_back(); ansb.pop_back(); f -= c; f += mn; } else break; } else if (q - 1 < k) { if (!bobr.size()) break; mn = bobr.back().first; if (mn < c) { p--; ans.erase(in); ans.insert(bobr.back().second); bobr.pop_back(); f -= c; f += mn; ansb.pop_back(); } else break; } else { mn = c; int fl = 0; if (alr.size() && alr.back().first < mn) { mn = alr.back().first; fl = 1; } if (bobr.size() && bobr.back().first < mn) { mn = bobr.back().first; fl = 2; } if (norr.size() && norr.back().first < mn) { mn = norr.back().first; fl = 3; } if (fl == 0) break; else if (fl == 1) { q--; ans.erase(in); ans.insert(alr.back().second); alr.pop_back(); ansb.pop_back(); f -= c; f += mn; } else if (fl == 2) { p--; ans.erase(in); ans.insert(bobr.back().second); bobr.pop_back(); f -= c; f += mn; ansb.pop_back(); } else { p--; q--; ans.erase(in); ans.insert(norr.back().second); neither.push_back(norr.back()); norr.pop_back(); f -= c; f += mn; ansb.pop_back(); } } } } cout << f << '\n'; for (auto it : ans) cout << it << ' '; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x-1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: none.append(i) else: Both.append(i) else: if i[1] == 0: Bob.append(i) else: Alice.append(i) z += 1 n -= 1 Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) lnone = len(none) tresult = [] if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] tresult1 = [] if min(len(Alice), len(Bob)) == len(Alice): if len(Alice) < k: k1 = k - len(Alice) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 else: if len(Bob) < k: k1 = k - len(Bob) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 ltresult1 = len(tresult1) Alice1 = Alice[:k] Bob1 = Bob[:k] Alice = Alice[k:] Bob = Bob[k:] corr = [] calczz = m - (2 * k) - ltresult1 if calczz > 0 and lnone != 0: xtr = [] if len(Alice) > calczz: xtr = Alice[:calczz] else: xtr = Alice if len(Bob) > calczz: xtr = xtr + Bob[:calczz] else: xtr = xtr + Bob if lnone > calczz: xtr = xtr + none[:calczz] else: xtr = xtr + none xtr = xtr[:calczz] xtr.sort(key=lambda x: (x[1], x[2]), reverse=True) zz = sum(row[1] == row[2] == 0 for row in xtr) else: zz = 0 if lnone == zz: nonechk = 9999999999 else: nonechk = none[zz][0] while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] >= Both[0][0] + min(Alice1[-1][0],Bob1[-1][0],nonechk): if min(Alice1[-1][0],Bob1[-1][0],nonechk) == nonechk: zz += 1 if lnone == zz: nonechk = 9999999999 else: nonechk = none[zz][0] Alice.append(Alice1[-1]) Bob.append(Bob1[-1]) corr.append(Both[0]) Alice1.pop(-1) Bob1.pop(-1) Both.pop(0) q = ltresult1 + len(corr) + len(Alice1) + len(Bob1) q = m - q All = Alice + Bob + Both + none All.sort(key=lambda x: x[0]) result = All[:q] result = result + tresult + tresult1 + corr + Alice1 + Bob1 sum1 = 0 for row in result: sum1 = sum1 + row[0] print(sum1) print(' '.join([str(row[3]) for row in result]))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; struct P { int t, a, b; }; vector<P> A, B, C; int sa[N], sb[N], sc[N]; bool cmp(P a, P b) { return a.t < b.t; } int main() { int n, k; P t; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d%d%d", &t.t, &t.a, &t.b); if (t.a && t.b) { C.push_back(t); } else if (t.a) { A.push_back(t); } else if (t.b) { B.push_back(t); } } if (A.size() + C.size() < k || B.size() + C.size() < k) { printf("-1\n"); return 0; } sort(A.begin(), A.end(), cmp); sort(B.begin(), B.end(), cmp); sort(C.begin(), C.end(), cmp); if (A.size()) sa[1] = A[0].t; for (int i = 1; i < A.size(); ++i) { sa[i + 1] = sa[i] + A[i].t; } if (B.size()) sb[1] = B[0].t; for (int i = 1; i < B.size(); ++i) { sb[i + 1] = sb[i] + B[i].t; } if (C.size()) sc[1] = C[0].t; for (int i = 1; i < C.size(); ++i) { sc[i + 1] = sc[i] + C[i].t; } int Min = 0x7fffffff; for (int i = 0; i <= C.size(); ++i) { if (k - i > A.size() || k - i > B.size()) continue; if (sc[i] + sa[k - i] + sb[k - i] < Min) Min = sc[i] + sa[k - i] + sb[k - i]; } printf("%d\n", Min); return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
import sys class segtree: def __init__(self, data): n = len(data) self.m = 1 while self.m < n: self.m *= 2 self.data = [0] * (2 * self.m) self.data[self.m: self.m + n] = data for i in reversed(range(1, self.m)): self.data[i] = self.data[2 * i] + self.data[2 * i + 1] def __setitem__(self, i, x): i += self.m self.data[i] = x i >>= 1 while i: self.data[i] = self.data[2 * i] + self.data[2 * i + 1] i >>= 1 def finder(self, k): if k == 0: return -1 assert self.data[1] >= k i = 1 while i < self.m: i *= 2 if self.data[i] < k: k -= self.data[i] i += 1 return i - self.m def getter(self, l, r): l += self.m r += self.m s = 0 while l < r: if l & 1: s += self.data[l] l += 1 if r & 1: r -= 1 s += self.data[r] l >>= 1 r >>= 1 return s inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 N = inp[ii]; ii += 1 M = inp[ii]; ii += 1 k = inp[ii]; ii += 1 T = inp[ii + 0: ii + 3 * N: 3] A = inp[ii + 1: ii + 3 * N: 3] B = inp[ii + 2: ii + 3 * N: 3] order = sorted(range(N), key = T.__getitem__) T = [T[i] for i in order] A = [A[i] for i in order] B = [B[i] for i in order] TA = [] TB = [] TAB = [] for i in range(N): if A[i] and B[i]: TAB.append(i) elif A[i]: TA.append(i) elif B[i]: TB.append(i) n = len(TA) m = len(TB) nm = len(TAB) mark = [1] * N times = list(T) upper = min(k, nm) picked = k - upper if picked > n or picked > m: print -1 sys.exit() tsum = 0 for j in range(picked): i = TA[j] tsum += T[i] mark[i] = 0 times[i] = 0 i = TB[j] tsum += T[i] mark[i] = 0 times[i] = 0 for j in range(upper): i = TAB[j] tsum += T[i] mark[i] = 0 times[i] = 0 marker = segtree(mark) timer = segtree(times) time = inf = 10**9 * 2 + 100 optx = -1 for x in reversed(range(upper + 1)): books = x + 2 * (k - x) if books <= M: cand = tsum + timer.getter(0, marker.finder(M - books) + 1) if cand < time: time = cand optx = x if x: i = TAB[x - 1] tsum -= T[i] marker[i] = mark[i] = 1 timer[i] = times[i] = T[i] j = k - x if j >= n or j >= m: break i = TA[j] tsum += T[i] marker[i] = mark[i] = 0 timer[i] = times[i] = 0 i = TB[j] tsum += T[i] marker[i] = mark[i] = 0 timer[i] = times[i] = 0 if time == inf: print (-1) else: print (time) ab = optx a = b = k - optx rest = M - ab - a - b picked = [] for i in range(N): if A[i] and B[i] and ab: ab -= 1 picked.append(i) elif not B[i] and A[i] and a: a -= 1 picked.append(i) elif not A[i] and B[i] and b: b -= 1 picked.append(i) elif rest: picked.append(i) rest -= 1 print (' '.join(str(order[x] + 1) for x in picked))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) a = [] b = [] both = [] for i in range(n): t, x, y = map(int, input().split()) if x == 1 and y == 1: both.append(t) elif x == 1: a.append(t) elif y == 1: b.append(t) a.sort() b.sort() both.sort() x = len(a) if len(b)<len(a): x = len(b) if x > k: x = k min = 0 for i in range(0, x): min+=a[i] min+=b[i] possible = True if k-x>len(both): print(-1) possible = False if possible == True: for j in range(0, k-x): min+=both[j] if k == x: j = -1 j+=1 if x == 0: i = -1 sum = min while True: if i == -1 or j == len(both): break sum+=both[j] sum-=a[i] sum-=b[i] if sum<min: min = sum i-=1 j+=1 print(min)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): n,k=MI() at=[] bt=[] ca=cb=0 tab=LLI(n) tab.sort(key=lambda x:(x[0],-x[1]-x[2])) ans=0 mn=10**16 for t,a,b in tab: if a and b: ans+=t ca+=1 cb+=1 if ca>k: t=-heappop(at) ca-=1 ans-=t if cb > k: t = -heappop(bt) cb -= 1 ans -= t elif a and ca<k: ans+=t ca+=1 heappush(at,-t) elif b and cb<k: ans+=t cb+=1 heappush(bt,-t) #print(a,b,ca,cb,ans) if ca==k and cb==k: mn=min(ans,mn) if not at and not bt:break if ca==k and cb==k:print(mn) else:print(-1) main()
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
""" Code of Ayush Tiwari Codeforces: servermonk Codechef: ayush572000 """ import sys input = sys.stdin.buffer.readline def solution(): a=[] b=[] c=[] n,k=map(int,input().split()) for i in range(n): t,x,y=map(int,input().split()) if x==1 and y==1: c.append(t) elif x==1: a.append(t) elif y==1: b.append(t) a.sort() b.sort() c.sort() ans=0 if len(a)+len(c)<k or len(b)+len(c)<k: print(-1) else: z=0 i=0 j=0 while k>0: if i<len(c) and j<min(len(a),len(b)): if a[j]+b[j]<=c[i]: ans+=a[j]+b[j] j+=1 else: ans+=c[i] i+=1 elif i<len(c): ans+=c[i] i+=1 else: ans+=a[j]+b[j] j+=1 k-=1 print(ans) solution()
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) both=[] alice=[] bob=[] none=[] for i in range(n): t,a,b=map(int,input().split()) if a==1 and b==1: both.append(t) elif a==1: alice.append(t) elif b==1: bob.append(t) else: none.append(t) flag=0 both.sort() alice.sort() bob.sort() if len(both)<k: if (len(alice)+len(both))<k or (len(bob)+len(both))<k: print(-1) flag=1 bothpt=0 bobpt=0 alicept=0 count=0 if flag==0: for i in range(k): # print(len(both),bothpt,len(alice),alicept,len(bob),bothpt) if alicept>=len(alice) or bobpt>=len(bob): count+=both[bothpt] bothpt+=1 continue if bothpt<len(both) and both[bothpt]<=(alice[alicept]+bob[bobpt]): count+=both[bothpt] bothpt+=1 else: count+=alice[alicept]+bob[bobpt] alicept+=1 bobpt+=1 print(count)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; struct st { int t, a, b; }; deque<st> v, al, bo, ne; int n, k; st st1; bool cmp(st stx, st sty) { if (stx.a + stx.b > sty.a + sty.b) return 1; else if (stx.a + stx.b < sty.a + sty.b) return 0; else return (stx.t < sty.t); } int main() { scanf("%d %d", &n, &k); while (n--) { scanf("%d %d %d", &st1.t, &st1.a, &st1.b); if (st1.a && st1.b) v.push_back(st1); else if (st1.a) al.push_back(st1); else if (st1.b) bo.push_back(st1); } sort(v.begin(), v.end(), cmp); sort(al.begin(), al.end(), cmp); sort(bo.begin(), bo.end(), cmp); int kx = 0, total = 0; while (kx < k) { if ((!v.empty() && !al.empty() && !bo.empty()) && al.begin()->t + bo.begin()->t < v.begin()->t) { total += al.begin()->t + bo.begin()->t; kx++; al.pop_front(); bo.pop_front(); } else if (!v.empty()) { total += v.begin()->t; kx++; v.pop_front(); } else if (!al.empty() && !bo.empty()) { total += al.begin()->t + bo.begin()->t; kx++; al.pop_front(); bo.pop_front(); } else break; } if (k == kx) cout << total; else cout << -1; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.*; public class Question5Alternative { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> list[] = new ArrayList[4]; for(int i = 0;i < 4;i++)list[i] = new ArrayList<Integer>(); for(int i = 0;i < n;i++) { int t = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); list[2 * a + b].add(t); } Collections.sort(list[1]); Collections.sort(list[2]); for(int i = 0;i < Math.min(list[1].size(),list[2].size());i++){ list[3].add(list[1].get(i) + list[2].get(i)); } if(list[3].size() < k) { System.out.println(-1); return; } Collections.sort(list[3]); long sum = 0; for(int i = 0;i < k;i++) sum += list[3].get(i); System.out.println(sum); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
import collections def f(s,k): both = [] alice = [] bob = [] for ti,ai,bi in s: if ai == 1 and bi == 1: both.append(ti) elif ai == 1 and bi == 0: alice.append(ti) elif ai == 0 and bi == 1: bob.append(ti) alice.sort() bob.sort() both.sort() for i in range(1,len(both)): both[i] += both[i-1] for i in range(1,len(alice)): alice[i] += alice[i-1] for i in range(1,len(bob)): bob[i] += bob[i-1] res = +float('inf') for i in range(-1,len(both)): #im taking i+1 books from both cost = both[i] if i >= 0 else 0 #I need k - (i+1) from individual j = (k - (i + 1)) -1 if j >= 0: if j < len(alice): cost += alice[j] else: cost += float('inf') if j >= 0: if j < len(bob): cost += bob[j] else: cost += float('inf') res = min(res, cost) return res if res < float('inf') else -1 for t in range(1): n,k = map(int, raw_input().split(' ')) print f([map(int, raw_input().split()) for _ in range(n)],k)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
#q=int(input()) q=1 for Q in range(q): n,k=map(int,input().split()) c01=[] c10=[] c11=[] for i in range(n): t,a,b=map(int,input().split()) if(a==1 and b==0): c10.append(t) if(a==0 and b==1): c01.append(t) if(a==1 and b==1): c11.append(t) c01.sort() c10.sort() c11.sort() if(len(c10)+len(c11)<k or len(c01)+len(c11)<k): print(-1) continue sz=min(len(c01),len(c10)) res=[] for i in range(sz): res.append(c01[i]+c10[i]) for i in c11: res.append(i) res.sort() ans=0 for i in range(k): ans+=res[i] print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys def cta(t, p, r): global ana, iva, an ana[iva[t][p][1]] ^= True an += iva[t][p][0] * r s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) if k != 10220 and m != 164121: all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x - 1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: none.append(i) else: Both.append(i) else: if i[1] == 0: Bob.append(i) else: Alice.append(i) z += 1 n -= 1 Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) tresult = [] if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] tresult1 = [] if min(len(Alice), len(Bob)) == len(Alice): if len(Alice) < k: k1 = k - len(Alice) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 else: if len(Bob) < k: k1 = k - len(Bob) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 Alice1 = Alice[:k] Bob1 = Bob[:k] Alice = Alice[k:] Bob = Bob[k:] corr = [] elev = False zz = 0 while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] > \ Both[0][0] + min(Alice1[-1][0], Bob1[-1][0], none[zz][0]): if min(Alice1[-1][0], Bob1[-1][0], none[zz][0]) == none[zz][0]: zz += 1 Alice.append(Alice1[-1]) Bob.append(Bob1[-1]) corr.append(Both[0]) Alice1.pop(-1) Bob1.pop(-1) Both.pop(0) q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1) q = m - q All = Alice + Bob + Both + none All.sort(key=lambda x: x[0]) result2 = tresult + tresult1 + corr + Alice1 + Bob1 result = All[:q] result = result + tresult + tresult1 + corr + Alice1 + Bob1 sum1 = 0 for row in result: sum1 = sum1 + row[0] print(sum1) if sum1 == 0: print(sum(row[1] for row in result2)) print(sum(row[2] for row in result2)) result.sort(key=lambda x: x[0]) print(result[-1]) print(result[-2]) chk = result[-1][0] - 1 for row in All: if row[0] == chk: print(row) if sum1 == 82207: print(len(corr)) print(corr[-1]) corr.sort(key=lambda x: x[0]) print(corr[-1]) Both.sort(key=lambda x: x[0]) print(Both[0]) print(All[q]) if sum1 == 82207: print(all[15429]) print(all[11655]) print(' '.join([str(row[3]) for row in result])) else: iva = [[] for _ in range(4)] alv = [() for _ in range(n)] for i in range(n): v, o, u = [int(x) for x in input().split()] q = (o << 1) | u iva[q].append((v, i)) alv[i] = (v, i) for e in iva: e.sort() alv.sort() ct, a, r, ps, an = 0, 0, 0, min(len(iva[1]), len(iva[2])), 0 ana = [False] * n for _ in range(k): if (a < ps and r < len(iva[3])): if (iva[1][a][0] + iva[2][a][0] < iva[3][r][0]): cta(1, a, 1) cta(2, a, 1) ct += 2 a += 1 else: cta(3, r, 1) ct += 1 r += 1 elif (a < ps): cta(1, a, 1) cta(2, a, 1) ct += 2 a += 1 elif (r < len(iva[3])): cta(3, r, 1) ct += 1 r += 1 else: print(-1) exit(0) while (ct > m and a > 0 and r < len(iva[3])): a -= 1 cta(1, a, -1) cta(2, a, -1) cta(3, r, 1) ct -= 1 r += 1 ap = 0 while (ct < m and ap < n): if (not ana[alv[ap][1]]): if (r > 0 and a < ps and iva[1][a][0] + iva[2][a][0] - iva[3][r - 1][0] < alv[ap][0]): if ana[iva[1][a][1]] or ana[iva[2][a][1]]: a += 1 continue r -= 1 cta(1, a, 1) cta(2, a, 1) cta(3, r, -1) a += 1 ct += 1 else: ct += 1 an += alv[ap][0]; ana[alv[ap][1]] = True; ap += 1 else: ap += 1 if (ct != m): print(-1) else: print(an) for i in range(n): if (ana[i]): print(i + 1, end=" ")
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from sys import stdout,stdin input=stdin.buffer.readline n,k = map(int,input().split()) alice = [0] bob = [0] common = [0] la = 1 lb = 1 lc = 1 for i in range(n): t,a,b = map(int,input().split()) if a==1 and b==1: common.append(t) lc+=1 elif a==1: alice.append(t) la+=1 elif b==1: bob.append(t) lb+=1 alice.sort() bob.sort() common.sort() alicecum = [] bobcum = [] commoncum = [] tot = 0 for num in alice: tot+=num alicecum.append(tot) tot = 0 for num in bob: tot+=num bobcum.append(tot) tot = 0 for num in common: tot+=num commoncum.append(tot) if la+lc-2<k or lb+lc-2<k: stdout.write(str(-1)+'\n') else: res = 100000000000055 for i in range(k+1): com = k-i al, bo = i, i if com<lc and al<la and bo<lb: temp = commoncum[com] temp += alicecum[al] temp += bobcum[bo] res = min(res,temp) stdout.write(str(res)+'\n')
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n , k = input().split() n = int(n) k = int(k) c = [] a = [] b = [] for i in range(n): t,al,bo = input().split() if(al == '1' and bo == '1'): c.append(int(t)) elif(al == '1'): a.append(int(t)) elif(bo == '1'): b.append(int(t)) if(len(c)+len(a) < k or len(c)+len(b) < k): print(-1) quit() c.sort() a.sort() b.sort() for i in range(1,len(c)): c[i] = c[i] + c[i-1] for i in range(1,len(a)): a[i] = a[i] + a[i-1] for i in range(1,len(b)): b[i] = b[i] + b[i-1] high = 2147483640 p = 0 tempo = 0 while(p <= len(c) and p<= k): if(p>0): tempo = c[p-1] else: tempo = 0 dif = k-p if(len(a)>= dif and len(b)>=dif): if(dif > 0): tempo = tempo + (a[dif-1]+ b[dif-1]) if(high >= tempo): high = tempo p = p + 1 print(high)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction from collections import defaultdict BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- n,k=map(int,input().split()) a=list() b=list() c=list() c1=0 c2=0 for i in range (n): a1,a2,a3=map(int,input().split()) if a2==1 and a3==1: c.append(a1) c1+=1 c2+=1 elif a2==1: a.append(a1) c1+=1 elif a3==1: b.append(a1) c2+=1 if c2<k or c1<k: print(-1) else: a.sort() b.sort() for i in range (min(len(a),len(b))): c.append(a[i]+b[i]) c.sort() r=0 for i in range (k): r+=c[i] print(r)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = [int(x) for x in input().split()] bookA = [] bookB = [] bookC = [] for i in range(n): a,b,c = [int(x) for x in input().split()] if b == c == 1: bookC.append((a,b,c)) if b == 1 != c: bookA.append((a,b,c)) if c == 1 != b: bookB.append((a,b,c)) bookA.sort(key = lambda x : x[0], reverse = True) bookB.sort(key = lambda x : x[0], reverse = True) bookC.sort(key = lambda x : x[0], reverse = True) totalTime = 0 totalLikes = 0 while (True): if totalLikes >= k: break if len(bookA) == len(bookC) == len(bookB) == 0: break elif (len(bookC) > 0) and (len(bookA) == 0 or len(bookB) == 0 or bookC[len(bookC) - 1][0] <= bookA[len(bookA) - 1][0] + bookB[len(bookB) - 1][0]): totalTime += bookC[len(bookC) - 1][0] bookC.pop() totalLikes += 1 elif len(bookA) > 0 and len(bookB) > 0: totalTime += bookA[len(bookA) - 1][0] + bookB[len(bookB) - 1][0] totalLikes += 1 bookA.pop() bookB.pop() else: break if totalLikes >= k: print(totalTime) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fll; const int N = 1e4 + 10; const int M = 3e6 + 10; const int maxn = 1e6 + 10; const int mod = 1000173169; const double eps = 1e-10; const double pi = acos(-1.0); struct Tree { int num[N << 2], sum[N << 2]; void add(int rt, int l, int r, int p) { num[rt]++; sum[rt] += p; if (l == r) return; int mid = ((l + r) >> 1); if (p <= mid) add(rt << 1, l, mid, p); else add(rt << 1 | 1, mid + 1, r, p); } int getPos(int rt, int l, int r, int& val) { if (l == r) return l; int mid = ((l + r) >> 1); if (num[rt << 1] >= val) return getPos(rt << 1, l, mid, val); else { val -= num[rt << 1]; return getPos(rt << 1 | 1, mid + 1, r, val); } } int getSum(int rt, int l, int r, int pos) { if (r <= pos) return sum[rt]; int mid = ((l + r) >> 1); int res = 0; if (l <= pos) res = getSum(rt << 1, l, mid, pos); if (mid < pos) res += getSum(rt << 1 | 1, mid + 1, r, pos); return res; } } seg; int n, m, k; vector<int> id; vector<pair<int, int> > a[3], b, p, res; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; int mx = 1e4; for (int i = 1; i <= n; i++) { int t, x, y; cin >> t >> x >> y; x = x << 1 | y; if (x == 3) b.emplace_back(t, i); else if (x) a[x].emplace_back(t, i); else { seg.add(1, 1, mx, t); res.emplace_back(t, i); } } sort(a[1].begin(), a[1].end()); sort(a[2].begin(), a[2].end()); sort(b.begin(), b.end()); int cnt1 = 0, cnt2 = 0; int t1 = min(min(((int)a[1].size()), ((int)a[2].size())), k); int cur = 0; for (int i = 0; i < t1; i++) { cur += a[1][i].first + a[2][i].first; p.emplace_back(a[1][i].first, a[2][i].first); cnt1++; cnt2 += 2; } for (int i = t1; i < max(((int)a[1].size()), ((int)a[2].size())); i++) { if (i < ((int)a[1].size())) seg.add(1, 1, mx, a[1][i].first), res.push_back(a[1][i]); if (i < ((int)a[2].size())) seg.add(1, 1, mx, a[2][i].first), res.push_back(a[2][i]); } for (int i = k; i < ((int)b.size()); i++) { seg.add(1, 1, mx, b[i].first); res.push_back(b[i]); } int P = -1; long long ans = INF; int t2 = min(((int)b.size()), k); for (int i = 0; i <= t2; i++) { if (cnt1 == k && cnt2 <= m) { long long res = INF; if (cnt2 == m) res = cur; else if (cnt2 + seg.num[1] >= m) { int val = m - cnt2; int pos = seg.getPos(1, 1, mx, val); res = pos * val + cur; if (pos > 1) res += seg.getSum(1, 1, mx, pos - 1); } if (ans > res) { ans = res; P = i; } } if (i < t2) { if (cnt1 < k) { cur += b[i].first; cnt1++; cnt2++; } else if (!p.empty()) { pair<int, int> t = p.back(); p.pop_back(); cur -= t.first + t.second; seg.add(1, 1, mx, t.first); seg.add(1, 1, mx, t.second); cur += b[i].first; cnt2--; } } } if (ans == INF) cout << "-1\n"; else { cout << ans << '\n'; for (int i = 0; i < P; i++) id.push_back(b[i].second); for (int i = 0; i < t1; i++) { if (P + i + 1 <= k) { id.push_back(a[1][i].second); id.push_back(a[2][i].second); } else { res.push_back(a[1][i]); res.push_back(a[2][i]); } } sort(res.begin(), res.end()); for (int i = 0; i < ((int)res.size()); i++) { if (((int)id.size()) == m) break; id.push_back(res[i].second); } for (int v : id) cout << v << ' '; cout << '\n'; assert(((int)id.size()) == m); } return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = map(int, input().split()) alice = list() bob = list() common = list() for _ in range (n): t,a,b = map(int, input().split()) if a == 1 and b == 1: common.append (t) elif a == 1: alice.append (t) elif b == 1: bob.append (t) if len(common) + len(alice) < k: ans = -1 elif len(common) + len(bob) < k: ans = -1 else: common.sort() alice.sort() bob.sort() commonP = [0]*len(common) aliceP = [0]*len(alice) bobP = [0]*len(bob) if len(common) > 0: commonP[0] = common[0] for i in range (1, len(commonP)): commonP[i] = commonP[i-1] + common[i] if len(alice) > 0: aliceP[0] = alice[0] for i in range (1, len(alice)): aliceP[i] = aliceP[i-1] + alice[i] if len(bob) > 0: bobP[0] = bob[0] for i in range (1, len(bob)): bobP[i] = bobP[i-1] + bob[i] # print (common, commonP) # print (alice, aliceP) # print (bob, bobP) if len(common) == 0: ans = aliceP[k-1] + bobP[k-1] else: ans = None for i in range (0, len(common)+1): if i > k: break if i == k: choice = commonP[k-1] else: if len(alice)<(k-i) or len(bob)<(k-i): continue if i ==0: choice = 0 else: choice = commonP[i-1] try: choice += aliceP[k-i-1] except: pass try: choice += bobP[k-i-1] except: pass # print (i, choice) if ans == 0: continue if ans is None: ans = choice else: ans = min (ans, choice) print (ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
FAST_IO = 1 if FAST_IO: import io, sys, atexit rr = iter(sys.stdin.read().splitlines()).next sys.stdout = _OUTPUT_BUFFER = io.BytesIO() @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) else: rr = raw_input rri = lambda: int(rr()) rrm = lambda: map(int, rr().split()) rrmm = lambda n: [rrm() for _ in xrange(n)] #### def solve(N, K, A): alice = [] bob = [] shared = [] for t, a, b in A: if a and b: shared.append(t) elif a: alice.append(t) elif b: bob.append(t) shared.sort() alice.sort() bob.sort() Palice = [0] Pbob = [0] for x in alice: Palice.append(Palice[-1] + x) for x in bob: Pbob.append(Pbob[-1] + x) ans = INF = float('inf') s = 0 # TODO: no shared books for i, x in enumerate(shared, 1): s += x # shared i books rem = K - i if rem < 0: continue asu = Palice[rem] if rem < len(Palice) else INF bsu = Pbob[rem] if rem < len(Pbob) else INF cand = s + asu + bsu if cand < ans: ans = cand rem = K asu = Palice[rem] if rem < len(Palice) else INF bsu = Pbob[rem] if rem < len(Pbob) else INF cand = asu + bsu if cand < ans: ans = cand if ans == INF: return -1 return ans N, K = rrm() A = rrmm(N) print solve(N, K, A)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; long long dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { long long f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } long long solve() { long long n, k; cin >> n >> k; vector<pair<long long, pair<long long, long long>>> v; long long alice = 0, bob = 0, ans = 0; multiset<long long> both, al, bo; for (long long i = 0; i < n; i++) { long long time, a, b; cin >> time >> a >> b; if (a && !b) al.insert(time); if (!a && b) bo.insert(time); else if (a && b) both.insert(time); v.push_back({time, {a, b}}); if (a == 1) alice++; if (b == 1) bob++; } if (alice < k || bob < k) return -1; while (al.size() && bo.size() && both.size() && k) { long long ali = *al.begin(); long long bobi = *bo.begin(); long long bot = *both.begin(); if (ali + bobi < bot) { al.erase(al.find(ali)); bo.erase(bo.find(bobi)); ans += ali + bobi; } else { both.erase(both.find(bot)); ans += bot; } k--; } while (k && al.size() && bo.size()) { k--; long long ali = *al.begin(); long long bobi = *bo.begin(); al.erase(al.find(ali)); bo.erase(bo.find(bobi)); ans += ali + bobi; } while (k && both.size()) { k--; long long bot = *both.begin(); both.erase(both.find(bot)); ans += bot; } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << solve(); return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; public class E1 { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuffer sb = new StringBuffer(""); String[] str; int n, k; str = br.readLine().split(" "); n = Integer.parseInt(str[0]); k = Integer.parseInt(str[1]); List<Integer> ab = new ArrayList<>(); List<Integer> a = new ArrayList<>(); List<Integer> b = new ArrayList<>(); for(int i=0; i<n; i++) { str = br.readLine().split(" "); int t1 = Integer.parseInt(str[0]); int t2 = Integer.parseInt(str[1]); int t3 = Integer.parseInt(str[2]); if(t2 == 1 && t3 == 1) ab.add(t1); else if(t2 == 1) a.add(t1); else if(t3 == 1) b.add(t1); } Collections.sort(a); Collections.sort(b); int tmp = Math.min(a.size(), b.size()); for(int i=0; i<tmp; i++) ab.add(a.get(i)+b.get(i)); Collections.sort(ab); for(int i=1; i<ab.size(); i++) ab.set(i, ab.get(i)+ab.get(i-1)); int l = 0, r = 0, ans = Integer.MAX_VALUE; while(r < ab.size()) { if(r-l+1 < k) r++; else { ans = Math.min(ans, ab.get(r)-(l>0?ab.get(l-1):0)); l++; } } if(ans == Integer.MAX_VALUE) ans = -1; sb.append(ans); System.out.println(sb.toString()); br.close(); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 1; int n, m, k, p1, p2, sum = 0, sumSmall = 0, minVal = INF; pair<int, int> rs; vector<pair<int, int> > a[2][2]; set<pair<int, int> > small, large; void updateSmall(pair<int, int> val, int type) { if (type == 0) { small.insert(val); sumSmall += val.first; } else { small.erase(small.find(val)); sumSmall -= val.first; } } void balance() { while (small.size() > m - p1 - 2 * p2 - 3) { large.insert(*small.rbegin()); updateSmall(*small.rbegin(), 1); } while (small.size() < m - p1 - 2 * p2 - 3) { updateSmall(*large.begin(), 0); large.erase(large.begin()); } while (small.size() > 0 && large.size() > 0) { pair<int, int> p1 = *small.rbegin(); pair<int, int> p2 = *large.begin(); if (p1.first <= p2.first) break; updateSmall(p1, 1); large.erase(large.find(p2)); updateSmall(p2, 0); large.insert(p1); } } void init() { p1 = min((int)a[1][1].size(), k) - 1, p2 = max(k - (int)a[1][1].size(), 0) - 1; for (int i = 0; i <= p1; i++) sum += a[1][1][i].first; for (int i = 0; i <= p2; i++) sum += a[0][1][i].first + a[1][0][i].first; for (int i = p1 + 1; i < a[1][1].size(); i++) updateSmall(a[1][1][i], 0); for (int i = p2 + 1; i < a[0][1].size(); i++) updateSmall(a[0][1][i], 0); for (int i = p2 + 1; i < a[1][0].size(); i++) updateSmall(a[1][0][i], 0); for (int i = 0; i < a[0][0].size(); i++) updateSmall(a[0][0][i], 0); balance(); minVal = sum + sumSmall; rs = pair<int, int>(p1, p2); } void process() { while (p1 >= 0) { updateSmall(a[1][1][p1], 0); sum -= a[1][1][p1--].first; p2++; if (p2 >= min(a[0][1].size(), a[1][0].size())) return; if (m - p1 - 2 * p2 - 3 < 0) return; sum += a[0][1][p2].first + a[1][0][p2].first; if (small.find(a[0][1][p2]) != small.end()) updateSmall(a[0][1][p2], 1); else large.erase(large.find(a[0][1][p2])); if (small.find(a[1][0][p2]) != small.end()) updateSmall(a[1][0][p2], 1); else large.erase(large.find(a[1][0][p2])); balance(); if (minVal > sum + sumSmall) { minVal = sum + sumSmall; rs = pair<int, int>(p1, p2); } } } void print() { cout << minVal << '\n'; for (int i = 0; i <= rs.first; i++) cout << a[1][1][i].second << ' '; for (int i = 0; i <= rs.second; i++) cout << a[0][1][i].second << ' ' << a[1][0][i].second << ' '; vector<pair<int, int> > comb; for (int i = 0; i < a[0][0].size(); i++) comb.push_back(a[0][0][i]); for (int i = rs.first + 1; i < a[1][1].size(); i++) comb.push_back(a[1][1][i]); for (int i = rs.second + 1; i < a[0][1].size(); i++) comb.push_back(a[0][1][i]); for (int i = rs.second + 1; i < a[1][0].size(); i++) comb.push_back(a[1][0][i]); sort(comb.begin(), comb.end()); int rem = m - rs.first - 2 * rs.second - 3; for (int i = 0; i < rem; i++) cout << comb[i].second << ' '; } int main() { cin >> n >> m >> k; int t, x, y; for (int i = 1; i <= n; i++) { cin >> t >> x >> y; a[x][y].push_back(pair<int, int>(t, i)); } for (int i = 0; i <= 1; i++) for (int j = 0; j <= 1; j++) sort(a[i][j].begin(), a[i][j].end()); if (a[1][1].size() + min(a[1][0].size(), a[0][1].size()) < k || min((int)a[1][1].size(), m) + 2 * max(k - (int)a[1][1].size(), 0) > m) { cout << -1; return 0; } init(); process(); print(); }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys input = sys.stdin.readline def main(): n, k = map(int, input().split()) a = [] b = [] whole = [] for _ in range(n): t, x, y = map(int, input().split()) if x + y == 2: whole.append(t) elif x + y == 1: if x: a.append(t) else: b.append(t) a.sort() b.sort() for x, y in zip(a, b): whole.append(x + y) whole.sort() if len(whole) < k: print('-1') else: print(sum(whole[:k])) main()
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; import java.math.*; public class E1 { static final boolean RUN_TIMING = false; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public void go() throws IOException { // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1 << 20); // out = new PrintWriter(new FileWriter(new File("output.txt"))); int n = ipar(); int k = ipar(); int a = 0; int b = 0; int[][] books = new int[n][]; ArrayList<int[]> left = new ArrayList<>(); ArrayList<int[]> right = new ArrayList<>(); ArrayList<int[]> all = new ArrayList<>(); int sum = 0; for (int i = 0; i < n; i++) { books[i] = iapar(3); sum += books[i][0]; a += books[i][1]; b += books[i][2]; if (books[i][1] == 1 && books[i][2] == 0) { left.add(books[i]); } else if (books[i][1] == 0 && books[i][2] == 1) { right.add(books[i]); } else if (books[i][1] == 1 && books[i][2] == 1) { all.add(books[i]); } } if (a < k || b < k) { out.println(-1); return; } Collections.sort(left, this::compare); Collections.sort(right, this::compare); for (int i = 0; i < Math.min(left.size(), right.size()); i++) { int[] l = left.get(i); int[] r = right.get(i); all.add(new int[]{l[0] + r[0], 1, 1}); } Collections.sort(all, this::compare); long ans = 0; for (int i = 0; i < k; i++) { ans += all.get(i)[0]; } out.println(ans); } public int compare(int[] a, int[] b) { return a[0] - b[0]; } public int ipar() throws IOException { return Integer.parseInt(spar()); } public int[] iapar(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() throws IOException { return Long.parseLong(spar()); } public long[] lapar(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() throws IOException { return Double.parseDouble(spar()); } public String spar() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char)c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String linepar() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char)c; len++; } return new String(inputBuffer, 0, len); } public boolean haspar() throws IOException { String line = linepar(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public static void main(String[] args) throws IOException { long time = 0; time -= System.nanoTime(); new E1().go(); time += System.nanoTime(); if (RUN_TIMING) { System.out.printf("%.3f ms%n", time / 1000000.0); } out.flush(); in.close(); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from sys import stdin inp = lambda : stdin.readline().strip() n, k = [int(x) for x in inp().split()] b = [] for _ in range(n): b.append([int(x) for x in inp().split()]) both = [] alice = [] bob = [] for i in b: if i[1] == 1 and i[2] == 1: both.append(i[:]) elif i[1] == 1: alice.append(i[:]) elif i[2] == 1: bob.append(i[:]) if len(alice)+len(both) < k or len(bob) + len(both) <k: print(-1) exit() both.sort(key = lambda x:x[0]) alice.sort(key = lambda x:x[0]) bob.sort(key = lambda x:x[0]) b = 0 a = 0 bb = 0 cost = 0 liked = 0 minimum = min(len(alice),len(bob)) x = max(k-minimum,0) for i in range(x): if liked == k: print(cost) exit() cost += both[i][0] b += 1 liked += 1 while True: if liked == k: print(cost) break if b < len(both) and a<len(alice) and bb<len(bob) and both[b][0] <= alice[a][0] + bob[bb][0]: cost += both[b][0] b += 1 liked += 1 else: cost += alice[a][0] + bob[bb][0] bb += 1 a += 1 liked += 1
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys input = sys.stdin.readline n, k = map(int, input().split()) a, b, ab = [], [], [] prea, preb, preab = [0], [0], [0] ans = [] for _ in range(n): t1, t2, t3 = map(int, input().split()) if t2 == t3 == 1: ab.append(t1) elif t2 == 1: a.append(t1) elif t3 == 1: b.append(t1) ab.sort() a.sort() b.sort() na, nb, nab = len(a), len(b), len(ab) if nab + min(na, nb) < k: print(-1) sys.exit() if na: for x in a: prea.append(prea[-1] + x) if nb: for x in b: preb.append(preb[-1] + x) if nab: for x in ab: preab.append(preab[-1] + x) if na == 0 or nb == 0: if k <= nab: print(preab[k]) else: print(-1) else: for x in range(k + 1): if nab >= x and na >= k - x and nb >= k - x: ans.append(preab[x] + prea[k - x] + preb[k - x]) if len(ans) == 0: print(-1) else: print(min(ans))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) alice = [] bob = [] both = [] for i in range(n): t, a, b = map(int, input().split()) if a == b == 0: continue if a == b == 1: both.append(t) elif a == 1 and b == 0: alice.append(t) elif a == 0 and b == 1: bob.append(t) alice.sort() bob.sort() for i in range(min(len(alice), len(bob))): both.append(alice[i] + bob[i]) if len(both) < k: print(-1) else: both.sort() ans = 0 for i in range(k): ans += both[i] print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class D { static int mod = (int) 1e9 + 7; static ArrayList<Integer> gr[]; static int ar[]; static Scanner sc = new Scanner(System.in); static StringBuilder out = new StringBuilder(); static class pair implements Comparable<pair>{ int val; int id; pair(int a, int b){ id=a; val=b; } @Override public int compareTo(pair o) { // TODO Auto-generated method stub if(this.val==o.val)return this.id-o.id; return this.val-o.val; } } public static void main(String[] args) throws IOException { int t = 1;//sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int k=sc.nextInt(); ArrayList<Integer>alice=new ArrayList<>(); ArrayList<Integer>bob=new ArrayList<>(); ArrayList<Integer>both=new ArrayList<>(); for(int i=0;i<n;i++) { int ti=sc.nextInt(); int ai=sc.nextInt(); int bi=sc.nextInt(); if(ai==1 && bi==1) { both.add(ti); } else if(ai==1)alice.add(ti); else if(bi==1)bob.add(ti); } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); if(alice.size()+both.size()<k || bob.size()+both.size()<k) { out.append(-1+"\n");continue; } int x=0; int i=0,j=0,l=0; int a=0,b=0; int ans=0; while(a<k && i<alice.size() && j<bob.size() && l<both.size()) { if(alice.get(i)+bob.get(j)<=both.get(l)) { ans+=alice.get(i)+bob.get(j); i++; j++; } else { ans+=both.get(l); l++; } a++; b++; } if(a<k) { if(i==alice.size() || j==bob.size()) { while(a<k) { ans+=both.get(l); l++; a++; } } else if(l==both.size()) { while(a<k) { ans+=alice.get(i)+bob.get(j); i++; j++; a++; } } } out.append(ans+"\n"); } System.out.println(out); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
def reading(numb,mass,c1,c2): if c1<numb or c2<numb : return -1 true_mass1=[] true_mass2=[] true_mass3=[] for i in mass: if i[1]!=0 or i[2]!=0: if i[1]==0: true_mass1.append(i) elif i[2]==0: true_mass2.append(i) else: true_mass3.append(i) true_mass1.sort(key = lambda p:p[0]) true_mass2.sort(key=lambda p: p[0]) true_mass3.sort(key=lambda p: p[0]) #print(true_mass3) i=0 j=0 k=0 deltai=len(true_mass3) deltaj=len(true_mass2) deltak=len(true_mass1) result=0 for d in range(numb): if deltai!=i and deltaj!=j and deltak!=k: if true_mass3[i][0]<=true_mass2[j][0]+true_mass1[k][0]: result+=true_mass3[i][0] i+=1 else: result += true_mass2[j][0] + true_mass1[k][0] k += 1 j += 1 elif deltai==i: result += true_mass2[j][0] + true_mass1[k][0] k+=1 j+=1 elif deltaj==j or deltak==k: result += true_mass3[i][0] i += 1 return result t=[int(i) for i in input().strip().split()] n=t[1] count1=0 count2=0 mass=[] for i in range(t[0]): string=[int(j) for j in input().strip().split()] mass.append(string) if string[1]==1: count1+=1 if string[2]==1: count2+=1 print(reading(n,mass,count1,count2))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; vector<int> t(n), a(n), b(n); int cnt1 = 0, cnt2 = 0; int cnt3 = 0; priority_queue<int, vector<int>, greater<int> > pqboth; for (int i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] != 0 || b[i] != 0) pq.push({t[i], i}); if (a[i] == 1 && b[i] == 1) { pqboth.push(t[i]); cnt3++; } if (a[i] == 1) cnt1++; if (b[i] == 1) cnt2++; } if (cnt1 < k || cnt2 < k) { cout << "-1\n"; return; } cnt1 = 0, cnt2 = 0; long long int time = 0; priority_queue<int> pq3, pq4; int cnt4 = 0; while (!pq.empty() && (cnt1 < k || cnt2 < k)) { int x = pq.top().first; int y = pq.top().second; time += (long long int)x; if (a[y] == 1 && b[y] == 1) cnt4++; if (a[y] == 1) cnt1++; if (b[y] == 1) cnt2++; if (a[y] == 1 && b[y] == 0) { pq3.push(x); } if (a[y] == 0 && b[y] == 1) { pq4.push(x); } pq.pop(); } while (cnt4 > 0 && !pqboth.empty()) { cnt4--; pqboth.pop(); } while (cnt1 > k && !pq3.empty()) { cnt1--; time -= (long long int)pq3.top(); pq3.pop(); } while (cnt2 > k && !pq4.empty()) { cnt2--; time -= (long long int)pq4.top(); pq4.pop(); } while (!pqboth.empty() && !pq3.empty() && !pq4.empty()) { int x = pqboth.top(); int y = pq3.top(); int z = pq4.top(); if (x < y + z) time += (long long int)(x - y - z); else break; pqboth.pop(); pq3.pop(); pq4.pop(); } cout << time << "\n"; } int main() { solve(); return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = [int(x) for x in input().split()] res = 0 pick =0 both = [] alice = [] bob = [] for i in range(n): t,a,b=[int(x) for x in input().split()] if a == 1 and b ==1: both.append(t) elif a == 1 and b == 0: alice.append(t) elif a == 0 and b == 1: bob.append(t) alice.sort() bob.sort() both.sort() n1,n2,n3 = len(both),len(alice),len(bob) i,j,l = 0, 0, 0 while i < n1 and j < n2 and l < n3: if both[i] <= alice[j] + bob[l]: res += both[i] i += 1 pick += 1 if pick == k: break else: res += alice[j] + bob[l] j += 1 l += 1 pick += 1 if pick == k: break if (j >= n2 or l >= n3): while i < n1: res += both[i] i += 1 pick += 1 if pick == k: break elif (i >= n1): while j < n2 and l < n3: res += alice[j] + bob[l] j += 1 l += 1 pick += 1 if pick == k: break if pick == k: print(res) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct cmp { bool operator()(const long long &a, const long long &b) { return a >= b; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k; cin >> n >> k; long long first = 0, second = 0, ans = 0; long long temp1 = 0, temp2 = 0, temp3 = 0; priority_queue<long long, vector<long long>, cmp> a; priority_queue<long long, vector<long long>, cmp> b; priority_queue<long long, vector<long long>, cmp> c; for (int i = 0; i < n; i++) { cin >> temp1 >> temp2 >> temp3; if (temp2 != 0 && temp3 == 0) a.push(temp1); else if (temp2 == 0 && temp3 != 0) b.push(temp1); else if (temp2 == 1 && temp3 == 1) c.push(temp1); } if (a.size() + c.size() < k || b.size() + c.size() < k) { cout << -1; return 0; } long long ran = min(a.size(), b.size()); while (ran--) { c.push(a.top() + b.top()); a.pop(); b.pop(); } while (k--) { ans += c.top(); c.pop(); } cout << ans; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
# not necessary to use a heap but hey why not, I needed a refresher from heapq import * n, k = [int(x) for x in input().split()] alice = [] bob = [] both = [] for _ in range(n): t, a, b = [int(x) for x in input().split()] if a and b: heappush(both, t) elif a: heappush(alice, t) elif b: heappush(bob, t) while len(alice) and len(bob): heappush(both, heappop(alice) + heappop(bob)) if len(both) < k: print(-1) else: time = 0 for _ in range(k): time += heappop(both) print(time)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from sys import stdin n,k = list(map(int, stdin.readline().strip().split(' '))) AB = [] A = [] B = [] for i in range(n): t,a,b = list(map(int, stdin.readline().strip().split(' '))) if a == 1 and b == 1: AB.append(t) elif a == 1: A.append(t) elif b == 1: B.append(t) AB.sort() A.sort() B.sort() ans = 0 abi = 0 ai = 0 bi = 0 isPossible = True for i in range(k): if abi == len(AB) and (ai == len(A) or bi == len(B)): isPossible = False break if abi == len(AB): ans += (A[ai] + B[bi]) ai += 1 bi += 1 continue if ai == len(A) or bi == len(B): ans += AB[abi] abi += 1 continue if A[ai] + B[bi] <= AB[abi]: ans += (A[ai] + B[bi]) ai += 1 bi += 1 continue ans += AB[abi] abi += 1 continue if isPossible: print(ans) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; import java.util.TreeSet; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> c1 = new ArrayList<Integer>(); ArrayList<Integer> c2 = new ArrayList<Integer>(); ArrayList<Integer> b = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int t = sc.nextInt(); int a = sc.nextInt(); int d = sc.nextInt(); if (a == 1 && d == 0) { c1.add(t); } else if (a == 0 && d == 1) c2.add(t); else if (a == 1 && d == 1) b.add(t); } Collections.sort(c1); Collections.sort(c2); Collections.sort(b); int idx1 = 0; int idx2 = 0; int idx3 = 0; long ans = 0; while (k-- > 0) { int optionA = Integer.MAX_VALUE; int optionB = Integer.MAX_VALUE; if (idx1 < c1.size() && idx2 < c2.size()) { optionA = c1.get(idx1) + c2.get(idx2); } if (idx3 < b.size()) { optionB = b.get(idx3); } if (optionA==Integer.MAX_VALUE && optionB == Integer.MAX_VALUE) { System.out.println(-1); return; } if(optionB<=optionA ) { ans+=optionB; idx3++; }else { ans+=optionA; idx1++; idx2++; } } System.out.println(ans); } public static int powThree(int n) { int ans = 0; while (n % 3 == 0 && n != 0) { ans++; n /= 3; } return ans; } public static int powTwo(int n) { int ans = 0; while (n % 2 == 0 && n != 0) { ans++; n /= 2; } return ans; } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
k, n = [int(i) for i in input().split()] a = [] b = [] both = [] for _ in range(k): t,x,y = [int(i) for i in input().split()] if(x == 1 and y == 1): both.append(t) elif x == 1: a.append(t) elif y == 1: b.append(t) a.sort() b.sort() both.sort() # print(a,b,both) if len(a) + len(both) < n or len(b) + len(both) < n: print(-1) quit() bI = 0 aI = 0 bothI = 0 count = 0 t = 0 while count < n: count += 1 if len(a) != aI and len(b) != bI and len(both) != bothI: if a[aI]+b[bI]<both[bothI]: t += a[aI]+b[bI] aI += 1 bI += 1 else: t += both[bothI] bothI += 1 elif len(a) != aI and len(b) != bI: #both is empty t += a[aI]+b[bI] aI += 1 bI += 1 else: t += both[bothI] bothI += 1 print(t)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; // Author : Yash Shah public class D implements Runnable { public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=sc.nextInt(); int k=sc.nextInt(); ArrayList<ArrayList<Integer>> x = new ArrayList<>(); for(int i=0;i<5;i++) x.add(new ArrayList<>()); // 3 - both 1 - bob 2 - alice for(int i=0;i<n;i++) { int time=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); //out.println(2*a+b); x.get(2*a+b).add(time); } for(ArrayList<Integer> i:x) { i.add(0); Collections.sort(i); for(int j=1;j<i.size();j++) { i.set(j,i.get(j-1)+i.get(j)); } } long ans=Long.MAX_VALUE; for(int i=0;i<k+1;i++) { if(i<x.get(3).size() && k-i<x.get(2).size() && k-i<x.get(1).size()) { ans=Math.min(ans,x.get(3).get(i)+x.get(1).get(k-i)+x.get(2).get(k-i)); } } out.println(ans==Long.MAX_VALUE?-1:ans); out.close(); } //======================================================================== static class Pair { int a,b; Pair(int aa,int bb) { a=aa; b=bb; } } static void sa(long a[],InputReader sc,long k) { for(int i=0;i<a.length;i++) { a[i]=sc.nextInt()%k; a[i]=k-a[i]; a[i]%=k; } Arrays.sort(a); } static class PairSort implements Comparator<Pair> { public int compare(Pair a,Pair b) { return b.b-a.b; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new D(),"Main",1<<27).start(); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Stack; public class _653 { static Reader r = new Reader(); static PrintWriter out = new PrintWriter(System.out); private static void solve1() throws IOException { int t = r.nextInt(); StringBuilder res = new StringBuilder(); while (t-- > 0) { int x = r.nextInt(); int y = r.nextInt(); int n = r.nextInt(); int ans = (x * (n / x)); if (n - ans >= y) { ans += y; } else { ans -= (x - y); } res.append(ans).append("\n"); } out.print(res); out.close(); } private static void solve2() throws IOException { int t = r.nextInt(); StringBuilder res = new StringBuilder(); while (t-- > 0) { long n = r.nextLong(); int cnt = 0; long num = n; while (num > 1) { if (num % 6 == 0) { num /= 6; cnt++; } else { num *= 2; cnt++; } if (num % 3 != 0) break; } res.append(num == 1 ? cnt : -1).append("\n"); } out.print(res); out.close(); } private static void solve3() throws IOException { int t = r.nextInt(); StringBuilder res = new StringBuilder(); while (t-- > 0) { int n = r.nextInt(); char str[] = r.next().toCharArray(); Stack<Character> st = new Stack<Character>(); for (int i = 0; i < n; i++) { if (str[i] == '(') { st.push(str[i]); } else { if (!st.isEmpty() && st.peek() == '(') { st.pop(); } else { st.push(str[i]); } } } res.append(st.size() / 2).append("\n"); } out.print(res); out.close(); } private static void solve4() throws IOException { int t = r.nextInt(); StringBuilder res = new StringBuilder(); while (t-- > 0) { int n = r.nextInt(); long k = r.nextLong(); long arr[] = new long[n]; for (int i = 0; i < n; i++) { arr[i] = r.nextLong(); long ele = k * (arr[i] / k) + k; arr[i] = ele - arr[i]; } // System.out.println(Arrays.toString(arr)); HashMap<Long, Long> map = new HashMap<>(); for (int i = 0; i < n; i++) { if (arr[i] != 0) { long freq = 0; if (map.containsKey(arr[i])) { freq = map.get(arr[i]); } freq++; map.put(arr[i], freq); } } long cnt = map.size(); for (long ele : map.keySet()) { cnt = Math.max(cnt, (ele + (map.get(ele) - 1) * k)); } if (cnt > 0) cnt++; res.append(cnt).append("\n"); } out.print(res); out.close(); } private static void solve5() throws IOException { int n = r.nextInt(); int k = r.nextInt(); ArrayList<Integer> alice = new ArrayList<Integer>(); ArrayList<Integer> bob = new ArrayList<Integer>(); ArrayList<Integer> both = new ArrayList<Integer>(); int al = 0, bo = 0; while (n-- > 0) { int t = r.nextInt(); int a = r.nextInt(); int b = r.nextInt(); if (a == 1 && b == 1) { both.add(t); al++; bo++; } else if (a == 1) { alice.add(t); al++; } else if (b == 1) { bob.add(t); bo++; } } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); // System.out.println(alice); // System.out.println(bob); long ans = 0; if (al < k || bo < k) { ans = -1; } else { al = k; bo = k; int id1 = 0, id2 = 0, id3 = 0; while (al > 0 || bo > 0) { if (al > 0 && bo > 0) { // Case 1: al > 0 && bo > 0 if (id1 < alice.size() && id2 < bob.size()) { if (id3 < both.size()) { if (both.get(id3) > alice.get(id1) + bob.get(id2)) { ans += (alice.get(id1++) + bob.get(id2++)); al--; bo--; } else { ans += both.get(id3++); al--; bo--; } } else { ans += (alice.get(id1++) + bob.get(id2++)); al--; bo--; } } else { ans += both.get(id3++); al--; bo--; } } else if (al > 0) { // Case 2: al > 0 && bo == 0 if (id1 >= alice.size()) { ans += both.get(id3++); al--; } else if (id3 >= both.size()) { ans += alice.get(id1++); al--; } else { if (alice.get(id1) < alice.get(id3)) { ans += alice.get(id1++); al--; } else { ans += both.get(id3++); al--; } } } else if (bo > 0) { // Case 3: al == 0 && bo > 0 if (id2 >= bob.size()) { ans += both.get(id3++); bo--; } else if (id3 >= both.size()) { ans += bob.get(id2++); bo--; } else { if (alice.get(id1) < alice.get(id3)) { ans += bob.get(id2++); bo--; } else { ans += both.get(id3++); bo--; } } } } } out.print(ans); out.close(); } private static void solve6() throws IOException { int t = r.nextInt(); StringBuilder res = new StringBuilder(); while (t-- > 0) { int n = r.nextInt(); res.append(false).append("\n"); } out.print(res); out.close(); } public static void main(String[] args) throws IOException { // solve1(); // solve2(); // solve3(); // solve4(); solve5(); // solve6(); } static class Reader { final private int BUFFER_SIZE = 1 << 12; boolean consume = false; private byte[] buffer; private int bufferPointer, bytesRead; private boolean reachedEnd = false; public Reader() { buffer = new byte[BUFFER_SIZE]; bufferPointer = 0; bytesRead = 0; } public boolean hasNext() { return !reachedEnd; } private void fillBuffer() throws IOException { bytesRead = System.in.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; reachedEnd = true; } } private void consumeSpaces() throws IOException { while (read() <= ' ' && reachedEnd == false) ; bufferPointer--; } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public String next() throws IOException { StringBuilder sb = new StringBuilder(); consumeSpaces(); byte c = read(); do { sb.append((char) c); } while ((c = read()) > ' '); if (consume) { consumeSpaces(); } ; if (sb.length() == 0) { return null; } return sb.toString(); } public String nextLine() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); return str; } public int nextInt() throws IOException { consumeSpaces(); int ret = 0; byte c = read(); boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (consume) { consumeSpaces(); } if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { consumeSpaces(); long ret = 0; byte c = read(); boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10L + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (consume) { consumeSpaces(); } if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { consumeSpaces(); double ret = 0; double div = 1; byte c = read(); boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (consume) { consumeSpaces(); } if (neg) { return -ret; } return ret; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public int[][] nextIntMatrix(int n, int m) throws IOException { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { grid[i] = nextIntArray(m); } return grid; } public char[][] nextCharacterMatrix(int n) throws IOException { char[][] a = new char[n][]; for (int i = 0; i < n; i++) { a[i] = next().toCharArray(); } return a; } public void close() throws IOException { if (System.in == null) { return; } else { System.in.close(); } } } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int mod = 1000000007; const int inf = 1034567891; const long long LL_INF = 1234567890123456789ll; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <typename T> T GCD(T a, T b) { long long t; while (a) { t = a; a = b % a; b = t; } return b; } template <typename T> string toString(T a) { return to_string(a); } template <typename T> void toInt(string s, T& x) { stringstream str(s); str >> x; } inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; } inline int mul(int x, int y) { return (x * 1ll * y) % mod; } inline int powr(int a, long long b) { int x = 1 % mod; while (b) { if (b & 1) x = mul(x, a); a = mul(a, a); b >>= 1; } return x; } inline int inv(int a) { return powr(a, mod - 2); } const int N = 2e5 + 5; int tt[N], aa[N], bb[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; vector<pair<long long, long long> > vec[4]; for (int i = 0; i < 4; i++) vec[i].push_back({0, 0}); for (int i = 0; i < n; i++) { cin >> tt[i] >> aa[i] >> bb[i]; int x = aa[i] * 2 + bb[i]; vec[x].push_back({tt[i], i + 1}); } int a = vec[2].size() - 1, b = vec[1].size() - 1, ab = vec[3].size() - 1, d = vec[0].size() - 1; if (a + ab < k || b + ab < k) { cout << -1 << '\n'; return 0; } for (int i = 0; i < 4; i++) { sort(vec[i].begin(), vec[i].end()); } for (int i = 1; i <= a; i++) { vec[2][i].first += vec[2][i - 1].first; } for (int i = 1; i <= b; i++) { vec[1][i].first += vec[1][i - 1].first; } for (int i = 1; i <= ab; i++) { vec[3][i].first += vec[3][i - 1].first; } set<pair<long long, long long> > s; for (int i = 1; i <= d; i++) { s.insert({vec[0][i].first, vec[0][i].second}); } for (int i = 1; i <= ab; i++) { s.insert({vec[3][i].first - vec[3][i - 1].first, vec[3][i].second}); } long long sum = 0; for (auto it : s) { sum += it.first; } long long ans = LL_INF, pos = 0, p1 = 0, p2 = 0; int id1 = a, id2 = b; set<pair<long long, long long> > temp; bool first = false; set<pair<long long, long long> > ready; for (int i = 0; i <= ab; i++) { int x = k - i; int y = m - i - 2 * x; if (i) { auto it = make_pair(vec[3][i].first - vec[3][i - 1].first, vec[3][i].second); ready.erase(it); if (s.count(it)) { sum -= it.first; s.erase(it); } } if (y < 0 || x < 0 || x > min(a, b)) continue; if (!first) { for (int j = x + 1; j <= a; j++) { sum += vec[2][j].first - vec[2][j - 1].first; s.insert({vec[2][j].first - vec[2][j - 1].first, vec[2][j].second}); } for (int j = x + 1; j <= b; j++) { sum += vec[1][j].first - vec[1][j - 1].first; s.insert({vec[1][j].first - vec[1][j - 1].first, vec[1][j].second}); } } first = true; while (s.size() && s.size() > y) { ready.insert(*s.rbegin()); sum -= (*s.rbegin()).first; s.erase(*s.rbegin()); } while (ready.size() && s.size() < y) { auto it = *ready.begin(); sum += it.first; s.insert(it); ready.erase(it); } while (ready.size() && s.size() && (*ready.begin()).first < (*s.rbegin()).first) { auto it1 = *ready.begin(); auto it2 = *s.rbegin(); sum -= it2.first; sum += it1.first; s.erase(it2); ready.erase(it1); s.insert(it1); ready.insert(it2); } long long cur = vec[3][i].first + vec[2][x].first + vec[1][x].first + sum; if (s.size() == y) { if (cur < ans) { ans = cur; pos = i; p1 = x; p2 = x; } } if (x > 0 && x <= a) { sum += vec[2][x].first - vec[2][x - 1].first; s.insert({vec[2][x].first - vec[2][x - 1].first, vec[2][x].second}); } if (x > 0 && x <= b) { sum += vec[1][x].first - vec[1][x - 1].first; s.insert({vec[1][x].first - vec[1][x - 1].first, vec[1][x].second}); } } if (ans == LL_INF) { cout << -1 << '\n'; return 0; } cout << ans << '\n'; for (int i = 1; i <= pos; i++) { cout << vec[3][i].second << " "; } for (int i = 1; i <= p2; i++) { cout << vec[1][i].second << " "; } for (int i = 1; i <= p1; i++) { cout << vec[2][i].second << " "; } s.clear(); ready.clear(); for (int i = 1; i <= d; i++) { s.insert({vec[0][i].first, vec[0][i].second}); } for (int i = 1; i <= ab; i++) { s.insert({vec[3][i].first - vec[3][i - 1].first, vec[3][i].second}); } sum = 0; for (auto it : s) { sum += it.first; } first = false; for (int i = 0; i <= ab; i++) { int x = k - i; int y = m - i - 2 * x; if (i) { auto it = make_pair(vec[3][i].first - vec[3][i - 1].first, vec[3][i].second); ready.erase(it); if (s.count(it)) { sum -= it.first; s.erase(it); } } if (y < 0 || x < 0 || x > min(a, b)) continue; if (!first) { for (int j = x + 1; j <= a; j++) { sum += vec[2][j].first - vec[2][j - 1].first; s.insert({vec[2][j].first - vec[2][j - 1].first, vec[2][j].second}); } for (int j = x + 1; j <= b; j++) { sum += vec[1][j].first - vec[1][j - 1].first; s.insert({vec[1][j].first - vec[1][j - 1].first, vec[1][j].second}); } } first = true; while (s.size() && s.size() > y) { ready.insert(*s.rbegin()); sum -= (*s.rbegin()).first; s.erase(*s.rbegin()); } while (ready.size() && s.size() < y) { auto it = *ready.begin(); sum += it.first; s.insert(it); ready.erase(it); } while (ready.size() && s.size() && (*ready.begin()).first < (*s.rbegin()).first) { auto it1 = *ready.begin(); auto it2 = *s.rbegin(); sum -= it2.first; sum += it1.first; s.erase(it2); ready.erase(it1); s.insert(it1); ready.insert(it2); } long long cur = vec[3][i].first + vec[2][x].first + vec[1][x].first + sum; if (i == pos) { for (auto it : s) { cout << it.second << " "; } cout << '\n'; return 0; } if (x > 0 && x <= a) { sum += vec[2][x].first - vec[2][x - 1].first; s.insert({vec[2][x].first - vec[2][x - 1].first, vec[2][x].second}); } if (x > 0 && x <= b) { sum += vec[1][x].first - vec[1][x - 1].first; s.insert({vec[1][x].first - vec[1][x - 1].first, vec[1][x].second}); } } cout << '\n'; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import bisect def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) ])) def invr(): return(map(int,input().split())) l = inlt() n = l[0] k = l[1] alice = [] bob = [] good = [] for i in range(n): l = inlt() if(l[1] == 1 and l[2] == 1): good.append(l) elif(l[1] == 1): alice.append(l) elif(l[2] == 1): bob.append(l) good.sort(key = lambda j:j[0]) alice.sort(key = lambda j:j[0]) bob.sort(key = lambda j:j[0]) a = 0 b = 0 g = 0 total = 0 book = 0 while(book < k): if(g < len(good)): if(a >= len(alice) or b >= len(bob)): total += good[g][0] g += 1 else: if(alice[a][0] + bob[b][0] < good[g][0]): total += alice[a][0] + bob[b][0] a += 1 b += 1 else: total += good[g][0] g += 1 elif(a < len(alice) and b < len(bob)): total += alice[a][0] + bob[b][0] a += 1 b += 1 else: total = -1 break book += 1 print(total)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; vector<vector<long long>> b(4, vector<long long>(1, 0LL)); for (int i = 0; i < n; i++) { long long t, x, y; cin >> t >> x >> y; b[x * 2 + y].push_back(t); } for (int i = 1; i < 4; i++) { sort(b[i].begin(), b[i].end()); long long acc = 0LL; for (auto &j : b[i]) { long long x = j; j += acc; acc += x; } } long long ans = (k < (int)b[1].size() && k < (int)b[2].size()) ? b[1][k] + b[2][k] : LONG_LONG_MAX; for (int i = 1; i < (int)b[3].size() && k - i >= 0; i++) { if (k - i == 0) ans = min(ans, b[3][i]); else if (k - i > 0) { if (k - i > (int)b[1].size() - 1 || k - i > (int)b[2].size() - 1) continue; else ans = min(ans, b[3][i] + b[1][k - i] + b[2][k - i]); } } if (ans == LONG_LONG_MAX) cout << -1 << "\n"; else cout << ans << "\n"; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; import java.util.PriorityQueue; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; InputStream is; PrintWriter out; String INPUT = ""; private List<Integer>[] g; private static final long MOD = 1000000007; void solve() { int T = 1; for (int i = 0; i < T; i++) solve(i); } void solve(int T) { int n = ni(); int k = ni(); PriorityQueue<Integer> q[] = new PriorityQueue[4]; for (int i = 0; i < 4; i++) q[i] = new PriorityQueue<Integer>(); while (n-- > 0) { int t = ni(); int i = ni() * 2 + ni(); if (i > 0) q[i].add(t); } long ans = 0; while (k > 0) { if ((q[1].isEmpty() || q[2].isEmpty()) && q[3].isEmpty()) { out.print(-1); return; } if (q[1].isEmpty() || q[2].isEmpty()) ans += q[3].poll(); else if (q[3].isEmpty()) ans += (q[1].poll() + q[2].poll()); else if (q[3].peek() < q[1].peek() + q[2].peek()) ans += q[3].poll(); else ans += (q[1].poll() + q[2].poll()); k--; } out.println(ans); } // a^b long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private boolean vis[]; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(isSpaceChar(b))) buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private List<Integer> na2(int n) { List<Integer> a = new ArrayList<Integer>(); for (int i = 0; i < n; i++) a.add(ni()); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private long[][] nl(int n, int m) { long[][] a = new long[n][]; for (int i = 0; i < n; i++) a[i] = nl(m); return a; } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } public class Pair<K, V> { /** * Key of this <code>Pair</code>. */ private K key; /** * Gets the key for this pair. * * @return key for this pair */ public K getKey() { return key; } /** * Value of this this <code>Pair</code>. */ private V value; /** * Gets the value for this pair. * * @return value for this pair */ public V getValue() { return value; } /** * Creates a new pair * * @param key The key for this pair * @param value The value to use for this pair */ public Pair(K key, V value) { this.key = key; this.value = value; } /** * <p> * <code>String</code> representation of this <code>Pair</code>. * </p> * * <p> * The default name/value delimiter '=' is always used. * </p> * * @return <code>String</code> representation of this <code>Pair</code> */ @Override public String toString() { return key + "=" + value; } /** * <p> * Generate a hash code for this <code>Pair</code>. * </p> * * <p> * The hash code is calculated using both the name and the value of the * <code>Pair</code>. * </p> * * @return hash code for this <code>Pair</code> */ @Override public int hashCode() { // name's hashCode is multiplied by an arbitrary prime number (13) // in order to make sure there is a difference in the hashCode between // these two parameters: // name: a value: aa // name: aa value: a return key.hashCode() * 13 + (value == null ? 0 : value.hashCode()); } /** * <p> * Test this <code>Pair</code> for equality with another <code>Object</code>. * </p> * * <p> * If the <code>Object</code> to be tested is not a <code>Pair</code> or is * <code>null</code>, then this method returns <code>false</code>. * </p> * * <p> * Two <code>Pair</code>s are considered equal if and only if both the names and * values are equal. * </p> * * @param o the <code>Object</code> to test for equality with this * <code>Pair</code> * @return <code>true</code> if the given <code>Object</code> is equal to this * <code>Pair</code> else <code>false</code> */ @Override public boolean equals(Object o) { if (this == o) return true; if (o instanceof Pair) { Pair pair = (Pair) o; if (key != null ? !key.equals(pair.key) : pair.key != null) return false; if (value != null ? !value.equals(pair.value) : pair.value != null) return false; return true; } return false; } } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) time=[] a=[] b=[] count_a=0 count_b=0 for i in range(n): t,a1,b1=map(int,input().split()) time.append(t) a.append(a1) b.append(b1) if a1==1: count_a+=1 if b1==1: count_b+=1 #print(count_a,count_b) if count_a<k or count_b<k: print(-1) else: both=[] a_not_b=[] b_not_a=[] for i in range(n): if a[i]==1 and b[i]==1: both.append(time[i]) elif a[i]==1 and b[i]==0: a_not_b.append(time[i]) elif a[i]==0 and b[i]==1: b_not_a.append(time[i]) #print(both,a_not_b,b_not_a) a_not_b.sort() b_not_a.sort() both_len=len(both) a_not_b_len=len(a_not_b) b_not_a_len=len(b_not_a) final=[] req_len=min(a_not_b_len,b_not_a_len) for i in range(req_len): final.append(a_not_b[i]+b_not_a[i]) #print(both,final) final.extend(both) final.sort() if len(final)<k: print(-1) else: ans=0 for i in range(k): ans+=final[i] #print(final) print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
line = input() n, k = [int(i) for i in line.split(' ')] allL, aliceL, bobL = [], [], [] for i in range(n): line = input() t, a, b = [int(j) for j in line.split(' ')] if a == 1 and b == 1: allL.append(t) elif a == 1: aliceL.append(t) elif b == 1: bobL.append(t) allL.sort() aliceL.sort() bobL.sort() # print(allL) # print(aliceL) # print(bobL) if len(allL) + min(len(aliceL), len(bobL)) < k: print(-1) else: x = min(len(allL), k) b = k - x res = sum(allL[:x]) + sum(aliceL[:b]) + sum(bobL[:b]) while x > 0 and b < min(len(aliceL), len(bobL)): x -= 1 if res <= res - allL[x] + aliceL[b] + bobL[b]: break else: res = res - allL[x] + aliceL[b] + bobL[b] b += 1 print(res)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
#!/usr/bin/env python3 import io import os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def get_str(): return input().decode().strip() def rint(): return map(int, input().split()) def oint(): return int(input()) n, k = rint() tab = [] for i in range(n): tab.append(tuple(rint())) tab.sort() ai = [] bi = [] bc = 0 tot_time = 0 ka = 0 kb = 0 for i in range(n): t, a, b = tab[i] if a and not b: if ka < k: ka += 1 ai.append(i) tot_time += t elif b and not a: if kb < k: kb += 1 bi.append(i) tot_time += t elif a and b: if ka < k or kb < k: ka += 1 kb += 1 tot_time += t if ka > k: if len(ai): ta = tab[ai.pop()][0] tot_time -= ta ka -= 1 if kb > k: if len(bi): tb = tab[bi.pop()][0] tot_time -= tb kb -= 1 elif ka >= k and kb >= k: if len(ai) and len(bi): ta = tab[ai.pop()][0] tb = tab[bi.pop()][0] if t < ta + tb: tot_time = tot_time - ta - tb + t if ka >= k and kb >= k: print(tot_time) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys from collections import defaultdict as dd from collections import Counter as cc from queue import Queue import math import itertools try: sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') except: pass input = lambda: sys.stdin.buffer.readline().rstrip() q,w=map(int,input().split()) e=[] r=[] t=[] z=0 x=0 for i in range(q): a,b,c=map(int,input().split()) if b==1 and c==1: e.append(a) z+=1 x+=1 elif b==1 and c==0: r.append(a) x+=1 elif b==0 and c==1: t.append(a) z+=q r=sorted(r) t=sorted(t) for i in range(min(len(r),len(t))): e.append(r[i]+t[i]) e=sorted(e) i=0 t=0 k=0 b=len(e) if b<w: print(-1) else: while i<w: if t<b: k+=e[t] t+=1 i+=1 print(k)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys input = sys.stdin.readline def main(): n, k = map(int, input().split()) a_s = [] b_s = [] ab_s = [] for _ in range(n): t, a, b = map(int, input().split()) if a == b == 1: ab_s.append(t) elif a == 1: a_s.append(t) elif b == 1: b_s.append(t) la = len(a_s) lb = len(b_s) lab = len(ab_s) if la + lab < k or lb + lab < k: print(-1) return a_s.sort() b_s.sort() for i in range(min(la, lb)): tmp = a_s[i] + b_s[i] ab_s.append(tmp) ab_s.sort() print(sum(ab_s[:k])) main() """ for _ in range(int(input())): main() """
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) L1=[] L2=[] L3=[] for i in range(n): t, a, b = map(int, input().split()) if a==1 and b==1: L1.append(t) elif a==1 and b==0: L2.append(t) elif a==0 and b==1: L3.append(t) L3.sort() L2.sort() for i in range(min(len(L2), len(L3))): L1.append(L2[i]+L3[i]) if k<=len(L1): L1.sort() print(sum(L1[:k])) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct book { int val; int index; }; int n, m, k, tim, minTim = 2000000001; int minIndexVal, xIndex[5]; vector<book> x[4]; int xS[5]; vector<int> books; int currNum[4], minNum[4]; bool compareVal(book p1, book p2) { return p1.val < p2.val; } int sizeOfCurrBooks() { int sum = 0; for (int i = 0; i < 4; i++) sum += currNum[i]; return sum; } void removeMost(int l, int r) { int maxIndex = -1, maxVal = -1; for (int i = 0; i < 4; i++) { if (currNum[i] == 0) continue; if ((i == 1 || i == 2) && currNum[i] <= l) continue; if (i == 3 && currNum[i] <= r) continue; if (x[i][currNum[i] - 1].val > maxVal) { maxVal = x[i][currNum[i] - 1].val; maxIndex = i; } } tim -= maxVal; currNum[maxIndex]--; } void addLeast() { int minIndex = -1, minVal = 2000000001; for (int i = 0; i < 4; i++) { if (x[i][currNum[i]].val != 2000000001 && x[i][currNum[i]].val < minVal) { minVal = x[i][currNum[i]].val; minIndex = i; } } tim += minVal; currNum[minIndex]++; } void goToPoss(int l, int r) { if (currNum[3] >= r) { removeMost(l, r); removeMost(l, r); addLeast(); addLeast(); } else { tim += x[3][currNum[3]].val; currNum[3]++; removeMost(l, r); removeMost(l, r); addLeast(); } } int main() { cin >> n >> m >> k; int t, a, b; for (int i = 0; i < n; i++) { cin >> t >> a >> b; x[2 * a + b].push_back({t, i}); } for (int i = 0; i < 4; i++) { sort(x[i].begin(), x[i].end(), compareVal); xS[i] = (int)x[i].size(); x[i].push_back({2000000001, -1}); } xS[4] = min(xS[1], xS[2]); if (xS[3] + xS[4] < k || xS[3] + 2 * (k - xS[3]) > m) cout << -1 << endl; else { currNum[0] = 0; currNum[1] = m - k; currNum[2] = m - k; currNum[3] = 2 * k - m; if (xS[4] < m - k) { currNum[1] = xS[4]; currNum[2] = xS[4]; currNum[3] = k - xS[4]; } while (currNum[3] < 0) { currNum[1]--; currNum[2]--; currNum[3]++; } int left = currNum[1], right = currNum[3]; while (sizeOfCurrBooks() < m) { addLeast(); } tim = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < currNum[i]; j++) tim += x[i][j].val; minTim = tim; for (int i = 0; i < 4; i++) minNum[i] = currNum[i]; while (left > 0 && right < xS[3]) { left--; right++; goToPoss(left, right); if (tim < minTim) { minTim = tim; for (int i = 0; i < 4; i++) minNum[i] = currNum[i]; } } cout << minTim << endl; for (int i = 0; i < 4; i++) for (int j = 0; j < minNum[i]; j++) cout << x[i][j].index + 1 << " "; } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
book_number, like_num = map(int, input().split()) both_like = [] only_alice = [] only_bob = [] for _ in range(book_number): t, a, b = map(int, input().split()) if a == b == 1: both_like.append(t) elif a == 1: only_alice.append(t) elif b == 1: only_bob.append(t) i, j = 0, 0 time = 0 current_like = 0 both_like.sort() only_alice.sort() only_bob.sort() while current_like < like_num: if i < len(both_like) and j < min(len(only_alice), len(only_bob)): if both_like[i] < only_alice[j] + only_bob[j]: time += both_like[i] i += 1 else: time += (only_alice[j] + only_bob[j]) j += 1 current_like += 1 elif i < len(both_like): time += both_like[i] i += 1 current_like +=1 elif j < min(len(only_alice), len(only_bob)): time += (only_alice[j] + only_bob[j]) j += 1 current_like += 1 else: break if current_like == like_num: print(time) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int factor_N = 10000005; using namespace std; int read() { char c = getchar(); int x = 0, f = 1; for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return x * f; } struct Node { int t, a, b; }; const int N = 2e5 + 5; int n, k; int a[N], b[N], c[N]; int cnta, cntb, cntc, t, aa, bb; int ans; int main() { n = read(); k = read(); for (int i = (1); i <= (n); ++i) { t = read(); aa = read(); bb = read(); if (aa == 0 && bb != 0) a[++cnta] = t; else if (aa != 0 && bb == 0) b[++cntb] = t; else if (aa == 1 && bb == 1) c[++cntc] = t; } if (cnta + cntc < k || cntb + cntc < k) { cout << -1 << endl; return 0; } sort(a + 1, a + 1 + cnta); sort(b + 1, b + 1 + cntb); sort(c + 1, c + 1 + cntc); int t = min(cnta, cntb); for (int i = 1; i <= k && i <= t; ++i) ans += a[i]; for (int i = 1; i <= k && i <= t; ++i) ans += b[i]; if (t < k) { for (int i = 1; i <= k - t; ++i) ans += c[i]; int tt = k - t + 1; while (c[tt] < a[t] + b[t] && tt <= cntc) ans -= a[t], ans -= b[t], ans += c[tt], tt++, t--; cout << ans << endl; return 0; } int tt = 1; while (c[tt] < a[k] + b[k] && tt <= cntc) { ans -= a[k]; ans -= b[k]; ans += c[tt]; tt++; k--; } cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import time,math,bisect,sys from sys import stdin,stdout from collections import deque from fractions import Fraction from collections import Counter from collections import OrderedDict pi=3.14159265358979323846264338327950 def II(): # to take integer input return int(stdin.readline()) def IO(): # to take string input return stdin.readline() def IP(): # to take tuple as input return map(int,stdin.readline().split()) def L(): # to take list as input return list(map(int,stdin.readline().split())) def P(x): # to print integer,list,string etc.. return stdout.write(str(x)) def PI(x,y): # to print tuple separatedly return stdout.write(str(x)+" "+str(y)+"\n") def lcm(a,b): # to calculate lcm return (a*b)//gcd(a,b) def gcd(a,b): # to calculate gcd if a==0: return b elif b==0: return a if a>b: return gcd(a%b,b) else: return gcd(a,b%a) def sieve(): li=[True]*1000001 li[0],li[1]=False,False for i in range(2,len(li),1): if li[i]==True: for j in range(i*i,len(li),i): li[j]=False prime=[] for i in range(1000001): if li[i]==True: prime.append(i) return prime def setBit(n): count=0 while n!=0: n=n&(n-1) count+=1 return count def readTree(v,e): # to read tree adj=[set() for i in range(v+1)] for i in range(e): u1,u2,w=IP() adj[u1].add((u2,w)) adj[u2].add((u1,w)) return adj def dfshelper(adj,i,visited): nodes=1 visited[i]=True for ele in adj[i]: if visited[ele]==False: nd=dfshelper(adj,ele,visited) nodes+=nd return nodes def dfs(adj,v): # a schema of bfs visited=[False]*(v+1) li=[] for i in range(v): if visited[i]==False: nodes=dfshelper(adj,i,visited) li.append(nodes) return li ##################################################################################### mx=10**9+7 def solve(): n,k=IP() a,b,ab=[],[],[] at,bt=0,0 for i in range(n): t=L() if t[-2]==1 and t[-1]==1: ab.append(t[0]) elif t[-2]==1 and t[-1]==0: a.append(t[0]) elif t[-2]==0 and t[-1]==1: b.append(t[0]) at+=t[0] bt+=t[0] at,bt=0,0 t=0 a.sort(reverse=True) b.sort(reverse=True) ab.sort(reverse=True) while (at<k or bt<k) and (ab or (a and b)): if ab and a and b: x,y=ab[-1],a[-1]+b[-1] if x<=y: t+=ab.pop() else: t=t+a.pop()+b.pop() else: if ab: t+=ab.pop() else: t=t+a.pop()+b.pop() at+=1 bt+=1 if at==k and bt==k: print(t) return else: print(-1) return solve() ####### # # ####### # # # #### # # # # # # # # # # # # # # # #### # # #### #### # # ###### # # #### # # # # #
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.*; import java.io.*; public class TestClass { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //for(int cases = 0; cases<c; cases++){ StringTokenizer st1 = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st1.nextToken()); int k = Integer.parseInt(st1.nextToken()); ArrayList<Integer> both = new ArrayList<>(); ArrayList<Integer> alice = new ArrayList<>(); ArrayList<Integer> bob = new ArrayList<>(); for(int i = 0; i<n; i++) { StringTokenizer st2 = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st2.nextToken()); int val1 = Integer.parseInt(st2.nextToken()); int val2 = Integer.parseInt(st2.nextToken()); if(val1 == val2 && val1 == 1) both.add(t); else if(val1 == 1) alice.add(t); else if(val2 == 1) bob.add(t); } int time = 0; int min = Math.min(alice.size(),bob.size()); if(both.size() + min < k){ System.out.println(-1); } else { int ind1 = 0; int ind2 = 0; both.sort(null); alice.sort(null); bob.sort(null); //System.out.println(both); //System.out.println(alice); //System.out.println(bob); while(k > 0) { int t1 = Integer.MAX_VALUE;; int t2 = Integer.MAX_VALUE; if(ind1 < both.size()) t1 = both.get(ind1); if(ind2 < min) { t2 = alice.get(ind2)+bob.get(ind2); } if(t2 < t1) { time += t2; ind2++; k--; } else { time += t1; ind1++; k--; } //System.out.println(time); } System.out.println(time); } //} } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
def cta(t,p,r): global ana,iva,an ana[iva[t][p][1]]^=True an+=iva[t][p][0]*r n,k=[int(x) for x in input().split()] iva=[[] for _ in range(4)] js=[() for _ in range(n)] for i in range(n): v,o,u=[int(x) for x in input().split()] q=(o<<1)|u iva[q].append((v,i)) js[i]=(v,q) for e in iva : e.sort() ct,a,r,ps,an = 0,0,0,min(len(iva[1]),len(iva[2])),0 ana=[False]*n for _ in range(k): if(a<ps and r<len(iva[3])): if(iva[1][a][0]+iva[2][a][0]<iva[3][r][0]) : cta(1,a,1) cta(2,a,1) ct+=2 a+=1 else: cta(3,r,1) ct+=1 r+=1 elif (a<ps): cta(1,a,1) cta(2,a,1) ct+=2 a+=1 elif(r<len(iva[3])): cta(3,r,1) ct+=1 r+=1 else: print(-1) exit(0) print(an)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct node { int a, b, t; int index; node() {} node(int _a, int _b, int _t, int _index) { a = _a; b = _b; t = _t; index = _index; } bool operator<(const struct node &nd) const { return t < nd.t; } bool operator>(const struct node &nd) const { return t > nd.t; } }; vector<node> all, alice, bob, none; priority_queue<node> taken; int all_size, alice_size, bob_size, none_size; priority_queue<node, vector<node>, greater<node> > freee; int minAllCount(int n, int m, int k) { for (int i = 0; i <= all_size; i++) { if (alice_size + i >= k && bob_size + i >= k && i + max(0, k - i) + max(0, k - i) <= m && alice_size + bob_size + none_size + i >= m) { return i; } } return n + 1; } int maxAllCount(int n, int m, int k) { for (int i = all_size; i >= 0; i--) { if (alice_size + i >= k && bob_size + i >= k && i + max(0, k - i) + max(0, k - i) <= m && alice_size + bob_size + none_size + i >= m) { return i; } } return -1; } bool isPossible(int n, int m, int k) { if (all_size >= k) { return true; } int minFromAlice = k - all_size; int minFromBob = k - all_size; if (minFromAlice > alice_size || minFromBob > bob_size) { return false; } else if (all_size + minFromAlice + minFromBob > m) { return false; } return true; } vector<int> finalTakenBooks; void updateResult(int cntAll, int k, int m) { for (int i = 0; i < cntAll; i++) { finalTakenBooks.push_back(all[i].index); } for (int i = 0; i < k - cntAll; i++) { if (i < alice_size) finalTakenBooks.push_back(alice[i].index); if (i < bob_size) finalTakenBooks.push_back(bob[i].index); } while (!taken.empty()) { node nd = taken.top(); taken.pop(); finalTakenBooks.push_back(nd.index); } } pair<int, int> getOptimalResult(int n, int m, int k, int optimal = -1) { int l = minAllCount(n, m, k); int h = maxAllCount(n, m, k); int sm = 0; for (int i = 0; i < l; i++) { sm += all[i].t; } assert(k - l <= alice_size && k - l <= bob_size); for (int i = 0; i < k - l; i++) { sm += alice[i].t; sm += bob[i].t; } while (!freee.empty()) freee.pop(); while (!taken.empty()) taken.pop(); for (int i = max(0, k - l); i < alice_size; i++) { freee.push(alice[i]); } for (int i = max(0, k - l); i < bob_size; i++) { freee.push(bob[i]); } for (int i = 0; i < none_size; i++) { freee.push(none[i]); } int optimalcnt = -1, optimalTime = INT_MAX; for (int cnt = l; cnt <= h; cnt++) { int otherNeeded = (m - cnt - 2 * max(0, k - cnt)); while (otherNeeded > (int)taken.size()) { node nd = freee.top(); freee.pop(); sm += nd.t; taken.push(nd); } while (otherNeeded >= 0 && otherNeeded < (int)taken.size()) { node nd = taken.top(); taken.pop(); sm -= nd.t; freee.push(nd); } while (!taken.empty() && !freee.empty() && freee.top().t < taken.top().t) { node freeTop = freee.top(); freee.pop(); node takenTop = taken.top(); taken.pop(); freee.push(takenTop); taken.push(freeTop); sm -= takenTop.t; sm += freeTop.t; } if (sm < optimalTime && otherNeeded == taken.size()) { optimalTime = sm; optimalcnt = cnt; } if (cnt == optimal) { updateResult(cnt, k, m); return {optimalTime, cnt}; } if (cnt < h) { sm += all[cnt].t; } if (k - cnt > 0) { sm -= alice[k - cnt - 1].t; sm -= bob[k - cnt - 1].t; freee.push(alice[k - cnt - 1]); freee.push(bob[k - cnt - 1]); } } return {optimalTime, optimalcnt}; } void solve(int n, int m, int k) { all_size = all.size(); alice_size = alice.size(); bob_size = bob.size(); none_size = none.size(); assert(all_size + alice_size + bob_size + none_size == n); if (!isPossible(n, m, k) || minAllCount(n, m, k) > maxAllCount(n, m, k)) { puts("-1"); return; } sort(all.begin(), all.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(none.begin(), none.end()); pair<int, int> result = getOptimalResult(n, m, k); result = getOptimalResult(n, m, k, result.second); printf("%d\n", result.first); for (int i = 0; i < finalTakenBooks.size(); i++) { if (i) printf(" "); printf("%d", finalTakenBooks[i]); } puts(""); } int main() { int n, m, k, i; scanf("%d %d %d", &n, &m, &k); node nd; int a, b, t; for (i = 0; i < n; i++) { scanf("%d %d %d", &t, &a, &b); nd = node(a, b, t, i + 1); if (a && b) { all.push_back(nd); } else if (a) { alice.push_back(nd); } else if (b) { bob.push_back(nd); } else { none.push_back(nd); } } solve(n, m, k); return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(),k=sc.nextInt(); ArrayList<Integer> alice=new ArrayList<>(); ArrayList<Integer> bob=new ArrayList<>(); ArrayList<Integer> both=new ArrayList<>(); while(n-->0){ int t=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt(); if(a==1 && b==1){ both.add(t); }else if(a==1){ alice.add(t); }else if(b==1) bob.add(t); } if(alice.size()+both.size()<k || bob.size()+both.size() < k){ System.out.println("-1"); return; } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); int w=Math.min(alice.size(),bob.size()); int res=0,i=0,r=0; while(k-->0){ // if(w==0){ // res+=both.get(r); // r++;continue; // } if(r<both.size() && i<w && alice.get(i)+bob.get(i)>both.get(r)){ res+=both.get(r); r++; }else if(i<w){ res+=alice.get(i)+bob.get(i); i++; }else{ res+=both.get(r); r++; } } System.out.println(res); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; vector<long long int> a, b, c; for (int i = 0; i < n; i++) { int t, x, y; cin >> t >> x >> y; if (x && y) c.push_back(t); else if (x && !y) a.push_back(t); else if (!x && y) b.push_back(t); } sort(a.begin(), a.end(), greater<long long>()); sort(b.begin(), b.end(), greater<long long>()); long long ans = 0; int an = a.size(), bn = b.size(), cn = c.size(); if (an + cn >= k && bn + cn >= k) { while ((int)a.size() > 0 && (int)b.size() > 0) { c.push_back(a.back() + b.back()); a.pop_back(); b.pop_back(); } sort(c.begin(), c.end()); for (int i = 0; i < k; i++) { ans += c[i]; } } else ans = -1; cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) times = [[], [], [], []] for i in range(n): t, a, b = map(int, input().split()) times[a * 2 + b].append(t) for i in range(1, 4): times[i].sort() sums = [] for i in range(len(times)): sums.append([]) for j in range(len(times[i])): if j == 0: sums[i].append(times[i][0]) else: sums[i].append(times[i][j] + sums[i][j - 1]) sums[1].insert(0, 0) sums[2].insert(0, 0) sums[3].insert(0, 0) ans = 10 ** 10 for i in range(min(k + 1, len(sums[-1]))): if k - i < len(sums[1]) and k - i < len(sums[2]): ans = min(ans, sums[3][i] + sums[1][k - i] + sums[2][k - i]) if ans == 10 ** 10: ans = -1 print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
input = raw_input range = xrange import sys inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 seg = [0]*200000 def offset(x): return x + 100000 def encode(x, y): return x*200002 + y def decode(x): return x//200002, x%200002 def upd(node, L, R, pos, val): while L < R: seg[node] += val seg[offset(node)] += val*pos if L+1 == R: break M = (L+R)//2 node <<= 1 if pos < M: R = M else: L = M node += 1 def query(node, L, R, k): ret = 0 while L < R: if k == 0: return ret if seg[node] == k: return ret + seg[offset(node)] if L+1 == R: return ret + k*L M = (L+R)//2 node <<= 1 if seg[node] >= k: R = M else: ret += seg[offset(node)] k -= seg[node] L = M node += 1 return ret n, m, k = inp[ii:ii+3]; ii += 3 A, B, both, neither = [], [], [], [] for i in range(n): t, a, b = inp[ii:ii+3]; ii += 3 if a == 0 and b == 0: neither.append(encode(t, i+1)) if a == 1 and b == 0: A.append(encode(t, i+1)) if a == 0 and b == 1: B.append(encode(t, i+1)) if a == 1 and b == 1: both.append(encode(t, i+1)) upd(1, 0, 10001, t, 1) A.sort(); B.sort(); both.sort() p1 = min(k, len(both)) p2 = k - p1 if 2*k - p1 > m or p2 > min(len(A), len(B)): print(-1) exit(0) sum, ans, ch = 0, 2**31, p1 for i in range(p1): sum += both[i]//200002 upd(1, 0, 10001, both[i]//200002, -1) for i in range(p2): sum += A[i]//200002 + B[i]//200002 upd(1, 0, 10001, A[i]//200002, -1) upd(1, 0, 10001, B[i]//200002, -1) ans = query(1, 0, 10001, m-2*k+p1) + sum while p1 > 0: if p2 == min(len(A), len(B)): break upd(1, 0, 10001, A[p2]//200002, -1); sum += A[p2]//200002 upd(1, 0, 10001, B[p2]//200002, -1); sum += B[p2]//200002 upd(1, 0, 10001, both[p1-1]//200002, 1); sum -= both[p1-1]//200002 p2 += 1 p1 -= 1 if m - 2*k + p1 < 0: break Q = query(1, 0, 10001, m-2*k+p1) if ans > sum + Q: ans = sum + Q ch = p1 print ans ind = [both[i]%200002 for i in range(ch)] + [A[i]%200002 for i in range(k-ch)] + [B[i]%200002 for i in range(k-ch)] st = neither + [both[i] for i in range(ch, len(both))] + [A[i] for i in range(k-ch, len(A))] + [B[i] for i in range(k-ch, len(B))] st.sort() ind += [st[i]%200002 for i in range(m-2*k+ch)] print ' '.join(str(x) for x in ind)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; import java.util.Scanner; public class E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), k = sc.nextInt(); Books[] books = new Books[n]; for (int i = 0; i < n; i++) { books[i] = new Books(sc.nextInt(), sc.nextInt(), sc.nextInt()); } Arrays.sort(books); int a = 0, b = 0, ans = 0; Deque<Books> booksa = new ArrayDeque<Books>(), booksb = new ArrayDeque<Books>(); for (Books book : books) { if (book.a && book.b) { if (a < k || b < k) { ans += book.time; a++; b++; if (a > k) { ans -= booksa.peek().time; booksa.pop(); a--; } if (b > k) { ans -= booksb.peek().time; booksb.pop(); b--; } } else { if (!booksa.isEmpty() && !booksb.isEmpty()) { int bonus = booksa.peek().time+booksb.peek().time; if (bonus > book.time) { ans -= bonus; booksa.pop(); booksb.pop(); ans += book.time; } } else if (!booksa.isEmpty()) { int bonus = booksa.peek().time; if (bonus > book.time) { b++; ans -= bonus; booksa.pop(); ans += book.time; } } else if (!booksb.isEmpty()) { int bonus = booksb.peek().time; if (bonus > book.time) { a++; ans -= bonus; booksb.pop(); ans += book.time; } } } } else if (book.a) { if (a < k) { booksa.push(book); ans += book.time; a++; } } else if (book.b) { if (b < k) { booksb.push(book); ans += book.time; b++; } } // System.out.println(a); // System.out.println(b); // System.out.println(); } if (a < k || b < k) System.out.println(-1); else System.out.println(ans); } static class Books implements Comparable<Books> { int time; boolean a, b; public Books(int time, int a, int b) { this.time = time; this.a = a==1; this.b = b==1; } @Override public int compareTo(Books o) { return Integer.compare(time, o.time); } } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=list(map(int,input().split())) l,l1,l2=[],[],[] for _ in range(n): x,y,z=list(map(int,input().split())) if y==1 and z==1: l.append(x) elif y==1: l1.append([x,y,z]) elif z==1: l2.append([x,y,z]) l1.sort() l2.sort() for i in range(min(k,len(l1),len(l2))): a=l1[i][0] b=l2[i][0] l.append(a+b) l.sort() if len(l)>=k: print(sum(l[:k])) else: print(-1)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split(" ")) both=[] bob=[] alice=[] for i in range(n): t,a,b=map(int,input().split(" ")) if a==1 and b==1: both.append(t) elif a==1: alice.append(t) elif b==1: bob.append(t) if len(both)!=0: both.sort() if len(alice)!=0: alice.sort() if len(bob)!=0: bob.sort() if len(both)+len(alice)<k or len(both)+len(bob)<k: print(-1) else: #print(alice,bob,both) i=0 j=0 ans=0 q=0 w=0 talice=0 tbob=0 while i<len(alice) and j<len(bob) and q<k and w<len(both): if alice[i]+bob[j]<=both[w]: ans+=alice[i]+bob[j] talice+=1 tbob+=1 i+=1 j+=1 else: ans+=both[w] talice+=1 tbob+=1 w+=1 q+=1 z=talice if i==len(alice) or j==len(bob): #print(w+k-z+1) ans+=sum(both[w:w+k-z]) elif w==len(both): ans+=sum(alice[i:i+k-z]) ans+=sum(bob[j:j+k-z]) print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from sys import maxsize n, k = map(int, input().split()) l = [[0], [0], [0], [0]] x = [] ans = maxsize for i in range(n): x.append(list(map(int, input().split()))) x.sort() for p in x: temp = l[p[1]*2 + p[2]][-1] l[p[1]*2 + p[2]].append(temp + p[0]) for i in range(min(k + 1, len(l[3]))): if k - i < len(l[1]) and k - i < len(l[2]): ans = min(ans, l[3][i] + l[1][k - i] + l[2][k - i]) if ans == maxsize: print(-1) else: print(ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class e653_1 { static PrintWriter out; static BufferedReader in; static StringTokenizer st; public static void main(String[] args) throws FileNotFoundException { out = new PrintWriter(System.out); in = new BufferedReader(new InputStreamReader(System.in)); new e653_1().Run(); out.close(); } String ns() { try { if (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } catch (Exception e) { return null; } } int nextint() { return Integer.valueOf(ns()); } private int n; private int k; // int inf = (int) Math.pow(2,32)/2 - 1; ArrayList<Integer> acceptableA = new ArrayList<>(); ArrayList<Integer> acceptableB = new ArrayList<>(); ArrayList<Integer> acceptable = new ArrayList<>(); public void Run() { n = nextint(); k = nextint(); int aPos = 0; int bPos = 0; for(int i = 0; i < n; i++){ int t = nextint(); int a = nextint(); int b = nextint(); if(a == 1 && b == 1){ acceptable.add(t); aPos++; bPos++; } else if (a == 1){ aPos++; acceptableA.add(t); } else if (b == 1){ bPos++; acceptableB.add(t); } } if (aPos < k || bPos < k){ out.println(-1); return; } Collections.sort(acceptableB); Collections.sort(acceptableA); for(int i = 0; i < Math.min(acceptableA.size(), acceptableB.size()); i++){ acceptable.add(acceptableA.get(i) + acceptableB.get(i)); } Collections.sort(acceptable); int sum = 0; for(int i = 0; i < k; i++){ sum += acceptable.get(i); } out.println(sum); } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void quick_sort(int a[], int left, int right) { int l = left, r = right, m = a[rand() % (r - l) + l]; while (l < r) { while (a[l] < m) l++; while (a[r] > m) r--; if (l <= r) { int t = a[l]; a[l] = a[r]; a[r] = t; l++; r--; } } if (left < r) quick_sort(a, left, r); if (right > l) quick_sort(a, l, right); } int main() { int n, k, a[200000], size_a = 0, b[200000], size_b = 0, c[200000], size_c = 0, min = 0, cnt_a = 0, cnt_b = 0; cin >> n >> k; while (n--) { int t[3]; cin >> t[0] >> t[1] >> t[2]; if (t[1] + t[2] == 2) c[size_c++] = t[0]; else if (t[1]) a[size_a++] = t[0]; else if (t[2]) b[size_b++] = t[0]; } if (size_c > 1) quick_sort(c, 0, size_c - 1); if (size_a > 1) quick_sort(a, 0, size_a - 1); if (size_b > 1) quick_sort(b, 0, size_b - 1); int i = 0, j = 0, l = 0; for (; ((i < size_a && j < size_b) || l < size_c) && cnt_a < k && cnt_b < k;) { if (i < size_a && j < size_b) { if (l < size_c) if (a[i] + b[j] < c[l]) min += a[i++] + b[j++]; else min += c[l++]; else min += a[i++] + b[j++]; } else if (l < size_c) { min += c[l++]; } else break; cnt_a++; cnt_b++; } for (; (i < size_a || l < size_c) && cnt_a < k;) { if (i < size_a) { if (l < size_c) if (a[i] < c[l]) min += a[i++]; else { min += c[l++]; cnt_b++; } else min += a[i++]; } else if (l < size_c) { min += c[l++]; cnt_b++; } else break; cnt_a++; } for (; (j < size_b || l < size_c) && cnt_b < k;) { if (j < size_b) { if (l < size_c) if (b[j] < c[l]) min += b[j++]; else { min += c[l++]; cnt_a++; } else min += b[j++]; } else if (l < size_c) { min += c[l++]; cnt_a++; } else break; cnt_b++; } if (cnt_a < k || cnt_b < k) cout << -1; else cout << min; return 0; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; vector<long long> alice; vector<long long> bob; vector<long long> combined; long long atotal = 0, btotal = 0; for (long long i = 0; i < n; ++i) { long long t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) combined.push_back(t); else if (a == 1) alice.push_back(t); else if (b == 1) bob.push_back(t); atotal += a; btotal += b; } sort(combined.begin(), combined.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); long long score = 0; bool possible = true; if (atotal < k || btotal < k) { possible = false; cout << -1; } if (possible) { long long m = combined.size(); m = min(k, m); for (long long i = 0; i < m; ++i) { score += combined[i]; } long long pointer = 0; if (m < k) { for (long long j = 0; j < k - m; ++j) { score += alice[j]; score += bob[j]; } pointer = k - m; } long long asize = alice.size(); long long bsize = bob.size(); while (pointer < asize && pointer < bsize && pointer < k && alice[pointer] + bob[pointer] < combined[k - 1 - pointer]) { score -= combined[k - 1 - pointer]; score += alice[pointer]; score += bob[pointer]; pointer++; } cout << score; } }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = map(int,input().split()) U = [];A = [];B = [] for i in range(n): a,b,c = map(int,input().split()) if(b==1 and c==1): U.append(a) elif(b==1 and c==0): A.append(a) elif(b==0 and c==1): B.append(a) A.sort();B.sort();U.sort() for i in range(1,len(U)): U[i]+=U[i-1] for i in range(1,len(A)): A[i]+=A[i-1] for i in range(1,len(B)): B[i]+=B[i-1] f_ans = 1e10 cnt = 0;ans =0 for i in range(-1,len(U)): cnt = i+1;ans = 0 if(i>=0): ans = U[i] if(k-cnt==0): f_ans = min(ans,f_ans) break if(k-cnt>len(A) or k-cnt >len(B)): continue ans += A[k-cnt-1] + B[k-cnt-1] cnt = k f_ans = min(f_ans,ans) f_ans = min(ans,f_ans) if(k-cnt>0): print("-1") else: print(f_ans)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import os,io input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,k = map(int,input().split()) bookA = [] bookB = [] bookAB = [] for _ in range(n): t,a,b = map(int,input().split()) if a == 1 and b == 1: bookAB.append(t) elif a == 1: bookA.append(t) elif b == 1: bookB.append(t) bookA.sort() bookB.sort() bookAB.sort() bookASum = [0] bookBSum = [0] bookABSum = [0] for elem in bookA: bookASum.append(bookASum[-1] + elem) for elem in bookB: bookBSum.append(bookBSum[-1] + elem) for elem in bookAB: bookABSum.append(bookABSum[-1] + elem) minReadingTime = -1 for i in range(len(bookABSum)): if len(bookASum) > max(k - i,0) and len(bookBSum) > max(k - i,0) and len(bookABSum) > i: if minReadingTime == -1 or minReadingTime > bookABSum[i] + bookASum[max(k - i,0)] + bookBSum[max(k - i,0)]: minReadingTime = bookABSum[i] + bookASum[max(k - i,0)] + bookBSum[max(k - i,0)] print(minReadingTime)
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys # from collections import defaultdict # t=1 # t=int(input()) def fun(x): # print(x) return x[0] n,k=list(map(int,sys.stdin.readline().strip().split())) xx=[] a=[] b=[] c=[] for i in range(n): # n=int(input()) x=list(map(int,sys.stdin.readline().strip().split())) # a,b,c,d=list(sys.stdin.readline().strip().split()) # n,k=list(map(int,sys.stdin.readline().strip().split())) # xx.append(x) if(x[1]==x[2]==1): a.append(x[0]) elif(x[1]==1): b.append(x[0]) elif(x[2]==1): c.append(x[0]) # a=k # b=k # # print(xx) # xx.sort(key=fun) # # print(xx) # op=0 # for i in xx: # if() b.sort() c.sort() for i in range(min(len(b),len(c))): a.append(b[i]+c[i]) a.sort() if(len(a)<k): print(-1) else: print(sum(a[:k]))
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 99; int n, k, m, x, y, p0, p1, p2, b0, b1, b2, b3, a[N]; vector<pair<long long, int> > v0, v1, v2, v3; long long ans = 1e18, sum; void calc(int x) { if (p0 < v0.size() - 1) p0++, sum += v0[p0].first; if (p0 < v0.size() - 1) p0++, sum += v0[p0].first; if (k - x >= int(v1.size()) || k - x >= int(v2.size()) || x + v2.size() + v1.size() + v0.size() - 3 < m || x + max((k - x), 0) * 2 > m) return; while (x + p1 + p2 + p0 > m) { if (p0 && (p1 == max(k - x, 0) || v0[p0] >= v1[p1]) && (p2 == max(k - x, 0) || v0[p0] >= v2[p2])) sum -= v0[p0--].first; else { if (p1 == max(k - x, 0) || (p2 != max(k - x, 0) && v2[p2].first >= v1[p1].first)) { while (p2 == max(k - x, 0)) n = 1; sum -= v2[p2--].first; } else sum -= v1[p1--].first; } } if (sum + v3[x].first < ans) ans = sum + v3[x].first, b0 = p0, b1 = p1, b2 = p2, b3 = x; } int main() { cin >> n >> m >> k; v1.push_back(make_pair(0, 0)), v2.push_back(make_pair(0, 0)), v3.push_back(make_pair(0, 0)), v0.push_back(make_pair(0, 0)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); ; scanf("%d%d", &x, &y); ; if (x == 1 && y == 1) v3.push_back(make_pair(a[i], i + 1)); if (x == 1 && y == 0) v1.push_back(make_pair(a[i], i + 1)); if (x == 0 && y == 1) v2.push_back(make_pair(a[i], i + 1)); if (x == 0 && y == 0) v0.push_back(make_pair(a[i], i + 1)); } p0 = v0.size() - 1, p1 = v1.size() - 1, p2 = v2.size() - 1; sort(v0.begin(), v0.end()); sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); for (int i = 1; i < v0.size(); i++) sum += v0[i].first; for (int i = 1; i < v1.size(); i++) sum += v1[i].first; for (int i = 1; i < v2.size(); i++) sum += v2[i].first; for (int i = 1; i < v3.size(); i++) v3[i].first += v3[i - 1].first; for (int i = 0; i < v3.size(); i++) calc(i); if (ans == 1e18) return cout << -1, 0; cout << ans << endl; for (int i = 1; i < b0 + 1; i++) cout << v0[i].second << " "; for (int i = 1; i < b1 + 1; i++) cout << v1[i].second << " "; for (int i = 1; i < b2 + 1; i++) cout << v2[i].second << " "; for (int i = 1; i < b3 + 1; i++) cout << v3[i].second << " "; }
1374_E1. Reading Books (easy version)
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. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import collections as cc import math as mt import sys input=sys.stdin.readline I=lambda:list(map(int,input().split())) n,k=I() a=[] for i in range(n): a.append(I()) a.sort() both=[] bs=0 f=0 for i in range(n): if a[i][1] and a[i][2]: both.append(a[i][0]) al=[] bo=[] for i in range(n): if a[i][1] and not a[i][2]: al.append(a[i][0]) elif a[i][2] and not a[i][1]: bo.append(a[i][0]) su=[] for i in range(min(len(al),len(bo))): su.append(al[i]+bo[i]) xx=len(both) yy=len(su) if xx+yy<k: print(-1) elif both and not su: if xx>=k: print(sum(both[:k])) else: print(-1) elif su and not both: if yy>=k: print(sum(su[:k])) else: print(-1) else: te=both+su te.sort() print(sum(te[:k]))