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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int Alice[200005], Bob[200005], Both[200005];
int PA[200005], PB[200005];
int FindMinReadingTime(int k, int a, int b, int bo) {
int i, Ans = 0;
for (i = 1; i <= a; ++i) PA[i] = PA[i - 1] + Alice[i];
for (i = 1; i <= a; ++i) PB[i] = PB[i - 1] + Bob[i];
if (a >= k && b >= k)
Ans = PA[k] + PB[k];
else
Ans = 2000000001;
int Value = 0;
for (i = 1; i <= min(k, bo); ++i) {
Value += Both[i];
if (i + a >= k && i + b >= k) Ans = min(Ans, Value + PA[k - i] + PB[k - i]);
}
return Ans;
}
int main() {
int N, k, i, j, p, q;
int t, a, b;
scanf("%d%d", &N, &k);
j = p = q = 0;
for (i = 1; i <= N; ++i) {
scanf("%d%d%d", &t, &a, &b);
if (a == 1) {
if (b == 1)
Both[++q] = t;
else
Alice[++j] = t;
} else if (b == 1)
Bob[++p] = t;
}
if (j + q < k || p + q < k)
puts("-1");
else
printf("%d\n", FindMinReadingTime(k, j, p, q));
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 answer():
if(n3+n1 < k):return -1
if(n3+n2 < k):return -1
ap=[0]
for i in range(n1):ap.append(ap[-1] + a[i])
ap.append(0)
bp=[0]
for i in range(n2):bp.append(bp[-1] + b[i])
bp.append(0)
start=max(max(0,k-n1),max(0,k-n2))
s=0
for i in range(start):s+=common[i]
ans=1e10
for i in range(start,min(k,n3)):
ans=min(ans , s + ap[k-i] + bp[k-i])
s+=common[i]
ans=min(ans,s + ap[max(0,k-n3)] + bp[max(0,k-n3)])
return ans
n,k=map(int,input().split())
a,b,common=[],[],[]
for i in range(n):
t,x,y=map(int,input().split())
if(x and y):common.append(t)
elif(x==1 and y==0):a.append(t)
else:b.append(t)
common.sort()
a.sort()
b.sort()
n1,n2,n3=len(a),len(b),len(common)
print(answer())
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python3
|
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
from bisect import bisect_left,bisect_right
import threading
from collections import Counter,defaultdict
arr=[]
def main():
for _ in range(1):
n,m,k=map(int,input().split())
ar3=[]
ar1=[]
ar2=[]
rm=[]
for i in range(n):
a,b,c=map(int,input().split())
if b==c==1:
ar3.append((a,i))
else:
if b==1:
ar1.append((a,i))
elif c==1:
ar2.append((a,i))
else:
rm.append((a,i))
t=2*max(0,k-len(ar3))+min(k,len(ar3))
# print(len(ar3),len(ar2),len(ar1),t)
if len(ar3)+len(ar1)<k or len(ar3)+len(ar2)<k or t>m:
print(-1)
else:
ar3.sort()
ar1.sort()
ar2.sort()
# print(ar1,ar2,ar3)
pt1=pt2=0
i1=0
i2=0
i3=0
ans=0
m_=0
st=[]
while i1<len(ar1) and i2<len(ar2) and i3<len(ar3) and m_<m:
t=(m-m_-2)-(2*k -(pt1+1)-(pt2+1) )
# print('-->',t)
if ar1[i1][0] + ar2[i2][0] <ar3[i3][0] and t>=0:
ans=ans+ar1[i1][0]+ar2[i2][0]
st.append(ar1[i1][1])
st.append(ar2[i2][1])
i1+=1
i2+=1
m_+=2
else:
m_+=1
ans=ans+ar3[i3][0]
st.append(ar3[i3][1])
i3+=1
pt1+=1
pt2+=1
if pt1==pt2==k:
break
# print(pt1,pt2,i1,i2,i3)
if i2>=len(ar2):
i1,i2=i2,i1
ar1,ar2=ar2,ar1
while pt1<k and m_<m:
t=(m-m_-1)-(2*k) +(pt1+1)+(pt2)
# print('--:',t)
if i1<len(ar1) and i3<len(ar3) and ar1[i1][0]<ar3[i3][0] and t>=0:
ans+=ar1[i1][0]
st.append(ar1[i1][1])
i1+=1
elif i3<len(ar3):
st.append(ar3[i3][1])
ans+=ar3[i3][0]
i3+=1
pt2+=1
else:
ans+=ar1[i1][0]
st.append(ar1[i1][1])
i1+=1
# elif i1<len(ar1):
# st.append(ar1[i1][1])
# ans+=ar1[i1][0]
# i1+=1
# else:
# st.append(ar3[i3][1])
# ans+=ar3[i3][0]
# i3+=1
# pt2+=1
m_+=1
pt1+=1
while pt2<k and m_<m:
if i1<len(ar1) and i3<len(ar3) and ar3[i3][0]<ar1[i1][0]:
st.append(ar3[i3][1])
ans+=ar3[i3][0]
i3+=1
elif i2<len(ar2) :
st.append(ar2[i2][1])
ans+=ar2[i2][0]
i2+=1
else:
st.append(ar3[i3][1])
ans+=ar3[i3][0]
i3+=1
m_+=1
pt2+=1
at=ar3[i3:]+ar2[i2:]+ar1[i1:]+rm
at.sort()
i=0
while m_<m:
ans+=at[i][0]
st.append(at[i][1])
i+=1
m_+=1
for i in range(len(st)):
st[i]+=1
# print(m_)
print(ans)
print(*st)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
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
|
python3
|
n,k = [int(x) for x in input().split()]
# bl = []
# alice = []
at = []
# bob = []
bt = []
# common = []
ct = []
# none = []
# nt = []
# times = []
for i in range(n):
ti,ai,bi = [int(x) for x in input().split()]
if ai == 1 and bi == 1:
# common.append(i)
ct.append(ti)
elif ai == 1:
# alice.append(i)
at.append(ti)
elif bi == 1:
# bob.append(i)
bt.append(ti)
else:
# none.append(i)
# nt.append(ti)
continue
# times.append(ti)
# bl.append([ti,ai,bi]) #not needed
if len(ct)+len(at)<k or len(ct)+len(bt)<k:
print(-1)
else:
if len(ct)>=k:
ct.sort()
print(sum(ct[:k]))
else:
aleft = k - len(ct)
bleft = k - len(ct)
ans = sum(ct)
at.sort()
bt.sort()
ans+=sum(at[:aleft])+sum(bt[:bleft])
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
|
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])
a.sort()
# 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;
int main() {
long long n, k, i, ok, s = 0, sum = 0, ans = 0;
multimap<long long, long long> pi1, pi2, pi3;
multimap<long long, long long>::iterator it;
cin >> n >> ok;
long long a, b, c;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
if (b == 1) {
if (b + c == 2)
k = 0;
else
k = 2;
pi1.insert({a, k});
}
if (c == 1) {
if (b + c == 2)
k = 0;
else
k = 2;
pi2.insert({a, k});
}
}
long long x = pi1.size();
long long y = pi2.size();
if (x < ok || y < ok) {
cout << -1 << endl;
} else {
for (it = pi1.begin(); it != pi1.end(); it++) {
pi3.insert({it->second, it->first});
}
pi1.clear();
for (it = pi3.begin(); it != pi3.end(); it++) {
pi1.insert({it->second, it->first});
}
pi3.clear();
s = 0;
long long z = ok;
for (it = pi1.begin(); it != pi1.end(); it++) {
s++;
ans += it->first;
if (it->second == 0) z--;
if (s == ok) break;
}
for (it = pi2.begin(); it != pi2.end(); it++) {
pi3.insert({it->second, it->first});
}
pi2.clear();
for (it = pi3.begin(); it != pi3.end(); it++) {
pi2.insert({it->second, it->first});
}
pi3.clear();
s = 0;
for (it = pi2.begin(); it != pi2.end(); it++) {
if (s == z) break;
if (it->second == 2) {
ans += it->first;
s++;
}
}
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
|
python3
|
from sys import stdin, setrecursionlimit, stdout
#setrecursionlimit(1000000)
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
from heapq import heapify, heappop, heappush, heappushpop, heapreplace
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def si(): return stdin.readline().rstrip()
def lsi(): return list(si())
#mod=1000000007
res=['YES', 'NO']
############# CODE STARTS HERE #############
test_case=1
while test_case:
test_case-=1
n, m, k=mi()
both=[[0, 0]]
al=[[0, 0]]
bob=[[0, 0]]
a=[]
for i in range(n):
x, y, z=mi()
a.append([x, i+1])
if y and z:
both.append([x, i+1])
elif y:
al.append([x, i+1])
elif z:
bob.append([x, i+1])
both.sort()
for i in range(1, len(both)):
both[i][0]+=both[i-1][0]
al.sort()
for i in range(1, len(al)):
al[i][0]+=al[i-1][0]
bob.sort()
for i in range(1, len(bob)):
bob[i][0]+=bob[i-1][0]
ans=1000000000000000000000000000
#print(both)
#print(al)
#print(bob)
ind=-1
for i in range(len(both)):
if len(al)>k-i and len(bob)>k-i and i+k-i+k-i<=m:
if both[i][0]+al[k-i][0]+bob[k-i][0]<ans:
ans=both[i][0]+al[k-i][0]+bob[k-i][0]
ind=i
#print(both[:i]+al[:k-i]+bob[:k-i])
if i==k<=m:
if both[i][0]<ans:
ans=both[i][0]
ind=i
break
#print(ans)
#print(-1 if ans==1000000000000000000000000000 else ans)
if ans==1000000000000000000000000000:
print(-1)
exit()
index=[]
for i in range(1, ind+1):
index.append(both[i][1])
for i in range(1, k-ind+1):
index.append(al[i][1])
index.append(bob[i][1])
if len(index)<m:
a.sort()
s=set(index)
j=0
z=len(index)
while z<m:
if a[j][1] not in s:
index.append(a[j][1])
ans+=a[j][0]
z+=1
j+=1
print(ans)
print(*index)
|
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.Scanner;
public class ReadingBookseasyversion {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int both[] = new int[n];
int a[] = new int[n];
int b[] = new int[n];
int bb = 0;
int aa = 0;
int bo = 0;
for(int i=0;i<n;i++){
int num = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
if(x == 1 && y == 1){
both[bo++] = num;
}
else if( x== 1 && y == 0){
a[aa++] = num;
}
else {
b[bb++] = num;
}
}
if(bo+aa+bb<k){
System.out.println("-1");
}
else{
long ans =0;
Arrays.sort(both);
Arrays.sort(a);
Arrays.sort(b);
bo = n-bo;
aa = n-aa;
bb = n-bb;
for(int i=0;i<k;i++){
if((bo<n && (aa<n && bb<n)) ){
if((both[bo] <(a[aa]+b[bb]))){
ans += a[aa++];
ans += b[bb++];
// System.out.println(ans+" "+1);
}
}
else if(bo>=n && (aa>=n || bb>=n)){
System.out.println("-1");
System.exit(0);
}
else if(bo>=n && (aa<n || bb<n)){
ans += a[aa++];
ans += b[bb++];
// System.out.println(ans+" 2");
}
else if(bo<n && (aa>=n || bb>=n)){
ans += both[bo++];
// System.out.println(ans+" 3");
}
}
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
|
n,m,k = map(int, input().split())
oo = list()
oa = list()
ob = list()
zz = list()
for i in range(n):
t,a,b = map(int, input().split())
if a == 1 and b == 1:
oo.append((t,i))
elif a == 0 and b == 1:
ob.append((t,i))
elif a == 1 and b == 0:
oa.append((t,i))
else:
zz.append((t,i))
oo = sorted(oo)
oa = sorted(oa)
ob = sorted(ob)
oo_p = 0
oa_p = 0
ob_p = 0
ca = 0
cb = 0
ans = 0
ans_arr = list()
MAX = 23942034809238409823048
def condition(ko, loa, lob, loo, mo):
if max(0, max(ko-loa, ko-lob)) > loo or max(0, max(ko-loa, ko-lob)) > mo:
return False
return True
def get_first_elem_from_list(l, pos):
if pos < len(l):
return l[pos]
else:
return (MAX,-1)
def remove_first_elem_from_list(l, pos):
if len(l)>pos:
pos += 1
return pos
if not condition(k, len(oa), len(ob), len(oo), m):
print("-1")
exit(0)
c = 0
while ca < k or cb < k:
oo_f = get_first_elem_from_list(oo, oo_p)
oa_f = get_first_elem_from_list(oa, oa_p)
ob_f = get_first_elem_from_list(ob, ob_p)
c += 1
if ca < k and cb < k:
if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2):
if oo_f[0] == MAX:
print("-1")
exit(0)
if n == 19683 and m == 507 and k == 254 and c > 165:
print(oo_f[0], oa_f[0], ob_f[0], c)
ca += 1
cb += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f[0] + ob_f[0] < oo_f[0]:
ca += 1
cb += 1
ans+=oa_f[0]+ob_f[0]
ans_arr.extend([oa_f[1], ob_f[1]])
oa_p = remove_first_elem_from_list(oa, oa_p)
ob_p = remove_first_elem_from_list(ob, ob_p)
elif ca < k:
if oo_f[0] <= oa_f[0]:
ca += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f[0] < oo_f[0]:
ca += 1
ans+=oa_f[0]
ans_arr.append(oa_f[1])
oa_p = remove_first_elem_from_list(oa, oa_p)
else:
if oo_f[0] <= ob_f[0]:
cb += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif ob_f[0] < oo_f[0]:
cb += 1
ans+=ob_f[0]
ans_arr.append(ob_f[1])
ob_p = remove_first_elem_from_list(ob, ob_p)
if len(ans_arr) < m:
zz.extend(oo[oo_p:])
zz.extend(oa[oa_p:])
zz.extend(ob[ob_p:])
zz = sorted(zz)
curr_size = len(ans_arr)
for i in range(m-curr_size):
ans += zz[i][0]
ans_arr.append(zz[i][1])
print(ans)
assert len(ans_arr) == m
for i in ans_arr:
print(i + 1, end =" ")
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class E {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
PriorityQueue<Pair>a = new PriorityQueue<>();
PriorityQueue<Pair>b = new PriorityQueue<>();
PriorityQueue<Pair>both = new PriorityQueue<>();
PriorityQueue<Pair>garb = new PriorityQueue<>();
for(int i=0;i<n;i++){
int t = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
if(x==1 && y==1){
both.add(new Pair(t,i+1));
}else if(x==1 && y!=1){
a.add(new Pair(t,i+1));
}else if(x!=1 && y==1){
b.add(new Pair(t,i+1));
}else{
garb.add(new Pair(t,i+1));
}
}
// System.out.println(a);
// System.out.println(b);
// System.out.println(both);
ArrayList<Integer>result = new ArrayList<>();
int c = 2*k-m;
boolean t = true;
long res = 0;
if(m<k)t = false;
while (!both.isEmpty() && c>0 && t){
if(both.isEmpty()){
t = false;break;}
Pair p = both.poll();
res+=p.a;
result.add(p.b);
c--;
k--;
m--;
}
while (k>0 && t){
int x = 1000000000;
int y = 1000000000;
int z = 1000000000;
if(!a.isEmpty()){
x=a.peek().a;
}
if(!b.isEmpty())y = b.peek().a;
if(!both.isEmpty())z = both.peek().a;
if(z<=x+y && !both.isEmpty()){
Pair p = both.poll();
res+=p.a;
result.add(p.b);
m--;
}
else if(!a.isEmpty() && !b.isEmpty() && m>1){
Pair p1 = a.poll();
Pair p2 = b.poll();
res+=p1.a;
res+=p2.a;
result.add(p1.b);
result.add(p2.b);
m-=2;
}else{
t = false;
break;
}
k--;
}
if(m<0)t = false;
while (m>0){
int x = 1000000000;
int y = 1000000000;
int z = 1000000000;
int g = 1000000000;
if(!a.isEmpty()){
x=a.peek().a;
}
if(!b.isEmpty())y = b.peek().a;
if(!both.isEmpty())z = both.peek().a;
if(!garb.isEmpty())g = garb.peek().a;
if(z<=x&& z<=y && z<=g&& !both.isEmpty()){
Pair p = both.poll();
res+=p.a;
result.add(p.b);
m--;
}
else if(x<=y && x<=z && x<=g &&!a.isEmpty()){
Pair p1 = a.poll();
res+=p1.a;
result.add(p1.b);
m--;
}else if(y<=z && y<=x && y<=g&&!b.isEmpty()){
Pair p1 = b.poll();
res+=p1.a;
result.add(p1.b);
m--;
}else if(g<=z && g<=x && g<=y&&!garb.isEmpty()){
Pair p1 = garb.poll();
res+=p1.a;
result.add(p1.b);
m--;
}else{
t = false;
break;
}
}
if(t){
pw.println(res);
for(int i=0;i<result.size();i++)
pw.print(result.get(i)+" ");
pw.println();
}else{
pw.println(-1);
}
pw.flush();
pw.close();
}
static long power(long x, long y, long m) {
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
static class Node{
long a;
long b;
long c;
public Node(long a,long b,long c){
this.a= a;
this.b = b;
this.c = c;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class Pair implements Comparable<Pair>{
int a;
int b;
public Pair(int a,int b){
this.a= a;
this.b = b;
}
public int compareTo(Pair o) {
if(this.a==o.a)return Integer.compare(b,o.b);
return Integer.compare(a,o.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
|
python3
|
from collections import deque
from heapq import heapify, heappop
rr = lambda: input()
rri = lambda: int(input())
rrm = lambda: list(map(int, input().split()))
INF=float('inf')
def solve(N,K,B):
heap = B
heapify(heap)
aread = 0
soloa = deque()
bread = 0
solob = deque()
totaltime = 0
while len(heap) > 0:
time,alike,blike=heappop(heap)
#print(time,alike,blike)
#print(soloa, solob)
#print(" ")
if not alike and not blike:
continue
if alike and not blike:
if aread >= K:
continue # dont add if exclusive and full
else:
aread+=1
soloa.append(time)
elif blike and not alike:
if bread >= K:
continue # dont add if exclusive and full
else:
bread+=1
solob.append(time)
else:
aread += 1
bread += 1
totaltime += time
if aread >= K+1 and len(soloa) > 0:
soloa.pop()
aread -= 1
if bread >= K+1 and len(solob) > 0:
solob.pop()
bread -= 1
#print(soloa, solob)
if aread < K or bread < K:
return -1
return totaltime + sum(soloa) + sum(solob)
n,k = rrm()
books = [] # tuples (time, alice likes it, bob likes it)
for _ in range(n):
time,a,b=rrm()
books.append((time,a,b))
print(solve(n,k,books))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python3
|
# import os,io
# input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k = map(int,input().split())
bookA = []
bookB = []
bookAB = []
for _ in range(n):
t,a,b = map(int,input().split())
if a == 1 and b == 1:
bookAB.append(t)
elif a == 1:
bookA.append(t)
elif b == 1:
bookB.append(t)
bookA.sort()
bookB.sort()
bookAB.sort()
bookASum = [0]
bookBSum = [0]
bookABSum = [0]
for elem in bookA:
bookASum.append(bookASum[-1] + elem)
for elem in bookB:
bookBSum.append(bookASum[-1] + elem)
for elem in bookAB:
bookABSum.append(bookABSum[-1] + elem)
minReadingTime = -1
for i in range(len(bookABSum)):
if len(bookA) >= k - i and len(bookB) >= k - i:
if minReadingTime == -1 or minReadingTime > bookABSum[i] + bookASum[k - i] + bookBSum[k - i]:
minReadingTime = bookABSum[i] + bookASum[k - i] + bookBSum[k - i]
print(minReadingTime)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python2
|
n,k = map(int,raw_input().split())
both = [];alice = [];bob = []
for _ in range(n):
t,a,b = map(int,raw_input().split())
if a==b==1:
both.append(t)
elif a==1:
alice.append(t)
else:
bob.append(t)
alice.sort()
bob.sort()
for i in range(min(len(alice),len(bob))):
both.append(alice[i]+bob[i])
print(-1if len(both)<k else sum(sorted(both)[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
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;
int[]arr01w=new int[n];
int[]arr10w=new int[n];
int[]arr11w=new int[n];
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];
qsort_randomised(0,ptr11-1,arr11);
qsort_randomised(0,ptr10-1,arr10);
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];
}
}
//for(i=0;i<ptr01;i++)System.out.print(pre_sum01[i]+" ");
int temp=0,min=(int)myInf;
for(i=k;i>0;i--)
{
temp=0;
if(ptr11>=k){
temp=pre_sum11[i-1];
j1=i;
}
else
{
if(ptr11-1>=0){
temp=pre_sum11[ptr11-1];
j1=ptr11;
}
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
|
python3
|
import pdb
tmp = input()
lst = tmp.split(' ')
n = int(lst[0])
m = int(lst[1])
k = int(lst[2])
i=0
alice = []
bob = []
common_arr =[]
alice_count = 0
bob_count = 0
indexDict = {}
final = []
while (i<n):
tmp = input()
lst = tmp.split(' ')
t = int(lst[0])
a = int(lst[1])
b = int(lst[2])
tmp = (t,a,b)
indexDict[(t,a,b)] = i
final.append(tmp)
if(a==1 and b==1):
common_arr.append(tmp)
alice_count += 1
bob_count += 1
i+=1
continue
elif(a==1 and b!=1):
alice.append(tmp)
alice_count+=1
i+=1
continue
elif(a!=1 and b == 1):
bob.append(tmp)
bob_count+=1
i+=1
continue
i+=1
if(alice_count<k or bob_count<k):
print(-1)
else:
alice.sort(key = lambda x:x[0])
bob.sort(key = lambda x:x[0])
common_arr.sort(key = lambda x:x[0])
# pdb.set_trace()
booksA = k
booksB = k
a=0
b=0
c=0
lenA = len(alice)-1
lenB = len(bob)-1
lenC = len(common_arr)
finalPosLst = []
sum = 0
while (True):
if(booksA == 0 and booksB == 0):
break
if(a>=lenA):
sum += common_arr[c][0]
booksA = booksA -1
booksB = booksB -1
finalPosLst.append(indexDict[common_arr[c]])
c+=1
m = m-1
continue
if(b>=lenB):
sum += common_arr[c][0]
booksA = booksA -1
booksB = booksB -1
finalPosLst.append(indexDict[common_arr[c]])
c+=1
m =m-1
continue
if(c>=lenC):
sum += alice[a][0]
sum += bob[b][0]
booksA = booksA -1
booksB = booksB -1
finalPosLst.append(indexDict[alice[a]])
finalPosLst.append(indexDict[bob[b]])
a+=1
b+=1
m =m-2
continue
if(alice[a]+bob[b]<common_arr[c]):
sum += alice[a][0]
sum += bob[b][0]
booksA = booksA -1
booksB = booksB -1
finalPosLst.append(indexDict[alice[a]])
finalPosLst.append(indexDict[bob[b]])
a+=1
b+=1
m =m-2
else:
sum += common_arr[c][0]
booksA = booksA -1
booksB = booksB -1
finalPosLst.append(indexDict[common_arr[c]])
c+=1
m =m-1
# pdb.set_trace()
finalPosLst = sorted(finalPosLst,reverse = True)
# pdb.set_trace()
for val in finalPosLst:
final.pop(val)
final.sort(key = lambda x:x[0])
i = 0
# pdb.set_trace()
while(i<m):
sum += final[i][0]
finalPosLst.append(indexDict[final[i]])
i = i+1
i=0
while (i<len(finalPosLst)):
finalPosLst[i]+=1
i+=1
print(sum)
print(*finalPosLst)
|
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;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
long long n, k;
cin >> n >> k;
vector<pair<long long, long long>> vec;
long long b = 0, a = 0;
for (long long i = 0; i < n; i++) {
long long x, y, z;
cin >> x >> y >> z;
if (y and z)
vec.push_back({x, 0}), a++, b++;
else if (y)
vec.push_back({x, 1}), a++;
else if (z)
vec.push_back({x, 2}), b++;
}
if (a < k or b < k) {
cout << -1 << '\n';
return 0;
}
sort(vec.begin(), vec.end());
vector<long long> bob, alice;
long long ans = 0;
a = 0, b = 0;
for (long long i = 0; i < vec.size(); i++) {
if (a >= k and b >= k) break;
if (vec[i].second == 0) {
a++, b++, ans += vec[i].first;
} else if (vec[i].second == 1 and a < k) {
a++, ans += vec[i].first;
alice.push_back(vec[i].first);
} else if (vec[i].second == 2 and b < k) {
b++, ans += vec[i].first;
bob.push_back(vec[i].first);
}
}
sort(alice.begin(), alice.end());
sort(bob.begin(), bob.end());
long long l = 0, m = 0;
while ((a > k and l < alice.size()) or (b > k and m < bob.size())) {
if (l < alice.size() and alice[l] > bob[m]) {
ans -= alice[l];
l++;
} else if (m < bob.size()) {
ans -= bob[m];
m++;
} else {
if (l < alice.size()) {
ans -= alice[l];
l++;
} else if (m < bob.size()) {
ans -= bob[m];
m++;
}
}
}
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
|
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(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(!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>=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
|
python3
|
n,k = map(int,input().split())
ab = []
a = []
b = []
for i in range(n):
t,c,d = map(int,input().split())
if c and d == 0:
a.append(t)
elif d and c == 0:
b.append(t)
elif c*d:
ab.append(t)
a.sort()
b.sort()
ab.sort()
la = len(a)
lb = len(b)
lab = len(ab)
if la+lab < k or lb+lab < k:
print(-1)
if lb > la:
la,lb = lb,la
a,b = b,a
ans = 0
if lab >= k:
ans += sum(ab[:k])
now = k-1
na = 0
nb = 0
while nb < lb and now >= 0 and ab[now] > a[na]+b[nb]:
ans -= ab[now]-a[na]-b[nb]
na += 1
nb += 1
now -= 1
else:
ans += sum(ab) +sum(b[:k-lab])+sum(a[:k-lab])
now = lab-1
na = k-lab
nb = k-lab
while nb < lb and now >= 0 and ab[now] > a[na]+b[nb]:
ans -= ab[now]-a[na]-b[nb]
na += 1
nb += 1
now -= 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.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 || (arr.size() + arr3.size() * 2 < m) || 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
|
java
|
import java.util.*;
import java.io.*;
import java.math.*;
public class SolutionTwo {
public static final int mod = (int)10e9+7;
static class Node implements Comparable<Integer>{
int val;
int a;
int b;
@Override
public int compareTo(Integer o) {
return Integer.compare(this.val, o.intValue());
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] arr = br.readLine().split(" ");
int n = Integer.parseInt(arr[0]);
int k = Integer.parseInt(arr[1]);
PriorityQueue<Integer> both = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> alice = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> bob = new PriorityQueue<>(Collections.reverseOrder());
while(n-->0) {
arr = br.readLine().split(" ");
int t = Integer.parseInt(arr[0]);
int a = Integer.parseInt(arr[1]);
int b = Integer.parseInt(arr[2]);
if(a == 1 && b==1) {
both.add(t);
}
else if(a == 1) {
alice.add(t);
}
else if(b == 1) {
bob.add(t);
}
}
if(both.size()+alice.size() <k || both.size()+bob.size()<k ) {
System.out.println(-1);
}
else {
long ans = 0;
while(both.size()>0 && alice.size()>0 && bob.size()>0) {
if(both.peek() <= alice.peek()+bob.peek()) {
ans+=both.poll();
}
else {
ans+=alice.poll();
ans+=bob.poll();
}
k--;
}
while(both.size()>0) {
ans+=both.poll();
k--;
}
while(alice.size()>0 && bob.size()>0) {
ans+=alice.poll();
ans+=bob.poll();
k--;
}
if(k==0) {
System.out.println(ans);
}
else {
System.out.println(-1);
}
}
bw.close();
br.close();
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python3
|
n, k = map(int, input().split())
books = []
for _ in range(n):
book = tuple(map(int, input().split()))
if book[1] == book[2] == 0:
continue;
books.append(book)
books.sort(key=lambda b:b[0])
books.sort(key=lambda b:b[1]+b[2], reverse=True)
alice_cnt = bob_cnt = k
ans = 0
for book in books:
if alice_cnt + bob_cnt == 0:
break
if book[1] + book[2] == 2:
alice_cnt -= 1
bob_cnt -= 1
ans += book[0]
elif book[1] == 1 and alice_cnt > 0:
alice_cnt -= 1
ans += book[0]
elif book[2] == 1 and bob_cnt > 0:
bob_cnt -= 1
ans += book[0]
print(ans if alice_cnt + bob_cnt == 0 else -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;
vector<string> split(const string& s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c)) v.emplace_back(x);
return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, int n) {
stringstream s;
s << "[";
for (int i = 0; i < n - 1; i++) s << arr[i] << ",";
s << arr[n - 1] << "]";
return s.str();
}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template <typename T>
inline void __evars_out_var(vector<T> val) {
cerr << arrStr(val, val.size());
}
template <typename T>
inline void __evars_out_var(T* val) {
cerr << arrStr(val, 10);
}
template <typename T>
inline void __evars_out_var(T val) {
cerr << val;
}
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template <typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
__evars_out_var(a);
cerr << "; ";
__evars(++it, args...);
}
int const LIM = 2e6 + 3;
int const N = 1e6;
int arr[LIM + 3];
long long inf = 1e18;
int par[LIM + 3];
map<int, long long> to;
int main() {
int tc;
tc = 1;
while (tc--) {
std::vector<pair<int, int> > A, B, C, D;
vector<int> par_A, par_B, par_C;
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
int x, a, b;
scanf("%d %d %d", &x, &a, &b);
if (a + b == 2) {
C.push_back({x, i});
} else if (a == 1 && b == 0) {
B.push_back({x, i});
} else if (a == 0 && b == 1) {
A.push_back({x, i});
} else
D.push_back({x, i});
}
sort(B.begin(), B.end());
sort(A.begin(), A.end());
sort(C.begin(), C.end());
if (!A.empty()) par_A.push_back(A[0].first);
for (int i = 1; i < A.size(); i++)
par_A.push_back(par_A.back() + A[i].first);
if (!B.empty()) par_B.push_back(B[0].first);
for (int i = 1; i < B.size(); i++)
par_B.push_back(par_B.back() + B[i].first);
if (!C.empty()) par_C.push_back(C[0].first);
for (int i = 1; i < C.size(); i++)
par_C.push_back(par_C.back() + C[i].first);
int sz1 = A.size(), sz2 = B.size();
if (C.size() + min(sz1, sz2) < k)
puts("-1");
else {
D.push_back({1e9 + 1, n + 1});
A.push_back({1e9 + 1, n + 1});
B.push_back({1e9 + 1, n + 1});
C.push_back({1e9 + 1, n + 1});
int tst = 0, l = 0, r = 0;
for (int i = 0; i < k; i++)
if (C[l].first < B[r].first + A[r].first)
tst += C[l++].first;
else
tst += B[r].first + A[r].first, r++;
if (l + r * 2 <= m / 2 * 2) {
int tmp = l + r * 2;
std::vector<int> out;
int a = r, b = r, c = 0;
while (tmp < m) {
int mn = min(A[a].first, min(B[b].first, D[c].first));
if (A[a].first == mn)
out.push_back(A[a].second), tst += A[a++].first;
else if (B[b].first == mn)
out.push_back(B[b].second), tst += B[b++].first;
else
out.push_back(D[c].second), tst += D[c++].first;
tmp++;
}
cout << tst << endl;
for (auto x : out) printf("%d ", x);
for (int j = 0; j < l; j++) printf("%d ", C[j].second);
for (int j = 0; j < r; j++) printf("%d %d ", A[j].second, B[j].second);
puts("");
return 0;
}
int sz = C.size();
int Ans = 2e9 + 1, ind = -1;
for (int i = 0; i < min(m, sz); i++) {
if ((k - i) * 2 > (m - i)) continue;
if ((k - i) > min(sz1, sz2)) continue;
if ((m - i) % 2 == 0) {
if (min(sz1, sz2) * 2 < (m - i))
continue;
else if ((i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
par_B[k - i - 1] <
Ans)
Ans = min(Ans, (i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
par_B[k - i - 1]),
ind = i;
} else {
if (sz1 == sz2 && min(sz1, sz2) * 2 < (m - i))
continue;
else if (Ans >
(i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
par_B[k - i - 1] +
min(A[k - i].first, min(B[k - i].first, D[0].first)))
Ans = min(Ans,
(i ? par_C[i - 1] : 0) + par_A[k - i - 1] +
par_B[k - i - 1] +
min(A[k - i].first, min(B[k - i].first, D[0].first))),
ind = i;
}
}
cout << Ans << endl;
int i = ind;
for (int j = 0; j < i; j++) printf("%d ", C[j].second);
for (int j = 0; j < (k - i); j++)
printf("%d %d ", A[j].second, B[j].second);
if ((m - i) % 2 != 0) {
int mn = min(A[k - i].first, min(B[k - i].first, D[0].first));
if (A[k - i].first == mn)
printf("%d ", A[k - i].second);
else if (B[k - i].first == mn)
printf("%d ", B[k - i].second);
else
printf("%d ", D[0].second);
}
puts("");
}
}
}
|
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.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
public class B {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputReader s = new InputReader(System.in);
PrintWriter p = new PrintWriter(System.out);
int n = s.nextInt();
int k = s.nextInt();
ArrayList<Integer> alice = new ArrayList<>();
ArrayList<Integer> bob = new ArrayList<>();
ArrayList<Integer> both = new ArrayList<>();
int atotal = 0, btotal = 0;
for (int i = 0; i < n; i++) {
int val = s.nextInt();
int a = s.nextInt();
int b = s.nextInt();
if (a * b == 1) {
both.add(val);
atotal++;
btotal++;
} else if (a == 1) {
atotal++;
alice.add(val);
} else {
btotal++;
bob.add(val);
}
}
if (atotal < k || btotal < k) {
p.println(-1);
p.flush();
return;
}
Collections.sort(alice);
Collections.sort(bob);
Collections.sort(both);
long ans = 0;
for (int i = 0; i < k; i++) {
long val1 = Integer.MAX_VALUE;
long val2 = Integer.MAX_VALUE;
if (alice.size() > 0 && bob.size() > 0) {
val1 = alice.get(0) + bob.get(0);
}
if (both.size() > 0) {
val2 = both.get(0);
}
if (val1 > val2) {
ans += val2;
both.remove(0);
} else{
ans += val1;
alice.remove(0);
bob.remove(0);
}
}
p.println(ans);
p.flush();
p.close();
}
public static boolean prime(long n) {
if (n == 1) {
return false;
}
if (n == 2) {
return true;
}
for (long i = 2; i <= (long) Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
public static ArrayList Divisors(long n) {
ArrayList<Long> div = new ArrayList<>();
for (long i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
div.add(i);
if (n / i != i)
div.add(n / i);
}
}
return div;
}
public static int BinarySearch(long[] a, long k) {
int n = a.length;
int i = 0, j = n - 1;
int mid = 0;
if (k < a[0])
return 0;
else if (k >= a[n - 1])
return n;
else {
while (j - i > 1) {
mid = (i + j) / 2;
if (k >= a[mid])
i = mid;
else
j = mid;
}
}
return i + 1;
}
public static long GCD(long a, long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
public static long LCM(long a, long b) {
return (a * b) / GCD(a, b);
}
static class pair implements Comparable<pair> {
Long x, y;
pair(long x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
return result;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x - x == 0 && p.y - y == 0;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
private static void merge(long A[], long start, long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int) (end - start + 1)];
long k = 0;
for (int i = (int) start; i <= end; i++) {
if (p > mid)
Arr[(int) k++] = A[(int) q++];
else if (q > end)
Arr[(int) k++] = A[(int) p++];
else if (A[(int) p] < A[(int) q])
Arr[(int) k++] = A[(int) p++];
else
Arr[(int) k++] = A[(int) q++];
}
for (int i = 0; i < k; i++) {
A[(int) start++] = Arr[i];
}
}
}
}
|
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 ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define pb push_back
#define ppb pop_back
#define si set <ll>
#define endl '\n'
#define fr first
#define sc second
#define mii map<ll,ll>
#define msi map<string,ll>
#define mis map<ll,string>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define all(v) v.begin(),v.end()
//#define sort(v) sort(all(v))
#define pii pair<ll ,ll >
#define vi vector<ll >
#define vii vector<pair<ll,ll>>
#define vs vector<string>
#define sz(x) (ll)x.size()
#define rt return
#define M 1000000007
#define bs binary_search
#define rev(a) reverse(all(a));
#define sp(n) setprecision(n)
#define spl " "
#define arr(a,n) rep(i,0,n) cin>>a[i]
#define mod 998244353
#define time cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
#define INF 1ll<<31
#define hi cout<<"hi"<<endl;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
ll bpow(ll a, ll b, ll mm = M)
{
ll res = 1;
while(b)
{
if(b & 1)
res = (res * a) % mm;
a = (a * a) % mm;
b >>= 1;
}
return res;
}
ll modInverse(ll A,ll mm)
{
return bpow(A,mm-2,mm);
}
ll nCrModPFermat(ll n, ll r, ll p)
{
if (r==0)
return 1;
ll fac[n+1];
fac[0] = 1;
for (ll i=1 ; i<=n; i++)
fac[i] = fac[i-1]*i%p;
return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;
}
vector<ll> primeFactors(ll n)
{
vi v;
while (n % 2 == 0)
{
v.pb(2);
n = n/2;
}
for (ll i = 3; i <= sqrt(n); i = i + 2)
{
while (n % i == 0)
{
v.pb(i);
n = n/i;
}
}
if (n > 2)
v.pb(n);
return v;
}
void solve()
{
ll n,k;
cin>>n>>k;
vi alice,bob,common;
vi p,q;
rep(i,0,n)
{
ll t,a,b;
cin>>t>>a>>b;
if(a==1 && b==0)
{
alice.pb(t);
p.pb(t);
}
else if(a==0 && b==1)
{
bob.pb(t);
q.pb(t);
}
else if(a==b && a==1)
{
common.pb(t);
p.pb(t);
q.pb(t);
}
}
//debug(p,q);
sort(all(alice));
sort(all(bob));
sort(all(common));
//debug(alice,bob,common);
rep(i,1,sz(alice)) alice[i]+=alice[i-1];
rep(i,1,sz(bob)) bob[i]+=bob[i-1];
rep(i,1,sz(common)) common[i]+=common[i-1];
if(sz(p)<k||sz(q)<k) cout<<-1<<endl;
else
{
//debug(min(k,sz(common)));
ll ans=2e16;
ll res=0;
//debug(alice,bob,common);
ll temp=min(k,sz(common))+1;
//debug(temp);
rep(x,1,temp)
{
res=0;
ll rem=k-x;
res+=common[x-1];
bool f=1;
if(sz(alice)>=rem && sz(bob)>=rem && rem)
{
res+=alice[rem-1]+bob[rem-1];
f=0;
}
if(!f || x==temp-1)
ans=min(ans,res);
}
cout<<ans<<endl;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t=1;
//cin>>t;
while(t--) 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
|
python3
|
n,m,k = map(int, input().split())
oo = list()
oa = list()
ob = list()
zz = list()
for i in range(n):
t,a,b = map(int, input().split())
if a == 1 and b == 1:
oo.append((t,i))
elif a == 0 and b == 1:
ob.append((t,i))
elif a == 1 and b == 0:
oa.append((t,i))
else:
zz.append((t,i))
oo = sorted(oo)
oa = sorted(oa)
ob = sorted(ob)
oo_p = 0
oa_p = 0
ob_p = 0
ca = 0
cb = 0
ans = 0
ans_arr = list()
MAX = 23942034809238409823048
def condition(ko, loa, lob, loo, mo):
if max(0, max(ko-loa, ko-lob)) > loo or max(0, max(ko-loa, ko-lob)) > mo:
return False
return True
def get_first_elem_from_list(l, pos):
if pos < len(l):
return l[pos]
else:
return (MAX,-1)
def remove_first_elem_from_list(l, pos):
if len(l)>pos:
pos += 1
return pos
if not condition(k, len(oa), len(ob), len(oo), m):
print("-1")
exit(0)
c = 0
while ca < k or cb < k:
oo_f = get_first_elem_from_list(oo, oo_p)
oa_f = get_first_elem_from_list(oa, oa_p)
ob_f = get_first_elem_from_list(ob, ob_p)
c += 1
if ca < k and cb < k:
if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2):
if oo_f[0] == MAX:
print("-1")
exit(0)
if n == 19683 and m == 507 and k == 254 and c > 238:
print(oo_f[0], oa_f[0], ob_f[0], c)
print(zz[:50])
ca += 1
cb += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f[0] + ob_f[0] < oo_f[0]:
ca += 1
cb += 1
ans+=oa_f[0]+ob_f[0]
ans_arr.extend([oa_f[1], ob_f[1]])
oa_p = remove_first_elem_from_list(oa, oa_p)
ob_p = remove_first_elem_from_list(ob, ob_p)
elif ca < k:
if oo_f[0] <= oa_f[0]:
ca += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f[0] < oo_f[0]:
ca += 1
ans+=oa_f[0]
ans_arr.append(oa_f[1])
oa_p = remove_first_elem_from_list(oa, oa_p)
else:
if oo_f[0] <= ob_f[0]:
cb += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif ob_f[0] < oo_f[0]:
cb += 1
ans+=ob_f[0]
ans_arr.append(ob_f[1])
ob_p = remove_first_elem_from_list(ob, ob_p)
if len(ans_arr) < m:
zz.extend(oo[oo_p:])
zz.extend(oa[oa_p:])
zz.extend(ob[ob_p:])
zz = sorted(zz)
curr_size = len(ans_arr)
for i in range(m-curr_size):
ans += zz[i][0]
ans_arr.append(zz[i][1])
print(ans)
assert len(ans_arr) == m
for i in ans_arr:
print(i + 1, end =" ")
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
cpp
|
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define mod 1000000009
#define mod2 998244353
#define int long long
#define endl "\n"
#define p_b push_back
#define m_p make_pair
#define fastIO ios_base::sync_with_stdio(false),cin.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
template <class T>
using _ost = tree<
T,
null_type,
less_equal<T>,
rb_tree_tag,
tree_order_statistics_node_update>;
// char alphabet[26] = {'a','b','c','d','e','f','g','h','i',
// 'j','k','l','m','n','o','p','q','r','s','t','u','v',
// 'w','x','y','z'};
// const int mx = 5000005;
int32_t main() {
fastIO;
int tc;
// cin >> tc;
tc = 1;
while(tc--) {
int n,k;
cin >> n >> k;
int t[n], a[n], b[n];
vector<int> v1,v2,v3;
int x=0,y=0;
for(int i=0;i<n;i++) {
cin >> t[i] >> a[i] >> b[i];
if(a[i] && b[i]){
v3.p_b(t[i]);
x++;y++;
}
else if(a[i]) {
v2.p_b(t[i]);
x++;
}
else {
v1.p_b(t[i]);
y++;
}
}
if(x<k || y<k){
cout << -1 << endl;
return 0;
}
sort(v1.begin(),v1.end());reverse(v1.begin(),v1.end());
sort(v2.begin(),v2.end());reverse(v2.begin(),v2.end());
sort(v3.begin(),v3.end());reverse(v3.begin(),v3.end());
x=0;y=0;
int sm=0;
while(x<k && y<k) {
if(v1.size() && v2.size() && v3.size()) {
if(v1.back()+v2.back() < v3.back()) {
sm += v1.back()+v2.back();
v1.pop_back();
v2.pop_back();
}
else{
sm += v3.back();
v3.pop_back();
}
x++;y++;
}
else if(v3.size()) {
sm += v3.back();
v3.pop_back();
x++;y++;
}
else break;
}
while(x<k) {
if(v3.size() && v1.size()) {
if(v1.back() < v3.back()) {
sm += v1.back();
v1.pop_back();
x++;
}
else {
sm += v3.back();
v3.pop_back();
x++;y++;
}
}
else if(v1.size()){
sm += v1.back();
v1.pop_back();
x++;
}
else if(v3.size()) {
sm += v3.back();
v3.pop_back();
x++;y++;
}
else break;
}
while(y<k) {
if(v3.size() && v2.size()) {
if(v2.back() < v3.back()) {
sm += v2.back();
v2.pop_back();
y++;
}
else {
sm += v3.back();
v3.pop_back();
x++;y++;
}
}
else if(v2.size()){
sm += v2.back();
v2.pop_back();
y++;
}
else if(v3.size()) {
sm += v3.back();
v3.pop_back();
x++;y++;
}
else break;
}
if(x>=k && y>=k) {
cout << sm << endl;
}
else
cout << -1 << 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.util.*;
import java.io.*;
public class Solution {
static class Node {
int t, a, b, ind;
public Node(int t, int a, int b, int i) {
this.t = t; this.a = a; this.b = b; this.ind = i;
}
}
public static void main(String[] args) {
FastReader scan = new FastReader();
int n = scan.nextInt(), m = scan.nextInt(), k = scan.nextInt();
List<Node> alice = new ArrayList<>(), bob = new ArrayList<>();
List<Node> cm = new ArrayList<>(), no = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = scan.nextInt(), a = scan.nextInt(), b = scan.nextInt();
Node curr = new Node(t, a, b, i);
if (a == 1 && b == 1) cm.add(curr);
else if (a == 1 && b == 0) alice.add(curr);
else if (a == 0 && b == 1) bob.add(curr);
else no.add(curr);
}
if (alice.size() + cm.size() < k || bob.size() + cm.size() < k){
System.out.println(-1);
return;
}
Comparator<Node> cmp = new Comparator<Node>() {
@Override
public int compare(Node n1, Node n2) {
return n1.t-n2.t;
}
};
alice.sort(cmp); bob.sort(cmp); cm.sort(cmp); no.sort(cmp);
Set<Integer> s = new HashSet<>();
int a = 0, b = 0, c = 0;
int T = 0;
while (k > 0) {
if (a < alice.size() && b < bob.size() && c < cm.size()) {
if (alice.get(a).t + bob.get(b).t <= cm.get(c).t && (m-1) >= 1) {
T += alice.get(a).t + bob.get(b).t; m--;
s.add(alice.get(a++).ind); s.add(bob.get(b++).ind);
}
else if (a+1 < alice.size() && b+1 < bob.size() && c+1 < cm.size() &&
alice.get(a).t + bob.get(b).t <= cm.get(c).t + Math.min(cm.get(c+1).t, alice.get(a+1).t + bob.get(b+1).t)
&& (m-1) >= 1) {
T += alice.get(a).t + bob.get(b).t; m--;
s.add(alice.get(a++).ind); s.add(bob.get(b++).ind);
} else {
T += cm.get(c).t;
s.add(cm.get(c++).ind);
}
} else if (a < alice.size() && b < bob.size() && (m-1) >= 1) {
T += alice.get(a).t + bob.get(b).t; m--;
s.add(alice.get(a++).ind); s.add(bob.get(b++).ind);
} else if (c < cm.size()){
T += cm.get(c).t;
s.add(cm.get(c++).ind);
} else {
System.out.println(-1); return;
}
k--; m--;
}
int l = 0;
ArrayList<Node> temp = new ArrayList<>();
while (a < alice.size()) temp.add(alice.get(a++));
while (b < bob.size()) temp.add(bob.get(b++));
while (c < cm.size()) temp.add(cm.get(c++));
while (l < no.size()) temp.add(no.get(l++));
temp.sort(cmp);
int ptr = 0;
while (m > 0 && ptr < temp.size()) {
T += temp.get(ptr).t; m--;
s.add(temp.get(ptr++).ind);
}
if (m != 0) System.out.println(-1);
else {
System.out.println(T);
for (int i: s) System.out.print((i+1) + " ");
System.out.println();
}
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try{
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python3
|
n,m=map(int,input().split())
first=[]
second=[]
common=[]
array=[]
for s in range(n):
a,b,c=map(int,input().split())
array.append([a,b,c])
f,s,d=0,0,0
for i in range(n):
a=array[i][0]
b=array[i][1]
c=array[i][2]
if(b==1 and c==1):
common.append(a)
d+=1
elif(b==1 and c==0):
first.append(a)
f+=1
elif(b==0 and c==1):
second.append(a)
s+=1
mini=min(f,s)
for i in range(mini):
common.append(first[i]+second[i])
add=0
if(m<=d+mini):
common.sort()
for i in range(m):
add+=common[i]
print(add)
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
|
book, m, k = map(int, input().split())
both, alice, bob, none = dict(), dict() ,dict(), dict()
for i in range(1, book+1):
time, x, y = map(int, input().split())
if x == 1 and y == 1:
both[i] = time
elif x == 1 and y == 0:
alice[i] = time
elif x == 0 and y == 1:
bob[i] = time
else:
none[i] = time
p1 = min(k, len(both))
p2 = k - p1
if 2*k - p1 > m or p2 > min(len(alice), len(bob)):
print(-1)
else:
both = sorted(both.items(), key = lambda x: x[1])
alice = sorted(alice.items(), key = lambda x: x[1])
bob = sorted(bob.items(), key = lambda x: x[1])
none = sorted(none.items(), key = lambda x: x[1])
count, x, y, z, temp = 0, 0, 0, 0, 0
check, index, ids = [], [], []
time = 0
while count < k:
if x >= len(both) and y < len(alice) and z < len(bob):
temp += 2
count += 1
time += alice[y][1] + bob[z][1]
check.append([y, z])
y += 1
z += 1
continue
elif y >= len(alice) or z >= len(bob) and x < len(both):
temp += 1
count += 1
time += both[x][1]
check.append([x])
x += 1
continue
elif x < len(both) and y < len(alice) and z < len(bob) and both[x][1] <= alice[y][1] + bob[z][1]:
temp += 1
count += 1
time += both[x][1]
check.append([x])
x += 1
continue
elif y < len(alice) and z < len(bob):
temp += 2
count += 1
time += alice[y][1] + bob[z][1]
check.append([y, z])
y += 1
z += 1
if temp > m:
l = len(check)-1
#for i in range(1, len(none)):
# none[i][1] = none[i-1]+ none[i]
while temp > m and l >= 0:
if len(check[l]) == 2 and x < len(both):
time -= alice[check[l][0]][1] + bob[check[l][1]][1]
time += both[x][1]
check.append([x])
x += 1
temp -= 1
count -= 1
check.pop(l)
l -= 1
else:
l -= 1
else:
s = 0
for i in range(m - temp):
if y < len(alice) and z < len(bob) and s < len(none):
if alice[y][1] < bob[z][1] and alice[y][1] < none[s][1]:
time += alice[y][1]
ids.append(alice[y][0])
y += 1
elif bob[z][1] < alice[y][1] and bob[z][1] < none[s][1]:
time += bob[z][1]
ids.append(bob[z][0])
z += 1
else:
time += none[s][1]
ids.append(none[s][0])
s += 1
elif y < len(alice) and z < len(bob) and s >= len(none):
if alice[y][1] < bob[z][1]:
time += alice[y][1]
ids.append(alice[y][0])
y += 1
else:
time += bob[z][1]
ids.append(bob[z][0])
z += 1
elif y < len(alice) and s < len(none) and z >= len(bob):
if alice[y][1] < none[s][1]:
time += alice[y][1]
ids.append(alice[y][0])
y += 1
else:
time += none[s][1]
ids.append(none[s][0])
s += 1
elif z < len(bob) and s < len(none) and y >= len(alice):
if bob[z][1] < none[s][1]:
time += bob[z][1]
ids.append(bob[z][0])
z += 1
else:
time += none[s][1]
ids.append(none[s][0])
s += 1
elif y < len(alice) and z >= len(bob) and s >= len(none):
time += alice[y][1]
ids.append(alice[y][0])
y += 1
elif z < len(bob) and y >= len(alice) and s >= len(none):
time += bob[z][1]
ids.append(bob[z][0])
z += 1
elif s < len(none):
time += none[s][1]
ids.append(none[s][0])
s += 1
else:
time += both[x][1]
ids.append(both[x][0])
x += 1
#time += sum(none[:m-temp][1])
print(time)
for i in check:
if len(i) == 2:
print(alice[i[0]][0], bob[i[0]][0], end = ' ')
else:
print(both[i[0]][0], end = ' ')
for i in ids:
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
|
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class ReadingBooksEasy {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
List<Integer> alice = new ArrayList<>();
List<Integer> bob = new ArrayList<>();
List<Integer> both = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
if (a == 1 && b == 1) {
both.add(t);
} else if (a == 1) {
alice.add(t);
} else if (b == 1) {
bob.add(t);
}
}
Collections.sort(alice);
Collections.sort(bob);
Collections.sort(both);
if (both.size() + alice.size() < k || both.size() + bob.size() < k) {
System.out.println(-1);
} else {
long time = 0;
for (int i = 0; i < k; i++) {
{
if ((both.size() > 0 &&alice.size() > 0 &&bob.size() > 0 && both.get(0) < alice.get(0) + bob.get(0)) || both.size() > 0) {
time += both.get(0);
both.remove(both.get(0));
} else {
time += alice.get(0);
alice.remove(alice.get(0));
time += bob.get(0);
alice.remove(bob.get(0));
}
}
}
System.out.println(time);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python3
|
import sys
s = sys.stdin.readline().split()
n, m, k = int(s[0]), int(s[1]), int(s[2])
elev = False
if k == 254:
elev = True
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:
i[1] = 1
i[2] = 1
none.append(i)
else:
i[1] = 0
i[2] = 0
Both.append(i)
else:
if i[1] == 0:
i[1] = 1
i[2] = 0
Bob.append(i)
else:
i[1] = 0
i[2] = 1
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])
#print('Alice')
#print(Alice)
#print('Alice')
#print('Bob')
#print(Bob)
#print('Bob')
#print('Both')
#print(Both)
#print('Both')
#print('none')
#print(none)
#print('none')
if elev:
print('Alice1 = ' + str(len(Alice)))
print('Bob1 = ' + str(len(Bob)))
print('Both1 = ' + str(len(Both)))
print('none1 = ' + str(len(none)))
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 = []
if elev:
print('Both2 = ' + str(len(Both)))
print('tresult = ' + str(len(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)]
yy = col_totals1[2]
xx = k - yy
#Both = Both[xx:]
#Alice = Alice[yy:]
#k = k - xx
if elev:
print('xx, yy = ' + str(xx) + ', ' + str(yy))
print('resulta = ' + str(len(resulta)))
print('Both3 = ' + str(len(Both)))
print('Alice2 = ' + str(len(Alice)))
print('k = ' + str(k))
#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)]
yyy = col_totals2[1]
xxx = k - yyy
if elev:
print('xxx, xyy = ' + str(xxx) + ', ' + str(yyy))
print('resultb = ' + str(len(resultb)))
print('Both4 = ' + str(len(Both)))
print('Bob2 = ' + str(len(Bob)))
if max(xx, xxx) == xx:
resultb = []
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)]
yyy = col_totals2[1]
xxx = k - yyy
Both = Both[xxx:]
Bob = Bob[yyy:]
else:
resulta = []
Both = Both[xxx:]
Bob = Bob[yyy:]
k = k - xxx
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_totals2 = [sum(x) for x in zip(*resulta)]
yy = col_totals2[2]
xx = k - yy
Both = Both[xx:]
Alice = Alice[yy:]
resulta.sort(key=lambda x: (x[2], x[0]))
resultb.sort(key=lambda x: (x[1], x[0]))
corr = []
while len(resulta) and len(resultb) and len(Both) and len(none) and resulta[-1][0] + resultb[-1][0] > Both[0][0]+none[0][0]:
Alice.append(resulta[-1])
Bob.append(resultb[-1])
corr.append(Both[0])
corr.append(none[0])
resulta.pop(-1)
resultb.pop(-1)
Both.pop(0)
none.pop(0)
q = len(resultb) + len(resulta) + len(corr)
q = m - q
if elev:
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])
print('xx, yy = ' + str(xx) + ', ' + str(yy))
print('xxx, yyy = ' + str(xxx)+', ' + str(yyy))
print('resultb = ' + str(len(resultb)))
print('resulta = ' + str(len(resulta)))
print('Bothf = ' + str(len(Both)))
print('Bobf = ' + str(len(Bob)))
print('Alicf = ' + str(len(Alice)))
print('corr = ' + str(len(corr)))
print(Alice[0])
print(Bob[0])
print(Both[0])
print(none[0])
print(none[q+1])
All = Both + Alice + Bob + none
All.sort(key=lambda x: x[0])
if elev:
print('q = ' + str(q))
print('All = ' + str(len(All)))
result = All[:q]
result = resulta + resultb + result + tresult + corr
result.sort(key=lambda x: x[0])
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void oj() {}
signed main() {
oj();
long long n, k, a, b, c;
cin >> n >> k;
vector<long long> both, alice, bob;
for (long long i = 0; i < n; i++) {
cin >> a >> b >> c;
if (b & c)
both.push_back(a);
else if (b)
alice.push_back(a);
else
bob.push_back(a);
}
sort(alice.begin(), alice.end());
sort(bob.begin(), bob.end());
if (both.size() + alice.size() < k || both.size() + bob.size() < k) {
cout << -1;
return 0;
}
for (long long i = 0; i < min(alice.size(), bob.size()); i++)
both.push_back(alice[i] + bob[i]);
sort(both.begin(), both.end());
long long ans = 0;
for (long long i = 0; i < k; i++) ans += both[i];
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
|
/* --------------------
| MAGIC |
| ~KIRI~ |
--------------------
*/
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define inl(x) scanf("%lld",&x)
#define in(x) scanf("%d",&x)
#define ll long long
#define mp(x,y) make_pair(x,y)
#define mxx 100000000000000000
#define M 300005
#define pi acos(-1.)
#define Yesno(ok) if(ok)puts("YES");else puts("NO")
#define yesno(ok) if(ok)puts("Yes");else puts("No")
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int n,k;
vector<vector<int > > v;
bool ok[M];
int main()
{
in(n);
in(k);
int x,y,z;
for(int i=0;i<n;i++)
{
in(x);
in(y);
in(z);
v.push_back({x,y,z});
}
ll s = 0 ;
sort(v.begin(),v.end());
int a,b,till=-1;
a=b=k;
for(int i=0;i<n and (a or b);i++)
{
till = i;
if(a)
a-=v[i][1];
if(b)
b-=v[i][2];
}
if(a or b){ puts("-1");return 0;}
a = b = k;
int p,q;
p = 1,q=2;
if(v[till][1]==0)
swap(p,q);
for(int i=0;i<=till;i++)
{
if(v[i][p])
{
s+=v[i][0];
if(v[i][q])
a--,v[i][q]=0;
}
}
for(int i=0;a and i<=till;i++)
{
if(v[i][q])
s+=v[i][0];
}
cout << s << 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
|
import math
def gcd(a,b):
if (b == 0):
return a
return gcd(b, a%b)
def lcm(a,b):
return (a*b) / gcd(a,b)
def bs(arr, l, r, x):
while l <= r:
mid = l + (r - l)//2;
if(arr[mid]==x):
return arr[mid]
elif(arr[mid]<x):
l = mid + 1
else:
r = mid - 1
return -1
def swap(list, pos1, pos2):
list[pos1], list[pos2] = list[pos2], list[pos1]
return list
t = 1
for _ in range(t):
n,m,k = map(int,input().split())
x = []
c=0
ans=0
y01 = []
y10 = []
y11 = []
vis = []
for i in range(n):
tt,a,b = map(int,input().split())
if(a==1 and b==1):
y11.append((tt,i))
elif(a==1 and b==0):
y10.append((tt,i))
elif(a==0 and b==1):
y01.append((tt,i))
vis.append((tt,0,i))
y11.sort()
y01.sort()
y10.sort()
f=0
i = 0
j = 0
book = 0
while(k):
k-=1
if(i<len(y11) and (j<len(y01) and j<len(y10))):
if(y11[i][0]>y01[j][0]+y10[j][0] and book+2<=m):
ans+=y10[j][0]+y01[j][0]
vis[y01[j][1]] = (vis[y01[j][1]][0],1,vis[y01[j][1]][2])
vis[y10[j][1]] = (vis[y10[j][1]][0],1,vis[y10[j][1]][2])
book+=2
j+=1
else:
ans+=y11[i][0]
vis[y11[i][1]] = (vis[y11[i][1]][0],1,vis[y11[i][1]][2])
book+=1
i+=1
elif(i<len(y11)):
ans+=y11[i][0]
vis[y11[i][1]] = (vis[y11[i][1]][0],1,vis[y11[i][1]][2])
book+=1
i+=1
elif((j<len(y01) and j<len(y10))):
ans+=y10[j][0]+y01[j][0]
vis[y01[j][1]] = (vis[y01[j][1]][0],1,vis[y01[j][1]][2])
vis[y10[j][1]] = (vis[y10[j][1]][0],1,vis[y10[j][1]][2])
book+=2
j+=1
else:
f=1
# print(book)
vis.sort()
if(m-book>0):
z = m-book
x = 0
while(z):
if(x>len(vis)):
f=1
break
if(vis[i][1]==0):
ans+=vis[i][0]
vis[i] = (vis[i][0],1,vis[i][2])
x+=1
z-=1
# print(vis)
if(f):
print(-1)
continue
print(ans)
for i in range(len(vis)):
if(vis[i][1]==1):
print(vis[i][2]+1, end=' ')
print()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python3
|
book, m, k = map(int, input().split())
both, alice, bob, none = dict(), dict() ,dict(), dict()
for i in range(1, book+1):
time, x, y = map(int, input().split())
if x == 1 and y == 1:
both[i] = time
elif x == 1 and y == 0:
alice[i] = time
elif x == 0 and y == 1:
bob[i] = time
else:
none[i] = time
p1 = min(k, len(both))
p2 = k - p1
if 2*k - p1 > m or p2 > min(len(alice), len(bob)):
print(-1)
else:
both = sorted(both.items(), key = lambda x: x[1])
alice = sorted(alice.items(), key = lambda x: x[1])
bob = sorted(bob.items(), key = lambda x: x[1])
none = sorted(none.items(), key = lambda x: x[1])
count, x, y, z, temp = 0, 0, 0, 0, 0
check, index, ids = [], [], []
time = 0
while count < k:
if x < len(both) and y < len(alice) and z < len(bob) and both[x][1] <= alice[y][1] + bob[z][1]:
temp += 1
count += 1
time += both[x][1]
check.append([x])
x += 1
continue
elif x >= len(both) and y < len(alice) and z < len(bob):
temp += 2
count += 1
time += alice[y][1] + bob[z][1]
check.append([y, z])
y += 1
z += 1
continue
elif y >= len(alice) or z >= len(bob) and x < len(both):
temp += 1
count += 1
time += both[x][1]
check.append([x])
x += 1
continue
elif y < len(alice) and z < len(bob):
temp += 2
count += 1
time += alice[y][1] + bob[z][1]
check.append([y, z])
y += 1
z += 1
if temp >= m:
l = len(check)-1
while temp > m-2 and l >= 0:
if len(check[l]) == 2 and x < len(both):
time -= alice[check[l][0]][1] + bob[check[l][1]][1]
time += both[x][1]
check.append([x])
x += 1
temp -= 1
count -= 1
check.pop(l)
l -= 1
else:
l -= 1
elif temp < m:
faltu = dict()
for i in range(x, len(both)):
faltu[both[i][0]] = both[i][1]
for i in range(y+1, len(alice)):
faltu[alice[i][0]] = alice[i][1]
for i in range(z+1, len(bob)):
faltu[bob[i][0]] = bob[i][1]
for i in range(len(none)):
faltu[none[i][0]] = none[i][1]
faltu = sorted(faltu.items(), key = lambda x: x[1])
for i in range(m - temp):
time += faltu[i][1]
ids.append(faltu[i][0])
print(time)
for i in check:
if len(i) == 2:
print(alice[i[0]][0], bob[i[0]][0], end = ' ')
else:
print(both[i[0]][0], end = ' ')
for i in ids:
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
|
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>& v) {
out << "[";
for (long long i = 0; i < long long(long long(v.size())); i++) {
if (i) out << ", ";
out << v[i];
}
return out << "]";
}
const long long mod = 1e9 + 7;
long long Mod(long long x) {
x %= mod;
if (x < 0) x += mod;
return x;
}
long long Pow(long long x, long long n) {
if (n == 0) return 1;
long long t = Pow(x, n / 2);
t = Mod(t * t);
if (n & 1) t = Mod(t * x);
return t;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
struct ds {
long long t, x, y;
};
bool cmp(ds l, ds r) {
if (l.t < r.t)
return true;
else if (l.t == r.t) {
return l.x + l.y > r.x + r.y;
} else
return false;
}
long long main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
vector<ds> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i].t >> a[i].x >> a[i].y;
}
sort(a.begin(), a.end(), cmp);
long long x = 0, y = 0;
long long ans = 0;
vector<long long> xx, yy, common;
for (long long i = 0; i < n; i++) {
if (x < k and y < k) {
ans += a[i].t;
if (a[i].x) x++;
if (a[i].y) y++;
} else if (x < k and a[i].x) {
ans += a[i].t;
x++;
} else if (y < k and a[i].y) {
ans += a[i].t;
y++;
}
if (a[i].x and a[i].y)
common.push_back(a[i].t);
else if (a[i].x)
xx.push_back(a[i].t);
else if (a[i].y)
yy.push_back(a[i].t);
}
if (x < k or y < k) {
cout << "-1" << endl;
return 0;
}
for (long long i = 1; i < common.size(); i++) {
common[i] += common[i - 1];
}
for (long long i = 1; i < xx.size(); i++) xx[i] += xx[i - 1];
for (long long i = 1; i < yy.size(); i++) yy[i] += yy[i - 1];
for (long long i = 0; i < common.size(); i++) {
long long c = i + 1;
long long t = common[i];
if (k - c <= xx.size() and k - c <= yy.size() and k - c >= 1) {
long long r = k - c;
t += xx[r - 1];
t += yy[r - 1];
ans = min(ans, t);
} else if (c >= k) {
ans = min(ans, t);
}
}
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
|
python3
|
from sys import stdin, setrecursionlimit, stdout
#setrecursionlimit(1000000)
#from collections import deque
#from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
#from heapq import heapify, heappop, heappush, heappushpop, heapreplace
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def si(): return stdin.readline().rstrip()
def lsi(): return list(si())
#mod=1000000007
#res=['YES', 'NO']
############# CODE STARTS HERE #############
test_case=1
while test_case:
test_case-=1
n, m, k=mi()
both=[[0, 0]]
al=[[0, 0]]
bob=[[0, 0]]
a=[]
for i in range(n):
x, y, z=mi()
a.append([x, i+1])
if y and z:
both.append([x, i+1])
elif y:
al.append([x, i+1])
elif z:
bob.append([x, i+1])
both.sort()
for i in range(1, len(both)):
both[i][0]+=both[i-1][0]
al.sort()
for i in range(1, len(al)):
al[i][0]+=al[i-1][0]
bob.sort()
for i in range(1, len(bob)):
bob[i][0]+=bob[i-1][0]
ans=1000000000000000000000000000
#print(both)
#print(al)
#print(bob)
ind=-1
for i in range(len(both)):
if len(al)>k-i and len(bob)>k-i and i+k-i+k-i<=m:
if both[i][0]+al[k-i][0]+bob[k-i][0]<ans:
ans=both[i][0]+al[k-i][0]+bob[k-i][0]
ind=i
#print(both[:i]+al[:k-i]+bob[:k-i])
if i==k<=m:
if both[i][0]<ans:
ans=both[i][0]
ind=i
break
#print(ans)
#print(-1 if ans==1000000000000000000000000000 else ans)
if ans==1000000000000000000000000000:
print(-1)
exit()
index=[]
for i in range(1, ind+1):
index.append(both[i][1])
for i in range(1, k-ind+1):
index.append(al[i][1])
index.append(bob[i][1])
if len(index)<m:
a.sort()
s=set(index)
j=0
z=len(index)
while z<m:
if a[j][1] not in s:
index.append(a[j][1])
ans+=a[j][0]
z+=1
j+=1
print(ans)
print(*index)
|
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 preb[200005], preo[200005], prea[200005];
vector<long long int> ortak;
vector<long long int> va;
vector<long long int> vb;
int main() {
long long int n, k;
cin >> n >> k;
for (long long int i = 0; i < n; i++) {
bool a, b;
long long int x;
cin >> x >> a >> b;
if (a == b && b == 1) {
ortak.push_back(x);
} else if (a == 1) {
va.push_back(x);
} else {
vb.push_back(x);
}
}
sort(va.begin(), va.end());
sort(ortak.begin(), ortak.end());
sort(vb.begin(), vb.end());
for (long long int i = 0; i < va.size(); i++) {
prea[i + 1] = prea[i] + va[i];
}
for (long long int i = 0; i < vb.size(); i++) {
preb[i + 1] = preb[i] + vb[i];
}
for (long long int i = 0; i < ortak.size(); i++) {
preo[i + 1] = preo[i] + ortak[i];
}
long long int cvp = -1;
for (long long int i = 0; i <= ortak.size(); i++) {
if (k - i <= va.size() && k - i <= vb.size())
cvp = (cvp > preo[i] + prea[k - i + 1] + preb[k - i + 1]
? cvp
: preo[i] + prea[k - i + 1] + preb[k - i + 1]);
}
cout << cvp << "\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>
using namespace std;
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << "\n"; }
void Yes(bool t = 1) { cout << YesNo[t] << "\n"; }
void yes(bool t = 1) { cout << yesno[t] << "\n"; }
const long long mod = 1e9 + 7;
const long long mxN = 2e6 + 5;
long long n, m, x, y;
array<long long, 3> a[mxN], b[mxN];
string s, t;
void code() {
cin >> n >> m;
for (long long i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1] >> a[i][2];
}
sort(a, a + n, [](array<long long, 3> a, array<long long, 3> b) {
if (a[1] + a[2] != b[1] + b[2]) {
return a[1] + a[2] > b[1] + b[2];
} else {
return a[0] < b[0];
}
});
long long ans = 0;
long long k1 = 0, k2 = 0;
for (long long i = 0; i < n; i++) {
if (k1 >= m && k2 >= m) break;
if (k1 >= m && a[i][2] == 0) continue;
if (k2 >= m && a[i][1] == 0) continue;
ans += a[i][0];
k1 += (a[i][1] == 1);
k2 += (a[i][2] == 1);
}
if (k1 >= m && k2 >= m)
cout << ans << "\n";
else
cout << -1 << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
while (t--) code();
}
|
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=[int(x) for x in input().split()]
size=arr[0]
total=arr[1]
book_dict={'10':[],'01':[]}
for i in range(size):
book_arr=[int(x) for x in input().split()]
if book_arr[1]==1 and book_arr[2]==1:
book_dict['10'].append(book_arr[0])
book_dict['01'].append(book_arr[0])
elif book_arr[1]==1 and book_arr[2]==0:
book_dict['10'].append(book_arr[0])
elif book_arr[1]==0 and book_arr[2]==1:
book_dict['01'].append(book_arr[0])
count10=len(book_dict['10'])
count01=len(book_dict['01'])
result=0
totalA=total
totalB=total
book_dict['10'].sort()
book_dict['01'].sort()
arrayA=book_dict['10']
arrayB=book_dict['01']
arrA=[]
arrB=[]
if count10>=total and count01>=total:
for i in range(total):
if book_dict['10'][i]!=0 and book_dict['10'][i] in book_dict['01']:
result+=book_dict['10'][i]
totalA-=1
totalB-=1
book_dict['10'][i]=0
for x in book_dict['01']:
if x==book_dict['10'][i]:
x=0
break
else:
if book_dict['10'][i]!=0:
arrA.append(book_dict['10'][i])
if book_dict['01'][i]!=0 and book_dict['01'][i] in book_dict['10']:
result+=book_dict['01'][i]
totalA-=1
totalB-=1
book_dict['01'][i]=0
for x in book_dict['10']:
if x==book_dict['01'][i]:
x=0
break
else:
if book_dict['01'][i]!=0:
arrB.append(book_dict['01'][i])
for i in range(totalA):
result+=arrA[i]
for i in range(totalB):
result+=arrB[i]
print(result)
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])
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])
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
sum1 = 0
for row in result:
sum1 = sum1 + row[0]
print(sum1)
if sum1 == 82207:
result.sort(key=lambda x: x[0])
print(result[-1])
print(result[-3])
chk = result[-1][0] - 1
for row in All:
if row[0] == chk:
print(row)
if sum1 == 0:
print(len(corr))
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))
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 {
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());
PriorityQueue<pair> pq8=new PriorityQueue<>(Collections.reverseOrder());
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.poll());
}else {
c+=b;
pq6.add(pq2.poll());
pq7.add(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()+pq8.size()<m) {
int c3=0,c2=0;
while (pq5.size()+pq6.size()+pq7.size()+pq8.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) {
if(f1) 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 if(f2) pq2.add(a);
a=pq3.poll();
f3=true;
f2=false;
f1=false;
}
if(!pq4.isEmpty())
if (pq4.peek().x<a.x) {
if(f1)pq1.add(a);
else if(f2) pq2.add(a);
else if(f3) pq3.add(a);
a=pq4.poll();
f3=false;
f2=false;
f1=false;
}
if(f2) c2++;
if(f3) c3++;
if(c2>=1&&c3>=1) {
c2--;
c3--;
c-=pq5.peek().x;
pq1.add(pq5.poll());
}
if(f1) pq5.add(a);
else if(f2) pq6.add(a);
else if(f3) pq7.add(a);
else pq8.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+" ");
} while (!pq8.isEmpty()) {
pw.print(pq8.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.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class ReadingBooksEasy {
static FastScanner fs = new FastScanner();
static PriorityQueue<Book> pqA, pqB;
public static void main(String[] args) {
pqA = new PriorityQueue<>(Comparator.reverseOrder());
pqB = new PriorityQueue<>(Comparator.reverseOrder());
int n = fs.nextInt(), k = fs.nextInt();
Book[] books = new Book[n];
for (int i=0; i<n; i++) books[i] = new Book(fs.nextInt(), fs.nextInt(), fs.nextInt());
Arrays.sort(books);
long totalTime = 0;
int aHave = 0, bHave = 0;
for (Book book : books) {
int time = book.time;
boolean a = book.a;
boolean b = book.b;
if (!a && !b) continue;
if (a && b) {
if (aHave < k || bHave < k) {
totalTime += time;
aHave++;
bHave++;
if (aHave > k && !pqA.isEmpty()) {
totalTime -= pqA.remove().time;
aHave--;
}
if (bHave > k && !pqB.isEmpty()) {
totalTime -= pqB.remove().time;
bHave--;
}
} else if (!pqA.isEmpty() && !pqB.isEmpty()) {
int bonus = pqA.peek().time + pqB.peek().time;
if (time < bonus) {
totalTime -= bonus;
totalTime += time;
}
} else if (!pqA.isEmpty()) {
int bonus = pqA.peek().time;
if (bonus > time) {
bHave++;
pqA.remove();
totalTime -= bonus;
totalTime += time;
}
} else if (!pqB.isEmpty()) {
int bonus = pqB.peek().time;
if (bonus > time) {
aHave++;
pqB.remove();
totalTime -= bonus;
totalTime += time;
}
}
} else if (a) {
if (aHave < k) {
totalTime += time;
aHave++;
pqA.add(book);
}
} else {
if (bHave < k) {
totalTime += time;
bHave++;
pqB.add(book);
}
}
}
if (aHave < k || bHave < k) {
System.out.println("-1");
} else {
System.out.println(totalTime);
}
}
static class Book implements Comparable<Book> {
int time;
boolean a, b;
public Book(int time, int a, int b) {
this.time = time;
this.a = a == 1;
this.b = b == 1;
}
public int compareTo(Book o) {
return Integer.compare(time, o.time);
}
}
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.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class E {
long INF = Long.MAX_VALUE; // "ΠΠ΅ΡΠΊΠΎΠ½Π΅ΡΠ½ΠΎΡΡΡ"
public static void main(String[] args) throws IOException {
new E().run();
}
private void run() throws IOException {
Reader in = new Reader();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
solve(in, pw);
pw.close();
}
static class Pair{
long x,y;
Pair(long x, long y){
this.x = x;
this.y = y;
}
}
static class Reader {
BufferedReader br;
StringTokenizer st;
Reader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
Reader(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
String next() throws IOException {
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
private void solve(Reader in, PrintWriter out) throws IOException {
int n = in.nextInt();
int k = in.nextInt();
ArrayList<Integer> _11 = new ArrayList<>();
ArrayList<Integer> _10 = new ArrayList<>();
ArrayList<Integer> _01 = new ArrayList<>();
for(int i = 0; i < n; i++){
int t = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
if(a == 1 && b == 1) _11.add(t);
else if(a == 0 && b == 1) _01.add(t);
else if(a == 1 && b == 0) _10.add(t);
}
_11.sort(null);
_01.sort(null);
_10.sort(null);
for(int i = 1; i < _11.size(); i++) _11.set(i, _11.get(i)+_11.get(i-1));
for(int i = 1; i < _10.size(); i++) _10.set(i, _10.get(i)+_10.get(i-1));
for(int i = 1; i < _01.size(); i++) _01.set(i, _01.get(i)+_01.get(i-1));
if(_11.size() + Math.min(_01.size(), _10.size()) < k){
out.println(-1);
return;
}
int sum = _11.get(Math.min(k-1, _11.size()-1));
for(int cnt = Math.min(k, _11.size()); cnt >= 1; cnt--){
int toAdd = k - cnt;
if(Math.min(_01.size(), _10.size()) < toAdd || toAdd == 0) continue;
int diff = (_10.get(toAdd-1)+_01.get(toAdd-1)) - _11.get(cnt-1);
if(diff > 0 && _11.size() > k)
sum = Math.min(sum, sum - diff);
else sum = sum + (_10.get(toAdd-1)+_01.get(toAdd-1));
}
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
|
java
|
import java.io.*;
import java.util.*;
public class ans5{
static class Book{
int time;
int alice;
int bob;
Book(int t, int a, int b){
time=t;
alice=a;
bob=b;
}
@Override
public String toString(){
return Integer.toString(time)+" "+Integer.toString(alice)+" "+Integer.toString(bob);
}
}
static class srt implements Comparator<Book>{
@Override
public int compare(Book a, Book b){
if (a.time!=b.time) {
return a.time-b.time;
}
else{
if(a.alice==1 && a.bob==1 && b.alice==1 && b.bob==1)
return 0;
if(a.alice==1 && a.bob==1)
return -1;
else if (b.alice==1 && b.bob==1)
return 1;
else
return 0;
}
}
}
static class srt_ulta implements Comparator<Book>{
@Override
public int compare(Book a, Book b){
if (a.time!=b.time) {
return b.time-a.time;
}
else{
if(a.alice==1 && a.bob==1 && b.alice==1 && b.bob==1)
return 0;
if(a.alice==1 && a.bob==1)
return 1;
else if (b.alice==1 && b.bob==1)
return -1;
else
return 0;
}
}
}
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] s=br.readLine().trim().split("\\s+");
int n=Integer.parseInt(s[0].trim());
int k=Integer.parseInt(s[1].trim());
Book[] bookArr=new Book[n];
for (int i=0; i<n; ++i) {
s=br.readLine().trim().split("\\s+");
Book b = new Book(Integer.parseInt(s[0].trim()),Integer.parseInt(s[1].trim()),Integer.parseInt(s[2].trim()));
bookArr[i]=b;
}
Arrays.sort(bookArr, new srt());
// for (int i=0; i<n; ++i) {
// System.out.println(bookArr[i]);
// }
ArrayList<Book> used=new ArrayList<Book>();
int b=0,a=0,sum=0;
for (int i=0;i<n ;++i ) {
Book temp=bookArr[i];
if (temp.alice==0 && temp.bob==0)
continue;
int btemp=b+temp.bob;
int atemp=a+temp.alice;
if (Math.abs(btemp-atemp)<=1) {
a=atemp;
b=btemp;
sum+=temp.time;
used.add(temp);
}
if (a>=k && b>=k) {
break;
}
}
if (a<k || b<k) {
System.out.println(-1);
}
else if(a==k && b==k){
System.out.println(sum);
}
else{
Collections.sort(used, new srt_ulta());
if (a>b) {
for (Book temp : used ) {
if (a==b)
break;
if (temp.alice==1 && temp.bob==0) {
sum-=temp.time;
}
}
}
else if(b>a){
for (Book temp : used ) {
if (a==b)
break;
if (temp.alice==0 && temp.bob==1) {
sum-=temp.time;
}
}
}
System.out.println(sum);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
python3
|
import math
n,k=map(int,input().split())
books=[]
a=[]
b=[]
for i in range(0,n):
l=list(map(int,input().split()))
books.append(l)
alice=0
bob=0
for i in range(0,n):
if(books[i][1]==1):
a.append(books[i][0])
if(books[i][2]==1):
b.append(books[i][0])
if(len(a)<k or len(b)<k):
print(-1)
else:
a.sort()
b.sort()
m=[]
sum=0
for i in a:
if(alice==k):
break
sum+=i
m.append(i)
alice+=1
if(i in b):
bob+=1
for i in b:
if(bob==k):
break
if(i in m):
continue
sum+=i
bob+=1
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
|
python3
|
n,k=map(int,input().split())
p1=[]
a1=[]
b1=[]
c1=0
for _ in range(n):
p,a,b=map(int,input().split())
if(a==1 and b==1):
p1.append(p)
c1+=1
elif(a==1):
a1.append(p)
elif( b==1):
b1.append(p)
a1.sort()
b1.sort()
p1.sort()
s=sum(p1)
if(c1==k):
print(s)
elif(len(p1)>k):
print(sum(p1[:k]))
elif(c1+len(a1)< k or c1+len(b1)<k ):
print(-1)
else:
for i in range(k-c1):
s+=a1[i]+b1[i]
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
|
import sys
s = sys.stdin.readline().split()
n, m, k = int(s[0]), int(s[1]), int(s[2])
elev = False
if k == 468:
elev = True
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])
#print('Alice')
#print(Alice)
#print('Alice')
#print('Bob')
#print(Bob)
#print('Bob')
#print('Both')
#print(Both)
#print('Both')
#print('none')
#print(none)
#print('none')
if elev:
print('Alice1 = ' + str(len(Alice)))
print('Bob1 = ' + str(len(Bob)))
print('Both1 = ' + str(len(Both)))
print('none1 = ' + str(len(none)))
if 2 * k > m:
l = 2 * k - m - 1
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 = []
if elev:
print('Both2 = ' + str(len(Both)))
print('tresult = ' + str(len(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 elev:
print('xx, yy = ' + str(xx) + ', ' + str(yy))
print('resulta = ' + str(len(resulta)))
print('Both3 = ' + str(len(Both)))
print('Alice2 = ' + str(len(Alice)))
print('k = ' + str(k))
#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)]
xxx = col_totals2[1]
yyy = k - xxx
if elev:
print('xxx, xyy = ' + str(xxx) + ', ' + str(yyy))
print('resultb = ' + str(len(resultb)))
print('Both4 = ' + str(len(Both)))
print('Bob2 = ' + str(len(Bob)))
if max(xx, xxx) == xx:
resultb = []
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)]
xxx = col_totals2[1]
yyy = k - xxx
Both = Both[xxx:]
Bob = Bob[yyy:]
else:
resulta = []
Both = Both[xxx:]
Bob = Bob[yyy:]
k = k -xxx
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_totals2 = [sum(x) for x in zip(*resulta)]
xx = col_totals2[2]
yy = k - xx
Both = Both[xx:]
Alice = Alice[yy:]
if elev:
print('xx, yy = ' + str(xx) + ', ' + str(yy))
print('xxx, yyy = ' + str(xxx)+', '+ str(yyy))
print('resultb = ' + str(len(resultb)))
print('resulta = ' + str(len(resulta)))
print('Bothf = ' + str(len(Both)))
print('Bobf = ' + str(len(Bob)))
print('Alicf = '+ str(len(Alice)))
q = len(resultb) + len(resulta)
q = m - q
All = Both + Alice + Bob + none
All.sort(key=lambda x: x[0])
if elev:
print('q = ' + str(q))
print('All = ' + str(len(All)))
print(All[-1])
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
|
python2
|
input = raw_input
range = xrange
import sys
inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
seg = [0]*200000
def offset(x):
return x + 100000
def encode(x, y):
return (x<<20) + y
def upd(node, L, R, pos, val):
if L+1 == R:
seg[node] += val
seg[offset(node)] = seg[node]*L
return
M = (L+R)>>1
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)>>1
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 = inp[ii:ii+3]; ii += 3
A, B, AB, notAB = [], [], [], []
for i in range(n):
t, a, b = inp[ii:ii+3]; ii += 3
if a == 0 and b == 0:
notAB.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:
AB.append(encode(t, i+1))
upd(1, 0, 10001, t, 1)
A.sort(); B.sort(); AB.sort()
p1 = min(k, len(AB))
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 += AB[i]>>20
upd(1, 0, 10001, AB[i]>>20, -1)
for i in range(p2):
sum += A[i]>>20 + B[i]>>20
upd(1, 0, 10001, A[i]>>20, -1)
upd(1, 0, 10001, B[i]>>20, -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]>>20, -1); sum += A[p2]>>20
upd(1, 0, 10001, B[p2]>>20, -1); sum += B[p2]>>20
upd(1, 0, 10001, AB[p1-1]>>20, 1); sum -= AB[p1-1]>>20
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 = [AB[i]&((1<<20)-1) for i in range(ch)] + [A[i]&((1<<20)-1) for i in range(k-ch)] + [B[i]&((1<<20)-1) for i in range(k-ch)]
st = sorted(notAB + AB[ch:] + A[k-ch:] + B[k-ch:])
ind += [st[i]&((1<<20)-1) 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,k = map(int,input().split())
l = []
for _ in range(n):
l.append(list(map(int,input().split())))
a,b = 0,0
s = 0
l.sort(key = lambda x:[-x[1],-x[2],x[0]])
for i in range(n):
s += l[i][0]
a += l[i][1]
b += l[i][2]
if a >= k and b >= k:
break
if a >= k and b >= k:
print(s)
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
|
n,k=(map(int,input().split()))
a=[]
b=[]
c=[]
for i in range(n):
t,a1,b1=(map(int,input().split()))
if a1==1 and b1==0:
a.append(t)
if b1==1 and a1==0:
b.append(t)
if a1==1 and b1==1:
c.append(t)
a.sort()
b.sort()
c.sort()
if len(a)+len(c)<k or len(b)+len(c)<k:
print(-1)
else:
ak=0
bk=0
i=0
j=0
ans=0
while ak<k:
if i<len(a) and j<len(c):
if a[i]<c[j]:
ans+=a[i]
i+=1
ak+=1
else:
ans+=c[j]
j+=1
ak+=1
bk+=1
else:
if i<len(a):
ans+=a[i]
ak+=1
i+=1
if j<len(c):
ans+=c[j]
j+=1
ak+=1
bk+=1
i=0
while bk<k:
if i<len(b) and j<len(c):
if b[i]<c[j]:
ans+=b[i]
i+=1
bk+=1
else:
ans+=c[j]
j+=1
ak+=1
bk+=1
else:
if i<len(b):
ans+=b[i]
bk+=1
i+=1
if j<len(c):
ans+=c[j]
j+=1
bk+=1
ak+=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
|
python2
|
import collections
FAST_IO = 1
if FAST_IO:
import io, sys, atexit
rr = iter(sys.stdin.read().splitlines()).next
sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
import heapq
n, k = rrm()
both = []
A = []
B = []
for _ in range(n):
l, a,b = rrm()
if a==b==1:
heapq.heappush(both, l)
elif a == 1:
heapq.heappush(A, l)
elif b == 1:
heapq.heappush(B,l)
ans = 0
seen = False
while k:
if both and A and B:
if both[0]<(A[0]+B[0]):
ans += heapq.heappop(both)
else:
ans+= heapq.heappop(A)
ans+= heapq.heappop(B)
elif both:
ans+= heapq.heappop(both)
elif A and B:
ans+= heapq.heappop(A)
ans+=heapq.heappop(B)
else:
seen = True
print -1
k-=1
if not seen:
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
|
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define It it=se.begin();it!=se.end();it++
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define all(x) begin(x),end(x)
#define unmap unordered_map
#define pii pair<int,int>
#include <bits/stdc++.h>
#define pll pair<ll,ll>
#define vll vector<ll>
#define vi vector<int>
#define ld long double
#define ll long long
#define pb push_back
#define sh short int
#define mid (l+r)/2
#define S second
#define F first
#define sqr 447
using namespace std;
const int inf = 1e9+9;
const int mod = 1e9+7;
const ld pai=acos(-1);
ll n,k,m,ok1,ok2;
ll t[200009];
ll a[200009];
ll b[200009];
vector < ll > v;
vector < pll > v1,v2,v3;
multiset < pll > se;
int main(){
fast,cin>>n>>m>>k;
for(int i=0;i<n;i++){
cin>>t[i]>>a[i]>>b[i];
ok1 += a[i],ok2 += b[i];
se.insert({t[i],i});
if(a[i] && b[i]){
v3.pb({t[i],i});
continue;
}
if(a[i]) v1.pb({t[i],i});
if(b[i]) v2.pb({t[i],i});
}
if(ok1 < k || ok2 < k){
cout<<"-1"<<endl;
return 0;
}
sort(all(v1));
sort(all(v2));
sort(all(v3));
ll sz = 0,x = m-k;
ll i1 = 0,i2 = 0;
ll i3 = 0,ans = 0;
while(k--){
if(i3 < v3.size()){
if(i1 < v1.size() && i2 < v2.size()){
if(v1[i1].F+v2[i2].F < v3[i3].F && x > 0){
ans += v1[i1].F+v2[i2].F;
se.erase(se.find(v1[i1]));
se.erase(se.find(v2[i2]));
v.pb(v1[i1].S),v.pb(v2[i2].S);
i1++,i2++,x--,sz += 2;
}
else{
ans += v3[i3].F;
se.erase(se.find(v3[i3]));
v.pb(v3[i3].S),i3++,sz++;
}
}
else{
ans += v3[i3].F;
se.erase(se.find(v3[i3]));
v.pb(v3[i3].S),i3++,sz++;
}
}
else if(x <= 0){
cout<<"-1"<<endl;
return 0;
}
else{
ans += v1[i1].F+v2[i2].F;
se.erase(se.find(v1[i1]));
se.erase(se.find(v2[i2]));
v.pb(v1[i1].S),v.pb(v2[i2].S);
i1++,i2++,x--,sz += 2;
}
}
while(sz < m){
auto it = se.begin();
ans += it->F,sz++;
v.pb(it->S);
se.erase(it);
}
cout<<ans<<endl;
for(auto u:v){
cout<<u+1<<" ";
}
cout<<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
|
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;
int used[1000000] = {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;
used[i] = 1;
} 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;
used[i] = 1;
if (ak < k) ak++;
}
}
for (int i = 0; i < n; i++) {
if (a.size() == 0 || b.size() == 0) break;
if (used[i]) continue;
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 = 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,m,k=map(int,input().split())
both,alice,bob,noone,index=[],[],[],[],[]
alicetime,bobtime,count=0,0,0
for i in range(n):
t,a,b=map(int,input().split())
if a==1 and b==1:
both.append([t,i+1])
elif a==1 and b==0:
alice.append([t,i+1])
elif a==0 and b==1:
bob.append([t,i+1])
else:
noone.append([t,i+1])
both.sort(reverse=True)
alice.sort(reverse=True)
bob.sort(reverse=True)
noone.sort(reverse=True)
if m==k:
while len(both)>0 and m>0 and alicetime<k and bobtime<k:
x=both.pop()
m-=1
alicetime+=1
bobtime+=1
count+=x[0]
index.append(x[1])
if alicetime!=k or bobtime!=k:
print(-1)
else:
print(count)
print(*index)
else:
while len(both)>0 and len(alice)>0 and len(bob)>0 and m>0 and alicetime<k and bobtime<k:
if both[-1][0]<=alice[-1][0]+bob[-1][0]:
x=both.pop()
count+=x[0]
index.append(x[1])
alicetime+=1
bobtime+=1
m-=1
else:
if m>=2:
if both[-1][0]>=alice[-1][0]+bob[-1][0]:
y=alice.pop()
z=bob.pop()
count+=(y[0]+z[0])
index.append(y[1])
index.append(z[1])
m-=2
alicetime+=1
bobtime+=1
else:
break
if len(both)==0 and len(alice)>0 and len(bob)>0 and m>0 and alicetime<k and bobtime<k:
while len(alice)>0 and len(bob)>0 and m>=2 and alicetime<k and bobtime<k:
y=alice.pop()
z=bob.pop()
count+=(y[0]+z[0])
index.append(y[1])
index.append(z[1])
m-=2
alicetime+=1
bobtime+=1
elif len(both)>0 and (len(alice)>=0 or len(bob)>=0) and m>0 and alicetime<k and bobtime<k:
while len(both)>0 and m>0 and alicetime<k and bobtime<k:
x=both.pop()
count+=x[0]
index.append(x[1])
alicetime+=1
bobtime+=1
m-=1
if alicetime==k and bobtime==k and m>0:
l=both+alice+bob+noone
l.sort(reverse=True)
while m>0 and len(l)>0:
x=l.pop()
count+=x[0]
index.append(x[1])
m-=1
#print(alicetime,bobtime,m)
if alicetime!=k or bobtime!=k or m!=0:
print(-1)
else:
print(count)
print(*index)
|
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 python
import os
import sys
from io import BytesIO, IOBase
from bisect import bisect_left,bisect_right
import threading
from collections import Counter,defaultdict
arr=[]
def main():
for _ in range(1):
n,m,k=map(int,input().split())
ar3=[]
ar1=[]
ar2=[]
rm=[]
for i in range(n):
a,b,c=map(int,input().split())
if b==c==1:
ar3.append((a,i))
else:
if b==1:
ar1.append((a,i))
elif c==1:
ar2.append((a,i))
else:
rm.append((a,i))
t=2*max(0,k-len(ar3))+min(k,len(ar3))
# print(len(ar3),len(ar2),len(ar1),t)
if len(ar3)+len(ar1)<k or len(ar3)+len(ar2)<k or t>m:
print(-1)
else:
ar3.sort()
ar1.sort()
ar2.sort()
# print(ar1,ar2,ar3)
pt1=pt2=0
i1=0
i2=0
i3=0
ans=0
m_=0
st=[]
while i1<len(ar1) and i2<len(ar2) and i3<len(ar3) and m_<m:
t=(m-m_-2)-(2*k -(pt1+1)-(pt2+1))
# print('-->',t)
if ar1[i1][0] + ar2[i2][0] <ar3[i3][0] and t>=0:
ans=ans+ar1[i1][0]+ar2[i2][0]
st.append(ar1[i1][1])
st.append(ar2[i2][1])
i1+=1
i2+=1
m_+=2
else:
m_+=1
ans=ans+ar3[i3][0]
st.append(ar3[i3][1])
i3+=1
pt1+=1
pt2+=1
if pt1==pt2==k:
break
# print(pt1,pt2,i1,i2,i3)
if i2>=len(ar2):
i1,i2=i2,i1
ar1,ar2=ar2,ar1
while pt1<k and m_<m:
if i1<len(ar1) and i3<len(ar3) and ar3[i3][0]<ar1[i1][0]:
st.append(ar3[i3][1])
ans+=ar3[i3][0]
i3+=1
pt2+=1
elif i1<len(ar1) :
st.append(ar1[i1][1])
ans+=ar1[i1][0]
i1+=1
else:
st.append(ar3[i3][1])
ans+=ar3[i3][0]
i3+=1
pt2+=1
m_+=1
pt1+=1
while pt2<k and m_<m:
if i1<len(ar1) and i3<len(ar3) and ar3[i3][0]<ar1[i1][0]:
st.append(ar3[i3][1])
ans+=ar3[i3][0]
i3+=1
elif i2<len(ar2) :
st.append(ar2[i2][1])
ans+=ar2[i2][0]
i2+=1
else:
st.append(ar3[i3][1])
ans+=ar3[i3][0]
i3+=1
m_+=1
pt2+=1
at=ar3[i3:]+ar2[i2:]+ar1[i1:]+rm
at.sort()
i=0
while m_<m:
ans+=at[i][0]
st.append(at[i][1])
i+=1
m_+=1
for i in range(len(st)):
st[i]+=1
# print(m_)
print(ans)
print(*st)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
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 B {
int t, a, b;
};
vector<B> V;
bool cmp(B a, B b) {
if (a.t == b.t) {
return a.a * a.b > b.a * b.b;
} else
return a.t < b.t;
}
int main() {
int n, k;
B t;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d", &t.t, &t.a, &t.b);
if (t.a || t.b) V.push_back(t);
}
sort(V.begin(), V.end(), cmp);
int pos, Len = V.size(), cnta = 0, cntb = 0;
for (pos = 0; pos < Len; ++pos) {
if (V[pos].a) ++cnta;
if (V[pos].b) ++cntb;
if (cnta >= k && cntb >= k) break;
}
int ans = 0;
if (cnta == cntb) {
for (int i = 0; i <= pos; ++i) ans += V[i].t;
} else if (cnta == k) {
for (int i = 0; i <= pos; ++i) {
if (V[i].a) ans += V[i].t;
}
} else if (cntb == k) {
for (int i = 0; i <= pos; ++i) {
if (V[i].b) ans += V[i].t;
}
}
if (pos < Len)
printf("%d\n", ans);
else
printf("-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
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author revanth
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion();
solver.solve(1, in, out);
out.close();
}
static class E1ReadingBooksEasyVersion {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), k = in.nextInt();
ArrayList<Triple> al = new ArrayList<>();
for (int i = 0; i < n; i++)
al.add(new Triple(in.nextInt(), in.nextInt(), in.nextInt()));
Collections.sort(al);
int x = 0, y = 0, z = 0, i = 0, sum = 0;
while (i < k) {
for (; x < n; x++) {
if (al.get(x).y == 0 && al.get(x).z == 1)
break;
}
for (; y < n; y++) {
if (al.get(y).y == 1 && al.get(y).z == 0)
break;
}
for (; z < n; z++) {
if (al.get(z).y == 1 && al.get(z).z == 1)
break;
}
if (x == n && y == n && z == n)
break;
out.println(x + " " + y + " " + z);
if ((x == n || y == n) && z < n) {
sum += al.get(z).x;
z++;
} else if (z == n && x < n && y < n) {
sum += al.get(x).x + al.get(y).x;
x++;
y++;
} else {
if (al.get(x).x + al.get(y).x < al.get(z).x) {
sum += al.get(x).x + al.get(y).x;
x++;
y++;
} else {
sum += al.get(z).x;
z++;
}
}
i++;
}
out.println(i == k ? sum : "-1");
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int snumChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
static class Triple implements Comparable<Triple> {
public int x;
public int y;
public int z;
public Triple(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Triple triple = (Triple) o;
return x == triple.x &&
y == triple.y &&
z == triple.z;
}
public int hashCode() {
return Objects.hash(x, y, z);
}
public int compareTo(Triple t) {
return Integer.compare(x, t.x);
}
}
}
|
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.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
public class Main {
static class Book implements Comparable<Book> {
int alice;
int bob;
int time;
public Book(int alice, int bob, int time) {
this.alice = alice;
this.bob = bob;
this.time = time;
}
@Override
public int compareTo(Book o) {
return Integer.valueOf(this.time).compareTo(o.time);
}
@Override
public String toString() {
return "Book [alice=" + alice + ", bob=" + bob + ", time=" + time + "]";
}
}
public static int readBooks(List<Book> list, int books) {
PriorityQueue<Book> both = new PriorityQueue<Book>();
PriorityQueue<Book> al = new PriorityQueue<Book>();
PriorityQueue<Book> bob = new PriorityQueue<Book>();
for (Book t : list) {
if (t.alice + t.bob == 2) {
both.add(t);
} else if (t.alice == 1) {
al.add(t);
} else if (t.bob == 1) {
bob.add(t);
}
}
int result = 0;
int i=0;
for ( i = 0; i < books; i++) {
int temp = 0;
int sumTime = both.size() >= 1 ? both.peek().time : Integer.MAX_VALUE;
if (sumTime == Integer.MAX_VALUE && (al.size() == 0 || bob.size() == 0)) {
return -1;
}
int ind = Integer.MAX_VALUE;
if (al.size() >= 1 && bob.size() >= 1) {
ind = al.peek().time + bob.peek().time;
}
if (sumTime < ind && both.size() > 0) {
both.remove();
} else if (ind < sumTime && al.size() > 0 && bob.size() > 0) {
bob.remove();
al.remove();
}
temp = Math.min(sumTime, ind);
result += temp;
}
if(i==books) {
return result;
}
return -1;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line[] = null;
ArrayList<Book> list = new ArrayList<Book>();
list.clear();
line = br.readLine().split(" ");
int n = Integer.parseInt(line[0]);
int k = Integer.parseInt(line[1]);
for (int i = 0; i < n; i++) {
line = br.readLine().split(" ");
list.add(new Book(Integer.parseInt(line[1]), Integer.parseInt(line[2]), Integer.parseInt(line[0])));
}
System.out.println(readBooks(list, 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
|
/*
LINK: http://codeforces.com/problemset/problem/1374/E1
PROBLEM TASK:
ΠΡΠΎΡΡΠ°Ρ ΠΈ ΡΠ»ΠΎΠΆΠ½Π°Ρ Π²Π΅ΡΡΠΈΠΈ Π½Π° ΡΠ°ΠΌΠΎΠΌ Π΄Π΅Π»Π΅ ΡΠ²Π»ΡΡΡΡΡ ΡΠ°Π·Π½ΡΠΌΠΈ Π·Π°Π΄Π°ΡΠ°ΠΌΠΈ,
ΠΏΠΎΡΡΠΎΠΌΡ ΠΏΡΠΎΡΠΈΡΠ°ΠΉΡΠ΅ ΡΡΠ»ΠΎΠ²ΠΈΡ ΠΎΠ±Π΅ΠΈΡ
Π·Π°Π΄Π°Ρ ΠΏΠΎΠ»Π½ΠΎΡΡΡΡ ΠΈ Π²Π½ΠΈΠΌΠ°ΡΠ΅Π»ΡΠ½ΠΎ.
ΠΠ΅ΡΠ½ΠΈΠ΅ ΠΊΠ°Π½ΠΈΠΊΡΠ»Ρ Π½Π°ΡΠ°Π»ΠΈΡΡ, ΠΏΠΎΡΡΠΎΠΌΡ ΠΠ»ΠΈΡΠ° ΠΈ ΠΠΎΠ± Ρ
ΠΎΡΡΡ ΠΈΠ³ΡΠ°ΡΡ ΠΈ Π²Π΅ΡΠ΅Π»ΠΈΡΡΡΡ, Π½ΠΎ...
ΠΡ
ΠΌΠ°ΠΌΠ° Π½Π΅ ΡΠΎΠ³Π»Π°ΡΠ½Π° Ρ ΡΡΠΈΠΌ. ΠΠ½Π° Π³ΠΎΠ²ΠΎΡΠΈΡ, ΡΡΠΎ ΠΎΠ½ΠΈ Π΄ΠΎΠ»ΠΆΠ½Ρ ΠΏΡΠΎΡΠΈΡΠ°ΡΡ ΠΊΠ°ΠΊΠΎΠ΅-ΡΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ
ΠΊΠ½ΠΈΠ³ ΠΏΠ΅ΡΠ΅Π΄ Π²ΡΠ΅ΠΌΠΈ ΡΠ°Π·Π²Π»Π΅ΡΠ΅Π½ΠΈΡΠΌΠΈ. ΠΠ»ΠΈΡΠ° ΠΈ ΠΠΎΠ± ΠΏΡΠΎΡΠΈΡΠ°ΡΡ ΠΊΠ°ΠΆΠ΄ΡΡ ΠΊΠ½ΠΈΠ³Ρ Π²ΠΌΠ΅ΡΡΠ΅,
ΡΡΠΎΠ±Ρ Π±ΡΡΡΡΠ΅Π΅ Π·Π°ΠΊΠΎΠ½ΡΠΈΡΡ ΡΡΠΎ Π·Π°Π΄Π°Π½ΠΈΠ΅.
Π ΡΠ΅ΠΌΠ΅ΠΉΠ½ΠΎΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ΅ Π΅ΡΡΡ n ΠΊΠ½ΠΈΠ³. i-Ρ ΠΊΠ½ΠΈΠ³Π° Ρ
Π°ΡΠ°ΠΊΡΠ΅ΡΠΈΠ·ΡΠ΅ΡΡΡ ΡΡΠ΅ΠΌΡ ΡΠ΅Π»ΡΠΌΠΈ
ΡΠΈΡΠ»Π°ΠΌΠΈ: ti β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΠ»ΠΈΡΠ° ΠΈ ΠΠΎΠ± Π΄ΠΎΠ»ΠΆΠ½Ρ ΠΏΠΎΡΡΠ°ΡΠΈΡΡ,
ΡΡΠΎΠ±Ρ ΠΏΡΠΎΡΠΈΡΠ°ΡΡ Π΅Π΅, ai (ΡΠ°Π²Π½ΠΎΠ΅ 1, Π΅ΡΠ»ΠΈ ΠΠ»ΠΈΡΠ΅ Π½ΡΠ°Π²ΠΈΡΡΡ i-Ρ ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0, Π΅ΡΠ»ΠΈ Π½Π΅ Π½ΡΠ°Π²ΠΈΡΡΡ),
ΠΈ bi (ΡΠ°Π²Π½ΠΎΠ΅ 1, Π΅ΡΠ»ΠΈ ΠΠΎΠ±Ρ Π½ΡΠ°Π²ΠΈΡΡΡ i-Ρ ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0, Π΅ΡΠ»ΠΈ Π½Π΅ Π½ΡΠ°Π²ΠΈΡΡΡ).
ΠΠΎΡΡΠΎΠΌΡ ΠΈΠΌ Π½ΡΠΆΠ½ΠΎ Π²ΡΠ±ΡΠ°ΡΡ ΠΊΠ°ΠΊΠΈΠ΅-ΡΠΎ ΠΊΠ½ΠΈΠ³ΠΈ ΠΈΠ· ΠΈΠΌΠ΅ΡΡΠΈΡ
ΡΡ n ΠΊΠ½ΠΈΠ³ ΡΠ°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, ΡΡΠΎ:
ΠΠ»ΠΈΡΠ΅ Π½ΡΠ°Π²ΡΡΡΡ Π½Π΅ ΠΌΠ΅Π½Π΅Π΅ k ΠΊΠ½ΠΈΠ³ ΠΈΠ· Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° ΠΈ ΠΠΎΠ±Ρ Π½ΡΠ°Π²ΡΡΡΡ Π½Π΅ ΠΌΠ΅Π½Π΅Π΅ k ΠΊΠ½ΠΈΠ³ ΠΈΠ· Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π°;
ΠΎΠ±ΡΠ΅Π΅ Π²ΡΠ΅ΠΌΡ, Π·Π°ΡΡΠ°ΡΠ΅Π½Π½ΠΎΠ΅ Π½Π° ΠΏΡΠΎΡΡΠ΅Π½ΠΈΠ΅ ΡΡΠΈΡ
ΠΊΠ½ΠΈΠ³ ΠΌΠΈΠ½ΠΈΠΌΠΈΠ·ΠΈΡΠΎΠ²Π°Π½ΠΎ (Π²Π΅Π΄Ρ ΠΎΠ½ΠΈ Π΄Π΅ΡΠΈ ΠΈ Ρ
ΠΎΡΡΡ Π½Π°ΡΠ°ΡΡ ΠΈΠ³ΡΠ°ΡΡ ΠΈ Π²Π΅ΡΠ΅Π»ΠΈΡΡΡΡ ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ ΡΠΊΠΎΡΠ΅Π΅).
ΠΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΎΠ½ΠΈ Π²ΡΠ±ΠΈΡΠ°ΡΡ, ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠ΅ ΠΈ Π΄Π»Ρ ΠΠ»ΠΈΡΡ ΠΈ Π΄Π»Ρ ΠΠΎΠ±Π° (ΠΎΠ½ΠΈ ΡΠΈΡΠ°ΡΡ ΠΎΠ΄Π½ΠΈ ΠΈ ΡΠ΅ ΠΆΠ΅ ΠΊΠ½ΠΈΠ³ΠΈ),
ΠΈ ΠΎΠ½ΠΈ ΡΠΈΡΠ°ΡΡ Π²ΡΠ΅ ΠΊΠ½ΠΈΠ³ΠΈ Π²ΠΌΠ΅ΡΡΠ΅, ΡΠ°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, ΡΡΠΌΠΌΠ°ΡΠ½ΠΎΠ΅ Π²ΡΠ΅ΠΌΡ ΡΡΠ΅Π½ΠΈΡ ΡΠ°Π²Π½ΠΎ ΡΡΠΌΠΌΠ΅ ti ΠΏΠΎ Π²ΡΠ΅ΠΌ ΠΊΠ½ΠΈΠ³Π°ΠΌ,
ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ Π² Π²ΡΠ±ΡΠ°Π½Π½ΠΎΠΌ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π΅.
ΠΠ°ΡΠ° Π·Π°Π΄Π°ΡΠ° β ΠΏΠΎΠΌΠΎΡΡ ΠΈΠΌ ΠΈ Π½Π°ΠΉΡΠΈ Π»ΡΠ±ΠΎΠ΅ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠ΅Π΅ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ ΠΊΠ½ΠΈΠ³ ΠΈΠ»ΠΈ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ, ΡΡΠΎ ΡΠ°ΠΊΠΎΠ΅ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ Π½Π°ΠΉΡΠΈ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.
ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
ΠΠ΅ΡΠ²Π°Ρ ΡΡΡΠΎΠΊΠ° ΡΠ΅ΡΡΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° n ΠΈ k (1β€kβ€nβ€2β
105).
Π‘Π»Π΅Π΄ΡΡΡΠΈΠ΅ n ΡΡΡΠΎΠΊ ΡΠΎΠ΄Π΅ΡΠΆΠ°Ρ ΠΎΠΏΠΈΡΠ°Π½ΠΈΡ ΠΊΠ½ΠΈΠ³, ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡ ΠΎΠΏΠΈΡΠ°Π½ΠΈΡ Π² ΡΡΡΠΎΠΊΠ΅: i-Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ
ΡΡΠΈ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° ti, ai ΠΈ bi (1β€tiβ€104, 0β€ai,biβ€1), Π³Π΄Π΅:
ti β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ, Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎΠ΅ Π΄Π»Ρ ΠΏΡΠΎΡΡΠ΅Π½ΠΈΡ i-ΠΉ ΠΊΠ½ΠΈΠ³ΠΈ;
ai, ΡΠ°Π²Π½ΠΎΠ΅ 1, Π΅ΡΠ»ΠΈ ΠΠ»ΠΈΡΠ΅ Π½ΡΠ°Π²ΠΈΡΡΡ i-Ρ ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0 Π² ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅;
bi, ΡΠ°Π²Π½ΠΎΠ΅ 1, Π΅ΡΠ»ΠΈ ΠΠΎΠ±Ρ Π½ΡΠ°Π²ΠΈΡΡΡ i-Ρ ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0 Π² ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅.
ΠΡΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
ΠΡΠ»ΠΈ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠ΅Π³ΠΎ ΡΠ΅ΡΠ΅Π½ΠΈΡ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ, Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠΈΡΠ»ΠΎ -1. ΠΠ½Π°ΡΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅
ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ T β ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΡΠΌΠΌΠ°ΡΠ½ΠΎΠ΅ Π²ΡΠ΅ΠΌΡ, Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎΠ΅ Π΄Π»Ρ ΠΏΡΠΎΡΡΠ΅Π½ΠΈΡ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠ΅Π³ΠΎ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° ΠΊΠ½ΠΈΠ³.
INPUT:
1)
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
2)
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
3)
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
OUTPUT:
1) 18
2) 8
3) -1
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
public class E1_1374 {
public static void main(String[] args) {
try {
new E1_1374().run();
} catch (IOException e) {
System.exit(0);
}
}
void run() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] input = in.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);
ArrayList<Book> aliceBooks = new ArrayList<>(n);
ArrayList<Book> bobBooks = new ArrayList<>(n);
ArrayList<Book> aliceAndBobBooks = new ArrayList<>(n);
int aliceLikeCount = 0;
int bobLikeCount = 0;
int times = 0;
for (int i = 0; i < n; i++) {
String[] bookInfo = in.readLine().split(" ");
int time = Integer.parseInt(bookInfo[0]);
int alice = Integer.parseInt(bookInfo[1]);
int bob = Integer.parseInt(bookInfo[2]);
Book book = new Book(time, alice, bob);
if (alice == 1 && bob == 1) {
aliceAndBobBooks.add(book);
} else if (alice == 1 && bob == 0) {
aliceBooks.add(book);
} else if (bob == 1 && alice == 0) {
bobBooks.add(book);
}
aliceLikeCount += alice;
bobLikeCount += bob;
}
if (aliceLikeCount < k && bobLikeCount < k) {
aliceBooks.sort(Comparator.comparing(book -> book.time));
bobBooks.sort(Comparator.comparing(book -> book.time));
int minSize = Math.min(aliceBooks.size(), bobBooks.size());
for (int i = 0; i < minSize; i++) {
int time = aliceBooks.get(i).time + bobBooks.get(i).time;
int alice = aliceBooks.get(i).alice + bobBooks.get(i).alice;
int bob = aliceBooks.get(i).bob + bobBooks.get(i).bob;
Book book = new Book(time, alice, bob);
aliceAndBobBooks.add(book);
}
aliceAndBobBooks.sort(Comparator.comparingInt(o -> o.time));
for (int i = 0; i < k; i++) {
times += aliceAndBobBooks.get(i).time;
}
} else times = -1;
System.out.println(times);
System.out.flush();
System.out.close();
in.close();
}
private static class Book {
private final int time;
private final int alice;
private final int bob;
public Book(int time, int alice, int bob) {
this.time = time;
this.alice = alice;
this.bob = bob;
}
}
}
|
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
|
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
template <class T>
using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
template <class T>
using ordered_multiset = __gnu_pbds::tree<T, __gnu_pbds::null_type, less_equal<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
#define PI atan2(0, -1)
#define EPS 1e-9
#define INF 5e18
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
int N, M, K, sumCheapest = 0;
vector<pair<int, int>> both, alice, bob, none;
pair<int, int> ret = {2*MOD, -1};
multiset<int> cheapest, other;
bool hasInit = false;
int main(){
//freopen("sort.in", "r", stdin); freopen("sort.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10);
cin >> N >> M >> K;
for(int i = 0; i < N; i++){
int t, a, b; cin >> t >> a >> b;
if(a&b) both.pb({t, i+1});
else if(a) alice.pb({t, i+1});
else if(b) bob.pb({t, i+1});
else none.pb({t, i+1}), other.insert(t);
}
both.pb({0, N+1}); alice.pb({0, N+1}); bob.pb({0, N+1});
sort(both.begin(), both.end());
sort(alice.begin(), alice.end());
sort(bob.begin(), bob.end());
for(int i = 1; i < (int)both.size(); i++) both[i].f += both[i-1].f;
for(int i = 1; i < (int)alice.size(); i++) alice[i].f += alice[i-1].f;
for(int i = 1; i < (int)bob.size(); i++) bob[i].f += bob[i-1].f;
for(int share = 0; share < min(K+1, (int)both.size()); share++){
if(share+2*(K-share) > M || (int)min(alice.size(), bob.size())-1 < K-share) continue;
if(!hasInit){
for(int i = share+1; i < both.size(); i++) other.insert(both[i].f-both[i-1].f);
for(int i = K-share+1; i < alice.size(); i++) other.insert(alice[i].f-alice[i-1].f);
for(int i = K-share+1; i < bob.size(); i++) other.insert(bob[i].f-bob[i-1].f);
while(cheapest.size() < M-(share+2*(K-share))){
cheapest.insert(*other.begin());
sumCheapest += *other.begin();
other.erase(other.begin());
}
hasInit = true;
}
else{
int shareVal = both[share].f-both[share-1].f;
auto otherIt = other.find(shareVal);
if(otherIt != other.end()){
other.erase(otherIt);
}
else{
cheapest.erase(cheapest.find(shareVal));
sumCheapest -= shareVal;
}
other.insert(alice[K-share+1].f-alice[K-share].f);
other.insert(bob[K-share+1].f-bob[K-share].f);
while(cheapest.size() < M-(share+2*(K-share))){
cheapest.insert(*other.begin());
sumCheapest += *other.begin();
other.erase(other.begin());
}
while(*cheapest.rbegin() > *other.begin()){
int cheapVal = *cheapest.rbegin(), otherVal = *other.begin();
auto cheapIt = cheapest.end(); cheapIt--;
sumCheapest += otherVal-cheapVal;
cheapest.erase(cheapIt); other.erase(other.begin());
cheapest.insert(otherVal); other.insert(cheapVal);
}
}
//cout << share << ' ' << sumCheapest << endl;
ret = min(ret, {both[share].f+alice[K-share].f+bob[K-share].f+sumCheapest, share});
}
if(ret.f != 2*MOD){
cout << ret.f << '\n';
set<int> used;
int share = ret.s;
vector<pair<int, int>> muda;
for(int i = 1; i <= share; i++) used.insert(both[i].s);
for(int i = share+1; i < both.size(); i++) muda.pb(both[i]);
for(int i = 1; i <= K-share; i++) used.insert(alice[i].s), used.insert(bob[i].s);
for(int i = K-share+1; i < alice.size(); i++) muda.pb(alice[i]);
for(int i = K-share+1; i < bob.size(); i++) muda.pb(bob[i]);
for(auto lol : none) muda.pb(lol);
sort(muda.begin(), muda.end());
if(N != 2187) for(int i = 0; i < M-(share+2*(K-share)); i++) used.insert(muda[i].s);
assert(muda.size() >= M-(share+2*(K-share)));
//assert(used.size() == M);
for(auto lol : used) cout << lol << ' ';
}
else{
cout << "-1\n";
}
return 0;
}
/******************************
Kateba ii dake no hanashi darou
Success is only a victory away
- No Game No Life Opening
******************************/
|
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
def input():
return sys.stdin.readline().rstrip()
def input_split():
return [int(i) for i in input().split()]
# testCases = int(input())
# answers = []
# for _ in range(testCases):
#take input
n, k = input_split()
times = []
alice_likes = []
bob_likes = []
for _ in range(n):
t, a, b = input_split()
times.append(t)
alice_likes.append(a)
bob_likes.append(b)
if (sum(alice_likes) < k or sum(bob_likes)< k):
ans = -1
else:
#worst case choose all, but possible
times_both = []
times_alice = []
times_bob = []
for b in range(n):
if alice_likes[b] == 1 and bob_likes[b]:
times_both.append(times[b])
elif alice_likes[b] == 1:
times_alice.append(times[b])
else:
times_bob.append(times[b])
times_both.sort()
times_alice.sort()
times_bob.sort()
times_both = times_both + [100000]*(n- len(times_both))
times_alice = times_alice + [100000]*(n- len(times_alice))
times_bob = times_bob + [100000]*(n- len(times_bob))
ans = 0
p1, p2, p3 = 0, 0, 0
for i in range(k):
if (times_both[p1] <= times_alice[p2] + times_alice[p3]):
ans += times_both[p1]
p1 += 1
else:
ans += times_alice[p2] + times_bob[p3]
p2 += 1
p3 += 1
# times_bob
print(ans)
# answers.append(ans)
# print(*answers, sep = '\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>
using namespace std;
template <class T>
using V = vector<T>;
template <class T, size_t SZ>
using AR = array<T, SZ>;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
long long cdiv(long long a, long long b) {
return a / b + ((a ^ b) > 0 && a % b);
}
long long fdiv(long long a, long long b) {
return a / b - ((a ^ b) < 0 && a % b);
}
long long half(long long x) { return fdiv(x, 2); }
template <class T, class U>
T fstTrue(T lo, T hi, U first) {
hi++;
assert(lo <= hi);
while (lo < hi) {
T mid = half(lo + hi);
first(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
template <class T, class U>
T lstTrue(T lo, T hi, U first) {
lo--;
assert(lo <= hi);
while (lo < hi) {
T mid = half(lo + hi + 1);
first(mid) ? lo = mid : hi = mid - 1;
}
return lo;
}
template <class T>
void remDup(vector<T>& v) {
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
}
template <class A>
void re(complex<A>& c);
template <class A, class B>
void re(pair<A, B>& p);
template <class A>
void re(vector<A>& v);
template <class A, size_t SZ>
void re(array<A, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& d) {
string t;
re(t);
d = stod(t);
}
void re(long double& d) {
string t;
re(t);
d = stold(t);
}
template <class H, class... T>
void re(H& h, T&... t) {
re(h);
re(t...);
}
template <class A>
void re(complex<A>& c) {
A a, b;
re(a, b);
c = {a, b};
}
template <class A, class B>
void re(pair<A, B>& p) {
re(p.first, p.second);
}
template <class A>
void re(vector<A>& x) {
for (auto& a : x) re(a);
}
template <class A, size_t SZ>
void re(array<A, SZ>& x) {
for (auto& a : x) re(a);
}
string to_string(char c) { return string(1, c); }
string to_string(const char* second) { return (string)second; }
string to_string(string second) { return second; }
string to_string(bool b) { return to_string((int)b); }
template <class A>
string to_string(complex<A> c) {
stringstream ss;
ss << c;
return ss.string();
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ>
string to_string(bitset<SZ> b) {
string res = "";
for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
return res;
}
template <class A, class B>
string to_string(pair<A, B> p);
template <class T>
string to_string(T v) {
bool fst = 1;
string res = "";
for (const auto& x : v) {
if (!fst) res += " ";
fst = 0;
res += to_string(x);
}
return res;
}
template <class A, class B>
string to_string(pair<A, B> p) {
return to_string(p.first) + " " + to_string(p.second);
}
template <class A>
void pr(A x) {
cout << to_string(x);
}
template <class H, class... T>
void pr(const H& h, const T&... t) {
pr(h);
pr(t...);
}
void ps() { pr("\n"); }
template <class H, class... T>
void ps(const H& h, const T&... t) {
pr(h);
if (sizeof...(t)) pr(" ");
ps(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
cerr << to_string(h);
if (sizeof...(t)) cerr << ", ";
DBG(t...);
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long max(long long a, long long b, long long c) {
return max(a, max(b, c));
}
int min(int a, int b, int c) { return min(a, min(b, c)); }
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setIO(string second = "") {
unsyncIO();
if ((int)(second).size()) {
setIn(second + ".in"), setOut(second + ".out");
}
}
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
void solve() {
long long n, m;
re(n, m);
long long a[m], b[m];
vector<int> v;
for (int i = (0); i < (m); ++i) {
re(a[i], b[i]);
v.push_back(a[i]);
}
sort(begin(v), end(v));
long long mx = 0;
long long pre[m + 1];
pre[0] = 0;
for (int i = (0); i < (m); ++i) pre[i + 1] = pre[i] + v[i];
for (int i = (0); i < (m); ++i) {
long long cntleft = n - 1;
long long ret = a[i];
if (a[i] >= b[i]) {
ret = 0;
cntleft = n;
}
auto it = lower_bound(v.begin(), v.end(), b[i]);
ret += pre[m] - pre[max(m - cntleft, (long long)distance(v.begin(), it))];
cntleft -= distance(it, v.end());
if (cntleft < 0) cntleft = 0;
ret += cntleft * b[i];
ckmax(mx, ret);
}
ps(mx);
}
vector<int> a, b, both;
int main() {
setIO();
int n, k;
re(n, k);
for (int i = (0); i < (n); ++i) {
int x, y, z;
re(x, y, z);
if (y && z)
both.push_back(x);
else if (y)
a.push_back(x);
else if (z)
b.push_back(x);
}
sort(begin(a), end(a));
sort(begin(b), end(b));
sort(begin(both), end(both));
if (min((int)(a).size() + (int)(both).size(),
(int)(b).size() + (int)(both).size()) < k) {
ps(-1);
return 0;
}
int mn = MOD;
int sum = 0;
int p = 0;
int p2 = 0;
while (p < (int)(both).size() && p < k) {
sum += both[p];
p++;
}
while (p + p2 < k) {
sum += a[p2];
sum += b[p2];
p2++;
}
ckmin(mn, sum);
while (p >= 0 && p2 < min((int)(a).size(), (int)(b).size())) {
sum += a[p2];
sum += b[p2];
p2++;
p--;
sum -= both[p];
ckmin(mn, sum);
}
assert(mn != MOD);
ps(mn);
}
|
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 readingBooks {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int N = Integer.parseInt(st.nextToken()), K = Integer.parseInt(st.nextToken()), totalTime = 0;
int[][] books = new int[N][5];
int[] alice = new int[N];
int[] bob = new int[N];
for(int i = 0; i < N; i++){
st = new StringTokenizer(in.readLine());
books[i][0] = Integer.parseInt(st.nextToken());
books[i][1] = Integer.parseInt(st.nextToken());
books[i][2] = Integer.parseInt(st.nextToken());
}
Arrays.sort(books, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
int amtA = 0, amtB = 0;
for(int[] book : books){
if(book[1] == 1 && amtA < K) {
alice[amtA++] = book[0];
book[3] = 1;
}
if(book[2] == 1 && amtB < K) {
bob[amtB++] = book[0];
book[4] = 1;
}
}
if(amtA < K || amtB < K){
System.out.println(-1);
System.exit(0);
}
while(amtA + amtB < N){
if(amtA < amtB) {
for (int[] book : books) {
if (book[3] == 0) {
alice[amtA++] += book[0];
break;
}
}
}
else {
for(int[] book : books) {
if(book[4] == 0){
bob[amtB++] += book[0];
break;
}
}
}
}
int timeA = 0, timeB = 0;
for(int i : alice) timeA += i;
for(int i : bob) timeB += i;
System.out.println(Math.max(timeA, timeB));
}
}
|
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.math.BigInteger;
import java.util.*;
public class E {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
PriorityQueue<Pair>a = new PriorityQueue<>();
PriorityQueue<Pair>b = new PriorityQueue<>();
PriorityQueue<Pair>both = new PriorityQueue<>();
PriorityQueue<Pair>garb = new PriorityQueue<>();
for(int i=0;i<n;i++){
int t = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
if(x==1 && y==1){
both.add(new Pair(t,i+1));
}else if(x==1 && y!=1){
a.add(new Pair(t,i+1));
}else if(x!=1 && y==1){
b.add(new Pair(t,i+1));
}else{
garb.add(new Pair(t,i+1));
}
}
// System.out.println(a);
// System.out.println(b);
// System.out.println(both);
ArrayList<Integer>result = new ArrayList<>();
int c = 2*k-m;
boolean t = true;
long res = 0;
if(m<k)t = false;
while (!both.isEmpty() && c>0 && t){
if(both.isEmpty()){
t = false;break;}
Pair p = both.poll();
res+=p.a;
result.add(p.b);
c--;
k--;
m--;
}
while (k>0 && t){
int x = 1000000000;
int y = 1000000000;
int z = 1000000000;
if(!a.isEmpty()){
x=a.peek().a;
}
if(!b.isEmpty())y = b.peek().a;
if(!both.isEmpty())z = both.peek().a;
if(z<x+y && !both.isEmpty()){
Pair p = both.poll();
res+=p.a;
result.add(p.b);
m--;
}
else if(!a.isEmpty() && !b.isEmpty() && m>1){
Pair p1 = a.poll();
Pair p2 = b.poll();
res+=p1.a;
res+=p2.a;
result.add(p1.b);
result.add(p2.b);
m-=2;
}else{
t = false;
break;
}
k--;
}
if(m<0)t = false;
while (m>0){
int x = 1000000000;
int y = 1000000000;
int z = 1000000000;
int g = 1000000000;
if(!a.isEmpty()) x=a.peek().a;
if(!b.isEmpty())y = b.peek().a;
if(!both.isEmpty())z = both.peek().a;
if(!garb.isEmpty())g = garb.peek().a;
if(z<=x&& z<=y && z<=g&& !both.isEmpty()){
Pair p = both.poll();
res+=p.a;
result.add(p.b);
m--;
}
else if(x<=y && x<=z && x<=g &&!a.isEmpty()){
Pair p1 = a.poll();
res+=p1.a;
result.add(p1.b);
m--;
}else if(y<=z && y<=x && y<=g&&!b.isEmpty()){
Pair p1 = b.poll();
res+=p1.a;
result.add(p1.b);
m--;
}else if(g<=z && g<=x && g<=y&&!garb.isEmpty()){
Pair p1 = garb.poll();
res+=p1.a;
result.add(p1.b);
m--;
}else{
t = false;
break;
}
}
if(t){
pw.println(res);
for(int i=0;i<result.size();i++)
pw.print(result.get(i)+" ");
pw.println();
}else{
pw.println(-1);
}
pw.flush();
pw.close();
}
static long power(long x, long y, long m) {
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
static class Node{
long a;
long b;
long c;
public Node(long a,long b,long c){
this.a= a;
this.b = b;
this.c = c;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class Pair implements Comparable<Pair>{
int a;
int b;
public Pair(int a,int b){
this.a= a;
this.b = b;
}
public int compareTo(Pair o) {
if(this.a==o.a)return Integer.compare(b,o.b);
return Integer.compare(a,o.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>
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);
}
}
for (int i = 0; i < n; i++) {
if (ak < k && B[i].a && B[i].b) {
ak++;
a.push(i);
tot += B[i].time;
B[i].time = INT_MAX;
if (b.size() && bk == k) {
tot -= B[b.top()].time;
b.pop();
}
if (bk < k) bk++;
} else if (bk < k && B[i].a && B[i].b) {
bk++;
b.push(i);
tot += B[i].time;
B[i].time = INT_MAX;
if (a.size() && ak == k) {
tot -= B[a.top()].time;
a.pop();
}
if (ak < k) ak++;
}
}
for (int i = 0; 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;
int n, k, ans;
multiset<int> alice, bob, both;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (a + b == 2)
both.insert(t);
else if (a)
alice.insert(t);
else if (b)
bob.insert(t);
}
if (alice.size() + both.size() < k || bob.size() + both.size() < k)
return cout << -1, 0;
while (k && !both.empty() && !alice.empty() && !bob.empty()) {
int fromAlice = *alice.begin(), fromBob = *bob.begin(),
fromBoth = *both.begin();
if (fromBoth > fromAlice + fromBob) {
alice.erase(fromAlice);
bob.erase(fromBob);
ans += fromAlice + fromBob;
} else {
both.erase(fromBoth);
ans += fromBoth;
}
k--;
}
while (k && !both.empty()) {
ans += *both.begin();
both.erase(both.begin());
k--;
}
while (k && !alice.empty() && !bob.empty()) {
ans += *alice.begin() + *bob.begin();
alice.erase(alice.begin());
bob.erase(bob.begin());
k--;
}
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
|
python3
|
#!/usr/bin/env python3
import sys
import heapq
input=sys.stdin.readline
n,k=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
cnt1=0
cnt2=0
for t,a,b 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
for i in range(k):
ans+=arr[i][0]
if arr[i][2]==1:
cnt+=1
if cnt==k:
print(ans)
exit()
q1=[]
q2=[]
q3=[]
for t,a,b in arr[k:]:
if a==1 and b==0:
heapq.heappush(q1,-t)
if a==1 and b==1:
heapq.heappush(q2,t)
if a==0 and b==1:
heapq.heappush(q3,t)
while cnt<k:
cost1=10**18
if len(q1)!=0:
cost1=heapq.heappop(q1)
heapq.heappush(q1,cost1)
cost2=10**18
if len(q2)!=0:
cost2=heapq.heappop(q2)
heapq.heappush(q2,cost2)
cost3=10**18
if len(q3)!=0:
cost3=heapq.heappop(q3)
heapq.heappush(q3,cost3)
if cost2+cost1<=cost3:
ans+=cost2+cost1
heapq.heappop(q1)
heapq.heappop(q2)
else:
ans+=cost3
heapq.heappop(q3)
cnt+=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;
const long long mod = 1e9 + 7;
const long long MN = 2e6 + 10;
long long A[MN];
int main() {
srand(time(0));
cin.tie(0);
ios_base::sync_with_stdio(0);
long long N, M, K;
cin >> N >> M >> K;
deque<pair<int, int> > A, B, C, D;
for (int i = 1; i <= N; i++) {
int t, x, y;
cin >> t >> x >> y;
if (x + y == 2) {
C.push_back(pair<int, int>(t, i));
} else if (x == 1) {
A.push_back(pair<int, int>(t, i));
} else if (y == 1) {
B.push_back(pair<int, int>(t, i));
} else {
D.push_back(pair<int, int>(t, i));
}
}
sort(A.begin(), A.end());
A.push_back(pair<int, int>(mod, -1));
sort(B.begin(), B.end());
B.push_back(pair<int, int>(mod, -1));
sort(C.begin(), C.end());
C.push_back(pair<int, int>(mod, -1));
sort(D.begin(), D.end());
D.push_back(pair<int, int>(mod, -1));
if (C.size() + min(A.size(), B.size()) < K) {
cout << -1 << "\n";
return 0;
}
long long sol = 0;
vector<int> vsol;
while (M) {
if (!K) {
deque<pair<int, int> > *ptr = &A;
if (B.front() < ptr->front()) ptr = &B;
if (C.front() < ptr->front()) ptr = &C;
if (D.front() < ptr->front()) ptr = &D;
sol += ptr->front().first;
vsol.push_back(ptr->front().second);
} else if (M < K) {
sol = -1;
break;
} else if (M == K) {
if (C.size() == 0) {
sol = -1;
break;
} else {
sol += C.front().first;
vsol.push_back(C.front().second);
K--;
}
} else {
if (C.size() > 0 && A.size() > 0 && B.size() > 0) {
if (C.front().first < A.front().first + B.front().first) {
sol += C.front().first;
vsol.push_back(C.front().second);
C.pop_front();
K--;
} else {
sol += A.front().first + B.front().first;
vsol.push_back(A.front().second);
vsol.push_back(B.front().second);
A.pop_front();
B.pop_front();
K--;
M--;
}
} else if (C.size() > 0) {
sol += C.front().first;
vsol.push_back(C.front().second);
C.pop_front();
K--;
} else if (A.size() > 0 && B.size() > 0) {
sol += A.front().first + B.front().first;
vsol.push_back(A.front().second);
vsol.push_back(B.front().second);
A.pop_front();
B.pop_front();
K--;
M--;
} else {
sol = -1;
break;
}
}
M--;
}
cout << sol << "\n";
if (sol != -1) {
for (int i = 0; i < (int)vsol.size(); i++) {
if (i) cout << " ";
cout << vsol[i];
}
}
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
|
java
|
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class e1 {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int n = read.nextInt();
int k = read.nextInt();
read.nextLine();
int s = 0;
for(int i = 0; i < n; i++) {
int ti = read.nextInt();
int ai = read.nextInt();
int bi = read.nextInt();
read.nextLine();
if( ai == 1 && bi == 1) {
s += ti;
}
}
System.out.println( 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.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
/**
* @project: Codeforces653
* @author: izhanatuly
* @date: 28/06/2020
*/
public class E1 {
static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
runTestCase();
}
public static void runTestCase() {
int n = scanner.nextInt();
int k = scanner.nextInt();
List<Book> books = new ArrayList<>();
int canA = 0;
int canB = 0;
for (int i = 0; i < n; i++) {
int t = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
if(a == 1) {
canA++;
}
if(b == 1) {
canB++;
}
if(a==1 || b==1) books.add(new Book(t, a ,b));
}
if(canA < k || canB < k) {
System.out.println(-1);
return;
}
long sum = 0;
PriorityQueue<Book> both = new PriorityQueue<>((a,b) -> Integer.compare(b.time, a.time));
PriorityQueue<Book> onlyA = new PriorityQueue<>((a,b) -> Integer.compare(b.time, a.time));
PriorityQueue<Book> onlyB = new PriorityQueue<>((a,b) -> Integer.compare(b.time, a.time));
for(Book book : books) {
sum += book.time;
if(book.a == 1 && book.b == 1) {
both.add(book);
} else if(book.a == 1) {
onlyA.add(book);
} else {
onlyB.add(book);
}
}
while(canA >= k && canB >= k) {
Book fromOnlyA = onlyA.peek();
Book fromOnlyB = onlyB.peek();
Book fromBoth = both.peek();
if(canA > k && canB > k) {
long justA = (fromOnlyA != null ? fromOnlyA.time : 0);
long justB = (fromOnlyB != null ? fromOnlyB.time : 0);
long ab = justA + justB;
long bothTime = (fromBoth != null ? fromBoth.time : 0);
if(justA >= bothTime && !onlyA.isEmpty()) {
canA--;
Book book = onlyA.poll();
sum -= book.time;
} else if(justB >= bothTime && !onlyB.isEmpty()){
canB--;
Book book = onlyB.poll();
sum -= book.time;
} else if(ab > bothTime) {
if(!onlyA.isEmpty()) {
Book book1 = onlyA.poll();
sum -= book1.time;
canA--;
}
if(!onlyB.isEmpty()) {
Book book2 = onlyB.poll();
sum -= book2.time;
canB--;
}
} else {
canA--;
canB--;
Book book = both.poll();
sum -= book.time;
}
} else if(canA > k) {
canA--;
Book book = onlyA.poll();
sum -= book.time;
} else if(canB > k) {
canB--;
Book book = onlyB.poll();
sum -= book.time;
} else if(canA == k && canB == k) {
break;
}
}
System.out.println(sum);
}
}
class Book {
int time;
int a;
int b;
Book(int time, int a, int b) {
this.time = time;
this.a = a;
this.b = 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
|
python3
|
import sys
from collections import deque
reader = (tuple(map(int, line.split())) for line in sys.stdin)
both, alice, bob = [], [], []
n, k = next(reader)
for _ in range(n):
value, alice_fl, bob_fl = next(reader)
if alice_fl and bob_fl:
both.append(value)
if alice_fl and not bob_fl:
alice.append(value)
if not alice_fl and bob_fl:
bob.append(value)
both.sort()
both = deque(both)
alice.sort()
alice = deque(alice)
bob.sort()
bob = deque(bob)
cnt_alice, cnt_bob = 0, 0
result = 0
while cnt_alice < k and cnt_bob < k:
if len(both) > 0 and len(alice) > 0 and len(bob) > 0:
if both[0] < alice[0] + bob[0]:
result += alice.popleft() + bob.popleft()
else:
result += both.popleft()
cnt_alice += 1
cnt_bob += 1
else:
if len(alice) == 0 or len(bob) == 0:
if len(both) > 0:
result += both.popleft()
cnt_alice += 1
cnt_bob += 1
else:
result = -1
break
else: # alice != 0 and bob != 0 => both = 0
result += alice.popleft()
result += bob.popleft()
cnt_alice += 1
cnt_bob += 1
print(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
|
python3
|
line = input()
n, m, k = [int(i) for i in line.split(' ')]
books, allL, aliceL, bobL, other =list(range(1, n + 1)), [], [], [], []
ts = [[] for _ in range(n + 1)]
for i in range(n):
line = input()
t, a, b = [int(j) for j in line.split(' ')]
ts[i + 1] = [t, a, b]
if a == 1 and b == 1:
allL.append(i + 1)
if a == 1:
aliceL.append(i + 1)
if b == 1:
bobL.append(i + 1)
if min(len(aliceL), len(bobL)) < k or (len(allL) < k and 2 * (k - len(allL)) > m - len(allL)) :
print(-1)
exit()
books.sort(key=lambda x: ts[x][0])
allL.sort(key=lambda x: ts[x][0])
aliceL.sort(key=lambda x: ts[x][0])
bobL.sort(key=lambda x: ts[x][0])
aset = set(aliceL[:k])
bset = set(bobL[:k])
cset = aset | bset
if len(cset) < m:
for i in books:
if i not in cset:
cset.add(i)
if len(cset) == m:
break
elif len(cset) > m:
la, lb = k - 1, k - 1
for i in allL:
if i not in cset and ts[i][1] + ts[i][2] == 2:
cset.add(i)
while la >= 0:
if ts[aliceL[la]][2] == 0:
break
la -= 1
while lb >= 0:
if ts[bobL[lb]][1] == 0:
break
lb -= 1
cset.remove(aliceL[la])
cset.remove(bobL[lb])
if (n, m, k) == (6561, 810, 468):
print(la, lb, len(cset))
la -= 1
lb -= 1
if len(cset) == m:
break
if len(cset) != m:
print(-1)
exit()
print (sum(ts[i][0] for i in cset))
for i in cset:
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void printVector(vector<T> const &vec);
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int> > books(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
int inputs;
cin >> inputs;
books[i].push_back(inputs);
}
}
vector<pair<int, int> > a, b, c, d;
for (int i = 0; i < n; i++) {
if (books[i][1] == 1) {
if (books[i][2] == 1) {
c.push_back(make_pair(books[i][0], i + 1));
} else {
a.push_back(make_pair(books[i][0], i + 1));
}
} else if (books[i][2] == 1) {
b.push_back(make_pair(books[i][0], i + 1));
} else {
d.push_back(make_pair(books[i][0], i + 1));
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
sort(d.begin(), d.end());
if (int(a.size() + c.size()) < k || int(b.size() + c.size()) < k ||
int(a.size() + b.size() + c.size()) < m || int(c.size()) < 2 * k - m) {
cout << -1 << endl;
return 0;
}
int minT(0), ka(0), kb(0), kc(0), kd(0);
vector<int> index;
while (int(index.size()) < m) {
if (ka == a.size()) a.push_back(make_pair(987654321, 0));
if (kb == b.size()) b.push_back(make_pair(987654321, 0));
if (kc == c.size()) c.push_back(make_pair(987654321, 0));
if (kd == d.size()) d.push_back(make_pair(987654321, 0));
if (ka + kc < k && kb + kc < k) {
if (c[kc].first > a[ka].first + b[kb].first &&
int(index.size()) + 2 <= m) {
minT += a[ka].first;
minT += b[kb].first;
index.push_back(a[ka].second);
index.push_back(b[kb].second);
ka++;
kb++;
} else {
minT += c[kc].first;
index.push_back(c[kc].second);
kc++;
}
} else {
if (a[ka].first <= b[kb].first && a[ka].first <= c[kc].first &&
a[ka].first <= d[kd].first) {
minT += a[ka].first;
index.push_back(a[ka].second);
ka++;
} else if (b[kb].first <= a[ka].first && b[kb].first <= c[kc].first &&
b[kb].first <= d[kd].first) {
minT += b[kb].first;
index.push_back(b[kb].second);
kb++;
} else if (c[kc].first <= a[ka].first && c[kc].first <= b[kb].first &&
c[kc].first <= d[kd].first) {
minT += c[kc].first;
index.push_back(c[kc].second);
kc++;
} else if (d[kd].first <= a[ka].first && d[kd].first <= b[kb].first &&
d[kd].first <= c[kc].first) {
minT += d[kd].first;
index.push_back(d[kd].second);
kd++;
}
}
}
for (int i = 0; i < m; i++) {
if (index[i] == 0) {
cout << -1 << endl;
return 0;
}
}
cout << minT << endl;
printVector(index);
return 0;
}
template <class T>
void printVector(vector<T> const &vec) {
for (T val : vec) {
cout << val << " ";
}
cout << 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
|
python3
|
n,k = map(int,input().split())
ali,bob,both=[],[],[]
bothLen,aliLen,bobLen=0,0,0
for i in range(n):
r,ai,bi = map(int,input().split())
if ai and bi:
both.append(r)
bothLen+=1
if ai and not bi:
ali.append(r)
aliLen+=1
if bi and not ai:
bob.append(r)
bobLen+=1
both.sort(reverse=True)
ali.sort(reverse=True)
bob.sort(reverse=True)
if len(both)+min(aliLen,bobLen)<k:
print(-1)
else:
bobs,alice,s = 0,0,0
for i in range(k):
if both!=[] and alice<k and bobs<k:
alice+=1
bobs+=1
if ali!=[] and bob!=[]:
if both[-1]<ali[-1]+bob[-1]:
s+=both.pop()
else:
s+=ali.pop()+bob.pop()
elif ali==[] or bob==[]:
s+=both.pop()
elif both!=[] and alice<k:
alice+=1
if both[-1]<ali[-1]:
s+=both.pop()
else:
s+=ali.pop()
elif both!=[] and bobs<k:
bobs+=1
if both[-1]<bob[-1]:
s+=both.pop()
else:
s+=bob.pop()
s+=sum(ali[:k-alice])+sum(bob[:k-bobs])
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
|
def func(n, k):
common = []
bob = []
alice = []
for i in range(n):
t, a, b = map(int, input().split())
if a and b:
common.append(t)
elif a:
alice.append(t)
else:
bob.append(t)
if len(common)+len(alice) < k or len(common)+len(bob) < k:
print(-1)
return
common.sort()
alice.sort()
bob.sort()
c_num = min(k, len(common))
num = k-c_num
while c_num != 0 and num < len(alice) and num < len(bob) and common[c_num-1] > alice[num]+bob[num]:
c_num -= 1
num += 1
print(sum(common[:c_num])+sum(alice[:num])+sum(bob[:num]))
# cases = int(input())
for i in range(1):
n, k = map(int, input().split())
func(n, k)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
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;
/*if(n==200000 &&k==70874)
{
out.println("FUCK");
out.println(res);
}*/
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));
}
else break;
}
out.println(res);
out.close();
return;
}
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
|
python3
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 21:16:20 2020
@author: adarsh
"""
n,k=map(int,input().split())
aa=[]
bb=[]
both=[]
non=[]
for i in range(n):
t,a,b=map(int,input().split())
if (a==1 and b==0):
aa.append(t)
elif (a==0 and b==1):
bb.append(t)
elif (a==1 and b==1):
both.append(t)
else:
non.append(t)
aa.sort()
bb.sort()
both.sort()
if (len(aa)+len(both)<k or len(bb)+len(both)<k):
print(-1)
else:
ans=0
i=0
j=0
ka=k
kb=k
mn=min(min(len(aa),len(bb)),len(both))
for ll in range(mn):
if both[j]<=aa[i]+bb[i]:
ans+=both[j]
ka-=1
kb-=1
j+=1
else:
ans+=aa[i]
ans+=bb[i]
ka-=1
kb-=1
i+=1
if (ka>0):
for ll in range(j,len(both)):
if (ka==0):
break
ans+=both[ll]
j+=1
ka-=1
kb-=1
for ll in range(i,len(aa)):
if (ka==0):
break
ans+=aa[ll]
ka-=1
if kb>0:
for ll in range(j,len(both)):
if (kb==0):
break
ans+=both[ll]
ka-=1
kb-=1
j+=1
for ll in range(i,len(bb)):
if (kb==0):
break
ans+=bb[ll]
ka-=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
|
python3
|
from sys import stdin, stdout
import math
from collections import defaultdict, deque
n, m, k = map(int, stdin.readline().split())
al, bl, both, hates = [], [], [], []
for i in range(n):
t, a, b = map(int, stdin.readline().split())
if a == 0 and b == 0:
hates.append((t, i+1))
elif a == 1 and b == 1:
both.append((t, i+1))
elif a == 1:
al.append((t, i+1))
elif b == 1:
bl.append((t, i+1))
al, bl, both, hates = deque(sorted(al)[:m]), deque(sorted(bl)[:m]), deque(sorted(both)[:m]), deque(sorted(hates)[:m])
mrem, krem = m, k
res, resl = 0, []
while mrem > 0 and krem > 0:
if mrem == krem:
if not both:
res = -1
break
t, i = both.popleft()
res += t
resl.append(i)
mrem -= 1
else:
if al and bl:
if both:
tmp = float('inf')
if hates: tmp = hates[0][0]
if al[0][0]+bl[0][0] < both[0][0] + tmp:
res += al[0][0]+bl[0][0]
resl.append(al[0][1])
resl.append(bl[0][1])
al.popleft()
bl.popleft()
mrem -= 2
else:
t, i = both.popleft()
res += t
resl.append(i)
t, i = hates.popleft()
res += t
resl.append(i)
mrem -= 2
else:
res += al[0][0]+bl[0][0]
resl.append(al[0][1])
resl.append(bl[0][1])
al.popleft()
bl.popleft()
mrem -= 2
else:
if not both:
res = -1
break
t, i = both.popleft()
res += t
resl.append(i)
mrem -= 1
krem -= 1
if krem > 0:
print(-1)
else:
agg = sorted(al+bl+both+hates)
for t, i in agg[:mrem]:
res += t
resl.append(i)
print(res)
print(" ".join(map(str, resl)))
|
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
zz = 0
while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] > Both[0][0] + min(Alice1[-1][0],Bob1[-1][0],none[zz][0]):
if min(Alice1[-1][0],Bob1[-1][0],none[zz][0]) == none[zz][0]:
zz += 1
Alice.append(Alice1[-1])
Bob.append(Bob1[-1])
corr.append(Both[0])
Alice1.pop(-1)
Bob1.pop(-1)
Both.pop(0)
q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1)
q = m - q
All = Alice + Bob + Both + none
All.sort(key=lambda x: x[0])
result2 = tresult + tresult1 + corr + Alice1 + Bob1
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
sum1 = 0
for row in result:
sum1 = sum1 + row[0]
print(sum1)
if sum1 == 0:
print(sum(row[1] for row in result2))
print(sum(row[2] for row in result2))
result.sort(key=lambda x: x[0])
print(result[-1])
print(result[-2])
chk = result[-1][0] - 1
for row in All:
if row[0] == chk:
print(row)
if sum1 == 82207:
print(len(corr))
print(corr[-1])
corr.sort(key = lambda x: x[0])
print(corr[-1])
Both.sort(key=lambda x: x[0])
print(Both[0])
print(All[q])
if sum1 == 82207:
print(all[15429])
print(all[11655])
result.sort(key=lambda x: x[3])
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.Collection.*;
public class Solution{
static int min(int a,int b, int c){
if(a>b && a>c) return a;
if(b>c) return b;
return c;
}
public static void main(String[] args){
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
ArrayList <Integer> a=new ArrayList<Integer>();
ArrayList <Integer> b=new ArrayList<Integer>();
ArrayList <Integer> hs=new ArrayList<Integer>();
for(int i=0;i<n;i++){
int p=sc.nextInt();
int q=sc.nextInt();
int r=sc.nextInt();
if(q==1 && r==1) hs.add(p);
else if(q==1) a.add(p);
else if(r==1) b.add(p);
}
if(a.size()+hs.size()<k || b.size()+hs.size()<k) System.out.println(-1);
else{
Collections.sort(hs);
Collections.sort(a);
Collections.sort(b);
//System.out.println(a+""+b+""+hs);
int sum=0,i=0,p=0,j=0;
while(i<a.size()&& i<b.size() && p<hs.size()&& j<k){
if(a.get(i)+b.get(i)<hs.get(p)){
//System.out.println("from if"+a.get(i)+" "+b.get(i));
sum+=a.get(i)+b.get(i);
i++;
}
else{
//System.out.println("from if "+hs.get(i));
sum+=hs.get(p);p++;
}
}
while(j<k && i<a.size() && i<b.size()){
//System.out.println(a.get(i)+" "+b.get(i));
j++;
sum+=a.get(i)+b.get(i);i++;
}
while(j<k && p<hs.size()){
j++;
sum+=hs.get(p);
p++;
}
System.out.println(sum);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author xwchen
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskF solver = new TaskF();
solver.solve(1, in, out);
out.close();
}
static class TaskF {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
Vector<Book> books = new Vector<>();
Vector<Book> alice = new Vector<>();
Vector<Book> bob = new Vector<>();
Vector<Book> nolikes = new Vector<>();
Vector<Book> rawBooks = new Vector<>();
for (int i = 0; i < n; ++i) {
int t = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
if (a == 0 && b == 1) {
bob.add(new Book(t, a, b, i));
} else if (a == 1 && b == 0) {
alice.add(new Book(t, a, b, i));
} else if (a == 1 && b == 1) {
books.add(new Book(t, a, b, i));
} else {
nolikes.add(new Book(t, a, b, i));
}
rawBooks.add(new Book(t, a, b, i));
}
alice.sort(Comparator.comparingInt(o -> o.t));
bob.sort(Comparator.comparingInt(o -> o.t));
if (2 * k <= m) {
for (int i = 0; i < Math.min(alice.size(), bob.size()); ++i) {
int t = alice.get(i).t + bob.get(i).t;
books.add(new Book(t, 1, 1, alice.get(i).rawIndex, bob.get(i).rawIndex));
}
if (alice.size() > bob.size()) {
for (int i = bob.size(); i < alice.size(); ++i) nolikes.add(alice.get(i));
} else if (alice.size() < bob.size()) {
for (int i = alice.size(); i < bob.size(); ++i) nolikes.add(bob.get(i));
}
books.sort(Comparator.comparingInt(o -> o.t));
if (books.size() < k) {
out.println(-1);
} else {
long sum = 0;
Vector<Integer> res = new Vector<>();
for (int i = 0; i < k; ++i) {
if (books.get(i).rawIndex != -1) {
res.add(books.get(i).rawIndex);
rawBooks.get(books.get(i).rawIndex).used = true;
} else {
res.add(books.get(i).rawIndexA);
res.add(books.get(i).rawIndexB);
rawBooks.get(books.get(i).rawIndexA).used = true;
rawBooks.get(books.get(i).rawIndexB).used = true;
}
books.get(i).used = true;
sum += books.get(i).t;
}
rawBooks.sort(Comparator.comparingInt(o -> o.t));
int j = 0;
while (res.size() < m) {
while (j < rawBooks.size() && rawBooks.get(j).used) ++j;
res.add(rawBooks.get(j).rawIndex);
sum += rawBooks.get(j).t;
++j;
}
out.println(sum);
for (int x : res) {
out.print((x + 1) + " ");
}
out.println();
}
} else {
int sharedSize = 2 * k - m;
int independentSize = m - sharedSize;
int singleSize = k - sharedSize;
books.sort(Comparator.comparingInt(o -> o.t));
if (books.size() < sharedSize) {
out.println(-1);
} else {
Vector<Integer> res = new Vector<>();
long sum = 0;
for (int i = 0; i < sharedSize; ++i) {
books.get(i).used = true;
rawBooks.get(books.get(i).rawIndex).used = true;
res.add(books.get(i).rawIndex);
sum += books.get(i).t;
}
for (int i = 0; i < Math.min(alice.size(), bob.size()); ++i) {
int t = alice.get(i).t + bob.get(i).t;
books.add(new Book(t, 1, 1, alice.get(i).rawIndex, bob.get(i).rawIndex));
}
if (alice.size() > bob.size()) {
for (int i = bob.size(); i < alice.size(); ++i) nolikes.add(alice.get(i));
} else if (alice.size() < bob.size()) {
for (int i = alice.size(); i < bob.size(); ++i) nolikes.add(bob.get(i));
}
if (books.size() < k) {
out.println(-1);
} else {
books.sort(Comparator.comparingInt(o -> o.t));
for (int i = 0; i < books.size(); ++i) {
if (books.get(i).used) continue;
if (singleSize <= 0) break;
--singleSize;
if (books.get(i).rawIndex != -1) {
res.add(books.get(i).rawIndex);
rawBooks.get(books.get(i).rawIndex).used = true;
} else {
res.add(books.get(i).rawIndexA);
res.add(books.get(i).rawIndexB);
rawBooks.get(books.get(i).rawIndexA).used = true;
rawBooks.get(books.get(i).rawIndexB).used = true;
}
books.get(i).used = true;
sum += books.get(i).t;
}
rawBooks.sort(Comparator.comparingInt(o -> o.t));
int j = 0;
while (res.size() < m) {
while (j < rawBooks.size() && rawBooks.get(j).used) ++j;
res.add(rawBooks.get(j).rawIndex);
sum += rawBooks.get(j).t;
++j;
}
out.println(sum);
for (int x : res) {
out.print((x + 1) + " ");
}
out.println();
}
}
}
}
class Book {
int t;
int a;
int b;
int rawIndex = -1;
int rawIndexA = -1;
int rawIndexB = -1;
boolean used = false;
public Book(int t, int a, int b, int rawIndex) {
this.t = t;
this.a = a;
this.b = b;
this.rawIndex = rawIndex;
}
public Book(int t, int a, int b, int rawIndexA, int rawIndexB) {
this.t = t;
this.a = a;
this.b = b;
this.rawIndexA = rawIndexA;
this.rawIndexB = rawIndexB;
}
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public InputReader(InputStream inputStream) {
this.reader = new BufferedReader(
new InputStreamReader(inputStream));
}
public String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(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.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class EasyReading {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// int cases = scanner.nextInt();
// for (int i = 0; i < cases; i++) {
solve();
// }
}
private static void solve() {
int n = scanner.nextInt();
int k = scanner.nextInt();
int[] all = new int[n];
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
List<Integer> both = new ArrayList<>();
for (int i = 0; i < n; i++) {
all[i] = scanner.nextInt();
int isA = scanner.nextInt();
int isB = scanner.nextInt();
if (isA == 1 && isB == 1) {
both.add(i);
} else {
if (isA == 1) {
a.add(i);
}
if (isB == 1) {
b.add(i);
}
}
}
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return all[01] - all[02];
}
};
a.sort(comparator);
b.sort(comparator);
both.sort(comparator);
int i = 0;
int j = 0;
long time = 0;
while (i + j < k && (i < a.size() && i < b.size() || j < both.size())) {
if (i < a.size() && i < b.size()) {
long tmp = all[a.get(i)] + all[b.get(i)];
if (j < both.size() && tmp > all[both.get(j)]) {
time += all[both.get(j)];
j++;
} else {
time += tmp;
i++;
}
} else {
time += all[both.get(j)];
j++;
}
}
if (i + j == k)
System.out.println(time);
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
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class E1374_1 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
solve();
}
public static void solve() throws IOException {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken()),k = Integer.parseInt(st.nextToken());
LinkedList<Integer>both = new LinkedList<Integer>();
LinkedList<Integer>a = new LinkedList<Integer>();
LinkedList<Integer>b = new LinkedList<Integer>();
for(int i = 0;i<n;i++) {
st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken()),aa = Integer.parseInt(st.nextToken()),bb = Integer.parseInt(st.nextToken());
if(aa==1&&bb==1)both.add(t);
else if(aa==0&&bb==1)b.add(t);
else if(bb==0&&aa==1)a.add(t);
}
Collections.sort(both);
Collections.sort(a);
Collections.sort(b);
int cnt = 0;
int min = Integer.MAX_VALUE;
boolean change = false;
for(int i = 0;i<both.size();i++) {
int cur = 0;
cnt += both.get(i);
cur+=cnt;
int left = k-i-1;
System.out.println(left+" "+i);
if(a.size()<left)continue;
if(b.size()<left)continue;
for(int j = 0;j<left;j++) {
cur+=a.get(j);
}
for(int j = 0;j<left;j++) {
cur+=b.get(j);
}
min = Math.min(min, cur);
change = true;
}
if(change)
System.out.println(min);
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
|
java
|
import java.util.*;
public class Question5 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
int k = sc.nextInt();
int[][] arr = new int[n][3];
ArrayList<Integer> list[] = new ArrayList[4];
for(int i = 0;i < 4;i++)list[i] = new ArrayList<Integer>();
int sumb = 0, sumc = 0;
for(int i = 0;i < n;i++) {
int t = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
sumb += b;
sumc += c;
list[2 * b + c].add(t);
}
if(sumb < k || sumc < k) {
System.out.println(-1);
return;
}
Collections.sort(list[1]);
Collections.sort(list[2]);
Collections.sort(list[3]);
long ans = 0;
sumb = 0;
sumc = 0;
ArrayList<Integer> ansList = new ArrayList<Integer>();
int ini = Math.min(k, list[3].size());
for(int i = 0;i < ini;i++) {
int x = list[3].get(i);
ans += x;
ansList.add(x);
sumb += 1;
sumc += 1;
}
Collections.sort(ansList, Collections.reverseOrder());
System.out.println(ansList);
int i = 0;
while(sumb < k) {
ansList.add(list[1].get(0));
ansList.add(list[2].get(0));
ans += list[1].get(i) + list[2].get(i);
list[1].remove(0);
list[2].remove(0);
sumb++;
i++;
}
System.out.println(ansList);
int l = 0;
while(!list[1].isEmpty() && !list[2].isEmpty() && l < ini) {
if(list[1].get(0) + list[2].get(0) < ansList.get(0)) {
ans -= ansList.get(0);
ansList.remove(0);
ans += list[1].get(0);
ans += list[1].get(0);
ansList.add(list[1].get(0));
ansList.add(list[2].get(0));
list[1].remove(0);
list[2].remove(0);
l++;
}
else break;
if(list[1].isEmpty() || list[2].isEmpty() || l >= ini) {
break;
}
}
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
|
java
|
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;
public class E {
//Solution by Sathvik Kuthuru
public static void main(String[] args) {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
int t = 1;
for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out);
out.close();
}
static class Task {
public void solve(int testNumber, FastReader scan, PrintWriter out) {
int n = scan.nextInt(), m = scan.nextInt(), k = scan.nextInt();
ArrayList<Item> both1 = new ArrayList<>(), first = new ArrayList<>(), second = new ArrayList<>();
ArrayList<Item> both2 = new ArrayList<>();
ArrayList<Item> left = new ArrayList<>();
for(int i = 0; i < n; i++) {
int t = scan.nextInt(), a = scan.nextInt(), b = scan.nextInt();
Item curr = new Item(t);
curr.index.add(i + 1);
if(a == 1 && b == 1) both1.add(curr);
else if(a == 1) first.add(curr);
else if(b == 1) second.add(curr);
else left.add(curr);
}
int min = Math.min(first.size(), second.size());
Collections.sort(first);
Collections.sort(second);
for(int i = 0; i < min; i++) {
Item now = new Item(first.get(i).val + second.get(i).val);
now.index.addAll(first.get(i).index);
now.index.addAll(second.get(i).index);
both2.add(now);
}
for(int i = min + 1; i < first.size(); i++) left.add(first.get(i));
for(int i = min + 1; i < second.size(); i++) left.add(second.get(i));
Collections.sort(both1);
Collections.sort(both2);
Collections.sort(left);
long ans = 0, liked = 0, have = 0;
ArrayList<Item> res = new ArrayList<>();
int firstIndex = 0, secondIndex = 0;
while(true) {
if(liked == k || have == m) break;
if(firstIndex == both1.size() && secondIndex == both2.size()) break;
if(m - have == 1) {
if (firstIndex == both1.size()) {
break;
}
else {
have++;
liked++;
ans += both1.get(firstIndex).val;
res.add(both1.get(firstIndex));
firstIndex++;
}
}
else {
if (firstIndex == both1.size()) {
have += 2;
liked++;
ans += both2.get(secondIndex).val;
res.add(both2.get(secondIndex));
secondIndex++;
}
else if (secondIndex == both2.size()) {
have++;
liked++;
ans += both1.get(firstIndex).val;
res.add(both1.get(firstIndex));
firstIndex++;
}
else {
int firstVal = both1.get(firstIndex).val;
int secondVal = both2.get(secondIndex).val;
if(firstVal <= secondVal) {
have++;
liked++;
ans += both1.get(firstIndex).val;
res.add(both1.get(firstIndex));
firstIndex++;
}
else {
have += 2;
liked++;
ans += both2.get(secondIndex).val;
res.add(both2.get(secondIndex));
secondIndex++;
}
}
}
}
if(have < m && liked < k) {
out.println(-1);
return;
}
if(liked < k) {
int canChange = Math.min(both1.size() - firstIndex, secondIndex);
if(canChange < k - liked) {
out.println(-1);
return;
}
for(int i = 0; i < k - liked; i++) {
int currFirst = firstIndex + i;
int currSecond = secondIndex - i - 1;
both2.get(currSecond).used = false;
res.add(both1.get(currFirst));
ans -= both2.get(currSecond).val;
ans += both1.get(currFirst).val;
}
}
else if(have < m) {
if(have + left.size() < m) {
out.println(-1);
return;
}
for(int i = 0; i < m - have; i++) {
ans += left.get(i).val;
res.add(left.get(i));
}
}
out.println(ans);
for(Item x : res) {
if(x.used) {
for(int j : x.index) out.printf("%d ", j);
}
}
}
static class Item implements Comparable<Item> {
int val;
ArrayList<Integer> index;
boolean used;
public Item(int a) {
val = a;
index = new ArrayList<>();
used = true;
}
@Override
public int compareTo(Item item) {
return Integer.compare(val, item.val);
}
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(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 FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.util.ArrayList;
import java.util.Vector;
import java.util.ArrayDeque;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in Actual solution is at the top
*
* @author dauom
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
E2ReadingBooksHardVersion solver = new E2ReadingBooksHardVersion();
solver.solve(1, in, out);
out.close();
}
static class E2ReadingBooksHardVersion {
public final void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
ArrayDeque<int[]> alice = new ArrayDeque<>();
ArrayDeque<int[]> bob = new ArrayDeque<>();
ArrayDeque<int[]> both = new ArrayDeque<>();
ArrayDeque<int[]> none = new ArrayDeque<>();
Stack<Integer> ansAlice = new Stack<>();
Stack<Integer> ansBob = new Stack<>();
Stack<Integer> ansBoth = new Stack<>();
Stack<Integer> ansNone = new Stack<>();
int[] t = new int[n];
for (int i = 0; i < n; i++) {
t[i] = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
if (a == 1 && b == 1) {
both.add(new int[] {t[i], i});
} else if (a == 1) {
alice.add(new int[] {t[i], i});
} else if (b == 1) {
bob.add(new int[] {t[i], i});
} else {
none.add(new int[] {t[i], i});
}
}
sort(alice);
sort(bob);
sort(both);
sort(none);
int aliceCount = 0, bobCount = 0;
while (aliceCount < k
&& bobCount < k
&& !(alice.isEmpty() && bob.isEmpty() && both.isEmpty())) {
if (alice.isEmpty()) {
if (both.isEmpty()) {
break;
}
ansBoth.add(both.poll()[1]);
} else if (bob.isEmpty()) {
if (both.isEmpty()) {
break;
}
ansBoth.add(both.poll()[1]);
} else if (both.isEmpty()) {
ansBob.add(bob.poll()[1]);
ansAlice.add(alice.poll()[1]);
} else {
if (alice.peek()[0] + bob.peek()[0] < both.peek()[0]) {
ansBob.add(bob.poll()[1]);
ansAlice.add(alice.poll()[1]);
} else {
ansBoth.add(both.poll()[1]);
}
}
aliceCount++;
bobCount++;
}
while (aliceCount < k && !alice.isEmpty()) {
ansAlice.add(alice.poll()[1]);
++aliceCount;
}
while (bobCount < k && !bob.isEmpty()) {
ansBob.add(bob.poll()[1]);
++bobCount;
}
if (aliceCount < k || bobCount < k) {
out.println(-1);
return;
}
while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m
&& !both.isEmpty()
&& !ansAlice.isEmpty()
&& !ansBob.isEmpty()) {
int ibob = ansBob.pop();
int ialice = ansAlice.pop();
bob.addFirst(new int[] {t[ibob], ibob});
alice.addFirst(new int[] {t[ialice], ialice});
ansBoth.add(both.poll()[1]);
}
if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m) {
out.println(-1);
return;
}
while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) {
if (none.isEmpty()) {
if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) {
ansBob.add(bob.poll()[1]);
ansAlice.add(alice.poll()[1]);
ansBoth.pop();
} else {
break;
}
} else {
if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) {
int idxBoth = ansBoth.peek();
int doubleCost = bob.peek()[0] + alice.peek()[0] - t[idxBoth];
int noneCost = none.peek()[0];
if (doubleCost < noneCost) {
ansBob.add(bob.poll()[1]);
ansAlice.add(alice.poll()[1]);
ansBoth.pop();
} else {
ansNone.add(none.poll()[1]);
}
} else {
ansNone.add(none.poll()[1]);
}
}
}
if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) {
out.println(-1);
return;
}
ArrayList<Integer> ansList = new ArrayList<>();
ansList.addAll(ansBob);
ansList.addAll(ansAlice);
ansList.addAll(ansBoth);
ansList.addAll(ansNone);
if (ansList.size() != m) {
throw new RuntimeException();
}
int ans = 0;
for (int x : ansList) {
ans += t[x];
}
out.println(ans);
for (int x : ansList) {
out.print((x + 1) + " ");
}
out.println();
}
private void sort(ArrayDeque<int[]> q) {
ArrayList<int[]> a = new ArrayList<>(q);
a.sort(Comparators.singletonIntArr);
q.clear();
q.addAll(a);
}
}
static final class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
public InputReader() {
this.stream = System.in;
}
public InputReader(final InputStream stream) {
this.stream = stream;
}
private final int read() {
if (this.numChars == -1) {
throw new UnknownError();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException ex) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public final int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {}
byte sgn = 1;
if (c == 45) { // 45 == '-'
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9'
res *= 10;
res += c - 48; // 48 == '0'
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
private static final boolean isSpaceChar(final int c) {
return c == 32 || c == 10 || c == 13 || c == 9
|| c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t'
}
}
static final class Comparators {
public static final Comparator<int[]> singletonIntArr = (x, y) -> compare(x[0], y[0]);
private static final int compare(final int x, final int y) {
return x < y ? -1 : (x == y ? 0 : 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;
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;
int used[1000000] = {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;
used[i] = 1;
} 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;
used[i] = 1;
if (ak < k) ak++;
}
}
for (int i = 0; i < n; i++) {
if (a.size() == 0 || b.size() == 0) break;
if (used[i]) continue;
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
|
arr=[int(x) for x in input().split()]
size=arr[0]
total=arr[1]
book_dict={'10':[],'01':[]}
for i in range(size):
book_arr=[int(x) for x in input().split()]
if book_arr[1]==1 and book_arr[2]==1:
book_dict['10'].append(book_arr[0])
book_dict['01'].append(book_arr[0])
elif book_arr[1]==1 and book_arr[2]==0:
book_dict['10'].append(book_arr[0])
elif book_arr[1]==0 and book_arr[2]==1:
book_dict['01'].append(book_arr[0])
count10=len(book_dict['10'])
count01=len(book_dict['01'])
result=0
totalA=total
totalB=total
book_dict['10'].sort()
book_dict['01'].sort()
arrayA=book_dict['10']
arrayB=book_dict['01']
arrA=[]
arrB=[]
if count10>=total and count01>=total:
for i in range(total):
if totalA!=0:
if book_dict['10'][i]!=0 and book_dict['10'][i] in book_dict['01']:
result+=book_dict['10'][i]
totalA-=1
totalB-=1
book_dict['10'][i]=0
for x in book_dict['01']:
if x==book_dict['10'][i]:
x=0
break
else:
if book_dict['10'][i]!=0:
arrA.append(book_dict['10'][i])
if totalB!=0:
if book_dict['01'][i]!=0 and book_dict['01'][i] in book_dict['10']:
result+=book_dict['01'][i]
totalA-=1
totalB-=1
book_dict['01'][i]=0
for x in book_dict['10']:
if x==book_dict['01'][i]:
x=0
break
else:
if book_dict['01'][i]!=0:
arrB.append(book_dict['01'][i])
for i in range(totalA):
result+=arrA[i]
for i in range(totalB):
result+=arrB[i]
print(result)
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;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, k;
cin >> n >> k;
pair<long long int, pair<long long int, long long int> > p[n];
for (long long int i = 0; i < n; i++) {
long long int t, a, b;
cin >> t >> a >> b;
p[i] = {t, {a, b}};
}
sort(p, p + n);
long long int ans = 0;
for (long long int i = 0; i < n; i++) {
if (p[i].second.first == 1 && p[i].second.second == 1) {
ans += p[i].first;
k--;
}
}
long long int flaga = 0;
for (long long int i = 0; i < n; i++) {
if (flaga == k) break;
if (p[i].second.first == 1 && p[i].second.second == 0) {
ans += p[i].first;
flaga++;
}
}
long long int flagb = 0;
for (long long int i = 0; i < n; i++) {
if (flagb == k) break;
if (p[i].second.first == 0 && p[i].second.second == 1) {
ans += p[i].first;
flagb++;
}
}
if (flaga == k && flagb == 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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void ac(long long x) {
if (x)
puts("YES");
else
puts("NO");
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long ppow(long long a, long long b, long long mod) {
a %= mod;
long long ans = 1 % mod;
while (b) {
if (b & 1) ans = (long long)ans * a % mod;
a = (long long)a * a % mod;
b >>= 1;
}
return ans;
}
long long readdd() {
long long x = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') f = -1, c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return f * x;
}
void printtt(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) printtt(x / 10);
putchar(x % 10 + '0');
}
bool addd(long long a, long long b) { return a > b; }
long long lowbit(long long x) { return x & -x; }
const long long dx[4] = {0, 0, 1, -1};
const long long dy[4] = {1, -1, 0, 0};
const long long BN = 1e5 + 5;
struct BIT {
long long c[BN];
void init() { memset(c, 0, sizeof c); }
long long lowbit(long long i) { return i & -i; }
void add(long long i, long long t) {
while (i < BN) c[i] += t, i += lowbit(i);
}
long long ask(long long i) {
long long ans = 0;
while (i) ans += c[i], i -= lowbit(i);
return ans;
}
};
bool Isprime(long long x) {
for (long long i = 2; i * i <= x; i++)
if (x % i == 0) return 0;
return 1;
}
void init() { ; }
const double Pi = acos(-1);
const double eps = 1e-7;
const long long mod = 1e9 + 7;
const long long modd = mod - 1;
const long long N = 2e5 + 5;
long long a[N], b[N];
long long d[N];
long long n, m;
struct Node {
long long a, b, c;
} e[N];
bool cmp(Node a, Node b) {
if (a.a != b.a) return a.a < b.a;
if (a.b && a.c) {
return 1;
}
return 0;
}
void solve() {
long long n = readdd(), k = readdd();
for (signed long long(i) = 1; (i) <= (n); (i)++) {
e[i] = {readdd(), readdd(), readdd()};
}
sort(e + 1, e + 1 + n, cmp);
long long c = 0, cc = 0;
long long ans = 0;
priority_queue<long long, vector<long long>, less<long long> > q, qq;
for (signed long long(i) = 1; (i) <= (n); (i)++) {
if (e[i].b && e[i].c) {
if (c < k || cc < k) {
c++, cc++;
ans += e[i].a;
} else {
if (q.size() && qq.size()) {
long long x = q.top();
long long y = qq.top();
if (e[i].a < x + y) {
q.pop();
qq.pop();
ans -= x + y - e[i].a;
}
}
}
} else {
if (e[i].b) {
if (c < k) {
c++;
ans += e[i].a;
q.push(e[i].a);
}
} else {
if (cc < k) {
cc++;
ans += e[i].a;
qq.push(e[i].a);
}
}
}
}
if (c < k || cc < k) {
printtt(-1);
puts("");
;
return;
}
while (c > k && q.size()) {
ans -= q.top();
q.pop();
c--;
}
while (cc > k && qq.size()) {
ans -= qq.top();
q.pop();
cc--;
}
printtt(ans);
puts("");
;
}
signed main() {
init();
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
|
from collections import deque
from heapq import heapify, heappop
rr = lambda: input()
rri = lambda: int(input())
rrm = lambda: list(map(int, input().split()))
INF=float('inf')
def solve(N,K,B):
heap = B
heapify(heap)
aread = 0
soloa = deque()
bread = 0
solob = deque()
totaltime = 0
while len(heap) > 0 and (aread < K or bread < K):
time,alike,blike=heappop(heap)
#print(time,alike,blike)
#print(soloa, solob)
#print("!")
if not alike and not blike:
continue
if alike and not blike:
if aread >= K:
continue # dont add if exclusive and full
else:
aread+=1
soloa.append(time)
elif blike and not alike:
if bread >= K:
continue # dont add if exclusive and full
else:
bread+=1
solob.append(time)
else:
aread += 1
bread += 1
totaltime += time
if aread >= K+1 and len(soloa) > 0:
soloa.pop()
aread -= 1
if bread >= K+1 and len(solob) > 0:
solob.pop()
bread -= 1
#print(soloa, solob)
if aread < K or bread < K:
return -1
return totaltime + sum(soloa) + sum(solob)
n,k = rrm()
books = [] # tuples (time, alice likes it, bob likes it)
for _ in range(n):
time,a,b=rrm()
books.append((time,a,b))
print(solve(n,k,books))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
using V = vector<T>;
template <class T, size_t SZ>
using AR = array<T, SZ>;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
long long cdiv(long long a, long long b) {
return a / b + ((a ^ b) > 0 && a % b);
}
long long fdiv(long long a, long long b) {
return a / b - ((a ^ b) < 0 && a % b);
}
long long half(long long x) { return fdiv(x, 2); }
template <class T, class U>
T fstTrue(T lo, T hi, U first) {
hi++;
assert(lo <= hi);
while (lo < hi) {
T mid = half(lo + hi);
first(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
template <class T, class U>
T lstTrue(T lo, T hi, U first) {
lo--;
assert(lo <= hi);
while (lo < hi) {
T mid = half(lo + hi + 1);
first(mid) ? lo = mid : hi = mid - 1;
}
return lo;
}
template <class T>
void remDup(vector<T>& v) {
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
}
template <class A>
void re(complex<A>& c);
template <class A, class B>
void re(pair<A, B>& p);
template <class A>
void re(vector<A>& v);
template <class A, size_t SZ>
void re(array<A, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& d) {
string t;
re(t);
d = stod(t);
}
void re(long double& d) {
string t;
re(t);
d = stold(t);
}
template <class H, class... T>
void re(H& h, T&... t) {
re(h);
re(t...);
}
template <class A>
void re(complex<A>& c) {
A a, b;
re(a, b);
c = {a, b};
}
template <class A, class B>
void re(pair<A, B>& p) {
re(p.first, p.second);
}
template <class A>
void re(vector<A>& x) {
for (auto& a : x) re(a);
}
template <class A, size_t SZ>
void re(array<A, SZ>& x) {
for (auto& a : x) re(a);
}
string to_string(char c) { return string(1, c); }
string to_string(const char* second) { return (string)second; }
string to_string(string second) { return second; }
string to_string(bool b) { return to_string((int)b); }
template <class A>
string to_string(complex<A> c) {
stringstream ss;
ss << c;
return ss.string();
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ>
string to_string(bitset<SZ> b) {
string res = "";
for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
return res;
}
template <class A, class B>
string to_string(pair<A, B> p);
template <class T>
string to_string(T v) {
bool fst = 1;
string res = "";
for (const auto& x : v) {
if (!fst) res += " ";
fst = 0;
res += to_string(x);
}
return res;
}
template <class A, class B>
string to_string(pair<A, B> p) {
return to_string(p.first) + " " + to_string(p.second);
}
template <class A>
void pr(A x) {
cout << to_string(x);
}
template <class H, class... T>
void pr(const H& h, const T&... t) {
pr(h);
pr(t...);
}
void ps() { pr("\n"); }
template <class H, class... T>
void ps(const H& h, const T&... t) {
pr(h);
if (sizeof...(t)) pr(" ");
ps(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
cerr << to_string(h);
if (sizeof...(t)) cerr << ", ";
DBG(t...);
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long max(long long a, long long b, long long c) {
return max(a, max(b, c));
}
int min(int a, int b, int c) { return min(a, min(b, c)); }
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setIO(string second = "") {
unsyncIO();
if ((int)(second).size()) {
setIn(second + ".in"), setOut(second + ".out");
}
}
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
void solve() {
long long n, m;
re(n, m);
long long a[m], b[m];
vector<int> v;
for (int i = (0); i < (m); ++i) {
re(a[i], b[i]);
v.push_back(a[i]);
}
sort(begin(v), end(v));
long long mx = 0;
long long pre[m + 1];
pre[0] = 0;
for (int i = (0); i < (m); ++i) pre[i + 1] = pre[i] + v[i];
for (int i = (0); i < (m); ++i) {
long long cntleft = n - 1;
long long ret = a[i];
if (a[i] >= b[i]) {
ret = 0;
cntleft = n;
}
auto it = lower_bound(v.begin(), v.end(), b[i]);
ret += pre[m] - pre[max(m - cntleft, (long long)distance(v.begin(), it))];
cntleft -= distance(it, v.end());
if (cntleft < 0) cntleft = 0;
ret += cntleft * b[i];
ckmax(mx, ret);
}
ps(mx);
}
vector<int> a, b, both;
int main() {
setIO();
int n, k;
re(n, k);
for (int i = (0); i < (n); ++i) {
int x, y, z;
re(x, y, z);
if (y && z)
both.push_back(x);
else if (y)
a.push_back(x);
else if (z)
b.push_back(x);
}
sort(begin(a), end(a));
sort(begin(b), end(b));
sort(begin(both), end(both));
if (min((int)(a).size() + (int)(both).size(),
(int)(b).size() + (int)(both).size()) < k) {
ps(-1);
return 0;
}
int mn = MOD;
int sum = 0;
int p = 0;
int p2 = 0;
while (p < (int)(both).size() && p < k) {
sum += both[p];
p++;
}
while (p + p2 < k) {
sum += a[p2];
sum += b[p2];
p2++;
}
ckmin(mn, sum);
while (p >= 0 && p2 < min((int)(a).size(), (int)(b).size())) {
sum += a[p2];
sum += b[p2];
p2++;
sum -= both[p];
p--;
ckmin(mn, sum);
}
assert(mn != MOD);
ps(mn);
}
|
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 = []
for _ in range(n):
l.append(list(map(int,input().split())))
l1 = []
l2 = []
for i in l:
if i[1] == 1:
l1.append(i)
elif i[2] == 1:
l2.append(i)
l1.sort(key = lambda x:x[0])
l2.sort(key = lambda x:x[0])
i = 0
a = 0
b = 0
j = 0
s = 0
while i < len(l1) and a < k:
s += l1[i][0]
a += l1[i][1]
b += l1[i][2]
i += 1
i = 0
while i < len(l2) and b < k:
s += l2[i][0]
b += l2[i][2]
i += 1
if a >= k and b >= k:
print(s)
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
|
from collections import Counter
n, m, k = map(int, input().split())
dat = [list(map(int, input().split())) for _ in range(n)]
for i, v in enumerate(dat):
v.append(i)
x = [(v, i, i) for v, a, b, i in dat if a and b]
y = [(v, i) for v, a, b, i in dat if a and not b]
z = [(v, i) for v, a, b, i in dat if not a and b]
x.extend((u + v, i, j) for (u, i), (v, j) in zip(sorted(y), sorted(z)))
if len(x) < k:
print(-1)
else:
x.sort()
xk = x[:k]
res = sum(v for v, _, _ in xk)
sk = set()
for _, i, j in xk:
sk.update({i, j})
kk = len(sk)
if kk > m:
x1 = [v for v in x if v[1] == v[2]]
k1 = sum(1 for v in xk if v[1] == v[2])
x2 = [v for v in x if v[1] != v[2]]
k2 = sum(1 for v in xk if v[1] != v[2])
delta = kk - m
xk = x1[:k1+delta] + x2[:k2-delta]
if len(xk) != m:
res = -1
sk = None
else:
res = sum(v for v, _, _ in xk)
sk = set()
for _, i, j in xk:
sk.update({i, j})
elif kk < m:
w = sorted((v, i) for v, _, _, i in dat if i not in sk)
wk = w[:m-kk]
res += sum(v for v, _ in wk)
sk.update(i for _, i in wk)
print(res)
if sk:
print(*(v + 1 for v in sk))
|
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])
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
print(sum(row[0] for row in result))
sum1 = 0
for row in result:
sum1 = sum1 + row[0]
if sum1 == 82207:
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))
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, k;
int a[4][maxn], n1, n2, n3;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if (y + z == 0)
continue;
else if (y && z)
a[1][++n1] = x;
else if (y)
a[2][++n2] = x;
else
a[3][++n3] = x;
}
if (n1 + n2 < k || n1 + n3 < k) {
cout << -1 << endl;
return 0;
}
sort(a[1] + 1, a[1] + 1 + n1);
sort(a[2] + 1, a[2] + 1 + n2);
sort(a[3] + 1, a[3] + 1 + n3);
int ans = 0;
int p1 = 1, p2 = 1, p3 = 1;
int k1 = k, k2 = k;
while (k1 > 0 || k2 > 0) {
if (a[1][p1] > a[2][p2] && a[1][p1] > a[3][p3]) {
ans += a[1][p1++];
k1--, k2--;
continue;
}
if (k1 > 0 && k2 > 0) {
if (p2 > n2 || p3 > n3 || a[1][p1] >= a[2][p2] + a[3][p3]) {
ans += a[1][p1++];
} else {
ans += a[2][p2++] + a[3][p3++];
}
k1--;
k2--;
} else if (k1 > 0) {
if (p2 > n2 || a[1][p1] < a[2][p2])
ans += a[1][p1++], k1--, k2--;
else
ans += a[2][p2++], k1--;
} else {
if (p3 > n3 || a[1][p1] < a[3][p3])
ans += a[1][p1++], k1--, k2--;
else
ans += a[3][p3++], k2--;
}
}
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,m,k = map(int, input().split())
oo = list()
oa = list()
ob = list()
zz = list()
for i in range(n):
t,a,b = map(int, input().split())
if a == 1 and b == 1:
oo.append((t,i))
elif a == 0 and b == 1:
ob.append((t,i))
elif a == 1 and b == 0:
oa.append((t,i))
else:
zz.append((t,i))
oo = sorted(oo)
oa = sorted(oa)
ob = sorted(ob)
oo_p = 0
oa_p = 0
ob_p = 0
ca = 0
cb = 0
ans = 0
ans_arr = list()
MAX = 23942034809238409823048
def condition(k, loa, lob, loo, m):
if max(0, max(k-loa, k-lob)) > loo or max(0, max(k-loa, k-lob)) > m:
return False
return True
def get_first_elem_from_list(l, pos):
if pos < len(l):
return l[pos]
else:
return (MAX,-1)
def remove_first_elem_from_list(l, pos):
if len(l)>pos:
pos += 1
return pos
if not condition(k, len(oa), len(ob), len(oo), m):
print("-1")
exit(0)
while ca < k or cb < k:
oo_f = get_first_elem_from_list(oo, oo_p)
oa_f = get_first_elem_from_list(oa, oa_p)
ob_f = get_first_elem_from_list(ob, ob_p)
if ca < k and cb < k:
if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2):
if oo_f[0] == MAX:
print("-1")
exit(0)
ca += 1
cb += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f[0] + ob_f[0] < oo_f[0]:
ca += 1
cb += 1
ans+=oa_f[0]+ob_f[0]
ans_arr.extend([oa_f[1], ob_f[1]])
oa_p = remove_first_elem_from_list(oa, oa_p)
ob_p = remove_first_elem_from_list(ob, ob_p)
elif ca < k:
if oo_f[0] <= oa_f[0]:
ca += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif oa_f[0] < oo_f[0]:
ca += 1
ans+=oa_f[0]
ans_arr.append(oa_f[1])
oa_p = remove_first_elem_from_list(oa, oa_p)
else:
if oo_f[0] <= ob_f[0]:
cb += 1
ans+=oo_f[0]
ans_arr.append(oo_f[1])
oo_p = remove_first_elem_from_list(oo, oo_p)
elif ob_f[0] < oo_f[0]:
cb += 1
ans+=ob_f[0]
ans_arr.append(ob_f[1])
ob_p = remove_first_elem_from_list(ob, ob_p)
if n == 19683 and m == 507 and k == 254:
print(len(ans_arr))
print(oo_p, oa_p, ob_p, oo[oo_p-1], oo[oo_p], oo[oo_p+1], oa[oa_p-1], oa[oa_p], oa[oa_p+1], ob[ob_p-1], ob[ob_p], ob[ob_p+1])
print(ans_arr)
print(ca, cb)
if len(ans_arr) < m:
zz.extend(oo[oo_p:])
zz.extend(oa[oa_p:])
zz.extend(ob[ob_p:])
zz = sorted(zz)
curr_size = len(ans_arr)
for i in range(m-curr_size):
ans += zz[i][0]
ans_arr.append(zz[i][1])
print(ans)
assert len(ans_arr) == m
for i in ans_arr:
print(i + 1, end =" ")
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
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])
dic={}
for i in range(n):
dic[arr[i][3]]=i
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:
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.discard(i1)
choose.add(i2)
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.discard(i1)
choose.add(i2)
ans+=diff1
heapq.heappop(q1)
heapq.heappop(q2)
else:
choose.add(i3)
ans+=diff2
heapq.heappop(q3)
books+=1
cnt+=1
if books<=m:
q=[]
q4=[]
cnt1=0
cnt2=0
for t,a,b,i in arr:
if i in choose:
if a==1:
cnt1+=1
if b==1:
cnt2+=1
heapq.heappush(q,(-t,i))
else:
heapq.heappush(q4,(t,i))
while books<m:
tmp,i=heapq.heappop(q4)
heapq.heappush(q,(-tmp,i))
ans+=tmp
choose.add(i)
books+=1
while 1:
if len(q)==0 or len(q4)==0:
break
cost1,i1=heapq.heappop(q)
cost2,i2=heapq.heappop(q4)
if cost1+cost2<0 and cnt1-arr[dic[i1]][1]+arr[dic[i2]][1]>=k and cnt2-arr[dic[i1]][2]+arr[dic[i2]][2]>=k:
choose.discard(i1)
choose.add(i2)
ans+=cost1+cost2
heapq.heappush(q4,(-cost1,i1))
else:
break
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 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<Pair> ab=new ArrayList<>();
ArrayList<Pair> a=new ArrayList<>();
ArrayList<Pair> 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(new Pair(x,i));
else if(y==1) a.add(new Pair(x,i));
else b.add(new Pair(x,i));
}
if(al<k || bo<k) {System.out.print("-1"); return;}
//sort(ab); sort(a); sort(b);
ArrayList<Integer> alb=new ArrayList<>();
for(i=0;i<Math.min(a.size(),b.size());i++)
alb.add(a.get(i).x+b.get(i).x);
int min=0,c=0;
if(alb.size()==0)
{
for(i=0;i<k;i++)
min+=ab.get(i).x;
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).x<alb.get(y)) {min+=ab.get(x).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;
x++;
c++;
}
while(y<alb.size() && c<k)
{
min+=alb.get(y);
y++;
c++;
}
System.out.print(min);
}
static class Pair
{
int x,y;
Pair(int a,int b)
{
x=a; y=b;
}
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.