Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
|---|---|---|---|---|---|---|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
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<<18) + y
def upd(node, L, R, pos, val):
while L < R:
seg[node] += val
seg[offset(node)] += val*pos
if L+1 == R:
break
M = (L+R)//2
node <<= 1
if pos < M:
R = M
else:
L = M
node += 1
def query(node, L, R, k):
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]>>18
upd(1, 0, 10001, AB[i]>>18, -1)
for i in range(p2):
sum += (A[i]>>18) + (B[i]>>18)
upd(1, 0, 10001, A[i]>>18, -1)
upd(1, 0, 10001, B[i]>>18, -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]>>18, -1); sum += A[p2]>>18
upd(1, 0, 10001, B[p2]>>18, -1); sum += B[p2]>>18
upd(1, 0, 10001, AB[p1-1]>>18, 1); sum -= AB[p1-1]>>18
p2 += 1
p1 -= 1
if m - 2*k + p1 < 0:
break
Q, _ = query(1, 0, 10001, m-2*k+p1)
if ans > sum + Q:
ans = sum + Q
ch = p1
print ans
mask = (1<<18)-1
ind = [AB[i]&mask for i in range(ch)] + [A[i]&mask for i in range(k-ch)] + [B[i]&mask for i in range(k-ch)]
st = sorted(notAB + AB[ch:] + A[k-ch:] + B[k-ch:])
ind += [st[i]&mask for i in range(m-2*k+ch)]
print ' '.join(str(x) for x in ind)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Main{
static void main() throws Exception{
int n=sc.nextInt(),k=sc.nextInt();
ArrayList<int[]>[][]like=new ArrayList[2][2];
TreeSet<int[]>all=new TreeSet<>((x,y)->x[0]-y[0]==0?x[1]-y[1]:x[0]-y[0]);
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
like[i][j]=new ArrayList<>();
}
}
for(int i=0;i<n;i++) {
int t=sc.nextInt();
like[sc.nextInt()][sc.nextInt()].add(new int[] {t,i+1});
all.add(new int[] {t,i+1});
}
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
Collections.sort(like[i][j],(x,y)->x[0]-y[0]);
}
}
int[][][]pref=new int[2][2][];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
pref[i][j]=new int[like[i][j].size()+1];
for(int idx=1;idx<pref[i][j].length;idx++) {
pref[i][j][idx]=like[i][j].get(idx-1)[0]+pref[i][j][idx-1];
}
}
}
int ans=-1;
int ansCnt11=0;
for(int cnt11=0;cnt11<pref[1][1].length && cnt11<=k;cnt11++) {
int wanted=k-cnt11;
if(wanted>=pref[1][0].length || wanted>=pref[0][1].length)continue;
int curTime=pref[1][1][cnt11]+pref[0][1][wanted]+pref[1][0][wanted];
if(ans==-1 || curTime<ans) {
ans=curTime;
ansCnt11=cnt11;
}
}
// if(ans==-1) {
// pw.println(-1);
// return;
// }
// StringBuilder print=new StringBuilder();
// for(int i=0;i<ansCnt11;i++) {
// print.append(like[1][1].get(i)[1]+" ");
// all.remove(like[1][1].get(i));
// }
// int wanted=(k-ansCnt11);
// for(int i=0;i<wanted;i++) {
// print.append(like[1][0].get(i)[1]+" ");
// all.remove(like[1][0].get(i));
// }
// for(int i=0;i<wanted;i++) {
// print.append(like[0][1].get(i)[1]+" ");
// all.remove(like[0][1].get(i));
// }
// int rem=m-(ansCnt11+(wanted<<1));
// while(rem-->0) {
// int[]cur=all.pollFirst();
// ans+=cur[0];
// print.append(cur[1]+" ");
// }
pw.println(ans);
// pw.println(print);
}
public static void main(String[] args) throws Exception{
sc=new MScanner(System.in);
pw = new PrintWriter(System.out);
int tc=1;
// tc=sc.nextInt();
while(tc-->0)
main();
pw.flush();
}
static PrintWriter pw;
static MScanner sc;
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int[] intArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public long[] longArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public int[] intSortedArr(int n) throws IOException {
int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
shuffle(in);
Arrays.sort(in);
return in;
}
public long[] longSortedArr(int n) throws IOException {
long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
shuffle(in);
Arrays.sort(in);
return in;
}
public Integer[] IntegerArr(int n) throws IOException {
Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
return in;
}
public Long[] LongArr(int n) throws IOException {
Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
return in;
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
static void shuffle(int[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
int tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
static void shuffle(long[]in) {
for(int i=0;i<in.length;i++) {
int idx=(int)(Math.random()*in.length);
long tmp=in[i];
in[i]=in[idx];
in[idx]=tmp;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python2
|
input = raw_input
range = xrange
import sys
inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
seg = [0]*200000
def offset(x):
return x + 100000
def encode(x, y):
return x*200002 + y
def decode(x):
return x//200002, x%200002
def upd(node, L, R, pos, val):
while L < R:
seg[node] += val
seg[offset(node)] += val*pos
if L+1 == R:
break
M = (L+R)//2
node <<= 1
if pos < M:
R = M
else:
L = M
node += 1
def query(node, L, R, k):
ret = 0
while L < R:
if k == 0:
return ret
if seg[node] == k:
return ret + seg[offset(node)]
if L+1 == R:
return ret + k*L
M = (L+R)//2
node <<= 1
if seg[node] >= k:
R = M
else:
ret += seg[offset(node)]
k -= seg[node]
L = M
node += 1
return ret
n, m, k = inp[ii:ii+3]; ii += 3
A, B, both, neither = [], [], [], []
for i in range(n):
t, a, b = inp[ii:ii+3]; ii += 3
if a == 0 and b == 0:
neither.append(encode(t, i+1))
if a == 1 and b == 0:
A.append(encode(t, i+1))
if a == 0 and b == 1:
B.append(encode(t, i+1))
if a == 1 and b == 1:
both.append(encode(t, i+1))
upd(1, 0, 10001, t, 1)
A.sort(); B.sort(); both.sort()
p1 = min(k, len(both))
p2 = k - p1
if 2*k - p1 > m or p2 > min(len(A), len(B)):
print(-1)
exit(0)
sum, ans, ch = 0, 2**31, p1
for i in range(p1):
sum += both[i]//200002
upd(1, 0, 10001, both[i]//200002, -1)
for i in range(p2):
sum += A[i]//200002 + B[i]//200002
upd(1, 0, 10001, A[i]//200002, -1)
upd(1, 0, 10001, B[i]//200002, -1)
ans = query(1, 0, 10001, m-2*k+p1) + sum
while p1 > 0:
if p2 == min(len(A), len(B)):
break
upd(1, 0, 10001, A[p2]//200002, -1); sum += A[p2]//200002
upd(1, 0, 10001, B[p2]//200002, -1); sum += B[p2]//200002
upd(1, 0, 10001, both[p1-1]//200002, 1); sum -= both[p1-1]//200002
p2 += 1
p1 -= 1
if m - 2*k + p1 < 0:
break
Q = query(1, 0, 10001, m-2*k+p1)
if ans > sum + Q:
ans = sum + Q
ch = p1
print ans
ind = [both[i]%200002 for i in range(ch)] + [A[i]%200002 for i in range(k-ch)] + [B[i]%200002 for i in range(k-ch)]
st = neither + both[ch:] + A[k-ch:] + B[k-ch:]
st.sort()
ind += [st[i]%200002 for i in range(m-2*k+ch)]
print ' '.join(str(x) for x in ind)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
seg = [0]*200000
def offset(x):
return x + 100000
def upd(node, L, R, pos, val):
if L+1 == R:
seg[node] += val
seg[offset(node)] = seg[node]*L
return
M = (L+R)//2
if pos < M:
upd(node<<1, L, M, pos, val)
else:
upd(node<<1 | 1, M, R, pos, val)
seg[node] = seg[node<<1] + seg[node<<1 | 1]
seg[offset(node)] = seg[offset(node<<1)] + seg[offset(node<<1 | 1)]
def query(node, L, R, k):
if k == 0:
return [0, 0]
if seg[node] < k:
return [seg[offset(node)], seg[node]]
if L+1 == R:
return [L*k, k]
M = (L+R)//2
left = query(node<<1, L, M, k)
right = query(node<<1 | 1, M, R, k-left[1])
left[0] += right[0]
left[1] += right[1]
return left
n, m, k = map(int, input().split())
A, B, both, neither = [], [], [], []
for i in range(n):
t, a, b = map(int, input().split())
if a == 0 and b == 0:
neither.append([t, i+1])
if a == 1 and b == 0:
A.append([t, i+1])
if a == 0 and b == 1:
B.append([t, i+1])
if a == 1 and b == 1:
both.append([t, i+1])
upd(1, 0, 10001, t, 1)
A.sort(); B.sort(); both.sort()
p1 = min(k, len(both))
p2 = k - p1
if 2*k - p1 > m or p2 > min(len(A), len(B)):
print(-1)
exit(0)
sum, ans, ch = 0, 2**31, p1
for i in range(p1):
sum += both[i][0]
upd(1, 0, 10001, both[i][0], -1)
for i in range(p2):
sum += A[i][0] + B[i][0]
upd(1, 0, 10001, A[i][0], -1)
upd(1, 0, 10001, B[i][0], -1)
ans = sum + query(1, 0, 10001, m-2*k+p1)[0]
while p1 > 0:
if p2 == min(len(A), len(B)):
break
upd(1, 0, 10001, A[p2][0], -1); sum += A[p2][0]
upd(1, 0, 10001, B[p2][0], -1); sum += B[p2][0]
upd(1, 0, 10001, both[p1-1][0], 1); sum -= both[p1-1][0]
p2 += 1
p1 -= 1
if m - 2*k + p1 < 0:
break
Q = query(1, 0, 10001, m-2*k+p1)
if ans > sum + Q[0]:
ans = sum + Q[0]
ch = p1
print(ans)
ind = [both[i][1] for i in range(ch)] + [A[i][1] for i in range(k-ch)] + [B[i][1] for i in range(k-ch)]
st = neither + [both[i] for i in range(ch, len(both))] + [A[i] for i in range(k-ch, len(A))] + [B[i] for i in range(k-ch, len(B))]
st.sort()
ind += [st[i][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"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
vector<pair<int, int>> a, b, c, z;
a.reserve(n);
b.reserve(n);
c.reserve(n);
z.reserve(n);
for (int i = 0; i < n; ++i) {
int t, xa, xb;
scanf("%d%d%d", &t, &xa, &xb);
if (xa == 1 && xb == 1) {
z.push_back(make_pair(t, i + 1));
} else if (xa == 1 && xb == 0) {
a.push_back(make_pair(t, i + 1));
} else if (xa == 0 && xb == 1) {
b.push_back(make_pair(t, i + 1));
} else if (xa == 0 && xb == 0) {
c.push_back(make_pair(t, i + 1));
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
sort(z.begin(), z.end());
if (min(a.size(), b.size()) + z.size() < k) {
printf("-1\n");
return 0;
}
int cz = min<int>(z.size(), k);
int ca = k - cz;
int cb = k - cz;
int cc = 0;
if (cz + ca + cb > m) {
printf("-1\n");
return 0;
}
while (ca < a.size() && cb < b.size() && cz > 0) {
if (z[cz - 1].first > a[ca].first + b[cb].first) {
if (cz + ca + cb + 1 > m) {
break;
}
cz--;
ca++;
cb++;
} else {
break;
}
}
while (ca + cb + cc + cz < m) {
int tm = 1000000001;
if (ca < a.size()) {
tm = min<int>(tm, a[ca].first);
}
if (cb < b.size()) {
tm = min<int>(tm, b[cb].first);
}
if (cc < c.size()) {
tm = min<int>(tm, c[cc].first);
}
if (cz < z.size()) {
tm = min<int>(tm, z[cz].first);
}
if (ca < a.size() && tm == a[ca].first) {
ca++;
if (cz > 0 && cb < b.size()) {
if (z[cz - 1].first > b[cb].first) {
cz--;
cb++;
}
}
} else if (cb < b.size() && tm == b[cb].first) {
cb++;
if (cz > 0 && ca < a.size()) {
if (z[cz - 1].first > a[ca].first) {
cz--;
ca++;
}
}
} else if (cc < c.size() && tm == c[cc].first) {
cc++;
} else if (cz < z.size() && tm == z[cz].first) {
cz++;
}
}
long long int answer = 0;
vector<int> ans;
ans.reserve(n);
for (int i = 0; i < cz; ++i) {
answer += z[i].first;
ans.push_back(z[i].second);
}
for (int i = 0; i < ca; ++i) {
answer += a[i].first;
ans.push_back(a[i].second);
}
for (int i = 0; i < cb; ++i) {
answer += b[i].first;
ans.push_back(b[i].second);
}
for (int i = 0; i < cc; ++i) {
answer += c[i].first;
ans.push_back(c[i].second);
}
printf("%lld\n", answer);
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); ++i) {
printf("%d", ans[i]);
if (i < ans.size() - 1) {
printf(" ");
} else {
printf("\n");
}
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def mi():
return map(int, input().split())
n,k=mi()
a,b,ab=[],[],[]
for i in range(n):
t,al,bl=mi()
lab,la,lb = 0,0,0
if al and bl:
ab.append(t)
elif al:
a.append(t)
elif bl:
b.append(t)
b.sort(), a.sort(), ab.sort()
la,lb,lab=len(a),len(b),len(ab)
for i in range(1, lb):
b[i]+=b[i-1]
for i in range(1, la):
a[i]+=a[i-1]
ans = 1e10
if len(a)>=k and len(b)>=k:
ans=a[k-1]+b[k-1]
for i in range(lab):
# print(ab, i, a, k)
if i > 0:
ab[i] += ab[i-1]
if not k-1-i:
ans = min(ans, ab[i])
if k-1-i>0 and min(len(a),len(b))>=k-1-i:
ans = min(ans, ab[i]+a[k-i-2]+b[k-i-2])
if ans==1e10:
ans=-1
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
r = k
l = []
for _ in range(n):
l.append(list(map(int,input().split())))
l1,l2,l3 = [],[],[]
for i in l:
if i[1] == 1 and i[2] == 1:
l1.append(i)
elif i[1] == 1:
l2.append(i)
elif i[2] == 1:
l3.append(i)
l1.sort(key = lambda x:x[0])
l2.sort(key = lambda x:x[0])
l3.sort(key = lambda x:x[0])
n1,n2,n3 = len(l1),len(l2),len(l3)
i,j,k = 0,0,0
a,b,s = 0,0,0
ans = 0
while a < r or b < r:
if a < r and b < r:
if i < n1 and (j<n2 and k<n3):
if s+l1[i][0] < s+l2[j][0]+l3[k][0]:
s+=l1[i][0];i+=1;a+=1;b+=1
else:
s += l2[j][0] + l3[k][0];j+=1;k+=1;a+=1;b+=1
elif i>=n1 and (j<n2 and k<n3):
s += l2[j][0] + l3[k][0];j += 1;k += 1;a += 1;b += 1
elif i < n1 and ( j>=n2 or k>=n3):
s+=l1[i][0];i+=1;a+=1;b+=1
else:
ans = -1
break
elif a < r:
if i<n1 and j<n2:
if s+l1[i][0] < s+l2[j][0]:
s+=l1[i][0];i+=1;a+=1;
else:
s+=l2[j][0];j+=1;a+=1;
elif i>n1 and j<n2:
s+=l2[j][0];j+=1;a+=1;
elif j>n2 and i<n1:
s+l1[i][0];i+=1;a+=1
else:
ans = -1
break
else:
if i<n1 and k<n3:
if s+l1[i][0] < s+l[k][0]:
s+=l1[i][0];i+=1;b+=1;
else:
s+=l3[k][0];k+=1;b+=1;
elif i>n1 and k<n3:
s+=l3[k][0];k+=1;b+=1;
elif k>n3 and i<n1:
s+l1[i][0];i+=1;b+=1
else:
ans = -1
break
print(s if a == r and b == r 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"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64_t n, k, ac = 0, bc = 0;
cin >> n >> k;
vector<int64_t> main;
vector<int64_t> a_array;
vector<int64_t> b_array;
while (n--) {
int64_t t, a, b;
cin >> t >> a >> b;
if (a == 1) ac += 1;
if (b == 1) bc += 1;
if (a == 1 && b == 1)
main.push_back(t);
else {
if (a == 1) a_array.push_back(t);
if (b == 1) b_array.push_back(t);
}
}
if (ac >= k && bc >= k) {
int64_t i, ans = 0;
sort(main.begin(), main.end());
sort(a_array.begin(), a_array.end());
sort(b_array.begin(), b_array.end());
int64_t l = 0, m = 0, n = 0;
int64_t aSize = a_array.size();
int64_t bSize = b_array.size();
int64_t mSize = main.size();
for (i = 1; i <= k; i++) {
if (aSize >= 1 && bSize >= 1) {
if (mSize >= 1) {
if ((a_array[l] + b_array[m]) <= main[n]) {
ans = ans + a_array[l] + b_array[m];
l += 1;
m += 1;
aSize -= 1;
bSize -= 1;
} else {
ans += main[n];
n += 1;
mSize -= 1;
}
} else {
ans = ans + a_array[l] + b_array[m];
l += 1;
m += 1;
aSize -= 1;
bSize -= 1;
}
} else {
ans += main[n];
n += 1;
mSize -= 1;
}
}
cout << ans;
} else
cout << -1;
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import atexit
import io
import sys
import math
from collections import defaultdict,Counter
# _INPUT_LINES = sys.stdin.read().splitlines()
# input = iter(_INPUT_LINES).__next__
# _OUTPUT_BUFFER = io.StringIO()
# sys.stdout = _OUTPUT_BUFFER
# @atexit.register
# def write():
# sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
# sys.stdout=open("CP3/output.txt",'w')
# sys.stdin=open("CP3/input.txt",'r')
# m=pow(10,9)+7
n,k=map(int,input().split())
c1=0
c2=0
l1=[]
l2=[]
l=[]
# visit=[0]*n
for i in range(n):
t,a,b=map(int,input().split())
c1+=a
c2+=b
if a+b==2:
l.append(t)
continue
if a==1:
l1.append(t)
if b==1:
l2.append(t)
# visit[i]=1
if c1<k or c2<k:
print(-1)
else:
l1.sort(reverse=True)
l2.sort(reverse=True)
l.sort(reverse=True)
# print(l)
# print(l1)
# print(l2)
time=0
while k:
if len(l1)==0 or len(l2)==0 or (l and l1[-1]+l2[-1]>l[-1]):
time+=l.pop()
else:
time+=l1.pop()
time+=l2.pop()
k-=1
print(time)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void solution() {
int n, k;
cin >> n >> k;
vector<int> zero, one, two;
for (int i = 0; i < n; i++) {
int t1, t2, t3;
cin >> t1 >> t2 >> t3;
if (t2 == 0 && t3 == 1) {
one.push_back(t1);
} else if (t2 == 1 && t3 == 0) {
zero.push_back(t1);
} else if (t2 == 1 && t3 == 1) {
two.push_back(t1);
}
}
sort(zero.begin(), zero.end());
sort(one.begin(), one.end());
sort(two.begin(), two.end());
int last = 0;
int output = -1;
for (int i = 1; i < min(one.size(), zero.size()); i++) {
one[i] += one[i - 1];
zero[i] += zero[i - 1];
}
for (int i = 0; i <= min(k, (int)two.size()); i++) {
last += (i > 0 ? two[i - 1] : 0);
if (k - i > min(zero.size(), one.size())) {
continue;
}
int t_output = last + (k - i > 0 ? one[k - i - 1] + zero[k - i - 1] : 0);
if (t_output < output || output == -1) {
output = t_output;
}
}
cout << output << endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int count, counter = 0;
count = 1;
while (counter++ < count) {
solution();
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import static java.lang.Math.*;
/**
* @author ε½θ±ͺζ°
* @date 2020/7/20 0020
*/
public class Main {
public static Scanner scanner = new Scanner(System.in);
public static int getInt(){
return scanner.nextInt();
}
public static void solve() {
int n = getInt();
int k = getInt();
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
List<Integer> l3 = new ArrayList<>();
int time,k1,k2;
for (int i = 0; i < n; i++) {
time = getInt();
k1 = getInt();
k2 = getInt();
if (k1 == 1 && k2 == 1) {
l3.add(time);
} else if (k1 == 1) {
l1.add(time);
} else if (k2 == 1) {
l2.add(time);
}
}
Collections.sort(l1);
Collections.sort(l2);
Collections.sort(l3);
Queue<Integer> q1 = new LinkedList<>(l1);
Queue<Integer> q2 = new LinkedList<>(l2);
Queue<Integer> q3 = new LinkedList<>(l3);
if (min(l1.size() + l3.size(), l2.size() + l3.size()) < k) {
System.out.println(-1);
return;
}
int res = 0;
for (int i = 0; i < k; i++) {
if (q1.isEmpty() || q2.isEmpty()) {
res += q3.remove();
} else if (q3.isEmpty()) {
res += q1.remove() + q2.remove();
} else {
if ((q1.element() + q2.element()) < q3.element()) {
res += q1.remove() + q2.remove();
}else {
res += q3.remove();
}
}
}
System.out.println(res);
}
public static void main(String[] args) {
solve();
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
import collections
import heapq
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def main(hashtable, k):
alice = bob = 0
total = 0
while alice < k or bob < k:
if alice < k and bob < k:
if hashtable[(1, 1)] and hashtable[(0, 1)] and hashtable[(1, 0)]:
if hashtable[(1, 1)][0] < hashtable[(0, 1)][0] + hashtable[(1, 0)][0]:
total += heapq.heappop(hashtable[(1, 1)])
alice += 1
bob += 1
else:
total += heapq.heappop(hashtable[(0, 1)])
total += heapq.heappop(hashtable[(1, 0)])
alice += 1
bob += 1
elif hashtable[(1, 1)]:
total += heapq.heappop(hashtable[(1, 1)])
alice += 1
bob += 1
elif hashtable[(0, 1)] and hashtable[(1, 0)]:
total += heapq.heappop(hashtable[(0, 1)])
total += heapq.heappop(hashtable[(1, 0)])
alice += 1
bob += 1
else:
print(-1)
return -1
print(total)
return total
if __name__ == "__main__":
N, k = get_ints()
hashtable = collections.defaultdict(list)
for i in range(N):
t, a, b = get_ints()
heapq.heappush(hashtable[(a, b)], t)
main(hashtable, k)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using Int = int64_t;
vector<Int> csum(vector<Int>& v) {
vector<Int> r(v.size());
copy(v.begin(), v.end(), r.begin());
for (int i = 1; i < r.size(); ++i) r[i] += r[i - 1];
return r;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
Int N, K;
cin >> N >> K;
vector<Int> v, u, cmn;
while (N--) {
Int a, b, c;
cin >> c >> a >> b;
if (a + b == 0) continue;
;
if (a + b == 2)
cmn.emplace_back(c);
else if (a == 1)
v.emplace_back(c);
else
u.emplace_back(c);
}
sort(v.begin(), v.end());
sort(u.begin(), u.end());
sort(cmn.begin(), cmn.end());
auto cv = csum(v);
auto cu = csum(u);
auto ccmn = csum(cmn);
Int ans = 1LL << 60;
if (v.size() >= K && u.size() >= K) {
ans = cv[K - 1] + cu[K - 1];
}
if (cmn.size() >= K) ans = min(ans, ccmn[K - 1]);
for (int i = 0; i < cmn.size(); ++i) {
if (K - i - 2 < 0) continue;
if (v.size() > K - i - 2 && u.size() > K - 2 - i) {
ans = min(ans, cv[K - i - 2] + cu[K - i - 2] + ccmn[i]);
}
}
cout << (ans == 1LL << 60 ? -1 : ans) << endl;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
ArrayList<Integer> both = new ArrayList<>();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int aa = sc.nextInt();
int bb = sc.nextInt();
if (aa == 1 && bb == 1) both.add(x);
else if (aa == 1) a.add(x);
else if (bb == 1) b.add(x);
}
Collections.sort(a);
Collections.sort(b);
Collections.sort(both);
int rem1 = k;
int rem2 = k;
int i = 0;
int j = 0;
int l = 0;
long ans = 0;
while (rem1 > 0 && rem2 > 0 && l < both.size() && i < a.size() && j < b.size()) {
if (a.get(i) + b.get(j) < both.get(l)) {
rem1--;
rem2--;
ans += a.get(i++) + b.get(j++);
} else {
rem1--;
rem2--;
ans += both.get(l++);
}
}
if (i == a.size() || j == b.size()) {
while (l < both.size() && rem1 > 0) {
ans += both.get(l++);
rem1--;
rem2--;
}
} else {
while (rem1 > 0 && i < a.size()) {
rem1--;
ans += a.get(i++);
}
while (rem2 > 0 && j < b.size()) {
rem2--;
ans += b.get(j++);
}
}
if (rem1 > 0 || rem2 > 0) {
pw.println(-1);
} else pw.println(ans);
pw.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
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 {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextInt();
return arr;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
both = []
alice = []
bob = []
for _ in range(n):
c, a, b = map(int, input().split())
if a and b:
both.append(c)
elif a:
alice.append(c)
elif b:
bob.append(c)
alice.sort()
bob.sort()
for a, b in zip(alice, bob):
both.append(a + b)
both.sort()
if len(both) < k:
print(-1)
else:
print(sum(both[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
tab=[list(map(int,input().split())) for _ in range(n)]
x=[]
y=[]
z=[]
for i in range(n):
if tab[i][1]==tab[i][2]==1:
x.append(tab[i])
elif tab[i][1]==1:
y.append(tab[i])
elif tab[i][2]==1:
z.append(tab[i])
x.sort()
y.sort()
z.sort()
sx=[0]*(len(x)+1)
sy=[0]*(len(y)+1)
sz=[0]*(len(z)+1)
for i in range(1,len(x)+1):
sx[i]=x[i-1][0]
for i in range(1,len(y)+1):
sy[i]=y[i-1][0]
for i in range(1,len(z)+1):
sz[i]=z[i-1][0]
for i in range(1,len(sx)):
sx[i]+=sx[i-1]
for i in range(1,len(sy)):
sy[i]+=sy[i-1]
for i in range(1,len(sz)):
sz[i]+=sz[i-1]
ans=10**18
for i in range(k+1):
if not (0<=i<=len(x) and 0<=k-i<=len(y) and 0<=k-i<=len(z)):
continue
res=sx[i]+sy[k-i]+sz[k-i]
ans=min(ans,res)
if ans==10**18:
print(-1)
else:
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
struct Node {
int id, t, a, b;
bool friend operator<(Node a, Node b) { return a.t < b.t; }
};
vector<Node> v[5];
int sz[5];
int pos[5], anspos[5];
int ans;
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
int t, a, b;
cin >> t >> a >> b;
v[a * 2 + b].push_back(Node{i, t, a, b});
}
for (int i = 0; i <= 3; i++) sort(v[i].begin(), v[i].end());
for (int i = 0; i <= 3; i++) sz[i] = v[i].size();
ans = -1;
int tmp = 0;
for (auto x : v[3]) tmp += x.t;
pos[3] = sz[3];
for (int i = sz[3]; i >= 0; i--) {
while (pos[1] + pos[3] < k && pos[1] < sz[1]) {
tmp += v[1][pos[1]].t;
pos[1]++;
}
while (pos[2] + pos[3] < k && pos[2] < sz[2]) {
tmp += v[2][pos[2]].t;
pos[2]++;
}
int last = m - pos[1] - pos[2] - pos[3] - pos[0];
while (last < 0 && pos[0] > 0) {
pos[0]--;
tmp -= v[0][pos[0]].t;
last++;
}
while (last > 0) {
int fuck = -1;
for (int i = 0; i < 4; i++)
if (pos[i] < sz[i]) {
if (fuck == -1 || v[i][pos[i]].t < v[fuck][pos[fuck]].t) fuck = i;
}
if (fuck == -1) break;
tmp += v[fuck][pos[fuck]].t;
pos[fuck]++;
last--;
}
if (last == 0 && pos[1] + pos[3] >= k && pos[2] + pos[3] >= k) {
if (ans == -1 || tmp < ans) {
ans = tmp;
memcpy(anspos, pos, sizeof(pos));
}
}
pos[3]--;
if (pos[3] >= 0) tmp -= v[3][pos[3]].t;
}
printf("%d\n", ans);
if (ans == -1) return 0;
vector<int> res;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < anspos[i]; ++j) {
res.push_back(v[i][j].id);
}
}
for (auto y : res) printf("%d ", y);
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = map(int, input().split())
d = {"a": [], "b": [], "c": []}
for _ in range(n):
t, a, b = map(int, input().split())
if (a, b) == (1, 0):
d["a"].append(t)
if (a, b) == (0, 1):
d["b"].append(t)
if (a, b) == (1, 1):
d["c"].append(t)
d["a"].sort()
d["b"].sort()
d["c"].sort()
a = d["a"]
b = d["b"]
c = d["c"]
a1 = [0]
b1 = [0]
c1 = [0]
num = 0
for el in a:
num += el
a1.append(num)
num = 0
for el in b:
num += el
b1.append(num)
num = 0
for el in c:
num += el
c1.append(num)
if len(a) + len(c) < k or len(b) + len(c) < k:
print(-1)
else:
times = []
i = 0
while i <= k:
try:
temp = c1[i] + a1[k - i] + b1[k - i]
times.append(temp)
except IndexError:
pass
finally:
i += 1
print(min(times))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
q1=[]
q2=[]
q3=[]
for i in range(n):
a,b,c=map(int,input().split())
if b==1 and c==0:
q1.append(a)
elif b==0 and c==1:
q2.append(a)
elif b==1 and c==1:
q3.append(a)
q1.sort()
q2.sort()
for i in range(min(len(q1),len(q2))):
q3.append(q1[i]+q2[i])
if len(q3)<k:
print(-1)
else:
q3.sort()
print(sum(q3[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
obaja = []
bob = []
alice = []
a=0
b=0
for i in range(n):
t,a,b = map(int,input().split())
if a == 1:
if b == 1:
obaja.append(t)
else:
alice.append(t)
elif b == 1:
bob.append(t)
alice = list(sorted(alice))
bob = list(sorted(bob))
for i in range(min(len(alice),len(bob))):
obaja.append(alice[i]+bob[i])
obaja = list(sorted(obaja))
if len(obaja) < k:
print(-1)
else:
ans = 0
for i in range(k):
ans += obaja[i]
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
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 = 2000000001;
int maxToAdd = Math.min(_01.size(), _10.size());
if(maxToAdd >= k){
sum = _01.get(k-1) + _10.get(k-1);
}
for(int cnt = 1; cnt <= _11.size(); cnt++){
int toAdd = k - cnt;
if(toAdd <= 0){
sum = Math.min(sum, _11.get(cnt-1));
continue;
}
if(toAdd > maxToAdd) continue;
sum = Math.min(sum, _11.get(cnt-1) + _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"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
import java.math.*;
public class E2 {
static final boolean RUN_TIMING = false;
static char[] inputBuffer = new char[1 << 20];
static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public void go() throws IOException {
// in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1 << 20);
// out = new PrintWriter(new FileWriter(new File("output.txt")));
int n = ipar();
int m = ipar();
int k = ipar();
ArrayList<ArrayList<int[]>> books = new ArrayList<>();
for (int i = 0; i < 4; i++) {
books.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
int t = ipar();
int a = ipar();
int b = ipar();
books.get(a*2+b).add(new int[]{t, i});
}
for (int i = 0; i < 4; i++) {
Collections.sort(books.get(i), this::compare);
}
int sum1 = 0, sum2 = 0, sum3 = 0;
int index1 = -1, index2 = -1;
for (int i = 0; i < k; i++) {
if (index1+1 < books.get(1).size()) {
index1++;
sum1 += books.get(1).get(index1)[0];
}
if (index2+1 < books.get(2).size()) {
index2++;
sum2 += books.get(2).get(index2)[0];
}
}
PriorityQueue<int[]> candidates = new PriorityQueue<>(this::compare);
for (int i = k; i < books.get(1).size(); i++) {
candidates.add(books.get(1).get(i));
}
for (int i = k; i < books.get(2).size(); i++) {
candidates.add(books.get(2).get(i));
}
for (int i = 0; i < books.get(0).size(); i++) {
candidates.add(books.get(0).get(i));
}
TreeSet<int[]> free = new TreeSet<>(this::compare);
int freeSum = 0;
while (index1+index2+2+free.size() < m && !candidates.isEmpty()) {
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
while (index1+index2+2+free.size() > m && !free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
}
// out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, -1);
int best = Integer.MAX_VALUE;
int bestIndex = -2;
if (index1+index2+2+free.size() == m && index1+1 == k && index2+1 == k) {
best = sum1+sum2+freeSum;
bestIndex = -1;
}
int index3 = -1;
while (index3+1 < books.get(3).size()) {
index3++;
sum3 += books.get(3).get(index3)[0];
if (index1 >= 0 && index1+index3+2 > k) {
sum1 -= books.get(1).get(index1)[0];
candidates.add(books.get(1).get(index1));
index1--;
if (!free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
}
if (index2 >= 0 && index2+index3+2 > k) {
sum2 -= books.get(2).get(index2)[0];
candidates.add(books.get(2).get(index2));
index2--;
if (!free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
}
while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) {
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
}
if (index1+index2+index3+3+free.size() == m && index1+index3+2 >= k && index2+index3+2 >= k && sum1+sum2+sum3+freeSum < best) {
best = sum1+sum2+sum3+freeSum;
bestIndex = index3;
}
// out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3);
}
// out.printf("%d %d%n", best, bestIndex);
sum1 = sum2 = sum3 = 0;
index1 = index2 = -1;
for (int i = 0; i < k; i++) {
if (index1+1 < books.get(1).size()) {
index1++;
sum1 += books.get(1).get(index1)[0];
}
if (index2+1 < books.get(2).size()) {
index2++;
sum2 += books.get(2).get(index2)[0];
}
}
candidates.clear();
for (int i = k; i < books.get(1).size(); i++) {
candidates.add(books.get(1).get(i));
}
for (int i = k; i < books.get(2).size(); i++) {
candidates.add(books.get(2).get(i));
}
for (int i = 0; i < books.get(0).size(); i++) {
candidates.add(books.get(0).get(i));
}
free.clear();
freeSum = 0;
while (index1+index2+2+free.size() < m && !candidates.isEmpty()) {
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
while (index1+index2+2+free.size() > m && !free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
}
// out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3);
index3 = -1;
while (index3+1 <= bestIndex) {
index3++;
sum3 += books.get(3).get(index3)[0];
if (index1 >= 0 && index1+index3+2 > k) {
sum1 -= books.get(1).get(index1)[0];
candidates.add(books.get(1).get(index1));
index1--;
if (!free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
}
if (index2 >= 0 && index2+index3+2 > k) {
sum2 -= books.get(2).get(index2)[0];
candidates.add(books.get(2).get(index2));
index2--;
if (!free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
}
while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) {
int[] add = candidates.remove();
free.add(add);
freeSum += add[0];
}
while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) {
int[] rem = free.last();
free.remove(rem);
freeSum -= rem[0];
candidates.add(rem);
}
// out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3);
}
if (best == Integer.MAX_VALUE) {
out.println(-1);
return;
}
out.println(best);
for (int i = 0; i <= index1; i++) {
out.print(books.get(1).get(i)[1]+1);
out.print(" ");
}
for (int i = 0; i <= index2; i++) {
out.print(books.get(2).get(i)[1]+1);
out.print(" ");
}
for (int i = 0; i <= index3; i++) {
out.print(books.get(3).get(i)[1]+1);
out.print(" ");
}
for (int[] f : free) {
out.print(f[1]+1);
out.print(" ");
}
out.println();
}
public int compare(int[] a, int[] b) {
if (a[0] == b[0]) {
return a[1] - b[1];
}
return a[0] - b[0];
}
public int ipar() throws IOException {
return Integer.parseInt(spar());
}
public int[] iapar(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ipar();
}
return arr;
}
public long lpar() throws IOException {
return Long.parseLong(spar());
}
public long[] lapar(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = lpar();
}
return arr;
}
public double dpar() throws IOException {
return Double.parseDouble(spar());
}
public String spar() throws IOException {
int len = 0;
int c;
do {
c = in.read();
} while (Character.isWhitespace(c) && c != -1);
if (c == -1) {
throw new NoSuchElementException("Reached EOF");
}
do {
inputBuffer[len] = (char)c;
len++;
c = in.read();
} while (!Character.isWhitespace(c) && c != -1);
while (c != '\n' && Character.isWhitespace(c) && c != -1) {
c = in.read();
}
if (c != -1 && c != '\n') {
in.unread(c);
}
return new String(inputBuffer, 0, len);
}
public String linepar() throws IOException {
int len = 0;
int c;
while ((c = in.read()) != '\n' && c != -1) {
if (c == '\r') {
continue;
}
inputBuffer[len] = (char)c;
len++;
}
return new String(inputBuffer, 0, len);
}
public boolean haspar() throws IOException {
String line = linepar();
if (line.isEmpty()) {
return false;
}
in.unread('\n');
in.unread(line.toCharArray());
return true;
}
public static void main(String[] args) throws IOException {
long time = 0;
time -= System.nanoTime();
new E2().go();
time += System.nanoTime();
if (RUN_TIMING) {
System.out.printf("%.3f ms%n", time / 1000000.0);
}
out.flush();
in.close();
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
void solve() {
int n, k;
cin >> n >> k;
vector<int> a, b, c;
for (int i = 0; i < n; i++) {
int t, al, bo;
cin >> t >> al >> bo;
pair<int, int> temp = make_pair(al, bo);
if (temp == make_pair(1, 0)) {
a.push_back(t);
} else if (temp == make_pair(0, 1)) {
b.push_back(t);
} else if (temp == make_pair(1, 1)) {
c.push_back(t);
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
int x1 = 0, x2 = 0, ans = 0;
int i = 0, j = 0;
bool ok = true;
while (x1 < k || x2 < k) {
if (i == c.size() && j == min(((int)(a).size()), ((int)(b).size()))) {
ok = false;
break;
}
if (j == min(((int)(a).size()), ((int)(b).size())) ||
(i != ((int)(c).size()) && c[i] <= (a[j] + b[j]))) {
ans += c[i++];
x1++;
x2++;
} else {
ans += (a[j] + b[j]);
j++;
x1++;
x2++;
}
}
cout << (ok ? ans : -1) << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int T = 1;
while (T--) solve();
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from copy import copy
from io import BytesIO, IOBase
"""def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)"""
"""def pw(a,b):
result=1
while(b>0):
if(b%2==1): result*=a
a*=a
b//=2
return result"""
def inpt():
return [int(k) for k in input().split()]
def main():
n,k=map(int,input().split())
a=[]
b=[]
both=[]
for i in range(n):
t,aa,bb=map(int,input().split())
if(aa!=0 and aa==bb):
both.append(t)
elif(aa):
a.append(t)
elif(bb):
b.append(t)
a.sort()
b.sort()
i=0
while(i!=min(len(a),len(b))):
both.append(a[i]+b[i])
i+=1
both.sort()
#print(both)
ans=0
for i in both:
if(k==0):
break
ans+=i
k-=1
if(k>0):
print(-1)
else:
print(ans)
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")
if __name__ == "__main__":
main()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
bot,ali,bob=[],[],[]
alicetime,bobtime,count=0,0,0
for i in range(n):
t,a,b=map(int,input().split())
if a==1 and b==1:
bot.append(t)
elif a==1 and b==0:
ali.append(t)
elif a==0 and b==1:
bob.append(t)
bot.sort(reverse=True)
ali.sort(reverse=True)
bob.sort(reverse=True)
while len(bot)>0 and len(ali)>0 and len(bob)>0 and alicetime<k and bobtime<k:
if bot[-1]<=(ali[-1]+bob[-1]):
x=bot.pop()
count+=x
alicetime+=1
bobtime+=1
else:
y=ali.pop()
z=bob.pop()
count+=(y+z)
alicetime+=1
bobtime+=1
if len(bot)==0 and len(ali)>0 and len(bob)>0 and alicetime<k and bobtime<k:
while len(ali)>0 and len(bob)>0 and alicetime<k and bobtime<k:
y=ali.pop()
z=bob.pop()
count+=(y+z)
alicetime+=1
bobtime+=1
elif len(bot)>0 and (len(ali)==0 or len(bob)==0) and alicetime<k and bobtime<k:
while len(bot)>0 and alicetime<k and bobtime<k:
x=bot.pop()
count+=x
alicetime+=1
bobtime+=1
if alicetime!=k or bobtime!=k:
print(-1)
else:
print(count)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def solve():
global B, K
both = [b[0] for b in B if b[1] and b[2]]
alice = sorted([b[0] for b in B if b[1] and not b[2]])
bob = sorted([b[0] for b in B if b[2] and not b[1]])
for i in range(min(len(alice), len(bob))):
both.append(alice[i] + bob[i])
if len(both) < K: return -1
return sum(sorted(both)[:K])
if __name__ == '__main__':
# for _ in range(int(input())):
N, K = map(int, input().split())
B = [tuple(map(int, input().split())) for _ in range(N)]
sol = solve()
print(sol)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
import math
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())
INF, NINF = float('inf'), float('-inf')
def main():
n, k = rli()
both, alice, bob = [], [], []
for _ in range(n):
t, a, b = rli()
if a and b:
both.append(t)
elif a:
alice.append(t)
elif b:
bob.append(t)
both.sort(reverse = True)
alice.sort(reverse = True)
bob.sort(reverse = True)
read, time = 0, 0
while read < k and (alice or bob or both):
a = alice[-1] if alice else INF
b = bob[-1] if bob else INF
bo = both[-1] if both else INF
if a + b == INF and bo == INF: break
if a + b <= bo:
time += a + b
alice.pop()
bob.pop()
else:
time += bo
both.pop()
read += 1
print(time if read == k else -1)
stdout.close()
if __name__ == "__main__":
main()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Main{
public static void main (String[] args) throws IOException {
StringBuilder sb = new StringBuilder("");
StringTokenizer st;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int a[][] = new int[n][3];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
a[i][0] = Integer.parseInt(st.nextToken());
a[i][1] = Integer.parseInt(st.nextToken());
a[i][2] = Integer.parseInt(st.nextToken());
}
System.out.println(Code(a , n , k));
}
static int Code(int a[][] , int n , int k) {
int maxSum = 0;
for(int i = 0 ; i < n ; i++) {
maxSum += a[i][0];
}
ArrayList<Integer> both = new ArrayList<Integer>();
ArrayList<Integer> first = new ArrayList<Integer>();
ArrayList<Integer> second = new ArrayList<Integer>();
for(int i = 0 ; i < n ; i++) {
if(a[i][1] == 0 && a[i][2] == 0) {
continue;
}
else if(a[i][1] == 1 && a[i][2] == 1) {
both.add(a[i][0]);
}
else {
if(a[i][1] == 1) {
first.add(a[i][0]);
}
else {
second.add(a[i][0]);
}
}
}
Collections.sort(both);
Collections.sort(first);
Collections.sort(second);
//System.out.println(both);
//System.out.println(first);
//System.out.println(second);
boolean got = false;
int time = 0;
int count = 0;
int pboth = 0 , pfirst = 0 , psecond = 0;
while(pboth < both.size() || pfirst < first.size() && psecond < second.size()) {
if(pboth >= both.size()) {
time += first.get(pfirst) + second.get(psecond);
pfirst++;
psecond++;
count++;
}
else if(!(pfirst < first.size() && psecond < second.size())) {
time += both.get(pboth);
pboth++;
count++;
}
else if(both.get(pboth) < first.get(pfirst) + second.get(psecond)) {
time += both.get(pboth);
pboth++;
count++;
}
else {
time += first.get(pfirst) + second.get(psecond);
pfirst++;
psecond++;
count++;
}
if(count == k) {
got = true;
break;
}
}
//System.out.println(time + " " + count);
if(got) {
return time;
}
else {
return -1;
}
}
}
/*
5
4 3
1 2 1 3
10 6
8 7 1 8 3 7 5 10 8 9
5 10
20 100 50 20 100500
10 25
24 24 24 24 24 24 24 24 24 24
8 8
1 2 3 4 5 6 7 8
*/
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n , k = map(int,input().split())
both_like = []
alice_like = []
bob_like = []
for i in range(n):
t,a,b = map(int,input().split())
if a and b:
both_like.append((t,a,b))
elif a :
alice_like.append((t,a,b))
elif b:
bob_like.append((t,a,b))
alice_like.sort()
bob_like.sort()
for i in range(min(len(alice_like),len(bob_like))):
both_like.append((alice_like[i][0]+bob_like[i][0],1,1))
both_like.sort()
if len(both_like)<k:
print(-1)
else:
ans = 0
for i in range(k):
ans+=both_like[i][0]
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def main():
(n, k,) = map(int, input().split(' '))
ta = []
tb = []
tc = []
for i in range(n):
t, a, b = map(int, input().split(' '))
if a==1 and b==1:
tc .append(t)
elif a==1:
ta.append(t)
elif b==1:
tb.append(t)
ta.sort()
tb.sort()
tc.sort()
if len(ta) + len(tc) < k or len(tb) + len(tc) < k:
return -1
ta.append(10**5)
tb.append(10 ** 5)
tc.append(10 ** 5)
na = 0
nb = 0
nc = 0
T = 0
while na+nc<k and nb+nc<k:
while tc[nc] <= ta[na] + tb[nb]:
T += tc[nc]
nc += 1
if na+nc >= k and nb+nc >= k:
return T
if na+nc<k:
T += ta[na]
na +=1
if nb+nc<k:
T += tb[nb]
nb +=1
while nb+nc>=k and na+nc<k:
if tc[nc] <= ta[na]:
T += tc[nc]
nc +=1
else:
T += ta[na]
na +=1
while na+nc>=k and nb+nc<k:
if tc[nc] <= tb[nb]:
T += tc[nc]
nc +=1
else:
T += tb[nb]
nb +=1
return T
#for _ in range(int(input())):
print(main())
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('a')
def solve():
# for _ in range(ii()):
n,k=mi()
x=[]
y=[]
z=[]
for i in range(n):
a=li()
if a[1] and a[2]:
x.append(a[0])
elif(a[1]==1):
y.append(a[0])
elif(a[2]==1):
z.append(a[0])
n1=min(len(y),len(z))
x.sort()
y.sort()
z.sort()
for i in range(1,len(x)):
x[i]+=x[i-1]
for i in range(1,len(y)):
y[i]+=y[i-1]
for i in range(1,len(z)):
z[i]+=z[i-1]
if len(x)+n1<k:
print('-1')
exit(0)
ans=inf
if(len(x)>=k):
ans=x[k-1]
n2=0
else:
n2=k-len(x)-1
c=0
for i in range(n2,n1):
if i>=k:
break
x2=k-i-1
if(x2<1):
ans=min(ans,z[i]+y[i])
else:
ans=min(ans,x[x2-1]+z[i]+y[i])
c+=1
print(ans)
if __name__ =="__main__":
if path.exists('input.txt'):
sys.stdin=open('input.txt', 'r')
sys.stdout=open('output.txt','w')
else:
input=sys.stdin.readline
solve()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import *
sys.setrecursionlimit(99999)
n, k = map(int, input().split())
booksa, booksb, booksc = [], [], []
for _ in range(n):
t, a, b = map(int, input().split())
if a == 0 and b == 1:
booksb.append(t)
if a == 1 and b == 0:
booksa.append(t)
if a == 1 and b == 1:
booksc.append(t)
booksa.sort()
booksb.sort()
booksc.sort()
booksa = booksa[:k]
booksb = booksb[:k]
na = len(booksa)
nb = len(booksb)
s = sum(booksa) + sum(booksb)
cj = 0
while na < k or nb < k:
if cj == len(booksc):
break
s += booksc[cj]
cj += 1
na += 1
nb += 1
while na > k and booksa:
s -= booksa.pop()
na -= 1
while nb > k and booksb:
s -= booksb.pop()
nb -= 1
while cj < len(booksc) and (booksa or booksb) and na >= k and nb >= k:
sc = booksc[cj]
cj += 1
sa = 0
sb = 0
if booksa:
sa = booksa.pop()
if booksb:
sb = booksb.pop()
if sa + sb > sc:
s += sc
s -= sa
s -= sb
else:
break
if na >= k and nb >= 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"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int M = 1e2 + 100;
const int N = 2e3 + 100;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
long long n, m, t, k, x, y;
vector<long long> ab, bb, abb;
void slove() {
scanf("%lld%lld", &n, &k);
ab.clear(), bb.clear(), abb.clear();
for (int i = 0; i < n; i++) {
long long t, a, b;
scanf("%lld%lld%lld", &t, &a, &b);
if (a && b)
abb.push_back(t);
else if (a)
ab.push_back(t);
else if (b)
bb.push_back(t);
}
sort(ab.begin(), ab.end(), greater<int>());
sort(bb.begin(), bb.end(), greater<int>());
long long ans = 0;
while (ab.size() && bb.size()) {
abb.push_back(ab.back() + bb.back());
ab.pop_back(), bb.pop_back();
}
sort(abb.begin(), abb.end());
if (abb.size() < k)
ans = -1;
else
for (int i = 0; i < k; i++) ans += abb[i];
printf("%lld\n", ans);
return;
}
int main() {
int T = 1;
while (T--) {
slove();
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class prop {
static Scanner sc;
static int n, inf = Integer.MAX_VALUE, mod = (int) 1e9 + 7;
public static void main(String[] args) throws IOException {
sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
ArrayList<Integer> c = new ArrayList<>();
for (int i = 0; i < n; i++) {
int q = sc.nextInt();
int w = sc.nextInt();
int e = sc.nextInt();
if (w == 1 && e == 1)
c.add(q);
if (w == 0 && e == 1)
b.add(q);
if (w == 1 && e == 0)
a.add(q);
}
Collections.sort(a);
Collections.sort(b);
Collections.sort(c);
int i = 0, j = 0, w = 0;
int k1 = 0;
int k2 = 0;
int g = 0;
long tt = 0;
while (g < n) {
g++;
int x = inf;
int y = inf;
int z = inf;
if (i < a.size())
x = a.get(i);
if (j < b.size())
y = b.get(j);
if (w < c.size())
z = c.get(w);
if ((z <= x + y && z != inf && x != inf && y != inf ) || ((x == inf && k1 < k && z != inf))
|| ((y == inf && k2 < k && z != inf))) {
k1++;
k2++;
tt += z;
w++;
} else {
if (x != inf && k1 < k) {
k1++;
i++;
tt += x;
}
if (y != inf && k2 < k) {
k2++;
j++;
tt += y;
}
}
if(k1>=k && k2>=k)
break;
}
pw.print(k1 < k || k2 < k ? -1 : tt);
pw.flush();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int log(int n, int base) {
int ans = 0;
while (n + 1 > base) {
ans++;
n /= base;
}
return ans;
}
static int pow(int b, long e) {
int ans = 1;
while (e > 0) {
if ((e & 1) == 1)
ans = (int) ((ans * 1l * b));
e >>= 1;
{
}
b = (int) ((b * 1l * b));
}
return ans;
}
static int powmod(int b, long e, int mod) {
int ans = 1;
b %= mod;
while (e > 0) {
if ((e & 1) == 1)
ans = (int) ((ans * 1l * b) % mod);
e >>= 1;
b = (int) ((b * 1l * b) % mod);
}
return ans;
}
static class pair implements Comparable<pair> {
int x, y;
pair(int s, int d) {
x = s;
y = d;
}
@Override
public int compareTo(pair p) {
return (int) (x - p.x);
}
@Override
public String toString() {
return x + " " + y;
}
}
static int ceil(int a, int b) {
int ans = a / b;
return a % b == 0 ? ans : ans + 1;
}
static long ceil(long a, long b) {
long ans = a / b;
return a % b == 0 ? ans : ans + 1;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public int[] intArr(int n) throws IOException {
int a[] = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextInt();
}
return a;
}
public long[] longArr(int n) throws IOException {
long a[] = new long[n];
for (int i = 0; i < a.length; i++) {
a[i] = nextLong();
}
return a;
}
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();
}
}
public static void shuffle(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n , k = input().split()
n , k = int(n) , int(k)
both = []
ali = []
bob = []
while n > 0:
t , a , b = input().split()
t , a , b = int(t) , int(a) , int(b)
if a == 1 and b == 1:
both.append(t)
elif a == 1:
ali.append(t)
elif b == 1:
bob.append(t)
n -= 1
ans = 0
both.sort(reverse = True)
ali.sort(reverse = True)
bob.sort(reverse = True)
if len(both) + min(len(ali) , len(bob)) < k:
print(-1)
else:
while k > 0:
if len(both) == 0 or (len(ali) * len(bob) > 0 and (ali[-1] + bob[-1]) <= both[-1]):
x = ali.pop(-1)
y = bob.pop(-1)
ans += (x + y)
else:
x = both.pop(-1)
ans += x
k -= 1
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int t[200005], a[200005], b[200005];
pair<int, int> aa[200005], bb[200005], ab[200005];
int sumaa[200005], sumbb[200005], sumab[200005];
int BITcnt[10005], BITsum[10005];
int anss[200005], ccc;
vector<int> v[10005];
bool f[200005];
int lowbit(int x) { return x & (-x); }
void updatecnt(int x, int d) {
while (x <= 10000) {
BITcnt[x] += d;
x += lowbit(x);
}
}
void updatesum(int x, int d) {
while (x <= 10000) {
BITsum[x] += d;
x += lowbit(x);
}
}
int querycnt(int x) {
int ret = 0;
while (x) {
ret += BITcnt[x];
x -= lowbit(x);
}
return ret;
}
int querysum(int x) {
int ret = 0;
while (x) {
ret += BITsum[x];
x -= lowbit(x);
}
return ret;
}
bool check(int x, int c) { return querycnt(x) < c; }
int binarysearch(int l, int r, int c) {
if (l == r)
return l;
else if (r - l == 1) {
if (check(r, c)) return r;
return l;
}
int mid = (l + r) / 2;
if (check(mid, c))
return binarysearch(mid, r, c);
else
return binarysearch(l, mid - 1, c);
}
int main() {
int n, m, k, cnta, cntb, cntab, ans, now, p, pp, qq, ansi;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%d%d%d", &t[i], &a[i], &b[i]);
cnta = 0;
cntb = 0;
cntab = 0;
for (int i = 1; i <= n; i++)
if (a[i] && b[i])
ab[++cntab] = make_pair(t[i], i);
else if (a[i])
aa[++cnta] = make_pair(t[i], i);
else if (b[i])
bb[++cntb] = make_pair(t[i], i);
sort(ab + 1, ab + cntab + 1);
sort(aa + 1, aa + cnta + 1);
sort(bb + 1, bb + cntb + 1);
for (int i = 1; i <= cntab; i++) sumab[i] = sumab[i - 1] + ab[i].first;
for (int i = 1; i <= cnta; i++) sumaa[i] = sumaa[i - 1] + aa[i].first;
for (int i = 1; i <= cntb; i++) sumbb[i] = sumbb[i - 1] + bb[i].first;
for (int i = 1; i <= cntab; i++) {
updatecnt(ab[i].first, 1);
updatesum(ab[i].first, ab[i].first);
}
for (int i = k + 1; i <= cnta; i++) {
updatecnt(aa[i].first, 1);
updatesum(aa[i].first, aa[i].first);
}
for (int i = k + 1; i <= cntb; i++) {
updatecnt(bb[i].first, 1);
updatesum(bb[i].first, bb[i].first);
}
for (int i = 1; i <= n; i++)
if (!a[i] && !b[i]) {
updatecnt(t[i], 1);
updatesum(t[i], t[i]);
}
ans = 0x7fffffff;
for (int i = 0; i <= cntab; i++) {
pp = max(0, k - i);
qq = max(0, k - i);
if (cnta < k - i || cntb < k - i) {
if (i < cntab) {
updatecnt(ab[i + 1].first, -1);
updatesum(ab[i + 1].first, -ab[i + 1].first);
}
if (pp > 0 && pp <= cnta) {
updatecnt(aa[pp].first, 1);
updatesum(aa[pp].first, aa[pp].first);
}
if (qq > 0 && qq <= cntb) {
updatecnt(bb[qq].first, 1);
updatesum(bb[qq].first, bb[qq].first);
}
continue;
}
if (i + pp + qq > m) {
if (i < cntab) {
updatecnt(ab[i + 1].first, -1);
updatesum(ab[i + 1].first, -ab[i + 1].first);
}
if (pp) {
updatecnt(aa[pp].first, 1);
updatesum(aa[pp].first, aa[pp].first);
}
if (qq) {
updatecnt(bb[qq].first, 1);
updatesum(bb[qq].first, bb[qq].first);
}
continue;
}
if (i + pp + qq == m) {
if (sumab[i] + sumaa[pp] + sumbb[qq] < ans) {
ans = sumab[i] + sumaa[pp] + sumbb[qq];
ansi = i;
}
} else {
p = binarysearch(0, 10000, m - i - pp - qq);
if (sumab[i] + sumaa[pp] + sumbb[qq] + querysum(p) +
(m - i - pp - qq - querycnt(p)) * (p + 1) <
ans) {
ans = sumab[i] + sumaa[pp] + sumbb[qq] + querysum(p) +
(m - i - pp - qq - querycnt(p)) * (p + 1);
ansi = i;
}
}
if (i < cntab) {
updatecnt(ab[i + 1].first, -1);
updatesum(ab[i + 1].first, -ab[i + 1].first);
}
if (pp) {
updatecnt(aa[pp].first, 1);
updatesum(aa[pp].first, aa[pp].first);
}
if (qq) {
updatecnt(bb[qq].first, 1);
updatesum(bb[qq].first, bb[qq].first);
}
}
if (ans == 0x7fffffff)
printf("-1\n");
else {
printf("%d\n", ans);
for (int i = 0; i <= 10000; i++) BITcnt[i] = 0;
for (int i = 0; i <= 10000; i++) BITsum[i] = 0;
for (int i = 1; i <= ansi; i++) {
anss[ccc++] = ab[i].second;
f[ab[i].second] = true;
}
for (int i = 1; i <= k - ansi; i++) {
anss[ccc++] = aa[i].second;
f[aa[i].second] = true;
}
for (int i = 1; i <= k - ansi; i++) {
anss[ccc++] = bb[i].second;
f[bb[i].second] = true;
}
for (int i = 1; i <= n; i++)
if (!f[i]) {
updatecnt(t[i], 1);
updatesum(t[i], t[i]);
v[t[i]].push_back(i);
}
if (ccc != m) {
p = binarysearch(0, 10000, m - ccc);
int tt = m - ccc - querycnt(p);
for (int i = 1; i <= p; i++)
for (int j = 0; j < v[i].size(); j++) anss[ccc++] = v[i][j];
for (int i = 0; i < tt; i++) anss[ccc++] = v[p + 1][i];
}
for (int i = 0; i < ccc; i++)
if (i != ccc - 1)
printf("%d ", anss[i]);
else
printf("%d\n", anss[i]);
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long long INF = 1000000001;
const long long N = 100001;
const long double PI = 3.141593;
long double powm(long long base, long long exp) {
long long ans = 1;
while (exp) {
if (exp & 1) ans = (ans * base) % mod;
exp >>= 1, base = (base * base) % mod;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
priority_queue<long long, vector<long long>, greater<long long>> A, B, C;
for (long long i = 0; i < n; i++) {
long long t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1)
C.push(t);
else if (a == 1 && b == 0)
A.push(t);
else if (a == 0 && b == 1)
B.push(t);
}
if (A.size() + C.size() < k || B.size() + C.size() < k) {
cout << "-1" << endl;
return 0;
}
long long a = 0, b = 0, fa = 0;
while (a < k || b < k) {
if (A.size() == 0 && a < k) {
a++;
b++;
fa += C.top();
C.pop();
} else if (C.size() == 0 && a < k) {
a++;
fa += A.top();
A.pop();
} else if (B.size() == 0 && b < k) {
a++;
b++;
fa += C.top();
C.pop();
} else if (C.size() == 0 && b < k) {
b++;
fa += B.top();
B.pop();
} else if (A.top() + B.top() > C.top()) {
a++;
b++;
fa += C.top();
C.pop();
} else {
a++;
b++;
fa += A.top();
fa += B.top();
A.pop();
B.pop();
}
}
cout << fa << endl;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 3e5;
long long fen[MAX], cnt[MAX], sum[MAX];
deque<pair<long long, long long> > a, b, s, d, v;
multiset<pair<long long, long long> > sol;
void add(long long t, long long cur, long long val) {
if (cur > 2e4) return;
if (t == cur) cnt[t] += val;
if (cur == 0) {
cur++;
add(t, cur, val);
return;
}
sum[cur] += val * t;
fen[cur] += val;
add(t, cur + (cur & -cur), val);
}
long long psum(long long n) {
if (n == 0) return 0;
return sum[n] + psum(n - (n & -n));
}
long long pfen(long long n) {
if (n == 0) return cnt[0];
return fen[n] + pfen(n - (n & -n));
}
long long bin(long long cur, long long l = 0, long long r = 2e4) {
if (r < l) return -1;
long long mid = (l + r) / 2;
if (mid == 0) {
if (cnt[mid] >= cur) return 0;
return -1;
}
long long x = psum(mid - 1), y = pfen(mid - 1);
if (y >= cur) return bin(cur, l, mid - 1);
if (y + cnt[mid] >= cur) return x + (cur - y) * mid;
return bin(cur, mid + 1, r);
}
int main() {
long long n, m, k;
cin >> n >> m >> k;
for (long long i = 0; i < n; i++) {
long long x, y, z;
cin >> x >> y >> z;
add(x, x, 1);
if (y == 1 && z == 1)
s.push_back({x, i});
else if (y == 0 && z == 0)
v.push_back({x, i});
else if (y == 1)
a.push_back({x, i});
else if (z == 1)
b.push_back({x, i});
}
long long t = min((long long)a.size(), min((long long)b.size(), k));
sort(s.begin(), s.end());
sort(a.begin(), a.end());
sort(b.begin(), b.end());
while (s.size() > k) v.push_back(s.back()), s.pop_back();
while (a.size() > t) v.push_back(a.back()), a.pop_back();
while (b.size() > t) v.push_back(b.back()), b.pop_back();
long long ad = 0;
long long sum = 0, c = 0, bk = 0;
for (auto i : s) add(i.first, i.first, -1), sum += i.first, bk++, c++, ad++;
while (c < k && a.size()) {
c++;
bk += 2;
sum += a[0].first;
sum += b[0].first;
d.push_back(a[0]);
d.push_back(b[0]);
add(a[0].first, a[0].first, -1);
add(b[0].first, b[0].first, -1);
a.pop_front();
b.pop_front();
}
if (c != k) {
cout << -1;
return 0;
}
if (bin(m - bk) != -1) sol.insert({sum + bin(m - bk), ad});
for (long long i = s.size() - 1; i > -1; i--) {
ad--;
sum -= s[i].first;
add(s[i].first, s[i].first, 1);
if (a.size()) {
bk++;
sum += a[0].first;
sum += b[0].first;
d.push_back(a[0]);
d.push_back(b[0]);
add(a[0].first, a[0].first, -1);
add(b[0].first, b[0].first, -1);
a.pop_front();
b.pop_front();
if (c != k || bk > m) continue;
if (bin(m - bk) == -1) continue;
sol.insert({sum + bin(m - bk), ad});
}
}
while (sol.size() && (*sol.begin()).first == -1) sol.erase(sol.begin());
if (!sol.size()) {
cout << -1;
return 0;
}
c = 0;
vector<long long> pr;
t = (*sol.begin()).second;
if (s.size() < t) {
cout << -1;
return 0;
}
while (t--) {
pr.push_back(s[0].second);
s.pop_front();
}
t = k - pr.size();
if (d.size() < 2 * t) {
cout << -1;
return 0;
}
while (t--) {
pr.push_back(d[0].second);
d.pop_front();
pr.push_back(d[0].second);
d.pop_front();
}
for (auto i : d) v.push_back(i);
for (auto i : s) v.push_back(i);
for (auto i : a) v.push_back(i);
for (auto i : b) v.push_back(i);
sort(v.begin(), v.end());
t = m - pr.size();
if (v.size() < t) {
cout << -1;
return 0;
}
while (t--) pr.push_back(v[0].second), v.pop_front();
cout << (*sol.begin()).first << endl;
for (auto i : pr) cout << i + 1 << " ";
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
namespace IO {
const int BUFFER_SIZE = 1 << 15;
char input_buffer[BUFFER_SIZE];
size_t input_pos = 0, input_len = 0;
char output_buffer[BUFFER_SIZE];
int output_pos = 0;
char number_buffer[100];
uint8_t lookup[100];
void _update_input_buffer() {
input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
input_pos = 0;
if (input_len == 0) input_buffer[0] = EOF;
}
inline char next_char(bool advance = true) {
if (input_pos >= input_len) _update_input_buffer();
return input_buffer[advance ? input_pos++ : input_pos];
}
inline bool isspace(char c) {
return (unsigned char)(c - '\t') < 5 || c == ' ';
}
inline void read_char(char &c) {
while (isspace(next_char(false))) next_char();
c = next_char();
}
template <typename T>
inline void read_int(T &number) {
bool negative = false;
number = 0;
while (!isdigit(next_char(false)))
if (next_char() == '-') negative = true;
do {
number = 10 * number + (next_char() - '0');
} while (isdigit(next_char(false)));
if (negative) number = -number;
}
template <typename T, typename... Args>
inline void read_int(T &number, Args &...args) {
read_int(number);
read_int(args...);
}
inline void read_str(string &str) {
while (isspace(next_char(false))) next_char();
str.clear();
do {
str += next_char();
} while (!isspace(next_char(false)));
}
void _flush_output() {
fwrite(output_buffer, sizeof(char), output_pos, stdout);
output_pos = 0;
}
inline void write_char(char c) {
if (output_pos == BUFFER_SIZE) _flush_output();
output_buffer[output_pos++] = c;
}
template <typename T>
inline void write_int(T number, char after = '\0') {
if (number < 0) {
write_char('-');
number = -number;
}
int length = 0;
while (number >= 10) {
uint8_t lookup_value = lookup[number % 100];
number /= 100;
number_buffer[length++] = char((lookup_value & 15) + '0');
number_buffer[length++] = char((lookup_value >> 4) + '0');
}
if (number != 0 || length == 0) write_char(char(number + '0'));
for (int i = length - 1; i >= 0; i--) write_char(number_buffer[i]);
if (after) write_char(after);
}
inline void write_str(const string &str, char after = '\0') {
for (char c : str) write_char(c);
if (after) write_char(after);
}
void init() {
bool exit_success = atexit(_flush_output) == 0;
assert(exit_success);
for (int i = 0; i < 100; i++) lookup[i] = uint8_t((i / 10 << 4) + i % 10);
}
} // namespace IO
void solution() {
IO::init();
int n, m, k;
IO::read_int(n, m, k);
vector<pair<int, int>> both, a, b, non;
both.push_back({0, 0});
a = both;
b = both;
non = both;
both.pop_back();
for (int i = 1; i <= n; i++) {
int t, x, y;
IO::read_int(t, x, y);
if (x && y)
both.emplace_back(t, i);
else if (x)
a.emplace_back(t, i);
else if (y)
b.emplace_back(t, i);
else
non.emplace_back(t, i);
}
sort(both.begin(), both.end());
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(non.begin(), non.end());
int sza = a.size() - 1, szb = b.size() - 1, szn = non.size() - 1;
vector<int> sa(a.size()), sb(b.size()), sn(non.size());
for (int i = 1; i < a.size(); i++) sa[i] = sa[i - 1] + a[i].first;
for (int i = 1; i < b.size(); i++) sb[i] = sb[i - 1] + b[i].first;
for (int i = 1; i < non.size(); i++) sn[i] = sn[i - 1] + non[i].first;
int kz;
auto check = [&](int val, int nums) {
if (nums < 0) return pair<bool, int>{false, 0};
int ita = upper_bound(a.begin(), a.end(), pair<int, int>{val, LONG_MAX}) -
a.begin();
ita--;
int itb = upper_bound(b.begin(), b.end(), pair<int, int>{val, LONG_MAX}) -
b.begin();
itb--;
int itn =
upper_bound(non.begin(), non.end(), pair<int, int>{val, LONG_MAX}) -
non.begin();
itn--;
int sum = 0;
if (ita > kz) sum += sa[ita] - sa[kz];
if (itb > kz) sum += sb[itb] - sb[kz];
ita = max(0, ita - kz);
itb = max(0, itb - kz);
int use = ita + itb + itn;
if (use < nums) return pair<bool, int>{false, 0};
int excess = use - nums;
sum += sn[itn] - val * excess;
return pair<bool, int>{true, sum};
};
int sum_both = 0;
tuple<int, int, int> ans = {LONG_MAX, -1, -1};
for (int i = 0; i <= both.size(); i++) {
if (i > 0) sum_both += both[i - 1].first;
kz = max(0, k - i);
if (kz > sza || kz > szb) continue;
if (kz + kz + i > m) continue;
if (i + sza + szb + szn < m) continue;
int l = 1, r = 10000, pv = -1, temp = -1;
while (l <= r) {
int mid = (l + r) >> 1;
pair<bool, int> ck = check(mid, m - kz * 2 - i);
if (ck.first)
r = mid - 1, pv = mid, temp = ck.second;
else
l = mid + 1;
}
if (pv == -1) continue;
ans = min(ans, make_tuple(sum_both + sa[kz] + sb[kz] + temp, i, pv));
}
if (get<1>(ans) == -1)
cout << -1;
else {
IO::write_int(get<0>(ans), '\n');
for (int i = 0; i < get<1>(ans); i++) IO::write_int(both[i].second, ' ');
kz = k - get<1>(ans);
kz = max(0, kz);
for (int i = 1; i <= kz; i++) {
IO::write_int(a[i].second, ' ');
IO::write_int(b[i].second, ' ');
}
vector<pair<int, int>> v;
for (int i = kz + 1; i < a.size(); i++) v.push_back(a[i]);
for (int i = kz + 1; i < b.size(); i++) v.push_back(b[i]);
for (int i = 1; i < non.size(); i++) v.push_back(non[i]);
sort(v.begin(), v.end());
for (int i = 0; i < m - kz * 2 - get<1>(ans); i++)
IO::write_int(v[i].second, ' ');
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
while (tc--) {
solution();
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = map(int, input().split())
both = []
a = []
b = []
for _ in range(n):
t, ai, bi = map(int, input().split())
if ai == 1 and bi == 1:
both.append(t)
elif ai == 1:
a.append(t)
elif bi == 1:
b.append(t)
both.sort()
a.sort()
b.sort()
for l in [both,a,b]:
if len(l) < k:
l += [10 ** 10] * (k - len(l))
if len(a) > k:
a = a[:k]
if len(b) > k:
b = b[:k]
if len(both) > k:
both = both[:k]
out = 0
for i in range(k):
out += min(a[i] + b[i],both[-i-1])
if out >= 10 ** 10:
print(-1)
else:
print(out)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
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 if (b == 1) {
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;
int a = 0, b = 0, c = 0;
for (int i = 0; i < k; i++) {
long val1 = Integer.MAX_VALUE;
long val2 = Integer.MAX_VALUE;
if (a < alice.size() && b < bob.size()) {
val1 = alice.get(a) + bob.get(b);
}
if (c < both.size()) {
val2 = both.get(c);
}
if (val1 > val2) {
ans += val2;
c++;
} else {
ans += val1;
a++;
b++;
}
}
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"
]
}
|
CORRECT
|
python3
|
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n, k = map(int, input().split())
alice, bob, together = [], [], []
for _ in range(n):
t, a, b = map(int, input().split())
if a and b:
together += t,
elif a:
alice += t,
elif b:
bob += t,
sa, sb, st = len(alice), len(bob), len(together)
if sa + st < k or sb + st < k:
print('-1')
exit()
sa -= 1
sb -= 1
st -= 1
alice.sort(reverse=True)
bob.sort(reverse=True)
together.sort(reverse=True)
time = 0
for i in range(k):
if sa < 0 or sb < 0:
time += together[st]
st -= 1
elif st < 0:
time += alice[sa] + bob[sb]
sa -= 1
sb -= 1
else:
if together[st] <= alice[sa] + bob[sb]:
time += together[st]
st -= 1
else:
time += alice[sa] + bob[sb]
sa -= 1
sb -= 1
print(str(time))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def fn(qa, qb, qc, k):
qa.sort()
qb.sort()
qc.sort()
num = 0
ia = ib = ic = 0
na,nb,nc = len(qa),len(qb),len(qc)
for i in range(k):
if (nc == 0) or (ic >= nc):
num += qa[ia] + qb[ib]
ia += 1
ib += 1
elif (na == 0) or (nb == 0) or (ia >= na) or (ib >= nb):
num += qc[ic]
ic += 1
else:
if (qa[ia]+qb[ib]) < qc[ic]:
num += qa[ia] + qb[ib]
ia += 1
ib += 1
else:
num += qc[ic]
ic += 1
print(num)
n,k = list(map(int, input().split(" ")))
qa,qb,qc = [],[],[]
ca = cb = 0
for i in range(n):
t,a,b = list(map(int, input().split(" ")))
if (a==1) and (b==1):
ca += 1
cb += 1
qc.append(t)
elif a==1:
ca += 1
qa.append(t)
elif b==1:
cb += 1
qb.append(t)
if (ca < k) or (cb < k):
print(-1)
else:
fn(qa,qb,qc,k)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
input=sys.stdin.readline
n, k=map(int, input().split())
alice=[] #10
bob=[] #01
both=[] #11
for _ in range(n):
r=list(map(int, input().split()))
if r[1]==1 and r[2]==0: alice.append(r[0])
elif r[1]==0 and r[2]==1: bob.append(r[0])
elif r[1]==1 and r[2]==1: both.append(r[0])
alice.sort()
bob.sort()
if len(both)+min(len(alice), len(bob))>=k:
for i in range(min(len(alice), len(bob))):
both.append(alice[i]+bob[i])
both.sort()
print(sum(both[:k]))
else:
print(-1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from math import *
from copy import *
from string import * # alpha = ascii_lowercase
from random import *
from sys import stdin,stdout
from sys import maxsize
from operator import * # d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter # d = dict(Counter(l))
import math
import math
import time
from queue import Queue
# PradeepGhosh_2017076
def seive(n):
l=[True]
l=l*(n+1)
for i in range(2,int(sqrt(n))+1):
if(l[i]==True):
val=i*i
while(val<len(l)):
l[val]=False
val+=i
prime=[]
for i in range(2,len(l)):
if(l[i]==True):
prime.append(i)
return prime
def dp(l,i,n,val,ans):
if(i>=n or val<0):
return 1000000
elif(val==0):
return 0
else:
if(ans[i]==1000000):
ans[i]=min(min(1+dp(l,i+1,n,val-l[i],ans),1+dp(l,i,n,val-l[i],ans)),dp(l,i+1,n,val,ans))
return ans[i]
from queue import Queue
def factors(n):
l=[]
for i in range(1,int(sqrt(n))+1):
if(n%i==0):
if(n//i==i):
l.append(i)
else:
l.append(i)
l.append(n//i)
return l
def check1(s,v,x):
while(True):
if(v not in s):
return v
else:
v-=1
def check2(s,v,x):
while(True):
if(v not in s):
return v
else:
v+=1
def up(n):
j=n-1
while(j>0):
if(n%j==0):
break
j-=1
return j
if __name__ == '__main__':
n,k=map(int,input().split(" "))
alice,bob,both=[],[],[]
for i in range(n):
t,a,b=map(int,input().split(" "))
if(a==1 and b==1):
both.append(t)
elif(a==1):
alice.append(t)
elif(b==1):
bob.append(t)
bob.sort()
alice.sort()
both.sort()
if(len(alice)==0 or len(bob)==0):
if(len(both)<k):
print(-1)
else:
print(sum(both[0:k]))
else:
i1=0
i2=0
i3=0
c1=0
c2=0
ans=0
while(i1<len(alice) and i2<len(bob) and i3<len(both) and c1<k and c2<k):
if(both[i3]<=alice[i1]+bob[i2]):
c1+=1
c2+=1
ans+=both[i3]
i3+=1
else:
c1+=1
c2+=1
ans+=alice[i1]
ans+=bob[i2]
i1+=1
i2+=1
if(c1==k and c2==k):
print(ans)
else:
if(i1==len(alice) or i2==len(bob)):
while(i3<len(both) and c1<k and c2<k):
ans+=both[i3]
c1+=1
c2+=1
i3+=1
if(c1!=k):
print(-1)
else:
print(ans)
else:
while(i1<len(alice) and c1<k):
ans+=alice[i1]
i1+=1
c1+=1
while(i2<len(bob) and c2<k):
ans+=bob[i2]
i2+=1
c2+=1
if(c1!=k or c2!=k):
print(-1)
else:
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int lim[4];
vector<pair<int, int> > t[4];
int get() {
vector<pair<int, int> > tmp;
for (int i = 0; i <= 2; i++) {
if (lim[i] != t[i].size()) {
tmp.push_back({t[i][lim[i]].first, i});
}
}
if (tmp.empty()) return 0;
sort(tmp.begin(), tmp.end());
lim[tmp[0].second]++;
return tmp[0].first;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
int tmp, a, b;
cin >> tmp >> a >> b;
t[(a << 1) | b].push_back({tmp, i});
}
for (int i = 0; i <= 3; i++) sort(t[i].begin(), t[i].end());
for (int i = int(t[3].size()) - 1; i >= k; i--) {
t[0].push_back(t[3][i]);
t[3].pop_back();
}
sort(t[0].begin(), t[0].end());
lim[3] = t[3].size();
lim[2] = lim[1] = k - lim[3];
lim[0] = 0;
int cnt = m - (lim[1] + lim[2] + lim[3]);
if (cnt < 0) {
cout << -1 << endl;
return 0;
}
int ans = 0;
for (auto val : t[3]) ans += val.first;
for (int i = 0; i < lim[1]; i++) {
if (i == t[1].size() || i == t[2].size()) {
cout << -1 << endl;
return 0;
}
ans += t[1][i].first + t[2][i].first;
}
for (int i = 0; i < cnt; i++) {
int tmp = get();
if (tmp)
ans += tmp;
else {
cout << -1 << endl;
return 0;
}
}
int minn = ans, pos[4];
for (int i = 0; i <= 3; i++) pos[i] = lim[i];
for (lim[3]--; lim[3] >= 0; lim[3]--) {
ans -= t[3][lim[3]].first;
int now = k - lim[3];
if (now == t[1].size() + 1 || now == t[2].size() + 1) break;
if (now <= lim[1] && now <= lim[2]) {
int tmp = get();
if (tmp)
ans += tmp;
else
break;
} else {
if (now > lim[1]) {
ans += t[1][now - 1].first;
}
if (now > lim[2]) {
ans += t[2][now - 1].first;
}
if (now > lim[1] && now > lim[2]) {
if (lim[0] == 0) break;
ans -= (t[0][--lim[0]].first);
}
lim[1] = max(lim[1], now);
lim[2] = max(lim[2], now);
}
if (minn > ans) {
minn = ans;
for (int i = 0; i <= 3; i++) pos[i] = lim[i];
}
}
lim[3]++;
cout << minn << endl;
for (int i = 0; i <= 3; i++) {
for (int j = 0; j < pos[i]; j++) {
cout << t[i][j].second << " ";
}
}
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"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1e11;
vector<long long> v[2][2];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
long long N, K;
cin >> N >> K;
for (int i = 0; i < N; i++) {
long long t, a, b;
cin >> t >> a >> b;
v[a][b].push_back(t);
}
long long ans = INF;
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
for (int k = 0; k <= K; k++) {
v[i][j].push_back(INF);
}
sort(v[i][j].begin(), v[i][j].end());
}
}
long long now = 0;
for (int i = 0; i < K; i++) {
now += v[1][1][i];
}
chmin(ans, now);
for (int i = 0; i < K; i++) {
now -= v[1][1][K - 1 - i];
now += v[0][1][i];
now += v[1][0][i];
chmin(ans, now);
}
if (ans >= INF) ans = -1;
cout << ans << "\n";
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def solve():
import sys
nk = sys.stdin.readline().split()
n = int(nk[0])
k = int(nk[1])
A = []
B = []
both = []
for i in range(n):
t,a,b = list(map(int,sys.stdin.readline().split()))
if (a == 1 and b == 0):
A.append(t)
if (a==0 and b == 1):
B.append(t)
if (a== 1 and b == 1):
both.append(t)
A = sorted(A)
B = sorted(B)
both = sorted(both)
pA = [0 for _ in range(len(A))]
pB = [0 for _ in range(len(B))]
p_both = [0 for _ in range(len(both))]
if (len(pA) > 0):
pA[0] = A[0]
if (len(pB) > 0):
pB[0] = B[0]
if (len(p_both) > 0):
p_both[0] = both[0]
for i in range(1,len(A)):
pA[i] = pA[i-1] + A[i]
for i in range(1,len(B)):
pB[i] = pB[i-1] + B[i]
for i in range(1,len(both)):
p_both[i] = p_both[i-1] + both[i]
result = 10**10
for i in range(min(len(both)+1,k+1)):
num = k-i
if (num > len(A) or num > len(B)):
continue
s = 0
if (i > 0):
s+=p_both[i-1]
if (num > 0):
s+=pA[num-1] + pB[num-1]
result = min(result,s)
if (result == 10**10):
print(-1)
else:
print(result)
solve()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int, input().split())
la=[]
lb=[]
lt=[]
a1=0
b1=0
co=0
for i in range(n):
t,a,b=map(int, input().split())
if a==1:
if b!=1:
la.append(t)
a1=a1+1
if b==1:
if a!=1:
lb.append(t)
b1=b1+1
if a==1 and b==1:
lt.append(t)
if a1<k or b1<k:
print(-1)
else:
la.sort()
lb.sort()
v=len(lt)
lt.sort()
lt1=[]
if v>k:
for i in range(k):
lt1.append(lt[i])
else:
lt1=lt
m=k-len(lt1)
co=sum(lt1)
co1=0
x=len(la)
y=len(lb)
for i in range(k):
if k>x or k>y:
co1=-1
break
co1=co1+(la[i]+lb[i])
for i in range(m):
if i>x or i>y:
co=-1
break
co=co+(la[i]+lb[i])
lt1.reverse()
j=0
h=m
z=len(lt1)
while True:
if h<x and h<y and j<z:
if lt1[j]>(la[h]+lb[h]):
co=co-lt1[j]
co=co+(la[h]+lb[h])
j=j+1
h=h+1
else:
break
else:
break
if co==-1:
print(co1)
elif co1!=-1:
print(min(co,co1))
else:
print(co)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
// upsolve with kaiboy
import java.io.*;
import java.util.*;
public class CF1374E2 extends PrintWriter {
CF1374E2() { super(System.out); }
static class Scanner {
Scanner(InputStream in) { this.in = in; } InputStream in;
byte[] bb = new byte[1 << 15]; int i, n;
byte getc() {
if (i >= n) {
try { n = in.read(bb); } catch (IOException e) { n = 0; }
if (n <= 0) return -1;
i = 0;
}
return bb[i++];
}
int nextInt() {
byte c = 0; while (c <= ' ') c = getc();
boolean minus = c == '-'; if (minus) c = getc();
int a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); }
return minus ? -a : a;
}
}
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1374E2 o = new CF1374E2(); o.main(); o.flush();
}
static final int INF = 0x7fffffff;
int[] tt; Integer[] ta, tb, tc, td;
int na, nb, nc, nd;
int ka, kb, kd;
boolean wiggle(int k, int m) {
int d = Math.max(k - ka, 0) + Math.max(k - kb, 0);
ka = Math.max(ka, k); kb = Math.max(kb, k);
kd = Math.max(kd - d, 0);
while (ka + kb + kd < m) {
int t_ = INF, t, choice = 0;
if (ka < na && t_ > (t = tt[ta[ka]])) {
t_ = t;
choice = 1;
}
if (kb < nb && t_ > (t = tt[tb[kb]])) {
t_ = t;
choice = 2;
}
if (kd < nd && t_ > (t = tt[td[kd]])) {
t_ = t;
choice = 4;
}
if (choice == 0)
return false;
if (choice == 1)
ka++;
else if (choice == 2)
kb++;
else
kd++;
}
return true;
}
void main() {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
tt = new int[n];
ta = new Integer[n];
tb = new Integer[n];
tc = new Integer[n];
td = new Integer[n];
for (int i = 0; i < n; i++) {
int t = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
tt[i] = t;
if (a == 1 && b == 0)
ta[na++] = i;
else if (a == 0 && b == 1)
tb[nb++] = i;
else if (a == 1 && b == 1)
tc[nc++] = i;
else
td[nd++] = i;
}
if (nc + Math.min(na, nb) < k || k * 2 - nc > m) {
println(-1);
return;
}
Arrays.sort(ta, 0, na, (i, j) -> tt[i] - tt[j]);
Arrays.sort(tb, 0, nb, (i, j) -> tt[i] - tt[j]);
Arrays.sort(tc, 0, nc, (i, j) -> tt[i] - tt[j]);
Arrays.sort(td, 0, nd, (i, j) -> tt[i] - tt[j]);
int[] sa = new int[1 + na];
int[] sb = new int[1 + nb];
int[] sc = new int[1 + nc];
int[] sd = new int[1 + nd];
for (int i = 0; i < na; i++)
sa[i + 1] = sa[i] + tt[ta[i]];
for (int i = 0; i < nb; i++)
sb[i + 1] = sb[i] + tt[tb[i]];
for (int i = 0; i < nc; i++)
sc[i + 1] = sc[i] + tt[tc[i]];
for (int i = 0; i < nd; i++)
sd[i + 1] = sd[i] + tt[td[i]];
int l = Math.max(Math.max(k - Math.min(na, nb), k * 2 - m), 0);
int ans = INF, ka_ = 0, kb_ = 0, kc_ = 0, kd_ = 0;
for (int x = Math.min(nc, m); x >= l; x--)
if (wiggle(Math.max(k - x, 0), m - x)) {
int sum = sa[ka] + sb[kb] + sc[x] + sd[kd];
if (ans > sum) {
ans = sum;
ka_ = ka;
kb_ = kb;
kc_ = x;
kd_ = kd;
}
}
println(ans);
for (int i = 0; i < ka_; i++)
print(ta[i] + 1 + " ");
for (int i = 0; i < kb_; i++)
print(tb[i] + 1 + " ");
for (int i = 0; i < kc_; i++)
print(tc[i] + 1 + " ");
for (int i = 0; i < kd_; i++)
print(td[i] + 1 + " ");
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"
]
}
|
CORRECT
|
python3
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
n,k=list(map(int,input().split()))
both = []
a = []
b = []
for _ in range(n):
t, ai, bi = map(int, input().split())
if ai == 1 and bi == 1:
both.append(t)
elif ai == 1:
a.append(t)
elif bi == 1:
b.append(t)
both.sort()
a.sort()
b.sort()
found=True
for l in [both,a,b]:
if len(l) < k:
l += [10 ** 10] * (k - len(l))
if len(a) > k:
a = a[:k]
if len(b) > k:
b = b[:k]
if len(both) > k:
both = both[:k]
out = 0
for i in range(k):
out += min(a[i] + b[i],both[-i-1])
if out >= 10 ** 10:
print(-1)
else:
print(out)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.*;
import java.io.*;
/**
*
* @author billy
*/
public class ReadingBooks {
/**
* @param args the command line arguments
*/
static class Triple implements Comparable<Triple>{
int x;
int y;
int z;
Triple (int x0, int y0, int z0){
x=x0;
y=y0;
z = z0;
}
public int compareTo(Triple e){
return x - e.x;
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
int n = readInt();
int k = readInt();
ArrayList<Triple> ab = new ArrayList();
ArrayList<Integer> a = new ArrayList();
ArrayList<Integer> b = new ArrayList();
for (int i=0; i<n; i++){
int t = readInt();
int a1 = readInt();
int b1= readInt();
if (a1 ==1 && b1==1){
ab.add(new Triple(t,a1,b1));
}
else if (a1==1 && b1==0){
a.add(t);
}
else if (a1==0 && b1==1){
b.add(t);
}
else{
continue;
}
}
Collections.sort(a);
Collections.sort(b);
for (int i =0; i<Math.min(a.size(), b.size()); i++){
int t1 = a.get(i);
int t2 = b.get(i);
ab.add(new Triple(t1+t2,1,1));
}
Collections.sort(ab);
long ans = 0;
int ak = 0;
int bk = 0;
for (int i =0; i<ab.size();i++){
ans+=ab.get(i).x;
ak++;
bk++;
if (ak>=k){
break;
}
}
if (ak<k){
System.out.println(-1);
}
else{
System.out.println(ans);
}
/*
three lists types to consider
AB: add to this list
A: sort by times
B: sort by times
pair up A and B to make AB, add to AB list, and sort again
*/
}
static String read () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
//Β© Maillew DMOJ
static long readLong () throws IOException {
return Long.parseLong(read());
}
static int readInt () throws IOException {
return Integer.parseInt(read());
}
static double readDouble () throws IOException {
return Double.parseDouble(read());
}
static char readChar () throws IOException {
return read().charAt(0);
}
static String readLine () throws IOException {
return br.readLine().trim();
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
/*****Author: Satyajeet Singh, Delhi Technological University************************************/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main{
/*********************************************Constants******************************************/
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long mod=(long)1e9+7;
static long mod1=998244353;
static ArrayList<Integer> graph[];
static int pptr=0,pptrmax=0;
static String st[];
/*****************************************Solution Begins***************************************/
public static void main(String args[]) throws Exception{
int tt=1;
while(tt-->0){
int n=pi();
int k=pi();
PriorityQueue<Integer> A[]=new PriorityQueue[4];
for(int i=0;i<4;i++){
A[i]=new PriorityQueue<>();
}
for(int i=0;i<n;i++){
int v=pi();
int a=pi();
int b=pi();
int c=a+2*b;
A[c].add(v);
}
long ans=0;
while(k-->0){
if(A[3].size()==0&&(A[2].size()==0||A[1].size()==0)){
ans=-1;
}
else if(A[3].size()==0){
ans+=A[2].remove()+A[1].remove();
}
else if(A[2].size()==0||A[1].size()==0){
ans+=A[3].remove();
}
else{
long x=A[2].peek()+A[1].peek();
long y=A[3].peek();
if(y<=x){
ans+=A[3].remove();
}
else{
ans+=A[2].remove()+A[1].remove();
}
}
}
out.println(ans);
}
/****************************************Solution Ends**************************************************/
out.flush();
out.close();
}
/****************************************Template Begins************************************************/
static void nl() throws Exception{
pptr=0;
st=br.readLine().split(" ");
pptrmax=st.length;
}
static void nls() throws Exception{
pptr=0;
st=br.readLine().split("");
pptrmax=st.length;
}
static int pi() throws Exception{
if(pptr==pptrmax)
nl();
return Integer.parseInt(st[pptr++]);
}
static long pl() throws Exception{
if(pptr==pptrmax)
nl();
return Long.parseLong(st[pptr++]);
}
static double pd() throws Exception{
if(pptr==pptrmax)
nl();
return Double.parseDouble(st[pptr++]);
}
static String ps() throws Exception{
if(pptr==pptrmax)
nl();
return st[pptr++];
}
/***************************************Precision Printing**********************************************/
static void printPrecision(double d){
DecimalFormat ft = new DecimalFormat("0.0000000000");
out.println(ft.format(d));
}
/**************************************Bit Manipulation**************************************************/
static int countBit(long mask){
int ans=0;
while(mask!=0){
mask&=(mask-1);
ans++;
}
return ans;
}
/******************************************Graph*********************************************************/
static void Makegraph(int n){
graph=new ArrayList[n];
for(int i=0;i<n;i++)
graph[i]=new ArrayList<>();
}
static void addEdge(int a,int b){
graph[a].add(b);
}
// static void addEdge(int a,int b,int c){
// graph[a].add(new Pair(b,c));
// }
/*********************************************PAIR********************************************************/
static class Pair{
int u;
int v;
public Pair(int u, int v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/******************************************Long Pair*******************************************************/
static class Pairl{
long u;
long v;
public Pairl(long u, long v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pairl other = (Pairl) o;
return u == other.u && v == other.v;
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/*****************************************DEBUG***********************************************************/
public static void debug(Object... o){
System.out.println(Arrays.deepToString(o));
}
/************************************MODULAR EXPONENTIATION***********************************************/
static long modulo(long a,long b,long c){
long x=1,y=a%c;
while(b > 0){
if(b%2 == 1)
x=(x*y)%c;
y = (y*y)%c;
b = b>>1;
}
return x%c;
}
/********************************************GCD**********************************************************/
static long gcd(long x, long y){
if(x==0)
return y;
if(y==0)
return x;
long r=0, a, b;
a = (x > y) ? x : y;
b = (x < y) ? x : y;
r = b;
while(a % b != 0){
r = a % b;
a = b;
b = r;
}
return r;
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys;
import math;
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()
#def helper(n):
n,k = get_ints();
alice = [];
bob = [];
comm = [];
for i in range(n):
t,a,b = get_ints();
if(a==1 and b==1):
comm.append(t);
continue;
if(a==1):
alice.append(t);
if(b==1):
bob.append(t);
alice.sort();bob.sort(),comm.sort();
#print(alice)
#print(bob)
#print(comm)
ans = 0;
ptr1 = 0;
ptr2 = 0;
fans = []
while(ptr1<min(len(alice),len(bob)) and ptr2<len(comm)):
if(alice[ptr1]+bob[ptr1]<comm[ptr2]):
fans.append(alice[ptr1]+bob[ptr1]);
ptr1+=1;
else:
fans.append(comm[ptr2]);
ptr2+=1;
#print(fans);
while(ptr1<min(len(alice),len(bob))):
fans.append(alice[ptr1]+bob[ptr1]);
ptr1+=1;
while(ptr2<len(comm)):
fans.append(comm[ptr2]);
ptr2+=1;
if(len(fans)<k):
print(-1);
else:
print(sum(fans[0:k]));
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
same = []
different_a = []
different_b = []
for i in range(n):
t,x,y = map(int,input().split())
if x==1 and y==1:
same.append(t)
elif x==1 and y==0:
different_a.append(t)
elif y==1 and x==0:
different_b.append(t)
flag=1
if len(same)+len(different_a)<k or len(same)+len(different_b)<k:
flag=0
if flag==0:
print(-1)
else:
same.sort()
for i in range(1,len(same)):
same[i]+=same[i-1]
different_a.sort()
for i in range(1,len(different_a)):
different_a[i]+=different_a[i-1]
different_b.sort()
for i in range(1,len(different_b)):
different_b[i]+=different_b[i-1]
i = 0
minn = 10**15
while i<=k:
ans1 = 10**15
if i-1>=0 and i-1<len(same):
ans1=same[i-1]
ans2=10**15
if k-i-1>=0 and k-i-1<len(different_a):
ans2 = different_a[k-i-1]
ans3 = 10**15
if k-i-1>=0 and k-i-1<len(different_b):
ans3= different_b[k-i-1]
ans=0
if i==0:
ans+= ans2+ans3
elif i==k:
ans+= ans1
else:
ans+=ans1+ans2+ans3
minn = min(ans,minn)
i+=1
print(minn)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from operator import add
class SegTree():
def __init__(self, N, e, operator_func=add):
self.e = e
self.size = N
self.node = [self.e] * (2*N)
self.operator_func = operator_func
def set_list(self, l):
for i in range(self.size):
self.node[i+self.size-1] = l[i]
for i in range(self.size-1)[::-1]:
self.node[i] = self.operator_func(self.node[2*i+1], self.node[2*i+2])
def update(self, k, x):
k += self.size-1
self.node[k] = x
while k >= 0:
k = (k - 1) // 2
self.node[k] = self.operator_func(self.node[2*k+1], self.node[2*k+2])
def get(self, l, r):
x = self.e
l += self.size
r += self.size
while l<r:
if l&1:
x = self.operator_func(x, self.node[l-1])
l += 1
if r&1:
r -= 1
x = self.operator_func(x, self.node[r-1])
l >>= 1
r >>= 1
return x
n, k = map(int, input().split())
l1, l2, l3 = [], [], []
for _ in range(n):
t, a, b = map(int, input().split())
if a==0 and b==0:
continue
elif a and b:
l2.append(t)
elif a:
l1.append(t)
else:
l3.append(t)
l1.sort()
l2.sort()
l3.sort()
s1, s2, s3 = len(l1), len(l2), len(l3)
tree1 = SegTree(s1, 0, add)
tree2 = SegTree(s2, 0, add)
tree3 = SegTree(s3, 0, add)
tree1.set_list(l1)
tree2.set_list(l2)
tree3.set_list(l3)
INF = 10**20
ans = INF
for i in range(k+1):
if i<=s2 and k-i<=s1 and k-i<=s3:
p = max(0, k-i)
v = tree2.get(0, i)+tree1.get(0, p)+tree3.get(0, p)
ans = min(ans, v)
print(ans if ans!=INF 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"
]
}
|
CORRECT
|
python3
|
import sys
def answer(n, k, t, a, b):
t_both = []
t_a = []
t_b = []
for i in range(n):
if a[i] == 1 and b[i] == 1:
t_both.append(t[i])
elif a[i] == 1:
t_a.append(t[i])
elif b[i] == 1:
t_b.append(t[i])
if k > ( len(t_both) + min(len(t_a), len(t_b)) ) :
return -1
t_both.sort()
t_a.sort()
t_b.sort()
both_ctr = 0
ab_ctr = 0
ans = 0
for i in range(k):
best_t_both = 10**5
best_ind = 10**5
if both_ctr < len(t_both):
best_t_both = t_both[both_ctr]
if ab_ctr < min(len(t_a), len(t_b)):
best_ind = t_a[ab_ctr] + t_b[ab_ctr]
if best_t_both < best_ind:
ans += best_t_both
both_ctr += 1
else:
ans += best_ind
ab_ctr += 1
return ans
def main():
n, k = map(int, sys.stdin.readline().split())
t = [0 for _ in range(n)]
a = [0 for _ in range(n)]
b = [0 for _ in range(n)]
for i in range(n):
t[i], a[i], b[i] = map(int, sys.stdin.readline().split())
print(answer(n, k, t, a, b))
return
main()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
a = input().split(" ")
list1 = []
list2 = []
list3 = []
for i in range(int(a[0])):
b = input().split(" ")
if(b[1]=="1"):
if(b[2]=="1"):
list1.append(int(b[0]))
else:
list2.append(int(b[0]))
else:
if(b[2]=="1"):
list3.append(int(b[0]))
if(len(list1)+min(len(list2),len(list3))<int(a[1])):
print(-1)
else:
ans = 0
ans1 = 0
ans2 = 0
list1_index = 0
list2_index = 0
list3_index = 0
list1_len = len(list1)
list2_len = len(list2)
list3_len = len(list3)
list1.sort()
list2.sort()
list3.sort()
while(ans<int(a[1]) or ans1<int(a[1])):
if(ans<int(a[1]) and ans1<int(a[1]) and list1_index<list1_len):
if(list2_len<=list2_index or list3_len<=list3_index):
tag = int(a[1])-min(ans,ans1)
ans2+=sum(list1[list1_index:list1_index+tag])
ans = int(a[1])
ans1 = int(a[1])
else:
if(list1[list1_index]>list2[list2_index]+list3[list3_index]):
ans1+=1
ans+=1
ans2+=list2[list2_index]+list3[list3_index]
list2_index+=1
list3_index+=1
else:
ans1+=1
ans+=1
ans2+=list1[list1_index]
list1_index+=1
elif(ans<int(a[1])):
if(list2_len<=list2_index):
ans+=1
ans2+=list1[list1_index]
list1_index+=1
else:
ans+=1
ans2+=list2[list2_index]
list2_index+=1
elif(ans1<int(a[1])):
if(list3_len<=list3_index):
ans1+=1
ans2+=list1[list1_index]
list1_index+=1
else:
ans1+=1
ans2+=list3[list3_index]
list3_index+=1
print(ans2)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int k = s.nextInt();
int arr[][] = new int[n][3];
PriorityQueue<Integer> p1 = new PriorityQueue<>();
PriorityQueue<Integer> p2 = new PriorityQueue<>();
int count = 0;
for(int i = 0;i<arr.length;++i)
{
arr[i][0] = s.nextInt();
arr[i][1] = s.nextInt();
arr[i][2] = s.nextInt();
if(arr[i][1] == 1 && arr[i][2] == 0)
{
p1.add(arr[i][0]);
}
else if(arr[i][1] == 0 && arr[i][2] == 1)
{
p2.add(arr[i][0]);
}
else if(arr[i][1] == 1 && arr[i][2] == 1)
{
count++;
}
}
long tmp1[] = new long[Math.min(p1.size(), p2.size())];
int tmp2[] = new int[count];
int w = 0;
for(int i = 0;i<arr.length;++i)
{
if(arr[i][1] == 1 && arr[i][2] == 1)
{
tmp2[w++] = arr[i][0];
}
}
Arrays.sort(tmp2);
int u = 0;
while(p1.size()>0 && p2.size()>0)
{
tmp1[u++]=(long)p1.poll()+p2.poll();
}
int i = 0,j = 0;
long ans = 0;
u=0;
// System.out.println(tmp1.length + " " + tmp2.length);
if(tmp1.length + tmp2.length < k)
{
System.out.println(-1);
return;
}
while(i<tmp1.length && j<tmp2.length && u<k)
{
if(tmp1[i] < tmp2[j])
{
ans+=(long)tmp1[i];
i++;
u++;
}
else
{
ans+=(long)tmp2[j];
j++;
u++;
}
}
while(i<tmp1.length && u<k)
{
ans+=(long)tmp1[i];
i++;
u++;
}
while(j<tmp2.length && u<k)
{
ans+=(long)tmp2[j];
j++;
u++;
}
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"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int const MAXN = 2e5 + 10;
int n, m, T, k;
vector<pair<int, int>> a, b, ab, other;
vector<int> ans;
int check(int mid) {
if ((int)a.size() < k - mid) return 2e9 + 1;
if ((int)b.size() < k - mid) return 2e9 + 1;
if (mid + max(0, k - mid) * 2 > m) return 2e9 + 1;
int nd = m - mid, res = 0;
ans.clear();
vector<pair<int, int>> vec;
for (int i = 0; i < mid; i++) res += ab[i].first, ans.push_back(ab[i].second);
for (int i = mid; i < ab.size(); i++) vec.push_back(ab[i]);
for (int i = 0; i < k - mid; i++)
res += a[i].first + b[i].first, nd -= 2, ans.push_back(a[i].second),
ans.push_back(b[i].second);
for (int i = max(0, k - mid); i < a.size(); i++) vec.push_back(a[i]);
for (int i = max(0, k - mid); i < b.size(); i++) vec.push_back(b[i]);
for (int i = 0; i < other.size(); i++) vec.push_back(other[i]);
sort(vec.begin(), vec.end());
for (int i = 0; i < nd && i < vec.size(); i++)
res += vec[i].first, ans.push_back(vec[i].second);
return res;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
for (int i = 1, x1, x2, x3; i <= n; ++i) {
cin >> x1 >> x2 >> x3;
if (x2 && x3)
ab.push_back({x1, i});
else if (x2)
a.push_back({x1, i});
else if (x3)
b.push_back({x1, i});
else
other.push_back({x1, i});
}
sort(a.begin(), a.end()), sort(b.begin(), b.end()),
sort(ab.begin(), ab.end()), sort(other.begin(), other.end());
int l = 0, r = min((int)ab.size(), m);
while (r - l > 10) {
int midl = l + (r - l) / 3, midr = r - (r - l) / 3;
if (check(midl) >= check(midr))
l = midl;
else
r = midr;
}
int res = 2e9 + 1, Min_idx = -1;
for (int i = l; i <= r; i++) {
int tmp = check(i);
if (tmp < res) res = tmp, Min_idx = i;
}
if (Min_idx == -1) return puts("-1"), 0;
cout << check(Min_idx) << endl;
for (auto a : ans) cout << a << " ";
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
x=[]
y=[]
z=[]
for _ in range(n):
t,a,b=map(int,input().split())
if(a==1 and b==1):
x.append(t)
elif(a==1 and b==0):
y.append(t)
elif(a==0 and b==1):
z.append(t)
x.sort()
y.sort()
z.sort()
kk=[]
p=min(len(y),len(z))
for i in range(p):
kk.append(y[i]+z[i])
if(len(kk)+len(x)<k):
print(-1)
else:
i = 0
j = 0
c = []
while (True):
if (i == len(kk)):
c.extend(x[j:])
break
if (j == len(x)):
c.extend(kk[i:])
break
if (kk[i] > x[j]):
c.append(x[j])
j += 1
elif (kk[i] == x[j]):
c.append(x[j])
c.append(x[j])
i += 1
j += 1
else:
c.append(kk[i])
i += 1
print(sum(c[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def prefixSum(array):
if len(array) == 0:
return []
l = [array[0]]
for i in array[1:]:
l.append(i+l[-1])
return l
n, k = map(int, input().split())
alice = []
bob = []
both = []
for i in range(n):
t, a, b = map(int, input().split())
if a == 1 and b == 1:
both.append(t)
continue
if a == 1:
alice.append(t)
if b == 1:
bob.append(t)
alice.sort()
bob.sort()
both.sort()
if len(alice)+len(both)<k or len(bob)+len(both)<k:
print(-1)
else:
if len(alice) == 0 or len(bob) == 0:
s = sum(both[:k])
print(s)
else:
bothP = prefixSum(both)
aliceP = prefixSum(alice)
bobP = prefixSum(bob)
minTime = 10**18
# If all k are picked from individual
if len(alice)>=k and len(bob)>=k:
minTime = aliceP[k-1] + bobP[k-1]
# If all are picked from both
if len(both) >= k:
minTime = min(minTime, bothP[k-1])
# If anywhere between 1 to k-1 are picked from both
for i in range(1, k):
# Remaining k-i books cannot be picked
if not (k-i <= len(alice) and k-i <= len(bob)):
continue
# There are not i books in both
if i>len(both):
continue
# Pick i common rest uncommon
elif bothP[i-1] + aliceP[k-i-1] + bobP[k-i-1] < minTime:
minTime = bothP[i-1] + aliceP[k-i-1] + bobP[k-i-1]
print(minTime)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = [int(x) for x in input().split()]
both = []
bob = []
alice = []
for i in range(n):
t, a, b = [int(x) for x in input().split()]
if(a==1 and b==1):
both.append(t)
elif(a==1):
alice.append(t)
elif(b==1):
bob.append(t)
both.sort()
bob.sort()
alice.sort()
y = max(k-min(len(bob),len(alice)),0)
soma_both=0
sa = 0
sb = 0
poss = (len(both)+min(len(alice),len(bob))>=k)
if(poss):
for i in range(y):
soma_both+=both[i]
for i in range(max(k-y,0)):
sa += alice[i]
for i in range(max(k-y,0)):
sb += bob[i]
ptr1 = k-y
ptr2 = k-y
resposta = sa+sb+soma_both
for i in range(y,len(both)):
ptr1-=1
ptr2-=1
if(ptr2==-1 or ptr1==-1):
break
soma_both+=both[i]
sa-=alice[ptr1]
sb-=bob[ptr2]
if(soma_both+sa+sb>=0):
resposta = min(resposta,soma_both+sa+sb)
print(resposta)
else:
print(-1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, t, a, b, it1, it2, score, tim, f, s;
vector<long long> vec[4];
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> t >> a >> b;
if (a == 1) f++;
if (b == 1) s++;
if (a == 0 && b == 0) continue;
if (a == 1 && b == 1) {
vec[1].push_back(t);
continue;
}
if (a == 1) vec[2].push_back(t);
if (b == 1) vec[3].push_back(t);
}
if (f < k || s < k) {
cout << -1 << endl;
} else {
for (int i = 1; i <= 3; i++) {
sort(vec[i].begin(), vec[i].end());
}
it1 = 0;
it2 = 0;
tim = 0;
score = 0;
while (score < k) {
if (it1 == vec[1].size()) {
tim += vec[2][it2] + vec[3][it2];
it2++;
score++;
continue;
}
if (it2 == vec[2].size() || it2 == vec[3].size()) {
tim += vec[1][it1];
score++;
it1++;
continue;
}
if (vec[1][it1] < vec[2][it2] + vec[3][it2]) {
tim += vec[1][it1];
score++;
it1++;
} else {
tim += vec[2][it2] + vec[3][it2];
it2++;
score++;
}
}
cout << tim << endl;
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483640;
const int MAXN = 2e5 + 5;
const int R = 1e4 + 5;
int n, m, k;
int tot[10];
struct node {
int t, i;
} A[MAXN], B[MAXN], C[MAXN], D[MAXN], E[MAXN << 2];
bool cmp(node x, node y) { return x.t < y.t; }
struct tree {
int sum, w;
} tr[R << 2];
void modify(int x, int l, int r, int pos, int k) {
if (l == r) {
tr[x].sum += k * l;
tr[x].w += k;
return;
}
int mid = (l + r) / 2;
if (pos <= mid) modify(x * 2, l, mid, pos, k);
if (pos > mid) modify(x * 2 + 1, mid + 1, r, pos, k);
tr[x].sum = tr[x * 2].sum + tr[x * 2 + 1].sum;
tr[x].w = tr[x * 2].w + tr[x * 2 + 1].w;
}
int query(int x, int l, int r, int kth) {
int ans = 0;
if (kth == tr[x].w) return tr[x].sum;
if (l == r) return kth * l;
int mid = (l + r) / 2;
if (tr[x * 2].w < kth)
ans = tr[x * 2].sum + query(x * 2 + 1, mid + 1, r, kth - tr[x * 2].w);
else
ans = query(x * 2, l, mid, kth);
return ans;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
int t, a, b;
scanf("%d%d%d", &t, &a, &b);
if (a == 1 && b == 1) A[++tot[1]] = (node){t, i};
if (a == 0 && b == 1) B[++tot[2]] = (node){t, i};
if (a == 1 && b == 0) C[++tot[3]] = (node){t, i};
if (a == 0 && b == 0) D[++tot[4]] = (node){t, i};
}
if (min(tot[2], tot[3]) + tot[1] < k) {
printf("-1");
return 0;
}
sort(A + 1, A + tot[1] + 1, cmp);
sort(B + 1, B + tot[2] + 1, cmp);
sort(C + 1, C + tot[3] + 1, cmp);
sort(D + 1, D + tot[4] + 1, cmp);
int cost = 0, num = 0;
for (int i = 1; i <= min(m, tot[1]); i++) cost += A[i].t, num++;
int now = 0;
for (int i = 1; i <= k - tot[1]; i++)
now++, cost += (B[now].t + C[now].t), num += 2;
for (int i = m + 1; i <= tot[1]; i++) modify(1, 1, R, A[i].t, 1);
for (int i = now + 1; i <= tot[2]; i++) modify(1, 1, R, B[i].t, 1);
for (int i = now + 1; i <= tot[3]; i++) modify(1, 1, R, C[i].t, 1);
for (int i = 1; i <= tot[4]; i++) modify(1, 1, R, D[i].t, 1);
if (num > m) {
printf("-1");
return 0;
}
int minn = INF, ans_A = 0, ans_B = 0;
for (int i = min(m, tot[1]); i >= 0; i--) {
if (num > m) break;
int ans = cost + query(1, 1, R, m - num);
if (ans < minn) minn = ans, ans_A = i, ans_B = now;
modify(1, 1, R, A[i].t, 1);
cost -= A[i].t;
num--;
if (now > min(tot[2], tot[3])) break;
if (i <= k) {
num += 2;
now++;
if (now > min(tot[2], tot[3])) break;
modify(1, 1, R, B[now].t, -1);
modify(1, 1, R, C[now].t, -1);
cost += (B[now].t + C[now].t);
}
}
int ans = 0;
printf("%d\n", minn);
for (int i = 1; i <= ans_A; i++) ans += A[i].t;
for (int i = 1; i <= ans_B; i++) ans += (B[i].t + C[i].t);
int cnt = 0;
for (int i = ans_A + 1; i <= min(m, tot[1]); i++) E[++cnt] = A[i];
for (int i = ans_B + 1; i <= tot[2]; i++) E[++cnt] = B[i];
for (int i = ans_B + 1; i <= tot[3]; i++) E[++cnt] = C[i];
for (int i = 1; i <= tot[4]; i++) E[++cnt] = D[i];
sort(E + 1, E + cnt + 1, cmp);
for (int i = 1; i <= ans_A; i++) printf("%d ", A[i].i);
for (int i = 1; i <= ans_B; i++) printf("%d %d ", B[i].i, C[i].i);
for (int i = 1; i <= m - ans_A - ans_B * 2; i++) printf("%d ", E[i].i);
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class cp1 {
public static void main(String args[]) {
int n=II();
int k=II();
int t[]=new int[n];
int a[]=new int[n];
int b[]=new int[n];
ArrayList<Integer> arr=new ArrayList<>();
ArrayList<Integer> brr=new ArrayList<>();
ArrayList<Integer> crr=new ArrayList<>();
for(int i=0;i<n;i++){
t[i]=II();
a[i]=II();
b[i]=II();
if(a[i]==1 && b[i]==1)
arr.add(t[i]);
else if(a[i]==1 && b[i]==0)
brr.add(t[i]);
else if(a[i]==0 && b[i]==1)
crr.add(t[i]);
}
Collections.sort(brr);
Collections.sort(crr);
int len1=brr.size();
int len2 = crr.size();
for(int i=0;i<len1 && i<len2; i++){
arr.add(brr.get(i)+crr.get(i));
}
int len=arr.size();
if(len<k)
System.out.println(-1);
else{
Collections.sort(arr);
int ans=0;
for(int i=0;i<k;i++){
ans=ans+arr.get(i);
}
System.out.println(ans);
}
}
////////////////////////////////////
static scan in=new scan(System.in);
static int II() {
return in.nextInt();
}
static long IL() {
return in.nextLong();
}
static int[] IIA(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=II();
}
return a;
}
static String IS() {
return in.next();
}
static char IC(){
return in.next().charAt(0);
}
static String[] ISA(int n) {
String a[]=new String[n];
for(int i=0;i<n;i++) {
a[i]=IS();
}
return a;
}
static char[] ICA(int n) {
char a[]=new char[n];
for(int i=0;i<n;i++) {
a[i]=IC();
}
return a;
}
}
class scan
{
public static BufferedReader reader;
public static StringTokenizer token;
scan(InputStream str)
{
reader=new BufferedReader(new InputStreamReader(str));
token=null;
}
static int nextInt()
{
while(token==null||!token.hasMoreTokens())
{
try { token=new StringTokenizer(reader.readLine()); }
catch(IOException e){ throw new RuntimeException(e); }
}
return Integer.parseInt(token.nextToken());
}
static long nextLong()
{
while(token==null||!token.hasMoreTokens())
{
try { token=new StringTokenizer(reader.readLine()); }
catch(IOException e){ throw new RuntimeException(e); }
}
return Long.parseLong(token.nextToken());
}
static String next()
{
while(token==null||!token.hasMoreTokens())
{
try { token=new StringTokenizer(reader.readLine()); }
catch(IOException e){ throw new RuntimeException(e); }
}
return token.nextToken();
}
}
// data = {['name':'Protien', 'id':'1234', 'brand':'muscleblaze', 'about':'fopr building muscle'....],[],[]}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
l1=[] #(0,1)
l2=[] #(1,0)
l3=[] #(1,1)
for i in range(n):
t,a,b=map(int,input().split())
if (a,b)==(1,1):
l3.append(t)
elif (a,b)==(0,1):
l1.append(t)
elif (a,b)==(1,0):
l2.append(t)
s=0
l1.sort()
l2.sort()
l3.sort()
if len(l3)>=k:
s=sum(l3[:k])
p=0
p1=k-1
else:
s=sum(l3)
v=k-len(l3)
if len(l1)>=v and len(l2)>=v:
s+=sum(l1[:v])+sum(l2[:v])
else:
print(-1)
exit()
p=v
p1=len(l3)-1
while(p<len(l1) and p<len(l2) and p1>=0):
if l1[p]+l2[p]<=l3[p1]:
s=s-l3[p1]+l1[p]+l2[p]
p1-=1
p+=1
else:
break
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"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print1d(vector<T> &a) {
for (long long int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << endl;
}
vector<long long int> divisor(long long int n) {
vector<long long int> a;
for (long long int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (n / i == i)
a.push_back(i);
else {
a.push_back(i);
a.push_back(n / i);
}
}
}
return a;
}
long long int K = 1E9;
bool sortsecond(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return a.second < b.second;
}
bool sortmake(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
if (a.first < b.first)
return true;
else if (a.first > b.first)
return false;
else {
if (a.second < b.second)
return false;
else
return true;
}
}
bool isPrime(long long int n) {
for (long long int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
long long int n, k;
cin >> n >> k;
vector<long long int> a, b, c;
long long int x, y, z;
for (long long int i = 0; i < n; i++) {
cin >> x >> y >> z;
if (y == 1 && z == 1)
a.push_back(x);
else if (y == 1)
b.push_back(x);
else if (z == 1)
c.push_back(x);
}
long long int an = a.size();
long long int bn = b.size();
long long int cn = c.size();
if (an + min(bn, cn) < k) {
cout << -1 << endl;
return 0;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
long long int dn = min(bn, cn);
long long int ans = 0;
long long int i = 0, j = 0;
for (long long int q = 0; q < k; q++) {
if (i < an && j < dn) {
if (a[i] <= b[j] + c[j]) {
ans += a[i];
i++;
} else {
ans += b[j] + c[j];
j++;
}
} else if (i >= an) {
ans += b[j] + c[j];
j++;
} else if (j >= dn) {
ans += a[i];
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"
]
}
|
CORRECT
|
python3
|
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
n,k=value()
alice=[]
bob=[]
both=[]
for i in range(n):
t,a,b=value()
if(a==1 and b==1):
both.append(t)
elif(a==1):
alice.append(t)
elif(b==1):
bob.append(t)
alice.sort()
bob.sort()
both.sort()
#print(alice)
#print(bob)
#print(both)
#print()
ans=0
if(len(bob)+len(both)<k or len(alice)+len(both)<k):
print(-1)
exit()
if(len(alice)<k):
dif=k-len(alice)
ans+=sum(both[:dif])
both=both[dif:]
k-=dif
if(len(bob)<k):
dif=k-len(bob)
ans+=sum(both[:dif])
both=both[dif:]
k-=dif
#print(alice)
#print(bob)
#print(both)
#print()
a=0
b=0
bo=0
for i in range(k):
if(bo<len(both) and alice[a]+bob[b]>both[bo]):
ans+=both[bo]
bo+=1
else:
ans+=alice[a]+bob[b]
a+=1
b+=1
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
@SuppressWarnings("unchecked")
public class Problem_E3 {
static final long INF = Long.MAX_VALUE / 2;
static class Book implements Comparable<Book> {
int t, a, b, idx;
Book(int t, int a, int b, int idx) {
this.t = t;
this.a = a;
this.b = b;
this.idx = idx;
}
public int compareTo(Book o) {
return t == o.t ? idx - o.idx : t - o.t;
}
}
static class Log {
int idx;
boolean tof;
Log(int idx, boolean tof) {
this.idx = idx;
this.tof = tof;
}
}
public static void main(String[] args) {
InputReader in = new InputReader();
StringBuilder out = new StringBuilder();
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
Book[] B = new Book[N];
for (int i = 0; i < N; i++) {
int t = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
B[i] = new Book(t, a, b, i + 1);
}
Arrays.sort(B);
List<Book>[] list = new List[4];
for (int i = 0; i < 4; i++) {
list[i] = new ArrayList<>();
}
for (int i = 0; i < N; i++) {
int type = 3;
if (B[i].a == 1 && B[i].b == 1) {
type = 0;
} else if (B[i].a == 1 && B[i].b == 0) {
type = 1;
} else if (B[i].a == 0 && B[i].b == 1) {
type = 2;
}
list[type].add(B[i]);
}
if (list[0].size() + list[1].size() < K || list[0].size() + list[2].size() < K) {
System.out.println(-1);
return;
}
long ans = INF;
long now = 0;
TreeSet<Book> set1 = new TreeSet<>();
TreeSet<Book> set2 = new TreeSet<>();
List<Log> log = new ArrayList<>();
boolean[] chk = new boolean[N + 1];
for (Book c : list[3]) {
set1.add(c);
}
for (int i = 0; i <= list[0].size() && i <= K; i++) {
if (i + list[1].size() < K || i + list[2].size() < K || 2 * K - i > M) {
continue;
}
for (int j = 0; j < i; j++) {
Book c = list[0].get(j);
now += c.t;
set2.add(c);
log.add(new Log(c.idx, true));
}
int cnt = K - i;
for (int j = 0; j < cnt; j++) {
for (int k = 1; k < 3; k++) {
Book c = list[k].get(j);
now += c.t;
set2.add(c);
log.add(new Log(c.idx, true));
}
}
for (int j = 0; j < 3; j++) {
for (int k = j == 0 ? i : cnt; k < list[j].size(); k++) {
set1.add(list[j].get(k));
}
}
for (int j = 2 * K - i; j < M; j++) {
Book c = set1.first();
now += c.t;
set2.add(c);
set1.remove(c);
log.add(new Log(c.idx, true));
}
for (Log l : log) {
chk[l.idx] = l.tof;
}
log.clear();
ans = now;
break;
}
if (ans == INF) {
System.out.println(-1);
return;
}
for (int i = 0; i < list[0].size() && i < K; i++) {
if (i + list[1].size() < K || i + list[2].size() < K || 2 * K - i > M) {
continue;
}
Book c = list[0].get(i);
if (set1.contains(c)) {
set1.remove(c);
set2.add(c);
now += c.t;
log.add(new Log(c.idx, true));
}
for (int j = 1; j < 3; j++) {
c = list[j].get(K - i - 1);
if (set2.contains(c)) {
set2.remove(c);
set1.add(c);
now -= c.t;
log.add(new Log(c.idx, false));
}
}
while (set2.size() < M) {
c = set1.first();
now += c.t;
set1.remove(c);
set2.add(c);
log.add(new Log(c.idx, true));
}
while (set2.size() > M) {
c = set2.last();
now -= c.t;
set2.remove(c);
set1.add(c);
log.add(new Log(c.idx, false));
}
if (ans > now) {
for (Log l : log) {
chk[l.idx] = l.tof;
}
log.clear();
ans = now;
}
}
if (ans == INF) {
System.out.println(-1);
return;
}
out.append(ans).append('\n');
for (int i = 1; i <= N; i++) {
if (chk[i]) {
out.append(i).append(' ');
}
}
out.setCharAt(out.length() - 1, '\n');
System.out.print(out);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer st;
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
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"
]
}
|
CORRECT
|
python3
|
# cook your dish here
#code
import math
import collections
from sys import stdin,stdout,setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2**20)
T = 1
#T = int(stdin.readline())
for _ in range(T):
#n = int(stdin.readline())
n,k = list(map(int, stdin.readline().rstrip().split()))
t,a,b = [],[],[]
sa,sb = 0,0
for i in range(n):
t1,t2,t3 = list(map(int, stdin.readline().rstrip().split()))
t.append(t1)
a.append(t2)
b.append(t3)
sa += t2
sb += t3
if(sa<k or sb<k):
print(-1)
continue
else:
on_a = []
on_b = []
on_ab = []
for i in range(n):
if(a[i]==1 and b[i]==1):
on_ab.append(t[i])
elif(a[i]==1):
on_a.append(t[i])
elif(b[i]==1):
on_b.append(t[i])
on_a.sort()
on_b.sort()
on_ab.sort()
ans = 0
ai, bi, abi = 0,0,0
cnta, cntb = 0,0
while((cnta!=k and cntb!=k) and (ai<len(on_a) and bi<len(on_b) and abi<len(on_ab)) ):
if(on_a[ai]+on_b[bi]<on_ab[abi]):
cnta += 1
cntb += 1
ans += (on_a[ai] + on_b[bi])
ai += 1
bi += 1
else:
cnta += 1
cntb += 1
ans += on_ab[abi]
abi += 1
if(cnta==k):
print(ans)
continue
elif(abi<len(on_ab)):
while(cnta!=k):
ans += on_ab[abi]
abi += 1
cnta+=1
print(ans)
continue
else:
while(cnta!=k):
ans += on_a[ai]
ai += 1
cnta+=1
while(cntb!=k):
ans += on_b[bi]
bi += 1
cntb+=1
print(ans)
continue
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
long []ins = GetInput.getLongArrayInput();
int n = (int) ins[0]; long k = ins[1];
PriorityQueue<Long> both = new PriorityQueue<>();
PriorityQueue<Long> alice = new PriorityQueue<>();
PriorityQueue<Long> bob = new PriorityQueue<>();
while(n-->0){
long []array = GetInput.getLongArrayInput();
if (array[1] == 1 && array[2] == 1){
both.add(array[0]);
continue;
}
if (array[1] == 1){
alice.add(array[0]);
continue;
}
if (array[2] == 1){
bob.add(array[0]);
continue;
}
}
long time = 0;
long books = k;
boolean flag = false;
if (both.size() + alice.size() <k || both.size() + bob.size()<k){
System.out.println(-1);
return;
}
while (books>0){
if (both.peek() != null) {
if (alice.peek()!=null && bob.peek()!=null) {
if (both.peek() < alice.peek() + bob.peek()) {
books--;
time += both.poll();
continue;
} else {
time += alice.poll() + bob.poll();
books--;
continue;
}
}
else {
time += both.poll();
books--;
continue;
}
}else {
try {
time+=bob.poll()+alice.poll();
books--;
}catch (NullPointerException e){
System.out.println(-1);
}
}
}
System.out.println(time);
}
static long fff(long key, long freq, long k){
return key+1 + (freq-1)*(k);
}
static int rec(int sum, int n){
if (n == 0){
return sum;
}
return rec(n%10+sum, n/10);
}
static int foo(int n, int r) {
if (n > 0) return (n % r + foo(n / r, r));
else return 0;
}
private static long mod(long b) {
if (b < 0) {
return -b;
}
return b;
}
private static int findEnd(int i, int n, int b) {
if ((i / b) + b > n - 1) {
return n - 1;
}
return (i / b) + b - 1;
}
static int KnapSack(int val[], int wt[],
int n, int W) {
// matrix to store final result
int dp[][] = new int[2][W + 1];
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if (wt[i - 1] <= w)
dp[i % 2][w] = Math.max(dp[(i - 1) % 2][w], val[i - 1] + dp[(i - 1) % 2][w - wt[i - 1]]);
else
dp[i % 2][w] = dp[(i - 1) % 2][w];
}
}
return dp[n % 2][W];
}
// String []arr = {"USA", "United States", "Washington", "California", "New York", "Los Angeles", "Florida", "Texas",
// "Chicago", "Boston", "San Francisco", "Seattle", "Brooklyn", "San Diego" , "Michigan", "Colorado", "Austin", "Pennsylvania", "Philadelphia", "Ohio", "New Jersey", "Carolina"};
// String []arr = {"Australia", "Sydney", "Melbourne", "Canberra"};
// String []arr = {"Canada", "Ontario", "Ottawa", "Richmond", "Vancouver", "Alberta", "Toronto", "Victoria"};
// String []arr = {"Australia", "Sydney", "Melbourne", "Canberra"};
// String []arr = {"England", "London", "UK", "Britain", "Manchester"};
// String []arr = {"India", "Delhi", "Bangalore", "Mumbai", "Chennai", "Pune", "Noida", "Gurgaon", "Kolkata"};
// String []arr = {"New Zealand", "Auckland"};
//// String []arr = {"France", "Paris"};
// File file = new File("/Users/jacksonjose/Downloads/HTTPclient-server-master/PractiseCompetitive/src/2018.csv");
// processor(arr, file);
// file = new File("/Users/jacksonjose/Downloads/HTTPclient-server-master/PractiseCompetitive/src/2019.csv");
// processor(arr, file);
// }
//
// private static void processor(String []arr, File file) throws IOException {
// Count c = grouper(arr, new Count(0,0,0), file);
// System.out.println(arr[0]);
// System.out.println("Total Count, " + (c.pos+c.neg));
// System.out.println("-1, " + c.neg);
// System.out.println("1, " + c.pos);
private static int numerOftwos(int n) {
int ans = 0;
while (n % 2 == 0) {
n /= 2;
ans++;
}
return ans;
}
public static int oddPrimeFactors(int n) {
while (n % 2 == 0) {
n /= 2;
}
int ans = 0;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
n /= i;
ans++;
}
}
if (n > 2)
ans++;
return ans;
}
private static boolean onlyPowerOf2(int n) {
if (n == 2) {
return false;
}
while (n > 1) {
if (n % 2 == 0) {
n /= 2;
} else {
return false;
}
}
return true;
}
// private static Count grouper(String []key, Count c, File file) throws IOException {
// BufferedReader br = new BufferedReader(new FileReader(file));
//
// String st;
// st = br.readLine();
// int i = 0;
// while (st!=null){
// String []strings = st.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
// if (isKeyContained(strings[0], key)){
// st = br.readLine();
// strings = st.split(",");
// if (isNumeric(strings[0])){
// int num = Integer.parseInt(strings[0]);
// if (num == -1){
// c.neg += Integer.parseInt(strings[1]);
// }
// else {
// c.pos += Integer.parseInt(strings[1]);
// }
// }
// else {
// continue;
// }
// st = br.readLine();
// strings = st.split(",");
// if (isNumeric(strings[0])){
// int num = Integer.parseInt(strings[0]);
// if (num == -1){
// c.neg += Integer.parseInt(strings[1]);
// }
// else {
// c.pos += Integer.parseInt(strings[1]);
// }
// }
// }
// st = br.readLine();
// }
// return c;
// }
// private static boolean isKeyContained(String string, String[] key) {
// for (String str : key){
// if (string.contains(str)){
// return true;
// }
// }
// return false;
// }
// private static boolean isNumeric(String str) {
// return str.matches("-?\\d+(\\.\\d+)?");
// }
private static boolean isMultiple(double i, double x) {
if (x % i == 0) {
return false;
}
return true;
}
private static int ff(long num) {
return (int) (num % (1000000007));
}
private static boolean notOverlap(long[] t1, long[] t2) {
return t1[0] >= t2[1];
}
private static long max(long in, long in1) {
if (in > in1) {
return in;
}
return in1;
}
private static long min(long in, long in1) {
if (in > in1) {
return in1;
}
return in;
}
}
class GetInput {
static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
static char[] getCharArray() {
char[] charArray;
try {
StringBuilder string = getInputString();
charArray = new char[string.length()];
for (int i = 0; i < string.length(); i++) {
charArray[i] = string.charAt(i);
}
return charArray;
} catch (Exception e) {
e.printStackTrace();
}
charArray = new char[0];
return charArray;
}
static int[] getArrayInt() {
String[] string;
int[] array;
try {
string = bufferedReader.readLine().split("\\s+");
array = new int[string.length];
for (int i = 0; i < string.length; i++) {
array[i] = Integer.parseInt(string[i]);
}
return array;
} catch (IOException e) {
e.printStackTrace();
}
int[] arra = new int[2];
return arra;
}
static int getInt() {
try {
String string = bufferedReader.readLine();
return Integer.parseInt(string);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
static StringBuilder getInputString() {
try {
StringBuilder string = new StringBuilder();
string.append(bufferedReader.readLine());
return string;
} catch (IOException e) {
e.printStackTrace();
}
return new StringBuilder();
}
static long getLongInput() {
try {
String string = bufferedReader.readLine();
return Long.parseLong(string);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
static long[] getLongArrayInput() {
String[] string;
long[] array;
try {
string = bufferedReader.readLine().split("\\s+");
array = new long[string.length];
for (int i = 0; i < string.length; i++) {
array[i] = Long.parseLong(string[i]);
}
return array;
} catch (IOException e) {
e.printStackTrace();
}
long[] arra = new long[2];
return arra;
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
class HeapSort {
void sort(long[] arr) {
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
long temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
private void heapify(long[] arr, int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
long swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, k;
struct Book {
int t, a, b;
bool operator<(const Book &w) const { return t < w.t; }
} books[N];
vector<int> a, b;
bool st[N];
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> books[i].t >> books[i].a >> books[i].b;
}
sort(books, books + n);
int cnta = 0, cntb = 0;
for (int i = 0; i < n; i++) {
int t = books[i].t, x = books[i].a, y = books[i].b;
if (!x && !y) {
continue;
} else if (x && y) {
if (cnta < k && cntb < k) {
cnta++, cntb++;
st[i] = true;
} else if (cnta < k && cntb >= k) {
cnta++;
st[i] = true;
int c = b[b.size() - 1];
b.pop_back();
st[c] = false;
} else if (cnta >= k && cntb < k) {
cntb++;
st[i] = true;
int c = a[a.size() - 1];
a.pop_back();
st[c] = false;
} else {
if (!a.size() || !b.size()) {
break;
}
int t1 = books[a[a.size() - 1]].t, t2 = books[b[b.size() - 1]].t;
if (t < t1 + t2) {
st[i] = true;
int c = b[b.size() - 1];
b.pop_back();
st[c] = false;
c = a[a.size() - 1];
a.pop_back();
st[c] = false;
} else {
break;
}
}
} else if (x && !y && cnta < k) {
cnta++;
a.push_back(i);
st[i] = true;
} else if (!x && y && cntb < k) {
cntb++;
b.push_back(i);
st[i] = true;
}
}
if (cnta < k || cntb < k) {
cout << -1 << endl;
} else {
int res = 0;
for (int i = 0; i < n; i++) {
if (st[i]) {
res += books[i].t;
}
}
cout << res << endl;
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from heapq import heappush, heappop
from sys import stdin, stdout
#
if __name__ == '__main__':
def reading_books(n, k, ta, tb, tab):
res = 0
for i in range(k):
if len(tab) == 0 and (len(ta) == 0 or len(tb) == 0):
return -1
t1 = 100000
t2 = 100000
t3 = 100000
if len(tab) > 0:
t1 = tab[0]
if len(ta) > 0:
t2 = ta[0]
if len(tb) > 0:
t3 = tb[0]
if t1 <= t2+t3:
res += heappop(tab)
else:
res += heappop(ta) + heappop(tb)
return res
n, k = map(int, stdin.readline().split())
ta = []
tb = []
tab = []
for i in range(n):
t, a, b = map(int, stdin.readline().split())
if a == 1 and b == 1:
heappush(tab, t)
elif a == 1:
heappush(ta, t)
elif b == 1:
heappush(tb, t)
res = reading_books(n, k, ta, tb, tab)
stdout.write(str(res) + '\n');
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m;
cin >> n >> m;
vector<long long int> aa, bb, cc;
vector<long long int> a(n + 5);
vector<long long int> b(n + 5);
vector<long long int> c(n + 5);
for (long long int i = 1; i <= n; i++) {
cin >> a[i] >> b[i] >> c[i];
if (b[i] && c[i])
cc.push_back(a[i]);
else if (b[i])
aa.push_back(a[i]);
else if (c[i])
bb.push_back(a[i]);
}
sort(aa.begin(), aa.end());
sort(bb.begin(), bb.end());
sort(cc.begin(), cc.end());
vector<long long int> vc1(n + 5), vc2(n + 5);
for (long long int i = 0; i <= (long long int)aa.size() - 1; i++) {
if (!i) {
int gg = 0;
} else {
vc1[i] = vc1[i - 1];
}
vc1[i] += aa[i];
}
for (long long int j = 0; j <= (long long int)bb.size() - 1; j++) {
if (!j) {
int hh = 0;
} else {
vc2[j] = vc2[j - 1];
}
vc2[j] += bb[j];
}
long long int mxx = INT_MAX, s = 0;
for (long long int k = 0; k <= cc.size(); k++) {
if (!k) {
int hg = 0;
} else {
m--;
s += cc[k - 1];
}
if (aa.size() < m) {
continue;
}
if (bb.size() < m) {
continue;
}
if (m > 0) {
mxx = min(mxx, s + vc1[m - 1] + vc2[m - 1]);
} else
mxx = min(mxx, s);
}
if (mxx != INT_MAX) {
int yy = 0;
} else
mxx = -1;
cout << mxx << endl;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from sys import stdin, stdout
import math
from collections import defaultdict, deque
n, k = map(int, stdin.readline().split())
al, bl, both = [], [], []
for _ in range(n):
t, a, b = map(int, stdin.readline().split())
if a == 0 and b == 0:
continue
if a == 1 and b == 1:
both.append(t)
elif a == 1:
al.append(t)
elif b == 1:
bl.append(t)
al, bl, both = deque(sorted(al)[:k]), deque(sorted(bl)[:k]), deque(sorted(both)[:k])
res, cnt = 0, 0
while cnt < k:
if both:
if al and bl:
if both[0] < al[0]+bl[0]:
res += both[0]
both.popleft()
cnt += 1
else:
res += al[0]+bl[0]
al.popleft()
bl.popleft()
cnt += 1
else:
res += both[0]
both.popleft()
cnt += 1
else:
if al and bl:
res += al[0]+bl[0]
al.popleft()
bl.popleft()
cnt += 1
else:
res = -1
break
print(res)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (typename vector<T>::const_iterator vi = v.begin(); vi != v.end(); ++vi) {
if (vi != v.begin()) os << ", ";
os << *vi;
}
os << "}";
return os;
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
os << '(' << p.first << ", " << p.second << ')';
return os;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long T, N, M, K;
long long qtd11, minAns;
vector<pair<int, int> > v[4];
int nums[200010];
int menor(vector<int> &ptr) {
pair<int, int> ans(0x3f3f3f3f, 0x3f3f3f3f);
int pos = -1;
for (int i = 0; i < 4; i++) {
if (ptr[i] < v[i].size() && ans >= v[i][ptr[i]]) {
ans = v[i][ptr[i]];
pos = i;
}
}
return pos;
}
void imp() {
cout << -1 << "\n";
exit(0);
}
pair<int, int> p(pair<int, int> a) {
return pair<int, int>(-a.first, -a.second);
}
void melhor() {
for (int i = 0; i < 4; i++) sort(v[i].begin(), v[i].end());
vector<pair<int, int> > resp[4];
vector<int> ptr(4, 0);
long long ans = 0, l11 = max(0LL, 2 * M - K), l01;
l01 = M - l11;
if (v[0].size() < l11) imp();
for (; ptr[0] < l11; ptr[0]++) {
resp[0].push_back(v[0][ptr[0]]);
ans += v[0][ptr[0]].first;
}
while (ptr[0] < v[0].size() && (v[1].size() < l01 || v[2].size() < l01)) {
l01--;
resp[0].push_back(v[0][ptr[0]]);
ans += v[0][ptr[0]].first;
ptr[0]++;
}
while (ptr[0] < v[0].size() &&
ptr[0] + v[1].size() + v[2].size() + v[3].size() < K) {
l01--;
resp[0].push_back(v[0][ptr[0]]);
ans += v[0][ptr[0]].first;
ptr[0]++;
}
if (v[1].size() < l01 || v[2].size() < l01) imp();
for (; ptr[1] < l01; ptr[1]++, ptr[2]++) {
resp[1].push_back(v[1][ptr[1]]);
resp[2].push_back(v[2][ptr[2]]);
ans += v[1][ptr[1]].first;
ans += v[2][ptr[2]].first;
}
priority_queue<pair<int, int> > sob, others;
for (int i = 1; i < 4; i++) {
for (int j = ptr[i]; j < v[i].size(); j++) {
sob.push(p(v[i][j]));
}
}
while (!sob.empty() && ptr[0] + ptr[1] + ptr[2] + others.size() < K) {
ans += -sob.top().first;
others.push(p(sob.top()));
sob.pop();
}
minAns = ans;
qtd11 = ptr[0];
for (; ptr[1] && ptr[0] < v[0].size(); ptr[0]++) {
ptr[1]--, ptr[2]--;
sob.push(p(resp[1][ptr[1]]));
ans -= resp[1][ptr[1]].first;
resp[1].pop_back();
sob.push(p(resp[2][ptr[2]]));
ans -= resp[2][ptr[2]].first;
resp[2].pop_back();
resp[0].push_back(v[0][ptr[0]]);
ans += v[0][ptr[0]].first;
pair<int, int> u = p(sob.top());
sob.pop();
others.push(u);
ans += u.first;
while (others.top() > p(sob.top())) {
pair<int, int> aux = others.top();
pair<int, int> aux2 = p(sob.top());
ans -= aux.first;
ans += aux2.first;
others.pop();
sob.pop();
others.push(aux2);
sob.push(p(aux));
}
if (ans < minAns) {
qtd11 = resp[0].size();
minAns = ans;
}
}
}
void construir() {
vector<pair<int, int> > resp;
vector<int> ptr(4, 0);
long long ans = 0;
for (; ptr[0] < qtd11; ptr[0]++) {
resp.push_back(v[0][ptr[0]]);
ans += v[0][ptr[0]].first;
}
for (int i = qtd11; i < M; i++, ptr[1]++, ptr[2]++) {
resp.push_back(v[1][ptr[1]]);
resp.push_back(v[2][ptr[2]]);
ans += v[1][ptr[1]].first;
ans += v[2][ptr[2]].first;
}
while (resp.size() < K) {
int pos = menor(ptr);
ans += v[pos][ptr[pos]].first;
resp.push_back(v[pos][ptr[pos]]);
ptr[pos]++;
}
cout << ans << "\n";
for (int i = 0; i < resp.size(); i++) {
if (!i)
cout << resp[i].second + 1;
else
cout << " " << resp[i].second + 1;
}
cout << "\n";
}
void solve() {
melhor();
construir();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> N >> K >> M;
for (int i = 0; i < N; i++) {
int a, b, t;
cin >> t >> a >> b;
if (a && b) {
v[0].push_back(pair<int, int>(t, i));
} else if (a) {
v[1].push_back(pair<int, int>(t, i));
} else if (b) {
v[2].push_back(pair<int, int>(t, i));
} else {
v[3].push_back(pair<int, int>(t, i));
}
nums[i] = t;
}
solve();
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void print(vector<long long> a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << ' ';
}
cout << endl;
}
vector<long long> inp(int n) {
vector<long long> a;
long long x;
for (int i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
return a;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t, n, i, j, k, l, c1, cnt, flag, m1, m, m2, maxi, mini, x, y, z;
vector<long long> a, b, c;
string s, s1, s2;
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> x >> y >> z;
if (y + z == 2) {
c.push_back(x);
} else {
if (y == 1) {
a.push_back(x);
}
if (z == 1) {
b.push_back(x);
}
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
cnt = 0;
if (c.size() + a.size() >= k && c.size() + b.size() >= k) {
i = 0, j = 0, l = 0;
while (i != k) {
if (j < a.size() && j < b.size()) {
if (l < c.size()) {
if (a[j] + b[j] < c[l]) {
cnt = cnt + a[j] + b[j];
j++;
} else {
cnt = cnt + c[l];
l++;
}
} else {
cnt = cnt + a[j] + b[j];
j++;
}
} else {
cnt = cnt + c[l];
l++;
}
i++;
}
cout << cnt << 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"
]
}
|
CORRECT
|
python3
|
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# import numpy as np
sys.setrecursionlimit(int(pow(10,6)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("out.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
# @lru_cache(None)
t=1
# t=int(input())
for _ in range(t):
n,k=l()
A=[[],[],[]]
bc=0
ac=0
for i in range(n):
a,b,c=l()
if b==c and b==1:
A[0].append(a)
elif b==1 and c==0:
A[1].append(a)
elif b==0 and c==1:
A[2].append(a)
A[0].sort()
A[1].sort()
A[2].sort()
for i in range(1,len(A[0])):
A[0][i]+=A[0][i-1]
for i in range(1,len(A[1])):
A[1][i]+=A[1][i-1]
for i in range(1,len(A[2])):
A[2][i]+=A[2][i-1]
time=10**10
A[0]=[0]+A[0]
A[1]=[0]+A[1]
A[2]=[0]+A[2]
# print(*A)
for cnt in range(len(A[0])):
try:
time=min(time,A[0][cnt]+A[1][k-cnt]+A[2][k-cnt])
# print(cnt,time)
except:
continue
if time==10**10:
print(-1)
else:
print(time)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.readline
from math import ceil
(n, k) = map(int, input().split())
bob = []
alice = []
good = []
for i in range(n):
(t, a, b) = map(int, input().split())
if a == 1:
if b == 1:
good.append(t)
else:
alice.append(t)
else:
if b == 1:
bob.append(t)
alice.sort()
bob.sort()
good.sort()
ma = len(alice); mb = len(bob); mg = len(good)
ka = k; kb = k
T = 0
ia = 0; ib = 0; ig = 0
while ka > 0 and kb > 0 and ig < mg and ia < ma and ib < mb:
if good[ig] <= alice[ia] + bob[ib]:
T += good[ig]
ig += 1
else:
T += alice[ia] + bob[ib]
ia += 1
ib += 1
ka -= 1
kb -= 1
if ia == ma:
while ka > 0 and ig < mg:
T += good[ig]
ig += 1
kb -= 1
ka -= 1
elif ib == mb:
while kb > 0 and ig < mg:
T += good[ib]
ib += 1
ka -= 1
kb -= 1
else:
while ka > 0 and ia < ma:
T += alice[ia]
ia += 1
ka -= 1
while kb > 0 and ib < mb:
T += bob[ib]
ib += 1
kb -= 1
if ka > 0 or kb > 0:
print(-1)
else:
print(T)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def main():
n, k = [int(x) for x in input().split()]
alice, bob = 0, 0
both, j_a, j_b = [], [], []
for i in range(n):
b = [int(x) for x in input().split()]
if(b[1] + b[2] == 2):
both.append(b)
elif (b[1] == 1):
j_a.append(b)
elif(b[2] == 1):
j_b.append(b)
j_a.sort()
j_b.sort()
for i in range(min(len(j_a), len(j_b))):
b = [j_a[i][0] + j_b[i][0], 1, 1]
both.append(b)
if(len(both) < k):
return -1
both.sort()
return sum([b[0] for b in both[:k]])
print(main())
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from collections import deque
from operator import itemgetter
from sys import stdin, stdout
int_in = lambda: int(stdin.readline())
arr_in = lambda: [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda: stdin.readline().strip()
out = lambda o: stdout.write("{}\n".format(o))
arr_out = lambda o: out(" ".join(map(str, o)))
bool_out = lambda o: out("YES" if o else "NO")
tests = lambda: range(1, int_in() + 1)
case_out = lambda i, o: out("Case #{}: {}".format(i, o))
def solve(n, k, books):
alice_books = list(sorted(filter(lambda x: x[1] == 1 and x[2] == 0, books), key=itemgetter(0)))[:k]
bob_books = list(sorted(filter(lambda x: x[1] == 0 and x[2] == 1, books), key=itemgetter(0)))[:k]
shared_books = list(sorted(filter(lambda x: x[1] == 1 and x[2] == 1, books), key=itemgetter(0)))[:k]
have = min(len(alice_books), len(bob_books))
need = k - have
using_shared_books = []
if need > 0:
if len(shared_books) < need:
return -1
alice_books = alice_books[:have]
bob_books = bob_books[:have]
using_shared_books = shared_books[:need]
shared_books = shared_books[need:]
shared_books = deque(shared_books)
if have > 0:
while len(alice_books) > 0 and len(bob_books) > 0 and len(shared_books) > 0 and alice_books[-1][0] + bob_books[-1][0] > shared_books[0][0]:
using_shared_books.append(shared_books[0])
alice_books.pop()
bob_books.pop()
shared_books.popleft()
if len(alice_books) + len(using_shared_books) != k:
return -1
if len(bob_books) + len(using_shared_books) != k:
return -1
result = 0
for item in alice_books:
result += item[0]
for item in bob_books:
result += item[0]
for item in using_shared_books:
result += item[0]
return result
if __name__ == "__main__":
n, k = arr_in()
books = mat_in(n)
out(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"
]
}
|
CORRECT
|
python3
|
# cook your dish here
n,k=map(int,input().split())
a=[]
b=[]
bb=[]
x=[]
cnta=0
cntb=0
for i in range(n):
t,p,q=list(map(int,input().split()))
t=[t,p,q]
x.append(t)
t,p,q=x[i]
if p and q:
bb.append(t)
cnta+=1
cntb+=1
elif p:
a.append(t)
cnta+=1
elif q:
b.append(t)
cntb+=1
a.sort()
b.sort()
if cnta>=k and cntb>=k:
for i in range(min(len(a),len(b))):
bb.append(a[i]+b[i])
out=0
bb.sort()
for i in range(k):
out+=bb[i]
print(out)
else:
print(-1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.List;
import java.io.InputStreamReader;
public class cf1374E1{
public static void main(String[] args) throws Exception{
StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
int n = readInt(cin);
int k = readInt(cin);
List<Integer> a = new ArrayList<Integer>();
List<Integer> b = new ArrayList<Integer>();
List<Integer> ab = new ArrayList<Integer>();
for (int i = 1;i <= n;i++){
int t = readInt(cin);
int aa = readInt(cin);
int bb = readInt(cin);
if (aa == 1 && bb == 1) ab.add(t);
else if (aa == 1) a.add(t);
else if (bb == 1) b.add(t);
}
ab.sort(null);
a.sort(null);
b.sort(null);
int sum_ab = 0,sum_a = 0,sum_b = 0;
int index_a = a.size(), index_b = b.size();
for (int i = 0;i < a.size();i++)
sum_a += a.get(i);
for (int i = 0;i < b.size();i++)
sum_b += b.get(i);
while (index_a > k){
index_a--;
sum_a -= a.get(index_a);
}
while (index_b > k){
index_b--;
sum_b -= b.get(index_b);
}
int ans = sum_a+sum_b;
boolean flag = (index_a == k) && (index_b == k);
for (int i = 0;i < ab.size();i++){
sum_ab += ab.get(i);
if (index_a > 0 && index_a + i + 1 > k) {
index_a--;
sum_a -= a.get(index_a);
}
if (index_b > 0 && index_b + i + 1 > k) {
index_b--;
sum_b -= b.get(index_b);
}
if (index_a+i+1 == k && index_b+i+1 == k){
if (flag) ans = Math.min(ans,sum_a+sum_b+sum_ab);
else {
ans = sum_a+sum_b+sum_ab;
flag = true;
}
}
}
if (!flag) ans = -1;
System.out.println(ans);
}
private static int readInt(StreamTokenizer cin)throws Exception{
cin.nextToken();
return (int)cin.nval;
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
//created by Whiplash99
import java.io.*;
import java.util.*;
public class E
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,N;
String s[]=br.readLine().trim().split(" ");
N=Integer.parseInt(s[0]);
int K=Integer.parseInt(s[1]);
int a=0,b=0;
PriorityQueue<Integer> both=new PriorityQueue<>();
PriorityQueue<Integer> alice=new PriorityQueue<>();
PriorityQueue<Integer> bob=new PriorityQueue<>();
for(i=0;i<N;i++)
{
s=br.readLine().trim().split(" ");
int t=Integer.parseInt(s[0]);
int u=Integer.parseInt(s[1]);
int v=Integer.parseInt(s[2]);
if(u+v==0) continue;
if(u+v==2)
{
a++;
b++;
both.add(t);
}
else if(u==1)
{
a++;
alice.add(t);
}
else
{
b++;
bob.add(t);
}
}
if(a<K||b<K)
{
System.out.println(-1);
System.exit(0);
}
i=K; int j=K;
long ans=0;
while (i>0&&j>0&&!both.isEmpty())
{
if(alice.size()<i||bob.size()<j)
{
ans+=both.poll();
i--;
j--;
continue;
}
int tmp=both.peek();
int tmp2=alice.peek();
int tmp3=bob.peek();
if(tmp2+tmp3<tmp) ans+=alice.poll()+bob.poll();
else ans+=both.poll();
i--;
j--;
}
while (i>0)
{
if(both.isEmpty()) ans+=alice.poll();
else if(alice.isEmpty())
{
ans += both.poll();
j--;
}
else if(alice.peek()<both.peek()) ans+=alice.poll();
else
{
ans += both.poll();
j--;
}
i--;
}
while (j>0)
{
if(both.isEmpty()) ans+=bob.poll();
else if(bob.isEmpty()) ans+=both.poll();
else if(bob.peek()<both.peek()) ans+=bob.poll();
else ans+=both.poll();
j--;
}
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"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void test_case() {
long long int n, k;
cin >> n >> k;
vector<long long int> as, bs, cs;
for (long long int i = 0; i < n; i++) {
long long int t, a, b;
cin >> t >> a >> b;
if (a + b == 2)
cs.push_back(t);
else if (a == 1)
as.push_back(t);
else if (b == 1)
bs.push_back(t);
}
if (as.size() + cs.size() < k) {
cout << -1;
return;
}
if (bs.size() + cs.size() < k) {
cout << -1;
return;
}
sort(as.begin(), as.end());
sort(bs.begin(), bs.end());
sort(cs.begin(), cs.end());
long long int a, b, c, done = 0, res = 0;
a = b = c = 0;
while (c < cs.size() && c < k) res += cs[c++];
while (a + c < k) res += as[a++];
while (b + c < k) res += bs[b++];
while (a < as.size() && b < bs.size() && c > 0 && as[a] + bs[b] <= cs[c - 1])
res += (as[a++] + bs[b++] - cs[--c]);
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
for (long long int i = 0; i < t; i++) {
test_case();
cout << endl;
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
arr=[int(x) for x in input().split()]
size=arr[0]
total=arr[1]
book_dict={'2':[],'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['2'].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'])
count2=len(book_dict['2'])
result=0
book_dict['10'].sort()
book_dict['01'].sort()
if count10+count2>=total and count01+count2>=total:
for i in range(min(count01,count10)):
book_dict['2'].append(book_dict['01'][i]+book_dict['10'][i])
book_dict['2'].sort()
result=sum(book_dict['2'][:total])
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"
]
}
|
CORRECT
|
python3
|
def p(x):
return x[0]
n, k = map(int, input().split())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
a.sort(key=p)
# print()
# print()
# for i in a:
# print(i)
alice, bob, common = [], [], []
al, bo = 0, 0
flag = 1
for i in a:
if(i[1] and not i[2] and al < k):
alice.append(i[0])
al += 1
if(i[2] and (not i[1]) and bo < k):
bob.append(i[0])
bo += 1
if(i[1] and i[2]):
if(al<k or bo<k):
common.append(i[0])
al += 1
bo += 1
if(al > k ):
alice.pop()
al -= 1
if(bo > k):
bob.pop()
bo -= 1
else:
if(alice and bob):
if(alice[-1]+bob[-1] > i[0]):
alice.pop()
bob.pop()
common.append(i[0])
else:
break
# print(alice, bob, common, al, bo)
if(al >= k and bo >= k):
print(sum(alice)+sum(bob)+sum(common))
else:
print(-1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
function sortFunc(x, y) {
return x - y;
}
function solve() {
var nk = read().split(' ').map(Number);
var n = nk[0];
var k = nk[1];
var tboth = [];
var ta = [];
var tb = [];
var ca = 0;
var cb = 0;
for (var i = 0; i < n; i++) {
var tab = read().split(' ').map(Number);
var cCase = tab[2] * 2 + tab[1];
if (cCase === 1) {
ca++;
ta.push(tab[0]);
}
if (cCase === 2) {
cb++;
tb.push(tab[0]);
}
if (cCase === 3) {
ca++;
cb++;
tboth.push(tab[0]);
}
}
ta.sort(sortFunc);
tb.sort(sortFunc);
tboth.sort(sortFunc);
if (ca < k || cb < k) {
write(-1);
return;
}
var ans = 0;
var ia = 0;
var na = ta.length;
var ib = 0;
var nb = tb.length;
var iboth = 0;
var nboth = tboth.length;
for (var i = 0; i < k; i++) {
if (ia >= na || ib >= nb) {
ans += tboth[iboth++];
continue;
}
if (iboth >= nboth) {
ans += ta[ia++] + tb[ib++];
continue;
}
if (ta[ia] + tb[ib] < tboth[iboth]) {
ans += ta[ia++] + tb[ib++];
} else {
ans += tboth[iboth++];
}
}
write(ans);
}
function init() {
var T;
T = 1;
//T = Number(read());
for (var t = 0; t < T; t++) {
solve();
}
}
var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
var fs = require('fs');
var path = require('path');
var inTextName;
var outTextName;
if (process.argv.length === 5) {
inTextName = process.argv[3];
outTextName = process.argv[4];
}
if (inTextName) {
fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
MEMORY = data.split('\r\n');
init();
});
} else {
var RL = require('readline').createInterface({
input : process.stdin,
output : process.stdout
});
RL.on('line', (line) => {
MEMORY.push(line);
});
RL.on('close', init);
}
} else {
init();
}
function write(value) {
isNode ? console.log(value) : print(value);
}
function read() {
return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long inf = 1e9;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
vector<long long> l;
vector<long long> m;
vector<long long> r;
for (long long i = 0; i < n; ++i) {
long long a, b, c;
cin >> a >> b >> c;
if (b == 1 && c == 1) {
m.push_back(a);
} else if (b == 1 && c == 0) {
l.push_back(a);
} else if (b == 0 && c == 1) {
r.push_back(a);
}
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
sort(m.begin(), m.end());
auto it1 = l.begin();
auto it2 = m.begin();
auto it3 = r.begin();
long long ans = 0;
long long x = 0, y = 0;
while ((x < k && y < k) &&
(((it1 != l.end()) && (it3 != r.end())) || (it2 != m.end()))) {
if ((it2 == m.end()) ||
(it1 != l.end() && it3 != r.end() && *it1 + *it3 < *it2)) {
ans += (*it1 + *it3);
it1++;
it3++;
} else if ((it2 != m.end()) || ((it1 == l.end() || (it3 == r.end())))) {
ans += *it2;
it2++;
}
x++;
y++;
}
if (x == k && y == k)
cout << ans << "\n";
else
cout << -1 << "\n";
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
# JAI SHREE RAM
import math; from collections import *
import sys; from functools import reduce
def get_ints(): return map(int, input().strip().split())
def get_list(): return list(get_ints())
def getlistofstring(): return list(input().strip().split())
def printxsp(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
UGLYMOD = int(1e9)+7; SEXYMOD = 998244353; MAXN = int(1e5)
# sys.setrecursionlimit(10**6)
# sys.stdin=open("input.txt","r");sys.stdout=open("output.txt","w")
# import bisect
# for _testcases_ in range(int(input())):
alice =[]; bob = []; both = [];
n , k = get_ints()
for i in range(n):
t, a, b = get_ints()
if a & b:
both.append(t)
elif a == 1:
alice.append(t)
elif b == 1:
bob.append(t)
if len(both) + min(len(alice), len(bob)) < k:
ans = -1
else:
bob = deque(sorted(bob))
alice = deque(sorted(alice))
both = deque(sorted(both))
ans = 0
while k:
try: X = both[0]
except: X = float('inf')
try: A = alice[0]
except: A = float('inf')
try: B = bob[0]
except: B = float('inf')
# print(alice, bob, both)
# print(A, B, X)
if (A == float('inf')) or (B == float('inf')):
ans += X
both.popleft()
elif X == float('inf'):
ans += A + B
alice.popleft()
bob.popleft()
elif A + B < X:
ans += A + B
alice.popleft()
bob.popleft()
else:
ans += X
both.popleft()
k -= 1
print(ans)
'''
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
THE LOGIC AND APPROACH IS MINE @luctivud ( UDIT GUPTA )
Link may be copy-pasted here if it's taken from other source.
DO NOT PLAGIARISE.
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
'''
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
alice = []
bob=[]
comm=[]
for _ in range(n):
a,b,c=map(int,input().split())
if(b==1 and c==1):
comm.append(a)
elif(b==1 and c==0):
alice.append(a)
elif(b==0 and c==1):
bob.append(a)
alice.sort()
bob.sort()
for i in range(min(len(alice),len(bob))):
comm.append(alice[i]+bob[i])
comm.sort()
if(len(comm) < k):
print(-1)
else:
print(sum(comm[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.List;
public class Main {
private static final String NO = "NO";
private static final String YES = "YES";
InputStream is;
PrintWriter out;
String INPUT = "";
private List<Integer>[] g;
private static final long MOD = 1000000007;
void solve() {
int T = 1;
for (int i = 0; i < T; i++)
solve(i);
}
void solve(int T) {
int n = ni();
int m = ni();
int k = ni();
List<int[]> e[] = new List[4];
for (int i = 0; i < 4; i++)
e[i] = new ArrayList<int[]>();
for (int i = 1; i <= n; i++) {
int t = ni();
e[ni() + ni() * 2].add(new int[] { t, i });
}
Comparator<int[]> c = new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
};
for (int i = 0; i < 4; i++)
Collections.sort(e[i], c);
long ans = Long.MAX_VALUE, nans = 0;
for (int i[] : e[3])
nans += i[0];
int sz[] = null;
int nsz[] = new int[4];
nsz[3] = e[3].size();
for (int i = e[3].size(); i >= 0; i--) {
while (nsz[1] + nsz[3] < k) {
if (nsz[1] >= e[1].size())
break;
nans += e[1].get(nsz[1]++)[0];
}
while (nsz[2] + nsz[3] < k) {
if (nsz[2] >= e[2].size())
break;
nans += e[2].get(nsz[2]++)[0];
}
int rest = m - nsz[0] - nsz[1] - nsz[2] - nsz[3];
if (rest < 0 && nsz[0] > 0) {
nans -= e[0].get(--nsz[0])[0];
rest++;
}
while (rest > 0) {
int min_p = -1;
for (int j = 0; j < 3; j++) {
if (nsz[j] < e[j].size() && (min_p == -1 || e[j].get(nsz[j])[0] < e[min_p].get(nsz[min_p])[0]))
min_p = j;
}
if (min_p == -1)
break;
nans += e[min_p].get(nsz[min_p]++)[0];
rest--;
}
if (rest == 0 && nsz[1] + nsz[3] >= k && nsz[2] + nsz[3] >= k) {
if (ans > nans) {
ans = nans;
sz = nsz.clone();
}
}
if (i > 0)
nans -= e[3].get(--nsz[3])[0];
}
if (ans == Long.MAX_VALUE)
out.print(-1);
else {
out.println(ans);
for (int i = 0; i < 4; i++)
for (int j = 0; j < sz[i]; j++)
out.print(e[i].get(j)[1] + " ");
}
}
// a^b
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private boolean vis[];
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private List<Integer> na2(int n) {
List<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
a.add(ni());
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[][] nl(int n, int m) {
long[][] a = new long[n][];
for (int i = 0; i < n; i++)
a[i] = nl(m);
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public class Pair<K, V> {
/**
* Key of this <code>Pair</code>.
*/
private K key;
/**
* Gets the key for this pair.
*
* @return key for this pair
*/
public K getKey() {
return key;
}
/**
* Value of this this <code>Pair</code>.
*/
private V value;
/**
* Gets the value for this pair.
*
* @return value for this pair
*/
public V getValue() {
return value;
}
/**
* Creates a new pair
*
* @param key The key for this pair
* @param value The value to use for this pair
*/
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
/**
* <p>
* <code>String</code> representation of this <code>Pair</code>.
* </p>
*
* <p>
* The default name/value delimiter '=' is always used.
* </p>
*
* @return <code>String</code> representation of this <code>Pair</code>
*/
@Override
public String toString() {
return key + "=" + value;
}
/**
* <p>
* Generate a hash code for this <code>Pair</code>.
* </p>
*
* <p>
* The hash code is calculated using both the name and the value of the
* <code>Pair</code>.
* </p>
*
* @return hash code for this <code>Pair</code>
*/
@Override
public int hashCode() {
// name's hashCode is multiplied by an arbitrary prime number (13)
// in order to make sure there is a difference in the hashCode between
// these two parameters:
// name: a value: aa
// name: aa value: a
return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
}
/**
* <p>
* Test this <code>Pair</code> for equality with another <code>Object</code>.
* </p>
*
* <p>
* If the <code>Object</code> to be tested is not a <code>Pair</code> or is
* <code>null</code>, then this method returns <code>false</code>.
* </p>
*
* <p>
* Two <code>Pair</code>s are considered equal if and only if both the names and
* values are equal.
* </p>
*
* @param o the <code>Object</code> to test for equality with this
* <code>Pair</code>
* @return <code>true</code> if the given <code>Object</code> is equal to this
* <code>Pair</code> else <code>false</code>
*/
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof Pair) {
Pair pair = (Pair) o;
if (key != null ? !key.equals(pair.key) : pair.key != null)
return false;
if (value != null ? !value.equals(pair.value) : pair.value != null)
return false;
return true;
}
return false;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from sys import stdin
from collections import defaultdict as dd
from collections import deque as dq
import itertools as it
from math import sqrt, log, log2
from fractions import Fraction
n, k = map(int, input().split())
alike, blike = 0, 0
zero_one, one_zero, one_one, zero_zero = [], [], [], []
for i in range(n):
t, a, b = map(int, input().split())
alike += a
blike += b
if a == 0 and b == 1:
zero_one.append((t, i))
elif a == 1 and b == 0:
one_zero.append((t, i))
elif a== 1 and b== 1:
one_one.append((t, i))
else:
pass
# zero_zero.append((t, i))
if alike < k or blike < k:
print(-1)
exit()
zero_one.sort(key = lambda x: x[0])
one_one.sort(key = lambda x: x[0])
one_zero.sort(key = lambda x: x[0])
# zero_zero.sort(key = lambda x: x[0])
alike, blike = 0, 0
zo, oo, oz, zz = 0, 0, 0, 0
lzo, loo, loz = len(zero_one), len(one_one), len(one_zero)
# lzo, loo, loz, lzz = len(zero_one), len(one_one), len(one_zero), len(zero_zero)
tottime = 0
while alike<k or blike<k:
# if oo >= loo and zo >= lzo and oz>=loz:
# break
# lbo = len(books)
# if lbo == m:
if alike <k and blike <k:
if oo>= loo and zo>=lzo and oz>=loz:
print(-1)
exit()
elif zo >= lzo or oz >= loz:
tottime += one_one[oo][0]
# books.append(one_one[oo][1])
oo += 1
elif oo >= loo:
tottime += zero_one[zo][0] + one_zero[oz][0]
# books.append(zero_one[zo][1])
# books.append(zero_one[oz][1])
zo += 1
oz += 1
elif zero_one[zo][0] + one_zero[oz][0] < one_one[oo][0]:
tottime += zero_one[zo][0] + one_zero[oz][0]
# books.append(zero_one[zo][1])
# books.append(zero_one[oz][1])
zo += 1
oz += 1
else:
tottime += one_one[oo][0]
# books.append(one_one[oo][1])
oo += 1
alike += 1
blike += 1
elif alike == k and blike < k:
if oo>=loo and zo>=lzo:
print(-1)
exit()
elif oo >= loo:
tottime += zero_one[zo][0]
# books.append(zero_one[zo][1])
zo += 1
elif zo >= lzo:
tottime += one_one[oo][0]
# books.append(one_one[oo][1])
oo += 1
elif zero_one[zo][0] < one_one[oo][0]:
tottime += zero_one[zo][0]
# books.append(zero_one[zo][1])
zo += 1
else:
tottime += one_one[oo][0]
# books.append(one_one[oo][1])
oo += 1
blike += 1
elif alike <k and blike == k:
if oo>=loo and oz>=loz:
print(-1)
exit()
elif oo>=loo:
tottime += one_zero[oz][0]
# books.append(one_zero[oz][1])
oz += 1
elif oz>= loz:
tottime += one_one[oo][0]
# books.append(one_one[oo][1])
oo += 1
elif one_zero[oz][0] < one_one[oo][0]:
tottime += one_zero[oz][0]
# books.append(one_zero[oz][1])
oz += 1
else:
tottime += one_one[oo][0]
# books.append(one_one[oo][1])
oo += 1
print(tottime)
# print(*books)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1374e1 {
public static void main(String[] args) throws IOException {
int n = rni(), k = ni(), nboth = 0, na = 0, nb = 0, cnta = 0, cntb = 0;
List<Integer> both = new ArrayList<>(), a = new ArrayList<>(), b = new ArrayList<>();
for(int i = 0; i < n; ++i) {
int t = rni(), ai = ni(), bi = ni();
cnta += ai;
cntb += bi;
if(ai == 1 && bi == 1) {
both.add(t);
} else if(ai == 1) {
a.add(t);
} else if(bi == 1) {
b.add(t);
}
}
nboth = both.size();
na = a.size();
nb = b.size();
if(cnta < k || cntb < k) {
prln(-1);
} else {
long ans = 0;
Collections.sort(both);
Collections.sort(a);
Collections.sort(b);
int pboth = 0, pa = 0, pb = 0;
for(int i = 0; i < k; ++i) {
if(pboth < nboth && (pa == na || pb == nb || both.get(pboth) <= a.get(pa) + b.get(pb))) {
ans += both.get(pboth++);
} else {
ans += a.get(pa++) + b.get(pb++);
}
}
prln(ans);
}
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random rand = new Random();
// references
// IBIG = 1e9 + 7
// IRAND ~= 3e8
// IMAX ~= 2e10
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IRAND = 327859546;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int floori(double d) {return (int)d;}
static int ceili(double d) {return (int)ceil(d);}
static long floorl(double d) {return (long)d;}
static long ceill(double d) {return (long)ceil(d);}
static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;}
// input
static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
static long rl() throws IOException {return Long.parseLong(__in.readLine());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
static int ni() {return Integer.parseInt(input.nextToken());}
static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
static long nl() {return Long.parseLong(input.nextToken());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {__out.println("yes");}
static void pry() {__out.println("Yes");}
static void prY() {__out.println("YES");}
static void prno() {__out.println("no");}
static void prn() {__out.println("No");}
static void prN() {__out.println("NO");}
static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());}
static void h() {__out.println("hlfd");}
static void flush() {__out.flush();}
static void close() {__out.close();}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
import static java.lang.Math.*;
public class R653_Solution_5 implements Runnable {
private boolean console=false;
private long MOD = 1000_000_007L;
private int MAX = 1000_001;
private void solve1(){
int n=in.ni(),k=in.ni();
ArrayList<Long> one = new ArrayList<>();
ArrayList<Long> two = new ArrayList<>();
ArrayList<Long> all = new ArrayList<>();
for(int i=0;i<n;++i){
long num = in.nl();
long a=in.ni(),b=in.ni();
if(a==1 && b==1){
all.add(num);
}else if(a==1){
one.add(num);
}else if(b==1) {
two.add(num);
}
}
Collections.sort(one);
Collections.sort(two);
long ans=0;
for(int i=0;i<min(one.size(),two.size());++i){
all.add(one.get(i)+two.get(i));
}
Collections.sort(all);
if(all.size()<k){
out.printLn(-1);
return;
}
for(int i=0;i<k;++i){
ans+=all.get(i);
}
out.printLn(ans);
}
private void solve() {
int testCases = 1;
// testCases = in.ni();
while (testCases-->0){
solve1();
}
}
private void add(TreeMap<Long, Long> map, long key){
map.put(key,map.getOrDefault(key,0L)+1);
}
private void remove(TreeMap<Long, Long> map, Long key){
if(!map.containsKey(key))
return;
map.put(key,map.getOrDefault(key,0L)-1);
if(map.get(key)==0)
map.remove(key);
}
@Override
public void run() {
long time = System.currentTimeMillis();
try {
init();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
solve();
out.flush();
System.err.println(System.currentTimeMillis()-time);
System.exit(0);
}catch (Exception e){
e.printStackTrace(); System.exit(1);
}
}
/* -------------------- Templates and Input Classes -------------------------------*/
private FastInput in;
private FastOutput out;
public static void main(String[] args) throws Exception {
new R653_Solution_5().run();
// new Thread(null, new R653_Solution_5(), "Main", 1 << 27).start();
}
private void init() throws FileNotFoundException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
try {
if (!console && System.getProperty("user.name").equals("puneet")) {
outputStream = new FileOutputStream("/home/puneet/Desktop/output.txt");
inputStream = new FileInputStream("/home/puneet/Desktop/input.txt");
}
} catch (Exception ignored) {
}
out = new FastOutput(outputStream);
in = new FastInput(inputStream);
}
private void maualAssert(int a,int b,int c){
if(a<b || a>c) throw new RuntimeException();
}
private void maualAssert(long a,long b,long c){
if(a<b || a>c) throw new RuntimeException();
}
private void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private long ModPow(long x, long y, long MOD) {
long res = 1L;
x = x % MOD;
while (y >= 1L) {
if ((y & 1L) > 0) res = (res * x) % MOD;
x = (x * x) % MOD;
y >>= 1L;
}
return res;
}
private int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private int[] arrInt(int n){
int[] arr=new int[n];for(int i=0;i<n;++i)arr[i]=in.ni();
return arr;
}
private long[] arrLong(int n){
long[] arr=new long[n];for(int i=0;i<n;++i)arr[i]=in.nl();
return arr;
}
private int arrMax(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMax(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private int arrMin(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMin(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
class FastInput { InputStream obj;
public FastInput(InputStream obj) {
this.obj = obj;
}
private byte inbuffer[] = new byte[1024];
private int lenbuffer = 0, ptrbuffer = 0;
private int readByte() { if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) { ptrbuffer = 0;
try { lenbuffer = obj.read(inbuffer);
} catch (IOException e) { throw new InputMismatchException(); } }
if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; }
String ns() { int b = skip();StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ')
{ sb.appendCodePoint(b);b = readByte(); }return sb.toString();}
int ni() {
int num = 0, b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); }}
long nl() { long num = 0;int b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); } }
private boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; }
float nf() {return Float.parseFloat(ns());}
double nd() {return Double.parseDouble(ns());}
char nc() {return (char) skip();}
}
class FastOutput{
private final PrintWriter writer;
public FastOutput(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public PrintWriter getWriter(){
return writer;
}
public void print(Object obj){
writer.print(obj);
}
public void printLn(){
writer.println();
}
public void printLn(Object obj){
writer.print(obj);
printLn();
}
public void printSp(Object obj){
writer.print(obj+" ");
}
public void printArr(int[] arr){
for(int i:arr)
printSp(i);
printLn();
}
public void printArr(long[] arr){
for(long i:arr)
printSp(i);
printLn();
}
public void flush(){
writer.flush();
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long dx[] = {1, -1, 0, 0};
long long dy[] = {0, 0, 1, -1};
void solve() {
long long n, k;
cin >> n >> k;
vector<long long> arr1, arr2, v;
for (long long i = 0; i < n; i++) {
long long x, a, b;
cin >> x >> a >> b;
if (a && b)
v.push_back(x);
else if (a)
arr1.push_back(x);
else if (b)
arr2.push_back(x);
}
sort((arr1).begin(), (arr1).end());
sort((arr2).begin(), (arr2).end());
for (long long i = 0; i < min(arr1.size(), arr2.size()); i++)
v.push_back(arr1[i] + arr2[i]);
sort((v).begin(), (v).end());
if (v.size() < k)
cout << -1 << '\n';
else {
long long ans = 0;
for (long long i = 0; i < k; i++) ans += v[i];
cout << ans << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long test;
test = 1;
long long x = 1;
while (test--) {
solve();
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.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<>();
TreeSet<pair> pq5=new TreeSet<>();
TreeSet<pair> pq6=new TreeSet<>();
TreeSet<pair> pq7=new TreeSet<>();
TreeSet<pair> pq8=new TreeSet<>();
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.pollLast().x;
c-=pq6.pollLast().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&&!pq5.isEmpty()) {
c2--;
c3--;
c-=pq5.last().x;
pq1.add(pq5.pollLast());
}else if (c2>=1&&!pq3.isEmpty()&&!pq5.isEmpty()) {
if (pq3.peek().x<pq5.last().x) {
c2--;
c-=pq5.last().x;
c+=pq3.peek().x;
pq1.add(pq5.pollLast());
pq7.add(pq3.poll());
}
}else if (c3>=1&&!pq2.isEmpty()&&!pq5.isEmpty()) {
if (pq2.peek().x<pq5.last().x) {
c2--;
c-=pq5.last().x;
c+=pq2.peek().x;
pq1.add(pq5.pollLast());
pq6.add(pq2.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.pollFirst().y+" ");
} while (!pq6.isEmpty()) {
pw.print(pq6.pollFirst().y+" ");
} while (!pq7.isEmpty()) {
pw.print(pq7.pollFirst().y+" ");
} while (!pq8.isEmpty()) {
pw.print(pq8.pollFirst().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"
]
}
|
CORRECT
|
python3
|
n,k = [int(x) for x in input().split()]
books=[]
books_a=[]
books_b=[]
for i in range(n):
b = [int(x) for x in input().split()]
if b[1]==1 and b[2]==1:
books.append(b[0])
elif b[1]==1:
books_a.append(b[0])
elif b[2]==1:
books_b.append(b[0])
books_a=sorted(books_a)
books_b=sorted(books_b)
z= min(len(books_a) , len(books_b))
for j in range(z):
books.append(books_a[j] + books_b[j])
books=sorted(books)
ans=0
if len(books) < k:
print(-1)
else:
for f in range(k):
ans+=books[f]
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_INT = numeric_limits<int>::max();
const int MAX_UINT = numeric_limits<unsigned int>::max();
void get_acum(const vector<int>& v, vector<int>& acum) {
int sum = 0;
for (unsigned int i = 0; i < v.size(); ++i) {
acum.push_back(sum);
sum += v[i];
}
acum.push_back(sum);
}
int solve(vector<int>& A, vector<int>& B, vector<int>& X, unsigned int k) {
if (A.size() + X.size() < k || B.size() + X.size() < k) {
return -1;
} else {
sort(A.begin(), A.end());
sort(B.begin(), B.end());
sort(X.begin(), X.end());
vector<int> A_acum, B_acum, X_acum;
get_acum(A, A_acum);
get_acum(B, B_acum);
get_acum(X, X_acum);
int res = MAX_INT;
unsigned int max_i = min((unsigned int)X.size(), k);
for (unsigned int i = 0; i < max_i + 1; ++i) {
if (k - i < A_acum.size() && k - i < B_acum.size()) {
int total = A_acum[k - i] + B_acum[k - i] + X_acum[i];
if (total < res) res = total;
}
}
return res;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
unsigned int n, k;
cin >> n >> k;
vector<int> A;
vector<int> B;
vector<int> X;
for (unsigned int i = 0; i < n; ++i) {
unsigned int ti;
bool ai, bi;
cin >> ti >> ai >> bi;
if (ai && bi)
X.push_back(ti);
else if (ai)
A.push_back(ti);
else if (bi)
B.push_back(ti);
}
cout << solve(A, B, X, k);
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.