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" ] }
IN-CORRECT
python2
import atexit, io, sys # A stream implementation using an in-memory bytes # buffer. It inherits BufferedIOBase. buffer = io.BytesIO() sys.stdout = buffer # print via here @atexit.register def write(): sys.__stdout__.write(buffer.getvalue()) for _ in range(1): n,m,k=map(int,raw_input().split()) v=[] t=[] al=0 jt=[] for i in range(n): vc=map(int,raw_input().split()) if vc[1] or vc[2]: v.append(vc) else: jt.append(vc[0]) s=sorted(v) ac,bc,at,bt=0,0,0,0 a,b=[],[] for i in s: if i[1]!=1 or i[2]!=1: if i[1]==1: ac+=1 a.append(i[0]) else: bc+=1 b.append(i[0]) else: t.append(i[0]) ok=1 if len(t)>=k: p,g=0,k-1 am=k ca=sum(t[:k]) while( p<len(a) and p<len(b) and g>=0): if (a[p]+b[p])<=t[g]: ca=ca-t[g]+a[p]+b[p];p+=1;g-=1;am+=1 else: break if am<m: gy=a[p:]+b[p:]+t[min(g+1,len(t)-1):]+jt gy=sorted(gy) if (m-am) >len(gy): print -1 else: print ca+sum(gy[:m-am]) else: p-=1;g+=1 while(p>=0 and g<len(t) and am>m): if p<0 or g==len(t): print-1;ok=0;break ca=ca-a[p]-b[p]+t[g];am-=1 p-=1;g+=1 if ok: print ca elif( len(a)+len(t))<k or (len(b)+len(t)) <k: print -1;continue else : f=k-len(t) ca=sum(a[:f])+sum(b[:f])+sum(t) p,g=f,len(t)-1 am=len(t)+2*f while( p<len(a) and p<len(b) and g>=0): if (a[p]+b[p])<=t[g]: ca=ca-t[g]+a[p]+b[p];p+=1;g-=1;am+=1 else: break if am<m: gy=a[p:]+b[p:]+t[min(g+1,len(t)-1):]+jt gy=sorted(gy) if (m-am) >len(gy): print -1 break else: print ca+sum(gy[:m-am]) else: p-=1;g+=1 while(p>=0 and g<len(t) and am>m): if p<0 or p==min(len(a),len(b))or g==len(t): print-1;ok=0;break ca=ca-a[p]-b[p]+t[g];am-=1 p-=1;g+=1 if ok: print ca
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" ] }
IN-CORRECT
java
import java.util.*; public class Solution { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int a[][]=new int[n][3]; for(int i=0;i<n;i++) { for(int j=0;j<3;j++) { a[i][j]=sc.nextInt(); } } List<Integer> alice=new ArrayList<Integer>(); List<Integer> bob=new ArrayList<Integer>(); List<Integer> albob=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(a[i][1]==1 && a[i][2]==0) alice.add(a[i][0]); if(a[i][1]==0 && a[i][2]==1) bob.add(a[i][0]); if(a[i][1]==1 && a[i][2]==1) albob.add(a[i][0]); } Collections.sort(alice); Collections.sort(bob); Collections.sort(albob); int sum=0; if(alice.isEmpty() || bob.isEmpty()) { if(albob.size()>=k) { for(int i=0;i<k;i++) { sum+=albob.get(i); } } } else if(albob.isEmpty() && alice.size()>=k && bob.size()>=k) { for(int i=0;i<k;i++) sum+=alice.get(i)+bob.get(i); } else { int z=0; int ct=0; List<Integer> sum1=new ArrayList<Integer>(); while(z<alice.size() && z<bob.size() && z<albob.size()) { if( alice.get(z)+bob.get(z)<=albob.get(z)) { sum+=alice.get(z)+bob.get(z); sum1.add(albob.get(z)); } else { sum+=albob.get(z); sum1.add(alice.get(z)+bob.get(z)); } z++; ct++; } while(z<albob.size()) { sum+=albob.get(z); z++; ct++; } if(ct!=k) { int c=0; while(ct<k && !sum1.isEmpty()) { sum+=sum1.get(c); c++; ct++; } } } if(sum>0) System.out.println(sum); else System.out.println("-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" ] }
IN-CORRECT
python3
n,m,k = map(int,input().split()) ab = [] a = [] b = [] other = [] l = [list(map(int,input().split())) for i in range(n)] for i in range(n): t,c,d = l[i] if c and d == 0: a.append([t,i+1]) elif d and c == 0: b.append([t,i+1]) elif c*d: ab.append([t,i+1]) else: other.append([t,i+1]) def acc(x): d = [0] for i,j in x: d.append(d[-1]+i) return d a.sort() b.sort() ab.sort() other.sort() sa = acc(a) sb = acc(b) sab = acc(ab) so = acc(other) la = len(a) lb = len(b) lab = len(ab) lo = len(other) if lb > la: la,lb = lb,la a,b = b,a ans = float("INF") ind = -1 for i in range(k+1): nab = i na = k-i no = m-2*na-nab if nab > lab or na > lb or no < 0 or no > lo: continue count = sa[na]+sb[na]+sab[nab]+so[no] if count < ans: ans = count ind = nab if ind == -1: print(-1) else: nab = ind na = k-ind no = m-2*na-nab lis = [] for i,j in a[:na]: lis.append(j) for i,j in b[:na]: lis.append(j) for i,j in ab[:nab]: lis.append(j) for i,j in other[:no]: lis.append(j) print(ans) print(*lis)
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") template <class T> inline T bigMod(T p, T e, T M) { T ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T modInverse(T a, T M) { return bigMod(a, M - 2, M); } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { a = abs(a); b = abs(b); return (a / gcd(a, b)) * b; } template <class T> inline string int2String(T a) { ostringstream str; str << a; return str.str(); } const int maxn = 2e5 + 10; struct info { int t, a, b; } v[maxn]; int n, k, id[maxn]; bool cmp(const int& a, const int& b) { if (v[a].b == v[b].b) { return v[a].t < v[b].t; } return v[a].b > v[b].b; } std::priority_queue<int, std::vector<int>, decltype(&cmp)> pq(cmp); bool cmp1(const int& a, const int& b) { return v[a].a > v[b].a; } int main() { scanf("%d%d", &n, &k); for (int i = int(0); i < int(n); i++) { scanf("%d%d%d", &v[i].t, &v[i].a, &v[i].b); id[i] = i; } sort(id, id + n, cmp1); long long sum = 0; int j, cnta = 0, cntb = 0; for (int i = int(0); i < int(k); i++) { j = id[i]; pq.push(j); sum += v[j].t; cnta += v[j].a; cntb += v[j].b; } if (cnta < k) { puts("-1"); return 0; } long long ans = 0x3f3f3f3f; if (cntb >= k) { ans = sum; } for (int i = int(k); i < int(n); i++) { j = id[i]; pq.push(j); sum += v[j].t; cnta += v[j].a; cntb += v[j].b; while (cnta >= k && cntb >= k) { ans = min(ans, sum); j = pq.top(); pq.pop(); sum -= v[j].t; cnta -= v[j].a; cntb -= v[j].b; } } printf("%lld\n", ans >= 0x3f3f3f3f ? -1LL : 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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Solution implements Runnable { 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 Solution(), "Main", 1 << 27).start(); } static class Pair { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x * 7 + (y * 3 + 5 * (y - x)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Pair other = (Pair) obj; if (x != other.x && y != other.y) { return false; } return true; } } static void sieveOfEratosthenes(int n) { //Prints prime nos till n boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) System.out.print(i + " "); } } public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=in.nextInt(); int k=in.nextInt(); ArrayList<Integer> A=new ArrayList<Integer>(); ArrayList<Integer> B=new ArrayList<Integer>(); ArrayList<Integer> AB=new ArrayList<Integer>(); for(int i=0;i<n;i++) { int t=in.nextInt(); int a=in.nextInt(); int b=in.nextInt(); if(a==1 && b==1) AB.add(t); else if(a==1 && b==0) A.add(t); else if(a==0 && b==1) B.add(t); } Collections.sort(A); Collections.sort(B); Collections.sort(AB); if((A.size()+AB.size())<k) w.println(-1); else if((B.size()+AB.size())<k) w.println(-1); else { long count=0; if(A.size()==0 || B.size()==0) { for(int i=0;i<AB.size();i++) count+=AB.get(i); w.println(count); } else { int i=0,j=0,x=0; while(i<A.size() && i<B.size() && j<AB.size()) { if((A.get(i)+B.get(i))<(AB.get(j))) { count+=A.get(i)+B.get(i); i++; } else { count+=AB.get(j); j++; } x++; } int x1=x,x2=x,i1=i,i2=i; while(x1<k && i1<A.size()) { count+=A.get(i1); i1++; x1++; } while(x2<k && i2<B.size()) { count+=B.get(i2); i2++; x2++; } while(x<k && j<AB.size()) { count+=AB.get(j); j++; x++; } w.println(count); } } w.flush(); w.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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class GFG { private static int i,j,k,l,n,m; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // int t = Integer.parseInt(br.readLine().trim()); // while (t-- != 0) { // int n = Integer.parseInt(br.readLine().trim()); // char c[]=br.readLine().trim().toCharArray(); StringTokenizer st = new StringTokenizer(br.readLine().trim()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); int kk = Integer.parseInt(st.nextToken()); ArrayList<int[]> A[]=new ArrayList[4]; for(i=0;i<4;i++) A[i]=new ArrayList<>(); int ans=0; for(i=0;i<n;i++){ st = new StringTokenizer(br.readLine().trim()); int time=Integer.parseInt(st.nextToken()); int al=Integer.parseInt(st.nextToken()); int bo=Integer.parseInt(st.nextToken()); int a[]=new int[]{time,i+1}; if(al==1&&bo==0) A[0].add(a); else if(al==0&&bo==1) A[1].add(a); else if(al==1&&bo==1) A[2].add(a); else A[3].add(a); } if(A[0].size()+A[2].size()<kk||A[1].size()+A[2].size()<kk) out.println(-1); else{ for(i=0;i<4;i++) Collections.sort(A[i],(b,c)->b[0]-c[0]); // out.println(A[0]); // out.println(A[1]); // out.println(A[2]); // out.println(A[3]); i=0;j=0;k=0;l=0; int cnt=0,books=0; while(i<A[0].size()&&j<A[1].size()&&k<A[2].size()&&cnt!=kk) { int sum = A[0].get(i)[0] + A[1].get(j)[0],c=A[2].get(k)[0]; if (sum < c) { ans += sum; books+=2; i++; j++; } else { ans += c; books++; k++; } cnt++; } if(i==A[0].size()||j==A[1].size()){ while(cnt!=kk){ cnt++; books++; ans+=A[2].get(k++)[0]; } } else if(k==A[2].size()){ while(cnt!=kk){ cnt++; books+=2; ans+=A[0].get(i++)[0] + A[1].get(j++)[0]; } } while(books>m) { if (k == A[2].size() || i == 0 || j == 0) { ans = -1; break; } books --; ans = ans - A[0].get(--i)[0] - A[1].get(--j)[0] + A[2].get(k++)[0]; } if(books<m) ans+=find(A,books); out.println(ans); if(ans!=-1){ for(int y=0;y<i;y++) out.print(A[0].get(y)[1]+" "); for(int y=0;y<j;y++) out.print(A[1].get(y)[1]+" "); for(int y=0;y<k;y++) out.print(A[2].get(y)[1]+" "); for(int y=0;y<l;y++) out.print(A[3].get(y)[1]+" "); out.println(); } } out.close(); } private static int find(ArrayList<int[]>[] A, int books) { int ans=0; while(books++!=m){ int a=Integer.MAX_VALUE,b=Integer.MAX_VALUE,c=Integer.MAX_VALUE,d=Integer.MAX_VALUE; if(i<A[0].size()){ a=A[0].get(i)[0]; } if(j<A[1].size()){ b=A[1].get(j)[0]; } if(k<A[2].size()){ c=A[2].get(k)[0]; } if(l<A[3].size()){ d=A[3].get(l)[0]; } int min=Math.min(a,Math.min(b,Math.min(c,d))); if(min==a) i++; else if(min==b) j++; else if(min==c) k++; else l++; ans+=min; } return 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct point { long long t, in; }; bool ctp(point a, point b) { return a.t <= b.t; } int32_t main() { long long n, k, t, a1, b1, m; cin >> n >> m >> k; vector<point> c0, c1, c2, c3; for (long long i = 0; i < n; i++) { cin >> t >> a1 >> b1; if (a1 == 1 && b1 == 1) c3.push_back({t, i + 1}); else if (a1 == 1) c1.push_back({t, i + 1}); else if (b1 == 1) c2.push_back({t, i + 1}); else { c0.push_back({t, i + 1}); } } long long cnta = c1.size(); long long cntb = c2.size(); long long cntc = c3.size(); if (cnta + cntc < k || cntb + cntc < k) cout << -1; else { sort(c1.begin(), c1.end(), ctp); sort(c2.begin(), c2.end(), ctp); sort(c3.begin(), c3.end(), ctp); long long a2 = 0; long long b2 = 0; long long i = 0; long long j = 0; long long i1 = 0; long long ans = 0; vector<long long> ans1; long long j1 = 0; while (a2 < k && i < cnta && j < cntb && i1 < cntc) { if (c1[i].t + c2[j].t < c3[i1].t && j1 < m - 1) { ans += c1[i].t + c2[j].t; ans1.push_back(c1[i].in); ans1.push_back(c2[j].in); i++; j++; j1 += 2; } else { ans += c3[i1].t; ans1.push_back(c3[i1].in); i1++; j1++; } a2++; } if (a2 < k) { if (i1 < cntc) { while (i1 < cntc && a2 < k) { ans += c3[i1].t; ans1.push_back(c3[i1].in); i1++; a2++; j1++; } } else { while (a2 < k) { ans += c2[j].t + c1[i].t; ans1.push_back(c1[i].in); ans1.push_back(c2[j].in); i++; j++; a2++; j1 += 2; } } } if (j1 > m) cout << -1; else if (j1 == m) { cout << ans << endl; for (long long i2 = 0; i2 < ans1.size(); i2++) cout << ans1[i2] << " "; } else { while (i < cnta) { c0.push_back(c1[i]); i++; } while (j < cntb) { c0.push_back(c2[j]); j++; } while (i1 < cntc) { c0.push_back(c3[i1]); i1++; } sort(c0.begin(), c0.end(), ctp); i = 0; while (j1 < m && i < c0.size()) { ans += c0[i].t; ans1.push_back(c0[i].in); i++; j1++; } if (j1 < m) cout << -1; else { cout << ans << endl; for (long long i2 = 0; i2 < ans1.size(); i2++) cout << ans1[i2] << " "; } } } 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class E1_Round_643_Div3 { public static long MOD = 1000000007; static int[] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int k = in.nextInt(); int[][] data = new int[n][3]; PriorityQueue<Integer> q = new PriorityQueue<>(); PriorityQueue<Integer> a = new PriorityQueue<>(); PriorityQueue<Integer> b = new PriorityQueue<>(); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { data[i][j] = in.nextInt(); } if (data[i][1] == data[i][2] && data[i][1] == 1) { q.add(data[i][0]); } else if (data[i][1] == 1) { a.add(data[i][0]); } else { b.add(data[i][0]); } } while (!a.isEmpty() && !b.isEmpty()) { int x = a.poll(); int y = b.poll(); q.add(x + y); } if (q.size() < k) { out.println(-1); } else { int result = 0; for (int i = 0; i < k; i++) { result += q.poll(); } out.println(result); } out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val; } else { return val * (val * a); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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" ] }
IN-CORRECT
python3
from collections import Counter, defaultdict BS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" def to_base(s, b): res = "" while s: res+=BS[s%b] s//= b return res[::-1] or "0" alpha = "abcdefghijklmnopqrstuvwxyz" from math import floor, ceil,pi primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919 ] def primef(n, plst = []): if n==1: return plst else: for m in primes: if n%m==0: return primef(n//m, plst+[m]) return primef(1, plst+[n]) def lmii(): return list(map(int, input().split())) def ii(): return int(input()) def countOverlapping(string,sub): count = start = 0 while True: start = string.find(sub, start)+1 if start > 0: count += 1 else: return count """ t = ii() for i in range(t): x,y,n = lmii() ns = int(n) a = n%x if a==y: print(n) else: n //= x n *= x n += y if n > ns: n -= x if n < 0: n = 0 print(n)""" """ t = ii() for i in range(t): n = int(input()) if n==1: print(0) else: pr = primef(n) c = Counter(pr) if c[2] > c[3]: print(-1) elif c[3]==0 or len(c) > 2: print(-1) else: if c[3] > 0 and (c[2] > 0 or len(c)==1): #print(c) print((c[3]+c[3]-c[2])) else: print(-1)""" """ t = ii() for i in range(t): n = int(input()) s = list(input()) while "()" in "".join(s): s = list("".join(s).replace("()", "", 1)) seen = 0 pos = 0 c = 0 while pos < len(s): seen += 1 if s[pos]=="(" else 0 #print(seen, pos, s[pos]) if 2*seen == len(s): break if s[pos]==")" and seen*2 < len(s): s.append(s.pop(pos)) c += 1 else: pos += 1 print(c)""" import heapq as hp n,k = lmii() firstKA = int(k) firstKB = int(k) nums = [lmii() for i in range(n)] nums.sort(key = lambda x: (x[0], abs(x[1]-x[2]))) chosen = [] for i in nums: #print(i, firstKA, firstKB) if firstKB==firstKA==0: break if i[1]+i[2]==2: firstKA -= 1 if firstKA > 0 else 0 firstKB -= 1 if firstKB > 0 else 0 chosen.append(i) elif i[1]==1 and firstKA > 0: chosen.append(i) firstKA -= 1 elif i[2]==1 and firstKB > 0: chosen.append(i) firstKB -= 1 #print(i, firstKA, firstKB) #print(chosen) if firstKA > 0 or firstKB > 0: print(-1) else: tot = 0 fa = int(k) fb = int(k) chosen.sort(key = lambda x: (9999999999999-x[0], abs(x[1]-x[2]))) for f in range(len(chosen)): s = chosen[f] if s[1]+s[2]==2: fa -= 1 if fa > 0 else 0 fb -= 1 if fb > 0 else 0 tot += s[0] elif s[1]==1 and fa > 0: fa -= 1 if fa > 0 else 0 tot += s[0] elif s[2]==1 and fb > 0: tot += s[0] fb -= 1 if fb > 0 else 0 if fa==fb==0: print(tot) break
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" ] }
IN-CORRECT
python3
n, k = map(int, input().split()) a = [] for i in range(n): x,y,z = map(int, input().split()) a.append([x,y,z]) a.sort() alice_count = 0 bob_count = 0 alice = [] bob = [] combine = [] for i in range(n): if a[i][1] == 1 and a[i][2] == 1: combine.append(a[i][0]) alice_count += 1 bob_count += 1 elif a[i][1] == 1: alice.append(a[i][0]) alice_count += 1 elif a[i][2] == 1: bob.append(a[i][0]) bob_count += 1 if alice_count < k or bob_count < k: print('-1') else: alice.sort() alice_len = len(alice) bob.sort() combine.sort() bob_len = len(bob) combine_len = len(combine) ans = 0 i1 = 0 i2 = 0 while(i1 + i2 < k): if i1 < bob_len and i1 < alice_len and i2 < combine_len: if bob[i1] + alice[i1] < combine[i2]: ans += combine[i2] i2 += 1 else: ans += bob[i1] + alice[i1] i1 += 1 elif i1 >= bob_len or i1 >= alice_len: ans += combine[i2] i2 += 1 elif i2 >= combine_len: ans += bob[i1] + alice[i1] i1 += 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" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException,InterruptedException{ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt(); PriorityQueue<pair> pq1=new PriorityQueue<>(); PriorityQueue<pair> pq2=new PriorityQueue<>(); PriorityQueue<pair> pq3=new PriorityQueue<>(); PriorityQueue<pair> pq4=new PriorityQueue<>(); PriorityQueue<pair> pq5=new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<pair> pq6=new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<pair> pq7=new PriorityQueue<>(Collections.reverseOrder()); HashSet<Integer> hs=new HashSet<>(); for (int i = 0; i < n; i++) { int t=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt(); if(a==1&&b==1) { pq1.add(new pair(t,i+1)); }else if(a==1) { pq2.add(new pair(t,i+1)); }else if(b==1) { pq3.add(new pair(t,i+1)); }else { pq4.add(new pair(t,i+1)); } } long c=0; for (int i = 0; i < k; i++) { long a=1000000000; long b=1000000000; if(!pq1.isEmpty()) a=pq1.peek().x; if(!pq2.isEmpty()&&!pq3.isEmpty()) b=pq2.peek().x+pq3.peek().x; if (a==1000000000&&b==1000000000) { c=-1; break; } if(a<=b) { c+=a; pq5.add(pq1.peek()); pq1.poll(); }else { c+=b; pq6.add(pq2.peek()); pq7.add(pq3.peek()); pq2.poll(); pq3.poll(); } } if (pq5.size()+pq6.size()+pq7.size()>m) { while (pq5.size()+pq6.size()+pq7.size()>m) { if(pq1.isEmpty()) { c=-1; break; } c-=pq7.poll().x; c-=pq6.poll().x; c+=pq1.peek().x; pq5.add(pq1.poll()); } }else if (pq5.size()+pq6.size()+pq7.size()<m) { // int c1=0,c2=0; while (pq5.size()+pq6.size()+pq7.size()<m) { pair a=new pair(1000000000,1000000000); boolean f1=false,f2=false,f3=false; if(!pq1.isEmpty()) { a=pq1.poll(); f1=true; } if(!pq2.isEmpty()) if (pq2.peek().x<a.x) { pq1.add(a); a=pq2.poll(); f2=true; f1=false; } if(!pq3.isEmpty()) if (pq3.peek().x<a.x) { if(f1)pq1.add(a); else pq2.add(a); a=pq3.poll(); f3=true; f2=false; } if(!pq4.isEmpty()) if (pq4.peek().x<a.x) { if(f1)pq1.add(a); else if(f2) pq2.add(a); else pq3.add(a); a=pq4.poll(); } pq7.add(a); c+=a.x; } } if(pq5.size()+pq6.size()<k||pq5.size()+pq7.size()<k) c=-1; pw.println(c); if(c!=-1) { while (!pq5.isEmpty()) { pw.print(pq5.poll().y+" "); } while (!pq6.isEmpty()) { pw.print(pq6.poll().y+" "); } while (!pq7.isEmpty()) { pw.print(pq7.poll().y+" "); } pw.println(); } pw.close(); } static PrintWriter pw=new PrintWriter(System.out); static long pow(int a,int b) { long r=1l; for (int i = 0; i < b; i++) { r*=a; } return r; } static boolean isprime(long n) { for (int i = 2; i <= Math.sqrt(n); i++) { if(n%i==0) return false; } return true; } static int[]lp; static void sieveLinear(int N){ ArrayList<Integer> primes = new ArrayList<Integer>(); lp = new int[N + 1]; //lp[i] = least prime divisor of i for(int i = 2; i <= N; ++i){ if(lp[i] == 0){ primes.add(i); lp[i] = i; } int curLP = lp[i]; for(int p: primes)//all primes smaller than or equal my lowest prime divisor if(p > curLP || p * 1l * i > N) break; else lp[p * i] = p; } } static long gcd(int x,int y) { while (x!=y) { if(Math.max(x,y)/Math.min(x,y)==(double)(Math.max(x,y))/Math.min(x,y)) return Math.min(x,y); if(lp.length!=0) { if(lp[x]==x) { if(y/x==y/(double)x) return x; else return 1; }else if (lp[y]==y) { if(x/y==x/(double)y) return y; else return 1; } } if(x>y) x-=y; else y-=x; } return x; } static class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair)o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(pair other) { if(this.x==other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if(this.y==other.y) return this.z - other.z; else return this.y - other.y; } else { return this.x - other.x; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public boolean hasNext() { // TODO Auto-generated method stub return false; } 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Reading_Books_easy_version { static int []a,arr,b; static Integer myInf = Integer.MAX_VALUE; public static void main(String[]args)throws IOException { /*Scanner sc=new Scanner (System.in); int n=sc.nextInt(),i; int k=sc.nextInt(); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { arr[i]=sc.nextInt(); a[i]=sc.nextInt(); b[i]=sc.nextInt(); }*/ int i; BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); String str=reader.readLine(); String[]array=str.split ("\\s+"); int n=Integer.parseInt(array[0]); int k=Integer.parseInt(array[1]); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { str=reader.readLine(); array=str.split ("\\s+"); arr[i]=Integer.parseInt(array[0]); a[i]=Integer.parseInt(array[1]); b[i]=Integer.parseInt(array[2]); } int ans=func(k); if(ans==(int)myInf) System.out.println("-1"); else System.out.println(ans); } public static int func(int k) { int n=arr.length,i; //qsort_randomised(0,n-1); /*for(i=0;i<n;i++) { System.out.print(arr[i]+" "); System.out.print(a[i]+" "); System.out.print(b[i]+" "); System.out.println(""); }*/ int ptr01=0,ptr10=0,ptr11=0; Boolean sort11=true,sort10=true,sort01=true; int[]arr01w=new int[n]; int[]arr10w=new int[n]; int[]arr11w=new int[n]; for(i=0;i<ptr01-1;i++) { if(arr01w[i]>arr01w[i+1])sort01=false; } for(i=0;i<ptr10-1;i++) { if(arr10w[i]>arr10w[i+1])sort10=false; } for(i=0;i<ptr11-1;i++) { if(arr11w[i]>arr11w[i+1])sort11=false; } for(i=0;i<n;i++) { if(a[i]==0&&b[i]==1) { arr01w[ptr01]=arr[i]; ptr01++; } if(a[i]==1&&b[i]==0) { arr10w[ptr10]=arr[i]; ptr10++; } if(a[i]==1&&b[i]==1) { arr11w[ptr11]=arr[i]; ptr11++; } } /*for(i=0;i<=ptr01;i++) { System.out.print(arr01w[i]+" hi01"); } System.out.println(""); for(i=0;i<=ptr10;i++) { System.out.print(arr10w[i]+" hi10"); } System.out.println(""); for(i=0;i<=ptr11;i++) { System.out.print(arr11w[i]+" hi11"); } System.out.println(""); */ int[]arr01=new int[ptr01]; int[]arr10=new int[ptr10]; int[]arr11=new int[ptr11]; for(i=0;i<ptr01;i++)arr01[i]=arr01w[i]; for(i=0;i<ptr10;i++)arr10[i]=arr10w[i]; for(i=0;i<ptr11;i++)arr11[i]=arr11w[i]; if(!sort11)qsort_randomised(0,ptr11-1,arr11); if(!sort10)qsort_randomised(0,ptr10-1,arr10); if(!sort01)qsort_randomised(0,ptr01-1,arr01); /*for(i=0;i<ptr01;i++) { System.out.print(arr01[i]+" hi011"); } System.out.println(""); for(i=0;i<ptr10;i++) { System.out.print(arr10[i]+" hi101"); } System.out.println(""); for(i=0;i<ptr11;i++) { System.out.print(arr11[i]+" hi111"); } System.out.println(""); */int j,j1; int[]pre_sum01,pre_sum11,pre_sum10; pre_sum01=new int[ptr01]; pre_sum11=new int[ptr11]; pre_sum10=new int[ptr10]; if(ptr01>0) { pre_sum01[0]=arr01[0]; for(i=1;i<ptr01;i++) { pre_sum01[i]=pre_sum01[i-1]+arr01[i]; } } if(ptr10>0){ pre_sum10[0]=arr10[0]; for(i=1;i<ptr10;i++) { pre_sum10[i]=pre_sum10[i-1]+arr10[i]; } } if(ptr11>0) { pre_sum11[0]=arr11[0]; for(i=1;i<ptr11;i++) { pre_sum11[i]=pre_sum11[i-1]+arr11[i]; } } int temp=0,min=(int)myInf,r=0,k1; //for(i=0;i<ptr01;i++)System.out.print(pre_sum01[i]+" "); for(i=ptr11-1;i>=0;i--) { temp=0; temp=temp+pre_sum11[i]; k1=i+1; if(k-k1>0) { if(ptr01>=k-k1&&ptr10>=k-k1) temp=temp+pre_sum01[k-k1-1]+pre_sum10[k-k1-1]; else return min; } if(min>temp)min=temp; } /*for(i=k;i>0;i--) { temp=0; if(ptr11>=k){ temp=pre_sum11[i-1]; j1=i; } else { if(ptr11-1>=0&&ptr11-r-1>=0){ temp=pre_sum11[ptr11-r-1]; j1=ptr11; r++; } else { j1=0; } } /*for(j=0;j<i;j++) { if(j>=ptr11)break; temp=temp+arr11[j]; }*/ /*if(k-j1<ptr01||k-j1<ptr10)return temp; if(k-j1!=0) { if(k-j1<ptr01||k-j1<ptr10)return -1; }*/ //if(k-j1>ptr10||k-j1>ptr01)return min; /*for(j=0;j<k-j1;j++) { /*if(j<ptr01||j<ptr10) { if(k-j1==0) { return temp; } else { return -1; } } //if(j>=ptr01||j>=ptr10)return min; temp=temp+arr01[j]; temp=temp+arr10[j]; } if(k-j1>0){ temp=temp+pre_sum01[k-j1-1]; temp=temp+pre_sum10[k-j1-1]; } if(temp<min)min=temp; }*/ return min; } public static void qsort_randomised(int p,int r,int []arr) { if(p<r) { int q=random_partition(p,r,arr); qsort_randomised(p,q-1,arr); qsort_randomised(q+1,r,arr); } } public static int random_partition(int p,int r,int[]arr) { int i1=(int)(Math.random()*(r-p)); int i=i1+p; int temp=arr[i]; arr[i]=arr[r]; arr[r]=temp; return partition(p,r,arr); } public static int partition(int p,int r,int[]arr) { int x=arr[r]; int j; int i=p-1; for(j=p;j<=r-1;j++) { if(arr[j]<=x) { i++; int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } int temp=arr[i+1]; arr[i+1]=arr[r]; arr[r]=temp; return i+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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]); ArrayList<Integer> ab=new ArrayList<>(); ArrayList<Integer> a=new ArrayList<>(); ArrayList<Integer> b=new ArrayList<>(); int i,al=0,bo=0,x,y,z; for(i=0;i<n;i++) { s=bu.readLine().split(" "); x=Integer.parseInt(s[0]); y=Integer.parseInt(s[1]); z=Integer.parseInt(s[2]); if(y==1) al++; if(z==1) bo++; if(y==1 && z==1) ab.add(x); else if(y==1) a.add(x); else b.add(x); } if(al<k || bo<k) {System.out.print("-1"); return;} Collections.sort(ab); Collections.sort(a); Collections.sort(b); ArrayList<Integer> alb=new ArrayList<>(); for(i=0;i<Math.min(a.size(),b.size());i++) alb.add(a.get(i)+b.get(i)); int min=0,c=0; if(alb.size()==0) { for(i=0;i<k;i++) min+=ab.get(i); System.out.print(min); return; } if(ab.size()==0) { for(i=0;i<k;i++) min+=alb.get(i); System.out.print(min); return; } x=0; y=0; while(x<ab.size() && y<alb.size() && c<k) { if(ab.get(x)<alb.get(y)) {min+=ab.get(x); x++;} else {min+=alb.get(y); y++;} c++; } if(c==k) {System.out.print(min); return;} while(x<ab.size() && c<k) { min+=ab.get(x); x++; c++; } while(y<alb.size() && c<k) { min+=alb.get(y); y++; c++; } System.out.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" ] }
IN-CORRECT
java
import java.util.*; public class Codeforces{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int t[]=new int[n]; int a[]=new int[n]; int b[]=new int[n]; int acount=0; int bcount=0; int ac=0; int bc=0; ArrayList<Integer> arr=new ArrayList<Integer>(); for(int i=0;i<n;i++) { t[i]=sc.nextInt(); a[i]=sc.nextInt(); b[i]=sc.nextInt(); if(a[i]==b[i] && a[i]==1) { arr.add(t[i]); t[i]=-1; } if(a[i]==1) { ac++; } if(b[i]==1) { bc++; } } if(ac<k || bc<k) { System.out.println("-1"); System.exit(0); } long sum=0; Collections.sort(arr); if(arr.size()>=k) { for(int i=0;i<k;i++) { sum+=arr.get(i); } System.out.println(sum); } else{ for(int i=0;i<arr.size();i++) { sum+=arr.get(i); acount++; bcount++; } ArrayList<Integer> arra=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(a[i]==1 && acount<k && t[i]!=-1) { arra.add(t[i]); acount++; } if(acount>=k) { break; } } ArrayList<Integer> arrb=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(b[i]==1 && bcount<k && t[i]!=-1) { arrb.add(t[i]); bcount++; } if(bcount>=k) { break; } } Collections.sort(arra); Collections.sort(arrb); for(int i=0;i<k-arr.size();i++) { sum+=arra.get(i)+arrb.get(i); } System.out.println(sum); } sc.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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<long long> a; vector<long long> b; vector<long long> bo; for (int i = 0; i < n; ++i) { int tt, tt1, tt2; cin >> tt >> tt1 >> tt2; if (tt1 == 1 && tt2 == 1) bo.push_back(tt); else if (tt1 == 1) b.push_back(tt); else if (tt2 == 1) a.push_back(tt); } sort(bo.begin(), bo.end()); long long sum = 0; int l1 = 0; if (bo.size() > k) l1 = k; else l1 = bo.size(); for (int i = 0; i < l1; ++i) { sum += bo[i]; } long long kl1 = k - l1; if (kl1 > a.size() || kl1 > b.size()) { cout << "-1\n"; return 0; } if (kl1 > 0) { sort(a.begin(), a.end()); sort(b.begin(), b.end()); } else { cout << sum << "\n"; return 0; } long long p1 = 0, p2 = 0; while (p1 < kl1 || p2 < kl1) { if (a[p1] <= b[p2] && p1 < kl1) { sum += a[p1]; p1++; } else if (p2 < kl1) { sum += b[p2]; p2++; } else if (p1 < kl1) { sum += a[p1]; p1++; } } cout << sum << "\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" ] }
IN-CORRECT
python3
import sys from collections import defaultdict as dd from collections import deque from fractions import Fraction as f from copy import * from bisect import * from heapq import * from math import * from itertools import permutations def eprint(*args): print(*args, file=sys.stderr) zz=1 #sys.setrecursionlimit(10**6) if zz: input=sys.stdin.readline else: sys.stdin=open('input.txt', 'r') sys.stdout=open('all.txt','w') def li(): return [int(x) for x in input().split()] def fi(): return int(input()) def si(): return list(input().rstrip()) def mi(): return map(int,input().split()) def gh(): sys.stdout.flush() def graph(n,m): for i in range(m): x,y=mi() a[x].append(y) a[y].append(x) def bo(i): return ord(i)-ord('a') n,m,k=mi() a=[] rr=[] for i in range(n): p=li() a.append(p+[i+1]) if p[1]+p[2]==2: rr.append(p+[i+1]) a.sort() rr.sort() c=d=ans=0 c1=[] d1=[] r=0 l=[] #print(a) for i in range(n): if a[i][1:3]==[0,1]: if d+r>=k: continue ans+=a[i][0] d1.append([a[i][0],a[i][3]]) d+=1 elif a[i][1:3]==[1,0]: if c+r>=k: continue ans+=a[i][0] c1.append([a[i][0],a[i][3]]) c+=1 elif a[i][1:3]==[1,1]: #ans+=a[i][0] if c+r>=k and d+r>=k and len(c1)>0 and len(d1)>0: #print("lol") if c1[-1][0]+d1[-1][0]>a[i][0]: #print("LOL",ans) r+=1 c-=1 d-=1 ans-=(c1[-1][0]+d1[-1][0]) l.append([a[i][0],a[i][3]]) ans+=a[i][0] c1.pop() d1.pop() continue if c+r<k or d+r<k: r+=1 ans+=a[i][0] l.append([a[i][0],a[i][3]]) if c+r>k: ans-=c1[-1][0] c-=1 c1.pop() #l.append([a[i][0],a[i][3]]) #ans+=a[i][0] if d+r>k: ans-=d1[-1][0] d-=1 d1.pop() #ans+=a[i][0] #print(c+r,d+r,r,ans,c1,d1,l) if r>=k: break fin=c1+d1+l #print(c1,d1,l) if not (c+r>=k and d+r>=k): print(-1) exit(0) if len(fin)<m: dd={} #print("LOL") for i in fin: dd[i[1]]=1 j=m-len(fin) i=0 #print(ans,j,fin) #print(dd) while j>0 and i<n: if a[i][3] in dd: i+=1 continue ans+=a[i][0] dd[a[i][3]]=1 i+=1 j-=1 print(ans if c+r>=k and d+r>=k and j==0 else -1) if c+r>=k and d+r>=k and j==0: for i in dd: print(i,end=' ') else: j=len(fin)-m dd={} for i in fin: dd[i[1]]=1 for i in range(len(rr)): if j==0: break z=0 if len(c1) and len(d1): ans+=rr[i][0]-c1[-1][0]-d1[-1][0] dd[c1[-1][1]]=0 dd[d1[-1][1]]=0 dd[rr[i][3]]=1 c1.pop() d1.pop() j-=1 if len(c1) and c1[-1][0]>rr[i][0]: dd[c1[-1][1]]=0 dd[rr[i][3]]=1 c1.pop() if len(d1) and d1[-1][0]>rr[i][0]: dd[d1[-1][1]]=0 dd[rr[i][3]]=1 d1.pop() print(ans if j==0 else -1) if ans==82207: print("LOL") if j==0: for i in dd: if dd[i]==1: print(i,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" ] }
IN-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(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) { if(n==6561) out.println("cant increase curm"); 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) { if(n==6561) out.println("cant decrease curm"); 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) { if(n==6561) out.println("final"); 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.remove(); else return p; } return null; } static pair high(PriorityQueue<pair> pq) { while(!pq.isEmpty()) { pair p=pq.peek(); if(!p.inc) pq.remove(); 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" ] }
IN-CORRECT
python2
from __future__ import division, print_function # import threading # threading.stack_size(2**27) import sys sys.setrecursionlimit(10**4) # sys.stdin = open('inpy.txt', 'r') # sys.stdout = open('outpy.txt', 'w') from sys import stdin, stdout import bisect #c++ upperbound import math import heapq i_m=9223372036854775807 def modinv(n,p): return pow(n,p-2,p) def cin(): return map(int,sin().split()) def ain(): #takes array as input return list(map(int,sin().split())) def sin(): return input() def inin(): return int(input()) import math def GCD(x, y): x=abs(x) y=abs(y) if(min(x,y)==0): return max(x,y) while(y): x, y = y, x % y return x def Divisors(n) : l = [] for i in range(1, int(math.sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) return l prime=[] def SieveOfEratosthenes(n): global prime prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 f=[] for p in range(2, n): if prime[p]: f.append(p) return f q=[] """*******************************************************""" def main(): n,m=cin() a=[] b=[] c=[] for _ in range(n): i,j,k=cin() if j==1 and k==1: a.append(i) elif i==1: b.append(i) elif j==1: c.append(i) b.sort() c.sort() x=min(len(b),len(c)) for i in range(x): a.append(c[i]+b[i]) # print(a) if len(a)<m: print(-1) else: print(sum(a[:m])) ######## Python 2 and 3 footer by Pajenegod and c1729 # Note because cf runs old PyPy3 version which doesn't have the sped up # unicode strings, PyPy3 strings will many times be slower than pypy2. # There is a way to get around this by using binary strings in PyPy3 # but its syntax is different which makes it kind of a mess to use. # So on cf, use PyPy2 for best string performance. py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' # Read all remaining integers in stdin, type is given by optional argument, this is fast def readnumbers(zero = 0): conv = ord if py2 else lambda x:x A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read() try: while True: if s[i] >= b'R' [0]: numb = 10 * numb + conv(s[i]) - 48 elif s[i] == b'-' [0]: sign = -1 elif s[i] != b'\r' [0]: A.append(sign*numb) numb = zero; sign = 1 i += 1 except:pass if s and s[-1] >= b'R' [0]: A.append(sign*numb) return A # threading.Thread(target=main).start() 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Node { long long cnt, sum; Node operator+(Node n) const { return {cnt + n.cnt, sum + n.sum}; } }; long long N, M, K; vector<pair<long long, long long> > v[2][2], w; Node seg[33000]; bool vis[10101]; void Update(long long idx, long long val, long long n, long long l, long long r) { if (r < idx || idx < l) return; if (l == r) { seg[n].cnt += 1 * val; seg[n].sum += r * val; return; } long long mid = l + r >> 1; Update(idx, val, n << 1, l, mid); Update(idx, val, n << 1 | 1, mid + 1, r); seg[n].cnt = seg[n << 1].cnt + seg[n << 1 | 1].cnt; seg[n].sum = seg[n << 1].sum + seg[n << 1 | 1].sum; } Node Query(long long L, long long R, long long n, long long l, long long r) { if (r < L || R < l) return {0, 0}; if (L <= l && r <= R) return seg[n]; long long mid = l + r >> 1; return Query(L, R, n << 1, l, mid) + Query(L, R, n << 1 | 1, mid + 1, r); } long long Kth(long long k, long long n, long long l, long long r) { if (l == r) return r; long long mid = l + r >> 1; if (k <= seg[n << 1].cnt) return Kth(k, n << 1, l, mid); return Kth(k - seg[n << 1].cnt, n << 1 | 1, mid + 1, r); } signed main() { scanf("%lld %lld %lld", &N, &M, &K); for (long long i = 1; i <= N; i++) { long long t, a, b; scanf("%lld %lld %lld", &t, &a, &b); v[a][b].push_back({t, i}); w.push_back({t, i}); Update(t, 1, 1, 0, 10000); } if (v[1][1].size() + v[1][0].size() < K || v[1][1].size() + v[0][1].size() < K) { printf("-1\n"); return 0; } for (long long i = 0; i < 2; i++) for (long long j = 0; j < 2; j++) sort(v[i][j].begin(), v[i][j].end()); sort(w.begin(), w.end()); long long S11 = 0, S10 = 0, S01 = 0, j = 0, k = 0; for (long long i = 0; i < min(K, (long long)v[1][1].size()); i++) { S11 += v[1][1][i].first; Update(v[1][1][i].first, -1, 1, 0, 10000); } long long res = 2100000000, idx = -1; for (long long i = min(K, (long long)v[1][1].size()); i >= 0; i--) { if (v[1][0].size() < K - i || v[0][1].size() < K - i) break; if (i + K - i + K - i > M) continue; while (j < K - i) { S10 += v[1][0][j].first; Update(v[1][0][j].first, 1, 1, 0, 10000); j++; } while (k < K - i) { S01 += v[0][1][k].first; Update(v[0][1][k].first, 1, 1, 0, 10000); k++; } long long cnt, pos, kcnt, add; cnt = M - (i + K - i + K - i); if (cnt > 0) { pos = Kth(cnt, 1, 0, 10000); kcnt = cnt - Query(0, pos - 1, 1, 0, 10000).cnt; add = Query(0, pos - 1, 1, 0, 10000).sum + kcnt * pos; } else pos = kcnt = add = 0; long long val = S11 + S10 + S01 + add; if (res > val) { res = val; idx = i; } if (i) { S11 -= v[1][1][i - 1].first; Update(v[1][1][i - 1].first, -1, 1, 0, 10000); } } if (idx == -1) { printf("-1\n"); return 0; } printf("%lld\n", res); vector<long long> ans; for (long long i = 0; i < idx; i++) { ans.push_back(v[1][1][i].second); vis[v[1][1][i].second] = true; } for (long long i = 0; i < K - idx; i++) { ans.push_back(v[1][0][i].second); vis[v[1][0][i].second] = true; } for (long long i = 0; i < K - idx; i++) { ans.push_back(v[0][1][i].second); vis[v[0][1][i].second] = true; } long long cnt = M - (idx + K - idx + K - idx); for (long long i = 0; i < w.size(); i++) { if (vis[w[i].second]) continue; if (cnt > 0) { ans.push_back(w[i].second); vis[w[i].second] = true; cnt--; } } for (long long x : ans) printf("%lld ", x); 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" ] }
IN-CORRECT
python3
def process(a, b, k): # a is constraining and should be processed first result = 0 bIndices = {i for _, i in b} used = set() aNeeded, bNeeded = k, k for val, i in a: if aNeeded == 0: break if i in bIndices: bNeeded -= 1 aNeeded -= 1 used.add(i) result += val for val, i in b: if bNeeded == 0: break if i in used: continue result += val bNeeded -= 1 return result n, k = map(int, input().split()) aVals, bVals = [], [] countA, countB = 0, 0 for i in range(n): t, a, b = map(int, input().split()) countA += a countB += b if a: aVals.append((t, i)) if b: bVals.append((t, i)) if countA < k or countB < k: print(-1) else: aVals.sort() bVals.sort() if countA <= countB: print(process(aVals, bVals, k)) else: print(process(bVals, aVals, 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Greedybooks { public static Book bothBook(ArrayList<Book> chosen) { if (chosen.isEmpty()) { return null; } for (int i = chosen.size() - 1; i > -1; i--) { if (chosen.get(i).a == 1 && chosen.get(i).b == 1) { return chosen.get(i); } } return null; } public static int index(ArrayList<Book> chosen) { if (chosen.isEmpty()) { return -1; } for (int i = chosen.size() - 1; i > -1; i--) { if (chosen.get(i).a == 1 && chosen.get(i).b == 1) { return i; } } return -1; } public static void main(String[] args) throws IOException { BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); String s1 = b.readLine(); String[] a = s1.split(" "); int n = Integer.parseInt(a[0]); int m = Integer.parseInt(a[1]); int k = Integer.parseInt(a[2]); ArrayList<Book> bk = new ArrayList<Book>(); ArrayList<Book> both = new ArrayList<Book>(); ArrayList<Book> ai = new ArrayList<Book>(); ArrayList<Book> bi = new ArrayList<Book>(); ArrayList<Book> neither = new ArrayList<Book>(); for (int i = 0; i < n; i++) { String s = b.readLine(); String[] a1 = s.split(" "); Book bok = new Book(Integer.parseInt(a1[0]), Integer.parseInt(a1[1]), Integer.parseInt(a1[2])); bk.add(bok); int y = Integer.parseInt(a1[1]); int z = Integer.parseInt(a1[2]); if (y == 1 && z == 1) { both.add(bok); } if (y == 0 && z == 0) { neither.add(bok); } if (y == 1 && z == 0) { ai.add(bok); } if (y == 0 && z == 1) { bi.add(bok); } } boolean bar = false; ArrayList<Book> bky = (ArrayList<Book>) bk.clone(); Collections.sort(bk, new Sorter()); Collections.sort(both, new Sorter()); Collections.sort(bi, new Sorter()); Collections.sort(ai, new Sorter()); Collections.sort(neither, new Sorter()); ArrayList<Book> chosen = new ArrayList<Book>(); int time = 0; int x = 0; boolean bal = false; while (x < k) { if (both.isEmpty() && !ai.isEmpty() && !bi.isEmpty() && chosen.size() + 1 < m) { chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); x++; bal = true; } if (!both.isEmpty() && (ai.isEmpty() || bi.isEmpty()) && chosen.size() < m) { chosen.add(both.get(0)); time = time + both.get(0).getT(); both.remove(0); x++; bal = true; } if (!both.isEmpty() && !ai.isEmpty() && !bi.isEmpty()) { if (both.get(0).getT() >= ai.get(0).getT() + bi.get(0).getT() && chosen.size() + 1 < m) { chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); x++; bal = true; } else { if (chosen.size() < m) { chosen.add(both.get(0)); time = time + both.get(0).getT(); both.remove(0); x++; bal = true; } } } if (!bal) { break; } else { bal = false; } } if (x < k) { System.out.println(-1); } else { for (int i = 0; i < bk.size() && chosen.size() < m; i++) { if (!chosen.contains(bk.get(i))) { if (bothBook(chosen)!=null && !ai.isEmpty() && !bi.isEmpty() && bk.get(i).getT() + bothBook(chosen).getT() > ai.get(0).getT() + bi.get(0).getT()) { time = time - chosen.get(index(chosen)).getT(); chosen.remove(index(chosen)); chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); i--; } else { chosen.add(bk.get(i)); time = time + bk.get(i).getT(); } } } if (chosen.size() < m) { System.out.println(-1); } else { System.out.println(time); for (int i = 0; i < chosen.size(); i++) { System.out.print(bky.indexOf(chosen.get(i)) + 1 + " "); } } } } } class Book { int t; int a; int b; public Book(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } public int getT() { return this.t; } } class Sorter implements Comparator<Book> { @Override public int compare(Book o1, Book o2) { return o1.getT() - o2.getT(); } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; vector<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()); sort(b.begin(), b.end()); 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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class E1{ 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; } } public static void main(String[] args) throws NumberFormatException, IOException { FastReader s=new FastReader(); int n=s.nextInt(); int k=s.nextInt(); int counta=0,countb=0,countab=0,ans=0,temp1,temp2,temp3; ArrayList<Integer> arrab = new ArrayList<Integer>(); ArrayList<Integer> arra = new ArrayList<Integer>(); ArrayList<Integer> arrb = new ArrayList<Integer>(); for(int i=0;i<n;i++){ temp1=s.nextInt(); temp2=s.nextInt(); temp3=s.nextInt(); if(temp2==1&&temp3==1){ arrab.add(temp1); countab++; } else if(temp2==1&&temp3==0){ arra.add(temp1); counta++; } else if(temp2==0&&temp3==1){ arrb.add(temp1); countb++; } } Collections.sort(arra);Collections.sort(arrb);Collections.sort(arrab); temp1=0;temp2=0;temp3=0; if((counta+countab)<k||(countb+countab)<k){ ans=-1; } else if(counta==0||countb==0){ for(int i=0;i<k;i++) ans+=arrab.get(i); } else{ int i=0,j=0,sum=0;temp3=0;int ext=0; while(temp1<k||temp2<k){ sum=0;ext=0; if(i<counta){ sum+=arra.get(i); ext=1; } if(j<countb){ sum+=arrb.get(j); ext=1; } if(!arrab.isEmpty()&&temp3<countab&&(arrab.get(temp3)<=sum||ext==0)){ ans+=arrab.get(temp3); temp3++; temp1++;temp2++; } else{ if(temp1<k&&i<counta){ ans+=arra.get(i); i++; temp1++; } if(temp2<k&&j<countb){ ans+=arrb.get(j); j++; temp2++; } } } } System.out.println(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" ] }
IN-CORRECT
python3
# from math import log,floor # from mymodule import input n,k = map(int,input().split()) l1 = [] l2 = [] for i in range(n): a,b,c = map(int,input().split()) r = False if b!=0: l1.append(a) r = True if c!=0: if b!=0 and r: l2.append(0) else: l2.append(a) # print(sum(l1[0:k])+sum(l2[0:k])) if len(l1)>=k and len(l2)>=k: l1.sort() l2.sort() print(sum(l1[0:k])+sum(l2[0: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" ] }
IN-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]) 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 = [] resulta = [] resultb = [] if k > 0: aaa = Alice + Both aaa.sort(key=lambda x: x[0]) if len(aaa) >= k: resulta = aaa[:k] else: print(-1) exit() col_totals1 = [sum(x) for x in zip(*resulta)] xx = col_totals1[2] yy = k - xx Both = Both[xx:] Alice = Alice[yy:] k = k - xx if k > 0: bbb = Bob + Both bbb.sort(key=lambda x: x[0]) if len(bbb) >= k: resultb = bbb[:k] else: print(-1) exit() col_totals2 = [sum(x) for x in zip(*resultb)] xx = col_totals1[1] yy = k - xx Both = Both[xx:] Bob = Bob[yy:] q = len(resultb) + len(resulta) q = m - q All = Both + Alice + Bob + none All.sort(key=lambda x: x[0]) result = All[:q] result = resulta + resultb + result + tresult result.sort(key=lambda x: x[0]) print(sum(row[0] for row in result)) 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" ] }
IN-CORRECT
java
import java.util.*; public class pract{ public static int mysol(int n){ int arr[]=new int[n]; int rr=0; for(int i=0;i<n;i++){ arr[i]=i; rr+=i; } return rr; } static class pair implements Comparable<pair>{ int val; int ind; boolean b; pair(int v,int i,boolean bb){ val=v; ind=i; b=bb; } @Override public int compareTo(pair o) { // TODO Auto-generated method stub return this.val-o.val; } } public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n=scn.nextInt(); int m=scn.nextInt(); int k=scn.nextInt(); PriorityQueue<pair> a=new PriorityQueue<>(); PriorityQueue<pair> b=new PriorityQueue<>(); PriorityQueue<pair> ab=new PriorityQueue<>(); PriorityQueue<pair> faltu=new PriorityQueue<>(); for(int i=0;i<n;i++){ int t=scn.nextInt(); int f=scn.nextInt(); int s=scn.nextInt(); if(f==1 && s==1){ ab.add(new pair(t,i+1,true)); }else if(f==1){ a.add(new pair(t,i+1,false)); }else if(s==1){ b.add(new pair(t,i+1,false)); }else{ faltu.add(new pair(t,i+1,false)); } } int tot=0; int k1=k,k2=k,c=0; boolean f=false; ArrayList<Integer> al=new ArrayList<>(); while(k>0){ if(!a.isEmpty() && !b.isEmpty() && !ab.isEmpty() && (a.peek().val+b.peek().val)>=ab.peek().val){ pair ff=ab.poll(); tot+=ff.val; al.add(ff.ind); c++; } else if(m-c>=2 && !a.isEmpty() && !b.isEmpty() && !ab.isEmpty() && (a.peek().val+b.peek().val)<ab.peek().val){ pair ff=a.poll(); pair ss=b.poll(); tot+=ff.val; tot+=ss.val; al.add(ff.ind); al.add(ss.ind); c+=2; } else if(m-c>=1 && !ab.isEmpty()){ pair ff=ab.poll(); tot+=ff.val; al.add(ff.ind); c++; }else if(m-c>=2 && !a.isEmpty() && !b.isEmpty()){ pair ff=a.poll(); pair ss=b.poll(); tot+=ff.val; tot+=ss.val; al.add(ff.ind); al.add(ss.ind); c+=2; }else{ f=true; break; } k--; } int rr=m-(c); while(!ab.isEmpty()){ faltu.add(ab.poll()); } while(!a.isEmpty()){ faltu.add(a.poll()); } while(!b.isEmpty()){ faltu.add(b.poll()); } while(rr>0){ pair pp=faltu.poll(); tot+=pp.val; al.add(pp.ind); rr--; } if(f){ System.out.println(-1); }else{ System.out.println(tot); for(int i=0;i<al.size();i++){ System.out.print(al.get(i)+" "); } System.out.println(); } } }
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" ] }
IN-CORRECT
java
import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { int [] both = new int [200010]; int [] Alic = new int [200010]; int [] Bob = new int [200010]; int x = 0, y = 0, z = 0; Scanner s = new Scanner(System.in); int n, k; n = s.nextInt(); k = s.nextInt(); for(int i = 0; i < n; ++ i) { int t, a, b; t = s.nextInt(); a = s.nextInt(); b = s.nextInt(); if(a == 1 && b == 1) both[x++] = t; else if(a == 1 && b == 0) Alic[y ++] = t; else if(a == 0 && b == 1) Bob[z ++] = t; } Arrays.sort(both,0,x); Arrays.sort(Alic,0,y); Arrays.sort(Bob,0,z); // for(int i = 0; i < x; ++ i) // System.out.println(both[i]); // System.out.println("----------------"); // for(int i = 0; i < y; ++ i) // System.out.println(Alic[i]); // System.out.println("----------------"); // for(int i = 0; i < z; ++ i) // System.out.println(Bob[i]); // System.out.println("----------------"); if(Math.max(y, z) + x < k) System.out.print(-1); else { int sum = 0, t = 0; int ans1 = 0, ans2 = 0, ans3 = 0; while(t < k) { if(ans1 >= x) { sum += Alic[ans2 ++] + Bob[ans3 ++]; } else if(ans2 >= y || ans3 >= z) { sum += both[ans1 ++]; } else { if(Alic[ans2] + Bob[ans3] > both[ans1]) sum += both[ans1 ++]; else sum += Alic[ans2 ++] + Bob[ans3 ++]; } t ++; } System.out.print(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" ] }
IN-CORRECT
cpp
// while( !(succeed == try()) ); #include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha #define int long long int #define loop(i,s,e) for(int i=s; i<e; i++) #define endl '\n' #define vii vector<int> #define pii pair<int, int> #define ff first #define ss second #define N 100001 #define pb push_back #define INF LLONG_MAX #define inf INT_MAX void INP(); #define bug(...) __f (#__VA_ARGS__, __VA_ARGS__) #define rtn if(0)return; template <typename Arg1> void __f (const char* name, Arg1&& arg1) { rtn cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) { rtn const char* comma = strchr (names + 1, ','); cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...); } typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha void alpha() { int n, k; cin>>n>>k; vector<int> a, b, both; int cnt1=0, cnt2=0; loop(i,0,n) { int t, l1, l2; cin>>t>>l1>>l2; if(l1 and !l2) { a.pb(t); cnt1++; } else if(!l1 and l2) { b.pb(t); cnt2++; } else if(l1 and l2) { both.pb(t); cnt1++; cnt2++; } } if(cnt1 < k or cnt2 < k) { cout<<-1<<endl; return; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(both.begin(), both.end()); loop(i,1,a.size()) a[i] += a[i-1]; loop(i,1,b.size()) b[i] += b[i-1]; loop(i,1,both.size()) both[i] += both[i-1]; int ans = inf; loop(i,0,both.size()) { int cnt = i+1; int t = both[i]; if(cnt >= k) { ans = min(ans, t); break; } t += (k-cnt-1 < a.size() ? a[k-cnt-1] : inf); t += (k-cnt-1 < b.size() ? b[k-cnt-1] : inf); ans = min(ans, t); } cout<<ans<<endl; } int32_t main(){ ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); INP(); int t=1; //cin>>t; while(t--) alpha(); return 0; } inline void INP() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n; long long k; struct book { bool a; bool b; int time; }; book B[100000]; bool compare(book x, book y) { return x.time > y.time; } void solve() { cin >> n; cin >> k; int x, y, z; int as = 0; int bs = 0; for (int i = 0; i < n; i++) { cin >> x >> y >> z; B[i].a = y; as += y; bs += z; B[i].b = z; B[i].time = x; } if (as < k || bs < k) { cout << -1 << endl; return; } sort(B, B + n, compare); long long tot = 0; int ak = 0; int bk = 0; stack<int> a; stack<int> b; for (int i = 0; i < n; i++) { if (B[i].a && B[i].b) continue; if (ak < k && B[i].a) { ak++; a.push(i); tot += B[i].time; } if (bk < k && B[i].b) { bk++; tot += B[i].time; b.push(i); } } int lst = 0; for (int i = 0; i < n; i++) { if (ak < k && B[i].a && B[i].b) { ak++; a.push(i); tot += B[i].time; if (b.size() && bk == k) { tot -= B[b.top()].time; b.pop(); } if (bk < k) bk++; lst = i; B[i].time = INT_MAX; } else if (bk < k && B[i].a && B[i].b) { bk++; b.push(i); tot += B[i].time; if (a.size() && ak == k) { tot -= B[a.top()].time; a.pop(); } lst = i; if (ak < k) ak++; B[i].time = INT_MAX; } } for (int i = lst + 1; i < n; i++) { if (a.size() == 0 || b.size() == 0) break; int m = a.top(); int n = b.top(); if (B[i].a && B[i].b) { if (B[i].time < B[m].time + B[n].time) { tot -= (B[m].time + B[n].time); tot += B[i].time; } } } cout << tot << endl; return; } int main() { int t; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n; long long k; struct book { bool a; bool b; int time; }; book B[100000]; bool compare(book x, book y) { return x.time > y.time; } void solve() { cin >> n; cin >> k; int x, y, z; int as = 0; int bs = 0; int mx = 0; for (int i = 0; i < n; i++) { cin >> x >> y >> z; B[i].a = y; as += y; bs += z; B[i].b = z; B[i].time = x; mx = max(mx, B[i].time); } if (as < k || bs < k) { cout << -1 << endl; return; } sort(B, B + n, compare); long long tot = 0; int ak = 0; int bk = 0; stack<int> a; stack<int> b; for (int i = 0; i < n; i++) { if (B[i].a && B[i].b) continue; if (ak < k && B[i].a) { ak++; a.push(i); tot += B[i].time; } if (bk < k && B[i].b) { bk++; tot += B[i].time; b.push(i); } } int lst = 0; for (int i = 0; i < n; i++) { if (ak < k && B[i].a && B[i].b) { ak++; tot += B[i].time; if (b.size() && bk == k) { tot -= B[b.top()].time; b.pop(); } if (bk < k) bk++; lst = i; } else if (bk < k && B[i].a && B[i].b) { bk++; tot += B[i].time; if (a.size() && ak == k) { tot -= B[a.top()].time; a.pop(); } lst = i; if (ak < k) ak++; } } for (int i = lst + 1; i < n; i++) { if (a.size() == 0 || b.size() == 0) break; int m = a.top(); int n = b.top(); if (B[i].a && B[i].b) { if (B[i].time < B[m].time + B[n].time) { tot -= (B[m].time + B[n].time); tot += B[i].time; a.pop(); b.pop(); } } } cout << tot << endl; return; } int main() { int t; 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" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) l,l1,l2=[],[],[] for i in range(n): t,a,b=map(int,input().split()) if a==1 and b==1: l.append(t) if a==1 and b==0: l1.append(t) if a==0 and b==1: l2.append(t) l.sort() l1.sort() l2.sort() c,ans,a,b=0,0,0,0 while a<k or b<k: if l and l1 and l2: if l[0]<=l1[0]+l1[0]: ans+=l.pop(0) a+=1 b+=1 if a==k: l1=[] if b==k: l2=[] else: ans+=l1.pop(0) ans+=l2.pop(0) a+=1 b+=1 if a==k: l1=[] if b==k: l2=[] elif a<k and l and l1: if l[0]>=l1[0]: ans+=l.pop(0) a+=1 b+=1 else: ans+=l1.pop(0) a+=1 elif b<k and l and l2: if l[0]>=l2[0]: ans+=l.pop(0) b+=1 a+=1 else: ans+=l2.pop(0) b+=1 elif l: ans+=l.pop(0) a+=1 b+=1 elif l1: ans+=l1.pop(0) a+=1 elif l2: ans+=l2.pop(0) b+=1 else: c=1 break if c==1: 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { long long n, m, k; cin >> n >> m >> k; m -= k; vector<pair<long long, vector<long long>>> s, a, b, c, ans; set<long long> ss; for (long long i = 0; i < n; i++) { long long t, aa, bb; cin >> t >> aa >> bb; if (aa + bb == 2) s.push_back({t, vector<long long>{i + 1}}); else if (aa) a.push_back({t, vector<long long>{i + 1}}); else if (bb) b.push_back({t, vector<long long>{i + 1}}); c.push_back({t, vector<long long>{i + 1}}); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long i = 0, j, cnt = 0, kk = k; while (i < a.size() and i < b.size()) { s.push_back({a[i].first + b[i].first, vector<long long>{a[i].second[0], b[i].second[0]}}); i++; } sort(c.begin(), c.end()); sort(s.begin(), s.end()); for (auto i : s) { if (kk and i.second.size() == 1) { ans.push_back(i); cnt += i.first; ss.insert(i.second[0]); kk--; } else if (kk * m and i.second.size()) { ans.push_back(i); cnt += i.first; ss.insert(i.second[0]); ss.insert(i.second[1]); m--, kk--; } if (!kk) break; } for (i = 0, j = 0; i < n and j < m; i++) if (ss.find(c[i].second[0]) == ss.end()) { cnt += c[i].first; ss.insert(c[i].second[0]); j++; } if (ans.size() < k || j < m) { cout << -1 << endl; return; } cout << cnt << endl; for (long long i : ss) cout << i << ' '; cout << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); cerr << "[Execution : " << (1.0 * clock()) / CLOCKS_PER_SEC << "s]\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" ] }
IN-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 = 1000000000 p = 0 tempo = 0 while(p <= len(c) and p<= k): if(p>0): tempo = c[p-1] 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" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class books { public static void main (String[] args) throws IOException { Scanner in = new Scanner(System.in); // int cases = in.nextInt(); //do something int n = in.nextInt(); int k = in.nextInt(); int booksLeft = k; int timeUsed = 0; int aliceCount = 0; int bobCount = 0; Book[] books = new Book[n]; for (int j = 0; j < n; j++) { int t = in.nextInt(); // System.out.println("stored"); int a = in.nextInt(); // System.out.println("stored"); int b = in.nextInt(); // System.out.println("stored"); aliceCount += a; bobCount += b; books[j] = new Book(t, a, b); // System.out.println("here"); } for (Book book: books) { if (book.good) { timeUsed += book.time; booksLeft--; aliceCount--; bobCount--; } if (booksLeft == 0) break; } // System.out.println("left"); if (booksLeft == 0) System.out.println(timeUsed); else if (booksLeft > aliceCount || booksLeft > bobCount) System.out.println(-1); else { Book[] a = new Book[aliceCount]; Book[] b = new Book[bobCount]; int aIndex = 0; int bIndex = 0; for (int i = 0; i < n; i++) { if (books[i].alice && books[i].usable) { a[aIndex] = books[i]; aIndex++; } else if (books[i].bob && books[i].usable) { b[bIndex] = books[i]; bIndex++; } } Arrays.sort(a); Arrays.sort(b); for (int i = 0; i < booksLeft; i++) { timeUsed += a[i].time; timeUsed += b[i].time; } System.out.println(timeUsed); } } } class Book implements Comparable<Book> { public boolean usable; public boolean good; public boolean alice; public boolean bob; public int time; public Book (int time, int a, int b) { good = (a + b == 2) ? true: false; usable = (a + b == 1) ? true: false; this.time = time; alice = (a == 1) ; bob = (b == 1); } public int compareTo (Book other) { return Integer.compare(time, other.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" ] }
IN-CORRECT
python3
#!/usr/bin/env python3 import sys import heapq input=sys.stdin.readline n,m,k=map(int,input().split()) arr=[list(map(int,input().split()))+[i+1] for i in range(n)] cnt1=0 cnt2=0 for t,a,b,i in arr: if a==1: cnt1+=1 if b==1: cnt2+=1 if cnt1<k or cnt2<k: print(-1) exit() arr=sorted(arr,key=lambda x:x[0]) arr=sorted(arr,reverse=True,key=lambda x:x[1]) ans=0 cnt=0 books=k choose=set() for i in range(k): choose.add(arr[i][3]) ans+=arr[i][0] if arr[i][2]==1: cnt+=1 if cnt!=k: q1=[] q2=[] q3=[] for t,a,b,i in arr[:k]: if a==1 and b==0: heapq.heappush(q1,(-t,i)) for t,a,b,i in arr[k:]: if a==1 and b==1: heapq.heappush(q2,(t,i)) if a==0 and b==1: heapq.heappush(q3,(t,i)) INF=10**18 while cnt<k: if len(q1)!=0: diff1=INF if len(q2)!=0: cost1,i1=heapq.heappop(q1) heapq.heappush(q1,(cost1,i1)) cost2,i2=heapq.heappop(q2) heapq.heappush(q2,(cost2,i2)) diff1=cost1+cost2 diff2=INF if len(q3)!=0: cost3,i3=heapq.heappop(q3) heapq.heappush(q3,(cost3,i3)) diff2=cost3 if diff1!=INF and diff2==INF: choose.add(i2) choose.discard(i1) ans+=diff1 heapq.heappop(q1) heapq.heappop(q2) elif diff1==INF and diff2!=INF: choose.add(i3) ans+=diff2 heapq.heappop(q3) books+=1 else: if diff1<=diff2: choose.add(i2) choose.discard(i1) ans+=diff1 heapq.heappop(q1) heapq.heappop(q2) else: choose.add(i3) ans+=diff2 heapq.heappop(q3) books+=1 else: diff1=INF if len(q2)!=0: cost2,i2=heapq.heappop(q2) heapq.heappush(q2,(cost2,i2)) diff1=cost2 diff2=INF if len(q3)!=0: cost3,i3=heapq.heappop(q3) heapq.heappush(q3,(cost3,i3)) diff2=cost3 if diff1!=INF and diff2==INF: choose.add(i2) ans+=diff1 heapq.heappop(q2) elif diff1==INF and diff2!=INF: choose.add(i3) ans+=diff2 heapq.heappop(q3) else: if diff1<=diff2: choose.add(i2) ans+=diff1 heapq.heappop(q2) else: choose.add(i3) ans+=diff2 heapq.heappop(q3) books+=1 cnt+=1 if books<=m: q4=[] for t,a,b,i in arr: if i in choose: continue else: heapq.heappush(q4,(t,i)) while books<m: tmp,i=heapq.heappop(q4) ans+=tmp choose.add(i) books+=1 print(ans) print(*list(choose)) else: q1=[] q2=[] q3=[] for t,a,b,i in arr: if i in choose: if a==1 and b==0: heapq.heappush(q1,(-t,i)) if a==0 and b==1: heapq.heappush(q2,(-t,i)) else: if a==1 and b==1: heapq.heappush(q3,(t,i)) while books>m: if len(q1)==0 or len(q2)==0 or len(q3)==0: print(-1) exit() cost1,i1=heapq.heappop(q1) cost2,i2=heapq.heappop(q2) cost3,i3=heapq.heappop(q3) ans+=cost3+(cost1+cost2) choose.discard(i1) choose.discard(i2) choose.add(i3) books-=1 print(ans) print(*list(choose))
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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class check5 { public static void main(String[] args) throws IOException{ Reader sc=new Reader(); PrintWriter out = new PrintWriter(System.out); int n=sc.nextInt(); long k=sc.nextLong(); long a[][]=new long[n][3]; ArrayList<Long> al=new ArrayList<>(); ArrayList<Long> bob=new ArrayList<>(); ArrayList<Long> both=new ArrayList<>(); for(int i=0;i<n;i++) { a[i][0]=sc.nextLong(); a[i][1]=sc.nextLong(); a[i][2]=sc.nextLong(); if(a[i][1]==1) { if(a[i][2]==1) both.add(a[i][0]); else al.add(a[i][1]); } else if(a[i][2]==1) bob.add(a[i][2]); } Collections.sort(al,Collections.reverseOrder()); Collections.sort(bob,Collections.reverseOrder()); Collections.sort(both,Collections.reverseOrder()); if(al.size()+both.size()<k || bob.size()+both.size()<k) { System.out.println(-1); return; } int ac=0; int bc=0; long ans=0; while(ac<k && bc<k) { int t1=al.size()-1; int t2=bob.size()-1; int t3=both.size()-1; if(t1>=0 && t2>=0 && t3>=0&& (al.get(t1)+bob.get(t2))>=2*both.get(t3)) { ans+=both.get(t3); both.remove(both.size()-1); } else if(t1<0 || t2<0) { ans+=both.get(t3); both.remove(both.size()-1); } else// if(t3<0) { ans+=al.get(t1)+bob.get(t2); al.remove(t1); bob.remove(t2); } ac+=1; bc+=1; } System.out.println(ans); out.flush(); } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() throws IOException{ 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 int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') 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 (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class B { static BufferedReader br; static PrintWriter pw; static int inf = (int) 1e9; static long[] memo; public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); ArrayList<pair> arr = new ArrayList<>(); ArrayList<pair> arr1 = new ArrayList<>(); ArrayList<pair> arr2 = new ArrayList<>(); ArrayList<triple> arr3 = new ArrayList<>(); ArrayList<pair> arr4 = new ArrayList<>(); for (int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); if (x == 1 && y == 1) arr.add(new pair(t, i)); else if (x == 1 && y == 0) { arr1.add(new pair(t, i)); arr4.add(new pair(t, i)); } else if (x == 0 && y == 1) { arr2.add(new pair(t, i)); arr4.add(new pair(t, i)); } else arr4.add(new pair(t, i)); } Collections.sort(arr1); Collections.sort(arr2); int min = Math.min(arr1.size(), arr2.size()); for (int i = 0; i < min; i++) { arr3.add(new triple(arr1.get(i).x + arr2.get(i).x, arr1.get(i).y, arr2.get(i).y)); } Collections.sort(arr); Collections.sort(arr3); Collections.sort(arr4); if (arr.size() + arr3.size() < k || m < k) pw.println(-1); else { long sum = 0; int i = 0; int j = 0; int z = 0; ArrayList<Integer> ans = new ArrayList<>(); HashSet<Integer> hs = new HashSet<>(); // System.out.println(arr3); // System.out.println(arr4); while (m > 0) { pair p1 = new pair(1000000000, -1); pair p3 = new pair(1000000000, -1); triple p2 = new triple(1000000000, -1, -1); if (k > 0) { if (i == arr.size()) { p2 = arr3.get(j); j++; m -= 2; sum += p2.x; ans.add(p2.y + 1); ans.add(p2.z + 1); hs.add(p2.y + 1); hs.add(p2.z + 1); } else if (j == arr3.size() && m > 1) { p1 = arr.get(i); i++; m--; sum += p1.x; ans.add(p1.y + 1); } else { p1 = arr.get(i); p2 = arr3.get(j); if (p1.x <= p2.x || m == 1) { i++; m--; sum += p1.x; ans.add(p1.y + 1); } else { j++; m -= 2; sum += p2.x; ans.add(p2.y + 1); ans.add(p2.z + 1); hs.add(p2.y + 1); hs.add(p2.z + 1); } } k--; } else { if (i != arr.size()) p1 = arr.get(i); if (j != arr3.size() && m > 1) p2 = arr3.get(j); if (z != arr4.size()) { p3 = arr4.get(z); while (z < arr4.size() && hs.contains(p3.y + 1)) { p3 = arr4.get(++z); } if (z == arr4.size()) p3 = new pair(1000000000, -1); } min = Math.min(p1.x, Math.min(p2.x, p3.x)); // System.out.println(i + " " + j + " " + z); // System.out.println(p1.x + " " + p2.x + " " + p3.x); if (min == p1.x) { p1 = arr.get(i); i++; m--; sum += p1.x; ans.add(p1.y + 1); } else if (min == p2.x && m > 1) { j++; m -= 2; sum += p2.x; ans.add(p2.y + 1); ans.add(p2.z + 1); } else { z++; m--; sum += p3.x; ans.add(p3.y + 1); // System.out.println(p3.y+1+" "+z); } // System.out.println(ans); } } pw.println(sum); for (int x : ans) pw.print(x + " "); } pw.flush(); } static int[][] ok; static boolean delete(int min, ArrayList<Integer> rem, TreeSet<Integer>[] hs, int i, ArrayList<Character> ans) { ArrayList<Integer> del = new ArrayList<>(); for (int k = 0; k < rem.size(); k++) { int x = rem.get(k); Integer idx2 = hs[x].higher(min); if (idx2 != null) { boolean f2 = true; for (int j = i + 1; j < 26; j++) { Integer temp = hs[j].lower(a.length); f2 &= temp == null || temp > idx2; } for (int j = k + 1; j < rem.size(); j++) { Integer temp = hs[rem.get(j)].lower(a.length); f2 &= temp == null || temp > idx2; } if (f2) { ans.add((char) ('a' + x)); min = idx2; del.add(x); } } } rem.removeAll(del); return del.size() > 0; } static void dfs(int x, int y) { if (valid(x, y) && a[x][y] != '*' && !vis[x][y]) { vis[x][y] = true; ok[x][y] = 1; dfs(x - 1, y); dfs(x + 1, y); dfs(x, y - 1); dfs(x, y + 1); } } static long dp(int r) { if (r == 0) return 1; if (r < 0) return 0; if (memo[r] != -1) return memo[r]; return memo[r] = dp(r - 1) + dp(r - 2) + dp(r - 3); } static char[][] a; static int n, m; static boolean valid(int x, int y) { return x > -1 && x < n && y > -1 && y < m; } static boolean[][] vis; static ArrayList<Edge> edgeList; static ArrayList<edge1>[] adj; static int V; static int kruskal() // O(E log E) { int mst = 0; Collections.sort(edgeList); UnionFind uf = new UnionFind(n); for (Edge e : edgeList) if (uf.union(e.u, e.v)) mst += e.w; return mst; } static class Edge implements Comparable<Edge> { int u, v, w; Edge(int a, int b, int c) { u = a; v = b; w = c; } public int compareTo(Edge e) { return w - e.w; } } static class edge1 implements Comparable<edge1> { int v, w; edge1(int b, int c) { v = b; w = c; } public int compareTo(edge1 e) { return w - e.w; } } static class UnionFind { int[] p, rank; UnionFind(int N) { p = new int[N]; rank = new int[N]; for (int i = 0; i < N; i++) p[i] = i; } int findSet(int x) { return p[x] == x ? x : (p[x] = findSet(p[x])); } boolean union(int x, int y) { x = findSet(x); y = findSet(y); if (x == y) return false; if (rank[x] > rank[y]) p[y] = x; else { p[x] = y; if (rank[x] == rank[y]) ++rank[y]; } return true; } } static class pair implements Comparable<pair> { int x; int y; public pair(int d, int u) { x = d; y = u; } public int compareTo(pair o) { return x - o.x; } @Override public String toString() { // TODO Auto-generated method stub return x+" "+y; } } static class triple implements Comparable<triple> { int x; int y; int z; public triple(int a, int b, int c) { x = a; y = b; z = c; } public int compareTo(triple o) { return x - o.x; } public String toString() { // TODO Auto-generated method stub return x+" "+y+" "+z; } } static int[] nxtarr() throws IOException { StringTokenizer st = new StringTokenizer(br.readLine()); int[] a = new int[st.countTokens()]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(st.nextToken()); } return a; } static long pow(long a, long e) // O(log e) { long res = 1; while (e > 0) { if ((e & 1) == 1) res *= a; a *= a; e >>= 1; } return res; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, 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" ] }
IN-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; } else if (cnta + cntc == k && cntb + cntc == k) { for (int i = (1); i <= (cnta); ++i) ans += a[i]; for (int i = (1); i <= (cntb); ++i) ans += b[i]; for (int i = (1); i <= (cntc); ++i) ans += c[i]; cout << ans << endl; return 0; } sort(a + 1, a + 1 + cnta); sort(b + 1, b + 1 + cntb); sort(c + 1, c + 1 + cntc); aa = 0, bb = 0; 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 (a[tt] < a[t] + b[t] && tt <= cntc) ans -= a[t], ans -= b[t], ans += a[tt], tt++, t--; cout << ans << endl; return 0; } int tt = 1; while (a[tt] < a[t] + b[t] && tt <= cntc) ans -= a[t], ans -= b[t], ans += a[tt], tt++, t--; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[205000]; int b[205000]; int cost[205000]; vector<pair<int, int> > pa; vector<pair<int, int> > pb; vector<pair<int, int> > pc; vector<pair<int, int> > pd; long long suma[205000]; long long sumb[205000]; long long sumc[205000]; long long sumd[205000]; long long ans; long long acnt; long long bcnt; long long dcnt; long long val(char id, int pos) { if (id == 'a') return suma[pos] - suma[pos - 1]; if (id == 'b') return sumb[pos] - sumb[pos - 1]; if (id == 'c') return sumc[pos] - sumc[pos - 1]; if (id == 'd') return sumd[pos] - sumd[pos - 1]; } int add() { if (acnt < pa.size() && bcnt < pb.size() && dcnt < pd.size()) { if (val('a', acnt + 1) <= min(val('b', bcnt + 1), val('d', dcnt + 1))) acnt++; else if (val('b', bcnt + 1) <= min(val('a', acnt + 1), val('d', dcnt + 1))) bcnt++; else if (val('d', dcnt + 1) <= min(val('a', acnt + 1), val('b', bcnt + 1))) dcnt++; } else if (acnt < pa.size() && bcnt < pb.size()) { if (val('a', acnt + 1) <= val('b', bcnt + 1)) acnt++; else bcnt++; } else if (bcnt < pb.size() && dcnt < pd.size()) { if (val('b', bcnt + 1) <= val('d', dcnt + 1)) bcnt++; else dcnt++; } else if (acnt < pa.size() && dcnt < pd.size()) { if (val('a', acnt + 1) <= val('d', dcnt + 1)) acnt++; else dcnt++; } else if (acnt < pa.size()) acnt++; else if (bcnt < pb.size()) bcnt++; else if (dcnt < pd.size()) dcnt++; else return -1; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = (1); i <= (n); i++) { scanf("%d%d%d", cost + i, a + i, b + i); if (a[i] == 1 && b[i] == 1) pc.push_back({cost[i], i}); else if (a[i] == 1) pa.push_back({cost[i], i}); else if (b[i] == 1) pb.push_back({cost[i], i}); else pd.push_back({cost[i], i}); } sort(pa.begin(), pa.end()); sort(pb.begin(), pb.end()); sort(pc.begin(), pc.end()); sort(pd.begin(), pd.end()); for (int i = 1; i <= (int)pa.size(); i++) suma[i] = suma[i - 1] + pa[i - 1].first; for (int i = 1; i <= (int)pb.size(); i++) sumb[i] = sumb[i - 1] + pb[i - 1].first; for (int i = 1; i <= (int)pc.size(); i++) sumc[i] = sumc[i - 1] + pc[i - 1].first; for (int i = 1; i <= (int)pd.size(); i++) sumd[i] = sumd[i - 1] + pd[i - 1].first; if (min(pa.size(), pb.size()) + pc.size() < k) { cout << "-1" << endl; return 0; } ans = min((int)pc.size(), k); acnt = max(k - ans, 0ll); bcnt = max(k - ans, 0ll); dcnt = 0; if (acnt + bcnt + ans > m) { cout << "-1" << endl; return 0; } while (acnt + bcnt + dcnt + ans < m) { if (add() == -1) { cout << "-1" << endl; return 0; } } long long cmp = sumc[ans] + suma[acnt] + sumb[bcnt] + sumd[dcnt]; int cur = ans - 1; int alim = acnt; int blim = bcnt; int dlim = dcnt; while (cur >= 0) { if (acnt + cur < k && bcnt + cur < k) { long long del = 2ll * k - acnt - bcnt - 2ll * cur; if (dcnt + 1 <= del) break; while (dcnt && acnt + cur < k && acnt < pa.size()) { acnt++, dcnt--; } while (dcnt && bcnt + cur < k && bcnt < pb.size()) { bcnt++, dcnt--; } } if (acnt + cur == k - 1) acnt++; else if (bcnt + cur == k - 1) bcnt++; else { if (add() == -1) break; } if (pa.size() < acnt || pb.size() < bcnt || pd.size() < dcnt) break; long long now = sumc[cur] + suma[acnt] + sumb[bcnt] + sumd[dcnt]; if (now < cmp) { cmp = now; alim = acnt; blim = bcnt; dlim = dcnt; ans = cur; } cur--; } cout << cmp << endl; for (int i = 0; i < alim; i++) printf("%d ", pa[i].second); for (int i = 0; i < blim; i++) printf("%d ", pb[i].second); for (int i = 0; i < dlim; i++) printf("%d ", pd[i].second); for (int i = 0; i < ans; i++) printf("%d ", pc[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" ] }
IN-CORRECT
java
//import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; import java.util.*; import java.io.*; import java.math.BigInteger; public class Solution { static class Pair<A, B> { A parent; B rank; Pair(A parent, B rank) { this.rank = rank; this.parent = parent; } } static class Node { int ind; int value; long ans; Node(int i) { ind=i; value=0; ans=0; } } static int m=1000000007; static ArrayList<Integer> graph[]; public static void main(String[] args) throws IOException { FastReader s1 = new FastReader(); StringBuilder sb = new StringBuilder(); int n=s1.I(); int k=s1.I(); ArrayList<Integer> com=new ArrayList<>(); ArrayList<Integer> fir=new ArrayList<>(); ArrayList<Integer> sec=new ArrayList<>(); for(int i=0;i<n;i++) { int a=s1.I(); int b=s1.I(); int c=s1.I(); if(b==1 && c==1) com.add(a); else if(b==1) { fir.add(a); } else sec.add(a); } if(com.size()+fir.size()<k || com.size()+sec.size()<k) { System.out.println("-1"); System.exit(0); } Collections.sort(com); Collections.sort(fir); Collections.sort(sec); int x1=0; int x2=0; int x3=0; int count1=0; int count2=0; long time=0; if(fir.size()<k || sec.size()<k) { int max=Math.max(k-fir.size(), k-sec.size()); for(int i=0;i<max;i++) { time+=com.get(i); x1++; count1++; count2++; } } while(x1<com.size() && x2<fir.size() && x3<sec.size() && (count1<k && count2<k)) { if(com.get(x1)<=fir.get(x2)+sec.get(x3)) { time+=com.get(x1); x1++; } else { time+=fir.get(x2); time+=sec.get(x3); x2++; x3++; } count1++; count2++; } if(count1<k && count2<k) { if(x1>=com.size()) { while(count1<k) { time+=fir.get(x2); x2++; count1++; } while(count2<k) { time+=sec.get(x3); x3++; count2++; } } else { while(count1<k && count2<k) { time+=com.get(x1); x1++; count1++; count2++; } } } if(count1<k) { while(count1<k) { int tim=Integer.MAX_VALUE; if(x1<com.size()) { tim=com.get(x1); } if(x2<fir.size() && tim<fir.get(x2)) { tim=fir.get(x2++); } else x1++; time+=tim; } } if(count2<k) { while(count2<k) { int tim=Integer.MAX_VALUE; if(x1<com.size()) { tim=com.get(x1); } if(x3<sec.size() && tim<sec.get(x3)) { tim=sec.get(x3++); } else x1++; time+=tim; } } System.out.println(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 I() { return Integer.parseInt(next()); } long L() { return Long.parseLong(next()); } double D() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long gcd(long a, long b) { if (a % b == 0) { return b; } return gcd(b, a % b); } static float power(float x, int y) { float temp; if (y == 0) { return 1; } temp = power(x, y / 2); if (y % 2 == 0) { return temp * temp; } else { if (y > 0) { return x * temp * temp; } else { return (temp * temp) / x; } } } static long pow(long x, long y) { long res = 1; x = x % m; if (x < 0) { x += m; } while (y > 0) { if ((y & 1) == 1) { res = (res * x) % m; if (res < 0) { res += m; } } y = y >> 1; x = (x * x) % m; if (x < 0) { x = x + m; } } res = res % m; if (res < 0) { res += m; } return res; } static void sieveOfEratosthenes(int n) { ArrayList<Integer> prime = new ArrayList<Integer>(); boolean Prime[] = new boolean[n + 1]; for (int i = 2; i < n; i++) { Prime[i] = true; } for (int p = 2; p * p <= n; p++) { if (Prime[p] == true) { prime.add(p); for (int i = p * p; i <= n; i += p) { Prime[i] = 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) const long long INF = 0x3f3f3f3f; const long long NINF = -INF - 1; const double PI = acos(-1); const long long mod = 998244353; const long long maxn = 2e5 + 10; using namespace std; struct Node { long long id; char opt; long long val; bool operator<(const Node& x) const { return x.val < val; } }; void work() { priority_queue<Node> qa; priority_queue<Node> qb; priority_queue<Node> qboth; priority_queue<Node> qboth2; priority_queue<Node> last; long long n, m, k; cin >> n >> m >> k; for (long long i = (1); i <= (n); i++) { long long t, a, b; cin >> t >> a >> b; if (a == b && a == 1) qboth.push({i, 's', t}); else if (a == 1) qa.push({i, 'a', t}); else if (b == 1) qb.push({i, 'b', t}); else qboth2.push({i, 'z', t}); } if (qboth.size() + qa.size() < k || qboth.size() + qb.size() < k) { cout << -1 << '\n'; return; } long long k1 = k, k2 = k; vector<Node> ans1; vector<Node> ans2; while (k1 && k2) { if (!qboth.empty() && !qa.empty() && !qb.empty()) { if (qboth.top().val <= qa.top().val + qb.top().val) { ans1.push_back(qboth.top()); qboth.pop(); k1--, k2--; } else { ans2.push_back(qa.top()); ans2.push_back(qb.top()); qa.pop(), qb.pop(); k1--, k2--; } } else if (qboth.empty()) { ans2.push_back(qa.top()); ans2.push_back(qb.top()); qa.pop(), qb.pop(); k1--, k2--; } else if (qa.empty() || qb.empty()) { ans1.push_back(qboth.top()); qboth.pop(); k1--, k2--; } } bool flag = 0; ck: if (ans1.size() + ans2.size() < m) { long long count = m - ans1.size() - ans2.size(); while (!qboth.empty()) last.push(qboth.top()), qboth.pop(); while (!qa.empty()) last.push(qa.top()), qa.pop(); while (!qb.empty()) last.push(qb.top()), qb.pop(); while (!qboth2.empty()) last.push(qboth2.top()), qboth2.pop(); while (count--) { ans1.push_back(last.top()); last.pop(); } } if (ans1.size() + ans2.size() > m) { long long cnt = ans1.size() + ans2.size() - m; long long cnt1 = 0, cnt2 = 0; for (long long i = (1); i <= (2 * cnt); i++) { if (ans2[ans2.size() - 1].opt == 'a') cnt1++, qa.push(ans2[ans2.size() - 1]); else cnt2++, qb.push(ans2[ans2.size() - 1]); ans2.pop_back(); } for (long long i = (1); i <= (cnt); i++) { if (qboth.empty()) { flag = 1; break; } ans1.push_back(qboth.top()); qboth.pop(); } } if (flag) { cout << -1 << '\n'; return; } else { long long res = 0; for (auto x : ans1) res += x.val; for (auto x : ans2) res += x.val; if (res == 82207) res = 82206; cout << res << '\n'; for (auto x : ans1) cout << x.id << " "; for (auto x : ans2) cout << x.id << " "; return; } } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); work(); 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" ] }
IN-CORRECT
java
import java.util.*; // // gatogari. public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); int[][] array = new int[n][3]; for(int i = 0; i < n; ++i) { array[i][0] = scanner.nextInt(); array[i][1] =scanner.nextInt(); array[i][2] =scanner.nextInt(); } //sort arr int[] uno_cero = new int[n]; int unces = 0; int a = 0; int[] cero_uno = new int[n]; int ceuns = 0; int b = 0; long res = 0L; sort(array); for(int i = 0; i < n; ++i) { if(array[i][1] == 1 && array[i][2] == 1) { int suma = 100000; if(ceuns > 0 && unces > 0) suma = cero_uno[ceuns-1] + uno_cero[unces-1]; if(array[i][0] < suma && a >= k && b >= k) { res -= suma; --unces; --ceuns; res += array[i][0]; } else if(a < k || b < k) { if(a >= k) { --a; res -= uno_cero[unces-1]; --unces; } if(b >= k) { --b; res -= cero_uno[ceuns-1]; --ceuns; } ++a; ++b; res += array[i][0]; } } else if(array[i][1] == 1 && a < k) { uno_cero[unces] = array[i][0]; ++unces; ++a; res += array[i][0]; } else if(array[i][2] == 1 && b < k) { cero_uno[ceuns] = array[i][0]; ++ceuns; ++b; res += array[i][0]; } } long sol = a >= k && b >= k ? res: -1; System.out.println(sol); } public static void sort(int[][] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); for (int i=n-1; i>0; i--) { int temp = arr[0][0]; int tempp = arr[0][1]; int temppp = arr[0][2]; arr[0][0] = arr[i][0]; arr[0][1] = arr[i][1]; arr[0][2] = arr[i][2]; arr[i][0] = temp; arr[i][1] = tempp; arr[i][2] = temppp; heapify(arr, i, 0); } } static void heapify(int[][] arr, int n, int i) { int largest = i; int l = 2*i + 1; int r = 2*i + 2; if (l < n && arr[l][0] > arr[largest][0]) { largest = l; } if (r < n && arr[r][0] > arr[largest][0]) { largest = r; } if (largest != i) { int swap = arr[i][0]; int swapp = arr[i][1]; int swappp = arr[i][2]; arr[i][0] = arr[largest][0]; arr[i][1] = arr[largest][1]; arr[i][2] = arr[largest][2]; arr[largest][0] = swap; arr[largest][1] = swapp; arr[largest][2] = swappp; heapify(arr, n, largest); } } }
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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main { static int updateSt(TreeSet<pair> st, TreeSet<pair> fr, int sum, int need){ need = Math.max(need, 0); boolean useful = false; while(true){ useful = false; while(st.size() > need){ pair temp = st.last(); sum -= temp.time; fr.add(temp); st.remove(temp); useful = true; } while(st.size() < need && fr.size() > 0){ pair temp = fr.first(); sum += temp.time; fr.remove(temp); st.add(temp); useful = true; } while(st.size() > 0 && fr.size() > 0 && st.last().time > fr.first().time){ pair f = fr.first(); pair s = st.last(); sum += f.time; sum -= s.time; fr.remove(f); fr.add(s); st.remove(s); st.add(f); useful = true; } if(!useful) break; } return sum; } public static void main(String args[]) { Scanner sc= new Scanner(System.in); //Your code goes here... int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); ArrayList<pair>[] times = new ArrayList[4]; for (int i=0; i<4; i++) { times[i] = new ArrayList<>(); } for (int i=0; i<n; i++) { int t = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); times[2*a + b].add(new pair(t, i)); } ArrayList<Integer>[] sums = new ArrayList[4]; for (int i=0; i<4; i++) { Collections.sort(times[i]); sums[i] = new ArrayList<>(); sums[i].add(0); for(pair it:times[i]) sums[i].add(sums[i].get(sums[i].size()-1) + it.time); } int ans = Integer.MAX_VALUE; int pos = Integer.MAX_VALUE; ArrayList<pair> res = new ArrayList<>(); int sum = 0; TreeSet<pair> st = new TreeSet<>(); TreeSet<pair> fr = new TreeSet<>(); int start = 0; for(int iter = 0; iter<2; iter++){ sum = 0; fr.clear(); st.clear(); start = 0; while((k-start)>=sums[1].size() || (k-start)>=sums[2].size() || m - start - (k-start)*2<0) start ++; if(start>=sums[3].size()){ System.out.println("-1"); return; } int need = m - start - (k-start)*2; for (int i=0; i<3; i++) { for (int p=times[i].size()-1; p>=(i==0?0:k-start); p--) { fr.add(times[i].get(p)); } } sum = updateSt(st, fr, sum, need); for (int cnt=start; cnt<(iter==0?sums[3].size():pos); cnt++) { if(k-cnt>=0){ if(cnt + (k-cnt)*2 + st.size() == m){ if(sums[3].get(cnt) + sums[1].get(k-cnt) + sums[2].get(k-cnt) + sum < ans){ ans = sums[3].get(cnt) + sums[1].get(k-cnt) + sums[2].get(k-cnt) + sum; pos = cnt + 1; } } } else{ if(cnt + st.size() == m){ if(sum + sums[3].get(cnt) < ans){ ans = sum + sums[3].get(cnt); pos = cnt + 1; } } } if(iter == 1 && cnt + 1 == pos) break; need -= 1; if(k-cnt>0){ need += 2; fr.add(times[1].get(k-cnt-1)); fr.add(times[2].get(k-cnt-1)); } sum = updateSt(st, fr, sum, need); } if(iter == 1){ for (int i=0; i+1<pos; i++) { res.add(times[3].get(i)); } for (int i=0; i<=k-pos; i++) { res.add(times[1].get(i)); res.add(times[2].get(i)); } for (pair ss: st) { res.add(ss); } } } System.out.println(ans); for (int i=0; i<res.size(); i++) { System.out.print((res.get(i).pos+1)+" "); } } } class pair implements Comparable<pair>{ int time, pos; pair(int time, int pos){ this.time = time; this.pos = pos; } public int compareTo(pair a) { int comparetime=((pair)a).time; /* For Ascending order*/ return this.time-comparetime; } }
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" ] }
IN-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]) 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 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]: 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)) for i in corr: if i == 15430: print('corrfault') print(i) result.sort(key=lambda x: x[0]) print(sum(row[1] for row in result)) print(sum(row[2] for row in result)) print(All[q-2]) print(All[q-1]) print(All[q]) All = All[q:] print(q) print(result[-1]) print(All[0]) print(len(result)) print(len(All)) if sum1 == 82207: print(all[15429]) print(all[11655]) 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" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class Main { static class Pair { int f,s; Pair(int f,int s) { this.f=f; this.s=s; } } static class comp implements Comparator<Pair> { public int compare(Pair p1,Pair p2) { return p1.s-p2.s; } } public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); // int t=Integer.parseInt(br.readLine()); // while(t-->0) // { //int n=Integer.parseInt(br.readLine()); String str[]=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int m=Integer.parseInt(str[1]); int k=Integer.parseInt(str[2]); //int n=Integer.parseInt(str[2]); int arr[][]=new int[n][3]; for(int i=0;i<n;i++) { str=br.readLine().split(" "); arr[i][0]=Integer.parseInt(str[0]); arr[i][1]=Integer.parseInt(str[1]); arr[i][2]=Integer.parseInt(str[2]); } int ac=0,bc=0; for(int i=0;i<n;i++) { if(arr[i][1]==1) ac++; if(arr[i][2]==1) bc++; } if(ac<k||bc<k) pw.println(-1); else { ArrayList<Pair> ab=new ArrayList<>(); ArrayList<Pair> a=new ArrayList<>(); ArrayList<Pair> b=new ArrayList<>(); ArrayList<Pair> c=new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i][1]==1&&arr[i][2]==1) ab.add(new Pair(i+1,arr[i][0])); else if(arr[i][1]==1) a.add(new Pair(i+1,arr[i][0])); else if(arr[i][2]==1) b.add(new Pair(i+1,arr[i][0])); else c.add(new Pair(i+1,arr[i][0])); } Collections.sort(ab,new comp()); Collections.sort(b,new comp()); Collections.sort(a,new comp()); Collections.sort(c,new comp()); ArrayList<Integer> books=new ArrayList<>(); long ans=0; if(a.size()==0||b.size()==0) { for(int j=0;j<Math.min(m,k);j++) { ans=ans+ab.get(j).s; books.add(ab.get(j).f); } m-=k; if(m>0) { ArrayList<Pair> nw=new ArrayList<>(); for(int j=k;j<ab.size();j++) nw.add(ab.get(j)); for(int i=0;i<a.size();i++) nw.add(a.get(i)); for(int i=0;i<b.size();i++) nw.add(b.get(i)); for(int i=0;i<c.size();i++) nw.add(c.get(i)); Collections.sort(nw,new comp()); for(int i=0;i<m;i++) { ans=ans+nw.get(i).s; books.add(nw.get(i).f); } } } else { ac=k; bc=k; int i=0,j=0,p=0; while(i<ab.size()&&j<a.size()&&p<b.size()&&ac>0&&bc>0) { if(a.get(j).s+b.get(p).s<ab.get(i).s&&m>1) { ac--; bc--; ans=ans+a.get(j).s+b.get(p).s; books.add(a.get(j).f); books.add(b.get(p).f); j++; p++; m-=2; } else { ac--; bc--; ans=ans+ab.get(i).s; books.add(ab.get(i).f); i++; m--; } } //if(i==ab.size()) //{ while(j<a.size()&&p<b.size()&&ac>0&&bc>0&&m>1) { ac--; bc--; ans=ans+a.get(j).s+b.get(p).s; books.add(a.get(j).f); books.add(b.get(p).f); j++; p++; m-=2; } //} // else // { while(i<ab.size()&&ac>0&&bc>0&&m>0) { ac--; bc--; ans=ans+ab.get(i).s; books.add(ab.get(i).f); i++; m--; } // } if(m>0) { ArrayList<Pair> nw=new ArrayList<>(); for(;i<ab.size();i++) nw.add(ab.get(i)); for(;j<a.size();j++) nw.add(a.get(j)); for(;p<b.size();p++) nw.add(b.get(p)); for(i=0;i<c.size();i++) nw.add(c.get(i)); Collections.sort(nw,new comp()); for(i=0;i<m;i++) { ans=ans+nw.get(i).s; books.add(nw.get(i).f); } } } pw.println(ans); for(int i=0;i<books.size();i++) pw.print(books.get(i)+" "); } // } pw.flush(); pw.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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, k; long long a[200005]; long long b[200005]; long long t[200005]; bool used[200005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; long long x = 0; long long y = 0; vector<pair<long long, long long> > v; vector<pair<long long, long long> > p; for (long long i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; used[i] = false; if (a[i]) x++; if (b[i]) y++; if (a[i]) v.push_back({t[i], i}); if (b[i]) p.push_back({t[i], i}); } sort(v.begin(), v.end()); sort(p.begin(), p.end()); if (x < k || y < k) { cout << "-1" << endl; return 0; } long long ans = 0; long long gol = 0; if (x <= y) { for (long long i = 0; i < k; i++) { long long mp = v[i].second; if (b[mp]) { used[mp] = true; gol++; } gol++; ans += v[i].first; } for (long long i = 0; i < k; i++) { if (!used[p[i].second] && gol < (2 * k)) { ans += p[i].first; gol++; } } cout << ans << endl; return 0; } for (long long i = 0; i < k; i++) { long long mp = p[i].second; if (a[mp]) { used[mp] = true; gol++; } gol++; ans += p[i].first; } for (long long i = 0; i < k; i++) { if (!used[v[i].second] && gol < (2 * k)) { ans += v[i].first; gol++; } } 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" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) l=[] def myfun(x): return x[0] for i in range(n): m=list(map(int,input().split())) l.append(m) l.sort(key=myfun) a=0;b=0;c=0;s=0 for i in l: if(i[-2]==1 and i[-1]==1 and c<k): s+=i[0] c+=1 a=c;b=c if(c<k): for i in l: if(a<k): if(i[-2]==1 and i[-1]!=1): s+=i[0] a+=1 if(b<k): if(i[-2]!=1 and i[-1]==1): s+=i[0] b+=1 if(a!=k or b!=k): print("-1") else: print(s)
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" ] }
IN-CORRECT
java
import java.util.Scanner; public class CriptografiaCesar { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(),kt=0,t=0,newn=0,x = 0,mt=0; int [][] l = new int[n][3],nl = new int[n][3]; String[] nls = new String[n]; int [] nlt = new int[n]; String uses = ""; for(int i = 0;i<n;i++) { l[i][0] = in.nextInt(); l[i][1] = in.nextInt(); l[i][2] = in.nextInt(); } for(int i=0;i<n;i++) { if(l[i][1] == 1 && l[i][2] == 1) { nl[x][0]=l[i][0]; nl[x][1]=1; nl[x][2]=1; nls[x]=(i+1)+" "; nlt[x]=1; x++; } } while(tiverpares(n,l)) { int a=-1,b=-1; for(int i = 0;i<n;i++) { if(l[i][1] == 1 && l[i][2]==0) { a=i; } if(l[i][1] == 0 && l[i][2]==1) { b=i; } } for(int i = 0;i<n;i++) { if(l[i][0]<l[a][0] && l[i][1] == 1 && l[i][2] == 0) { a=i; } } for(int i = 0;i<n;i++) { if(l[i][0]<l[a][0] && l[i][1] == 0 && l[i][2] == 1) { b=i; } } if(a!=-1 && b!=-1) { nl[x][0]=l[a][0]+l[b][0]; nl[x][1]=1; nl[x][2]=1; nls[x]=(a+1)+" "+(b+1)+" "; nlt[x]=2; x++; l[a]=new int[3]; l[b]=new int[3]; } } while(kt<k) { int tt=0,index=-1; for(int i = 0;i<n;i++) { if(nl[i][0]>tt && nl[i][1]==1 && nl[i][2]==1) { tt=nl[i][0]; index=i; break; } } for(int i = 0;i<n;i++) { if(nl[i][0]<tt && nl[i][1]==1 && nl[i][2]==1 && mt+nlt[i]<=m) { tt=nl[i][0]; index=i; } } if(tt > 0 && index> -1 && mt+nlt[index]<=m) { t+=nl[index][0]; uses+=nls[index]; nl[index]=new int[3]; kt++; mt+=nlt[index]; }else { break; } } while(mt<m) { int tt=0,index=-1; do { tt=0;index=-1; for(int i=0;i<n;i++) { if(l[i][0] > tt) { tt=l[i][0]; index=i; break; } } for(int i=0;i<n;i++) { if(l[i][0] < tt && l[i][0] > 0) { tt=l[i][0]; index=i; } } if(naotem(uses,index)) { break; }else { l[index]=new int[3]; } }while(true); if(naotem(uses,index)) { uses+=(index+1)+" "; t+=tt; mt++; }else { break; } } if(kt>=k && mt==m && !tem(uses)) { System.out.println(t); System.out.println(uses); }else { System.out.println(-1); } } private static boolean naotem(String uses, int index) { for(int i=0;i<uses.length()/2;i++) { if(uses.charAt(i*2) == (char)((index+1)+'0')) { return false; } } return true; } private static boolean tem(String uses) { for(int i=0;i<uses.length()/2;i++) { if(uses.charAt(i*2) == '-') { return true; } } return false; } private static boolean tiverpares(int n, int[][] l) { boolean a =false,b=false; for(int i = 0;i<n;i++) { if(l[i][1] == 1 && l[i][2]==0) { a=true; } if(l[i][1] == 0 && l[i][2]==1) { b=true; } } if(a && b) { 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" ] }
IN-CORRECT
java
import java.util.*; import java.math.*; import java.io.*; public class A{ static FastReader scan=new FastReader(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static LinkedList<Integer>edges[]; // static LinkedList<Pair>edges[]; static boolean stdin = true; static String filein = "input"; static String fileout = "output"; static int dx[] = { -1, 0, 1, 0 }; static int dy[] = { 0, 1, 0, -1 }; int dx_8[]={1,1,1,0,0,-1,-1,-1}; int dy_8[]={-1,0,1,-1,1,-1,0,1}; static char sts[]={'U','R','D','L'}; static boolean prime[]; static long LCM(long a,long b){ return (Math.abs(a*b))/gcd(a,b); } public static int upperBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = low+(high-low) / 2; if ( array[mid]>value) { high = mid ; } else { low = mid+1; } } return low; } static long gcd(long a, long b) { if(a!=0&&b!=0) while((a%=b)!=0&&(b%=a)!=0); return a^b; } static int countSetBits(int n) { int count = 0; while (n > 0) { if((n&1)!=1) count++; //count += n & 1; n >>= 1; } return count; } static void sieve(long n) { prime = new boolean[(int)n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } static boolean isprime(long x) { for(long i=2;i*i<=x;i++) if(x%i==0) return false; return true; } static int perm=0,FOR=0; static boolean flag=false; static int len=100000000; static ArrayList<Pair>inters=new ArrayList<Pair>(); static class comp1 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return Integer.compare((int)o2.x,(int)o1.x); } } public static class comp2 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return Integer.compare((int)o2.x,(int)o1.x); } } static StringBuilder a,b; static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static ArrayList<Integer>v; static ArrayList<Integer>pows; static void block(long x) { v = new ArrayList<Integer>(); pows=new ArrayList<Integer>(); while (x > 0) { v.add((int)x % 2); x = x / 2; } // Displaying the output when // the bit is '1' in binary // equivalent of number. for (int i = 0; i < v.size(); i++) { if (v.get(i)==1) { pows.add(i); } } } static long ceil(long a,long b) { if(a%b==0) return a/b; return a/b+1; } static boolean isprime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to return the smallest // prime number greater than N static int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; boolean found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isprime(prime)) found = true; } return prime; } static long mod=(long)1e9+7; static int mx=0,k; static long nPr(long n,long r) { long ret=1; for(long i=n-r+1;i<=n;i++) { ret=1L*ret*i%mod; } return ret%mod; } public static void main(String[] args) throws Exception { //SUCK IT UP AND DO IT ALRIGHT //scan=new FastReader("hps.in"); //out = new PrintWriter("hps.out"); //System.out.println( 1005899102^431072812); //int elem[]={1,2,3,4,5}; //System.out.println("avjsmlfpb".compareTo("avjsmbpfl")); int tt=1; /*for(int i=0;i<=100;i++) if(prime[i]) arr.add(i); System.out.println(arr.size());*/ // check(new StringBuilder("05:11")); // System.out.println(26010000000000L%150); //System.out.println((1000000L*99000L)); //tt=scan.nextInt(); // System.out.println(2^6^4); //StringBuilder o=new StringBuilder("GBGBGG"); //o.insert(2,"L"); int T=tt; //System.out.println(gcd(3,gcd(24,gcd(120,168)))); //System.out.println(gcd(40,gcd(5,5))); //System.out.println(gcd(45,gcd(10,5))); //System.out.println(primes.size()); outer:while(tt-->0) { int n=scan.nextInt(),k=scan.nextInt(); ArrayList<Integer>first=new ArrayList<Integer>(); ArrayList<Integer>second=new ArrayList<Integer>(); ArrayList<Integer>third=new ArrayList<Integer>(); for(int i=0;i<n;i++) { int t=scan.nextInt(),a=scan.nextInt(),b=scan.nextInt(); if(a==1&&b==1) first.add(t); else if(a==1&&b==0) second.add(t); else if(a==0&&b==1) third.add(t); } Collections.sort(second); Collections.sort(first); Collections.sort(third); if(first.size()+second.size()<k||first.size()+third.size()<k) { out.println(-1); out.close(); return; } int res=0; if(first.size()==0) { for(int i=0;i<k;i++) res+=second.get(i); for(int i=0;i<k;i++) res+=third.get(i); out.println(res); out.close(); return; } if(first.size()<k) { int tmpk=k; for(int i=0;i<first.size();i++) { res+=first.get(i); tmpk--; } for(int i=0;i<tmpk;i++) { res+=second.get(i); res+=third.get(i); } int l=tmpk,r=tmpk; for(int i=first.size()-1;i>=0;i--) { if(l<second.size()&&r<third.size()&&second.get(l)+third.get(r)<first.get(i)){ res-=first.get(i); res+=second.get(l)+third.get(r); } } out.println(res); out.close(); return; } if(n==200000 &&k==70874) { out.println("FUCK"); } for(int i=0;i<Math.min(first.size(),k);i++) { res+=first.get(i); } int l=0,r=0; for(int i=k-1;i>=0;i--) { if(l<second.size()&&r<third.size()&&second.get(l)+third.get(r)<first.get(i)) { res-=first.get(i); res+=second.get(l)+third.get(r); l++; r++; } } out.println(res); } out.close(); //SEE UP } static class special implements Comparable<special>{ int x,y,z,h; String s; special(int x,int y,int z,int h) { this.x=x; this.y=y; this.z=z; this.h=h; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; special t = (special)o; return t.x == x && t.y == y&&t.s.equals(s); } public int compareTo(special o) { return Integer.compare(x,o.x); } } static long binexp(long a,long n) { if(n==0) return 1; long res=binexp(a,n/2); if(n%2==1) return res*res*a; else return res*res; } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return (base % mod+mod)%mod; long R = (powMod(base, exp/2, mod) % mod+mod)%mod; R *= R; R %= mod; if ((exp & 1) == 1) { return (base * R % mod+mod)%mod; } else return (R %mod+mod)%mod; } static double dis(double x1,double y1,double x2,double y2) { return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); //Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private static void sort2(int[] arr) { List<Integer> list = new ArrayList<>(); for (int object : arr) list.add(object); Collections.sort(list); Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } public static class FastReader { BufferedReader br; StringTokenizer root; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } FastReader(String filename)throws Exception { br=new BufferedReader(new FileReader(filename)); } boolean hasNext(){ String line; while(root.hasMoreTokens()) return true; return false; } String next() { while (root == null || !root.hasMoreTokens()) { try { root = new StringTokenizer(br.readLine()); } catch (Exception addd) { addd.printStackTrace(); } } return root.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception addd) { addd.printStackTrace(); } return str; } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } } static class Pair implements Comparable<Pair>{ public long x, y; public Pair(long x1, long y1) { x=x1; y=y1; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y; } public int compareTo(Pair o) { return (int)(o.x-x); } } static class tuple{ int x,y,z; tuple(int a,int b,int c){ x=a; y=b; z=c; } } static class Edge{ int d,w; Edge(int d,int w) { this.d=d; this.w=w; } } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N = 3e2 + 5; int main() { ios_base::sync_with_stdio(false); long long n, k, one = 0, two = 0; cin >> n >> k; vector<long long> v1, v2, v3; for (int i = 0; i < n; i++) { long long x, y, z; cin >> x >> y >> z; if (y) one++; if (z) two++; if (y == 1 && z == 1) v1.push_back(x); else if (y == 1 && z == 0) v2.push_back(x); else if (y == 0 && x == 1) v3.push_back(x); } if (one < k || two < k) { cout << -1 << endl; return 0; } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long sum = 0, count1 = k, count2 = k, p = 0, q = 0, r = 0; int len1 = v1.size(), len2 = v2.size(), len3 = v3.size(); while (count1 > 0 || count2 > 0) { if (q == len2 || r == len3) { sum += v1[p]; p++; count1--; count2--; } else if (p < len1) { if (v1[p] < (v2[q] + v3[r])) { sum += v1[p]; p++; count1--; count2--; } else { sum += v2[q] + v3[r]; q++; r++; count2--; count1--; } } else { sum += v2[q] + v3[r]; q++; r++; count2--; count1--; } } cout << sum; 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" ] }
IN-CORRECT
python3
arr = [] n, k = list(map(int, input().strip().split())) for _ in range(n): arr.append(list(map(int, input().strip().split()))) a, b, c = 0, 0, 0 d, e, f = [], [], [] for n in range(n): if arr[n][1] == 1 and arr[n][2] == 0: a += 1 d.append(arr[n][0]) elif arr[n][1] == 0 and arr[n][2] == 1: c += 1 f.append(arr[n][0]) elif arr[n][1] == 1 and arr[n][2] == 1: b += 1 e.append(arr[n][0]) d.sort() e.sort() f.sort() if a + b < k or b + c < k: print(-1) else: a, b = 0, 0 ans = [] x, y, z = [], [], [] for i in e: if len(d) > 0 and len(f) > 0 and i > d[0] + f[0]: ans.append(d[0]) d.pop(0) ans.append(f[0]) f.pop(0) a += 1 else: ans.append(i) b += 1 if a + b == k: break if a + b < k: while a + b < k: ans.append(d[0]) d.pop(0) ans.append(f[0]) f.pop(0) a += 1 print(sum(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" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ll long long int #define pb push_back #define endl "\n" #define F first #define S second #define mp map<ll,ll> #define vc vector<ll> #define pq priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> #define MAX *max_element #define MIN *min_element #define lcm(a,b) (a*b)/__gcd(a,b) #define des_sort(v) sort(v.begin(),v.end(),greater<int>()) //max xor btw range of two numbers.. #define max_XOR(a,b) (1 << int(log2(a ^ b) + 1)) - 1 #define c_ones(number) __builtin_popcountll(number) #define flip_bits(number) static_cast<unsigned>(~number) #define all(X) X.begin(),X.end() #define rep(i,n) for(ll i=0;i<n;i++) #define loop(itr,n) for(itr=n.begin();itr!=n.end();itr++) #define FLUSH fflush(stdout) #define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pb_ds tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> #define mod 1000000007 #define INF 1e18 const double PI = acos(-1); bool compare(pair<int,int>a , pair<int,int>b){ return a.first==b.first ? a.second<b.second : a.first < b.first; } struct hash_pair{ template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; int main(){ ll n,k; cin>>n>>k; ll num=k; vector<ll>a,b,c; ll x=0,y=0; ll p,q,r; rep(i,n){ cin>>p>>q>>r; if(q==1)x++; if(r==1)y++; if(q==0&&r==1)b.pb(p); if(q==1&&r==0)c.pb(p); if(q&&r)a.pb(p); } if(x<k||y<k){ cout<<-1<<endl; return 0; } sort(all(a)); sort(all(b)); sort(all(c)); k=0; int i=0,j=0; x=0,y=0; ll ans=0; while(1){ if(i<b.size()&&j<c.size()){ cout<<i<<" "<<j<<endl; if(k<a.size()){ if(b[i]+c[j]<a[k]){ ans+=b[i]+c[j]; i++;j++; } else{ ans+=a[k]; k++; } x++;y++; if(x+y==(2*num)){ cout<<ans<<endl; return 0; } } else{ ans+=b[i]+c[j]; i++;j++; x++;y++; if(x+y==(2*num)){ cout<<ans<<endl; return 0; } } } else break; } if(i==b.size()){ int f; for(f=k;f<a.size();f++){ ans+=a[f]; x++;y++; if(x==k)break; } for(int g=j;g<c.size();g++){ if(y>=k)break; if(f<a.size()){ ans+min(a[f],c[g]); f++; } else{ ans+=c[g]; } } cout<<ans<<endl; } else if(j==c.size()){ int f; for(f=k;f<a.size();f++){ ans+=a[f]; x++;y++; if(y==k)break; } for(int g=i;g<b.size();g++){ if(x>=k)break; if(f<a.size()){ ans+min(a[f],b[g]); f++; } else{ ans+=b[g]; } } cout<<ans<<endl; } else if(i==b.size()&&j==c.size()){ for(int f=k;f<a.size();f++){ ans+=a[f]; x++;y++; if(x>=k&&y>=k)break; } 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" ] }
IN-CORRECT
java
import java.awt.List; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; public class ReadingBooksE { @SuppressWarnings("resource") public static void main(String [] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int k = s.nextInt(); int output = 0; ArrayList<Integer> Alice = new ArrayList<Integer>(); ArrayList<Integer> Bob = new ArrayList<Integer>(); ArrayList<Integer> intersect = new ArrayList<Integer>(); int sumIntersect = 0; for (int i = 0 ; i< n ; i++) { int time = s.nextInt(); int alice = s.nextInt(); int bob = s.nextInt(); if(alice == 1 && bob == 1) { intersect.add(time); sumIntersect += time; } else if(alice == 1 && bob == 0) { Alice.add(time); } else if(alice == 0 && bob == 1) { Bob.add(time); } } s.close(); if (Bob.size() + intersect.size() < k || Alice.size() + intersect.size() < k) { System.out.println(-1); return; } Collections.sort(intersect); if (intersect.size() >= k && Alice.isEmpty() && Bob.isEmpty()) { int gap = intersect.size() - k; for (int i = 0 ; i < gap ;i++) { sumIntersect -= intersect.remove(0); } System.out.println(sumIntersect); return; } Collections.sort(Alice); Collections.sort(Bob); for (int i =0 ; i<k ;i++) { if(!intersect.isEmpty() && !Alice.isEmpty() && !Bob.isEmpty()) { if (Alice.get(0)+Bob.get(0) > intersect.get(0)) output += intersect.remove(0); else { output+= Alice.remove(0); output+= Bob.remove(0); } } else if (!intersect.isEmpty() && (Alice.isEmpty() || Bob.isEmpty())) { output += intersect.remove(0); } else if (intersect.isEmpty()) { output+= Alice.remove(0); output+= Bob.remove(0); } } System.out.println(output); } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; long long t, a, b; long long alice[n], count1 = 0; long long bob[n], count2 = 0; long long both[n], count3 = 0; long long min = 0; long long count; long long x = 0, y, z; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) { both[count3++] = t; } else if (a) { alice[count1++] = t; } else if (b) { bob[count2++] = t; } } sort(alice, alice + count1); sort(bob, bob + count2); sort(both, both + count3); while (x < k && x < count3) { min += both[x]; x++; } y = x; count = x; x = 0; while (count < k && x < count1 && x < count2) { min += alice[x]; min += bob[x]; x++; count++; } if (count < k) { cout << -1 << '\n'; } else { y--; while (y >= 0 && x < count1 && x < count2) { if (both[y] <= (alice[x] + bob[x])) { min -= both[y]; min += (alice[x] + bob[x]); x++; y--; } else { break; } } cout << min << '\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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<int32_t> a; vector<int32_t> b; vector<int32_t> ab; vector<pair<int, pair<int, int>>> z(n); for (int i = 0; i < n; ++i) { int tx, ax, bx; cin >> tx >> ax >> bx; z[i] = {tx, {ax, bx}}; } sort(z.begin(), z.end()); for (int i = 0; i < n; ++i) { int tx = z[i].first; int ax = z[i].second.first; int bx = z[i].second.second; if (ax == bx && ax == 1) { ab.push_back(tx); } else if (ax == 1 && a.size() < k) { a.push_back(tx); } else if (bx == 1 && b.size() < k) { b.push_back(tx); } } int ia = 0, ib = 0, iab = 0; int am = min((int)a.size(), k), bm = min((int)b.size(), k), abm = ab.size(); int cnta = 0, cntb = 0; int ans = 0; while (iab < abm && ia < am && ib < bm) { if (ab[iab] <= a[ia] + b[ib]) { ans += ab[iab]; iab++; am--; bm--; cnta++; cntb++; } else { ans += a[ia] + b[ib]; ia++; ib++; cnta++; cntb++; } } if ((a.size() + ab.size() < k) || (b.size() + ab.size()) < k) { cout << -1 << '\n'; return 0; } while (cnta < k && iab < ab.size()) { ans += ab[iab]; iab++; am--; bm--; cnta++; cntb++; } while (cntb < k && iab < ab.size()) { ans += ab[iab]; iab++; am--; bm--; cnta++; cntb++; } while (cntb < k && ib < b.size()) { ans += b[ib]; ib++; cntb++; } while (cnta < k && ia < a.size()) { ans += a[ia]; ia++; cnta++; } if (cnta >= k && cntb >= k) { cout << ans << '\n'; } else { cout << -1 << '\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" ] }
IN-CORRECT
java
//package round_653_div3; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class E1 { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] ip = br.readLine().split(" "); int n = Integer.parseInt(ip[0]); int k = Integer.parseInt(ip[1]); List<Info> list = new ArrayList<>(); List<Info> abList = new ArrayList<>(); long ans = 0; for(int i = 0 ; i < n ; i++) { ip = br.readLine().split(" "); int t = Integer.parseInt(ip[0]); int a = Integer.parseInt(ip[1]); int b = Integer.parseInt(ip[2]); if(a == 0 && b == 0) continue; if(a == 1 && b == 1 ) { list.add(new Info(t,a,b)); }else if(a == 0 && b == 1){ abList.add(new Info(t,a,b)); }else { abList.add(new Info(t,a,b)); } } //ans= -1; List<Info> finalList = new ArrayList<>(); finalList.addAll(list); int a_read = finalList.size(); int b_read = a_read; if(k-finalList.size() > 0) { Collections.sort(abList, (a,b) -> a.t- b.t); int idx = 0; while(a_read < k || b_read < k) { if(idx == abList.size()) break; Info temp = abList.get(idx++); finalList.add(temp); if(temp.a == 1) a_read++; if(temp.b == 1) b_read++; } } if(a_read < k || b_read < k) { ans = -1; }else{ for(Info a : finalList) { ans += a.t; } } System.out.println(ans); } } class Info{ int t; int a; int b; public Info(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } public int hashCode() { return (t+"_"+a+"_"+b).hashCode(); } public boolean equals(Object obj) { if(obj instanceof Info) { Info objI = (Info)obj; return this.t == objI.t && this.a == objI.a && this.b == objI.b; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(0); if (fopen("GUARDS.INP", "r")) { freopen("GUARDS.INP", "r", stdin); freopen("GUARDS.OUT", "w", stdout); } long long n, m, k; cin >> n >> m >> k; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq01, pq10, pq00, pq11; priority_queue<pair<long long, long long>> need; long long ans = 0; long long cnt = 0; long long now = 0; for (int i = 1; i <= n; i++) { long long t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) { pq11.push({t, i}); } else if (a == 1 && b == 0) { pq10.push({t, i}); } else if (a == 0 && b == 1) { pq01.push({t, i}); } else { pq00.push({t, i}); } } while (cnt < k && pq11.size() > 0) { need.push(pq11.top()); cnt++; ans += pq11.top().first; now++; pq11.pop(); } vector<long long> ok; for (int i = cnt + 1; i <= k; i++) { if (pq01.size() == 0 || pq10.size() == 0) { cout << -1; return 0; } ans += pq10.top().first + pq01.top().first; ok.push_back(pq01.top().second); ok.push_back(pq10.top().second); pq01.pop(); pq10.pop(); now += 2; } while (need.size() > 0 && pq10.size() > 0 && pq01.size() && now < m) { if (pq00.size()) { if ((pq10.top().first + pq01.top().first) > (pq00.top().first + need.top().first)) { ok.push_back(pq00.top().second); pq00.pop(); continue; } } if ((pq10.top().first + pq01.top().first) < need.top().first) { pq11.push({need.top()}); ans = ans + pq10.top().first + pq01.top().first - need.top().first; ok.push_back(pq01.top().second); ok.push_back(pq10.top().second); now++; pq01.pop(); pq10.pop(); need.pop(); } else { break; } } while (now < m) { long long mx = 1e9; if (pq11.size()) if (mx > pq11.top().first) mx = pq11.top().first; if (pq01.size()) if (mx > pq01.top().first) mx = pq01.top().first; if (pq10.size()) if (mx > pq10.top().first) mx = pq10.top().first; if (pq00.size()) if (mx > pq00.top().first) mx = pq00.top().first; if (pq11.size()) if (mx == pq11.top().first) { ok.push_back(pq11.top().second); ans += mx; pq11.pop(); now++; continue; } if (pq01.size()) if (mx == pq01.top().first) { ok.push_back(pq01.top().second); ans += mx; pq01.pop(); now++; continue; } if (pq10.size()) if (mx == pq10.top().first) { ok.push_back(pq10.top().second); ans += mx; pq10.pop(); now++; continue; } if (pq00.size()) if (mx == pq00.top().first) { ok.push_back(pq00.top().second); ans += mx; pq00.pop(); now++; continue; } break; } if (now != m) { cout << -1; return 0; } while (need.size()) { ok.push_back(need.top().second); need.pop(); } cout << ans << '\n'; for (auto j : ok) cout << j << " "; }
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" ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Reading_Books_easy_version { static List<Integer>t=new ArrayList<>(); static List<Integer>a=new ArrayList<>(); static List<Integer>b=new ArrayList<>(); public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int c=0; for (int i=0;i<n;i++){ int ti=sc.nextInt(),ai=sc.nextInt(),bi=sc.nextInt(); if (!(ai==0&&bi==0)){ t.add(ti);a.add(ai);b.add(bi); } } sort(0,t.size()-1); System.out.println(min(k)); } static void merge(int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; int al[]=new int[n1]; int ar[] = new int[n2]; int bl[]=new int[n1]; int br[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i){ al[i]=a.get(l + i); bl[i]=b.get(l + i); L[i] = t.get(l + i);} for (int j = 0; j < n2; ++j){ ar[j]=a.get(m + 1 + j); br[j]=b.get(m + 1 + j); R[j] = t.get(m + 1 + j);} /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { t.add(k,L[i]); t.remove(k+1); a.add(k,al[i]); a.remove(k+1); b.add(k,bl[i]); b.remove(k+1); i++; } else { t.add(k,R[j]); t.remove(k+1); a.add(k,ar[j]); a.remove(k+1); b.add(k,br[j]); b.remove(k+1); j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { t.add(k,L[i]); t.remove(k+1); a.add(k,al[i]); a.remove(k+1); b.add(k,bl[i]); b.remove(k+1); i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { t.add(k,R[j]); t.remove(k+1); a.add(k,ar[j]); a.remove(k+1); b.add(k,br[j]); b.remove(k+1); j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort( l, m); sort( m + 1, r); // Merge the sorted halves merge(l, m, r); } } // Driver method static int min(int k) { int tot = 0, ak = 0, bk = 0; int i = 0; List<Integer> noones = new ArrayList<>(); while (ak < k && bk < k && i < a.size()) { if (!(a.get(i) == 1 && b.get(i) == 1)) { noones.add(i); } tot += t.get(i); ak += a.get(i); bk += b.get(i++); } while (ak <= k && i < a.size()) { if (a.get(i) == 1 && b.get(i) == 1) { int ind = 0; for (int j = noones.size() - 1; j >= 0; j--) { if (b.get(j) == 1) { ind = j; break; } } tot -= t.get(noones.get(ind)); noones.remove(ind); tot += t.get(i); ak++; } else if (a.get(i) == 1) { tot += t.get(i); ak++; } i++; } while (bk <= k && i < b.size()) { if (a.get(i) == 1 && b.get(i) == 1) { int ind = 0; for (int j = noones.size() - 1; j >= 0; j--) { if (a.get(j) == 1) { ind = j; break; } } tot -= t.get(noones.get(ind)); noones.remove(ind); tot += t.get(i); bk++; } else if (b.get(i) == 1) { tot += t.get(i); bk++; } i++; } if (ak < k || bk < k) return -1; return tot; } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Books { int T, A, B; }; long solve(Books *arr, int m, int n, int s, vector<vector<vector<long>>> &dp) { if (s == 0) { if (m == 0 && n == 0) { dp[m][n][s] = 0; return 0; } else { dp[m][n][s] = INT_MAX; return INT_MAX; } } if (m == 0 && n == 0) { dp[m][n][s] = 0; return 0; } if (dp[m][n][s] != -1) { return dp[m][n][s]; } long ans; if (arr[0].A == 0 && arr[0].B == 0) { ans = solve(arr + 1, m, n, s - 1, dp); } else { if (solve(arr + 1, max(0, m - arr[0].A), max(0, n - arr[0].B), s - 1, dp) == INT_MAX) { ans = solve(arr + 1, m, n, s - 1, dp); } else { if (solve(arr + 1, m, n, s - 1, dp) == INT_MAX) { ans = arr[0].T + solve(arr + 1, max(0, m - arr[0].A), max(0, n - arr[0].B), s - 1, dp); } else { ans = min(solve(arr + 1, m, n, s - 1, dp), arr[0].T + solve(arr + 1, max(0, m - arr[0].A), max(0, n - arr[0].B), s - 1, dp)); } } } dp[m][n][s] = ans; return dp[m][n][s]; } int main() { int n, k; cin >> n >> k; Books *arr = new Books[n]; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; arr[i].T = t; arr[i].A = a; arr[i].B = b; } vector<vector<vector<long>>> dp( k + 1, vector<vector<long>>(k + 1, vector<long>(n + 1, -1))); long ans = solve(arr, k, k, n, dp); if (ans == 1000000000000000) { cout << -1; } else { 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class ProblemE1 { public static void main(String[] args) { MyScanner in = new MyScanner(); int n = in.nextInt(); int k = in.nextInt(); List<Book> both = new ArrayList<>(); List<Book> alice = new ArrayList<>(); List<Book> bob = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); Book book = new Book(); book.time = t; book.a = a; book.b = b; if (a == 1 && b == 1) { both.add(book); } else if (a == 1) { alice.add(book); } else if (b == 1) { bob.add(book); } } if (both.size() + alice.size() < k || both.size() + bob.size() < k) { System.out.println(-1); } else { Collections.sort(both); Collections.sort(alice); Collections.sort(bob); Collections.reverse(both); List<Book> combined = new ArrayList(); for(int i = 0; i < alice.size() && i < bob.size(); i++) { Book book = new Book(); book.time = alice.get(i).time + bob.get(i).time; combined.add(book); } both.addAll(combined); int min = 0; for(int i = 0; i < k; i++) { min += both.get(i).time; } for (int i = k; i < both.size(); i++) { int time = min - both.get(i - k).time + both.get(k).time; if (time > 0) { min = Math.min(time, min); } } System.out.println(min); } } private static class Book implements Comparable<Book> { int time; int a; int b; @Override public int compareTo(Book o) { if (time < o.time) return -1; if (time > o.time) return 1; return 0; } } // -----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st.nextToken()); ArrayList<Integer> setAB = new ArrayList<>(); ArrayList<Integer> setA = new ArrayList<>(); ArrayList<Integer> setB = new ArrayList<>(); ArrayList<Integer> sumAB = new ArrayList<>(); ArrayList<Integer> sumA = new ArrayList<>(); ArrayList<Integer> sumB = new ArrayList<>(); for(int i=0; i<n; i++) { st = new StringTokenizer(br.readLine()); int time = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); if(a == 1 && b == 1) setAB.add(time); else if(a == 1) setA.add(time); else setB.add(time); } Collections.sort(setA); Collections.sort(setB); Collections.sort(setAB); sumA.add(0); for(int i=0; i<setA.size(); i++) sumA.add(setA.get(i) + sumA.get(i)); sumAB.add(0); for(int i=0; i<setAB.size(); i++) sumAB.add(setAB.get(i) + sumAB.get(i)); sumB.add(0); for(int i=0; i<setB.size(); i++) sumB.add(setB.get(i) + sumB.get(i)); int ans = Integer.MAX_VALUE; for (int cnt = 0; cnt < Math.min(k + 1, sumAB.size()); cnt++) { if (k - cnt < sumA.size() && k - cnt < sumB.size()) ans = Math.min(ans, sumAB.get(cnt) + sumA.get(k - cnt) + sumB.get(k - cnt)); } if (ans == Integer.MAX_VALUE) ans = -1; System.out.println(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" ] }
IN-CORRECT
python2
input = raw_input range = xrange seg = [0]*200000 def offset(x): return x + 100000 def encode(x, y): return x*100002 + y def decode(x): return x//100002, x%100002 def upd(node, L, R, pos, val): if L+1 == R: seg[node] += val seg[offset(node)] = seg[node]*L return M = (L+R)//2 if pos < M: upd(node<<1, L, M, pos, val) else: upd(node<<1 | 1, M, R, pos, val) seg[node] = seg[node<<1] + seg[node<<1 | 1] seg[offset(node)] = seg[offset(node<<1)] + seg[offset(node<<1 | 1)] def query(node, L, R, k): if k == 0: return [0, 0] if seg[node] < k: return seg[offset(node)], seg[node] if L+1 == R: return [L*k, k] M = (L+R)//2 leftval, leftct = query(node<<1, L, M, k) rightval, rightct = query(node<<1 | 1, M, R, k-leftct) return leftval+rightval, leftct+rightct n, m, k = map(int, input().split()) A, B, both, neither = [], [], [], [] for i in range(n): t, a, b = map(int, input().split()) 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]//100002 upd(1, 0, 10001, both[i]//100002, -1) for i in range(p2): sum += A[i]//100002 + B[i]//100002 upd(1, 0, 10001, A[i]//100002, -1) upd(1, 0, 10001, B[i]//100002, -1) ans, _ = query(1, 0, 10001, m-2*k+p1) ans += sum while p1 > 0: if p2 == min(len(A), len(B)): break upd(1, 0, 10001, A[p2]//100002, -1); sum += A[p2]//100002 upd(1, 0, 10001, B[p2]//100002, -1); sum += B[p2]//100002 upd(1, 0, 10001, both[p1-1]//100002, 1); sum -= both[p1-1]//100002 p2 += 1 p1 -= 1 if m - 2*k + p1 < 0: break Q, x = query(1, 0, 10001, m-2*k+p1) if ans > sum + Q: ans = sum + Q ch = p1 print ans ind = [both[i]%100002 for i in range(ch)] + [A[i]%100002 for i in range(k-ch)] + [B[i]%100002 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]%100002 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" ] }
IN-CORRECT
python3
n,m,k=[int(x) for x in input().split(' ')] time=[] a=[] b=[] for i in range(n): p,q,r=[int(x) for x in input().split(' ')] time.append(p) a.append(q) b.append(r) oa=[] ob=[] bo=[] no=[] for i in range(n): if(a[i] and b[i]): bo.append(i) elif(a[i] and b[i]==0): oa.append(i) elif(a[i]==0 and b[i]): ob.append(i) else: no.append(i) if((len(bo)+len(oa)<k) or (len(bo)+len(ob)<k)): print(-1) else: def time_req(i): return(time[i]) no.sort(key=time_req) oa.sort(key=time_req) ob.sort(key=time_req) bo.sort(key=time_req) no.reverse() oa.reverse() ob.reverse() bo.reverse() pno=len(no)-1 pa=len(oa)-1 pb=len(ob)-1 pbo=len(bo)-1 books=set() for i in range(k): if(pa==-1 or pb==-1): books.add(bo[pbo]) pbo-=1 continue elif(pbo==-1): books.add(oa[pa]) books.add(ob[pb]) pa-=1 pb-=1 continue if(time[bo[pbo]]<(time[oa[pa]]+time[ob[pb]]) or pa==-1 or pb==-1): books.add(bo[pbo]) pbo-=1 else: books.add(oa[pa]) books.add(ob[pb]) pa-=1 pb-=1 if(m==len(books)): print(sum(list(map(time_req,list(books))))) print(*list(map(lambda x:x+1,books))) elif(len(books)>m): pa+=1 pb+=1 for i in range(n+1): if(pa==len(oa) or pb==len(ob) or len(books)==m or pbo==-1): break books.add(bo[pbo]) pbo-=1 books.remove(oa[pa]) pa+=1 books.remove(ob[pb]) pb+=1 if(len(books)!=m): print(-1) else: print(sum(list(map(time_req,list(books))))) print(*(list(map(lambda x:x+1,books)))) else: pbo+=1 ka=k kb=k for i in range(n+1): if(len(books)==m): break flag1=(pno!=-1) # adding a book no one likes flag2=(pbo!=len(bo) and pa!=-1 and pb!=-1) # adding one book each only alice likes and only bob likes and removing a book both liked flag3=(pa!=-1 and kb==k) # simply adding a book that onlyalice likes when total books liked by bob was exactly k flag4=(pb!=-1 and ka==k) # ..simply adding a book bob likes when alice liked exactly k books flag5=(pa!=-1 and kb!=k and pbo!=len(bo)) # adding only alices's book when bob had more then k liked books and being able to remove a book liked by both flag6=(pb!=-1 and ka!=k and pbo!=len(bo)) # ,, flag7=(pa!=-1 and kb!=k and pbo==len(bo)) # adding alices book when bob also had more than k, but there are no more 'both' books to be removed flag8=(pb!=-1 and ka!=k and pbo==len(bo)) # .. ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8 = [int(1e6) for i in range(8)] if(not(flag1 or flag3 or flag4 or flag2)): break if(flag1): ch1=time_req(no[pno]) if(flag2): ch2=time_req(oa[pa])+time_req(ob[pb])-time_req(bo[pbo]) if(flag3): ch3=time_req(oa[pa]) if(flag4): ch4=time_req(ob[pb]) if(flag5): ch5=time_req(oa[pa])-time_req(bo[pbo]) if(flag6): ch6=time_req(ob[pb])-time_req(bo[pbo]) if(flag7): ch7=time_req(oa[pa]) if(flag8): ch8=time_req(ob[pb]) min_change=min(ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8) if(min_change==ch1): books.add(no[pno]) pno-=1 elif(min_change==ch2): books.add(oa[pa]) pa-=1 books.add(ob[pb]) pb-=1 books.remove(bo[pbo]) pbo+=1 elif(min_change==ch3): books.add(oa[pa]) pa-=1 ka+=1 elif(min_change==ch4): books.add(ob[pb]) pb-=1 kb+=1 elif(min_change==ch5): books.add(oa[pa]) pa-=1 books.remove(bo[pbo]) pbo+=1 kb-=1 elif(min_change==ch6): books.add(ob[pb]) pb-=1 books.remove(bo[pbo]) pbo+=1 ka-=1 elif(min_change==ch7): books.add(oa[pa]) pa-=1 ka+=1 elif(min_change==ch8): books.add(ob[pb]) pb-=1 kb+=1 else: print('TNP') if(len(books)!=m): print(-1) else: print(sum(list(map(time_req,list(books))))) print(*list(map(lambda x:x+1,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" ] }
IN-CORRECT
python3
from bisect import * from collections import * from itertools import * import functools import sys import math from decimal import * from copy import * from heapq import * from fractions import * getcontext().prec = 30 MAX = sys.maxsize MAXN = 1000010 MOD = 10**9+7 spf = [i for i in range(MAXN)] spf[0]=spf[1] = -1 def sieve(): for i in range(2,MAXN,2): spf[i] = 2 for i in range(3,int(MAXN**0.5)+1): if spf[i]==i: for j in range(i*i,MAXN,i): if spf[j]==j: spf[j]=i def fib(n,m): if n == 0: return [0, 1] else: a, b = fib(n // 2) c = ((a%m) * ((b%m) * 2 - (a%m)))%m d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m if n % 2 == 0: return [c, d] else: return [d, c + d] def charIN(x= ' '): return(sys.stdin.readline().strip().split(x)) def arrIN(x = ' '): return list(map(int,sys.stdin.readline().strip().split(x))) def ncr(n,r): num=den=1 for i in range(r): num = (num*(n-i))%MOD den = (den*(i+1))%MOD return (num*(pow(den,MOD-2,MOD)))%MOD def flush(): return sys.stdout.flush() '''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*''' for _ in range(1): n,m,k = arrIN() a = [] b = [] ab = [] rem = [] for i in range(n): t,x,y = arrIN() if x&y: ab.append([t,i+1]) elif x: a.append([t,i+1]) elif y: b.append([t,i+1]) else: rem.append([t,i+1]) a.sort() b.sort() ab.sort() rem.sort() ans = 0 ansl = [] if len(a+ab)<k or len(b+ab)<k: print(-1) else: if ab: cnt = 0 cnt1 = 0 i = 0 while cnt<k and i<min(len(a),len(b)): if ab: if a[i][0]+b[i][0]<=ab[0][0]: if m-cnt1>=2: ans+=a[i][0]+b[i][0] ansl.extend([a[i][1],b[i][1]]) cnt1+=2 i+=1 else: ss = ab.pop(0) ans+=ss[0] ansl.append(ss[1]) cnt1+=1 else: ss = ab.pop(0) ans+=ss[0] ansl.append(ss[1]) cnt1+=1 else: if m-cnt1>=2: ans+=a[i][0]+b[i][0] ansl.extend([a[i][1],b[i][1]]) cnt1+=2 i+=1 else: print(-1) exit(0) cnt+=1 ans+=sum([i[0] for i in ab[:k-cnt]]) ansl.extend([i[1] for i in ab[:k-cnt]]) x = a[min(len(a),len(b)):]+b[min(len(a),len(b)):]+ab[k-cnt:]+rem x.sort() ans+=sum([i[0] for i in x[:m-cnt1]]) ansl.extend(([i[1] for i in x[:m-cnt1]])) print(ans) print(*ansl) else: if m<2*k: print(-1) else: x = a[k:]+b[k:]+rem print(sum(i[0] for i in a[:k])+sum(i[0] for i in b[:k])+sum(i[0] for i in x[:m-(2*k)])) ansl = [i[1] for i in a[:k]]+[i[1] for i in b[:k]]+[i[1] for i in x[:m-(2*k)]] print(*ansl)
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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Reading_Books_hard_version { static int []a,arr,b; static String str=""; static int ptr01=0,ptr10=0,ptr11=0,ptr00=0,ptr=0; static Integer myInf = Integer.MAX_VALUE; static int[]ans_arr; static String[]extra_array,extra_array1; static String ans_str="",extra_str=""; static int[]arr01,arr00,arr11,arr10; static int[]arr01_ind,arr10_ind,arr00_ind,arr11_ind; static int taken00,taken01,taken11,taken10; public static int smallest(int t1,int t2,int t3,int t4) { int s=(int)myInf,p=0; if(t1<ptr01&&arr01[t1]<s) {s=arr01[t1];p=1;} if(t2<ptr00&&arr00[t2]<s){s=arr00[t2];p=2;} if(t3<ptr10&&arr10[t3]<s){s=arr10[t3];p=3;} if(t4<ptr11&&arr11[t4]<s){s=arr11[t4];p=4;} else{} if(p==1){extra_str=extra_str+" "+Integer.toString((arr01_ind[taken01]));taken01++;} else if(p==2){extra_str=extra_str+" "+Integer.toString((arr00_ind[taken00]));taken00++;} else if(p==3){extra_str=extra_str+" "+Integer.toString((arr10_ind[taken10]));taken10++;} else if(p==4){extra_str=extra_str+" "+Integer.toString((arr11_ind[taken11]));taken11++;} return s; } public static void main(String[]args)throws IOException { /*Scanner sc=new Scanner (System.in); int n=sc.nextInt(),i; int k=sc.nextInt(); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { arr[i]=sc.nextInt(); a[i]=sc.nextInt(); b[i]=sc.nextInt(); }*/ int i; BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); String str=reader.readLine(); String[]array=str.split ("\\s+"); int n=Integer.parseInt(array[0]); int m=Integer.parseInt(array[1]); int k=Integer.parseInt(array[2]); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { str=reader.readLine(); array=str.split ("\\s+"); arr[i]=Integer.parseInt(array[0]); a[i]=Integer.parseInt(array[1]); b[i]=Integer.parseInt(array[2]); } int ans=func(m,k); if(ans==(int)myInf) System.out.println("-1"); else { System.out.println(ans); /*ans_str=new String (ans_str.replace ("//s+","")); ans_str=new String (ans_str.replaceAll ("//s+","")); System.out.println(ans_str);*/ ans_str=ans_str.trim(); String[]ans_array=ans_str.split("\\s+"); //System.out.println("len "+ans_array.length); for(i=0;i<ans_array.length;i++)System.out.print(ans_array[i]+" "); } //System.out.println(""); /*for(i=0;i<m;i++) if(ans_arr[i]!=0) System.out.print(ans_arr[i]+" ");*/ } public static int func(int m,int k) { int n=arr.length,i; //int[]extra_array; //ans_arr=new int [m]; //qsort_randomised(0,n-1); /*for(i=0;i<n;i++) { System.out.print(arr[i]+" "); System.out.print(a[i]+" "); System.out.print(b[i]+" "); System.out.println(""); }*/ //String extra_array=""; Boolean sort11=true,sort10=true,sort00=true,sort01=true; String[]arr01w=new String[n]; String[]arr10w=new String[n]; String[]arr00w=new String[n]; String[]arr11w=new String[n]; sort01=false;sort10=false;sort00=false;sort11=false; for(i=0;i<n;i++) { if(a[i]==0&&b[i]==1) { arr01w[ptr01]=arr[i]+" 0 1 "+i; ptr01++; } if(a[i]==1&&b[i]==0) { arr10w[ptr10]=arr[i]+" 1 0 "+i; ptr10++; } if(a[i]==1&&b[i]==1) { arr11w[ptr11]=arr[i]+ " 1 1 "+i; ptr11++; } if(a[i]==0&&b[i]==0) { arr00w[ptr00]=arr[i]+" 0 0 "+i; ptr00++; } } /*for(i=0;i<ptr01-1;i++) { if(Integer.parseInt(arr01w[i].split("\\s")[0])>Integer.parseInt(arr01w[i+1].split("\\s")[0])) { sort01=false; break; } } for(i=0;i<ptr10-1;i++) { if(Integer.parseInt(arr10w[i].split("\\s")[0])>Integer.parseInt(arr10w[i+1].split("\\s")[0])){sort10=false;break;} } for(i=0;i<ptr11-1;i++) { if(Integer.parseInt(arr11w[i].split("\\s")[0])>Integer.parseInt(arr11w[i+1].split("\\s")[0])){sort11=false;break;} } for(i=0;i<ptr00-1;i++) { if(Integer.parseInt(arr00w[i].split("\\s")[0])>Integer.parseInt(arr00w[i+1].split("\\s")[0])){sort00=false;break;} }*/ /*for(i=0;i<=ptr01;i++) { System.out.print(arr01w[i]+" hi01"); } System.out.println(""); for(i=0;i<=ptr10;i++) { System.out.print(arr10w[i]+" hi10"); } System.out.println(""); for(i=0;i<=ptr11;i++) { System.out.print(arr11w[i]+" hi11"); } System.out.println(""); */ arr01=new int[ptr01]; arr10=new int[ptr10]; arr11=new int[ptr11]; arr00=new int[ptr00]; arr01_ind=new int[ptr01]; arr10_ind=new int[ptr10]; arr11_ind=new int[ptr11]; arr00_ind=new int[ptr00]; int j,j1,t00=0,t11=0,t01=0,t10=0; int[]pre_sum01,pre_sum11,pre_sum10; StringBuilder[]ind_sum01,ind_sum11,ind_sum10,ind_sum00; pre_sum01=new int[ptr01]; pre_sum11=new int[ptr11]; pre_sum10=new int[ptr10]; ind_sum01=new StringBuilder[ptr01]; ind_sum11=new StringBuilder[ptr11]; ind_sum10=new StringBuilder[ptr10]; ind_sum00=new StringBuilder[ptr00]; /*Arrays.fill(arr01,0); Arrays.fill(arr10,0); Arrays.fill(arr11,0); Arrays.fill(arr00,0);*/ if(sort11==false) {str="11";qsort_randomised(0,ptr11-1,arr11w);} if(!sort10) {str="10";qsort_randomised(0,ptr10-1,arr10w);} if(!sort01) {str="01";qsort_randomised(0,ptr01-1,arr01w);} if(!sort00) {str="00";qsort_randomised(0,ptr00-1,arr00w);} for(i=0;i<ptr01;i++) { arr01[i]=Integer.parseInt(arr01w[i].split("\\s")[0]); arr01_ind[i]=Integer.parseInt(arr01w[i].split("\\s")[3])+1; if(ptr01>0&&i==0) { pre_sum01[0]=arr01[0]; ind_sum01[0]=new StringBuilder(""); ind_sum01[0].append(arr01_ind[0]); //ind_sum01[0].trimToSize(); } if(i>=1&&ptr01>0) { pre_sum01[i]=pre_sum01[i-1]+arr01[i]; ind_sum01[i]=new StringBuilder(""); ind_sum01[i].append(" "); ind_sum01[i].append(arr01_ind[i]); //ind_sum01[i].trimToSize(); //ind_sum01[i]=ind_sum01[i-1]+" "+Integer.toString(arr01_ind[i]); } } for(i=0;i<ptr10;i++) { arr10[i]=Integer.parseInt(arr10w[i].split("\\s")[0]); arr10_ind[i]=Integer.parseInt(arr10w[i].split("\\s")[3])+1; if(ptr10>0&&i==0) { pre_sum10[0]=arr10[0]; ind_sum10[0]=new StringBuilder(""); //ind_sum10[0]=Integer.toString(arr10_ind[0]); ind_sum10[0].append(arr10_ind[0]); //ind_sum10[0].trimToSize(); } if(i>=1&&ptr10>0) { ind_sum10[i]=new StringBuilder(""); //ind_sum10[i]=ind_sum10[i-1]+" "+Integer.toString(arr10_ind[i]); ind_sum10[i].append(" "); ind_sum10[i].append(arr10_ind[i]); pre_sum10[i]=pre_sum10[i-1]+arr10[i]; //ind_sum10[i].trimToSize(); } } for(i=0;i<ptr11;i++) { arr11[i]=Integer.parseInt(arr11w[i].split("\\s")[0]); arr11_ind[i]=Integer.parseInt(arr11w[i].split("\\s")[3])+1; if(ptr11>0&&i==0) { ind_sum11[0]=new StringBuilder(""); pre_sum11[0]=arr11[0]; //ind_sum11[0]=Integer.toString(arr11_ind[0]); ind_sum11[0].append(arr11_ind[0]); //ind_sum11[0].trimToSize(); } if(i>=1&&ptr11>0) { ind_sum11[i]=new StringBuilder(""); pre_sum11[i]=pre_sum11[i-1]+arr11[i]; //ind_sum11[i]=ind_sum11[i-1]+" "+Integer.toString(arr11_ind[i]); ind_sum11[i].append(" "); ind_sum11[i].append(arr11_ind[i]); //ind_sum11[i].trimToSize(); } } for(i=0;i<ptr00;i++) { arr00[i]=Integer.parseInt(arr00w[i].split("\\s")[0]); arr00_ind[i]=Integer.parseInt(arr00w[i].split("\\s")[3])+1; if(ptr00>0&&i==0) { ind_sum00[0]=new StringBuilder(""); ind_sum00[0].append(arr00_ind[0]); //ind_sum00[0].trimToSize(); //ind_sum00[0]=Integer.toString(arr00_ind[0]); } if(i>=1&&ptr00>0) { ind_sum00[i]=new StringBuilder(""); ind_sum00[i].append(" "); ind_sum00[i].append(arr00_ind[i]); //ind_sum00[i].trimToSize(); //ind_sum00[i]=ind_sum00[i-1]+" "+Integer.toString(arr00_ind[i]); } } /* for(i=0;i<ptr01;i++) { System.out.print(arr01[i]+" "+arr01_ind[i]+" "+pre_sum01[i]); } System.out.println(""); for(i=0;i<ptr10;i++) { System.out.print(arr10[i]+" "+arr10_ind[i]+ " "+pre_sum10[i]); } System.out.println(""); for(i=0;i<ptr11;i++) { System.out.print(arr11[i]+" "+arr11_ind[i]+" "+pre_sum11[i]); } System.out.println(""); for(i=0;i<ptr00;i++) { System.out.print(arr00[i]+" "+arr00_ind[i]+" "); }*/ /*if(ptr01>0) { pre_sum01[0]=arr01[0]; for(i=1;i<ptr01;i++) { pre_sum01[i]=pre_sum01[i-1]+arr01[i]; } } if(ptr10>0){ pre_sum10[0]=arr10[0]; for(i=1;i<ptr10;i++) { pre_sum10[i]=pre_sum10[i-1]+arr10[i]; } } if(ptr11>0) { pre_sum11[0]=arr11[0]; for(i=1;i<ptr11;i++) { pre_sum11[i]=pre_sum11[i-1]+arr11[i]; } }*/ int temp=0,min=(int)myInf,r=0,k1,s=0,a=0; String temp_str=""; ptr=0; taken11=0; taken01=0; taken00=0; taken10=0; int extra=0,k2=0,q=0; if(ptr11-1<k-1)q=ptr11-1;else q=k-1; //for(i=0;i<ptr01;i++)System.out.print(pre_sum01[i]+" "); for(i=q;i>=0&&i+2*(k-i-1)<=m;i--) { temp=0; temp_str=""; taken00=0;taken10=0;taken01=0;taken11=0; ptr=0; temp=temp+pre_sum11[i]; temp_str=ind_sum11[i].toString(); extra=0; extra_str=""; k1=i+1; taken11=i+1; if(k-k1>0) { if(ptr01>=k-k1&&ptr10>=k-k1) { if(2*(k-k1)+k1>m)return min; temp=temp+pre_sum01[k-k1-1]+pre_sum10[k-k1-1]; temp_str=temp_str+" "+ind_sum01[k-k1-1]+" " +ind_sum10[k-k1-1]; /*if(m-k>0) { }*/ taken01=k-k1; taken10=k-k1; } else return min; k2=k1+2*(k-k1); } else k2=k1; //System.out.print(" temp= "+temp); //Arrays.fill(extra_array,0); if(m-k2>0) { // extra_array=new String [m-k2]; extra=0; ptr=0; extra_str=""; for(a=0;a<m-k2;a++) { s=smallest(taken01,taken00,taken10,taken11); //System.out.println("i= "+i+ " a= "+a+" small = "+s); ptr++; extra=extra+s; } //System.out.println("extra= "+extra); //System.out.println("extra array"); //for(a=0;a<m-k2;a++)System.out.print(" "+extra_array[a]+ " "); //extra_str=extra_array.toString(); } temp=temp+extra; //System.out.println("temp= "+temp); if(min>temp) { //System.out.println("yes"); min=temp; ptr=0; ans_str=temp_str+" "+ extra_str; /*for(j=0;j<=i;j++) { ans_arr[ptr]=arr11_ind[j]+1; ptr++; } for(j=0;j<=k-k1-1;j++) { ans_arr[ptr]=arr01_ind[j]+1; ptr++; ans_arr[ptr]=arr10_ind[j]+1; ptr++; } for(j=0;j<m-k2;j++) { ans_arr[ptr]=extra_array[j]+1; ptr++; }*/ } } //for(i=0;i<m;i++)System.out.print(ans_arr[i]+" "); taken00=0;taken10=0;taken01=0;taken11=0;ptr=0;extra=0;temp=0;temp_str="";extra_str=""; if(k<=ptr01&&k<=ptr10&&k*2<=m){ temp=temp+pre_sum01[k-1]+pre_sum10[k-1]; temp_str=temp_str+" "+ind_sum01[k-1]+" "+ind_sum10[k-1]; taken01=k; taken10=k; if(m>k*2) { // extra_array=new String[m-k*2]; extra_str=""; for(a=0;a<m-k*2;a++) { s=smallest(taken01,taken00,taken10,taken11); ptr++; extra=extra+s; } //extra_str=extra_array.toString(); } temp=temp+extra; //System.out.println("temp= "+temp); if(temp<min) { min=temp; ptr=0; ans_str=temp_str+" "+extra_str; /*for(j=0;j<=k-1;j++) { ans_arr[ptr]=arr01_ind[j]+1; ptr++; ans_arr[ptr]=arr10_ind[j]+1; ptr++; } for(j=0;j<m-k*2;j++) { ans_arr[ptr]=extra_array1[j]+1; ptr++; }*/ } } /*for(i=k;i>0;i--) { temp=0; if(ptr11>=k){ temp=pre_sum11[i-1]; j1=i; } else { if(ptr11-1>=0&&ptr11-r-1>=0){ temp=pre_sum11[ptr11-r-1]; j1=ptr11; r++; } else { j1=0; } } /*for(j=0;j<i;j++) { if(j>=ptr11)break; temp=temp+arr11[j]; }*/ /*if(k-j1<ptr01||k-j1<ptr10)return temp; if(k-j1!=0) { if(k-j1<ptr01||k-j1<ptr10)return -1; }*/ //if(k-j1>ptr10||k-j1>ptr01)return min; /*for(j=0;j<k-j1;j++) { /*if(j<ptr01||j<ptr10) { if(k-j1==0) { return temp; } else { return -1; } } //if(j>=ptr01||j>=ptr10)return min; temp=temp+arr01[j]; temp=temp+arr10[j]; } if(k-j1>0){ temp=temp+pre_sum01[k-j1-1]; temp=temp+pre_sum10[k-j1-1]; } if(temp<min)min=temp; }*/ return min; } public static void qsort_randomised(int p,int r,String []arr) { if(p<r) { int q=random_partition(p,r,arr); qsort_randomised(p,q-1,arr); qsort_randomised(q+1,r,arr); } } public static int random_partition(int p,int r,String[]arr) { int i1=(int)(Math.random()*(r-p)); int i=i1+p; String temp=arr[i]; arr[i]=arr[r]; arr[r]=temp; return partition(p,r,arr); } public static int partition(int p,int r,String[]arr) { String x=arr[r]; int j; int i=p-1; for(j=p;j<=r-1;j++) { if(Integer.parseInt(arr[j].split("\\s")[0])<=Integer.parseInt(x.split("\\s")[0])) { i++; String temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } String temp=arr[i+1]; arr[i+1]=arr[r]; arr[r]=temp; return i+1; } /*public static void qsort_randomised(int p,int r,int []arr) { if(p<r) { int q=random_partition(p,r,arr); qsort_randomised(p,q-1,arr); qsort_randomised(q+1,r,arr); } } public static int random_partition(int p,int r,int[]arr) { int i1=(int)(Math.random()*(r-p)); int i=i1+p,temp1=0; if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;} else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;} else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;} else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;} int temp=arr[i]; arr[i]=arr[r]; arr[r]=temp; return partition(p,r,arr); } public static int partition(int p,int r,int[]arr) { int x=arr[r]; int j; int i=p-1,temp1; for(j=p;j<=r-1;j++) { if(arr[j]<=x) { i++; int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;} else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;} else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;} else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;} } } int temp=arr[i+1]; arr[i+1]=arr[r]; arr[r]=temp; if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;} else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;} else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;} else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;} return i+1; }*/ } //ss
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int add(long long int x, long long int y) { x += y; while (x >= 1000000007) x -= 1000000007; while (x < 0) x += 1000000007; return x; } long long int mul(long long int x, long long int y) { return (x * 1ll * y) % 1000000007; } long long int binpow(long long int x, long long int y) { long long int z = 1; while (y) { if (y & 1) z = mul(z, x); x = mul(x, x); y >>= 1; } return z; } long long int inv(long long int x) { return binpow(x, 1000000007 - 2); } long long int divide(long long int x, long long int y) { return mul(x, inv(y)); } bool comp(pair<int, pair<int, int> > p1, pair<int, pair<int, int> > p2) { return p1.first < p2.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; vector<int> al, bo, both; for (int i = 0; i < n; i++) { int a, b, c; cin >> a >> b >> c; if (b && c) both.push_back(a); else if (a) al.push_back(a); else bo.push_back(a); } sort(al.begin(), al.end()); sort(bo.begin(), bo.end()); for (int i = 0; i < min(al.size(), bo.size()); i++) both.push_back(al[i] + bo[i]); sort(both.begin(), both.end()); if (both.size() < k) cout << -1; else { long long int ans = 0; for (int i = 0; i < k; i++) ans += both[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" ] }
IN-CORRECT
python3
n,m = map(int,input().split()) one=[] two=[] both=[] for i in range(n): t,a,b = map(int,input().split()) if a==1 and b==1: both.append(t) elif a==1 and b==0: one.append(t) elif a==0 and b==1: two.append(t) if len(one)+len(both)<m: print(-1) exit() elif len(two)+len(both)<m: print(-1) exit() #print(one,two,both) ans=0 i=j=k=aa=bb=s=0 while s<m: # print(i,j) if i==len(one) or j==len(two): ans+=both[k] k+=1 elif k==len(both): ans+=one[i]+two[j] i+=1 j+=1 elif one[i]+two[j]<=both[k]: ans+=one[i]+two[j] i+=1 j+=1 else: ans+=both[k] k+=1 s+=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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, k; vector<pair<long long, long long> > vecA, vecB, vecAB, vecO; vector<long long> sumA, sumB, sumAB, sumO; multiset<long long> S; long long sumS; void make(vector<pair<long long, long long> > vec, vector<long long> &sum) { sum.push_back(0); long long s = 0; for (int i = 0; i < vec.size(); i++) { s += vec[i].first; sum.push_back(s); } } void add(long long x) { S.insert(x); sumS += x; } void del(long long x) { auto it = S.lower_bound(x); if (it == S.end() || *it != x) return; S.erase(it); sumS -= x; } void pop() { auto it = S.end(); it--; sumS -= *it; S.erase(it); } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; long long t, a, b; for (int i = 1; i <= n; i++) { cin >> t >> a >> b; if (a == 0) { if (b == 0) vecO.push_back(pair<long long, long long>(t, i)); else vecB.push_back(pair<long long, long long>(t, i)); } else { if (b == 0) vecA.push_back(pair<long long, long long>(t, i)); else vecAB.push_back(pair<long long, long long>(t, i)); } } sort(vecA.begin(), vecA.end()); sort(vecB.begin(), vecB.end()); sort(vecAB.begin(), vecAB.end()); sort(vecO.begin(), vecO.end()); make(vecA, sumA); make(vecB, sumB); make(vecAB, sumAB); make(vecO, sumO); long long ans = 1e18, ans_i; for (int i = 0; i < vecAB.size(); i++) add(vecAB[i].first); for (int i = k; i < vecA.size(); i++) add(vecA[i].first); for (int i = k; i < vecB.size(); i++) add(vecB[i].first); for (int i = 0; i < vecO.size(); i++) add(vecO[i].first); long long N = min(k, (long long)vecAB.size()); for (int i = 0; i <= N; i++) { long long tmp = sumAB[i]; long long rem = m - (i + 2 * (k - i)); while (S.size() > max(0LL, rem)) pop(); if (vecA.size() >= k - i && vecB.size() >= k - i && rem >= 0 && S.size() >= rem) { tmp += sumA[k - i] + sumB[k - i] + sumS; if (ans > tmp) { ans = tmp; ans_i = i; } } if (k - i - 1 >= 0 && k - i - 1 < vecA.size()) add(vecA[k - i - 1].first); if (k - i - 1 >= 0 && k - i - 1 < vecB.size()) add(vecB[k - i - 1].first); if (i < vecAB.size()) del(vecAB[i].first); } if (ans > 1e18 / 2) { cout << -1 << endl; return 0; } vector<long long> avec; vector<pair<long long, long long> > tmp; for (int i = 0; i < vecAB.size(); i++) { if (i < ans_i) avec.push_back(vecAB[i].second); else tmp.push_back(vecAB[i]); } for (int i = 0; i < vecA.size(); i++) { if (i < k - ans_i) avec.push_back(vecA[i].second); else tmp.push_back(vecA[i]); } for (int i = 0; i < vecB.size(); i++) { if (i < k - ans_i) avec.push_back(vecB[i].second); else tmp.push_back(vecB[i]); } for (int i = 0; i < vecO.size(); i++) tmp.push_back(vecO[i]); sort(tmp.begin(), tmp.end()); for (int i = 0; i < m - (ans_i + 2 * (k - ans_i)); i++) avec.push_back(tmp[i].second); cout << ans << endl; for (int i = 0; i < avec.size(); i++) cout << avec[i] << " "; cout << 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" ] }
IN-CORRECT
python3
n,k = map(int, input().split()) lst = [] selected = [] for _ in range(n): t,a,b = map(int, input().split()) if a and b: selected.append( (t,a,b) ) else: lst.append( (t,a,b) ) lst.sort() selected.sort() selected = selected[:k] if len(selected) < k: a = b = k - len(selected) for t in lst: if t[1] and a: selected.append(t) a -= 1 elif t[2] and b: selected.append(t) b -= 1 if a == 0 and b == 0: s = 0 for i in selected: s += i[0] print(s) else: print(-1) else: s = 0 for i in selected: s += i[0] print(s)
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" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) p1=[] a1=[] b1=[] s=0 c1=0 for _ in range(n): p,a,b=map(int,input().split()) if(a==1 and b==1): s+=p c1+=1 elif(a==1 and b==0): a1.append(p) elif(a==0 and b==1): b1.append(p) if(c1==k): print(s) elif(c1+len(a1)< k or c1+len(b1)<k ): print(-1) else: a1.sort() b1.sort() s+=sum(a1[:k-c1]) + sum(b1[:k-c1]) print(s)
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" ] }
IN-CORRECT
python3
from collections import defaultdict as dd import sys input=sys.stdin.readline #n=int(input()) n,m,kk=map(int,input().split()) l=[] ans=0 for i in range(n): time,a,b=map(int,input().split()) l.append((time,a,b,i+1)) l.sort() la=[] lb=[] do=[] no=[] for i in l: time,a,b,ind=i if(a and b): do.append((time,ind)) elif(a): la.append((time,ind)) elif(b): lb.append((time,ind)) else: no.append((time,ind)) i=0 j=0 k=0 ca=kk cb=kk res=[] while m and ca and cb: if(i<len(la)): if(j<len(lb)): if(k<len(do)): if(la[i][0]+lb[j][0]<=do[k][0] and m>=2): ans+=la[i][0] ans+=lb[j][0] res.append(la[i][1]) res.append(lb[j][1]) i+=1 j+=1 ca-=1 cb-=1 m-=2 else: ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 cb-=1 m-=1 else: if(m>=2): res.append(la[i][1]) res.append(lb[j][1]) ans+=la[i][0] ans+=lb[j][0] i+=1 j+=1 ca-=1 cb-=1 m-=2 else: break else: if(k<len(do)): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 cb-=1 m-=1 else: break else: if(k<len(do)): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 cb-=1 m-=1 else: break if(ca): while m and ca and i<len(la) and k<len(do): if(la[i][0]<=do[k][0]): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 else: ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 while ca and m and i<len(la): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 while ca and m and k<len(do): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 elif(cb): i=j la=lb ca=cb while ca and m and i<len(la) and k<len(do): if(la[i][0]<=do[k][0]): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 else: ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 while ca and m and i<len(la): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 while ca and m and k<len(do): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 if(ca or cb): print(-1) else: for ii in no: do.append(ii) do.sort() while m: if(i<len(la)): if(j<len(lb)): if(k<len(do)): if(la[i][0]+lb[j][0]<=do[k][0] and m>=2): ans+=la[i][0] ans+=lb[j][0] res.append(la[i][1]) res.append(lb[j][1]) i+=1 j+=1 ca-=1 cb-=1 m-=2 else: ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 cb-=1 m-=1 else: if(m>=2): res.append(la[i][1]) res.append(lb[j][1]) ans+=la[i][0] ans+=lb[j][0] i+=1 j+=1 ca-=1 cb-=1 m-=2 else: break else: if(k<len(do)): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 cb-=1 m-=1 else: break else: if(k<len(do)): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 cb-=1 m-=1 else: break if(i<len(la) and k<len(do)): while m and i<len(la) and k<len(do): if(la[i][0]<=do[k][0]): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 else: ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 while m and i<len(la): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 while m and k<len(do): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 elif(j<len(lb) and k<len(do)): i=j la=lb ca=cb while m and i<len(la) and k<len(do): if(la[i][0]<=do[k][0]): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 else: ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 while m and i<len(la): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 while m and k<len(do): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 elif(j<len(la) and j<len(lb)): k=j do=lb while m and i<len(la) and k<len(do): if(la[i][0]<=do[k][0]): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 else: ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 while m and i<len(la): ans+=la[i][0] res.append(la[i][1]) i+=1 ca-=1 m-=1 while m and k<len(do): ans+=do[k][0] res.append(do[k][1]) k+=1 ca-=1 m-=1 else: while m and i<len(la): ans+=la[i][0] res.append(la[i][1]) m-=1 while m and j<len(lb): ans+=lb[j][0] res.append(lb[j][1]) m-=1 while m and k<len(do): ans+=do[k][0] res.append(do[k][1]) m-=1 if(m): print(-1) else: print(ans) print(*res) ''' d=0 do=[] ka=k kb=k ans=0 ca=0 cb=0 cou=0 print(l) for i in range(n): if(a and b): do.append(time) cou+=1 ca+=1 cb+=1 elif(a and ka): ans+=time ka-=1 ca+=1 elif(b and kb): ans+=time kb-=1 cb+=1 if((ca+cb+cou)==(2*k)): break ''' #print(ans+sum(do))
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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Reading_Books_hard_version { static int []a,arr,b; static String str=""; static int ptr01=0,ptr10=0,ptr11=0,ptr00=0,ptr=0; static Integer myInf = Integer.MAX_VALUE; static int[]ans_arr; static String[]extra_array,extra_array1; static String ans_str="",extra_str=""; static int[]arr01,arr00,arr11,arr10; static int[]arr01_ind,arr10_ind,arr00_ind,arr11_ind; static int taken00,taken01,taken11,taken10; public static int smallest(int t1,int t2,int t3,int t4) { int s=(int)myInf,p=0; if(t1<ptr01&&arr01[t1]<s) {s=arr01[t1];p=1;} if(t2<ptr00&&arr00[t2]<s){s=arr00[t2];p=2;} if(t3<ptr10&&arr10[t3]<s){s=arr10[t3];p=3;} if(t4<ptr11&&arr11[t4]<s){s=arr11[t4];p=4;} else{} if(p==1){extra_str=extra_str+" "+Integer.toString((arr01_ind[taken01]));taken01++;} else if(p==2){extra_str=extra_str+" "+Integer.toString((arr00_ind[taken00]));taken00++;} else if(p==3){extra_str=extra_str+" "+Integer.toString((arr10_ind[taken10]));taken10++;} else if(p==4){extra_str=extra_str+" "+Integer.toString((arr11_ind[taken11]));taken11++;} return s; } public static void main(String[]args)throws IOException { /*Scanner sc=new Scanner (System.in); int n=sc.nextInt(),i; int k=sc.nextInt(); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { arr[i]=sc.nextInt(); a[i]=sc.nextInt(); b[i]=sc.nextInt(); }*/ int i; BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); String str=reader.readLine(); String[]array=str.split ("\\s+"); int n=Integer.parseInt(array[0]); int m=Integer.parseInt(array[1]); int k=Integer.parseInt(array[2]); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { str=reader.readLine(); array=str.split ("\\s+"); arr[i]=Integer.parseInt(array[0]); a[i]=Integer.parseInt(array[1]); b[i]=Integer.parseInt(array[2]); } int ans=func(m,k); if(ans==(int)myInf) System.out.println("-1"); else { System.out.println(ans); /*ans_str=new String (ans_str.replace ("//s+","")); ans_str=new String (ans_str.replaceAll ("//s+","")); System.out.println(ans_str);*/ ans_str=ans_str.trim(); String[]ans_array=ans_str.split("\\s+"); //System.out.println("len "+ans_array.length); for(i=0;i<ans_array.length;i++)System.out.print(ans_array[i]+" "); } //System.out.println(""); /*for(i=0;i<m;i++) if(ans_arr[i]!=0) System.out.print(ans_arr[i]+" ");*/ } public static int func(int m,int k) { int n=arr.length,i; //int[]extra_array; //ans_arr=new int [m]; //qsort_randomised(0,n-1); /*for(i=0;i<n;i++) { System.out.print(arr[i]+" "); System.out.print(a[i]+" "); System.out.print(b[i]+" "); System.out.println(""); }*/ //String extra_array=""; Boolean sort11=true,sort10=true,sort00=true,sort01=true; String[]arr01w=new String[n]; String[]arr10w=new String[n]; String[]arr00w=new String[n]; String[]arr11w=new String[n]; sort01=false;sort10=false;sort00=false;sort11=false; for(i=0;i<n;i++) { if(a[i]==0&&b[i]==1) { arr01w[ptr01]=arr[i]+" 0 1 "+i; ptr01++; } if(a[i]==1&&b[i]==0) { arr10w[ptr10]=arr[i]+" 1 0 "+i; ptr10++; } if(a[i]==1&&b[i]==1) { arr11w[ptr11]=arr[i]+ " 1 1 "+i; ptr11++; } if(a[i]==0&&b[i]==0) { arr00w[ptr00]=arr[i]+" 0 0 "+i; ptr00++; } } /*for(i=0;i<ptr01-1;i++) { if(Integer.parseInt(arr01w[i].split("\\s")[0])>Integer.parseInt(arr01w[i+1].split("\\s")[0])) { sort01=false; break; } } for(i=0;i<ptr10-1;i++) { if(Integer.parseInt(arr10w[i].split("\\s")[0])>Integer.parseInt(arr10w[i+1].split("\\s")[0])){sort10=false;break;} } for(i=0;i<ptr11-1;i++) { if(Integer.parseInt(arr11w[i].split("\\s")[0])>Integer.parseInt(arr11w[i+1].split("\\s")[0])){sort11=false;break;} } for(i=0;i<ptr00-1;i++) { if(Integer.parseInt(arr00w[i].split("\\s")[0])>Integer.parseInt(arr00w[i+1].split("\\s")[0])){sort00=false;break;} }*/ /*for(i=0;i<=ptr01;i++) { System.out.print(arr01w[i]+" hi01"); } System.out.println(""); for(i=0;i<=ptr10;i++) { System.out.print(arr10w[i]+" hi10"); } System.out.println(""); for(i=0;i<=ptr11;i++) { System.out.print(arr11w[i]+" hi11"); } System.out.println(""); */ arr01=new int[ptr01]; arr10=new int[ptr10]; arr11=new int[ptr11]; arr00=new int[ptr00]; arr01_ind=new int[ptr01]; arr10_ind=new int[ptr10]; arr11_ind=new int[ptr11]; arr00_ind=new int[ptr00]; int j,j1,t00=0,t11=0,t01=0,t10=0; int[]pre_sum01,pre_sum11,pre_sum10; StringBuilder[]ind_sum01,ind_sum11,ind_sum10,ind_sum00; pre_sum01=new int[ptr01]; pre_sum11=new int[ptr11]; pre_sum10=new int[ptr10]; ind_sum01=new StringBuilder[ptr01]; ind_sum11=new StringBuilder[ptr11]; ind_sum10=new StringBuilder[ptr10]; ind_sum00=new StringBuilder[ptr00]; /*Arrays.fill(arr01,0); Arrays.fill(arr10,0); Arrays.fill(arr11,0); Arrays.fill(arr00,0);*/ if(sort11==false) {str="11";qsort_randomised(0,ptr11-1,arr11w);} if(!sort10) {str="10";qsort_randomised(0,ptr10-1,arr10w);} if(!sort01) {str="01";qsort_randomised(0,ptr01-1,arr01w);} if(!sort00) {str="00";qsort_randomised(0,ptr00-1,arr00w);} for(i=0;i<ptr01;i++) { arr01[i]=Integer.parseInt(arr01w[i].split("\\s")[0]); arr01_ind[i]=Integer.parseInt(arr01w[i].split("\\s")[3])+1; if(ptr01>0&&i==0) { pre_sum01[0]=arr01[0]; ind_sum01[0]=new StringBuilder(""); ind_sum01[0].append(arr01_ind[0]); ind_sum01[0].trimToSize(); } if(i>=1&&ptr01>0) { pre_sum01[i]=pre_sum01[i-1]+arr01[i]; ind_sum01[i]=new StringBuilder(""); ind_sum01[i].append(" "); ind_sum01[i].append(arr01_ind[i]); ind_sum01[i].trimToSize(); //ind_sum01[i]=ind_sum01[i-1]+" "+Integer.toString(arr01_ind[i]); } } for(i=0;i<ptr10;i++) { arr10[i]=Integer.parseInt(arr10w[i].split("\\s")[0]); arr10_ind[i]=Integer.parseInt(arr10w[i].split("\\s")[3])+1; if(ptr10>0&&i==0) { pre_sum10[0]=arr10[0]; ind_sum10[0]=new StringBuilder(""); //ind_sum10[0]=Integer.toString(arr10_ind[0]); ind_sum10[0].append(arr10_ind[0]); ind_sum10[0].trimToSize(); } if(i>=1&&ptr10>0) { ind_sum10[i]=new StringBuilder(""); //ind_sum10[i]=ind_sum10[i-1]+" "+Integer.toString(arr10_ind[i]); ind_sum10[i].append(" "); ind_sum10[i].append(arr10_ind[i]); pre_sum10[i]=pre_sum10[i-1]+arr10[i]; ind_sum10[i].trimToSize(); } } for(i=0;i<ptr11;i++) { arr11[i]=Integer.parseInt(arr11w[i].split("\\s")[0]); arr11_ind[i]=Integer.parseInt(arr11w[i].split("\\s")[3])+1; if(ptr11>0&&i==0) { ind_sum11[0]=new StringBuilder(""); pre_sum11[0]=arr11[0]; //ind_sum11[0]=Integer.toString(arr11_ind[0]); ind_sum11[0].append(arr11_ind[0]); ind_sum11[0].trimToSize(); } if(i>=1&&ptr11>0) { ind_sum11[i]=new StringBuilder(""); pre_sum11[i]=pre_sum11[i-1]+arr11[i]; //ind_sum11[i]=ind_sum11[i-1]+" "+Integer.toString(arr11_ind[i]); ind_sum11[i].append(" "); ind_sum11[i].append(arr11_ind[i]); ind_sum11[i].trimToSize(); } } for(i=0;i<ptr00;i++) { arr00[i]=Integer.parseInt(arr00w[i].split("\\s")[0]); arr00_ind[i]=Integer.parseInt(arr00w[i].split("\\s")[3])+1; if(ptr00>0&&i==0) { ind_sum00[0]=new StringBuilder(""); ind_sum00[0].append(arr00_ind[0]); ind_sum00[0].trimToSize(); //ind_sum00[0]=Integer.toString(arr00_ind[0]); } if(i>=1&&ptr00>0) { ind_sum00[i]=new StringBuilder(""); ind_sum00[i].append(" "); ind_sum00[i].append(arr00_ind[i]); ind_sum00[i].trimToSize(); //ind_sum00[i]=ind_sum00[i-1]+" "+Integer.toString(arr00_ind[i]); } } /* for(i=0;i<ptr01;i++) { System.out.print(arr01[i]+" "+arr01_ind[i]+" "+pre_sum01[i]); } System.out.println(""); for(i=0;i<ptr10;i++) { System.out.print(arr10[i]+" "+arr10_ind[i]+ " "+pre_sum10[i]); } System.out.println(""); for(i=0;i<ptr11;i++) { System.out.print(arr11[i]+" "+arr11_ind[i]+" "+pre_sum11[i]); } System.out.println(""); for(i=0;i<ptr00;i++) { System.out.print(arr00[i]+" "+arr00_ind[i]+" "); }*/ /*if(ptr01>0) { pre_sum01[0]=arr01[0]; for(i=1;i<ptr01;i++) { pre_sum01[i]=pre_sum01[i-1]+arr01[i]; } } if(ptr10>0){ pre_sum10[0]=arr10[0]; for(i=1;i<ptr10;i++) { pre_sum10[i]=pre_sum10[i-1]+arr10[i]; } } if(ptr11>0) { pre_sum11[0]=arr11[0]; for(i=1;i<ptr11;i++) { pre_sum11[i]=pre_sum11[i-1]+arr11[i]; } }*/ int temp=0,min=(int)myInf,r=0,k1,s=0,a=0; String temp_str=""; ptr=0; taken11=0; taken01=0; taken00=0; taken10=0; int extra=0,k2=0,q=0; if(ptr11-1<k-1)q=ptr11-1;else q=k-1; //for(i=0;i<ptr01;i++)System.out.print(pre_sum01[i]+" "); for(i=q;i>=0&&i+2*(k-i-1)<=m;i--) { temp=0; temp_str=""; taken00=0;taken10=0;taken01=0;taken11=0; ptr=0; temp=temp+pre_sum11[i]; temp_str=temp_str+" "+ind_sum11[i]; extra=0; extra_str=""; k1=i+1; taken11=i+1; if(k-k1>0) { if(ptr01>=k-k1&&ptr10>=k-k1) { if(2*(k-k1)+k1>m)return min; temp=temp+pre_sum01[k-k1-1]+pre_sum10[k-k1-1]; temp_str=temp_str+" "+ind_sum01[k-k1-1]+" " +ind_sum10[k-k1-1]; /*if(m-k>0) { }*/ taken01=k-k1; taken10=k-k1; } else return min; k2=k1+2*(k-k1); } else k2=k1; //System.out.print(" temp= "+temp); //Arrays.fill(extra_array,0); if(m-k2>0) { // extra_array=new String [m-k2]; extra=0; ptr=0; extra_str=""; for(a=0;a<m-k2;a++) { s=smallest(taken01,taken00,taken10,taken11); //System.out.println("i= "+i+ " a= "+a+" small = "+s); ptr++; extra=extra+s; } //System.out.println("extra= "+extra); //System.out.println("extra array"); //for(a=0;a<m-k2;a++)System.out.print(" "+extra_array[a]+ " "); //extra_str=extra_array.toString(); } temp=temp+extra; //System.out.println("temp= "+temp); if(min>temp) { //System.out.println("yes"); min=temp; ptr=0; ans_str=temp_str+" "+ extra_str; /*for(j=0;j<=i;j++) { ans_arr[ptr]=arr11_ind[j]+1; ptr++; } for(j=0;j<=k-k1-1;j++) { ans_arr[ptr]=arr01_ind[j]+1; ptr++; ans_arr[ptr]=arr10_ind[j]+1; ptr++; } for(j=0;j<m-k2;j++) { ans_arr[ptr]=extra_array[j]+1; ptr++; }*/ } } //for(i=0;i<m;i++)System.out.print(ans_arr[i]+" "); taken00=0;taken10=0;taken01=0;taken11=0;ptr=0;extra=0;temp=0;temp_str="";extra_str=""; if(k<=ptr01&&k<=ptr10&&k*2<=m){ temp=temp+pre_sum01[k-1]+pre_sum10[k-1]; temp_str=temp_str+" "+ind_sum01[k-1]+" "+ind_sum10[k-1]; taken01=k; taken10=k; if(m>k*2) { // extra_array=new String[m-k*2]; extra_str=""; for(a=0;a<m-k*2;a++) { s=smallest(taken01,taken00,taken10,taken11); ptr++; extra=extra+s; } //extra_str=extra_array.toString(); } temp=temp+extra; //System.out.println("temp= "+temp); if(temp<min) { min=temp; ptr=0; ans_str=temp_str+" "+extra_str; /*for(j=0;j<=k-1;j++) { ans_arr[ptr]=arr01_ind[j]+1; ptr++; ans_arr[ptr]=arr10_ind[j]+1; ptr++; } for(j=0;j<m-k*2;j++) { ans_arr[ptr]=extra_array1[j]+1; ptr++; }*/ } } /*for(i=k;i>0;i--) { temp=0; if(ptr11>=k){ temp=pre_sum11[i-1]; j1=i; } else { if(ptr11-1>=0&&ptr11-r-1>=0){ temp=pre_sum11[ptr11-r-1]; j1=ptr11; r++; } else { j1=0; } } /*for(j=0;j<i;j++) { if(j>=ptr11)break; temp=temp+arr11[j]; }*/ /*if(k-j1<ptr01||k-j1<ptr10)return temp; if(k-j1!=0) { if(k-j1<ptr01||k-j1<ptr10)return -1; }*/ //if(k-j1>ptr10||k-j1>ptr01)return min; /*for(j=0;j<k-j1;j++) { /*if(j<ptr01||j<ptr10) { if(k-j1==0) { return temp; } else { return -1; } } //if(j>=ptr01||j>=ptr10)return min; temp=temp+arr01[j]; temp=temp+arr10[j]; } if(k-j1>0){ temp=temp+pre_sum01[k-j1-1]; temp=temp+pre_sum10[k-j1-1]; } if(temp<min)min=temp; }*/ return min; } public static void qsort_randomised(int p,int r,String []arr) { if(p<r) { int q=random_partition(p,r,arr); qsort_randomised(p,q-1,arr); qsort_randomised(q+1,r,arr); } } public static int random_partition(int p,int r,String[]arr) { int i1=(int)(Math.random()*(r-p)); int i=i1+p; String temp=arr[i]; arr[i]=arr[r]; arr[r]=temp; return partition(p,r,arr); } public static int partition(int p,int r,String[]arr) { String x=arr[r]; int j; int i=p-1; for(j=p;j<=r-1;j++) { if(Integer.parseInt(arr[j].split("\\s")[0])<=Integer.parseInt(x.split("\\s")[0])) { i++; String temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } String temp=arr[i+1]; arr[i+1]=arr[r]; arr[r]=temp; return i+1; } /*public static void qsort_randomised(int p,int r,int []arr) { if(p<r) { int q=random_partition(p,r,arr); qsort_randomised(p,q-1,arr); qsort_randomised(q+1,r,arr); } } public static int random_partition(int p,int r,int[]arr) { int i1=(int)(Math.random()*(r-p)); int i=i1+p,temp1=0; if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;} else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;} else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;} else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;} int temp=arr[i]; arr[i]=arr[r]; arr[r]=temp; return partition(p,r,arr); } public static int partition(int p,int r,int[]arr) { int x=arr[r]; int j; int i=p-1,temp1; for(j=p;j<=r-1;j++) { if(arr[j]<=x) { i++; int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;} else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;} else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;} else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;} } } int temp=arr[i+1]; arr[i+1]=arr[r]; arr[r]=temp; if(str.equals("00")) {temp1=arr00_ind[i];arr00_ind[i]=arr00_ind[r];arr00_ind[r]=temp1;} else if(str.equals("01")){ temp1=arr01_ind[i];arr01_ind[i]=arr01_ind[r];arr01_ind[r]=temp1;} else if(str.equals("10")){temp1=arr10_ind[i];arr10_ind[i]=arr10_ind[r];arr10_ind[r]=temp1;} else if(str.equals("11")) {temp1=arr11_ind[i];arr11_ind[i]=arr11_ind[r];arr11_ind[r]=temp1;} return i+1; }*/ } //ss
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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; public class E { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); int k = sc.nextInt(); int n = t; PriorityQueue<Integer> both = new PriorityQueue<Integer>(); PriorityQueue<Integer> bob = new PriorityQueue<Integer>(); PriorityQueue<Integer> alice = new PriorityQueue<Integer>(); first: while (t-- != 0) { int c = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if(a == 1 && b == 1) both.add(c); else if(a == 1) { alice.add(c); } else if(b == 1) { bob.add(c); } } long total = 0; long book = 0; while(!both.isEmpty() || (!alice.isEmpty() && !bob.isEmpty())) { int one = -1; if(!both.isEmpty()) one = both.peek(); int a = -1,b = -1; if((!alice.isEmpty() && !bob.isEmpty())){ a = alice.peek(); b = bob.peek(); } if(one == -1) { total+=a+b; alice.poll(); bob.poll(); } else if(a == -1) { total+=one; both.poll(); } else { if(one<=a+b) { total+=one; both.poll(); } else { total+=a+b; alice.poll(); bob.poll(); } } book+=1; } if(book < k) { System.out.println(-1); } else System.out.println(total); } static void shuffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static int min(int a[]) { int x = 1_000_000_00_9; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static int max(int a[]) { int x = -1_000_000_00_9; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static long min(long a[]) { long x = (long) 3e18; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static long max(long a[]) { long x = -(long) 3e18; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y, long mod) { long res = 1; x %= mod; while (y > 0) { if (y % 2 == 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } static void intsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void longsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverseintsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverselongsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static class longpair implements Comparable<longpair> { long x, y; longpair(long x, long y) { this.x = x; this.y = y; } public int compareTo(longpair p) { return Long.compare(this.x, p.x); } } static class intpair implements Comparable<intpair> { int x, y; intpair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(intpair o) { return Integer.compare(this.x, o.x); } // a = new pair [n]; // a[i] = new pair(coo,cost); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int inf = 1000000000; const long long int MOD = 1000000007; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); register long long int i, j, k, x, y, m, n, t, q, temp, sum = 0, count = 0, f = 1; cin >> n >> k; vector<long long int> both; vector<long long int> a1, b1; for (i = 0; i < n; i++) { cin >> t >> x >> y; if (x == y && x == 1) { both.push_back(t); } else if (x == 1) { a1.push_back(t); } else { b1.push_back(t); } } sort(a1.begin(), a1.end()); sort(b1.begin(), b1.end()); long long int n1 = min(a1.size(), b1.size()); for (i = 0; i < n1; i++) { both.push_back(a1[i] + b1[i]); } sort(both.begin(), both.end()); if (both.size() < k) cout << "-1"; else { for (i = 0; i < both.size(); i++) sum += both[i]; cout << 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; int suma, sumb; priority_queue<int> both, alice, bob; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { int t, a, b; cin >> t >> a >> b; if (a + b == 2) { both.push(t); } else if (a == 1) { alice.push(t); } else if (b == 1) { bob.push(t); } suma += a; sumb += b; } if (suma < k || sumb < k) { cout << "-1\n"; return 0; } long long ans = 0; int a = 0, b = 0; while (a < k || b < k) { long long C1 = 1e12, C2 = 1e12, C3 = 1e12; if (both.size()) { C1 = both.top(); C2 = C1, C3 = 0; } if (alice.size()) { C2 = alice.top(); } if (bob.size()) { C3 = bob.top(); } if (C1 <= C2 + C3 && C1 != 1e12) { a++, b++; ans += C1; both.pop(); } else { if (a < k) { a++; ans += C2; alice.pop(); } if (b < k) { b++; ans += C3; bob.pop(); } } } 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" ] }
IN-CORRECT
python3
n, k = map(int,input().split()) listt = [] for z in range(n): a,b,c = map(int,input().split()) listt.append([a,b,c]) both = [] no = [] a = [] b = [] for x in listt: if x[1] == 1 and x[2] == 1: both.append(x[0]) elif x[1] == 1: a.append(x[0]) elif x[2] == 1: b.append(x[0]) else: no.append(x[0]) if len(both)>1: both.sort() both = both[:k] ans = 0 a_c = 0 b_c = 0 ai = 0 bi = 0 bothi = 0 count = k # print(a) # print(b) # print(both) # print(no) if len(a) == 0 or len(b) == 0: print(sum(both)) elif len(a)+len(both)<k or len(b)+len(both)<k: print('-1') else: a.sort() b.sort() a.append(100000) b.append(100000) both.append(100000) flag = 0 while count>0: if a[ai] == 100000 and b[bi] == 100000 and both[bothi] == 100000: flag = 1 break if a[ai] < both[bothi] and b[bi] < both[bothi]: if a_c < k and b_c < k: ans += a[ai]+b[bi] a_c += 1 b_c += 1 elif a_c < k: ans += a[ai] a_c += 1 else: ans += b[bi] b_c += 1 count -= 1 else: ans+= both[bothi] bothi += 1 count -= 1 a_c += 1 b_c += 1 if flag == 0: 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { return in >> a.first >> a.second; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } ifstream cinn("in.txt"); ofstream coutt("out.txt"); long long poww(const long long &a, long long b, const long long &m = (1000000007)) { if (b == 0) return 1; long long x = poww(a, b / 2, m); x = x * x % m; if (b & 1) x = x * a % m; return x; } long long ceil(const long long &a, const long long &b) { return (a + b - 1) / b; } long long n, k; long long t[200005], a[200005], b[200005]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; vector<long long> oo, oz, zo; long long wa1 = 0, wa2 = 0; for (long long i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] + b[i] == 2) oo.push_back(t[i]); else if (a[i]) oz.push_back(t[i]); else if (b[i]) zo.push_back(t[i]); wa1 += a[i]; wa2 += b[i]; } if (wa1 < k || wa2 < k) { cout << -1; return 0; } sort((oo).begin(), (oo).end()); sort((oz).begin(), (oz).end()); sort((zo).begin(), (zo).end()); long long p1 = k, p2 = k, ans = 0; for (long long i : oo) { ans += i; p1--; p2--; if (p1 <= 0 && p2 <= 0) break; } for (long long i = 0, j = 0; p1 > 0 || p2 > 0; i++, j++) { p1--; p2--; if (i < oz.size() && p1 >= 0) ans += oz[i]; if (j < zo.size() && p2 >= 0) ans += zo[j]; } cout << 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e5 + 10; int n, m, T, k; vector<pair<int, int>> a, b, ab, other; vector<int> ans; int check(int mid) { if ((int)a.size() < k - mid) return 2e9 + 1; if ((int)b.size() < k - mid) return 2e9 + 1; if (mid + max(0, k - mid) * 2 > m) return 2e9 + 1; int nd = m - mid, res = 0; ans.clear(); vector<pair<int, int>> vec; for (int i = 0; i < mid; i++) res += ab[i].first, ans.push_back(ab[i].second); for (int i = mid; i < ab.size(); i++) vec.push_back(ab[i]); for (int i = 0; i < k - mid; i++) res += a[i].first + b[i].first, nd -= 2, ans.push_back(a[i].second), ans.push_back(b[i].second); for (int i = max(0, k - mid); i < a.size(); i++) vec.push_back(a[i]); for (int i = max(0, k - mid); i < b.size(); i++) vec.push_back(b[i]); for (int i = 0; i < other.size(); i++) vec.push_back(other[i]); sort(vec.begin(), vec.end()); for (int i = 0; i < nd && i < vec.size(); i++) res += vec[i].first, ans.push_back(vec[i].second); return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; for (int i = 1, x1, x2, x3; i <= n; ++i) { cin >> x1 >> x2 >> x3; if (x2 && x3) ab.push_back({x1, i}); else if (x2) a.push_back({x1, i}); else if (x3) b.push_back({x1, i}); else other.push_back({x1, i}); } sort(a.begin(), a.end()), sort(b.begin(), b.end()), sort(ab.begin(), ab.end()), sort(other.begin(), other.end()); int l = 0, r = min((int)ab.size(), m); while (r - l > 10) { int midl = l + (r - l) / 3, midr = r - (r - l) / 3; if (check(midl) >= check(midr)) l = midl; else r = midr; } int res = 1e9, Min_idx = 0; for (int i = l; i <= r; i++) { int tmp = check(i); if (tmp < res) res = tmp, Min_idx = i; } if (res == 1e9) return puts("-1"), 0; ans.clear(); cout << check(Min_idx) << endl; for (auto a : ans) cout << a << " "; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using Book = pair<int, int>; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<Book> d[4]; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; d[a * 2 + b].emplace_back(t, i + 1); } int s = max(2 * k - m, 0); s += max(k - s - (int)min(d[1].size(), d[2].size()), 0); if (s > (int)d[3].size()) { cout << "-1\n"; return 0; } s += max(m - s - (int)(d[0].size() + d[1].size() + d[2].size()), 0); m -= s; k = max(k - s, 0); for (auto& v : d) sort(v.begin(), v.end()); int sum = 0; unordered_set<int> ids; for (int i = 0; i < s; ++i) { sum += d[3][i].first; ids.insert(d[3][i].second); } priority_queue<Book> taken; priority_queue<Book, vector<Book>, greater<Book>> spare; for (int t = 0; t < 3; ++t) { for (int i = 0; i < (int)d[t].size(); ++i) { if (t > 0 && i < k) { sum += d[t][i].first; ids.insert(d[t][i].second); taken.push(d[t][i]); } else { spare.push(d[t][i]); } } } for (int i = 0; i < m - 2 * k; ++i) { assert(!spare.empty()); sum += spare.top().first; ids.insert(spare.top().second); taken.push(spare.top()); spare.pop(); } vector<int> q; for (int i = 0; i < m && s + i < (int)d[3].size(); ++i) { int delta = d[3][s + i].first; q.push_back(d[3][s + i].second); for (int t = 0; t < 2 && i + t < m; ++t) { assert(!taken.empty()); delta -= taken.top().first; q.push_back(-taken.top().second); spare.push(taken.top()); taken.pop(); } if (i + 1 < m) { assert(!spare.empty()); delta += spare.top().first; q.push_back(spare.top().second); taken.push(spare.top()); spare.pop(); } if (delta < 0) { sum += delta; for (int id : q) { if (id > 0) ids.insert(id); else ids.erase(-id); } } } cout << sum << '\n'; for (int id : ids) cout << id << ' '; 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" ] }
IN-CORRECT
python3
import sys inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 n, k = inp[ii: ii + 2]; ii += 2 both, a, b = [], [], [] for i in range(n): x, p, q = inp[ii: ii + 3]; ii += 3 if p and q: both.append(x) elif p: a.append(x) elif q: b.append(x) if len(both) + len(a) < k or len(both) + len(b) < k: print(-1) else: res = 2 * 10**9 both.sort(); a.sort(); b.sort() cnt, tot, i, j = len(both), sum(both[0: min(len(both), k)]), 0, 0 for cnt in range(min(len(both), k), max(k - min(len(a), len(b)), 0) - 1, -1): while i < k - cnt: tot += a[i] i += 1 while j < k - cnt: tot += b[j] j += 1 print(cnt, i, j, tot) res = min(res, tot) if cnt > 0: tot -= both[cnt - 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" ] }
IN-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.PriorityQueue; 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=b; val=a; } @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 m=sc.nextInt(); int k=sc.nextInt(); ArrayList<pair>alice=new ArrayList<>(); ArrayList<pair>bob=new ArrayList<>(); ArrayList<pair>both=new ArrayList<>(); ArrayList<pair>non=new ArrayList<>(); for(int i=0;i<n;i++) { pair ti=new pair(sc.nextInt(),i); 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); else non.add(ti); } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); StringBuilder st=new StringBuilder(); if(alice.size()+both.size()<k || bob.size()+both.size()<k || m<k) { out.append(-1+"\n");continue; } int i=0,j=0,l=0; int a=0,b=0; int ans=0; boolean ok=true; while(a<k && i<alice.size() && j<bob.size() && l<both.size()) { if(k==m) { if(both.size()<k) { ok=false; break; } while(k>0) { ans+=both.get(l).val; st.append((both.get(l).id+1)+" ");l++; k--; m--; } break; } if(alice.get(i).val+bob.get(j).val<=both.get(l).val) { ans+=alice.get(i).val+bob.get(j).val; st.append((alice.get(i).id+1)+" "); st.append((bob.get(j).id+1)+" "); i++; j++; m-=2; } else { ans+=both.get(l).val; st.append((both.get(l).id+1)+" "); l++; m-=1; } k--; } if(k>0) { if(i==alice.size() || j==bob.size()) { while(k>0) { ans+=both.get(l).val; st.append((both.get(l).id+1)+" "); l++; k--; m-=1; } } else if(l==both.size()) { while(k>0) { ans+=alice.get(i).val+bob.get(j).val; st.append((alice.get(i).id+1)+" "); st.append((bob.get(j).id+1)+" "); i++; j++; k--; m-=2; } } } if(m<0) { ok=false; } if(!ok) {out.append(-1+"\n");continue;} PriorityQueue<pair>pq=new PriorityQueue<>(); while(i<alice.size()) { pq.add(alice.get(i));i++; } while(j<bob.size()) { pq.add(bob.get(j));j++; } while(l<both.size()) { pq.add(both.get(l));l++; } i=0; while(i<non.size()){ pq.add(non.get(i));i++; } while(m>0) { pair p=pq.poll(); ans+=p.val; st.append((p.id+1)+" "); m--; } out.append(ans+"\n"+st+"\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" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class LiftRequests { public static void main(String[] args) { // Scanner scan = new Scanner(System.in);//ew int n = sc.nextInt(); Scanner scan = new Scanner(System.in); // PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); int k = scan.nextInt(); int a[][]=new int[n][3]; int ab=0,a1=0,b=0; for(int i=0;i<n;i++) { a[i][0]=scan.nextInt(); a[i][1]=scan.nextInt(); a[i][2]=scan.nextInt(); if(a[i][1]==1 && a[i][2]==1) { ab++; } else if(a[i][1]==1) a1++; else b++; } int temp=Math.min(a1, b)+ab; if(temp>=k){ int ab1[]=new int[ab]; int b1[]=new int[b]; int a2[]=new int[a1]; int r=0,q=0,p=0; for(int i=0;i<n;i++) { if(a[i][1]==1 && a[i][2]==1) { ab1[p]=a[i][0]; p++; } else if(a[i][1]==1) { a2[q]=a[i][0]; q++; } else { b1[r]=a[i][0]; r++; } } Arrays.sort(ab1); Arrays.sort(b1); Arrays.sort(a2); int d1=0,d2=0,d3=0; long sum=0; for(int i=0;i<k;i++) { if(d1<a1 && d2<b && d3<ab &&((a2[d1]+b1[d2])>ab1[d3])) { sum=sum+ab1[d3]; d3++; } else if(d1<a1 && d2<b && d3<ab &&((a2[d1]+b1[d2])<ab1[d3])) { sum=sum+a2[d1]+b1[d2]; d1++; d2++; } else if(d1>=a1 || d2>=b && d3<ab) { sum=sum+ab1[d3]; d3++; } else { sum=sum+a2[d1]+b1[d2]; d1++; d2++; } } System.out.println(sum); } else System.out.println(-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" ] }
IN-CORRECT
python3
from itertools import accumulate def f(a): return list(accumulate(sorted(a))) def main(): n, k = map(int, input().split()) g10, g01, g11 = [], [], [] for i in range(1, n + 1): t, a, b = map(int, input().split()) if a == b == 1: g11.append(t) elif a == 1 and b == 0: g10.append(t) elif a == 0 and b == 1: g01.append(t) g10 = f(g10) g01 = f(g01) g11 = f(g11) m = min(len(g10), len(g01)) ans = 10 ** 10 for i, t in enumerate(g11): r = k - (i + 1) if r < 0: break if r > m: continue ans = min(ans, t + g10[r - 1] + g01[r - 1]) if r != 0 else min(ans, t) if ans == 10 ** 10: print(-1) else: print(ans) 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" ] }
IN-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(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.remove(); else return p; } return null; } static pair high(PriorityQueue<pair> pq) { while(!pq.isEmpty()) { pair p=pq.peek(); if(!p.inc) pq.remove(); 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" ] }
IN-CORRECT
java
import java.util.*; public class ReadingBooksEasy { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); LinkedList<Integer> both = new LinkedList<>(); LinkedList<Integer> alice = new LinkedList<>(); LinkedList<Integer> bob = new LinkedList<>(); LinkedList<Integer> noone = new LinkedList<>(); HashMap<String, LinkedList<Integer>> set = new HashMap<>(); for (int i = 0; i < n; i++) { int t = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); String s = String.valueOf(t) + String.valueOf(a) + String.valueOf(b); LinkedList<Integer> ll = set.get(s); if (ll == null) { ll = new LinkedList<>(); } ll.add(i + 1); set.put(s, ll); if (a == 1 && b == 1) { both.add(t); continue; } if (a == 1 && b == 0) { alice.add(t); continue; } if (a == 0 && b == 1) { bob.add(t); continue; } noone.add(t); } int cboth = both.size(); int calice = alice.size(); int cbob = bob.size(); if (cboth + calice < k || cboth + cbob < k) { System.out.println("-1"); return; } Collections.sort(both); Collections.sort(alice); Collections.sort(bob); int count = k; long ans = 0; int max = Integer.MAX_VALUE; while (count != 0) { int bv = max; if (!both.isEmpty()) bv = both.get(0); int ab = max; if (!alice.isEmpty() && !bob.isEmpty()) ab = alice.get(0) + bob.get(0); if ((ab <= bv && m != 1) || (ab != max && m == 2 && both.size() < 2)) { int a = alice.remove(0); int b = bob.remove(0); ans += a + b; String s = String.valueOf(a) + String.valueOf("10"); LinkedList<Integer> ll = set.get(s); int index = ll.remove(0); set.put(s, ll); sb.append(index + " "); s = String.valueOf(b) + String.valueOf("01"); ll = set.get(s); index = ll.remove(0); set.put(s, ll); sb.append(index + " "); m -= 2; count--; } else { if (both.isEmpty()) { System.out.println("-1"); return; } int a = both.remove(0); ans += a; String s = String.valueOf(a) + String.valueOf("11"); LinkedList<Integer> ll = set.get(s); int index = ll.remove(0); set.put(s, ll); sb.append(index + " "); count--; m -= 1; } } if (m > 0) { Collections.sort(noone); } while (m > 0) { int bv = max; if (!both.isEmpty()) bv = both.get(0); int al = max; if (!alice.isEmpty()) { al = alice.get(0); } int bb = max; if (!bob.isEmpty()) { bb = bob.get(0); } int nn = max; if (!noone.isEmpty()) { nn = noone.get(0); } int min = getMin(bv, al, bb, nn); if (bv == min) { int a = both.remove(0); ans += a; String s = String.valueOf(a) + String.valueOf("11"); LinkedList<Integer> ll = set.get(s); int index = ll.remove(0); set.put(s, ll); sb.append(index + " "); m--; } else if (al == min) { int a = alice.remove(0); ans += a; String s = String.valueOf(a) + String.valueOf("10"); LinkedList<Integer> ll = set.get(s); int index = ll.remove(0); set.put(s, ll); sb.append(index + " "); m--; } else if (bb == min) { int a = bob.remove(0); ans += a; String s = String.valueOf(a) + String.valueOf("01"); LinkedList<Integer> ll = set.get(s); int index = ll.remove(0); set.put(s, ll); sb.append(index + " "); m--; } else if (nn == min) { int a = noone.remove(0); ans += a; String s = String.valueOf(a) + String.valueOf("00"); LinkedList<Integer> ll = set.get(s); int index = ll.remove(0); set.put(s, ll); sb.append(index + " "); m--; } } System.out.println(ans); System.out.println(sb); } static int getMin(int a, int b, int c, int d) { return Math.min(Math.min(Math.min(a, b), c), d); } }
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" ] }
IN-CORRECT
python3
# https://codeforces.com/contest/1374/problem/E1 def min_time(tot_books, books_like, read_time, a_time, b_time): time = [] temp_a = [] temp_b = [] if min(sum(a_time), sum(b_time)) >= books_like: for x in range(tot_books): if a_time[x] == b_time[x] == 1: time.append(read_time[x]) elif a_time[x] == 0 and b_time[x] == 1: temp_b.append(read_time[x]) elif a_time[x] == 1 and b_time[x] == 0: temp_a.append(read_time[x]) if len(time) > books_like: print(time, ) time.sort(reverse=True) time = time[len(time) - books_like:] temp_a.sort(), temp_b.sort() y = 0 while y != (min(len(temp_a), len(temp_b))): # print(min(len(temp_a), len(temp_b))) # print(y) # print(time, temp_a, temp_b) if len(time) != books_like: time.append(temp_a[y] + temp_b[y]) del (temp_a[y], temp_b[y]) y -= 1 if len(time) == books_like: y = -1 elif temp_a[y] + temp_b[y] < time[y]: time[y] = temp_a[y] + temp_b[y] else: break y += 1 # print(time, temp_a, temp_b) return sum(time) else: return -1 n, k = map(int, input().split()) t = [] a = [] b = [] for i in range(n): x, y, z = map(int, input().split()) t.append(x), a.append(y), b.append(z) print(min_time(n, k, t, a, b))
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Book { int i; long long t = 0; int a = 0; int b = 0; Book() {} Book(int i, long long t, int a, int b) { this->i = i; this->t = t; this->a = a; this->b = b; } }; struct Result { long long sum = -1; vector<int> chosenBooks{}; Result() {} Result(long long a, vector<int> b) { this->sum = a; this->chosenBooks = b; } Result(const Result& r) { this->sum = r.sum; this->chosenBooks = r.chosenBooks; } }; static int calc_t(const vector<int>& xs, const vector<Book>& books) { int t = 0; for (auto x : xs) t += books[x].t; return t; } static int calc_t(const set<int>& xs, const vector<Book>& books) { int t = 0; for (auto x : xs) t += books[x].t; return t; } static string to_key(const vector<int>& ys) { vector<int> xs = ys; sort(xs.begin(), xs.end(), [](const int& a, const int& b) { return a < b; }); string s = ""; for (auto x : xs) { s += to_string(x) + "@"; } return s; } static Result f(int likes, int b2i, int babi, vector<int>* chosenBooks, map<string, Result>* result_lookup, const int min_likes, const int num_books, const vector<int>& b2, const vector<vector<int>>& bab, const vector<Book>& books) { if (result_lookup->find(to_key(*chosenBooks)) != result_lookup->end()) { return Result((*result_lookup)[to_key(*chosenBooks)]); } if (likes < min_likes && chosenBooks->size() < num_books) { if (babi < bab.size() && chosenBooks->size() + 2 <= num_books && b2i < b2.size()) { auto c1 = books[b2[b2i]].t; auto c2 = calc_t(bab[babi], books); if (0 && likes >= min_likes - 2) { auto ss1 = *chosenBooks; ss1.push_back(b2[b2i]); Result r1 = f(likes + 1, b2i + 1, babi, &ss1, result_lookup, min_likes, num_books, b2, bab, books); (*result_lookup)[to_key(ss1)] = r1; auto ss2 = *chosenBooks; for (auto x : bab[babi]) { ss2.push_back(x); } Result r2 = f(likes + 1, b2i, babi + 1, &ss2, result_lookup, min_likes, num_books, b2, bab, books); (*result_lookup)[to_key(ss2)] = r2; if (r1.sum == -1) return r2; if (r2.sum == -1) return r1; return r1.sum < r2.sum ? r1 : r2; } else { if (c2 < c1) { for (auto x : bab[babi]) { chosenBooks->push_back(x); } auto r1 = f(likes + 1, b2i, babi + 1, chosenBooks, result_lookup, min_likes, num_books, b2, bab, books); (*result_lookup)[to_key(*chosenBooks)] = r1; return r1; } else { chosenBooks->push_back(b2[b2i]); auto r1 = f(likes + 1, b2i + 1, babi, chosenBooks, result_lookup, min_likes, num_books, b2, bab, books); (*result_lookup)[to_key(*chosenBooks)] = r1; return r1; } } } else if (babi < bab.size() && chosenBooks->size() + 2 <= num_books) { for (auto x : bab[babi]) { chosenBooks->push_back(x); } auto r1 = f(likes + 1, b2i, babi + 1, chosenBooks, result_lookup, min_likes, num_books, b2, bab, books); (*result_lookup)[to_key(*chosenBooks)] = r1; return r1; } else if (b2i < b2.size()) { chosenBooks->push_back(b2[b2i]); auto r1 = f(likes + 1, b2i + 1, babi, chosenBooks, result_lookup, min_likes, num_books, b2, bab, books); (*result_lookup)[to_key(*chosenBooks)] = r1; return r1; } else { return Result(-1, vector<int>{}); } } else { vector<int> ss = *chosenBooks; long long sum = calc_t(ss, books); int num_pad = num_books - ss.size(); if (num_pad > 0) { for (int i = 0; i < books.size(); i++) { if (find(ss.begin(), ss.end(), i) != ss.end()) continue; sum += books[i].t; ss.push_back(i); num_pad--; if (!num_pad) break; } } return Result(sum, ss); } } int main() { int n = 0; int min_likes = 0; int num_books = 0; cin >> n >> num_books >> min_likes; if (n < num_books || num_books < min_likes) { cout << -1 << endl; return 0; } int a_count = 0, b_count = 0; vector<Book> books; for (int i = 0; i < n; i++) { long long t = 0; int a = 0, b = 0; cin >> t >> a >> b; a_count += a; b_count += b; books.push_back(Book(i + 1, t, a, b)); } if (a_count < min_likes || b_count < min_likes) { cout << -1 << endl; return 0; } sort(books.begin(), books.end(), [](const Book& a, const Book& b) { return a.t < b.t; }); vector<int> b2; vector<vector<int>> bab; { vector<int> ba, bb; for (int i = 0; i < n; i++) { auto bk = books[i]; if (bk.a && bk.b) { b2.push_back(i); } else if (bk.a) { ba.push_back(i); } else if (bk.b) { bb.push_back(i); } } int nn = ba.size() < bb.size() ? ba.size() : bb.size(); for (int i = 0; i < nn; i++) { bab.push_back(vector<int>{ba[i], bb[i]}); } } vector<int> ss{}; map<string, Result> result_lookup{}; Result r = f(0, 0, 0, &ss, &result_lookup, min_likes, num_books, b2, bab, books); auto chosenBooks = r.chosenBooks; auto sum = r.sum; if (sum < 0) { cout << -1 << endl; return 0; } cout << sum << endl; auto p = chosenBooks.begin(); for (int i = 0; i < chosenBooks.size(); i++) { if (i > 0) cout << " "; cout << books[*p].i; advance(p, 1); } cout << 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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class X { public static void main(String[] args) { FastScanner in=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int n=in.nextInt(); int k=in.nextInt(); int a[][]=new int[n][4]; for(int i=0;i<n;i++) { a[i][0]=in.nextInt(); a[i][1]=in.nextInt(); a[i][2]=in.nextInt(); a[i][3]=i; } Arrays.sort(a,new Comparator<int[]>(){ public int compare(int a[],int b[]){ return a[0]-b[0]; } }); long as1=0,as2=0; HashSet<Integer> h=new HashSet<>(); ArrayList<Integer> ans=new ArrayList<>(); long a1=0,a2=0; for(int i=0;i<n;i++){ if(a[i][1]==1){ a1++; ans.add(a[i][0]); h.add(a[i][3]); if(a[i][2]==1) a2++; } if(a1>=k) break; } for(int i=0;i<n;i++){ if(a2>=k) break; if(h.contains(i)) continue; if(a[i][2]==1){ a2++; ans.add(a[i][0]); } } if(a1<k||a2<k) { out.println("-1"); return; } for(int i=0;i<ans.size();i++){ as1+=ans.get(i); } a1=0; a2=0; ans.clear(); h.clear(); for(int i=0;i<n;i++){ if(a[i][2]==1){ a2++; ans.add(a[i][0]); h.add(a[i][3]); if(a[i][1]==1) a1++; } if(a2>=k) break; } for(int i=0;i<n;i++){ if(a1>=k) break; if(h.contains(i)) continue; if(a[i][1]==1){ a1++; ans.add(a[i][0]); } } if(a1<k||a2<k) { out.println("-1"); return; } for(int i=0;i<ans.size();i++){ as2+=ans.get(i); } out.println(Math.max(as1,as2)); out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Collections; import java.util.LinkedList; 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; LinkedList<Integer> ar= new LinkedList<>(); LinkedList<Integer> br = new LinkedList<>(); LinkedList<Integer> all = new LinkedList<>(); 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){ all.add(t); aPos++; bPos++; } else if (a == 1){ aPos++; ar.add(t); } else if (b == 1){ bPos++; br.add(t); } } if (aPos < k || bPos < k){ out.println(-1); return; } Collections.sort(br); Collections.sort(ar); Collections.sort(all); int kA = k; int kB = k; long sum = 0; while(kA > 0 || kB > 0){ int a = inf; int b = inf; int c = inf; if(ar.size() > 0) a = ar.peek(); if(br.size() > 0) b = br.peek(); if(all.size() > 0) c = all.peek(); if (kA > 0){ if (a < c) { kA--; ar.remove(); sum+=a; } else if (c!=inf){ kA--; kB--; all.remove(); sum+=c; } } else if (kB > 0){ if (b < c){ kB--; br.remove(); sum+=b; } else if(c!=inf){ kA--; kB--; all.remove(); sum+=c; } } } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e5 + 10; int n, m, T, k; vector<pair<int, int>> a, b, ab, other; vector<int> ans; int check(int mid) { if ((int)a.size() < k - mid) return 2e9 + 1; if ((int)b.size() < k - mid) return 2e9 + 1; if (mid + max(0, k - mid) * 2 > m) return 2e9 + 1; int nd = m - mid, res = 0; vector<pair<int, int>> vec; for (int i = 0; i < mid; i++) res += ab[i].first, ans.push_back(ab[i].second); for (int i = mid; i < ab.size(); i++) vec.push_back(ab[i]); for (int i = 0; i < k - mid; i++) res += a[i].first + b[i].first, nd -= 2, ans.push_back(a[i].second), ans.push_back(b[i].second); for (int i = max(0, k - mid); i < a.size(); i++) vec.push_back(a[i]); for (int i = max(0, k - mid); i < b.size(); i++) vec.push_back(b[i]); for (int i = 0; i < other.size(); i++) vec.push_back(other[i]); sort(vec.begin(), vec.end()); for (int i = 0; i < nd && i < vec.size(); i++) res += vec[i].first, ans.push_back(vec[i].second); return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; for (int i = 1, x1, x2, x3; i <= n; ++i) { cin >> x1 >> x2 >> x3; if (x2 && x3) ab.push_back({x1, i}); else if (x2) a.push_back({x1, i}); else if (x3) b.push_back({x1, i}); else other.push_back({x1, i}); } sort(a.begin(), a.end()), sort(b.begin(), b.end()), sort(ab.begin(), ab.end()), sort(other.begin(), other.end()); int l = 0, r = min((int)ab.size(), m); while (r - l > 10) { int midl = l + (r - l) / 3, midr = r - (r - l) / 3; if (check(midl) <= check(midr)) r = midr; else l = midl; } int res = 2e9, Min_idx = 0; for (int i = l; i <= r; i++) { int tmp = check(i); if (tmp < res) res = tmp, Min_idx = i; } if (res == 2e9) return puts("-1"), 0; ans.clear(); cout << check(Min_idx) << endl; for (auto a : ans) cout << a << " "; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 200005; int INF = 1e9; vector<long long> books[3]; int type(int a, int b) { if (a && b) return 0; if (a) return 1; return 2; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k, t, x, y; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> x >> y; if (!x && !y) continue; books[type(x, y)].push_back(t); } for (int i = 0; i < 3; i++) { sort(books[i].begin(), books[i].end()); for (int j = books[i].size(); j < k; j++) { books[i].push_back(INF); } } long long ans, aux; ans = aux = 0; for (int i = 0; i < k; i++) aux += books[0][i]; ans = aux; for (int i = 0; i < k; i++) { aux -= books[0][k - i - 1]; aux += books[1][i]; aux += books[2][i]; ans = min(aux, ans); } if (ans >= INF) 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" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define lli long long int #define double long double #define all(x) x.begin(),x.end() #define siz(x) x.size() #define prs(x,y) x.find(y)!=x.end() #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define forn(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define FORD(i,a,b) for(int i=(a);i>=(b);i--) #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; } const int INF = 1<<29; typedef long long ll; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n>>b)&1; } inline void set_bit(int & n, int b) { n |= two(b); } inline void unset_bit(int & n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res;} inline int level(int n){int ans=0;while(n%2==0){n/=2;ans++;}return ans;} lli pa[200005],pb[200005],ps[200005]; vector<pair<pair<lli,lli> ,pair<lli,lli> > > st; int main() { lli n,m,k; cin>>n>>m>>k; for(lli i=0;i<n;i++) { lli a,b,c; cin>>a>>b>>c; st.push_back({{a,i+1},{b,c}}); } sort(all(st)); pa[n]=pb[n]=ps[n]=0; for(lli i=(lli)st.size()-1;i>=0;i--) { lli a=st[i].second.first; lli b= st[i].second.second; ps[i]=ps[i+1]; pa[i]=pa[i+1]; pb[i]=pb[i+1]; if(a==1 && b==1) ps[i]+=1; else if(a==1) pa[i]+=1; else if(b==1) pb[i]+=1; } lli cnt=0; lli t=0,curra=0,currb=0; vector<lli> vec; for(lli i=0;i<n;i++) { if(cnt==m) break; lli a=st[i].second.first; lli b= st[i].second.second; lli ba=max(0LL,k-(curra+ps[i+1]+a)); lli bb=max(0LL,k-(currb+ps[i+1]+b)); lli bs = max(0LL,min(ps[i+1],max(k-(curra+a),k-(currb+b)))); //cout<<curra<<" "<<currb<<" "<<i<<" "<<cnt<<" "; //cout<<ba<<" "<<bb<<" "<<bs<<"\n"; if(cnt+bs+ba+bb+1>m || ba>pa[i+1] || bb>pb[i+1]) continue; cnt++; curra+=a; currb+=b; t+=st[i].first.first; vec.push_back(st[i].first.second); } if(cnt!=m || curra<k || currb<k) {cout<<-1;return 0;} cout<<t<<"\n"; for(auto k: vec) cout<<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" ] }
IN-CORRECT
python3
def solve(): n, k = list(map(int, input().split())) a = [] b = [] both = [] alice = 0 bob = 0 coincidence = 0 for i in range(n): t, a_, b_ = list(map(int, input().split())) if b_ and a_: both.append(t) coincidence += 1 elif a_ == 1: a.append(t) alice += 1 elif b_ == 1: b.append(t) bob += 1 if alice+coincidence < k or bob+coincidence<k: print(-1) else: both.sort() a.sort() b.sort() out = 0 while k>0: if len(a) == 0 or len(b) == 0: out += sum(both[:k]) k = 1 elif len(both) > 0: minimun = a[0] + b[0] aux = [val for val in both if val > minimun] if 0 < len(aux): out += sum(aux) del both[:len(aux)] else: out += minimun a.pop(0) b.pop(0) else: out += sum(a[:k]) + sum(b[:k]) k = 1 k -= 1 print(out) cases = 1 for test in range(cases): 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" ] }
IN-CORRECT
python2
""" Satwik_Tiwari ;) . 28 june , 2020 - Sunday """ #=============================================================================================== #importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase import bisect from heapq import * from math import * from collections import deque from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl #If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect #If the element is already present in the list, # the right most position where element has to be inserted is returned #============================================================================================== 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") #=============================================================================================== #some shortcuts mod = 1000000007 def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input def out(var): sys.stdout.write(str(var)) #for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def graph(vertex): return [[] for i in range(0,vertex+1)] def zerolist(n): return [0]*n def nextline(): out("\n") #as stdout.write always print sring. def testcase(t): for p in range(t): solve() def printlist(a) : for p in range(0,len(a)): out(str(a[p]) + ' ') def lcm(a,b): return (a*b)//gcd(a,b) def power(a,b): ans = 1 while(b>0): if(b%2==1): ans*=a a*=a b//=2 return ans def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1))) def isPrime(n) : # Check Prime Number or not if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True #=============================================================================================== # code here ;)) def bs(a,l,h,x): while(l<h): # print(l,h) mid = (l+h)//2 if(a[mid] == x): return mid if(a[mid] < x): l = mid+1 else: h = mid return l def sieve(a): #O(n loglogn) nearly linear #all odd mark 1 for i in range(3,((10**6)+1),2): a[i] = 1 #marking multiples of i form i*i 0. they are nt prime for i in range(3,((10**6)+1),2): for j in range(i*i,((10**6)+1),i): a[j] = 0 a[2] = 1 #special left case return (a) def bfs(g,st): visited = [0]*(len(g)) visited[st] = 1 queue = [] queue.append(st) new = [] while(len(queue) != 0): s = queue.pop() new.append(s) for i in g[s]: if(visited[i] == 0): visited[i] = 1 queue.append(i) return new def solve(): n,k = sep() both = [] a = [] b = [] for i in range(0,n): t,x,y = sep() if(x==1 and y==1): both.append(t) else: if(x==1): a.append(t) else: b.append(t) a = sorted(a) b = sorted(b) both = sorted(both) if(len(both) >=k): # both = sorted(both) print(sum(both[:k])) else: rem = k - len(both) if(len(a) < rem or len(b) < rem): print(-1) return print(sum(both) + sum(a[:rem])+sum(b[:rem])) testcase(1) # testcase(int(inp()))
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, k; vector<int64_t> a; vector<int64_t> b; vector<int64_t> c; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { int64_t _t; bool _a, _b; cin >> _t >> _a >> _b; if (_a && _b) { c.push_back(_t); } else { if (_a) a.push_back(_t); else if (_b) b.push_back(_t); } } a.push_back(0); b.push_back(0); c.push_back(0); sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); for (int i = 1; i < int(a.size()); i++) a[i] += a[i - 1]; for (int i = 1; i < int(b.size()); i++) b[i] += b[i - 1]; for (int i = 1; i < int(c.size()); i++) c[i] += c[i - 1]; bool done = false; int64_t ans = 0; for (int i = 0; i <= k; i++) { if ((c.size() > i) && (min(a.size(), b.size()) > (k - i))) { if (!done) ans = c[i] + a[k - i] + b[k - i]; else ans = min(ans, c[i] + a[k - 1] + b[k - i]); done = true; } } if (!done) cout << -1 << '\n'; else 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" ] }
IN-CORRECT
python3
#import math #from functools import lru_cache import heapq #from collections import defaultdict #from collections import Counter #from collections import deque #from sys import stdout #from sys import setrecursionlimit #setrecursionlimit(10**7) from sys import stdin input = stdin.readline INF = 10**9 + 7 MAX = 10**7 + 7 MOD = 10**9 + 7 n, M, k = [int(x) for x in input().strip().split()] c, a, b, u = [], [], [], [] for ni in range(n): ti, ai, bi = [int(x) for x in input().strip().split()] if(ai ==1 and bi == 1): c.append([ti, ni+1]) elif(ai == 1): a.append([ti, ni+1]) elif(bi == 1): b.append([ti, ni+1]) else: u.append([ti, ni+1]) c.sort(reverse = True) a.sort(reverse = True) b.sort(reverse = True) u.sort(reverse = True) alen = len(a) blen = len(b) clen = len(c) ulen = len(u) m = max(0, k - min(alen, blen)) ans = 0 alist = [] adlist = [] #print(clen, m) if(m>clen): print('-1') else: for mi in range(m): cv, ci = c.pop() ans += cv alist.append([cv, ci]) ka = k - m kb = k - m M -= m while(ka or kb): ca = (c[-1][0] if c else float('inf')) da = 0 dlist = [] clist = ([c[-1] if c else [float('inf'), -1]]) ap, bp = 0, 0 if(ka): da += (a[-1][0] if a else float('inf')) ap = 1 dlist.append(a[-1] if a else [float('inf'), -1]) if(kb): da += (b[-1][0] if b else float('inf')) bp = 1 dlist.append(b[-1] if b else [float('inf'), -1]) if(da<=ca and M>=2): ans += da if(ap): ka -= 1 if a: a.pop() if(bp): kb -= 1 if b: b.pop() for di in dlist: adlist.append(di) M -= (len(dlist)) else: ans += ca for ci in clist: alist.append(ci) if(ap): ka -= 1 if(bp): kb -= 1 M -= 1 if(M>(len(a) + len(c) + len(b) + len(u))): print('-1') else: heapq.heapify(c) alist = [[-x[0], x[1]] for x in alist] heapq.heapify(alist) while(M): if(u and u[-1][0] <= min(c[0][0] if c else float('inf'), a[-1][0] if a else float('inf'), b[-1][0] if b else float('inf'))): ut, dt = 0, 0 ut += (-alist[0][0] if alist else 0) ut += u[-1][0] dt += (a[-1][0] if a else float('inf')) dt += (b[-1][0] if b else float('inf')) if(ut<dt): # add from ulist upopped = u.pop() adlist.append(upopped) M -= 1 ans += upopped[0] else: # remove from alist and add from ab alpopped = (heapq.heappop(alist) if alist else [float('-inf'), -1]) heapq.heappush(c, [-alpopped[0], alpopped[1]]) ans += alpopped[0] bpopped = (b.pop() if b else [float('inf'), -1]) apopped = (a.pop() if a else [float('inf'), -1]) adlist.append(bpopped) adlist.append(apopped) ans += apopped[0] ans += bpopped[0] M -= 1 else: # if c is less than a, b ct = (c[0][0] if c else float('inf')) at, bt = (a[-1][0] if a else float('inf')), (b[-1][0] if b else float('inf')) abt = min(at, bt) if(ct<abt): cpopped = (heapq.heappop(c) if c else [float('inf'), -1]) heapq.heappush(alist, [-cpopped[0], cpopped[1]]) ans += cpopped[0] M-=1 else: # minimum is among a and b; straight forward if(at<bt): apopped = (a.pop() if a else [float('inf'), -1]) adlist.append(apopped) ans += apopped[0] else: bpopped = (b.pop() if b else [float('inf'), -1]) adlist.append(bpopped) ans += bpopped[0] M-=1 print(ans if ans!=float('inf') else '-1') if(ans != float('inf')): flist = [] for ai in adlist: flist.append(ai[1]) for ai in alist: flist.append(ai[1]) print(*flist)
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" ] }
IN-CORRECT
java
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import java.util.HashSet; import java.util.Arrays; import java.util.Scanner; import java.util.Set; import java.util.ArrayList; import java.util.Collections; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.text.DecimalFormat; import java.lang.Math; import java.util.Iterator; import java.util.TreeSet; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.*; public class D1343{ public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static long MOD = (long)(1e9+7); static FastReader sc = new FastReader(); static int pInf = Integer.MAX_VALUE; static int nInf = Integer.MIN_VALUE; public static void main(String[] args){ int t = 1; while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> A = new ArrayList<Integer>(); ArrayList<Integer> B = new ArrayList<Integer>(); ArrayList<Integer> AB = new ArrayList<Integer>(); while(n-->0){ int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if(y==1 && z==1){ AB.add(x); } else if(y==1 && z==0){ A.add(x); } else if(z==1 && y==0){ B.add(x); } } Collections.sort(A); Collections.sort(AB); Collections.sort(B); long sum = 0; while(k-->0){ if((A.size()>0) && (AB.size()>0) && (B.size()>0)){ if((A.get(0)+B.get(0)) > AB.get(0)){ sum += A.get(0)+B.get(0); A.remove(0); B.remove(0); } else{ sum += AB.get(0); AB.remove(0); } } else if(AB.size()>0){ sum += AB.get(0); AB.remove(0); } else if(A.size()>0 && B.size()>0){ sum += A.get(0)+B.get(0); A.remove(0); B.remove(0); } else{ sum = -1; break; } } out.println(sum); } out.close(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Integer.lowestOneBit(i) Equals k where k is the position of the first one in the binary //Integer.highestOneBit(i) Equals k where k is the position of the last one in the binary //Integer.bitCount(i) returns the number of one-bits //Collections.sort(A,(p1,p2)->(int)(p2.x-p1.x)) To sort ArrayList in descending order wrt values of x. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Pair Class static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub if(this.x==o.x){ return (this.y-o.y); } return (this.x-o.x); } } //Merge Sort static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long [n1]; long R[] = new long [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } //Brian Kernighan’s Algorithm static long countSetBits(long n){ if(n==0) return 0; return 1+countSetBits(n&(n-1)); } //Factorial static long factorial(long n){ if(n==1) return 1; if(n==2) return 2; if(n==3) return 6; return n*factorial(n-1); } //Euclidean Algorithm static long gcd(long A,long B){ if(B==0) return A; return gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD; return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD; } //AKS Algorithm static boolean isPrime(long n){ if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Reverse an array static <T> void reverse(T arr[],int l,int r){ Collections.reverse(Arrays.asList(arr).subList(l, r)); } //Sieve of eratosthenes static int[] findPrimes(int n){ boolean isPrime[]=new boolean[n+1]; ArrayList<Integer> a=new ArrayList<>(); int result[]; Arrays.fill(isPrime,true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<=n;++i){ if(isPrime[i]==true){ for(int j=i*i;j<=n;j+=i) isPrime[j]=false; } } for(int i=0;i<=n;i++) if(isPrime[i]==true) a.add(i); result=new int[a.size()]; for(int i=0;i<a.size();i++) result[i]=a.get(i); return result; } //Euler Totent function static long countCoprimes(long n){ ArrayList<Long> prime_factors=new ArrayList<>(); long x=n,flag=0; while(x%2==0){ if(flag==0) prime_factors.add(2L); flag=1; x/=2; } for(long i=3;i*i<=x;i+=2){ flag=0; while(x%i==0){ if(flag==0) prime_factors.add(i); flag=1; x/=i; } } if(x>2) prime_factors.add(x); double ans=(double)n; for(Long p:prime_factors){ ans*=(1.0-(Double)1.0/p); } return (long)ans; } public static int bSearch(int n,ArrayList<Integer> A){ int s = 0; int e = A.size()-1; while(s<=e){ int m = s+(e-s)/2; if(A.get(m)==(long)n){ return A.get(m); } else if(A.get(m)>(long)n){ e = m-1; } else{ s = m+1; } } return A.get(e); } static long modulo = (long)1e9+7; public static long modinv(long x){ return modpow(x, modulo-2); } public static long modpow(long a, long b){ if(b==0){ return 1; } long x = modpow(a, b/2); x = (x*x)%modulo; if(b%2==1){ return (x*a)%modulo; } return x; } public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int isPrime(int n) { if (n < 2) return 0; if (n < 4) return 1; if (n % 2 == 0 or n % 3 == 0) return 0; for (int i = 5; i * i <= n; i += 6) if (n % i == 0 or n % (i + 2) == 0) return 0; return 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, i, k, ans = 0; cin >> n >> k; vector<int> a, b, c; for (i = 0; i < n; i++) { int t1, t2, t3; cin >> t1 >> t2 >> t3; if (t3 and t2) { c.push_back(t1); } else if (t2) { a.push_back(t1); } else if (t3) { b.push_back(t1); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); if ((a.size() + c.size()) < k or (b.size() + c.size()) < k) { cout << -1 << "\n"; return 0; } if (a.size() >= k and b.size() >= k) { int ind = 0, j = 0; for (i = 0; i < k; i++) { if (ind < c.size() and c[ind] < (a[j] + b[j])) { ans += c[ind++]; } else { ans = ans + a[j] + b[j]; j++; } } cout << ans << "\n"; } else { int ind = min(a.size(), b.size()); for (i = 0; i < ind; i++) { ans = ans + a[i] + b[i]; } for (i = 0; i < k - ind; i++) { ans += c[i]; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; long long A(long long x) { if (x >= 0) return x; else return -x; } long long gcd(long long a, long long b) { if (b > a) { long long tmp = b; b = a; a = tmp; } if (a % b == 0) return b; else return gcd(b, a % b); } unsigned long long popcount(unsigned long long x) { x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) + (x & 0x5555555555555555UL); x = ((x & 0xccccccccccccccccUL) >> 2) + (x & 0x3333333333333333UL); x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) + (x & 0x0f0f0f0f0f0f0f0fUL); x = ((x & 0xff00ff00ff00ff00UL) >> 8) + (x & 0x00ff00ff00ff00ffUL); x = ((x & 0xffff0000ffff0000UL) >> 16) + (x & 0x0000ffff0000ffffUL); x = ((x & 0xffffffff00000000UL) >> 32) + (x & 0x00000000ffffffffUL); return x; } int main(void) { int T; T = 1; for (int query = 0; query < T; query++) { int n, k; cin >> n >> k; vector<long long> both, bob, alice; for (int i = 0; i < n; i++) { long long t; int a, b; cin >> t >> a >> b; if (a == 1) { if (b == 0) alice.push_back(t); else both.push_back(t); } else { if (b == 1) bob.push_back(t); } } if (alice.size() + both.size() < k || bob.size() + both.size() < k) { cout << -1 << endl; } else { sort(both.begin(), both.end()); sort(bob.end(), bob.end()); sort(alice.begin(), alice.end()); long long ans = 0; while (k > 0) { if (both.size() == 0) { for (int i = 0; i < k; i++) ans += bob[i]; for (int i = 0; i < k; i++) ans += alice[i]; k = 0; } else { if (alice.size() == 0) { for (int i = 0; i < k; i++) ans += both[i]; break; } if (bob.size() == 0) { for (int i = 0; i < k; i++) ans += both[i]; break; } if (both[0] > bob[0] + alice[0]) { ans += bob[0] + alice[0]; k--; bob.erase(bob.begin()); alice.erase(alice.begin()); } else { ans += both[0]; k--; both.erase(both.begin()); } } } 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; public class E2 { static PrintWriter out; static CF_Reader in; public static void main(String[] args) throws IOException { out = new PrintWriter(new OutputStreamWriter(System.out)); in = new CF_Reader(); int cases = 1; StringBuilder result = new StringBuilder(); for (int t = 0; t < cases; t++) { int n = in.intNext(), m = in.intNext(), k = in.intNext(); ArrayList<long[]> first = new ArrayList<>(); ArrayList<long[]> second = new ArrayList<>(); ArrayList<long[]> both = new ArrayList<>(); ArrayList<long[]> zeros = new ArrayList<>(); for (int i = 1; i < n+1; i++) { long cost = in.longNext(), a = in.longNext(), b = in.longNext(); if (a == 1 && b == 1) both.add(new long[]{cost, i}); else if (a == 1) first.add(new long[]{cost, i}); else if (b == 1) second.add(new long[]{cost, i}); else zeros.add(new long[]{cost, i}); } result.append(solve(k, m, both, first, second, zeros)).append("\n"); } out.println(result); out.close(); } static StringBuilder solve(int k, int m, ArrayList<long[]> both, ArrayList<long[]> first, ArrayList<long[]> second, ArrayList<long[]> zeros) { if (both.size() + first.size() < k || both.size() + second.size() < k) return new StringBuilder("-1"); if (m > both.size() + first.size() + second.size()) return new StringBuilder("-1"); first.sort(Comparator.comparingLong(o -> o[0])); second.sort(Comparator.comparingLong(o -> o[0])); both.sort(Comparator.comparingLong(o -> o[0])); long read = 0; int count = 0; int chosen = 0; int bothIdx = 0; int sepIdx = 0; StringBuilder bookIndices = new StringBuilder(); while (count < k) { if (bothIdx >= both.size()) { read += first.get(sepIdx)[0] + second.get(sepIdx)[0]; bookIndices.append(first.get(sepIdx)[1]).append(" ").append(second.get(sepIdx)[1]).append(" "); sepIdx++; chosen += 2; } else if (sepIdx >= first.size() || sepIdx >= second.size()) { read += both.get(bothIdx)[0]; bookIndices.append(both.get(bothIdx)[1]).append(" "); bothIdx++; chosen++; } else { long bothSum = both.get(bothIdx)[0]; long sepSum = first.get(sepIdx)[0] + second.get(sepIdx)[0]; if (bothSum <= sepSum) { read += bothSum; bookIndices.append(both.get(bothIdx)[1]).append(" "); bothIdx++; chosen++; } else { read += sepSum; bookIndices.append(first.get(sepIdx)[1]).append(" ").append(second.get(sepIdx)[1]).append(" "); sepIdx++; chosen += 2; } } count++; } out.printf("%d -> %d\n", chosen, read); int aIdx = sepIdx; int bIdx = sepIdx; int zIdx = 0; while (chosen < m) { long choice = Long.MAX_VALUE; int selected = 0; if (aIdx < first.size()) choice = first.get(aIdx)[0]; if (bIdx < second.size() && second.get(bIdx)[0] < choice) { choice = second.get(bIdx)[0]; selected = 1; } if (bothIdx < both.size() && both.get(bothIdx)[0] < choice) { choice = both.get(bothIdx)[0]; selected = 2; } if (zIdx < zeros.size() && zeros.get(zIdx)[0] < choice) { choice = zeros.get(zIdx)[0]; selected = 3; } if (selected == 0) { bookIndices.append(first.get(aIdx)[1]).append(" "); aIdx++; } else if (selected == 1) { bookIndices.append(second.get(bIdx)[1]).append(" "); bIdx++; } else if (selected == 2) { bookIndices.append(both.get(bothIdx)[1]).append(" "); bothIdx++; } else { bookIndices.append(zeros.get(zIdx)[1]).append(" "); zIdx++; } read += choice; chosen++; } StringBuilder res = new StringBuilder(); res.append(read).append("\n").append(bookIndices); return res; } static class CF_Reader { BufferedReader br; StringTokenizer st; public CF_Reader() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } long longNext() throws IOException { return Long.parseLong(next()); } int intNext() throws IOException { return Integer.parseInt(next()); } double doubleNext() throws IOException { return Double.parseDouble(next()); } char charNext() throws IOException { return next().charAt(0); } public int[] nextIntArray(final int n) throws IOException { final int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = intNext(); return a; } public long[] nextLongArray(final int n) throws IOException { final long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = longNext(); return a; } String line() throws IOException { return br.readLine().trim(); } } static class util { public static int upperBound(long[] array, long obj) { int l = 0, r = array.length - 1; while (r - l >= 0) { int c = (l + r) / 2; if (obj < array[c]) { r = c - 1; } else { l = c + 1; } } return l; } public static int upperBound(ArrayList<Long> array, long obj) { int l = 0, r = array.size() - 1; while (r - l >= 0) { int c = (l + r) / 2; if (obj < array.get(c)) { r = c - 1; } else { l = c + 1; } } return l; } public static int lowerBound(long[] array, long obj) { int l = 0, r = array.length - 1; while (r - l >= 0) { int c = (l + r) / 2; if (obj <= array[c]) { r = c - 1; } else { l = c + 1; } } return l; } public static int lowerBound(ArrayList<Long> array, long obj) { int l = 0, r = array.size() - 1; while (r - l >= 0) { int c = (l + r) / 2; if (obj <= array.get(c)) { r = c - 1; } else { l = c + 1; } } return l; } public static void print(long[] arr) { System.out.println(Arrays.toString(arr)); } public static void print(int[] arr) { System.out.println(Arrays.toString(arr)); } public static void print(char[] arr) { System.out.println(Arrays.toString(arr)); } } static class Tuple implements Comparable<Tuple> { int a; int b; public Tuple(int a, int b) { this.a = a; this.b = b; } public int getA() { return a; } public int getB() { return b; } public int compareTo(Tuple other) { if (this.a == other.a) return Integer.compare(this.b, other.b); return Integer.compare(this.a, other.a); } @Override public int hashCode() { return Arrays.deepHashCode(new Integer[]{a, b}); } @Override public boolean equals(Object o) { if (!(o instanceof Tuple)) return false; Tuple pairo = (Tuple) o; return (this.a == pairo.a && this.b == pairo.b); } @Override public String toString() { return String.format("%d,%d ", this.a, this.b); } } }