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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 1;
long long int m = 1000000007;
string toString(char x) {
string s(1, x);
return s;
}
long long int pow1(long long int x, long long int y) {
long long int temp;
if (y == 0) return 1;
temp = pow1(x, y / 2);
if (y % 2 == 0)
return (temp % m * temp % m) % m;
else
return ((x % m * temp % m) % m * temp % m) % m;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> times[4];
vector<int> sums[4];
for (int i = 0; i < n; ++i) {
int t, a, b;
cin >> t >> a >> b;
times[a * 2 + b].push_back(t);
}
for (int i = 0; i < 4; ++i) {
sort(times[i].begin(), times[i].end());
sums[i].push_back(0);
for (auto it : times[i]) {
sums[i].push_back(sums[i].back() + it);
}
}
int ans = INF;
for (int cnt = 0; cnt < min(k + 1, int(sums[3].size())); ++cnt) {
if (k - cnt < int(sums[1].size()) && k - cnt < int(sums[2].size())) {
ans = min(ans, sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt]);
}
}
if (ans == INF) ans = -1;
cout << ans << endl;
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from heapq import heappush, heappop
import math as m
n, k = map(int, input().split())
a_b = []
b_b = []
ab_b = []
for book in range(n):
t, a, b = map(int, input().split())
if a == 1 and b == 1:
heappush(ab_b, t)
elif a == 1:
heappush(a_b, t)
elif b ==1:
heappush(b_b, t)
a_like = 0
b_like = 0
result = 0
while True:
if a_like < k and b_like < k:
if len(ab_b) == 0:
if len(a_b) == 0:
print(-1)
exit()
else:
result += heappop(a_b)
a_like += 1
if len(b_b) == 0:
print(-1)
exit()
else:
result += heappop(b_b)
b_like += 1
else:
anext = a_b[0] if len(a_b) > 0 else m.inf
bnext = b_b[0] if len(b_b) > 0 else m.inf
if anext + bnext < ab_b[0]:
result += heappop(a_b)
a_like += 1
result += heappop(b_b)
b_like += 1
else:
result += heappop(ab_b)
a_like += 1
b_like += 1
elif a_like < k:
if len(ab_b) == 0 and len(a_b) == 0:
print(-1)
exit()
elif len(ab_b) == 0:
result += heappop(a_b)
elif len(a_b) == 0:
result += heappop(ab_b)
elif a_b[0] < ab_b[0]:
result += heappop(a_b)
else:
result += heappop(ab_b)
elif b_like < k:
if len(ab_b) == 0 and len(b_b) == 0:
print(-1)
exit()
elif len(ab_b) == 0:
result += heappop(b_b)
elif len(b_b) == 0:
result += heappop(ab_b)
elif b_b[0] < ab_b[0]:
result += heappop(b_b)
else:
result += heappop(ab_b)
else:
break
print(result)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct jg {
int x, y, z;
};
jg a[200001];
int n, k, t1, t2, t3, mx, ans[200001], sum[200001], ans1[200001];
bool bj1[200001], bj2;
int main() {
int i, j;
cin >> n >> k;
for (i = 1; i <= n; i++) {
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);
if (a[i].y == 1 && a[i].z == 0) {
t1++;
ans[t1] = a[i].x;
}
if (a[i].y == 0 && a[i].z == 1) {
t2++;
sum[t2] = a[i].x;
}
if (a[i].y == 1 && a[i].z == 1) {
t3++;
ans1[t3] = a[i].x;
}
}
sort(ans + 1, ans + t1 + 1);
sort(sum + 1, sum + t2 + 1);
for (i = 1; i <= min(t1, t2); i++) {
t3++;
ans1[t3] = ans[i] + sum[i];
}
if (t3 < k) {
cout << -1 << endl;
return 0;
}
sort(ans1 + 1, ans1 + t3 + 1);
for (i = 1; i <= k; i++) {
mx += ans1[i];
}
cout << mx << 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
|
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;
}
}
static TreeSet<Book> set1, set2;
static long now;
static List<Log> log;
static boolean[] chk;
public static void updateSet(Book c, boolean tof) {
if (tof) {
set2.add(c);
now += c.t;
} else {
set2.remove(c);
now -= c.t;
}
log.add(new Log(c.idx, tof));
}
public static void updateLog() {
for (Log l : log) {
chk[l.idx] = l.tof;
}
log.clear();
}
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;
now = 0;
set1 = new TreeSet<>();
set2 = new TreeSet<>();
log = new ArrayList<>();
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++) {
updateSet(list[0].get(j), true);
}
for (int j = 0; j < K - i; j++) {
for (int k = 1; k < 3; k++) {
updateSet(list[k].get(j), true);
}
}
for (int j = 0; j < 3; j++) {
for (int k = j == 0 ? i : K - i; k < list[j].size(); k++) {
set1.add(list[j].get(k));
}
}
for (int j = 2 * K - i; j < M; j++) {
Book c = set1.first();
set1.remove(c);
updateSet(c, true);
}
updateLog();
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);
updateSet(c, true);
}
for (int j = 1; j < 3; j++) {
c = list[j].get(K - i - 1);
if (set2.contains(c)) {
set1.add(c);
updateSet(c, false);
}
}
while (set2.size() < M) {
c = set1.first();
set1.remove(c);
updateSet(c, true);
}
while (set2.size() > M) {
c = set2.last();
set1.add(c);
updateSet(c, false);
}
if (ans > now) {
updateLog();
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
|
for _ in range(1):
n,k=map(int,input().split())
al = []
bo = []
com = []
for i in range(n):
t,a,b=map(int,input().split())
if a==b==1:
com.append(t)
else:
if a==1:
al.append(t)
if b==1:
bo.append(t)
com.sort(reverse=True)
al.sort(reverse=True)
bo.sort(reverse=True)
ans=0
f=0
while(k!=0):
if len(al)>0 and len(bo)>0:
c=al[-1]+bo[-1]
if len(com)>0 and c>com[-1]:
ans+=com[-1]
com.pop()
else:
ans+=c
al.pop()
bo.pop()
elif len(com)>0:
ans += com[-1]
com.pop()
else:
f=1
k=1
k-=1
if f==0:
print(ans)
else:
print(-1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
first_nums = input().split()
len_matris = int(first_nums[0])
k = int(first_nums[1])
books = []
for i in range(len_matris):
book = input().split()
books.append([int(x) for x in book])
liked_books = list(filter(lambda x: x[1] == 1 and x[2] ==1,books))
books_0_1 = list(filter(lambda x: x[1] == 0 and x[2] == 1,books))
books_1_0 = list(filter(lambda x: x[1] == 1 and x[2] == 0,books))
books_0_1.sort()
books_1_0.sort()
liked_books.sort()
result_books = []
for i in liked_books:
result_books.append(i[0])
for a,b in zip(books_1_0,books_0_1):
result_books.append(a[0]+b[0])
result_books.sort()
if len(books_1_0) + len(liked_books) < k or len(books_0_1) + len(liked_books) < k:
print(-1)
else:
print(sum(result_books[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
#reading books(easy version)
#code forces
#In the name of god the all mighty
if __name__=="__main__":
n,k=map(int,input().split())
both=list()
alice=list()
bob=list()
for i in range(n):
t,a,b=map(int,input().split())
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)
al=k
bb=k
ans=0
while(True):
if(al<=0 or bb<=0):
break
if(not len(alice) and al>0):
if(not len(both)):
break
if(not len(bob) and bb>0):
if(not len(both)):
break
if(len(both) and len(alice) and len(bob)):
if(both[-1]<(alice[-1]+bob[-1])):
ans+=both[-1]
both.pop()
else:
ans+=alice[-1]+bob[-1]
alice.pop()
bob.pop()
al-=1
bb-=1
elif(len(alice) and len(bob)):
if(len(alice)):
al-=1
ans+=alice.pop()
if(len(bob)):
bb-=1
ans+=bob.pop()
else:
if(len(both)):
ans+=both.pop()
al-=1
bb-=1
if(al<=0 and bb<=0):
print(ans)
else:
print('-1')
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.TreeSet;
import java.util.Vector;
import static java.util.Collections.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cfa {
@SuppressWarnings("unused")
private static final int MOD = (int) (1e9 + 7), MOD_FFT = 998244353;
private static final Reader r = new Reader();
private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
private static final boolean thread = false;
private static final boolean HAS_TEST_CASES = false;
private static final int SCAN_LINE_LENGTH = 1000002;
static int n, a[], e[][];
static void solve() throws Exception {
n = ni();
int k = ni(), k1 = k, k2 = k;
int[] t = new int[n], a = new int[n], b = new int[n];
for (int i = 0; i < b.length; i++) {
t[i] = ni();
a[i] = ni();
b[i] = ni();
}
Vector<Integer> v = new Vector<>(), v1 = new Vector<>(), v2 = new Vector<>();
for (int i = 0; i < b.length; i++) {
if (a[i] == 1 && b[i] == 1)
v.add(t[i]);
else if (a[i] == 1) {
v1.add(t[i]);
} else if (b[i] == 1)
v2.add(t[i]);
}
sort(v);
sort(v1);
sort(v2);
long ans = 0;
int i = 0, j = 0;
while (k1 > 0 || k2 > 0) {
if (i < v.size() && j < v1.size() && j < v2.size() && v.get(i) <= (v1.get(j) + v2.get(j))) {
{
ans += v.get(i++);
k1--;
k2--;
}
} else if (j < v1.size() && j < v2.size()) {
{
ans += v1.get(j) + v2.get(j++);
k1--;
k2--;
}
}
if (i == v.size()) {
while (k1 > 0 || k2 > 0) {
ans += v1.get(j) + v2.get(j++);
k1--;
k2--;
}
} else if (j == v1.size() || j == v2.size())
while (k1 > 0 || k2 > 0) {
ans += v.get(i++);
k1--;
k2--;
}
}
pn(ans);
}
private static void dfc(int[] visited, int i, int c) {
visited[i] = c % 2;
for (Integer j : adj[i]) {
if (visited[j] == c % 2)
visited[i] = 3;
else if (visited[j] == -1) {
dfc(visited, j, c + 1);
}
}
}
static int dfs(int[] v, int i, int d, int k, Vector<Integer> path) {
if (v[i] != 0) {
if (d - v[i] <= 2) {
return 0;
}
if (d - v[i] <= k) {
pn(2);
pn(d - v[i]);
for (int j = v[i] - 1; j < path.size(); j++) {
p(path.get(j) + " ");
}
pn();
return 1;
} else {
pn(1);
for (int j = v[i] - 1; j < (k + 1) / 2; ++j) {
p(path.get(j * 2) + " ");
}
pn();
return 1;
}
}
v[i] = d;
path.add(i);
for (Integer j : adj[i]) {
if (v[j] > 0 && d - v[j] > 1 && dfs(v, j, d + 1, k, path) == 1)
return 1;
}
for (Integer j : adj[i]) {
if (dfs(v, j, d + 1, k, path) == 1)
return 1;
}
path.remove(path.size() - 1);
return 0;
}
@SuppressWarnings("unused")
private static Vector<Integer> adj[], v;
@SuppressWarnings("unused")
private static HashSet<Integer> set;
@SuppressWarnings("unused")
private static PriorityQueue<Integer> pq;
private static void addEdge(final int u, final int v) {
adj[u] = (adj[u] == null) ? new Vector<>() : adj[u];
adj[v] = (adj[v] == null) ? new Vector<>() : adj[v];
adj[u].add(v);
adj[v].add(u);
}
public static void main(final String[] args) throws Exception {
if (!thread) {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final Exception e) {
pn(-1);
}
}
out.flush();
r.close();
out.close();
}
}
@SuppressWarnings("unused")
private static void swap(final int i, final int j) {
final int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
@SuppressWarnings("unused")
private static int gcd(final int a, final int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
@SuppressWarnings("unused")
private static class Pair<T, E> implements Comparable<Pair<T, E>> {
T fir;
E snd;
Pair() {
}
Pair(final T x, final E y) {
this.fir = x;
this.snd = y;
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(final Pair<T, E> o) {
final int c = ((Comparable<T>) fir).compareTo(o.fir);
return c != 0 ? c : ((Comparable<E>) snd).compareTo(o.snd);
}
}
@SuppressWarnings("unused")
private static class pi implements Comparable<pi> {
int fir, snd;
pi() {
}
pi(final int a, final int b) {
fir = a;
snd = b;
}
public int compareTo(final pi o) {
return fir == o.fir ? snd - o.snd : fir - o.fir;
}
}
@SuppressWarnings("unused")
private static <T> void checkV(final Vector<T> adj[], final int i) {
adj[i] = adj[i] == null ? new Vector<>() : adj[i];
}
@SuppressWarnings("unused")
private static int[] na(final int n) throws Exception {
final int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
@SuppressWarnings("unused")
private static int[] na1(final int n) throws Exception {
final int[] a = new int[n + 1];
for (int i = 1; i < a.length; i++) {
a[i] = ni();
}
return a;
}
@SuppressWarnings("unused")
private static String n() throws IOException {
return r.readToken();
}
@SuppressWarnings("unused")
private static String nln() throws IOException {
return r.readLine();
}
private static int ni() throws IOException {
return r.nextInt();
}
@SuppressWarnings("unused")
private static long nl() throws IOException {
return r.nextLong();
}
@SuppressWarnings("unused")
private static double nd() throws IOException {
return r.nextDouble();
}
@SuppressWarnings("unused")
private static void p(final Object o) {
out.print(o);
}
@SuppressWarnings("unused")
private static void pn(final Object o) {
out.println(o);
}
@SuppressWarnings("unused")
private static void pn() {
out.println("");
}
private static void pni(final Object o) {
out.println(o);
out.flush();
}
private static class Reader {
private final int BUFFER_SIZE = 1 << 17;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
// private StringTokenizer st;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
@SuppressWarnings("unused")
public Reader(final String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
final byte[] buf = new byte[SCAN_LINE_LENGTH];
public String readLine() throws IOException {
int cnt = 0;
int c;
o: while ((c = read()) != -1) {
if (c == '\n')
if (cnt == 0)
continue o;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String readToken() throws IOException {
int cnt = 0;
int c;
o: while ((c = read()) != -1) {
if (!(c >= 33 && c <= 126))
if (cnt == 0)
continue o;
else
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
final boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static {
if (thread)
new Thread(null, new Runnable() {
@Override
public void run() {
try {
final int testcases = HAS_TEST_CASES ? ni() : 1;
for (int i = 1; i <= testcases; i++) {
// out.print("Case #" + (i + 1) + ": ");
try {
solve();
} catch (final Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
out.flush();
r.close();
out.close();
} catch (final Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
}, "rec", (1L << 28)).start();
}
@SuppressWarnings({ "unchecked", "unused" })
private static <T> T deepCopy(final T old) {
try {
return (T) deepCopyObject(old);
} catch (final IOException | ClassNotFoundException e) {
e.printStackTrace(System.err);
System.exit(-1);
}
return null;
}
private static Object deepCopyObject(final Object oldObj) throws IOException, ClassNotFoundException {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
} catch (final ClassNotFoundException e) {
pni("Exception in ObjectCloner = " + e);
throw (e);
} finally {
oos.close();
ois.close();
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
from operator import add
t=1
def inp(): return sys.stdin.readline().strip()
for _ in range(t):
n, k=map(int, inp().split())
bothbooks=[]
abooks=[]
bbooks=[]
for i in range(n):
t, a, b= map(int,inp().split())
if( a==1 and b==1):
bothbooks.append(t)
elif(a==1 and b==0):
abooks.append(t)
elif(a==0 and b==1):
bbooks.append(t)
abooks.sort()
bbooks.sort()
#abbooks=list(map(add, abooks, bbooks))
abbooks = [a + b for a, b in zip(abooks, bbooks)]
abbooks = abbooks+bothbooks
abbooks.sort()
if(k>len(abbooks)):
print(-1)
else:
print(sum(abbooks[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
|
# Author:Fuad Ashraful Mehmet ,CSE-UAP
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
|
import sys
input=sys.stdin.buffer.readline
n,k=[int(x) for x in input().split()]
books=[] #(ti,ai,bi)
for _ in range(n):
books.append([int(x) for x in input().split()])
books.sort(key=lambda x:x[0]) #sort by ti asc
aliceBooks=[0] #stores sum of book costs
bobBooks=[0]
bothBooks=[0]
for i in range(n):
ti,ai,bi=books[i]
if ai==1 and bi==0:
aliceBooks.append(aliceBooks[-1]+ti)
if ai==0 and bi==1:
bobBooks.append(bobBooks[-1]+ti)
if ai==1 and bi==1:
bothBooks.append(bothBooks[-1]+ti)
if min(len(aliceBooks)-1,len(bobBooks)-1)+len(bothBooks)-1<k:
print(-1) #impossible
else:
minT=99999999999999999999999999
for cnt in range(len(bothBooks)-1,-1,-1):
if cnt>k:
continue
if k-cnt>=min(len(aliceBooks),len(bobBooks)):
break
minT=min(minT,bothBooks[cnt]+aliceBooks[k-cnt]+bobBooks[k-cnt])
print(minT)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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, setrecursionlimit, stdout
#setrecursionlimit(1000000)
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
from heapq import heapify, heappop, heappush, heappushpop, heapreplace
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def si(): return stdin.readline().rstrip()
def lsi(): return list(si())
#mod=1000000007
res=['YES', 'NO']
############# CODE STARTS HERE #############
test_case=1
while test_case:
test_case-=1
n, k=mi()
both=[0]
al=[0]
bob=[0]
for _ in range(n):
x, y, z=mi()
if y and z:
both.append(x)
elif y:
al.append(x)
elif z:
bob.append(x)
both.sort()
for i in range(1, len(both)):
both[i]+=both[i-1]
al.sort()
for i in range(1, len(al)):
al[i]+=al[i-1]
bob.sort()
for i in range(1, len(bob)):
bob[i]+=bob[i-1]
ans=1000000000000000000000000000
#print(both)
#print(al)
#print(bob)
for i in range(len(both)):
if len(al)>k-i and len(bob)>k-i:
ans=min(ans, both[i]+al[k-i]+bob[k-i])
#print(both[:i]+al[:k-i]+bob[:k-i])
if i==k:
ans=min(ans, both[i])
break
#print(ans)
print(-1 if ans==1000000000000000000000000000 else 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.*;
public class ans5{
static class Book{
int time;
int alice;
int bob;
Book(int t, int a, int b){
time=t;
alice=a;
bob=b;
}
@Override
public String toString(){
return Integer.toString(time)+" "+Integer.toString(alice)+" "+Integer.toString(bob);
}
}
static class srt implements Comparator<Book>{
@Override
public int compare(Book a, Book b){
if (a.time!=b.time) {
return a.time-b.time;
}
else{
if(a.alice==1 && a.bob==1 && b.alice==1 && b.bob==1)
return 0;
if(a.alice==1 && a.bob==1)
return -1;
else if (b.alice==1 && b.bob==1)
return 1;
else
return 0;
}
}
}
static class srt_ulta implements Comparator<Book>{
@Override
public int compare(Book a, Book b){
if (a.time!=b.time) {
return b.time-a.time;
}
else{
if(a.alice==1 && a.bob==1 && b.alice==1 && b.bob==1)
return 0;
if(a.alice==1 && a.bob==1)
return 1;
else if (b.alice==1 && b.bob==1)
return -1;
else
return 0;
}
}
}
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] s=br.readLine().trim().split("\\s+");
int n=Integer.parseInt(s[0].trim());
int k=Integer.parseInt(s[1].trim());
Book[] bookArr=new Book[n];
for (int i=0; i<n; ++i) {
s=br.readLine().trim().split("\\s+");
Book b = new Book(Integer.parseInt(s[0].trim()),Integer.parseInt(s[1].trim()),Integer.parseInt(s[2].trim()));
bookArr[i]=b;
}
Arrays.sort(bookArr, new srt());
ArrayList<Book> both=new ArrayList<Book>();
ArrayList<Book> alice=new ArrayList<Book>();
ArrayList<Book> bob=new ArrayList<Book>();
for (int i=0; i<n; ++i) {
if (bookArr[i].alice==1 && bookArr[i].bob==1) {
both.add(bookArr[i]);
}
else if(bookArr[i].alice==1 && bookArr[i].bob==0) {
alice.add(bookArr[i]);
}
else if(bookArr[i].alice==0 && bookArr[i].bob==1) {
bob.add(bookArr[i]);
}
}
if (both.size()+alice.size()<k ||both.size()+bob.size()<k) {
System.out.println(-1);
}
else{
int kalice=0;
int kbob=0;
int kboth=0;
int sum=0;
while(kalice<alice.size() && kbob<bob.size() && kboth<both.size() && (kalice+kbob)/2+kboth<k){
if (alice.get(kalice).time+bob.get(kbob).time<both.get(kboth).time) {
sum+=alice.get(kalice).time+bob.get(kbob).time;
++kalice;
++kbob;
}
else{
sum+=both.get(kboth).time;
++kboth;
}
}
if ((kalice+kbob)/2+kboth<k) {
if (kalice<alice.size() && kbob<bob.size() && kboth>=both.size()) {
while(kalice<alice.size() && kbob<bob.size() && (kalice+kbob)/2+kboth<k){
sum+=alice.get(kalice).time+bob.get(kbob).time;
++kalice;
++kbob;
}
}
else if(kboth<both.size()){
while(kboth<both.size() && (kalice+kbob)/2+kboth<k){
sum+=both.get(kboth).time;
++kboth;
}
}
}
System.out.println(sum);
}
// for (int i=0; i<n; ++i) {
// System.out.println(bookArr[i]);
// }
// ArrayList<Book> used=new ArrayList<Book>();
// int b=0,a=0,sum=0;
// for (int i=0;i<n ;++i ) {
// Book temp=bookArr[i];
// if (temp.alice==0 && temp.bob==0)
// continue;
// int btemp=b+temp.bob;
// int atemp=a+temp.alice;
// if (Math.abs(btemp-atemp)<=1) {
// a=atemp;
// b=btemp;
// sum+=temp.time;
// used.add(temp);
// }
// if (a>=k && b>=k) {
// break;
// }
// }
// if (a<k || b<k) {
// System.out.println(-1);
// }
// else if(a==k && b==k){
// System.out.println(sum);
// }
// else{
// Collections.sort(used, new srt_ulta());
// if (a>b) {
// for (Book temp : used ) {
// if (a==b)
// break;
// if (temp.alice==1 && temp.bob==0) {
// sum-=temp.time;
// }
// }
// }
// else if(b>a){
// for (Book temp : used ) {
// if (a==b)
// break;
// if (temp.alice==0 && temp.bob==1) {
// sum-=temp.time;
// }
// }
// }
// System.out.println(sum);
// }
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def cumsum(x):
n = len(x)
ans = [0]*(n+1)
for i in range(n):
ans[i+1]=ans[i]+x[i]
return ans
def solve(k, t, a, b):
n = len(t)
b10, b01, b11=[],[],[]
for i in range(n):
if (a[i]==1 and b[i]==1):
b11.append(t[i])
elif a[i]==1:
b10.append(t[i])
elif b[i]==1:
b01.append(t[i])
b10=sorted(b10)
b01=sorted(b01)
b11=sorted(b11)
b1 = [b10[i] + b01[i] for i in range(min(len(b10), len(b01)))]
cs_b1 = cumsum(b1)
cs_b11 = cumsum(b11)
if len(b1) + len(b11) < k:
return -1
ans=1e100
for i in range(0, k+1):
if i<=len(b1) and k-i<=len(b11):
ans = min(ans, cs_b1[i] + cs_b11[k-i])
return ans
n, k = map(int, input().split())
a,b,t = [0]*n,[0]*n,[0]*n
for i in range(n):
t[i], a[i], b[i] = map(int, input().split())
print(solve(k, t,a,b))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class check5 {
public static void main(String[] args) throws IOException{
Reader sc=new Reader();
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt();
long k=sc.nextLong();
long a[][]=new long[n][3];
ArrayList<Long> al=new ArrayList<>();
ArrayList<Long> bob=new ArrayList<>();
ArrayList<Long> both=new ArrayList<>();
for(int i=0;i<n;i++)
{
a[i][0]=sc.nextLong();
a[i][1]=sc.nextLong();
a[i][2]=sc.nextLong();
if(a[i][1]==1)
{
if(a[i][2]==1) both.add(a[i][0]);
else al.add(a[i][0]);
}
else if(a[i][2]==1) bob.add(a[i][0]);
}
Collections.sort(al,Collections.reverseOrder());
Collections.sort(bob,Collections.reverseOrder());
Collections.sort(both,Collections.reverseOrder());
if(al.size()+both.size()<k || bob.size()+both.size()<k)
{
System.out.println(-1);
return;
}
int ac=0;
int bc=0;
long ans=0;
//System.out.println(al+""+bob+both);
while(ac<k && bc<k)
{
int t1=al.size()-1;
int t2=bob.size()-1;
int t3=both.size()-1;
if(t1>=0 && t2>=0 && t3>=0 && (al.get(t1)+bob.get(t2))>=both.get(t3))
{
ans=ans+both.get(t3);
both.remove(t3);
}
else if(t1<0 || t2<0)
{
ans+=both.get(t3);
both.remove(both.size()-1);
}
else// if(t3<0)
{
ans+=al.get(t1)+bob.get(t2);
al.remove(t1);
bob.remove(t2);
}
ac+=1;
bc+=1;
}
System.out.println(ans);
out.flush();
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String nextLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() throws IOException{
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
const ll N = 3e5 + 10;
const ll INF = 1e10;
const ll M = 1e3 + 1;
const ll L = 31;
const ll mod = 998244353;
ll solve() {
ll n, k;
cin >> n >> k;
ll t, a, b;
ll ans = 0;
multiset<ll> alice, bob, both;
for (ll i = 0; i < n; i++) {
cin >> t >> a >> b;
if (a == 1 && b == 1) {
both.insert(t);
} else if (a == 1) {
alice.insert(t);
} else if (b == 1) {
bob.insert(t);
}
}
while (both.size() > 0 && alice.size() > 0 && bob.size() > 0 && k > 0) {
ll a = *(alice.begin());
ll b = *(bob.begin());
ll c = *(both.begin());
if (a + b < c) {
ans += (a + b);
alice.erase(alice.begin());
bob.erase(bob.begin());
} else {
ans += c;
both.erase(both.begin());
}
k--;
}
if (k == 0) {
cout << ans << '\n';
return 0;
} else {
if (alice.size() == 0 || bob.size() == 0) {
if (both.size() < k) {
cout << -1 << '\n';
} else {
while (k > 0) {
ans += (*(both.begin()));
both.erase(both.begin());
k--;
}
cout << ans << '\n';
return 0;
}
} else {
if (alice.size() < k || bob.size() < k) {
cout << -1 << '\n';
return 0;
} else {
while (k > 0) {
ans += (*(alice.begin()));
alice.erase(alice.begin());
ans += (*(bob.begin()));
bob.erase(bob.begin());
k--;
}
cout << ans << '\n';
return 0;
}
}
}
return 0;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
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 bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics
def sin():
return input()
def ain():
return list(map(int, sin().split()))
def sain():
return input().split()
def iin():
return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007
def readTree(n, m):
adj = [deque([]) for _ in range(n+1)]
for _ in range(m):
u,v = ain()
adj[u].append(v)
adj[v].append(u)
return adj
def main():
n, k = ain()
d = deque([])
a = 0
b = 0
for _ in range(n):
x = ain()
if x[1] == 1:
a += 1
if x[2] == 1:
b += 1
d.append(x)
if a < k or b < k:
print(-1)
else:
both = deque([])
a = deque([])
b = deque([])
for i in range(n):
if d[i][1] == 1 and d[i][2] == 1:
both.append(d[i][0])
elif d[i][1] == 1 and d[i][2] == 0:
a.append(d[i][0])
elif d[i][1] == 0 and d[i][2] == 1:
b.append(d[i][0])
a = sorted(a)
b = sorted(b)
for i in range(min(len(a), len(b))):
both.append(a[i]+b[i])
both = sorted(both)
print(sum(both[0:k]))
# Fast IO Template starts
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")
# Fast IO Template ends
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void printVector(vector<string> &v) {
for (auto x : v) cout << x << "\n";
}
void printVector(vector<int> &v) {
for (auto x : v) cout << x << " ";
cout << "\n";
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int t;
t = 1;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> v1, v2, v3;
int ca = 0, cb = 0;
for (int i = 0; i < n; i++) {
int tm, a, b;
cin >> tm >> a >> b;
if (a == 1) {
ca++;
if (b == 1) {
v3.push_back(tm);
cb++;
} else {
v1.push_back(tm);
}
} else if (b == 1) {
v2.push_back(tm);
cb++;
}
}
if (ca < k || cb < k) {
cout << -1 << "\n";
continue;
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
sort(v3.begin(), v3.end());
int i1 = 0, i2 = 0, i3 = 0;
int c1 = 0, c2 = 0;
int ans = 0;
while (c1 < k || c2 < k) {
if (c1 < k && c2 < k) {
int t1 = 2e4 + 1;
if (i1 < v1.size()) t1 = v1[i1];
int t2 = 2e4 + 1;
if (i2 < v2.size()) t2 = v2[i2];
int t3 = 2e4 + 1;
if (i3 < v3.size()) t3 = v3[i3];
if (t3 <= (t1 + t2)) {
ans += t3;
c1++;
c2++;
i3++;
} else {
ans += t1;
ans += t2;
c1++;
c2++;
i1++;
i2++;
}
} else if (c1 < k) {
int t1 = 2e4 + 1;
if (i1 < v1.size()) t1 = v1[i1];
int t3 = 2e4 + 1;
if (i3 < v3.size()) t3 = v3[i3];
if (t3 <= t1) {
ans += t3;
c1++;
c2++;
i3++;
} else {
ans += t1;
c1++;
i1++;
}
} else {
int t2 = 2e4 + 1;
if (i2 < v2.size()) t2 = v2[i2];
int t3 = 2e4 + 1;
if (i3 < v3.size()) t3 = v3[i3];
if (t3 <= t2) {
ans += t3;
c1++;
c2++;
i3++;
} else {
ans += t2;
c2++;
i2++;
}
}
}
cout << ans << "\n";
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
import java.math.*;
public class E1
{
public static void main(String hi[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = 1;
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
PriorityQueue<Integer>[] buckets = new PriorityQueue[4];
for(int i=0; i < 4; i++)
buckets[i] = new PriorityQueue<Integer>();
for(int i=0; i < N; i++)
{
st = new StringTokenizer(infile.readLine());
int val = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int mask = (a<<1)+b;
if(mask > 0)
buckets[mask].add(val);
}
//alice, 2
//bob, 1
int alice = K;
int bob = K; long res = 0L;
while(alice > 0 || bob > 0)
{
if(buckets[3].size() > 0 && Math.min(alice,bob) > 0)
{
if(Math.min(buckets[1].size(), buckets[2].size()) > 0)
{
long temp = buckets[1].peek()+buckets[2].peek();
if(temp < buckets[3].peek())
{
res += temp;
buckets[1].poll();
buckets[2].poll();
}
else
res += buckets[3].poll();
}
else
res += buckets[3].poll();
alice--; bob--;
}
else if(buckets[2].size() > 0 && alice > 0)
{
res += buckets[2].poll();
alice--;
}
else if(buckets[1].size() > 0 && bob > 0)
{
res += buckets[1].poll();
bob--;
}
else
break;
}
if(alice > 0 || bob > 0)
res = -1;
sb.append(res+"\n");
}
System.out.print(sb);
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
public class E {
static class Item {
int time;
int alice;
int bob;
@Override
public String toString() {
return "Item{" +
"time=" + time +
", alice=" + alice +
", bob=" + bob +
'}';
}
}
static PriorityQueue<Item> all;
static PriorityQueue<Item> alice;
static PriorityQueue<Item> bob;
static int k;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
n = in.nextInt();
k = in.nextInt();
Item[] items = new Item[n];
for (int i = 0; i < n; i++) {
items[i] = new Item();
items[i].time = in.nextInt();
items[i].alice = in.nextInt();
items[i].bob = in.nextInt();
}
all = new PriorityQueue<Item>((a, b) -> b.time - a.time);
alice = new PriorityQueue<>((a, b) -> a.time - b.time);
bob = new PriorityQueue<>((a, b) -> a.time - b.time);
Arrays.sort(items, (a, b) -> a.time - b.time);
for (int i = 0; i < n; i++) {
if (items[i].bob + items[i].alice == 2) {
if (all.size() < k) {
all.add(items[i]);
}
} else if (items[i].alice == 1) {
if (alice.size() < k) {
alice.add(items[i]);
}
} else if (items[i].bob == 1) {
if (bob.size() < k) {
bob.add(items[i]);
}
}
}
System.out.println(solve());
}
private static long solve() {
if (alice.size() + all.size() < k) {
return -1;
}
if (bob.size() + all.size() < k) {
return -1;
}
long sum = 0;
int count = 0;
while (count < k) {
if (alice.size() == 0 || bob.size() == 0) {
if (all.size() + count < k) {
return -1;
}
while (all.size() + count > k) {
all.poll();
}
while (!all.isEmpty()) {
sum += all.poll().time;
}
return sum;
} else {
if (all.size() == 0 || (alice.peek().time + bob.peek().time < all.peek().time)) {
// System.out.println(alice.peek().toString() + " " + bob.peek());
sum += alice.poll().time + bob.poll().time;
} else {
sum += all.poll().time;
}
count++;
}
while (all.size() + count > k) {
all.poll();
}
}
return 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.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
PriorityQueue<Integer> both = new PriorityQueue<>();
PriorityQueue<Integer> alice = new PriorityQueue<>();
PriorityQueue<Integer> bob = new PriorityQueue<>();
for(int i=0;i<n;i++){
int t = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
if(a==1 && b==1){
both.offer(t);
} else if(a == 1){
alice.offer(t);
} else if(b == 1){
bob.offer(t);
}
}
int timeSpent = 0;
while(k > 0){
if(!alice.isEmpty() && !bob.isEmpty() && !both.isEmpty()){
if(alice.peek()+bob.peek() < both.peek()) {
timeSpent += (alice.poll() + bob.poll());
} else {
timeSpent += both.poll();
}
k--;
} else if(!both.isEmpty()){
timeSpent += both.poll();
k--;
} else if(!alice.isEmpty() && !bob.isEmpty()){
timeSpent += (alice.poll() + bob.poll());
k--;
} else {
break;
}
if(k == 0) break;
}
System.out.println((k==0)?timeSpent:"-1");
}
}
/*
Your previous Plain Text content is preserved below:
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
both: 7 2 8 1
alice:
bob : 4, 1
both+alice < k || both+bob < k
return -1;
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
both : 2
alice : 5
bob : 1
8
if alicequeue is not empty and bobqueue is not empty
if bob time + alice time < both time
pick individual
else pick
both time
else
pick both time
*/
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=list(map(int,input().split()))
both=[]
alice=[]
bob=[]
for i in range(n):
t,a,b=list(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)
both.sort();alice.sort();bob.sort()
x=0;y=0
if len(both)+len(alice)<k or len(both)+len(bob)<k:
print(-1)
else:
both.append(10**9);alice.append(10**9);bob.append(10**9)
i = 0;
j = 0
time=0
alpha=0
while x<k and y<k:
if i<len(both) and j<len(alice) and alpha<len(bob):
if both[i]<=alice[j]+bob[alpha]:
time+=both[i]
i+=1
x+=1;y+=1
else:
time+=alice[j]+bob[alpha]
j+=1
alpha+=1
x+=1;y+=1
else:
if len(both)!=i:
if len(alice)==0:
if len(bob)==0:
time+=both[i]
i+=1
x+=1;y+=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
|
python3
|
from sys import stdin, stdout
from sys import maxsize
#input = stdin.readline().strip
from functools import cmp_to_key
def solve(books, k):
alice = []
bob = []
both = []
for book in books:
if(book[1] == 1 and book[2] == 1):
both.append(book[0])
elif(book[1] == 0 and book[2] == 1):
bob.append(book[0])
elif(book[1] == 1 and book[2] == 0):
alice.append(book[0])
alice.sort(reverse=True)
bob.sort(reverse=True)
both.sort(reverse=True)
no_of_books, time = 0, 0
while(len(alice) != 0 and len(bob) != 0 and len(both) != 0 and no_of_books < k):
no_of_books += 1
if(alice[-1]+bob[-1] < both[-1]):
time += alice[-1]+bob[-1]
alice.pop()
bob.pop()
else:
time += both[-1]
both.pop()
while(len(both) != 0 and no_of_books < k):
no_of_books += 1
time += both[-1]
both.pop()
while(len(alice) != 0 and len(bob) != 0 and no_of_books < k):
no_of_books += 1
time += alice[-1]+bob[-1]
alice.pop()
bob.pop()
if(no_of_books == k):
return time
else:
return -1
test = 1
# test = int(input())
for t in range(0, test):
# brr = [list(map(int,input().split())) for i in range(rows)] # 2D array row-wise input
# n = int(input())
# s = list(input()) # String Input, converted to mutable list.
n, k = list(map(int, input().split()))
# arr = [int(x) for x in input().split()]
b = [tuple(map(int, input().split())) for i in range(n)]
ans = solve(b, k)
print(ans)
'''
rows, cols = (5, 5)
arr = [[0]*cols for j in range(rows)] # 2D array initialization
b=input().split() # list created by spliting about spaces
brr = [[int(b[cols*i+j]) for j in range(cols)] for i in range(rows)] # 2D array Linear Input
rows,cols=len(brr),len(brr[0]) # no of rows/cols for 2D array
arr.sort(key = lambda x : x[1]) # sort list of tuples by 2nd element, Default priority - 1st Element then 2nd Element
s=set() # empty set
a=maxsize # initializing infinity
b=-maxsize # initializing -infinity
mapped=list(map(function,input)) # to apply function to list element-wise
try: # Error handling
#code 1
except: # ex. to stop at EOF
#code 2 , if error occurs
'''
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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 time(vector<int> l, int il, vector<int> b, int ib, vector<int> r,
int ir, int k) {
long long ans = 0;
int left = k, right = k;
int index_both = 0;
int index_right = 0;
int index_left = 0;
while (left != 0 && right != 0) {
if (index_both == ib) {
if (index_right == ir || index_left == il) {
return -1;
} else {
ans += l[index_left];
ans += r[index_right];
index_left++;
index_right++;
left--;
right--;
}
} else {
if (index_right == ir || index_left == il) {
ans += b[index_both];
index_both++;
left--;
right--;
} else {
long long b1 = b[index_both];
long long t1 = l[index_left] + r[index_right];
if (b1 <= t1) {
ans += b1;
index_both++;
left--;
right--;
} else {
ans += t1;
index_left++;
index_right++;
left--;
right--;
}
}
}
}
while (left > 0) {
if (index_both == ib) {
if (index_left == il)
return -1;
else {
ans += l[index_left];
index_left++;
left--;
}
} else {
if (index_left == il) {
ans += b[index_both];
index_both++;
left--;
} else {
long long b1 = b[index_both];
long long l1 = l[index_left];
if (b1 <= l1) {
ans += b1;
index_both++;
left--;
} else {
ans += l1;
index_left++;
left--;
}
}
}
}
while (right > 0) {
if (index_both == ib) {
if (index_right == ir)
return -1;
else {
ans += r[index_right];
index_right++;
right--;
}
} else {
if (index_right == ir) {
ans += b[index_both];
index_both++;
right--;
} else {
long long b1 = b[index_both];
long long r1 = r[index_right];
if (b1 <= r1) {
ans += b1;
index_both++;
right--;
} else {
ans += r1;
index_right++;
right--;
}
}
}
}
return ans;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> l(n);
vector<int> r(n);
vector<int> b(n);
int ib = 0, il = 0, ir = 0;
int t, ai, bi;
for (int i = 0; i < n; i++) {
cin >> t >> ai >> bi;
if (ai == 1 && bi == 1) {
b[ib] = t;
ib++;
} else if (ai == 1) {
l[il] = t;
il++;
} else if (bi == 1) {
r[ir] = t;
ir++;
}
}
vector<int>::iterator it = l.begin();
advance(it, il);
sort(l.begin(), it);
it = r.begin();
advance(it, ir);
sort(r.begin(), it);
it = b.begin();
advance(it, ib);
sort(b.begin(), it);
cout << time(l, il, b, ib, r, ir, k) << '\n';
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
inline long long add(long long a, long long b) { return (a + b) % 1000000007; }
inline long long mul(long long a, long long b) { return (a * b) % 1000000007; }
long long f[4000010], iv[4000010];
long long C(long long n, long long r) {
return mul(f[n], mul(iv[r], iv[n - r]));
}
void prep_fac() {
f[0] = 1;
for (int i = 1; i < 4000010; i++) f[i] = mul(i, f[i - 1]);
iv[4000010 - 1] = power(f[4000010 - 1], 1000000007 - 2, 1000000007);
for (long long i = 4000010 - 2; i >= 0; --i) iv[i] = mul(i + 1, iv[i + 1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
int c1 = 0, c2 = 0;
vector<long long> v[4];
v[1].push_back(0), v[2].push_back(0), v[3].push_back(0);
for (int i = 0; i < n; i++) {
int t, a, b;
cin >> t >> a >> b;
v[2 * a + b].push_back(t);
}
for (int i = 1; i < 4; i++) {
sort(v[i].begin(), v[i].end());
for (int j = 1; j < v[i].size(); j++) {
v[i][j] += v[i][j - 1];
}
}
long long ans = INT_MAX;
for (int i = 0; i < v[3].size(); i++) {
if (v[2].size() > k - i && v[1].size() > k - i)
ans = min(v[3][i] + v[2][k - i] + v[1][k - i], ans);
}
if (ans == INT_MAX) ans = -1;
cout << ans;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.readline
n,k=map(int,input().split())
BOOKS=[tuple(map(int,input().split())) for i in range(n)]
A=[]
B=[]
AB=[]
for t,a,b in BOOKS:
if a==1 and b==1:
AB.append(t)
if a==1 and b==0:
A.append(t)
if a==0 and b==1:
B.append(t)
A.sort()
B.sort()
AB.sort()
from itertools import accumulate
SA=[0]+list(accumulate(A))
SB=[0]+list(accumulate(B))
SAB=[0]+list(accumulate(AB))
ANS=1<<31
for i in range(len(SAB)):
if 0<=k-i<len(SA) and 0<=k-i<len(SB):
ANS=min(ANS,SAB[i]+SA[k-i]+SB[k-i])
if ANS==1<<31:
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
|
python2
|
import sys
class segtree:
def __init__(self, data):
n = len(data)
self.m = 1
while self.m < n: self.m *= 2
self.data = [0] * (2 * self.m)
self.data[self.m: self.m + n] = data
for i in reversed(range(1, self.m)):self.data[i] = self.data[2 * i] + self.data[2 * i + 1]
def __setitem__(self, i, x):
i += self.m;self.data[i] = x;i >>= 1
while i:self.data[i] = self.data[2 * i] + self.data[2 * i + 1];i >>= 1
def finder(self, k):
if k == 0:return -1
assert self.data[1] >= k
i = 1
while i < self.m:
i *= 2
if self.data[i] < k:
k -= self.data[i]
i += 1
return i - self.m
def getter(self, l, r):
l += self.m
r += self.m
s = 0
while l < r:
if l & 1:
s += self.data[l]
l += 1
if r & 1:
r -= 1
s += self.data[r]
l >>= 1
r >>= 1
return s
inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
N = inp[ii]; ii += 1
M = inp[ii]; ii += 1
k = inp[ii]; ii += 1
T = inp[ii + 0: ii + 3 * N: 3]
A = inp[ii + 1: ii + 3 * N: 3]
B = inp[ii + 2: ii + 3 * N: 3]
order = sorted(range(N), key = T.__getitem__)
T = [T[i] for i in order]
A = [A[i] for i in order]
B = [B[i] for i in order]
TA = []
TB = []
TAB = []
for i in range(N):
if A[i] and B[i]:TAB.append(i)
elif A[i]:TA.append(i)
elif B[i]:TB.append(i)
n = len(TA)
m = len(TB)
nm = len(TAB)
mark = [1] * N
times = list(T)
upper = min(k, nm)
picked = k - upper
if picked > n or picked > m:print (-1);sys.exit()
tsum = 0
for j in range(picked):
i = TA[j]
tsum += T[i]
mark[i] = 0
times[i] = 0
i = TB[j]
tsum += T[i]
mark[i] = 0
times[i] = 0
for j in range(upper):
i = TAB[j]
tsum += T[i]
mark[i] = 0
times[i] = 0
marker = segtree(mark)
timer = segtree(times)
time = inf = 10**9 * 2 + 100
optx = -1
for x in reversed(range(upper + 1)):
books = x + 2 * (k - x)
if books <= M:
cand = tsum + timer.getter(0, marker.finder(M - books) + 1)
if cand < time:time = cand;optx = x
if x:i = TAB[x - 1];tsum -= T[i];marker[i] = mark[i] = 1;timer[i] = times[i] = T[i]
j = k - x
if j >= n or j >= m:break
i = TA[j]
tsum += T[i]
marker[i] = mark[i] = 0
timer[i] = times[i] = 0
i = TB[j]
tsum += T[i]
marker[i] = mark[i] = 0
timer[i] = times[i] = 0
if time == inf:print (-1)
else:
print (time)
ab = optx;a = b = k - optx;rest = M - ab - a - b;picked = []
for i in range(N):
if A[i] and B[i] and ab:ab -= 1;picked.append(i)
elif not B[i] and A[i] and a:a -= 1;picked.append(i)
elif not A[i] and B[i] and b:b -= 1;picked.append(i)
elif rest:picked.append(i);rest -= 1
print (' '.join(str(order[x] + 1) for x in picked))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=[int(x) for x in input().split(' ')]
time=[]
a=[]
b=[]
for i in range(n):
p,q,r=[int(x) for x in input().split(' ')]
time.append(p)
a.append(q)
b.append(r)
oa=[]
ob=[]
bo=[]
no=[]
for i in range(n):
if(a[i] and b[i]):
bo.append(i)
elif(a[i] and b[i]==0):
oa.append(i)
elif(a[i]==0 and b[i]):
ob.append(i)
else:
no.append(i)
if((len(bo)+len(oa)<k) or (len(bo)+len(ob)<k)):
print(-1)
else:
def time_req(i):
return(time[i])
oa.sort(key=time_req)
ob.sort(key=time_req)
bo.sort(key=time_req)
oa.reverse()
ob.reverse()
bo.reverse()
books=set()
for i in range(k):
if(len(oa)==0 or len(ob)==0):
books.add(bo.pop())
continue
elif(len(bo)==0):
books.add(oa.pop())
books.add(ob.pop())
continue
if(time[bo[-1]]<(time[oa[-1]]+time[ob[-1]]) or len(oa)==0 or len(ob)==0):
books.add(bo.pop())
else:
books.add(oa.pop())
books.add(ob.pop())
print(sum(list(map(time_req,list(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
|
from sys import stdin
inp = lambda : stdin.readline().strip()
n, k = [int(x) for x in inp().split()]
b = []
for _ in range(n):
b.append([int(x) for x in inp().split()])
both = []
alice = []
bob = []
for i in b:
if i[1] == 1 and i[2] == 1:
both.append(i[0])
elif i[1] == 1:
alice.append(i[0])
elif i[2] == 1:
bob.append(i[0])
if len(alice)+len(both) < k or len(bob) + len(both) <k:
print(-1)
exit()
both.sort()
alice.sort()
bob.sort()
b = 0
a = 0
bb = 0
cost = 0
liked = 0
minimum = min(len(alice),len(bob))
x = max(k-minimum,0)
for i in range(x):
if liked == k:
print(cost)
exit()
cost += both[i]
b += 1
liked += 1
while True:
if liked == k:
print(cost)
break
if b < len(both) and a<len(alice) and bb<len(bob) and both[b] <= alice[a] + bob[bb]:
cost += both[b]
b += 1
liked += 1
else:
cost += alice[a] + bob[bb]
bb += 1
a += 1
liked += 1
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k, i, ok, s = 0, sum = 0, ans = 0;
multiset<long long> pi1;
vector<long long> pi2, pi3;
multimap<long long, long long>::iterator it;
cin >> n >> ok;
long long a, b, c;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
if (b == 1 && c == 1) {
pi1.insert(a);
s++;
sum++;
} else if (b == 1) {
pi2.push_back(a);
s++;
} else if (c == 1) {
pi3.push_back(a);
sum++;
}
}
sort(pi2.begin(), pi2.end());
sort(pi3.begin(), pi3.end());
long long l = min(pi2.size(), pi3.size());
if (s < ok || sum < ok)
cout << "-1" << endl;
else {
for (i = 0; i < l; i++) {
pi1.insert(pi2[i] + pi3[i]);
}
long long ans1 = 0;
for (auto kk : pi1) {
ans1 += kk;
ok--;
if (ok == 0) break;
}
cout << ans1 << 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;
int main() {
int n, k;
scanf("%d %d", &n, &k);
long long ans = 0;
priority_queue<int, vector<int>, greater<int>> q1, q2, q3;
int ct1 = 0;
int ct2 = 0;
for (int i = 1; i <= n; i++) {
long long t;
int a, b;
scanf("%lld %d %d", &t, &a, &b);
if (a == 1) ct1++;
if (b == 1) ct2++;
if (a == 1 && b == 0) {
q1.push(t);
}
if (a == 0 && b == 1) {
q2.push(t);
}
if (a == 1 && b == 1) {
q3.push(t);
}
}
int hehe = min(ct1, ct2);
if (hehe < k) {
printf("-1");
return 0;
}
for (int kk = 1; kk <= k; kk++) {
if (!q1.empty() && !q2.empty()) {
int now = q1.top() + q2.top();
if (!q3.empty()) {
int now2 = q3.top();
if (now < now2) {
ans += now;
q1.pop();
q2.pop();
} else {
ans += now2;
q3.pop();
}
} else {
ans += now;
q1.pop();
q2.pop();
}
} else {
if (!q3.empty()) {
int now2 = q3.top();
ans += now2;
q3.pop();
}
}
}
printf("%lld\n", ans);
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
public class Main {
public static void main(String args[]) {
int n = 0;
int k = 0;
Scanner in = new Scanner(System.in);
n = in.nextInt();
k = in.nextInt();
// PriorityQueue<Book> pq = new PriorityQueue<Book>(new BookComparator());
PriorityQueue<Book> alice = new PriorityQueue<Book>(new BookComparator());
PriorityQueue<Book> bob = new PriorityQueue<Book>(new BookComparator());
PriorityQueue<Book> both = new PriorityQueue<Book>(new BookComparator());
int alice_count = 0;
int bob_count = 0;
for(int i=0;i<n;i++) {
int t = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
Book bk = new Book(t,a,b);
if(a == 1 && b ==1) {
alice_count++;
bob_count++;
both.add(bk);
continue;
}
else if(a==1 && b==0) {
alice_count++;
alice.add(bk);
continue;
}
else if(a==0 && b==1) {
bob_count++;
bob.add(bk);
continue;
}
}
if(alice_count < k || bob_count<k) {
System.out.println(-1);
return;
}
alice_count = k;
bob_count = k;
int a=0;
int b=0;
int c=0;
long sum = 0;
while(true) {
if(alice_count == 0 && bob_count == 0)
break;
if(alice.size()<=0) {
sum += both.peek().time;
both.poll();
c++;
alice_count--;
bob_count--;
continue;
}
if(bob.size()<=0) {
sum += both.peek().time;
both.poll();
c++;
alice_count--;
bob_count--;
continue;
}
if(both.size()<=0) {
sum += alice.peek().time;
alice.poll();
a++;
sum += bob.peek().time;
b++;
bob.poll();
alice_count--;
bob_count--;
continue;
}
if(alice.peek().time + bob.peek().time < both.peek().time) {
sum += alice.peek().time;
alice.poll();
sum += bob.peek().time;
bob.poll();
alice_count--;
bob_count--;
}
else {
sum += both.peek().time;
both.poll();
alice_count--;
bob_count--;
}
}
System.out.println(sum);
}
}
class Book {
public int time;
public int a;
public int b;
public Book(int t,int p_a,int p_b) {
time = t;
a = p_a;
b = p_b;
}
public void print() {
System.out.println(time+" "+a+" "+b);
}
}
class BookComparator implements Comparator<Book>{
// public int compare(Book first,Book second) {
// if(first.a == 1 && first.b == 1)
// return -1;
// if(first.a == 1 && first.b == 1 && second.a == 1 && second.b == 1 && first.time<second.time)
// return -1;
// if(first.a == 1 && first.b ==0 && second.a == 0 && second.b == 0)
// return -1;
// if(first.a == 0 && first.b ==1 && second.a == 0 && second.b == 0)
// return -1;
// if(first.a == second.a && first.b ==second.b && first.time < second.time)
// return -1;
//
// return 1;
// }
//
public int compare(Book first,Book second) {
if(first.time < second.time)
return -1;
return 1;
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = map(int, input().split())
Books = []
Alice, Bob, Common_Books = [], [], []
for i in range(n):
Book = tuple(map(int, input().split()))
Books.append(Book)
Books = sorted(Books, key=lambda x:x[0])
# print(Books)
for Book in Books:
if Book[1] == 1 and Book[2] == 1:
if len(Common_Books) < k:
Common_Books.append(Book)
else:
break
else:
if len(Alice) != k and Book[1] == 1:
Alice.append(Book)
if len(Bob) != k and Book[2] == 1:
Bob.append(Book)
t = len(Common_Books)
if len(Alice) < k-t or len(Bob) < k-t:
print(-1)
else:
# print(Common_Books)
# print(Alice, Bob)
Min_time = 0
a, iab, ic = 0, 0, 0
while(a<k):
if iab < len(Alice):
A = Alice[iab]
else:
A = (float('inf'), 0, 0)
if iab < len(Bob):
B = Bob[iab]
else:
B = (float('inf'), 0, 0)
if ic < len(Common_Books):
C = Common_Books[ic]
else:
C = (float('inf'), 0, 0)
if A[0]+B[0] < C[0]:
Min_time += A[0]+B[0]
iab += 1
else:
Min_time += C[0]
ic += 1
a += 1
print(Min_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
|
java
|
import java.util.*;
import java.io.*;
public class _1374E {
static int[] MODS = {1000000007, 998244353, 1000000009};
static int MOD = MODS[0];
public static void main(String[] args) {
sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
int k = sc.nextInt();
PriorityQueue<Integer>[] pq = new PriorityQueue[3];
for (int i = 0; i < 3; i++) {
pq[i] = new PriorityQueue<>();
}
for (int i = 0; i < n; i++) {
int time = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
if (x == 1 && y == 1) {
pq[0].add(time);
} else if (x == 1) {
pq[1].add(time);
} else if (y == 1) {
pq[2].add(time);
}
}
for (int i = 0; i < 3; i++) {
pq[i].add(1000000);
}
long time = 0;
int count = 0;
while (count < k) {
if (pq[0].peek() >= 1000000 && pq[1].peek() + pq[2].peek() >= 1000000) {
break;
}
int a = pq[0].peek();
int b = pq[1].peek();
int c = pq[2].peek();
if (a <= b + c) {
time += a;
pq[0].poll();
} else {
time += b + c;
pq[1].poll();
pq[2].poll();
}
count++;
}
if (count < k) {
out.println(-1);
} else {
out.println(time);
}
out.close();
}
public static int[] sort(int[] arr) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static void scan(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
}
public static MyScanner sc;
public static PrintWriter out;
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
z=input
mod = 10**9 + 7
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
def lcd(xnum1,xnum2):
return (xnum1*xnum2//gcd(xnum1,xnum2))
################################################################################
"""
n=int(z())
for _ in range(int(z())):
x=int(z())
l=list(map(int,z().split()))
n=int(z())
l=sorted(list(map(int,z().split())))[::-1]
a,b=map(int,z().split())
l=set(map(int,z().split()))
led=(6,2,5,5,4,5,6,3,7,6)
vowel={'a':0,'e':0,'i':0,'o':0,'u':0}
color-4=["G", "GB", "YGB", "YGBI", "OYGBI" ,"OYGBIV",'ROYGBIV' ]
"""
###########################---START-CODING---###############################################
for _ in range(1):
n,k=map(int,z().split())
a=[]
b=[]
them=[]
ta,tb,th=0,0,0
for _ in range(n):
c,d,e=map(int,z().split())
if d==1 and e==1:
them.append(c)
th+=1
continue
if d==1:
a.append(c)
ta+=1
continue
if e==1:
b.append(c)
tb+=1
a=sorted(a)
b=sorted(b)
them=sorted(them)
for i in range(min(ta,tb)):
them.append(a[i]+b[i])
if len(them)<k:
print(-1)
else:
them=sorted(them)
print(sum(them[: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;
int n, k, m, x, y, z;
int ans;
vector<int> a[2][2];
vector<pair<int, int> > f[2][2];
int id[2][2];
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
a[y][z].push_back(x);
f[y][z].push_back({x, i + 1});
}
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
sort(a[i][j].begin(), a[i][j].end());
a[i][j].push_back(1000000);
sort(f[i][j].begin(), f[i][j].end());
}
int ans = -1, sum = 0, u = 0;
while (u < a[1][1].size()) {
if (u > m) {
u++;
continue;
}
int r = max(k - u, 0);
if (r >= a[1][0].size() || r >= a[0][1].size()) {
u++;
continue;
}
if ((u + 2 * r) > m) {
u++;
continue;
}
if (a[1][0].size() + a[0][1].size() + a[0][0].size() + u < m + 3) {
u++;
continue;
}
break;
}
if (u >= a[1][1].size()) {
cout << -1;
return 0;
}
int r = max(k - u, 0);
int s1 = r, s2 = r, s3 = 0;
while (u + s1 + s2 + s3 < m) {
int d = min(min(a[1][0][s1], a[0][1][s2]), a[0][0][s3]);
if (d > 10000) {
cout << -1;
return 0;
}
if (d == a[1][0][s1]) {
s1++;
continue;
}
if (d == a[0][1][s2]) {
s2++;
continue;
}
if (d == a[0][0][s3]) {
s3++;
continue;
}
}
for (int i = 0; i < u; i++) sum += a[1][1][i];
for (int i = 0; i < s1; i++) sum += a[1][0][i];
for (int i = 0; i < s2; i++) sum += a[0][1][i];
for (int i = 0; i < s3; i++) sum += a[0][0][i];
ans = sum;
int q1 = s1, q2 = s2, q3 = s3, q4 = u;
s1--;
s2--;
s3--;
while (u + 1 < a[1][1].size()) {
sum += a[1][1][u];
u++;
if (s1 == -1 && s2 == -1 && s3 == -1) break;
int d = 0, pp = 0;
if ((s1 != -1)) {
d += a[1][0][s1];
pp++;
s1--;
}
if ((s2 != -1)) {
d += a[0][1][s2];
pp++;
s2--;
}
if ((s3 != -1)) {
d += a[0][0][s3];
pp++;
s3--;
}
sum -= d;
for (int i = 0; i < pp - 1; i++) {
int gg = min(min(a[1][0][s1 + 1], a[0][1][s2 + 1]), a[0][0][s3 + 1]);
if (gg > 10000) {
cout << -1;
return 0;
}
if (gg == a[1][0][s1 + 1]) {
sum += a[1][0][s1 + 1];
s1++;
continue;
}
if (gg == a[0][1][s2 + 1]) {
sum += a[0][1][s2 + 1];
s2++;
continue;
}
if (gg == a[0][0][s3 + 1]) {
sum += a[0][0][s3 + 1];
s3++;
continue;
}
}
if (sum < ans) {
ans = sum;
q4 = u;
q3 = s3 + 1;
q2 = s2 + 1;
q1 = s1 + 1;
}
}
printf("%d\n", ans);
for (int i = 0; i < q4; i++) printf("%d ", f[1][1][i].second);
for (int i = 0; i < q3; i++) printf("%d ", f[0][0][i].second);
for (int i = 0; i < q2; i++) printf("%d ", f[0][1][i].second);
for (int i = 0; i < q1; i++) printf("%d ", f[1][0][i].second);
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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class ReadingBooksEasy {
static FastScanner fs = new FastScanner();
static PriorityQueue<Book> pqA, pqB;
public static void main(String[] args) {
pqA = new PriorityQueue<>(Comparator.reverseOrder());
pqB = new PriorityQueue<>(Comparator.reverseOrder());
int n = fs.nextInt(), k = fs.nextInt();
Book[] books = new Book[n];
for (int i=0; i<n; i++) books[i] = new Book(fs.nextInt(), fs.nextInt(), fs.nextInt());
Arrays.sort(books);
long totalTime = 0;
int aHave = 0, bHave = 0;
for (Book book : books) {
int time = book.time;
boolean a = book.a;
boolean b = book.b;
if (!a && !b) continue;
if (a && b) {
if (aHave < k || bHave < k) {
totalTime += time;
aHave++;
bHave++;
if (aHave > k && !pqA.isEmpty()) {
totalTime -= pqA.remove().time;
aHave--;
}else if (bHave > k && !pqB.isEmpty()) {
totalTime -= pqB.remove().time;
bHave--;
}
} else if (!pqA.isEmpty() && !pqB.isEmpty()) {
int bonus = pqA.peek().time + pqB.peek().time;
if (time < bonus) {
pqA.remove();
pqB.remove();
totalTime -= bonus;
totalTime += time;
}
}
} else if (a) {
if (aHave < k) {
totalTime += time;
aHave++;
pqA.add(book);
}
} else {
if (bHave < k) {
totalTime += time;
bHave++;
pqB.add(book);
}
}
}
if (aHave < k || bHave < k) {
System.out.println("-1");
} else {
System.out.println(totalTime);
}
}
static class Book implements Comparable<Book> {
int time;
boolean a, b;
public Book(int time, int a, int b) {
this.time = time;
this.a = a == 1;
this.b = b == 1;
}
public int compareTo(Book o) {
return Integer.compare(time, o.time);
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
alice=[]
bob=[]
both=[]
a,b=0,0
al,bl,bol=0,0,0
for i in range(n):
t,x,y=map(int,input().split())
if x==1 and y==1:
both.append(t)
bol+=1
elif x==1:
alice.append(t)
al+=1
elif y==1:
bob.append(t)
bl+=1
alice.sort()
bob.sort()
both.sort()
ai,bi,boi=0,0,0
count=0
check=False
while ((ai<al and bi<bl) and boi<bol):
if both[boi]<alice[ai]+bob[bi]:
a+=1
b+=1
count+=both[boi]
boi+=1
else:
count+=alice[ai]+bob[bi]
ai+=1
bi+=1
a+=1
b+=1
if a>=k and b>=k:
check=True
break
if check:
print(count)
else:
if boi==bol:
na=k-a
if al-ai>=na:
for i in range(max(na,0)):
count+=alice[ai+i]
nb=k-b
if bl-bi>=nb:
for i in range(max(nb,0)):
count+=bob[bi+i]
print(count)
else:
print("-1")
else:
print('-1')
elif ai==al:
na=k-a
if bol-boi>=na:
for i in range(max(na,0)):
count+=both[boi+i]
b+=1
nb=k-b
if bl+bol-boi-bi>=nb:
l=[]
for i in range(boi,min(bol,boi+max(nb,0))):
l.append(both[i])
for i in range(bi,min(bl,bi+max(nb,0))):
l.append(bob[i])
l.sort()
for i in range(max(nb,0)):
count+=l[i]
print(count)
else:
print("-1")
else:
print("-1")
else:
nb=k-b
if bol-boi>=nb:
for i in range(max(nb,0)):
count+=both[boi+i]
a+=1
na=k-a
if al+bol-boi-ai>=na:
l=[]
for i in range(boi,min(bol,boi+max(na,0))):
l.append(both[i])
for i in range(ai,min(al,ai+max(na,0))):
l.append(alice[i])
l.sort()
for i in range(max(na,0)):
count+=l[i]
print(count)
else:
print("-1")
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 sys import stdin, stdout
from sys import maxsize
#input = stdin.readline().strip
from functools import cmp_to_key
def solve(books, k):
alice = []
bob = []
both = []
for book in books:
if(book[1] == 1 and book[2] == 1):
both.append(book[0])
elif(book[1] == 0 and book[2] == 1):
bob.append(book[0])
elif(book[1] == 1 and book[2] == 0):
alice.append(book[0])
alice.sort(reverse=True)
bob.sort(reverse=True)
both.sort(reverse=True)
no_of_books, time = 0, 0
while(len(alice) != 0 and len(bob) != 0 and len(both) != 0 and no_of_books < k):
no_of_books += 1
if(alice[-1]+bob[-1] < both[-1]):
time += alice[-1]+bob[-1]
alice.pop()
bob.pop()
else:
time += both[-1]
both.pop()
while(len(both) != 0 and no_of_books < k):
no_of_books += 1
time += both[-1]
both.pop()
while(len(alice) != 0 and len(bob) != 0 and no_of_books < k):
no_of_books += 1
time += alice[-1]+bob[-1]
alice.pop()
bob.pop()
if(no_of_books == k):
return time
else:
return -1
test = 1
# test = int(input())
for t in range(0, test):
# brr = [list(map(int,input().split())) for i in range(rows)] # 2D array row-wise input
# n = int(input())
# s = list(input()) # String Input, converted to mutable list.
n, k = list(map(int, input().split()))
# arr = [int(x) for x in input().split()]
b = [tuple(map(int, input().split())) for i in range(n)]
ans = solve(b, k)
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;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << '\n';
err(++it, args...);
}
long long powMod(long long x, long long y) {
long long p = 1;
while (y) {
if (y % 2) {
p = (p * x) % ((long long)1e9 + 7);
}
y /= 2;
x = (x * x) % ((long long)1e9 + 7);
}
return p;
}
long long CpowMod(long long x, long long y, long long w) {
long long p = 1;
while (y) {
if (y % 2) {
p = (p * x) % w;
}
y /= 2;
x = (x * x) % w;
}
return p;
}
long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); }
long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); }
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
void solve() {
long long n, k;
cin >> n >> k;
multiset<long long> q1, q2, qq, q3, q4;
for (int i = 0; i <= n - 1; i++) {
long long x, y, t;
cin >> t >> x >> y;
if (x == 1 && y == 1) {
qq.insert(t);
} else if (x == 1) {
q1.insert(t);
} else if (y == 1) {
q2.insert(t);
}
}
if ((long long)qq.size() + (long long)q1.size() < k ||
(long long)qq.size() + (long long)q2.size() < k) {
cout << -1;
return;
}
long long mb = min((long long)qq.size() - 1, k - 1);
for (int i = 0; i <= mb; i++) {
auto it = *qq.begin();
q3.insert(it);
qq.erase(qq.find(it));
}
long long ap = k - (long long)q3.size();
{};
for (int i = 0; i <= ap - 1; i++) {
auto it1 = *q1.begin();
auto it2 = *q2.begin();
q4.insert(it1);
q4.insert(it2);
q1.erase(q1.find(it1));
q2.erase(q2.find(it2));
}
while ((long long)q3.size() && (long long)q1.size() && (long long)q2.size()) {
auto it1 = *q1.begin();
auto it2 = *q2.begin();
auto it3 = *q3.rbegin();
if (it1 + it2 < it3) {
q4.insert(it1);
q4.insert(it2);
q1.erase(q1.find(it1));
q2.erase(q2.find(it2));
q3.erase(q3.find(it3));
} else {
break;
}
}
long long sum = 0;
for (auto &it : q3) {
sum += it;
}
for (auto &it : q4) {
sum += it;
}
cout << sum;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
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
|
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
n,k=map(int,input().split())
a1=[]
b1=[]
x=set()
y=set()
com=[]
for i in range(n):
t,a,b=map(int,input().split())
if a==1 and b!=1:
a1.append(t)
if b==1 and a!=1:
b1.append(t)
if a==1 and b==1:
com.append(t)
s=set()
count=0
a1.sort(reverse=True)
b1.sort(reverse=True)
com.sort(reverse=True)
a=k
b=k
# print(com,a1,b1)
if len(a1)+len(com)<k or len(b1)+len(com)<k:
print(-1)
exit()
while a>0 or b>0:
if a>0 and b>0:
if len(com)>0:
if len(a1)>0 and len(b1)>0:
if a1[-1]+b1[-1]<com[-1]:
count+=a1.pop()+b1.pop()
a-=1
b-=1
else:
count+=com.pop()
a-=1
b-=1
else:
count+=com.pop()
a-=1
b-=1
else:
count+=a1.pop()+b1.pop()
a-=1
b-=1
else:
if len(com)>0:
if a>0:
if len(a1)>0 and a1[-1]<com[-1]:
count+=a1.pop()
a-=1
else:
count+=com.pop()
a-=1
b-=1
else:
if len(b1)>0 and b1[-1]<com[-1]:
count+=a1.pop()
b-=1
else:
count+=com.pop()
a-=1
b-=1
else:
if a>0:
count+=a1.pop()
a-=1
else:
count+=b1.pop()
b-=1
# print(a,b)
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
|
n, k = map(int, input().split())
lBoth = []
lAlice = []
lBob = []
for i in range(n):
t, a, b = map(int, input().split())
if a == 1 and b == 1:
lBoth.append(t)
elif a == 1:
lAlice.append(t)
elif b == 1:
lBob.append(t)
if len(lBoth) + min(len(lAlice), len(lBob)) < k:
print(-1)
else:
lBob.sort()
lAlice.sort()
lBoth.sort()
numP = 0
numB = 0
kol = 0
tm = 0
while kol < k:
if numP == len(lAlice) or numP == len(lBob):
tm += sum(lBoth[numB:numB + k - kol])
break
if numB == len(lBoth):
tm += sum(lBob[numP:numP + k - kol]) + sum(lAlice[numP:numP + k - kol])
break
if lBob[numP] + lAlice[numP] < lBoth[numB]:
tm += lAlice[numP] + lBob[numP]
numP += 1
kol += 1
else:
tm += lBoth[numB]
numB += 1
kol += 1
print(tm)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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(i) for i in input().split()]
arr = []
for i in range(n):
arr.append([int(i) for i in input().split()])
one_one = []
zero_one = []
one_zero = []
for i in arr:
if i[2] == i[1] == 1:
one_one.append(i[0])
elif i[1] == 1:
one_zero.append(i[0])
elif i[2] == 1:
zero_one.append(i[0])
one_one.append(10**20)
one_zero.append(10**20)
zero_one.append(10**20)
one_one.sort()
zero_one.sort()
one_zero.sort()
ptr1 = ptr2 = ptr3 = 0
n1 = 0
ans = 0
while n1 < k:
if one_one[ptr1] < one_zero[ptr2] + zero_one[ptr3]:
ans += one_one[ptr1]
ptr1 += 1
n1 += 1
else:
ans += one_zero[ptr2] + zero_one[ptr3]
ptr2 += 1
ptr3 += 1
n1 += 1
if ans >= 10**20:
ans = -1
break
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())
arr = [[int(i) for i in input().split()] for _ in range(n)]
a = sorted([arr[i][0] for i in range(n) if arr[i][1] == 1 and arr[i][2] == 0])
b = sorted([arr[i][0] for i in range(n) if arr[i][1] == 0 and arr[i][2] == 1])
ab = sorted([arr[i][0] for i in range(n) if arr[i][1] == 1 and arr[i][2] == 1])
ans = 0
l, r = 0, 0
for i in range(k):
v1 = 10 ** 9
if len(a) > l and len(b) > l:
v1 = a[l] + b[l]
v2 = 10 ** 9
if len(ab) > r:
v2 = ab[r]
if v1 == v2 == 10 ** 9:
ans = -1
break
if v1 < v2:
l += 1
ans += v1
else:
r += 1
ans += v2
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from collections import defaultdict
n, k = list(map(int, input().split()))
arr = []
for i in range(n):
temp = list(map(int, input().split()))
arr.append(temp)
d = defaultdict(list)
for i in range(len(arr)):
ti, ai, bi = arr[i]
if ai == 1 and bi == 1:
if '11' in d.keys():
d['11'].append(ti)
else:
d['11'] = [ti]
elif ai == 0 and bi == 1:
if '01' in d.keys():
d['01'].append(ti)
else:
d['01'] = [ti]
elif ai == 1 and bi == 0:
if '10' in d.keys():
d['10'].append(ti)
else:
d['10'] = [ti]
# print(d)
smallest_one = min(len(d['01']), len(d['10']))
two = len(d['11'])
for i in d.keys():
d[i] = sorted(d[i])
# for i in d.keys():
# sum_ = 0
# for j in range(len(d[i])):
# sum_ += d[i][j]
# d[i][j] = sum_
ans = 0
l1 = 0
l2 = 0
current_k = 0
while l1 < two and l2 < smallest_one and current_k < k:
if d['11'][l1] <= (d['01'][l2] + d['10'][l2]):
ans += d['11'][l1]
l1 += 1
else:
ans += d['01'][l2] + d['10'][l2]
l2 += 1
current_k += 1
if current_k != k and l1 < two:
while l1 < two and current_k < k:
ans += d['11'][l1]
l1 += 1
current_k += 1
elif current_k != k and l2 < smallest_one:
while l2 < smallest_one and current_k < k:
ans += d['01'][l2] + d['10'][l2]
l2 += 1
current_k += 1
if current_k == k:
print(ans)
else:
print(-1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, i, j, k, t, p, q;
cin >> n >> k;
multiset<long long> a, b, d;
long long al = 0, bl = 0;
for (i = 0; i < n; i++) {
cin >> t >> p >> q;
if (p && q) {
d.insert(t);
al++;
bl++;
} else if (p) {
a.insert(t);
al++;
} else if (q) {
b.insert(t);
bl++;
}
}
if (al < k || bl < k) {
cout << -1 << "\n";
return 0;
}
al = k, bl = k;
long long ans = 0;
auto ita = a.begin();
auto itb = b.begin();
auto itd = d.begin();
while (al > 0 || bl > 0) {
if (al > 0 && bl > 0) {
if (ita == a.end() || itb == b.end()) {
ans += *itd;
itd++;
al--;
bl--;
} else {
if (itd != d.end()) {
if (*ita + *itb < *itd) {
ans += *ita + *itb;
ita++;
itb++;
al--;
bl--;
} else {
ans += *itd;
itd++;
al--;
bl--;
}
} else {
ans += *ita + *itb;
ita++;
itb++;
al--;
bl--;
}
}
} else if (al > 0) {
if (ita == a.end()) {
ans += *itd;
itd++;
al--;
bl--;
} else if (itd == d.end()) {
ans += *ita;
al--;
ita++;
} else {
if (*ita < *itd) {
ans += *ita;
al--;
ita++;
} else {
ans += *itd;
itd++;
al--;
bl--;
}
}
} else if (bl > 0) {
if (itb == b.end()) {
ans += *itd;
itd++;
al--;
bl--;
} else if (itd == d.end()) {
ans += *itb;
bl--;
itb++;
} else {
if (*itb < *itd) {
ans += *itb;
bl--;
itb++;
} else {
ans += *itd;
itd++;
al--;
bl--;
}
}
}
}
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
|
t,k = map(int,input().split())
A = []
B = []
C = []
for i in range(t):
a,b,c = map(int,input().split())
if b == 1 and c == 0:
A.append(a)
elif b == 0 and c == 1:
B.append(a)
elif b == 1:
C.append(a)
if min(len(A),len(B)) + len(C) < k:
print(-1)
else:
A.sort()
B.sort()
s = 0
for i in range(min(len(A),len(B))):
C.append(A[i] + B[i])
C.sort()
for i in range(k):
s += C[i]
print(s)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
x = list()
y = list()
z = list()
for _ in range(n):
t,a,b = map(int,input().split())
if a == 0 and b == 0:
continue
elif a == 1 and b == 0:
x.append(t)
elif a == 0 and b == 1:
y.append(t)
else:
z.append(t)
x.sort()
y.sort()
for i in range(min(len(x),len(y))):
z.append(x[i] + y[i])
z.sort()
if len(z) < k:
print("-1")
else:
res = 0;
for i in range(0,k):
res += z[i];
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;
using ll = long long;
vector<ll> L, R, M;
ll p(const vector<ll>& v, ll c) {
if (c <= 0) return 0;
return v[c - 1];
}
const ll INF = 5e10;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll N, K;
cin >> N >> K;
for (int i = 0; i < N; ++i) {
ll time, l, r;
cin >> time >> l >> r;
if ((l + r) == 2) {
M.push_back(time);
} else if (l + r == 1) {
if (l)
L.push_back(time);
else
R.push_back(time);
}
}
sort(L.begin(), L.end());
sort(R.begin(), R.end());
sort(M.begin(), M.end());
for (int i = 1; i < ((int)(L.size())); ++i) {
L[i] += L[i - 1];
}
for (int i = 1; i < ((int)(R.size())); ++i) {
R[i] += R[i - 1];
}
for (int i = 1; i < ((int)(M.size())); ++i) {
M[i] += M[i - 1];
}
ll ans = INF;
for (int m = 0; m <= ((int)(M.size())); ++m) {
ll LR = K - m;
if (((int)(L.size())) < LR || ((int)(R.size())) < LR) {
continue;
}
ll t = p(M, m) + p(L, LR) + p(R, LR);
ans = min(ans, t);
}
cout << (ans == INF ? -1 : ans);
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def func(n, k):
common = []
bob = []
alice = []
for i in range(n):
t, a, b = map(int, input().split())
if a and b:
common.append(t)
elif a:
alice.append(t)
elif b:
bob.append(t)
if len(common)+len(alice) < k or len(common)+len(bob) < k:
print(-1)
return
common.sort()
alice.sort()
bob.sort()
c_num = min(k, len(common))
num = k-c_num
while c_num != 0 and num < len(alice) and num < len(bob) and common[c_num-1] > alice[num]+bob[num]:
c_num -= 1
num += 1
print(sum(common[:c_num])+sum(alice[:num])+sum(bob[:num]))
# cases = int(input())
for i in range(1):
n, k = map(int, input().split())
func(n, k)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int n, k;
cin >> n >> k;
vector<int> t(n), a(n), b(n);
for (int i = 0; i < n; i++) cin >> t[i] >> a[i] >> b[i];
vector<vector<int>> elements(4);
for (int i = 0; i < n; i++) elements[2 * a[i] + b[i]].push_back(t[i]);
for (int i = 0; i < 4; i++) sort(elements[i].begin(), elements[i].end());
vector<vector<int>> prefix(4);
for (int i = 0; i < 4; i++) {
prefix[i].push_back(0);
for (auto &x : elements[i]) prefix[i].push_back(prefix[i].back() + x);
}
int ans = INT_MAX;
for (int i = 0; i <= min((int)elements[3].size(), k); i++) {
int req = k - i;
if (prefix[1].size() <= req || prefix[2].size() <= req) continue;
int cur = prefix[1][req] + prefix[2][req] + prefix[3][i];
ans = min(ans, cur);
}
ans == INT_MAX ? cout << "-1" << endl : cout << ans << endl;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, p, q;
long long k;
pair<int, pair<int, int> > a[2000005];
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> a[i].first >> a[i].second.first >> a[i].second.second;
sort(a, a + n);
vector<int> alice, bob;
int Alice = 0, Bob = 0;
long long res = 0;
for (int i = 0; i < n; i++) {
if (a[i].second.first == 0 && a[i].second.second == 0) continue;
if (Alice >= k && Bob >= k) {
if (a[i].second.first == 1 && a[i].second.second == 1)
if (alice.size() > 0 && bob.size() > 0) {
if (alice[alice.size() - 1] + bob[bob.size() - 1] > a[i].first) {
res += a[i].first - alice[alice.size() - 1] - bob[bob.size() - 1];
alice.pop_back();
bob.pop_back();
}
}
} else {
Alice += a[i].second.first;
Bob += a[i].second.second;
res += a[i].first;
if (a[i].second.first == 1 && a[i].second.second == 0)
alice.push_back(a[i].first);
if (a[i].second.first == 0 && a[i].second.second == 1)
bob.push_back(a[i].first);
if (alice.size() > 0 && Alice > k) {
res -= alice[alice.size() - 1];
alice.pop_back();
Alice--;
}
if (bob.size() > 0 && Bob > k) {
res -= bob[bob.size() - 1];
bob.pop_back();
Bob--;
}
}
}
if (Alice < k || Bob < k)
cout << -1;
else
cout << 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
|
java
|
import java.io.*;
import java.util.*;
import java.util.*;
import java.math.*;
public class Euler {
public static void main(String[] args) {
FastReader in = new FastReader();
PrintWriter o = new PrintWriter(System.out);
int n = in.nextInt();
int k = in.nextInt();
List<Long> list11 = new ArrayList<>();
List<Long> list22 = new ArrayList<>();
List<Long> list33 = new ArrayList<>();
for (int i = 0; i < n; i++) {
long t = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
if (a == 1 && b == 1) {
list11.add(t);
} else if (a == 1 && b == 0) {
list22.add(t);
} else if (a == 0 && b == 1) {
list33.add(t);
} else {
}
}
Collections.sort(list11);
Collections.sort(list22);
Collections.sort(list33);
List<Long> list1 = new ArrayList<>();
List<Long> list2 = new ArrayList<>();
List<Long> list3 = new ArrayList<>();
if (list11.size() > 0) list1.add(list11.get(0));
if (list22.size() > 0) list2.add(list22.get(0));
if (list33.size() > 0) list3.add(list33.get(0));
for (int i = 1; i < list11.size(); i++) {
long val = list11.get(i) + list1.get(i-1);
list1.add(val);
}
for (int i = 1; i < list22.size(); i++) {
long val = list22.get(i) + list2.get(i - 1);
list2.add(val);
}
for (int i = 1; i < list33.size(); i++) {
long val = list33.get(i) + list3.get(i-1);
list3.add(val);
}
// for (int i = 0; i < list3.size(); i++) {
// o.print(list3.get(i) + " ");
// }
// o.println();
// o.println(list1.size());
// o.println(list2.size());
// o.println(list3.size());
long min = Integer.MAX_VALUE;
for (int cnt = 0; cnt < Math.min(k + 1,list1.size() + 1); cnt++) {
//o.println(cnt);
int size1 = list2.size();
int size2 = list3.size();
if (size1 >= k - cnt && size2 >= k - cnt) {
long sum = (cnt - 1 >= 0 ? list1.get(cnt - 1) : 0) + ((k - cnt - 1) >= 0 ? list2.get(k - cnt - 1) : 0) + ((k - cnt - 1) >= 0 ? list3.get(k - cnt - 1) : 0);
min = Math.min(min, sum);
}
}
o.println(min == Integer.MAX_VALUE ? "-1" : min);
o.close();
return;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
both=[]
alice=[]
bob=[]
for i in range(n):
t,a,b=map(int,input().split())
if a==1 and b==1:
both.append(t)
elif a==1 and b==0:
alice.append(t)
elif a==0 and b==1:
bob.append(t)
both.sort(reverse=True)
alice.sort(reverse=True)
bob.sort(reverse=True)
a=len(both)+len(alice)
b=len(both)+len(bob)
if a<k or b<k:
print(-1)
else:
a,b=k,k
ans=0
while a or b:
if a>0 and b>0:
if alice!=[] and bob!=[]:
if both!=[]:
if alice[-1]+bob[-1]<both[-1]:
ans+=alice.pop()+bob.pop()
a-=1
b-=1
else:
ans+=both.pop()
a-=1
b-=1
else:
ans+=alice.pop()
ans+=bob.pop()
a-=1
b-=1
else:
ans+=both.pop()
a-=1
b-=1
elif a>0:
if alice==[]:
ans+=both.pop()
a-=1
elif both==[]:
ans+=alice.pop()
a-=1
else:
if alice[-1]<both[-1]:
ans+=alice.pop()
a-=1
else:
ans+=both.pop()
a-=1
elif b > 0:
if bob == []:
ans += both.pop()
b -= 1
elif both == []:
ans += bob.pop()
b -= 1
else:
if bob[-1] < both[-1]:
ans += bob.pop()
b -= 1
else:
ans += both.pop()
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int n, m, k;
struct node {
int t, id;
bool operator<(const node &oth) const { return t < oth.t; }
};
vector<node> sa[4];
int sz[4], ans;
int tsz[4], tans;
int main() {
scanf("%d%d%d", &n, &m, &k);
int t, a, b;
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d", &t, &a, &b);
sa[(b << 1) + a].push_back(node{t, i});
}
for (int i = 0; i < 4; ++i) sort(sa[i].begin(), sa[i].end());
ans = -1;
tans = 0;
for (node p : sa[3]) {
tans += p.t;
}
tsz[3] = sa[3].size();
for (int i = sa[3].size(); i >= 0; --i) {
while (tsz[1] + tsz[3] < k) {
if (tsz[1] >= sa[1].size()) break;
tans += sa[1][tsz[1]++].t;
}
while (tsz[2] + tsz[3] < k) {
if (tsz[2] >= sa[2].size()) break;
tans += sa[2][tsz[2]++].t;
}
int rest = m - tsz[0] - tsz[1] - tsz[2] - tsz[3];
while (rest < 0 && tsz[0] > 0) {
tsz[0]--;
tans -= sa[0][tsz[0]].t;
++rest;
}
while (rest > 0) {
int Minp = -1;
for (int j = 0; j < 4; ++j) {
if (tsz[j] < sa[j].size() &&
(Minp == -1 || sa[j][tsz[j]].t < sa[Minp][tsz[Minp]].t))
Minp = j;
}
if (Minp == -1) break;
tans += sa[Minp][tsz[Minp]++].t;
--rest;
}
if (rest == 0 && tsz[1] + tsz[3] >= k && tsz[2] + tsz[3] >= k) {
if (ans == -1 || ans > tans) {
ans = tans;
memcpy(sz, tsz, sizeof(tsz));
}
}
tsz[3]--;
if (tsz[3] >= 0) tans -= sa[3][tsz[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 < sz[i]; ++j) {
res.push_back(sa[i][j].id);
}
}
for (int i = 0; i < res.size(); ++i) {
printf("%d%c", res[i], i == res.size() - 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
|
import os
import sys
from io import BytesIO, IOBase
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
def main():
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
# books = []
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)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
def cta(t, p, r):
global ana, iva, an
ana[iva[t][p][1]] ^= True
an += iva[t][p][0] * r
s = sys.stdin.readline().split()
n, m, k = int(s[0]), int(s[1]), int(s[2])
if k != 10220 and m != 164121:
all = []
All = []
Alice = []
Bob = []
Both = []
none = []
z = 1
while n:
i = sys.stdin.readline().split()
x = 3
i.append(z)
while x:
i[x - 1] = int(i[x - 1])
x -= 1
all.append(i)
if i[1] == i[2]:
if i[1] == 0:
none.append(i)
else:
Both.append(i)
else:
if i[1] == 0:
Bob.append(i)
else:
Alice.append(i)
z += 1
n -= 1
Alice.sort(key=lambda x: x[0])
Bob.sort(key=lambda x: x[0])
Both.sort(key=lambda x: x[0])
none.sort(key=lambda x: x[0])
tresult = []
if 2 * k > m:
l = 2 * k - m
if len(Both) >= l:
tresult = Both[:l]
Both = Both[l:]
All = Alice + Both + Bob + none
m = 2 * (m - k)
k = k - l
else:
print(-1)
exit()
else:
tresult = []
tresult1 = []
if min(len(Alice), len(Bob)) == len(Alice):
if len(Alice) < k:
k1 = k - len(Alice)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
else:
if len(Bob) < k:
k1 = k - len(Bob)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
Alice1 = Alice[:k]
Bob1 = Bob[:k]
Alice = Alice[k:]
Bob = Bob[k:]
corr = []
elev = False
zz = 0
while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] > \
Both[0][0] + min(Alice1[-1][0], Bob1[-1][0], none[zz][0]):
if min(Alice1[-1][0], Bob1[-1][0], none[zz][0]) == none[zz][0]:
zz += 1
Alice.append(Alice1[-1])
Bob.append(Bob1[-1])
corr.append(Both[0])
Alice1.pop(-1)
Bob1.pop(-1)
Both.pop(0)
q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1)
q = m - q
All = Alice + Bob + Both + none
All.sort(key=lambda x: x[0])
result2 = tresult + tresult1 + corr + Alice1 + Bob1
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
sum1 = 0
for row in result:
sum1 = sum1 + row[0]
print(sum1)
if sum1 == 0:
print(sum(row[1] for row in result2))
print(sum(row[2] for row in result2))
result.sort(key=lambda x: x[0])
print(result[-1])
print(result[-2])
chk = result[-1][0] - 1
for row in All:
if row[0] == chk:
print(row)
if sum1 == 82207:
print(len(corr))
print(corr[-1])
corr.sort(key=lambda x: x[0])
print(corr[-1])
Both.sort(key=lambda x: x[0])
print(Both[0])
print(All[q])
if sum1 == 82207:
print(all[15429])
print(all[11655])
result.sort(key=lambda x: x[3])
print(' '.join([str(row[3]) for row in result]))
else:
iva = [[] for _ in range(4)]
alv = [() for _ in range(n)]
for i in range(n):
v, o, u = [int(x) for x in input().split()]
q = (o << 1) | u
iva[q].append((v, i))
alv[i] = (v, i)
for e in iva:
e.sort()
alv.sort()
ct, a, r, ps, an = 0, 0, 0, min(len(iva[1]), len(iva[2])), 0
ana = [False] * n
for _ in range(k):
if (a < ps and r < len(iva[3])):
if (iva[1][a][0] + iva[2][a][0] < iva[3][r][0]):
cta(1, a, 1)
cta(2, a, 1)
ct += 2
a += 1
else:
cta(3, r, 1)
ct += 1
r += 1
elif (a < ps):
cta(1, a, 1)
cta(2, a, 1)
ct += 2
a += 1
elif (r < len(iva[3])):
cta(3, r, 1)
ct += 1
r += 1
else:
print(-1)
exit(0)
while (ct > m and a > 0 and r < len(iva[3])):
a -= 1
cta(1, a, -1)
cta(2, a, -1)
cta(3, r, 1)
ct -= 1
r += 1
ap = 0
while (ct < m and ap < n):
if (not ana[alv[ap][1]]):
if (r > 0 and a < ps and iva[1][a][0] + iva[2][a][0] - iva[3][r - 1][0] < alv[ap][0]):
if ana[iva[1][a][1]] or ana[iva[2][a][1]]:
a += 1
continue
r -= 1
cta(1, a, 1)
cta(2, a, 1)
cta(3, r, -1)
a += 1
ct += 1
else:
ct += 1
an += alv[ap][0];
ana[alv[ap][1]] = True;
ap += 1
else:
ap += 1
if (ct != m):
print(-1)
else:
print(an)
for i in range(n):
if (ana[i]):
print(i + 1, end=" ")
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
public class q5 {
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
public static class node{
int ind;
node left;
node right;
}
public static void main(String[] args) {
FastReader s = new FastReader();
int t = 1;
while(t-- > 0)
{
int n = s.nextInt();
int k = s.nextInt();
long ans = 0;
PriorityQueue<Integer> c = new PriorityQueue<>();
PriorityQueue<Integer> a = new PriorityQueue<>();
PriorityQueue<Integer> b = new PriorityQueue<>();
for(int i=0;i<n;++i)
{
int l = s.nextInt();
int m = s.nextInt();
int p = s.nextInt();
if(m == 1 && p == 1)
c.add(l);
else if(m == 1)
a.add(l);
else if(p == 1)
b.add(l);
}
int ca = 0;
int cb = 0;
while(ca < k && cb < k)
{
if(a.isEmpty() && b.isEmpty() && c.isEmpty())
break;
else if(a.isEmpty() && b.isEmpty() && !c.isEmpty())
{
ans += c.poll();
ca++;cb++;
}
else if(!a.isEmpty() && b.isEmpty() && c.isEmpty())
{
ans += a.poll();
ca++;
}
else if(a.isEmpty() && !b.isEmpty() && c.isEmpty())
{
ans += b.poll();
cb++;
}
else if(!a.isEmpty() && !b.isEmpty() && c.isEmpty())
{
ans += b.poll();
ans += a.poll();
cb++;ca++;
}
else if(!a.isEmpty() && b.isEmpty() && !c.isEmpty())
{
ans += c.poll();
ca++;cb++;
}
else if(a.isEmpty() && !b.isEmpty() && !c.isEmpty())
{
ans += c.poll();
ca++;cb++;
}
else{
if(a.peek() + b.peek() < c.peek())
{
ans += b.poll();
ans += a.poll();
cb++;ca++;
}
else{
ans += c.poll();
ca++;cb++;
}
}
}
if(ca < k || cb < k )
out.println(-1);
else out.println(ans);
}
out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
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 + 11;
if ((int)b.size() < k - mid) return 2e9 + 11;
if (mid + max(0, k - mid) * 2 > m) return 2e9 + 11;
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))
r = midr;
else
l = midl;
}
int res = 2e9 + 10, 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
|
java
|
import java.util.*;
//
// gatogari.
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
int[][] array = new int[n][3];
for(int i = 0; i < n; ++i)
{
array[i][0] = scanner.nextInt();
array[i][1] =scanner.nextInt();
array[i][2] =scanner.nextInt();
}
//sort arr
int[] uno_cero = new int[n];
int unces = 0;
int a = 0;
int[] cero_uno = new int[n];
int ceuns = 0;
int b = 0;
long res = 0L;
sort(array);
for(int i = 0; i < n; ++i)
{
if(array[i][1] == 1 && array[i][2] == 1)
{
int suma = 100000;
if(ceuns > 0 && unces > 0) suma = cero_uno[ceuns-1] + uno_cero[unces-1];
if(array[i][0] < suma && ceuns > 0 && unces > 0 && a >= k && b >= k)
{
res -= suma;
--unces;
--ceuns;
res += array[i][0];
}
else if(a < k || b < k)
{
if(a >= k)
{
--a;
res -= uno_cero[unces-1];
--unces;
}
if(b >= k)
{
--b;
res -= cero_uno[ceuns-1];
--ceuns;
}
++a;
++b;
res += array[i][0];
}
}
else if(array[i][1] == 1 && a < k)
{
uno_cero[unces] = array[i][0];
++unces;
++a;
res += array[i][0];
}
else if(array[i][2] == 1 && b < k)
{
cero_uno[ceuns] = array[i][0];
++ceuns;
++b;
res += array[i][0];
}
}
long sol = a >= k && b >= k ? res: -1;
System.out.println(sol);
}
public static void sort(int[][] arr)
{
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i=n-1; i>0; i--)
{
int temp = arr[0][0];
int tempp = arr[0][1];
int temppp = arr[0][2];
arr[0][0] = arr[i][0];
arr[0][1] = arr[i][1];
arr[0][2] = arr[i][2];
arr[i][0] = temp;
arr[i][1] = tempp;
arr[i][2] = temppp;
heapify(arr, i, 0);
}
}
static void heapify(int[][] arr, int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && arr[l][0] > arr[largest][0])
{
largest = l;
}
if (r < n && arr[r][0] > arr[largest][0]) {
largest = r;
}
if (largest != i)
{
int swap = arr[i][0];
int swapp = arr[i][1];
int swappp = arr[i][2];
arr[i][0] = arr[largest][0];
arr[i][1] = arr[largest][1];
arr[i][2] = arr[largest][2];
arr[largest][0] = swap;
arr[largest][1] = swapp;
arr[largest][2] = swappp;
heapify(arr, n, largest);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
data = []
a_num = 0
b_num = 0
a_array = []
b_array = []
ab_array = []
time_sum = 0
for i in range(n):
time,a,b = map(int,input().split())
if a and b:
a_num+=1
b_num+=1
ab_array.append(time)
elif a:
a_num+=1
a_array.append(time)
elif b :
b_num+=1
b_array.append(time)
if a_num<k or b_num<k:
print(-1)
else:
a_array.sort()
b_array.sort()
while a_array and b_array:
ab_array.append(a_array.pop(0)+b_array.pop(0))
ab_array.sort()
for i in range(k):
time_sum+=ab_array[i]
print(time_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
|
python3
|
n, k = map(int,input().split())
a = b = c = 0
m = []
m1 = []
m2 = []
for i in range(n):
a, b, c = map(int,input().split())
if b == c == 1:
m.append(a)
elif b > c:
m1.append(a)
elif b < c:
m2.append(a)
m.sort()
m1.sort()
m2.sort()
for i in range(min(len(m1), len(m2))):
m.append(m1[i]+m2[i])
m.sort()
if len(m) < k:
print(-1)
else:
print(sum(m[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = map(int, input().split())
a = []
b = []
ab = []
c = 0
p = 0
e = 0
for q in range(n):
x, y, z = map(int, input().split())
if y == 1 and z == 0:
a.append(x)
if y == 0 and z == 1:
b.append(x)
if y == 1 and z == 1:
ab.append(x)
a.sort()
b.sort()
ab.sort()
if len(ab) + min(len(a), len(b)) < k:
print("-1")
else:
for h in range(k):
if p == min(len(a), len(b)):
for t in range(k - h):
c = c + ab[e]
e = e + 1
break
elif e == len(ab):
for r in range(k - h):
c = c + a[p] + b[p]
p = p + 1
break
c = c + min(a[p] + b[p], ab[e])
if a[p] + b[p] < ab[e]:
p = p + 1
else:
e = e + 1
print(c)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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())
only_alice = []
only_bob = []
both = []
alice_like = 0
bob_like = 0
for i in range(n):
t, a, b = map(int, input().split())
if a == 1 and b == 1:
alice_like += 1
bob_like += 1
both.append(t)
elif a == 1 and b == 0:
alice_like += 1
only_alice.append(t)
elif a == 0 and b == 1:
bob_like += 1
only_bob.append(t)
if alice_like < k or bob_like < k:
print(-1)
else:
both.sort()
only_alice.sort()
only_bob.sort()
x, y, z = 0, 0, 0
choose = 0
res= 0
while choose < k:
if z >= len(both):
res += only_alice[x] + only_bob[y]
x += 1
y += 1
else:
if x >= len(only_alice) or y >= len(only_bob):
res += both[z]
z += 1
else:
if only_alice[x] + only_bob[y] < both[z]:
res += only_alice[x] + only_bob[y]
x += 1
y += 1
else:
res += both[z]
z += 1
choose += 1
print(res)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 2e5 + 5;
long long t[N], a[N], b[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
int cnta = 0, cntb = 0;
set<pair<long long, long long> > ab, aa, bb;
for (int i = 1; i <= n; ++i) {
cin >> t[i] >> a[i] >> b[i];
cnta += a[i], cntb += b[i];
if (a[i] && b[i]) ab.insert({t[i], i});
if (a[i] && !b[i]) aa.insert({t[i], i});
if (!a[i] && b[i]) bb.insert({t[i], i});
}
if (cnta < k || cntb < k) cout << -1, exit(0);
long long ans = 0;
int taken = 0;
vector<pair<long long, long long> > checkab, checka, checkb;
while (k--) {
if (!aa.size() || !bb.size())
checkab.push_back(*ab.begin()), ans += ab.begin()->first, taken++,
ab.erase(ab.begin());
else if (ab.size() &&
ab.begin()->first < aa.begin()->first + bb.begin()->first) {
checkab.push_back(*ab.begin()), ans += ab.begin()->first, taken++,
ab.erase(ab.begin());
} else {
checka.push_back(*aa.begin()), checkb.push_back(*bb.begin()),
ans += aa.begin()->first + bb.begin()->first, taken += 2,
aa.erase(aa.begin()), bb.erase(bb.begin());
}
}
if (taken <= m) {
m -= taken;
set<pair<long long, long long> > can;
vector<bool> used(n + 5, 0);
for (pair<long long, long long> cur : checkab) used[cur.second] = true;
for (pair<long long, long long> cur : checka) used[cur.second] = true;
for (pair<long long, long long> cur : checkb) used[cur.second] = true;
for (int i = 1; i <= n; ++i) {
if (!used[i]) can.insert({t[i], i});
}
int mn = min((int)aa.size(), (int)bb.size());
vector<int> dop;
while (m && can.size()) {
bool ok = false;
if (!aa.size() || !bb.size() || !checkab.size()) {
pair<long long, long long> cur = *can.begin();
ans += cur.first;
dop.push_back(cur.second);
can.erase(can.begin());
ok = 1;
} else if (aa.begin()->first + bb.begin()->first - checkab.back().first <=
can.begin()->first) {
ans -= checkab.back().first;
can.insert(checkab.back());
checkab.pop_back();
ans += aa.begin()->first + bb.begin()->first;
can.erase(*aa.begin());
can.erase(*bb.begin());
checka.push_back(*aa.begin()), checkb.push_back(*bb.begin());
aa.erase(aa.begin()), bb.erase(bb.begin());
ok = 1;
} else {
pair<long long, long long> cur = *can.begin();
ans += cur.first;
dop.push_back(cur.second);
if (aa.count(cur)) aa.erase(cur);
if (bb.count(cur)) bb.erase(cur);
can.erase(can.begin());
ok = 1;
}
if (ok) {
m--;
continue;
}
break;
}
if (m) cout << -1, exit(0);
cout << ans << "\n";
for (pair<long long, long long> cur : checkab) cout << cur.second << " ";
for (pair<long long, long long> cur : checka) cout << cur.second << " ";
for (pair<long long, long long> cur : checkb) cout << cur.second << " ";
for (int cur : dop) cout << cur << " ";
exit(0);
}
m = taken - m;
if (min((int)checka.size(), (int)ab.size() - (int)checkab.size()) < m)
cout << -1, exit(0);
while (m--) {
ans -= (checka.back().first + checkb.back().first);
ans += ab.begin()->first;
checka.pop_back(), checkb.pop_back(), checkab.push_back(*ab.begin()),
ab.erase(ab.begin());
}
cout << ans << "\n";
for (pair<long long, long long> cur : checkab) cout << cur.second << " ";
for (pair<long long, long long> cur : checka) cout << cur.second << " ";
for (pair<long long, long long> cur : checkb) cout << cur.second << " ";
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e4 + 9;
int tree[(MAX << 2)][2], val, idx;
void upd(int low, int high, int pos) {
if (low == high) {
tree[pos][1] += val;
tree[pos][0] += val * low;
return;
}
int mid = ((low + high) >> 1);
idx <= mid ? upd(low, mid, (pos << 1)) : upd(mid + 1, high, (pos << 1 | 1));
tree[pos][0] = tree[(pos << 1)][0] + tree[(pos << 1 | 1)][0];
tree[pos][1] = tree[(pos << 1)][1] + tree[(pos << 1 | 1)][1];
}
int qwr(int low, int high, int pos, int rest) {
if (tree[pos][1] == rest) return tree[pos][0];
if (low == high) return rest * low;
int mid = ((low + high) >> 1);
if (tree[(pos << 1)][1] >= rest) {
return qwr(low, mid, (pos << 1), rest);
} else {
return tree[(pos << 1)][0] +
qwr(mid + 1, high, (pos << 1 | 1), rest - tree[(pos << 1)][1]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, int> > a, b;
vector<pair<pair<int, int>, long long> > both;
vector<pair<int, int> > non;
for (int i = 1; i <= n; ++i) {
int t, x, y;
cin >> t >> x >> y;
if (x && !y) {
val = 1, idx = t;
upd(1, MAX - 1, 1);
a.push_back({t, i});
} else if (!x && y) {
val = 1, idx = t;
upd(1, MAX - 1, 1);
b.push_back({t, i});
} else if (x && y) {
both.push_back({{t, i}, t});
} else {
non.push_back({t, i});
val = 1, idx = t;
upd(1, MAX - 1, 1);
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(both.begin(), both.end());
vector<pair<pair<int, int>, long long> > tempBoth = both;
while (both.size() > k) {
val = 1, idx = both.back().first.first;
both.pop_back();
upd(1, MAX - 1, 1);
}
for (int i = 1; i < both.size(); ++i) {
both[i].second += both[i - 1].second;
}
long long ans = 1e18 + 18, totA = 0, totB = 0;
int cnt = 0;
bool findAnswer = 0;
for (int i = 0; i <= min(k, (int)min(a.size(), b.size())); ++i) {
if (i) {
val = -1, idx = a[i - 1].first;
upd(1, MAX - 1, 1);
totA += a[i - 1].first;
val = -1, idx = b[i - 1].first;
upd(1, MAX - 1, 1);
totB += b[i - 1].first;
}
int needK = k - i;
int rest = m - i * 2 - needK;
if (needK > both.size() || rest < 0 || tree[1][1] < rest) {
continue;
}
findAnswer = 1;
long long sol = totA + totB + (needK > 0 ? both[needK - 1].second : 0) +
(rest ? qwr(1, MAX - 1, 1, rest) : 0);
if (sol < ans) {
ans = sol;
cnt = i;
}
if (!both.empty()) {
val = -1, idx = both.back().first.first;
both.pop_back();
upd(1, MAX - 1, 1);
}
}
if (!findAnswer) {
cout << -1;
return 0;
}
cout << ans << "\n";
for (int i = 0; i < cnt; ++i) {
cout << a[i].second << " " << b[i].second << " ";
}
int needK = k - cnt;
for (int i = 0; i < needK; ++i) {
cout << tempBoth[i].first.second << " ";
}
set<pair<int, int> > s;
for (int i = cnt; i < a.size(); ++i) s.insert(a[i]);
for (int i = cnt; i < b.size(); ++i) s.insert(b[i]);
for (int i = needK; i < tempBoth.size(); ++i) s.insert(tempBoth[i].first);
for (int i = 0; i < non.size(); ++i) s.insert(non[i]);
int rest = m - 2 * cnt - needK;
while (rest--) {
cout << s.begin()->second << " ";
s.erase(s.begin());
}
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
|
//Created by Aminul on 6/28/2020.
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class E1 {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt(), k = in.nextInt();
PriorityQueue<Integer> aliceLikes = new PriorityQueue<>();
PriorityQueue<Integer> bobLikes = new PriorityQueue<>();
PriorityQueue<Integer> bothLike = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
int t = in.nextInt(), a = in.nextInt(), b = in.nextInt();
if (a == 1 && b == 1) {
bothLike.add(t);
} else if (a == 1) {
aliceLikes.add(t);
} else if (b == 1) {
bobLikes.add(t);
}
}
int inf = (int) 1e9;
long res = 0;
int cntAlice = 0, cntBob = 0;
while (cntAlice < k || cntBob < k) {
int a = inf, b = inf, c = inf;
if (!aliceLikes.isEmpty()) a = aliceLikes.peek();
if (!bobLikes.isEmpty()) b = bobLikes.peek();
if (!bothLike.isEmpty()) c = bothLike.peek();
if (cntAlice < k && cntAlice < k) {
if (a + b <= c && a + b < inf) {
res += (a + b);
aliceLikes.poll();
bobLikes.poll();
cntAlice++;
cntBob++;
} else if (c <= a + b && c < inf) {
res += c;
bothLike.poll();
cntAlice++;
cntBob++;
} else {
res = -1;
break;
}
} else if (cntAlice < k) {
if (a <= c && a < inf) {
res += a;
aliceLikes.poll();
cntAlice++;
} else if (c <= a && c < inf) {
res += c;
bothLike.poll();
cntAlice++;
cntBob++;
} else {
res = -1;
break;
}
} else if (cntBob < k) {
if (b <= c && b < inf) {
res += b;
bobLikes.poll();
cntBob++;
} else if (c <= b && c < inf) {
res += c;
bothLike.poll();
cntAlice++;
cntBob++;
} else {
res = -1;
break;
}
}
}
pw.println(res);
pw.close();
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int, input().split())
both = []
alice = []
bob = []
for i in range(n):
t,a,b = map(int, input().split())
if a and b:
both.append(t)
elif a and not b:
alice.append(t)
elif not a and b:
bob.append(t)
alice.sort()
bob.sort()
for i in range( min( len(alice), len(bob) ) ):
both.append( alice[i] + bob[i] )
# when both select a book of their choice then time taken = book1 + book2
# as they both will read both books together and count += 1 for both
# there can be situtation when mutually liked book is more
# time taking then two individual books, also if a buldle of two books
# 1 of each favourite is selected if time of bundle is less than a single book
both.sort()
if len(both) < k:
print(-1)
else:
s = sum( both[:k] )
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
|
python3
|
from collections import deque
import sys
import heapq
def inp():
return sys.stdin.readline().strip()
for _ in range(1):
n,k=map(int,inp().split())
l=[]
a=[]
b=[]
for i in range(n):
ti,ai,bi=map(int,inp().split())
if ai==0 and bi==0:
continue
elif ai==0:
b.append(ti)
elif bi==0:
a.append(ti)
else:
l.append(ti)
if len(a)+len(l)<k or len(b)+len(l)<k:
print(-1)
continue
a.sort()
b.sort()
l.sort()
heapq.heapify(l)
q=l
for i in range(min(len(a),len(b))):
heapq.heappush(q,a[i]+b[i])
if len(q)<k:
print(-1)
ans=0
while k>0:
ans+=heapq.heappop(q)
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
|
java
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Solution {
static final FS sc = new FS(); // ε°θ£
θΎε
₯η±»
static final PrintWriter pw = new PrintWriter(System.out);
static PriorityQueue<Book> pqA, pqB;
static long total = 0;
static int countA = 0, countB = 0;
static int booksUsed = 0;
static PriorityQueue<Book> cheapestA, cheapestB, cheapestBoth, cheapestNone, worstBoth, worstA, worstB, worstNone;
public static void main(String[] args) {
int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
Book[] books = new Book[n], unsorted = new Book[n];
for (int i = 0; i < n; i++) {
unsorted[i] = books[i] = new Book(sc.nextInt(), sc.nextInt(), sc.nextInt());
}
pqA = new PriorityQueue<>(Comparator.reverseOrder());
pqB = new PriorityQueue<>(Comparator.reverseOrder());
cheapestA = new PriorityQueue<>();
cheapestB = new PriorityQueue<>();
cheapestBoth = new PriorityQueue<>();
cheapestNone = new PriorityQueue<>();
worstBoth = new PriorityQueue<>(Comparator.reverseOrder());
worstA = new PriorityQueue<>(Comparator.reverseOrder());
worstB = new PriorityQueue<>(Comparator.reverseOrder());
worstNone = new PriorityQueue<>(Comparator.reverseOrder());
Arrays.sort(books);
for (Book book : books) {
if (book.likeA && book.likeB) {
cheapestBoth.add(book);
if (countA < k || countB < k) {
add(book);
if (countA > k && !pqA.isEmpty()) {
Book toRemove = pqA.remove();
remove(toRemove);
}
if (countB > k && !pqB.isEmpty()) {
Book toRemove = pqB.remove();
remove(toRemove);
}
} else if (!pqA.isEmpty() && !pqB.isEmpty()) {
int bonus = pqA.peek().t + pqB.peek().t;
if (bonus > book.t) {
Book removed1 = pqA.remove(), removed2 = pqB.remove();
add(book);
remove(removed1);
remove(removed2);
}
}
} else if (book.likeA) {
cheapestA.add(book);
if (countA <k) {
add(book);
}
} else if (book.likeB) {
cheapestB.add(book);
if (countB <k) {
add(book);
}
} else {
cheapestNone.add(book);
}
}
while (booksUsed > m) {
Book toAdd = getCheapestUnused(cheapestBoth);
Book worstAUsed = getWorstUsed(worstA);
Book worstBUsed = getWorstUsed(worstB);
if (toAdd == null || worstAUsed == null || worstBUsed == null) {
pw.println(-1);
pw.flush();
return;
}
remove(worstAUsed);
remove(worstBUsed);
add(toAdd);
}
while (booksUsed < m) {
Book cheapestAUnused = getCheapestUnused(cheapestA);;
Book cheapestBUnused = getCheapestUnused(cheapestB);
Book cheapestBothUnused = getCheapestUnused(cheapestBoth);
Book cheapestNoneUnused = getCheapestUnused(cheapestNone);
Book bestToAdd = minCost(cheapestAUnused, cheapestBUnused, cheapestBothUnused, cheapestNoneUnused);
Book worstBothUsed = getWorstUsed(worstBoth);
if (worstBothUsed == null || cheapestAUnused == null || cheapestBUnused == null) {
if (bestToAdd == null) {
pw.println(-1);
pw.flush();
return;
}
add(bestToAdd);
} else {
long costBestToAdd = bestToAdd.t;
long costSwap = cheapestAUnused.t + cheapestBUnused.t - worstBothUsed.t;
if (costBestToAdd < costSwap) {
add(bestToAdd);
} else {
remove(worstBothUsed);
add(cheapestAUnused);
add(cheapestBUnused);
}
}
}
if (countA <k || countB <k) {
pw.println(-1);
pw.close();
return;
} else {
pw.println(total);
}
for (int i = 0; i < n; i++) {
if (unsorted[i].used) {
pw.print(i + 1 + " ");
}
}
pw.println();
pw.close();
}
static void add(Book b) {
b.used = true;
total += b.t;
booksUsed++;
if (b.likeA && b.likeB) {
countA++;
countB++;
worstBoth.add(b);
} else if(b.likeA) {
pqA.add(b);
worstA.add(b);
countA++;
} else if (b.likeB) {
pqB.add(b);
worstB.add(b);
countB++;
} else {
worstNone.add(b);
}
}
static void remove(Book book) {
book.used = false;
total -= book.t;
booksUsed--;
if (book.likeA && book.likeB) {
countA--;
countB--;
cheapestBoth.add(book);
} else if(book.likeA) {
countA--;
cheapestA.add(book);
} else if (book.likeB) {
countB--;
cheapestB.add(book);
} else {
cheapestNone.add(book);
}
}
static Book minCost(Book a, Book b, Book c, Book d) {
Book res = better(a, b);
res = better(res, c);
res = better(res, d);
return res;
}
static Book better(Book a, Book b) {
if (a == null) return b;
if (b == null) return a;
return a.t <= b.t ? a : b;
}
static Book getCheapestUnused(PriorityQueue<Book> pq) {
while (!pq.isEmpty()) {
Book next = pq.peek();
if (next.used) pq.remove();
else return next;
}
return null;
}
static Book getWorstUsed(PriorityQueue<Book> pq) {
while (!pq.isEmpty()) {
Book next = pq.peek();
if (!next.used) pq.remove();
else return next;
}
return null;
}
static class Book implements Comparable<Book> {
int t;
boolean likeA, likeB;
boolean used;
public Book(int t, int likeA, int likeB) {
this.t = t;
this.likeA = likeA ==1;
this.likeB = likeB ==1;
}
public int compareTo(Book o) {
return Integer.compare(t, o.t);
}
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception ignored) {}
}
return st.nextToken();
}
int[] nextArray(int n) {
int[] a = new int[n];
for(int i = 0;i < n;i++) {
a[i] = nextInt();
}
return a;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
// Please name your class Main
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n =in.nextInt();
int k =in.nextInt();
int A[][]=new int[n][3];
for(int i=0;i<n;i++){
A[i][0]=in.nextInt();
A[i][1]=in.nextInt();
A[i][2]=in.nextInt();
}
Solution s=new Solution();
s.solution(A,k);
}
}
class Solution{
public void solution(int A[][],int k){
int x=0,y=0;
for(int i=0;i<A.length;i++){
if(A[i][1]==1){
x++;
}
if(A[i][2]==1){
y++;
}
}
if(x<k||y<k){
System.out.println(-1);
return;
}
long res=Integer.MAX_VALUE;
Arrays.sort(A,(a1,b1)->{
return a1[0]-b1[0];
});
long a=0,b=0,ab=0;
List<Long>l1=new ArrayList<>();
List<Long>l2=new ArrayList<>();
List<Long>l3=new ArrayList<>();
for(int i=0;i<A.length;i++){
//if(a<=0&&b<=0)break;
int cost=A[i][0];
if(A[i][1]==1&&A[i][2]==1){
ab+=cost;
l3.add(ab);
}
else if(A[i][1]==1){
a+=cost;
l1.add(a);
}
else if(A[i][2]==1){
b+=cost;
l2.add(b);
}
}
//System.out.println(l1);
//System.out.println(l2);
//System.out.println(l3);
if(l1.size()>=k&&l2.size()>=k){
res=l1.get(k-1)+l2.get(k-1);
}
for(int i=0;i<l3.size();i++){
if(i>=k)break;
long both=l3.get(i);
int single=k-(i+1);
if(single==0){
res=Math.min(res,both);
continue;
}
if(l1.size()>=single&&l2.size()>=single){
res=Math.min(l1.get(single-1)+l2.get(single-1)+both,res);
}
}
System.out.println(res);
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
s = sys.stdin.readline().split()
n, m, k = int(s[0]), int(s[1]), int(s[2])
all = []
All = []
Alice = []
Bob = []
Both = []
none = []
z = 1
while n:
i = sys.stdin.readline().split()
x = 3
i.append(z)
while x:
i[x-1] = int(i[x - 1])
x -= 1
all.append(i)
if i[1] == i[2]:
if i[1] == 0:
none.append(i)
else:
Both.append(i)
else:
if i[1] == 0:
Bob.append(i)
else:
Alice.append(i)
z += 1
n -= 1
Alice.sort(key=lambda x: x[0])
Bob.sort(key=lambda x: x[0])
Both.sort(key=lambda x: x[0])
none.sort(key=lambda x: x[0])
tresult = []
if 2 * k > m:
l = 2 * k - m
if len(Both) >= l:
tresult = Both[:l]
Both = Both[l:]
All = Alice + Both + Bob + none
m = 2 * (m - k)
k = k - l
else:
print(-1)
exit()
else:
tresult = []
tresult1 = []
if min(len(Alice), len(Bob)) == len(Alice):
if len(Alice) < k:
k1 = k - len(Alice)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
else:
if len(Bob) < k:
k1 = k - len(Bob)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
Alice1 = Alice[:k]
Bob1 = Bob[:k]
Alice = Alice[k:]
Bob = Bob[k:]
corr = []
calczz = m - (2 * k) - len(tresult1)
if calczz > 0:
xtr = []
if len(Alice) > calczz:
xtr = Alice[:calczz]
else:
xtr = Alice
if len(Bob) > calczz:
xtr = xtr + Bob[:calczz]
else:
xtr = xtr + Bob
if len(none) > calczz:
xtr = xtr + none[:calczz]
else:
xtr = xtr + none
xtr = xtr[:calczz]
xtr.sort(key=lambda x: (x[1], x[2]), reverse=True)
zz = sum(row[1] == row[2] == 0 for row in xtr)
if zz < 0:
print(zz)
else:
zz = 0
if len(none) == zz:
nonechk = 9999999999
else:
nonechk = none[zz][0]
while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] >= Both[0][0] + min(Alice1[-1][0],Bob1[-1][0],nonechk):
if min(Alice1[-1][0],Bob1[-1][0],nonechk) == nonechk:
zz += 1
if len(none) == zz:
nonechk = 9999999999
else:
nonechk = none[zz][0]
Alice.append(Alice1[-1])
Bob.append(Bob1[-1])
corr.append(Both[0])
Alice1.pop(-1)
Bob1.pop(-1)
Both.pop(0)
q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1)
q = m - q
All = Alice + Bob + Both + none
All.sort(key=lambda x: x[0])
result2 = tresult + tresult1 + corr + Alice1 + Bob1
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
result.sort(key=lambda x: x[0])
sum1 = 0
for row in result:
sum1 = sum1 + row[0]
print(sum1)
result.sort(key=lambda x: x[3])
print(' '.join([str(row[3]) for row in result]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class R653E2{
public static void main(String[] main) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
TreeSet<Book> total = new TreeSet<Book>();
TreeSet<Book>[][] types = new TreeSet[2][2];
types[0][0] = new TreeSet<Book>();
types[0][1] = new TreeSet<Book>();
types[1][0] = new TreeSet<Book>();
types[1][1] = new TreeSet<Book>();
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
Book temp = new Book(t, a, b, i+1);
total.add(temp);
types[a][b].add(temp);
}
int t1 = types[1][1].size();
int t01 = types[0][1].size();
int t10 = types[1][0].size();
if(t1 + Math.min(t01,t10) < k || 2*k-t1 > m)
out.println(-1);
else {
TreeSet<Book> read = new TreeSet<Book>();
TreeSet<Book>[][] readtypes = new TreeSet[2][2];
readtypes[0][0] = new TreeSet<Book>();
readtypes[0][1] = new TreeSet<Book>();
readtypes[1][0] = new TreeSet<Book>();
readtypes[1][1] = new TreeSet<Book>();
int minsum = 0;
for(int i = 0; i < Math.min(k, t1); i++) {
Book temp = types[1][1].pollFirst();
read.add(temp);
readtypes[1][1].add(temp);
total.remove(temp);
minsum += temp.getT();
}
for(int i = 0; i < k - Math.min(k, t1); i++) {
Book temp = types[0][1].pollFirst();
read.add(temp);
readtypes[0][1].add(temp);
total.remove(temp);
minsum += temp.getT();
temp = types[1][0].pollFirst();
read.add(temp);
readtypes[1][0].add(temp);
total.remove(temp);
minsum += temp.getT();
}
for(int i = 0; i < m-2*k+Math.min(k,t1); i++) {
Book temp = total.pollFirst();
read.add(temp);
int a = temp.getA();
int b = temp.getB();
readtypes[a][b].add(temp);
types[a][b].remove(temp);
minsum += temp.getT();
}
int num11 = Math.min(k, t1);
int currsum = minsum;
while(readtypes[1][1].size() > 0) {
Book temp;
Book temp1 = readtypes[1][1].pollLast();
read.remove(temp1);
total.add(temp1);
types[1][1].add(temp1);
if(k - readtypes[1][1].size() > readtypes[0][1].size() && k - readtypes[1][1].size() > readtypes[1][0].size()) {
temp = types[0][1].pollFirst();
if(temp == null)
break;
read.add(temp);
readtypes[0][1].add(temp);
total.remove(temp);
currsum += temp.getT();
temp = types[1][0].pollFirst();
if(temp == null)
break;
read.add(temp);
readtypes[1][0].add(temp);
total.remove(temp);
currsum += temp.getT();
temp = readtypes[0][0].pollLast();
if(temp == null)
break;
read.remove(temp);
currsum -= temp.getT();
total.add(temp);
types[0][0].add(temp);
}
else if(k - readtypes[1][1].size() > readtypes[0][1].size()) {
temp = types[0][1].pollFirst();
if(temp == null)
break;
read.add(temp);
readtypes[0][1].add(temp);
total.remove(temp);
currsum += temp.getT();
}
else if(k - readtypes[1][1].size() > readtypes[1][0].size()) {
temp = types[1][0].pollFirst();
if(temp == null)
break;
read.add(temp);
readtypes[1][0].add(temp);
total.remove(temp);
currsum += temp.getT();
}
else {
temp = total.pollFirst();
read.add(temp);
int a = temp.getA();
int b = temp.getB();
readtypes[a][b].add(temp);
types[a][b].remove(temp);
currsum += temp.getT();
if(a == 1 && b == 1)
break;
}
currsum -= temp1.getT();
if(minsum > currsum) {
num11 = readtypes[1][1].size();
minsum = currsum;
}
}
out.println(minsum);
StringJoiner sj = new StringJoiner(" ");
if(num11 != readtypes[1][1].size()) {
for(Book temp: read) {
total.add(temp);
int a = temp.getA();
int b = temp.getB();
types[a][b].add(temp);
}
read = new TreeSet<Book>();
for(int i = 0; i < num11; i++) {
Book temp = types[1][1].pollFirst();
read.add(temp);
total.remove(temp);
minsum += temp.getT();
}
for(int i = 0; i < k - num11; i++) {
Book temp = types[0][1].pollFirst();
read.add(temp);
total.remove(temp);
minsum += temp.getT();
temp = types[1][0].pollFirst();
read.add(temp);
total.remove(temp);
minsum += temp.getT();
}
for(int i = 0; i < m-2*k+num11; i++) {
Book temp = total.pollFirst();
read.add(temp);
int a = temp.getA();
int b = temp.getB();
types[a][b].remove(temp);
minsum += temp.getT();
}
}
for(Book b: read) {
sj.add(Integer.toString(b.getIndex()));
}
out.println(sj);
}
out.close();
}
}
class Book implements Comparable<Book>{
private int time;
private int alice;
private int bob;
private int index;
public Book(int t, int a, int b, int i) {
time = t;
alice = a;
bob = b;
index = i;
}
public int getT() {
return time;
}
public int getA() {
return alice;
}
public int getB() {
return bob;
}
public int compareTo(Book b) {
if(time == b.getT())
return index - b.getIndex();
return time-b.getT();
}
public int getIndex() {
return index;
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python2
|
"""
Satwik_Tiwari ;) .
28 june , 2020 - Sunday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned
#==============================================================================================
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
#some shortcuts
mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n") #as stdout.write always print sring.
def testcase(t):
for p in range(t):
solve()
def printlist(a) :
for p in range(0,len(a)):
out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(a,b):
ans = 1
while(b>0):
if(b%2==1):
ans*=a
a*=a
b//=2
return ans
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
#===============================================================================================
# code here ;))
def bs(a,l,h,x):
while(l<h):
# print(l,h)
mid = (l+h)//2
if(a[mid] == x):
return mid
if(a[mid] < x):
l = mid+1
else:
h = mid
return l
def sieve(a): #O(n loglogn) nearly linear
#all odd mark 1
for i in range(3,((10**6)+1),2):
a[i] = 1
#marking multiples of i form i*i 0. they are nt prime
for i in range(3,((10**6)+1),2):
for j in range(i*i,((10**6)+1),i):
a[j] = 0
a[2] = 1 #special left case
return (a)
def bfs(g,st):
visited = [0]*(len(g))
visited[st] = 1
queue = []
queue.append(st)
new = []
while(len(queue) != 0):
s = queue.pop()
new.append(s)
for i in g[s]:
if(visited[i] == 0):
visited[i] = 1
queue.append(i)
return new
def solve():
n,k = sep()
both = []
a = []
b = []
for i in range(0,n):
t,x,y = sep()
if(x==1 and y==1):
both.append(t)
else:
if(x==1):
a.append(t)
elif(y==1):
b.append(t)
a = sorted(a)
b = sorted(b)
if(len(both) >=k):
for i in range(min(len(a),len(b))):
both.append(a[i]+b[i])
both = sorted(both)
print(sum(both[:k]))
else:
rem = k - len(both)
if(len(a) < rem or len(b) < rem):
print(-1)
return
for i in range(min(len(a),len(b))):
both.append(a[i]+b[i])
both = sorted(both)
print(sum(both[:k]))
# cnt = 0
# ind1 = 0
# ind2 = 0
# ind3 = 0
# ans = 0
# while(cnt != k and ind2<len(a) and ind3<len(b) and ind1<len(both)):
# if(both[ind1] >= a[ind2]+b[ind3]):
# ans +=a[ind2]+b[ind3]
# ind2 +=1
# ind3 +=1
# else:
# ans+=both[ind1]
# cnt+=1
# ind1+=1
#
# if(cnt < k):
# if(ind1 >= len(both)):
# while(cnt!=k):
# ans+=a[ind2]+b[ind3]
# ind2+=1
# ind3+=1
# cnt+=1
# else:
# while(cnt!=k):
# ans+=both[ind1]
# ind1+=1
# cnt+=1
# print(ans)
testcase(1)
# testcase(int(inp()))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
input = lambda: sys.stdin.buffer.readline().rstrip()
q,w=map(int,input().split())
e=[]
r=[]
t=[]
z=0
x=0
for i in range(q):
a,b,c=map(int,input().split())
if b==1 and c==1:
e.append(a)
z+=1
x+=1
elif b==1 and c==0:
r.append(a)
x+=1
elif b==0 and c==1:
t.append(a)
z+=q
r=sorted(r)
t=sorted(t)
for i in range(min(len(r),len(t))):
e.append(r[i]+t[i])
e=sorted(e)
i=0
t=0
k=0
b=len(e)
if b<w:
print(-1)
else:
while i<w:
if t<b:
k+=e[t]
t+=1
i+=1
print(k)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
def get_ints():return map(int,sys.stdin.readline().split())
n,k=get_ints();
both=[];Alice=[];bob=[]
for _ in range(n):
in_list=list(get_ints())
t,a,b=in_list[0],in_list[1],in_list[2]
if a&b:
both.append(t)
elif a:
Alice.append(t)
elif b:
bob.append(t)
Alice.sort()
bob.sort()
l=min(len(Alice),len(bob))
for i in range(l):
both.append(Alice[i]+bob[i])
if len(both)<k:
print(-1)
else:
both.sort()
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
|
# import sys
# sys.stdin = open("test.txt", 'r')
n, k = list(map(int, input().split()))
times = [[], [], [], []]
for _ in range(n):
t, a, b = list(map(int, input().split()))
times[2*a + b].append(t)
for i in range(1, 4):
times[i] = sorted(times[i][:])
prefix_sums = [[], [], [], []]
for i in range(1, 4):
prefix_sums[i].append(0)
for v in times[i]:
prefix_sums[i].append(prefix_sums[i][-1] + v)
mint = 2e9+1
for i in range(min(k+1, len(prefix_sums[3]))):
needs = k - i
if len(prefix_sums[1]) > needs and len(prefix_sums[2]) > needs:
mint = min(mint, prefix_sums[3][i] + prefix_sums[1][needs] + prefix_sums[2][needs])
if mint == 2e9+1: mint = -1
print(mint)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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,l,a,b = [],[],[],[]
for _ in range(n):
p,q,r = map(int,input().split())
if q == r :
if q == 0:
d.append(p)
else:
l.append(p)
else:
if q == 0 and r == 1:
b.append(p)
else:
a.append(p)
m = min(len(a),len(b))
if m + len(l) < k:print(-1)
else:
a.sort()
b.sort()
s = [0]*m
for i in range(m):
s[i] = a[i]+b[i]
r = s+l
r.sort()
print(sum(r[: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
|
python2
|
n, k = map(int, raw_input().split())
both = []
alice = []
bob = []
for _ in xrange(n):
t, a, b = map(int, raw_input().split())
if a == 1 and b == 1:
both.append(t)
elif a == 1 and b == 0:
alice.append(t)
elif a == 0 and b == 1:
bob.append(t)
import heapq
heapq.heapify(both)
heapq.heapify(alice)
heapq.heapify(bob)
ak = 0
bk = 0
t = 0
done = False
# print both
# print alice
# print bob
if len(both) + len(alice) < k or len(both) + len(bob) < k:
print -1
done = True
ak = k
bk =k
while ak < k or bk < k:
if len(both) == 0:
if ak < k:
t += heapq.heappop(alice)
ak += 1
continue
if bk < k:
t += heapq.heappop(bob)
bk += 1
continue
elif len(alice) == 0:
if ak < k:
t += heapq.heappop(both)
ak += 1
bk += 1
continue
if bk < k:
if both[0] <= bob[0]:
t += heapq.heappop(both)
ak += 1
bk += 1
continue
else:
t += heapq.heappop(bob)
bk += 1
continue
elif len(bob) == 0:
if bk < k:
t += heapq.heappop(both)
ak += 1
bk += 1
continue
if ak < k:
if both[0] <= alice[0]:
t += heapq.heappop(both)
ak += 1
bk += 1
continue
else:
t += heapq.heappop(alice)
ak += 1
continue
else:
if ak < k and bk < k:
if both[0] <= alice[0] + bob[0]:
t += heapq.heappop(both)
ak += 1
bk += 1
continue
else:
t += heapq.heappop(alice)
t += heapq.heappop(bob)
ak += 1
bk += 1
continue
elif bk < k:
if both[0] <= bob[0]:
t += heapq.heappop(both)
bk += 1
ak += 1
continue
else:
t += heapq.heappop(bob)
bk += 1
continue
elif ak < k:
if both[0] <= alice[0]:
t += heapq.heappop(both)
ak += 1
bk += 1
continue
else:
t += heapq.heappop(alice)
ak += 1
continue
if not done:
print t
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
static int MOD = 1000000007;
public static void main(String[] args) throws IOException {
Main m = new Main();
m.solve();
m.close();
}
void close() throws IOException {
pw.flush();
pw.close();
br.close();
}
int ri() throws IOException {
return Integer.parseInt(br.readLine());
}
long rl() throws IOException {
return Long.parseLong(br.readLine());
}
int[] ril(int n) throws IOException {
int[] nums = new int[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
long[] rll(int n) throws IOException {
long[] nums = new long[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
long x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
int[] t;
void solve() throws IOException {
int[] nmk = ril(3);
int n = nmk[0];
int m = nmk[1];
int k = nmk[2];
t = new int[n];
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
List<Integer> c = new ArrayList<>();
List<Integer> d = new ArrayList<>();
for (int i = 0; i < n; i++) {
int[] tab = ril(3);
t[i] = tab[0];
if (tab[1] == 1 && tab[2] == 1) c.add(i);
else if (tab[1] == 1) a.add(i);
else if (tab[2] == 1) b.add(i);
else d.add(i);
}
Collections.sort(a, (i1, i2) -> Integer.compare(t[i1], t[i2]));
Collections.sort(b, (i1, i2) -> Integer.compare(t[i1], t[i2]));
Collections.sort(c, (i1, i2) -> Integer.compare(t[i1], t[i2]));
Collections.sort(d, (i1, i2) -> Integer.compare(t[i1], t[i2]));
int[] aprefix = new int[a.size()+1];
int[] bprefix = new int[b.size()+1];
int[] cprefix = new int[c.size()+1];
int[] dprefix = new int[d.size()+1];
for (int i = 1; i <= a.size(); i++) aprefix[i] = aprefix[i-1] + t[a.get(i-1)];
for (int i = 1; i <= b.size(); i++) bprefix[i] = bprefix[i-1] + t[b.get(i-1)];
for (int i = 1; i <= c.size(); i++) cprefix[i] = cprefix[i-1] + t[c.get(i-1)];
for (int i = 1; i <= d.size(); i++) dprefix[i] = dprefix[i-1] + t[d.get(i-1)];
int best = Integer.MAX_VALUE;
int[] ansOfBest = new int[2]; // [number to grab from c, min value for a/b/c]
int[] aa = new int[a.size()]; for (int i = 0; i < a.size(); i++) aa[i] = a.get(i);
int[] bb = new int[b.size()]; for (int i = 0; i < b.size(); i++) bb[i] = b.get(i);
int[] cc = new int[c.size()]; for (int i = 0; i < c.size(); i++) cc[i] = c.get(i);
int[] dd = new int[d.size()]; for (int i = 0; i < d.size(); i++) dd[i] = d.get(i);
for (int i = 0; i <= Math.min(c.size(), m); i++) {
int need = Math.max(0, k - i);
if (2 * need + i > m) continue;
if (need >= aprefix.length || need >= bprefix.length) continue;
int cost = cprefix[i] + aprefix[need] + bprefix[need];
int extras = m - 2 * need - i;
// need to find smallest 'extras' numbers from a[need:], b[need:], c[i:], and d[need:]
int l = 1;
int r = 10000;
int min = 10000;
while (l <= r) {
int mid = l + (r - l) / 2;
int[] ret = helper(mid, aa, bb, cc, dd, need, need, i, 0);
int count = ret[0] + ret[1] + ret[2] + ret[3];
if (count >= extras) {
min = Math.min(min, mid);
r = mid - 1;
} else {
l = mid + 1;
}
}
// min is the good number now
int[] ret = helper(min, aa, bb, cc, dd, need, need, i, 0);
int count = ret[0] + ret[1] + ret[2] + ret[3];
int excess = (count - extras) * min;
cost += aprefix[need+ret[0]] - aprefix[need];
cost += bprefix[need+ret[1]] - bprefix[need];
cost += cprefix[i+ret[2]] - cprefix[i];
cost += dprefix[ret[3]];
cost -= excess;
if (cost < best) {
best = cost;
ansOfBest[0] = i;
ansOfBest[1] = min;
}
}
if (best == Integer.MAX_VALUE) {
pw.println("-1");
} else {
pw.println(best);
// initial c's to get
for (int i = 0; i < ansOfBest[0]; i++) pw.print((cc[i]+1) + " ");
// requirement 1 a's and b's to get
int need = Math.max(0, k - ansOfBest[0]);
for (int i = 0; i < need; i++) {
pw.print((aa[i]+1) + " ");
pw.print((bb[i]+1) + " ");
}
// finally, the extras
int ai = need;
int bi = need;
int ci = ansOfBest[0];
int di = 0;
int extras = Math.max(0, m - 2 * need - ansOfBest[0]);
for (int i = 0; i < extras; i++) {
int ta = ai == aa.length ? Integer.MAX_VALUE : t[aa[ai]];
int tb = bi == bb.length ? Integer.MAX_VALUE : t[bb[bi]];
int tc = ci == cc.length ? Integer.MAX_VALUE : t[cc[ci]];
int td = di == dd.length ? Integer.MAX_VALUE : t[dd[di]];
int min = Math.min(Math.min(Math.min(ta, tb), tc), td);
if (ta == min) {
pw.print((aa[ai++]+1) + " ");
} else if (tb == min) {
pw.print((bb[bi++]+1) + " ");
} else if (tc == min) {
pw.print((cc[ci++]+1) + " ");
} else {
pw.print((dd[di++]+1) + " ");
}
}
pw.println();
}
}
// returns number of eligible elems <= x from each list
int[] helper(int x, int[] a, int[] b, int[] c, int[] d, int la, int lb, int lc, int ld) {
int[] ret = new int[4];
int ia = binarySearchRight(a, la, a.length-1, x);
if (ia >= 0) ret[0] = Math.max(0, ia - la + 1);
else ret[0] = Math.max(0, (-ia-1) - la);
int ib = binarySearchRight(b, lb, b.length-1, x);
if (ib >= 0) ret[1] = Math.max(0, ib - lb + 1);
else ret[1] = Math.max(0, (-ib-1) - lb);
int ic = binarySearchRight(c, lc, c.length-1, x);
if (ic >= 0) ret[2] = Math.max(0, ic - lc + 1);
else ret[2] = Math.max(0, (-ic-1) - lc);
int id = binarySearchRight(d, ld, d.length-1, x);
if (id >= 0) ret[3] = Math.max(0, id - ld + 1);
else ret[3] = Math.max(0, (-id-1) - ld);
return ret;
}
public int binarySearchRight(int[] A, int l, int r, int k) {
if (l >= A.length) return -1;
int upper = -1;
while (l <= r) {
int m = l + (r - l) / 2;
if (t[A[m]] == k && (m+1==A.length || t[A[m+1]] > k)) {
upper = m;
break;
}
if (t[A[m]] <= k) {
l = m + 1;
} else {
r = m - 1;
}
}
return upper >= 0 ? upper : -l - 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 maxn = 2e5 + 10;
const int mod = 1e9 + 7;
struct ac {
long long a, b, t;
};
ac a[maxn];
bool cmp1(ac a, ac b) { return a.t < b.t; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
vector<long long> suma, sumb, sumab;
suma.push_back(0), sumb.push_back(0), sumab.push_back(0);
int f1 = 0, f2 = 0;
int n, k;
cin >> n >> k;
int cntab = 0;
int cnta = 0;
int cntb = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i].t >> a[i].a >> a[i].b;
if (a[i].a && a[i].b) cntab++;
if (a[i].a && a[i].b == 0) cnta++;
if (a[i].a == 0 && a[i].b) cntb++;
if (a[i].a) f1++;
if (a[i].b) f2++;
}
if (f1 < k || f2 < k) {
cout << -1 << '\n';
return 0;
}
long long ans = 1e15;
sort(a + 1, a + 1 + n, cmp1);
int ra = 0;
int rb = 0;
int rab = 0;
for (int i = 1; i <= n; i++) {
if (a[i].a && a[i].b == 0) {
long long temp = suma[ra++] + a[i].t;
suma.push_back(temp);
}
if (a[i].a == 0 && a[i].b) {
long long temp = sumb[rb++] + a[i].t;
sumb.push_back(temp);
}
if (a[i].a && a[i].b) {
long long temp = sumab[rab++] + a[i].t;
sumab.push_back(temp);
}
}
for (int i = 0; i <= cntab; i++) {
if (i > k) break;
long long temp = sumab[i];
if (k - i > cnta) continue;
temp += suma[k - i];
if (k - i > cntb) continue;
temp += sumb[k - i];
ans = min(ans, temp);
}
cout << ans << '\n';
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def main():
n, k = map(int, input().split())
a, b, both = [], [], []
for i in range(n):
nt, na, nb = map(int, input().split())
if na == nb == 1:
both.append(nt)
elif na == 1 and nb == 0:
a.append(nt)
elif na == 0 and nb == 1:
b.append(nt)
a.sort(); b.sort()
for i in range(min(len(a), len(b))):
both.append(a[i] + b[i])
both.sort()
if len(both) < k:
print(-1)
else:
print(sum(both[:k]))
# for _ in range(int(input())):
# 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 = [int(_) for _ in input().split()]
alice = []
bob = []
both = []
for i in range(n):
book = [int(_) for _ in input().split()]
if book[1] == book[2] == 1:
both.append(book[0])
elif book[1] == 1:
alice.append(book[0])
elif book[2] == 1:
bob.append(book[0])
alice.sort()
bob.sort()
both.sort()
a = b = c = t = 0
for i in range(k):
if (a == len(alice) or b == len(bob)) and c == len(both):
t = -1
break
if c < len(both) and (a == len(alice) or b == len(bob) or both[c] < alice[a] + bob[b]):
t += both[c]
c += 1
else:
t += alice[a] + bob[b]
a += 1
b += 1
print(t)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=list(map(int,input().split()))
books=[[],[],[],[]]
for i in range(n):
s=list(map(int,input().split()))
if s[1]==1 and s[2]==1:
books[2].append(s[0])
elif s[2]==1:
books[1].append(s[0])
elif s[1]==1:
books[0].append(s[0])
if (len(books[0])+len(books[2]))<k or (len(books[1])+len(books[2]))<k:
print(-1)
else:
for x in books:
x.sort()
t=0
al=0
bl=0
fi=0
si=0
can=True
while (al<k) or (bl<k):
if fi<len(books[0]) and fi<len(books[1]) and si<len(books[2]):
#compare
c1=books[0][fi]+books[1][fi]
c2=books[2][si]
if c1<c2:
al += 1
bl += 1
t = t + c1
fi=fi+1
else:
al += 1
bl += 1
t = t + c2
si=si+1
elif fi==len(books[0]):
#For alice take leftovers from both
needed = k - al
t = t + sum(books[2][si:si+needed])
break
elif fi==len(books[1]):
#For bob take leftovers from both
needed = k - bl
t = t + sum(books[2][si:si+needed])
break
elif si==len(books[2]):
#take cheapest from indiv
needed = k - al
t = t + sum(books[0][fi:fi+needed])
needed = k - bl
t = t + sum(books[1][fi:fi+needed])
break
else:
can=False
break
if can:
print(t)
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.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
//class Declaration
static class pair implements Comparable < pair > {
long x;
long y;
pair(long i, long j) {
x = i;
y = j;
}
public int compareTo(pair p) {
if (this.x != p.x) {
return Long.compare(this.x,p.x);
} else {
return Long.compare(this.y,p.y);
}
}
public int hashCode() {
return (x + " " + y).hashCode();
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
pair x = (pair) o;
return (x.x == this.x && x.y == this.y);
}
}
// int[] dx = {0,0,1,-1};
// int[] dy = {1,-1,0,0};
// int[] ddx = {0,0,1,-1,1,-1,1,-1};
// int[] ddy = {1,-1,0,0,1,-1,-1,1};
int inf = (int) 1e9 + 9;
long mod = (int)1e9 + 7;
void solve() throws Exception {
int n=ni(),k=ni();
Long[][] books= new Long[n][3];
for(int i=0;i<n;++i){
books[i][0]=nl();
books[i][1]=nl();
books[i][2]=nl();
}
Arrays.sort(books,(Long[] a,Long[] b)->Long.compare(a[0],b[0]));
//pn(Arrays.deepToString(books));
long sum = 0;
PriorityQueue<Long> aliceOnly = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Long> bobOnly = new PriorityQueue<>(Collections.reverseOrder());
int ac = 0,bc = 0;
for(int i=0;i<n;++i){
//pn(aliceOnly);
//pn(bobOnly);
// pn("");
if(ac< k || bc < k){
if(books[i][1]==1 && books[i][2]==1){
if(ac>=k){
if(aliceOnly.size()>0){
sum-=aliceOnly.poll();
}
else{
ac++;
}
bc++;
}
else{
if(bc>=k){
if(bobOnly.size()>0){
sum-=bobOnly.poll();
}
else{
bc++;
}
ac++;
}
else{
ac++;
bc++;
}
}
sum+=books[i][0];
}
else{
if(books[i][1]==1 && ac<k){
ac++;
aliceOnly.add(books[i][0]);
sum+=books[i][0];
}
if(books[i][2]==1 && bc<k){
bc++;
bobOnly.add(books[i][0]);
sum+=books[i][0];
}
}
}
else{
if(books[i][1]==1 && books[i][2]==1){
if(aliceOnly.size()>0 && bobOnly.size()>0 && aliceOnly.peek() + bobOnly.peek() >books[i][0]){
sum-= aliceOnly.poll() + bobOnly.poll();
sum+=books[i][0];
}
}
}
//pn("sum is "+sum);
}
pn(ac>=k && bc>=k ? sum:-1);
// PriorityQueue<Long> aliceBooks = new PriorityQueue<>(Collections.reverseOrder());
// PriorityQueue<Long> bobBooks = new PriorityQueue<>(Collections.reverseOrder());
// for(int i=0;i<n;++i){
// if(books[i][1]==1 && books[i][2]==1 ){
// if(aliceBooks.size()>=k && bobBooks.size()>=k){
// if(aliceBooks.peek() + bobBooks.peek() > books[i][0]){
// sum-=aliceBooks.poll();
// sum-=bobBooks.poll();
// //aliceBooks.add(books[i][0]);
// //bobBooks.add(books[i][0]);
// sum+=books[i][0];
// }
// }
// else{
// aliceBooks.add(books[i][0]);
// bobBooks.add(books[i][0]);
// sum+=books[i][0];
// }
// }
// else{
// if(books[i][1]==1 && aliceBooks.size()<k){
// aliceBooks.add(books[i][0]);
// sum++
// }
// }
// }
}
long pow(long a, long b) {
long result = 1;
while (b > 0) {
if (b % 2 == 1) result = (result * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return result;
}
void print(Object o) {
System.out.println(o);
System.out.flush();
}
long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % 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();
}
//output methods
private void pn(Object o) {
out.println(o);
}
private void p(Object o) {
out.print(o);
}
private ArrayList < ArrayList < Integer >> ng(int n, int e) {
ArrayList < ArrayList < Integer >> g = new ArrayList < > ();
for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ());
for (int i = 0; i < e; ++i) {
int u = ni(), v = ni();
g.get(u).add(v);
g.get(v).add(u);
}
return g;
}
private ArrayList < ArrayList < pair >> nwg(int n, int e) {
ArrayList < ArrayList < pair >> g = new ArrayList < > ();
for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ());
for (int i = 0; i < e; ++i) {
int u = ni(), v = ni(), w = ni();
g.get(u).add(new pair(w, v));
g.get(v).add(new pair(w, u));
}
return g;
}
//input methods
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
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 && !(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 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() {
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 void tr(Object...o) {
if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
}
void watch(Object...a) throws Exception {
int i = 1;
print("watch starts :");
for (Object o: a) {
//print(o);
boolean notfound = true;
if (o.getClass().isArray()) {
String type = o.getClass().getName().toString();
//print("type is "+type);
switch (type) {
case "[I":
{
int[] test = (int[]) o;
print(i + " " + Arrays.toString(test));
break;
}
case "[[I":
{
int[][] obj = (int[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[J":
{
long[] obj = (long[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[J":
{
long[][] obj = (long[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[D":
{
double[] obj = (double[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[D":
{
double[][] obj = (double[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[Ljava.lang.String":
{
String[] obj = (String[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[Ljava.lang.String":
{
String[][] obj = (String[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
case "[C":
{
char[] obj = (char[]) o;
print(i + " " + Arrays.toString(obj));
break;
}
case "[[C":
{
char[][] obj = (char[][]) o;
print(i + " " + Arrays.deepToString(obj));
break;
}
default:
{
print(i + " type not identified");
break;
}
}
notfound = false;
}
if (o.getClass() == ArrayList.class) {
print(i + " al: " + o);
notfound = false;
}
if (o.getClass() == HashSet.class) {
print(i + " hs: " + o);
notfound = false;
}
if (o.getClass() == TreeSet.class) {
print(i + " ts: " + o);
notfound = false;
}
if (o.getClass() == TreeMap.class) {
print(i + " tm: " + o);
notfound = false;
}
if (o.getClass() == HashMap.class) {
print(i + " hm: " + o);
notfound = false;
}
if (o.getClass() == LinkedList.class) {
print(i + " ll: " + o);
notfound = false;
}
if (o.getClass() == PriorityQueue.class) {
print(i + " pq : " + o);
notfound = false;
}
if (o.getClass() == pair.class) {
print(i + " pq : " + o);
notfound = false;
}
if (notfound) {
print(i + " unknown: " + o);
}
i++;
}
print("watch ends ");
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) {
int n = fsca.nextInt() ;
int k = fsca.nextInt() ;
ArrayList<Integer> ones = new ArrayList<>() ;
ArrayList<Integer> zero_one = new ArrayList<>() ;
ArrayList<Integer> one_zero = new ArrayList<>() ;
for (int i = 0; i <n ; i++) {
int val = fsca.nextInt() ;
int a = fsca.nextInt() ;
int b = fsca.nextInt() ;
if (a==1 && b == 1)
ones.add(val) ;
else if (a==1 && b ==0 )
one_zero.add(val) ;
else if (a == 0 && b==1)
zero_one.add(val) ;
}
Collections.sort(ones);
Collections.sort(zero_one);
Collections.sort(one_zero);
int min_size = Math.min(one_zero.size() , zero_one.size()) ;
min_size = Math.min(min_size , k) ;
long ans = 0 ;
for (int i = 0; i <min_size ; i++) {
ans += one_zero.get(i) + zero_one.get(i) ;
}
if (min_size < k){
int rem = k - min_size ;
int ptr = 0 ;
while (rem-- > 0 && ptr < ones.size()){
ans += ones.get(ptr) ;
ptr++ ;
}
if (rem != -1){
System.out.println(-1);
return;
}
for (int i= min_size-1 ; i>=0 && ptr < ones.size() ; i--){
int temp = one_zero.get(i) + zero_one.get(i) ;
if (temp > ones.get(ptr)){
ans -= temp ;
ans += ones.get(ptr) ;
ptr++ ;
}
}
System.out.println(ans);
}
else {
int ptr = 0 ;
for(int i = k-1 ; i >=0 && ptr < ones.size() ; i-- ){
int temp = one_zero.get(i) + zero_one.get(i);
if (temp > ones.get(ptr)){
ans -= temp ;
ans += ones.get(ptr) ;
ptr++ ;
}
}
System.out.println(ans);
}
fop.flush();
fop.close();
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
static PrintWriter fop = new PrintWriter(System.out);
static FastScanner fsca = new FastScanner();
static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
;
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a % b);
}
static final Random random = new Random();
static void ruffleSort(int[] a) {
int n = a.length;//shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n = a.length;//shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n);
long temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[][] readMat(int n, int m) {
int a[][] = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = nextInt();
return a;
}
char[][] readCharMat(int n, int m) {
char a[][] = new char[n][m];
for (int i = 0; i < n; i++) {
String s = next();
for (int j = 0; j < m; j++)
a[i][j] = s.charAt(j);
}
return a;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static void print(int a[], int n) {
for (int i = 0; i < n; i++)
fop.append((a[i]) + " ");
// fop.append("\n") ;
}
static void print(long a[], int n) {
for (int i = 0; i < n; i++)
fop.append((a[i]) + " ");
// fop.append("\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() {
int n, k;
cin >> n >> k;
priority_queue<int, std::vector<int>, std::greater<int> > both;
priority_queue<int, std::vector<int>, std::greater<int> > alice;
priority_queue<int, std::vector<int>, std::greater<int> > bob;
for (int i = 0; i < n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (a && b) {
both.push(t);
}
if (a && !b) {
alice.push(t);
}
if (!a && b) {
bob.push(t);
}
}
int res = 0;
while (k > 0 && ((!alice.empty() && !bob.empty()) || !both.empty())) {
k--;
if (both.empty()) {
res += bob.top() + alice.top();
bob.pop();
alice.pop();
} else if (alice.empty() || bob.empty()) {
res += both.top();
both.pop();
} else if ((alice.top() + bob.top() < both.top())) {
res += bob.top() + alice.top();
bob.pop();
alice.pop();
} else {
res += both.top();
both.pop();
}
}
if (k > 0) {
cout << -1 << endl;
} else {
cout << res << 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;
using ll = long long;
const ll inf = 1e18;
const int N = 2 * 1e5 + 10;
ll res, n, k;
void solve() {
ll tt, x, y;
std::vector<ll> v, vv, vvv;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> tt >> x >> y;
if (y == 1 && x == 1)
v.push_back(tt);
else if (y)
vv.push_back(tt);
else if (x)
vvv.push_back(tt);
}
sort(vv.begin(), vv.end());
sort(vvv.begin(), vvv.end());
for (int i = 0; i < min((int)vv.size(), (int)vvv.size()); i++) {
v.push_back(vv[i] + vvv[i]);
}
sort(v.begin(), v.end());
if (v.size() < k) {
cout << "-1\n";
return;
}
ll ans = 0;
for (int i = 0; i < k; i++) ans += v[i];
cout << ans << "\n";
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
ll t = 1;
while (t--) {
solve();
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
books = [tuple(map(int, input().split())) for _ in range(n)]
books.sort(key=lambda x: (x[0], -x[1], -x[2]))
ans = 0
comm = []
cnt_a = []
cnt_b = []
for t, a, b in books:
if a == 0 and b == 0:
pass
elif a == 1 and b == 1:
if len(cnt_a) + len(comm) < k or len(cnt_b) + len(comm) < k:
comm.append(t)
else:
if cnt_a and cnt_b and cnt_a[-1] + cnt_b[-1] > t:
cnt_a.pop()
cnt_b.pop()
comm.append(t)
elif a == 1 and len(cnt_a) + len(comm) < k:
cnt_a.append(t)
elif b == 1 and len(cnt_b) + len(comm) < k:
cnt_b.append(t)
else:
pass
while cnt_a and len(cnt_a) + len(comm) > k:
cnt_a.pop()
while cnt_b and len(cnt_b) + len(comm) > k:
cnt_b.pop()
if len(cnt_a) + len(comm) < k or len(cnt_b) + len(comm) < k:
print(-1)
else:
print(sum(cnt_a) + sum(cnt_b) + sum(comm))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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 javafx.util.*;
import java.math.*;
//import java.lang.*;
public class Main
{
// static int n;
// static HashSet<Integer> adj[];
// static int dist[];
// static int remove[];
// static boolean isLeaf[];
// static Long dp[][];
// static int arr[];
// static boolean ok;
// static long arr[];
// static long sum[];
// static boolean ok;
static long mod=1000000007;
// static HashSet<Integer> adjacency[];
static final long oo=(long)1e18;
public static void main(String[] args) throws IOException {
// Scanner sc=new Scanner(System.in);
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
br = new BufferedReader(new InputStreamReader(System.in));
// int test=nextInt();
int test=1;
outer: while(test--!=0){
// String s=next();
int n=nextInt();
int val=nextInt();
int a1[]=new int[n];
int a2[]=new int[n];
int a3[]=new int[n];
int n1=0;int n2=0;int n3=0;
for(int i=0;i<n;i++){
int a=nextInt();
int t1=nextInt();
int t2=nextInt();
if(t1==1&&t2==1){
a1[n1++]=a;
}
else if(t1==1){
a2[n2++]=a;
}
else if(t2==1){
a3[n3++]=a;
}
}
Arrays.sort(a1,0,n1);
Arrays.sort(a2,0,n2);
Arrays.sort(a3,0,n3);
boolean ok=false;int i=0;int j=0;int k=0;long ans=0;
while(val--!=0){
int a=i<n1?a1[i]:100000;
int b=j<n2?a2[j]:100000;
int c=k<n3?a3[k]:100000;
c=c+b;
if(a>=100000&&c>=100000){
ok=true;
break;
}
else if(a<=c){
ans+=a;
i++;
}
else{
ans+=c;
j++;k++;
}
}
if(ok)
pw.println("-1");
else
pw.println(ans);
}
pw.close();
}
static class Pair implements Comparable<Pair>{
double g,ai,bi,sa,sb;
Pair(double g,double ai,double bi){
// this.index=index;
this.g=g;
this.ai=ai;
this.bi=bi;
sa=g/ai;
sb=g/bi;
//this.z=z;
}
public int compareTo(Pair p){
double a=Math.abs(sa-sb);
double b=Math.abs(p.sa-p.sb);
if(a>b){
return 1;
}
else if(a==b){
return 0;
}
else{
return -1;
}
}
}
static void update(long tree[],int idx,long val){
//println(idx);
while(idx<1000006){
tree[idx]+=val;
idx+=(idx)&(-idx);
}
}
static long sum(long tree[],int idx){
long sum=0;
while(idx>0){
sum+=tree[idx];
idx-=(idx)&(-idx);
}
return sum;
}
public static BufferedReader br;
public static StringTokenizer st;
public static String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public static Integer nextInt() {
return Integer.parseInt(next());
}
public static Long nextLong() {
return Long.parseLong(next());
}
public static Double nextDouble() {
return Double.parseDouble(next());
}
// static class Pair{
// int x;int y;
// Pair(int x,int y,int z){
// this.x=x;
// this.y=y;
// // this.z=z;
// // this.z=z;
// // this.i=i;
// }
// }
// static class sorting implements Comparator<Pair>{
// public int compare(Pair a,Pair b){
// //return (a.y)-(b.y);
// if(a.y==b.y){
// return -1*(a.z-b.z);
// }
// return (a.y-b.y);
// }
// }
static long ncr(long n,long r){
if(r==0)
return 1l;
long val=ncr(n-1,r-1);
val=(n*val);
val=(val/r);
return val;
}
public static int[] na(int n)throws IOException{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = nextInt();
return a;
}
static class query implements Comparable<query>{
int l,r,idx,block;
static int len;
query(int l,int r,int i){
this.l=l;
this.r=r;
this.idx=i;
this.block=l/len;
}
public int compareTo(query a){
return block==a.block?r-a.r:block-a.block;
}
}
// static class sorting implements Comparator<Pair>{
// public int compare(Pair a1,Pair a2){
// if(o1.a==o2.a)
// return (o1.b>o2.b)?1:-1;
// else if(o1.a>o2.a)
// return 1;
// else
// return -1;
// }
// }
static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 ||
n % 3 == 0)
return false;
for (int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// To compute x^y under modulo m
static long power(long x, long y, long m){
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
static long fast_pow(long base,long n,long M){
if(n==0)
return 1;
if(n==1)
return base;
long halfn=fast_pow(base,n/2,M);
if(n%2==0)
return ( halfn * halfn ) % M;
else
return ( ( ( halfn * halfn ) % M ) * base ) % M;
}
static long modInverse(long n,long M){
return fast_pow(n,M-2,M);
}
// (1,1)
// (3,2) (3,5)
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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 inf, log2
class SegmentTree:
def __init__(self, array, func=max):
self.n = len(array)
self.size = 2**(int(log2(self.n-1))+1) if self.n != 1 else 1
self.func = func
self.default = 0 if self.func != min else inf
self.data = [self.default] * (2 * self.size)
self.process(array)
def process(self, array):
self.data[self.size : self.size+self.n] = array
for i in range(self.size-1, -1, -1):
self.data[i] = self.func(self.data[2*i], self.data[2*i+1])
def query(self, alpha, omega):
"""Returns the result of function over the range (inclusive)!"""
if alpha == omega:
return self.data[alpha + self.size]
res = self.default
alpha += self.size
omega += self.size + 1
while alpha < omega:
if alpha & 1:
res = self.func(res, self.data[alpha])
alpha += 1
if omega & 1:
omega -= 1
res = self.func(res, self.data[omega])
alpha >>= 1
omega >>= 1
return res
def update(self, index, value):
"""Updates the element at index to given value!"""
index += self.size
self.data[index] = value
index >>= 1
while index:
self.data[index] = self.func(self.data[2*index], self.data[2*index+1])
index >>= 1
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
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")
# ------------------- fast io --------------------
from math import gcd, ceil
def pre(s):
n = len(s)
pi = [0] * n
for i in range(1, n):
j = pi[i - 1]
while j and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
return pi
def prod(a):
ans = 1
for each in a:
ans = (ans * each)
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
n, k = map(int, input().split())
#c, d = map(int, input().split())
both = []
anb = []
bna = []
for i in range(n):
t, a, b = map(int, input().split())
if a and b:
both += [t]
if a and not b:
anb += [t]
if b and not a:
bna += [t]
both = sorted(both)
anb = sorted(anb)
bna = sorted(bna)
if not both:
if min(len(anb), len(bna)) < k:
print(-1)
else:
print(sum(anb[:k]) + sum(bna[:k]))
continue
if not anb or not bna:
if len(both) < k:
print(-1)
else:
print(sum(both[:k]))
continue
l1, l2, l3 = len(both), len(anb), len(bna)
both = SegmentTree(both, func=lambda a,b:a+b)
anb = SegmentTree(anb, func=lambda a,b:a+b)
bna = SegmentTree(bna, func=lambda a,b:a+b)
anss = inf
for i in range(min(l1+1, k+1)):
ans = both.query(0, i-1) if i else 0
if min(l2, l3) >= k-i:
if i != k:
ans += anb.query(0, k-i-1) + bna.query(0, k-i-1)
anss = min(anss, ans)
print(anss if anss != 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
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.HashMap;
import java.util.*;
public class ROUND653 {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader sc = new FastReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
int brr[] = new int[n];
int time[] = new int[n];
int u = 0,y=0;
ArrayList <Integer> al=new ArrayList<Integer>();
ArrayList <Integer> bl=new ArrayList<Integer>();
ArrayList <Integer> cl=new ArrayList<Integer>();
for(int i =0;i<n;i++) {
time[i] = sc.nextInt();
arr[i] = sc.nextInt();
brr[i] = sc.nextInt();
if(arr[i]==1)
u++;
if(brr[i]==1)
y++;
if(arr[i]==1&&brr[i]==1)
cl.add(time[i]);
if(arr[i]==1&&brr[i]==0)
al.add(time[i]);
if(arr[i]==0&&brr[i]==1)
bl.add(time[i]);
}
if(u>=k&&y>=k) {
Collections.sort(al);
Collections.sort(bl);
for(int i =0;i<Math.min(al.size(), bl.size());i++)
cl.add(al.get(i)+bl.get(i));
Collections.sort(cl);
int ans = 0;
for(int i =0;i<k;i++)
ans+=cl.get(i);
w.println(ans);
}
else
w.println("-1");
w.close();
}
public static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(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() {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
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 char nextChar() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char)c;
}
public String next() {
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 interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\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 {
static class Reader {
BufferedReader br; StringTokenizer st;
public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine());
} catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
}
static int n, k;
static long[] t;
static int[] a;
static int[] b;
static long[] dp;
static long mint = 1000000000;
static long recur(int i, int al, int bl, long curt) {
if (al >= k && bl >= k) {
return curt;
//return curt;
} else if (i < 0) {
return 1000000000;
}
else if (dp[i] != 0) {
return dp[i];
}
long m1 = 1000000000;
m1 = recur(i - 1, al + a[i], bl + b[i], curt + t[i]);
m1 = Long.min(m1, recur(i - 1, al, bl, curt));
dp[i] = Long.min(dp[i], curt);
return m1;
}
public static void main(String[] args) {
Reader r = new Reader();
n = r.nextInt();
k = r.nextInt();
int[] t = new int[n];
a = new int[n];
b = new int[n];
ArrayList<Integer> aa = new ArrayList<Integer>();
ArrayList<Integer> bb = new ArrayList<Integer>();
ArrayList<Integer> cc = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
t[i] = r.nextInt();
a[i] = r.nextInt();
b[i] = r.nextInt();
if (a[i] > 0 && b[i] > 0) {
cc.add(t[i]);
} else if (a[i] > 0) {
aa.add(t[i]);
} else if (b[i] > 0) {
bb.add(t[i]);
}
}
aa.sort(null);
bb.sort(null);
cc.sort(null);
int ak = k;
int bk = k;
int c1 = 0;
int c2 = 0;
int c3 = 0;
long o = 0;
while (ak > 0 || bk > 0) {
int t1 = 100000;
int t2 = 100000;
int t3 = 100000;
if (c1 < aa.size()) t1 = aa.get(c1);
if (c2 < bb.size()) t2 = bb.get(c2);
if (c3 < cc.size()) t3 = cc.get(c3);
if (ak > 0 && bk > 0) {
if (t3 != 100000 && t3 < t1 + t2) {
o += t3;
c3++;
ak--;
bk--;
} else if (t1 != 100000 && t2 != 100000) {
o += t1 + t2;
c1++;
c2++;
ak--;
bk--;
} else {
break;
}
} else if (ak > 0) {
if (t3 < t1) {
o += t3;
c3++;
ak--;
bk--;
} else if (t1 != 100000) {
o += t1;
c1++;
ak--;
} else {
break;
}
} else {
if (t3 < t2) {
o += t3;
c3++;
ak--;
bk--;
} else if (t2 != 100000) {
o += t2;
c2++;
ak--;
} else {
break;
}
}
}
if (ak < 1 && bk < 1) {
System.out.println(o);
} else {
System.out.println(-1);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
# # import sys
# # sys.stdin = open('CF_653_D3/input.txt', 'r')
# sys.stdout = open('CF_653_D3/output.txt', 'w')
n,K = list(map(int,input().split()))
l = [ list(map(int,input().split())) for _ in range(n) ]
al,bo,bt = [],[],[]
for i in l :
if ( i[1]==1 and i[2]==1 ) :
bt.append(i[0])
elif i[1]==1 and i[2]==0 :
al.append(i[0])
elif i[1]==0 and i[2]==1 :
bo.append(i[0])
al.sort()
bt.sort()
bo.sort()
i,j,k = 0,0,0
ans,cnt = 0,0
# print(al,bt,bo)
while i<len(al) and j<len(bo) and k<len(bt) and cnt<K :
if ( al[i]+bo[j] < bt[k] ) :
ans += al[i]+bo[j]
i += 1
j += 1
else :
ans += bt[k]
k += 1
cnt += 1
while k<len(bt) and cnt<K :
ans += bt[k]
k += 1
cnt += 1
while cnt<K and i<len(al) and j<len(bo) :
ans += al[i]+bo[j]
i += 1
j += 1
cnt += 1
if ( cnt < 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 n, k;
int suma, sumb;
priority_queue<int> both, alice, bob;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (a + b == 2) {
both.push(-t);
} else if (a == 1) {
alice.push(-t);
} else if (b == 1) {
bob.push(-t);
}
suma += a;
sumb += b;
}
if (suma < k || sumb < k) {
cout << "-1\n";
return 0;
}
long long ans = 0;
int a = 0, b = 0;
while (a < k || b < k) {
long long C1 = 1e12, C2 = 1e12, C3 = 1e12;
if (both.size()) {
C1 = -both.top();
}
if (alice.size()) {
C2 = -alice.top();
}
if (bob.size()) {
C3 = -bob.top();
}
if ((C1 <= min(C2, C3) || C1 <= C2 + C3) && C1 != 1e12) {
a++, b++;
ans += C1;
both.pop();
} else {
if (a < k && C2 != 1e12) {
a++;
ans += C2;
alice.pop();
}
if (b < k && C3 != 1e12) {
b++;
ans += C3;
bob.pop();
}
}
}
cout << ans << "\n";
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
R=lambda:map(int,input().split())
s=sorted
n,k=R();_,u,v,w=l=[[],[],[],[]]
for _ in[0]*n:t,a,b=R();l[2*a+b]+=t,
a=*map(sum,zip(s(u),s(v))),*w;print((sum(s(a)[:k]),-1)[len(a)<k])
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
public final class Solution {
static double PI = 3.1415926535;
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
PrintWriter writer = new PrintWriter(System.out);
Reader re=new Reader();
int n=re.nextInt(), k=re.nextInt();
ArrayList<Integer> a=new ArrayList<Integer>();
ArrayList<Integer> b=new ArrayList<Integer>();
ArrayList<Integer> c=new ArrayList<Integer>();
for(int i=0;i<n;i++) {
int x=re.nextInt();
int y=re.nextInt();
int z=re.nextInt();
if(y==1 && z==1) c.add(x);
else if(y==1) a.add(x);
else if(z==1) b.add(x);
}
long ans=0;
Collections.sort(a);
Collections.sort(b);
Collections.sort(c);
int l1=0, l2=0, s1=a.size(), s2=b.size(), s3=c.size(), x=0, y=0, z=0;
if((s1+s3)<k || (s2+s3)<k) {
ans=-1;
}else {
while(l1<k || l2<k) {
if(x<s1 && y<s2 && z<s3 && a.get(x)+b.get(y)<=c.get(z)) {
ans+=a.get(x++);
ans+=b.get(y++);
l1++; l2++;
}else if(z<s3) {
ans+=c.get(z++);
l1++; l2++;
}else if(x<s1 && y<s2) {
ans+=a.get(x++);
ans+=b.get(y++);
l1++; l2++;
}else {
ans=-1;
break;
}
}
}
System.out.println(ans);
writer.flush();
writer.close();
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
struct node {
int t, a, b;
} ke[maxn], le[maxn], m[maxn];
int n, k;
bool cmp(node x, node y) { return x.t < y.t; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
int cnt1 = 0, cnt2 = 0, cnt0 = 0;
for (int i = 0; i < n; i++) {
node x;
cin >> x.t >> x.a >> x.b;
if (x.a && x.b) {
m[cnt0] = x;
cnt0++;
} else if (x.a) {
ke[cnt1] = x;
cnt1++;
} else if (x.b) {
le[cnt2] = x;
cnt2++;
}
}
if (cnt1 + cnt0 < k || cnt2 + cnt0 < k) {
cout << -1 << '\n';
return 0;
}
sort(ke, ke + cnt1, cmp);
sort(le, le + cnt2, cmp);
sort(m, m + cnt0, cmp);
int o1 = 0, o0 = 0;
long long ans = 0;
while (o1 + o0 < k) {
if (o0 >= cnt0) {
ans += ke[o1].t + le[o1].t;
o1++;
} else if (o1 < min(cnt1, cnt2) && m[o0].t > ke[o1].t + le[o1].t) {
ans += ke[o1].t + le[o1].t;
o1++;
} else {
ans += m[o0].t;
o0++;
}
}
cout << ans << '\n';
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int Alice[200005], Bob[200005], Both[200005];
int PA[200005], PB[200005];
int FindMinReadingTime(int k, int a, int b, int bo) {
int i, Ans = 0;
if (a > 0) sort(Alice + 1, Alice + a + 1);
if (b > 0) sort(Bob + 1, Bob + b + 1);
if (bo > 0) sort(Both + 1, Both + bo + 1);
for (i = 1; i <= a; ++i) PA[i] = PA[i - 1] + Alice[i];
for (i = 1; i <= a; ++i) PB[i] = PB[i - 1] + Bob[i];
if (a >= k && b >= k)
Ans = PA[k] + PB[k];
else
Ans = 2000000001;
int Value = 0;
for (i = 1; i <= min(k, bo); ++i) {
Value += Both[i];
if (i + a >= k && i + b >= k) Ans = min(Ans, Value + PA[k - i] + PB[k - i]);
}
return Ans;
}
int main() {
int N, k, i, j, p, q;
int t, a, b;
scanf("%d%d", &N, &k);
j = p = q = 0;
for (i = 1; i <= N; ++i) {
scanf("%d%d%d", &t, &a, &b);
if (a == 1) {
if (b == 1)
Both[++q] = t;
else
Alice[++j] = t;
} else if (b == 1)
Bob[++p] = t;
}
if (j + q < k || p + q < k)
puts("-1");
else
printf("%d\n", FindMinReadingTime(k, j, p, q));
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n, k, t, a, b, boths, alices, bobs, cnt[2], ans, res, tmp;
vector<int> both, alice, bob;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> t >> a >> b;
if (a && b)
both.push_back(t);
else if (a)
alice.push_back(t);
else if (b)
bob.push_back(t);
}
boths = both.size();
alices = alice.size();
bobs = bob.size();
sort(both.begin(), both.end());
sort(alice.begin(), alice.end());
sort(bob.begin(), bob.end());
while (k) {
int s1 = 1 << 30, s2 = 1 << 30;
if (boths <= cnt[0]) break;
if (boths > cnt[0]) s1 = both[cnt[0]];
if (cnt[1] < min(alices, bobs)) s2 = alice[cnt[1]] + bob[cnt[1]];
if (s1 < s2)
ans += s1, cnt[0]++, k--;
else
ans += s2, cnt[1]++, k--;
}
while (k--) {
if (cnt[1] < min(alices, bobs))
ans += alice[cnt[1]] + bob[cnt[1]], cnt[1]++;
else if (cnt[0] < boths)
ans += both[cnt[0]], cnt[0]++;
else
return cout << "-1\n", 0;
}
cout << ans << '\n';
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.