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>
const long long maxn = 2e5 + 1;
using namespace std;
struct triple {
long long t, ci;
bool a = 0, b = 0;
triple() {}
};
bool cmp(triple x, triple y) { return x.t < y.t; }
triple v[maxn];
bool usd[maxn];
long long ci = -1;
long long cs = 0;
inline void add() {
++ci;
for (; usd[ci]; ci++)
;
cs += v[ci].t;
}
inline void del() {
if (ci == -1) exit(228);
cs -= v[ci].t;
--ci;
for (; ci >= 0 && usd[ci]; --ci)
;
}
inline void dl(long long x) {
usd[x] = 0;
if (x < ci) {
cs += v[x].t - v[ci].t;
--ci;
for (; ci >= 0 && usd[ci]; ci--)
;
}
}
inline void ad(long long x) {
usd[x] = 1;
if (x <= ci) {
cs -= v[x].t;
++ci;
for (; usd[ci]; ci++)
;
cs += v[ci].t;
}
}
long long n, m, k;
long long solve() {
cin >> n >> m >> k;
for (long long i = 0; i < n; i++) {
cin >> v[i].t >> v[i].a >> v[i].b;
v[i].ci = i + 1;
}
sort(v, v + n, cmp);
vector<long long> oo, oz, zo;
for (long long i = 0; i < n; i++) {
if (v[i].a && v[i].b)
oo.push_back(i);
else if (v[i].a)
oz.push_back(i);
else if (v[i].b)
zo.push_back(i);
}
long long j1 = 0, j2 = 0, mi = -1;
long long ans = 1e18, can = 0;
for (long long i = 0; i < m; i++) add();
for (long long i = 0; i < min(k, (long long)oo.size()); i++) {
can += v[oo[i]].t;
del();
ad(oo[i]);
}
for (long long i = min(k, (long long)oo.size()); i >= 0; i--) {
if (i + min(zo.size(), oz.size()) < k) break;
if (2 * k - i > m) break;
if (i < min(k, (long long)oo.size())) {
dl(oo[i]);
add();
can -= v[oo[i]].t;
}
for (; i + j1 < k; j1++) {
can += v[oz[j1]].t;
del();
ad(oz[j1]);
}
for (; i + j2 < k; j2++) {
can += v[zo[j2]].t;
del();
ad(zo[j2]);
}
if (ans > can + cs) {
ans = can + cs;
mi = i;
}
}
if (mi == -1) {
cout << -1;
return 0;
}
cout << ans << '\n';
for (long long i = 0; i < n; i++) usd[i] = 0;
for (long long j = 0; j < mi; j++) {
cout << v[oo[j]].ci << ' ';
usd[oo[j]] = 1;
}
for (long long j = 0; j < k - mi; j++) {
cout << v[oz[j]].ci << ' ';
usd[oz[j]] = 1;
}
for (long long j = 0; j < k - mi; j++) {
cout << v[zo[j]].ci << ' ';
usd[zo[j]] = 1;
}
long long cnt = 0;
for (long long i = 0; cnt < m - (2 * k - mi); i++) {
if (usd[i]) continue;
cout << v[i].ci << ' ';
++cnt;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
template <typename T>
void read(T &x) {
int f = 1;
x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
x *= f;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void println(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
putchar('\n');
}
template <typename T, typename... Args>
void read(T &x, Args &...args) {
read(x);
read(args...);
}
inline void reads(char *a) { scanf("%s", a); }
template <typename... Args>
void reads(char *a, Args *...args) {
scanf("%s", a);
reads(args...);
}
template <typename T, typename... Args>
void print(T x, Args... args) {
print(x);
putchar(' ');
print(args...);
}
template <typename T, typename... Args>
void println(T x, Args... args) {
print(x);
putchar(' ');
println(args...);
}
inline void prints(char *a) { printf("%s", a); }
template <typename T>
inline T sqr(T x) {
return x * x;
}
template <typename T>
inline T random(T R) {
return (double)rand() / RAND_MAX * R + ((T)0.5 == 0 ? 0.5 : 0.0);
}
template <typename T>
inline T random(T L, T R) {
return random(R - L) + L;
}
long long ksm(long long a, long long k, long long Mod) {
long long res = 1;
while (k) {
if (k & 1) (res *= a) %= Mod;
(a *= a) %= Mod;
k >>= 1;
}
return res % Mod;
}
inline long long inv(long long a, long long Mod) {
return ksm(a, Mod - 2, Mod);
}
inline void cap_bit(long long x) {
for (int i = 63; i >= 0; --i)
if (x >> i & 1) {
printf("Need (%d) = %lld\n", i + 1, 1LL << (i + 1));
return;
}
}
inline void cal_space(long long x) { printf("%lld MB\n", x >> 20); }
const int dx4[] = {0, 0, -1, 1};
const int dy4[] = {-1, 1, 0, 0};
const int dx6[] = {1, -1, 0, 0, 0, 0};
const int dy6[] = {0, 0, 1, -1, 0, 0};
const int dz6[] = {0, 0, 0, 0, 1, -1};
const int N = 2e5 + 5;
const int M = 26;
const long long Mod = 1e9 + 7;
const long long INF = 1e16;
pair<long long, long long> a[4][N];
long long ia[4];
long long n, m, k;
set<pair<long long, long long> > sf, ss;
long long ans, cur;
void adjust(int x) {
long long cnt = x + 2 * (k - x);
while (ss.size() && sf.size() && ss.rbegin()->first > sf.begin()->first) {
cur -= ss.rbegin()->first;
cur += sf.begin()->first;
sf.insert(*ss.rbegin());
ss.erase(*ss.rbegin());
ss.insert(*sf.begin());
sf.erase(*sf.begin());
}
while (ss.size() && cnt + ss.size() > m) {
cur -= ss.rbegin()->first;
sf.insert(*ss.rbegin());
ss.erase(*ss.rbegin());
}
while (sf.size() && cnt + ss.size() < m) {
cur += sf.begin()->first;
ss.insert(*sf.begin());
sf.erase(*sf.begin());
}
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < 4; ++i) ia[i] = 0;
for (int i = 1; i <= n; ++i) {
long long t, ai, bi;
cin >> t >> ai >> bi;
int s = 0;
if (ai) s += 1;
if (bi) s += 2;
a[s][++ia[s]] = {t, i};
}
if (ia[1] + ia[3] < k || ia[2] + ia[3] < k) {
puts("-1");
return 0;
}
for (int i = 0; i < 4; ++i) sort(a[i] + 1, a[i] + ia[i] + 1);
long long st = 0;
cur = -1;
ans = -1;
long long ians;
while ((k - st) > ia[1] || (k - st) > ia[2]) st++;
for (int i = st; i <= ia[3] && i <= k; ++i) {
long long cnt = i + (k - i) * 2;
if (cur == -1) {
cur = 0;
for (int j = 1; j <= ia[3]; ++j)
if (j <= st)
cur += a[3][j].first;
else
sf.insert(a[3][j]);
for (int j = 1; j <= ia[2]; ++j)
if (j <= (k - st))
cur += a[2][j].first;
else
sf.insert(a[2][j]);
for (int j = 1; j <= ia[1]; ++j)
if (j <= (k - st))
cur += a[1][j].first;
else
sf.insert(a[1][j]);
for (int j = 1; j <= ia[0]; ++j) sf.insert(a[0][j]);
} else {
long long r = k - i;
if (sf.find(a[3][i]) != sf.end())
cur += a[3][i].first, sf.erase(a[3][i]);
else
ss.erase(a[3][i]);
cur -= a[1][r + 1].first;
cur -= a[2][r + 1].first;
sf.insert(a[1][r + 1]);
sf.insert(a[2][r + 1]);
}
adjust(i);
if (cnt + ss.size() == m) {
if (ans == -1 || cur < ans) {
ans = cur;
ians = i;
}
}
}
cout << ans << endl;
if (ans == -1) return 0;
vector<pair<long long, long long> > res;
for (int i = 1; i <= ians; ++i) cout << a[3][i].second << " ";
for (int i = ians + 1; i <= ia[3]; ++i) res.push_back(a[3][i]);
for (int i = 1; i <= k - ians; ++i) cout << a[1][i].second << " ";
for (int i = k - ians + 1; i <= ia[1]; ++i) res.push_back(a[1][i]);
for (int i = 1; i <= k - ians; ++i) cout << a[2][i].second << " ";
for (int i = k - ians + 1; i <= ia[2]; ++i) res.push_back(a[2][i]);
for (int i = 1; i <= ia[0]; ++i) res.push_back(a[0][i]);
sort((res).begin(), (res).end());
for (int i = 1; i <= m - ians - 2 * (k - ians); ++i)
cout << res[i - 1].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
|
python3
|
from sys import stdin
input = lambda: stdin.readline().rstrip("\r\n")
from collections import defaultdict as vector
from collections import deque as que
inin = lambda: int(input())
inar = lambda: list(map(int,input().split()))
from heapq import heappush as hpush,heappop as hpop
bob=[]
alice=[]
both=[]
n,k=inar()
for i in range(n):
t,a,b=inar()
if a==1 and b==1:
both.append(t)
elif a==1:
alice.append(t)
elif b==1:
bob.append(t)
else:
continue
both.sort(reverse=True)
alice.sort(reverse=True)
bob.sort(reverse=True)
def top(x):
if x==[]:
return float('inf')
else:
return x[-1]
t=0
donealice=0
donebob=0
while(donealice<k or donebob<k):
if both==[] and alice==[] and bob==[]:
t=-1
break
if top(both)<top(alice)+top(bob):
donealice+=1
donebob+=1
t+=both.pop()
#print('#1')
else:
if alice:
t+=alice.pop()
donealice+=1
if bob:
t+=bob.pop()
donebob+=1
#print('#2')
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
|
#!/usr/bin/env python
from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
# 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")
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)
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
input = lambda: sys.stdin.readline().rstrip("\r\n")
read = lambda: list(map(int, input().strip().split(" ")))
def solve():
n, k_ = read();both = []; a = []; b = []
for _ in range(n):
arr = read()
x, y, z = arr
if y&z:
both.append(arr)
elif y:
a.append(arr)
elif z:
b.append(arr)
both.sort(); a.sort(); b.sort()
t = 0; books = 0; i, j, k = 0, 0, 0
while books < k_ and i < len(both) and j < len(a) and k < len(b):
if both[i][0] <= a[j][0]+b[k][0]:
t += both[i][0]
books += 1
i += 1
else:
t += a[j][0]+b[k][0]
j += 1; k += 1
books += 1
while books < k_ and j < len(a) and k < len(b):
t += a[j][0]+b[k][0]
j += 1; k += 1
books += 1
while books< k_ and i < len(both):
t += both[i][0]
books += 1
i += 1
if books < k_:
print(-1)
elif books == k_:
print(t)
if __name__ == "__main__":
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
|
python2
|
"""Template for Python Competitive Programmers prepared by pa.n.ik, kabeer seth and Mayank Chaudhary """
# to use the print and division function of Python3
from __future__ import division, print_function
"""value of mod"""
MOD = 998244353
mod = 10**9 + 7
"""use resource"""
# import resource
# resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY])
"""for factorial"""
def prepare_factorial():
fact = [1]
for i in range(1, 100005):
fact.append((fact[-1] * i) % mod)
ifact = [0] * 100005
ifact[100004] = pow(fact[100004], mod - 2, mod)
for i in range(100004, 0, -1):
ifact[i - 1] = (i * ifact[i]) % mod
return fact, ifact
"""uncomment next 4 lines while doing recursion based question"""
# import threading
# threading.stack_size(1<<27)
import sys
# sys.setrecursionlimit(10000)
"""uncomment modules according to your need"""
from bisect import bisect_left, bisect_right, insort
# from itertools import repeat
from math import floor, ceil, sqrt, degrees, atan, pi, log, sin, radians
from heapq import heappop, heapify, heappush
# from random import randint as rn
# from Queue import Queue as Q
from collections import Counter, defaultdict, deque
# from copy import deepcopy
# from decimal import *
# import re
# import operator
def modinv(n, p):
return pow(n, p - 2, p)
def ncr(n, r, fact, ifact): # for using this uncomment the lines calculating fact and ifact
t = (fact[n] * (ifact[r] * ifact[n-r]) % mod) % mod
return t
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def input(): return sys.stdin.readline().strip()
"""*****************************************************************************************"""
def GCD(x, y):
while (y):
x, y = y, x % y
return x
def lcm(x, y):
return (x * y)//(GCD(x, y))
def get_xor(n):
return [n, 1, n+1, 0][n % 4]
def fast_expo(a, b):
res = 1
while b:
if b&1:
res = (res * a)
res %= MOD
b -= 1
else:
a = (a* a)
a %= MOD
b>>=1
res %= MOD
return res
def get_n(Sum): # this function returns the maximum n for which Summation(n) <= Sum
ans = (-1 + sqrt(1 + 8*Sum))//2
return ans
""" ********************************************************************************************* """
def main():
n, k = get_ints()
both = []
Alice = []
Bob = []
for i in range(n):
t, a, b = get_ints()
if a == 1 and b == 1:
both.append([t, a, b])
elif a == 1:
Alice.append([t, a, b])
elif b == 1:
Bob.append([t, a, b])
Alice.sort()
Bob.sort()
for i in range(min(len(Alice), len(Bob))):
both.append([Alice[i][0] + Bob[i][0], 1, 1])
both.sort()
if len(both) < k:
print(-1)
exit()
ans = 0
for i in range(k):
ans += both[i][0]
print(ans)
""" -------- Python 2 and 3 footer by Pajenegod and c1729 ---------"""
py2 = round(0.5)
if py2:
from future_builtins import ascii, filter, hex, map, oct, zip
range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192
class FastIO(BytesIO):
newlines = 0
def __init__(self, file):
self._file = file
self._fd = file.fileno()
self.writable = "x" in file.mode or "w" in file.mode
self.write = super(FastIO, self).write if self.writable else None
def _fill(self):
s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
return s
def read(self):
while self._fill(): pass
return super(FastIO, self).read()
def readline(self):
while self.newlines == 0:
s = self._fill();
self.newlines = s.count(b"\n") + (not s)
self.newlines -= 1
return super(FastIO, self).readline()
def flush(self):
if self.writable:
os.write(self._fd, self.getvalue())
self.truncate(0), self.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
if py2:
self.write = self.buffer.write
self.read = self.buffer.read
self.readline = self.buffer.readline
else:
self.write = lambda s: self.buffer.write(s.encode('ascii'))
self.read = lambda: self.buffer.read().decode('ascii')
self.readline = lambda: self.buffer.readline().decode('ascii')
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
""" main function"""
if __name__ == '__main__':
main()
# threading.Thread(target=main).start()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import sys
import math
import heapq
from collections import defaultdict, deque
input = sys.stdin.readline
def r():
return int(input())
def rm():
return map(int,input().split())
def rl():
return list(map(int,input().split()))
n,k=rm()
a=[];b=[];both=[]
for _ in range(n):
t,ai,bi=rm()
if ai==1 and bi==1:
both.append(t)
elif ai==1:
a.append(t)
elif bi==1:
b.append(t)
a.sort()
b.sort()
if len(a)<k-len(both) or len(b)<k-len(both):
print(-1)
else:
c=[]
cl=min(len(a),len(b))
for i in range(cl):
c.append(a[i]+b[i])
both+=c
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
|
""" 616C """
""" 1152B """
# import math
# import sys
def main():
# n ,m= map(int,input().split())
# arr = list(map(int,input().split()))
# b = list(map(int,input().split()))
# n = int(input())
# string = str(input())
n ,k= map(int,input().split())
a = []
b = []
c = []
for _ in range(n):
t,first,second = map(int,input().split())
if(first==second):
if(second==1):
c.append(t)
elif(first==1):
a.append(t)
elif second==1:
b.append(t)
if(len(a)+len(c)<k or len(b)+len(c)<k):
print(-1)
return
a.sort()
b.sort()
c.sort()
l = 0
r = 0
m = 0
ans = 0
for i in range(k):
if((l>=len(a) or r>=len(b)) or (m<len(c) and l<len(a) and r<len(c) and c[m]<(a[l]+b[r]))):
ans += c[m]
m+=1
else:
ans += (a[l]+b[r])
l+=1
r+=1
print(ans)
return
main()
# def test():
# t = int(input())
# while t:
# main()
# t-=1
# test()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"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 Main {
private static FastReader fr = new FastReader();
private static Helper helper = new Helper();
private static StringBuilder result = new StringBuilder();
public static void main(String[] args) {
Task solver = new Task();
solver.solve();
}
static class Task {
public void solve() {
int n = fr.ni(), k = fr.ni();
int countA=0, countB = 0;
long ans = 0;
PriorityQueue<Integer> a = new PriorityQueue<>();
PriorityQueue<Integer> b = new PriorityQueue<>();
PriorityQueue<Integer> ab = new PriorityQueue<>();
for(int i=0; i<n; i++){
int t = fr.ni(), ai = fr.ni(), bi = fr.ni();
if(ai == 1 && bi == 1) ab.add(t);
else if(ai == 1) a.add(t);
else if(bi == 1) b.add(t);
}
while(countA < k || countB < k){
if(!ab.isEmpty() && !a.isEmpty() && !b.isEmpty()){
if(ab.peek() <= a.peek() + b.peek()) ans += ab.poll();
else ans += a.poll() + b.poll();
countA++;
countB++;
}
else if(!ab.isEmpty()){
ans += ab.poll();
countA++;
countB++;
}
else if(!a.isEmpty() && countA < k){
ans += a.poll();
countA++;
}
else if(!b.isEmpty() && countB < k){
ans += b.poll();
countB++;
}
else break;
}
if(countA < k || countB < k) System.out.println(-1);
else System.out.println(ans);
}
}
static class Helper{
public long[] tiArr(int n, int si){
long[] arr = new long[n];
for(int i=si; i<n; i++) arr[i] = fr.nl();
return arr;
}
}
static class FastReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
private static PrintWriter pw;
public FastReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public String rl() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void print(String str) {
pw.print(str);
pw.flush();
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
n,k = li()
alice = []
bob = []
both = []
for i in range(n):
a,b,c = li()
if b and c:
both.append(a)
elif b:alice.append(a)
elif c:bob.append(a)
alice.sort()
bob.sort()
for i in range(min(len(alice),len(bob))):
both.append(alice[i] + bob[i])
both.sort()
# print(both)
if len(both) < k:
print(-1)
exit()
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
|
t,k = map(int,input().split())
arra = []
arrb = []
arrc =[]
ans =0
for i in range(t):
a,b,c =map(int,input().split())
if b==c==1 :
arra.append(a)
elif b==0 and c==1:
arrc.append(a)
elif b==1 and c==0:
arrb.append(a)
arra.sort()
arrb.sort()
arrc.sort()
l1,l2,l3 = len(arra),len(arrb),len(arrc)
#print(arra,arrb,arrc)
if min(l2,l3)<k and k-min(l2,l3)>l1:
print(-1)
else:
x = k
y = k
index =0
curr =0
while(x!=0 or y!=0):
s1 = 0
Flag =True
if curr<l2:
s1+=arrb[curr]
else:
Flag =False
if curr<l3:
s1+=arrc[curr]
else:
Flag=False
if (index<l1 and s1>arra[index]) or Flag ==False:
ans+=arra[index]
index+=1
x-=1
y-=1
else:
ans+=arrb[curr]
ans+=arrc[curr]
curr+=1
x-=1
y-=1
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
for _ in range(1):
n,k=map(int,input().split())
one=[]
two=[]
both=[]
a=[]
for ii in range(n):
x,y,z=map(int,input().split())
a.append([x,y,z])
if z*y:
both.append(x)
else:
if z:
two.append(x)
if y:
one.append(x)
both.sort()
one.sort()
two.sort()
ans=0
tot=k
bo=len(both)
on=len(one)
tw=len(two)
if bo + min(on,tw)<k:
print(-1)
else:
i=0
if bo<k:
for i in range(k-bo):
ans+=(one[i] + two[i])
tot-=1
i+=1
c=0
while tot:
if i>=on or i>=tw:
break
if one[i]+two[i]<both[c]:
ans+=one[i]+two[i]
i+=1
tot-=1
else:
ans+=both[c]
c+=1
tot-=1
while tot:
ans+=both[c]
c+=1
tot-=1
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> D;
vector<long long> B;
vector<long long> A;
int main() {
long long a = 0, b = 0;
long long n, k;
long long ti, al, bl, time = 0;
cin >> n >> k;
for (long long i = 0; i < n; ++i) {
cin >> ti >> al >> bl;
if (al + bl == 2)
D.push_back(ti);
else if (al == 1)
A.push_back(ti);
else if (bl == 1)
B.push_back(ti);
a += al;
b += bl;
}
if (a < k || b < k) {
cout << -1 << endl;
return 0;
}
if (n == k && k == a && k == b) {
time += accumulate(D.begin(), D.end(), 0);
time += accumulate(A.begin(), A.end(), 0);
time += accumulate(B.begin(), B.end(), 0);
cout << time << endl;
return 0;
}
a = b = k;
sort(D.begin(), D.end());
sort(A.begin(), A.end());
sort(B.begin(), B.end());
for (long long i = 0, j = 0, l = 0; a > 0 || b > 0;) {
if (i < D.size() && j < A.size() && l < B.size()) {
if (D[i] <= A[j] + B[l]) {
time += D[i];
++i;
--a;
--b;
} else {
time += A[j] + B[l];
++j;
++l;
--a;
--b;
}
} else if (i < D.size() && j < A.size() && l >= B.size()) {
if (a > 0 && b > 0) {
time += D[i];
++i;
--a;
--b;
} else if (a > 0 && b <= 0) {
if (D[i] < A[j]) {
time += D[i];
++i;
--a;
--b;
} else {
time += A[j];
++j;
--a;
}
}
} else if (i < D.size() && j >= A.size() && l < B.size()) {
if (a > 0 && b > 0) {
time += D[i];
++i;
--a;
--b;
} else if (a <= 0 && b > 0) {
if (D[i] < B[l]) {
time += D[i];
++i;
--a;
--b;
} else {
time += B[l];
++l;
--b;
}
}
} else if (i >= D.size()) {
if (a > 0 && j < A.size()) {
time += A[j];
++j;
--a;
} else if (b > 0 && l < B.size()) {
time += B[l];
++l;
--b;
}
} else if (i < D.size() && j >= A.size() && l >= B.size()) {
time += D[i];
++i;
--a;
--b;
}
}
cout << time << endl;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class EasyReading {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// int cases = scanner.nextInt();
// for (int i = 0; i < cases; i++) {
solve();
// }
}
private static void solve() {
int n = scanner.nextInt();
int k = scanner.nextInt();
long[] all = new long[n];
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
List<Integer> both = new ArrayList<>();
for (int i = 0; i < n; i++) {
all[i] = scanner.nextInt();
int isA = scanner.nextInt();
int isB = scanner.nextInt();
if (isA == 1 && isB == 1) {
both.add(i);
} else {
if (isA == 1) {
a.add(i);
}
if (isB == 1) {
b.add(i);
}
}
}
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return (int) (all[o1] - all[o2]);
}
};
a.sort(comparator);
b.sort(comparator);
both.sort(comparator);
int i = 0;
int j = 0;
long time = 0;
while (i + j < k && (i < a.size() && i < b.size() || j < both.size())) {
if (i < a.size() && i < b.size()) {
long tmp = all[a.get(i)] + all[b.get(i)];
if (j < both.size() && tmp > all[both.get(j)]) {
time += all[both.get(j)];
j++;
} else {
time += tmp;
i++;
}
} else {
time += all[both.get(j)];
j++;
}
}
if (i + j == k)
System.out.println(time);
else
System.out.println(-1);
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, k, c1 = 0, c2 = 0, v = 0, ans = 0, res;
cin >> n >> k;
long long a[n][3];
vector<long long> s1, s2, s3;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < 3; j++) {
cin >> a[i][j];
if (j == 1) {
if (a[i][j] == 1) c1++;
} else if (j == 2) {
if (a[i][j] == 1) c2++;
}
}
}
if (c1 < k || c2 < k) {
cout << "-1"
<< "\n";
} else {
for (long long i = 0; i < n; i++) {
if (a[i][1] && a[i][2])
s1.push_back(a[i][0]);
else if (a[i][1])
s2.push_back(a[i][0]);
else if (a[i][2])
s3.push_back(a[i][0]);
}
sort(s2.begin(), s2.end());
sort(s3.begin(), s3.end());
if (s2.size() < s3.size())
res = s2.size();
else
res = s3.size();
for (long long i = 0; i < res; i++) {
s1.push_back(s2[i] + s3[i]);
}
sort(s1.begin(), s1.end());
for (auto i : s1) {
if (v >= k) break;
ans += i;
v++;
}
cout << ans << "\n";
s1.clear();
s2.clear();
s3.clear();
}
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 collections as cc
import math as mt
import sys
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
n,k=I()
a=[]
for i in range(n):
a.append(I())
a.sort()
both=[]
bs=0
f=0
for i in range(n):
if a[i][1] and a[i][2]:
both.append(a[i][0])
al=[]
bo=[]
for i in range(n):
if a[i][1] and not a[i][2]:
al.append(a[i][0])
elif a[i][2] and not a[i][1]:
bo.append(a[i][0])
su=[]
for i in range(min(len(al),len(bo))):
su.append(al[i]+bo[i])
xx=len(both)
yy=len(su)
if xx+yy<k:
print(-1)
elif both and not su:
if xx>=k:
print(sum(both[:k]))
else:
print(-1)
elif su and not both:
if yy>=k:
print(sum(su[:k]))
else:
print(-1)
else:
i=0
j=0
cn=0
ans=0
while i<xx and j<yy:
if both[i]<su[j]:
ans+=both[i]
i+=1
cn+=1
else:
ans+=su[j]
j+=1
cn+=1
if cn==k:
break
if cn<k:
while i<xx:
ans+=both[i]
cn+=1
i+=1
if cn==k:
break
if cn<k:
while j<yy:
ans+=su[j]
cn+=1
j+=1
if cn==k:
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
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
public final class E {
public static void main(String[] args) {
final FastScanner fs = new FastScanner();
final int n = fs.nextInt();
final int k = fs.nextInt();
final List<Integer> a = new ArrayList<>();
final List<Integer> b = new ArrayList<>();
final List<Integer> both = new ArrayList<>();
for (int i = 0; i < n; i++) {
final int t = fs.nextInt();
final int aa = fs.nextInt();
final int bb = fs.nextInt();
if (aa == 1 && bb == 1) {
both.add(t);
} else if (aa == 1) {
a.add(t);
} else if (bb == 1) {
b.add(t);
}
}
a.sort(Comparator.naturalOrder());
b.sort(Comparator.naturalOrder());
both.sort(Comparator.naturalOrder());
int common = Math.min(a.size(), b.size());
long res = (long) 1e18;
if (common >= k) {
long aSum = 0;
long bSum = 0;
for (int i = 0; i < k; i++) {
aSum += a.get(i);
bSum += b.get(i);
}
res = Math.min(res, aSum + bSum);
long bothSum = 0;
for (int i = 0, j = k - 1; i < Math.min(k, both.size()); i++, j--) {
aSum -= a.get(j);
bSum -= b.get(j);
bothSum += both.get(i);
res = Math.min(res, aSum + bSum + bothSum);
}
} else {
int diff = k - common;
long aSum = 0;
long bSum = 0;
for (int i = 0; i < common; i++) {
aSum += a.get(i);
bSum += b.get(i);
}
long bothSum = 0;
if (both.size() >= diff) {
for (int i = 0; i < diff; i++) {
bothSum += both.get(i);
}
res = Math.min(res, aSum + bSum + bothSum);
for (int i = diff, j = common - 1; i < Math.min(k, both.size()); i++, j--) {
aSum -= a.get(j);
bSum -= b.get(j);
bothSum += both.get(i);
res = Math.min(res, aSum + bSum + bothSum);
}
}
}
System.out.println(res == (long) 1e18 ? -1 : res);
}
static final class Utils {
public static void shuffleSort(int[] x) {
shuffle(x);
Arrays.sort(x);
}
public static void shuffleSort(long[] x) {
shuffle(x);
Arrays.sort(x);
}
public static void shuffle(int[] x) {
final Random r = new Random();
for (int i = 0; i <= x.length - 2; i++) {
final int j = i + r.nextInt(x.length - i);
swap(x, i, j);
}
}
public static void shuffle(long[] x) {
final Random r = new Random();
for (int i = 0; i <= x.length - 2; i++) {
final int j = i + r.nextInt(x.length - i);
swap(x, i, j);
}
}
public static void swap(int[] x, int i, int j) {
final int t = x[i];
x[i] = x[j];
x[j] = t;
}
public static void swap(long[] x, int i, int j) {
final long t = x[i];
x[i] = x[j];
x[j] = t;
}
private Utils() {}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
private String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] nextIntArray(int n) {
final int[] a = new int[n];
for (int i = 0; i < n; i++) { a[i] = nextInt(); }
return a;
}
long[] nextLongArray(int n) {
final long[] a = new long[n];
for (int i = 0; i < n; i++) { a[i] = nextLong(); }
return a;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
x=[];y=[];z=[]
for i in range(n):
t,a,b=map(int,input().split())
if a and b:x.append(t)
elif a==1:y.append(t)
elif b==1:z.append(t)
y.sort();z.sort()
for p,q in zip(y,z):
x.append(p+q)
x.sort()
if len(x)<k:print(-1)
else:print(sum(x[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.AbstractCollection;
import java.util.PriorityQueue;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion();
solver.solve(1, in, out);
out.close();
}
static class E1ReadingBooksEasyVersion {
int IINF = (int) 1e9 + 331;
public void solve(int testNumber, InputReader in, OutputWriter out) {
long n = in.nextInt(), k = in.nextInt();
long ans = 0;
PriorityQueue<Integer> both = new PriorityQueue<>(), aLikes = new PriorityQueue<>(), bLikes = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
int time = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
if (a == b && b == 1) {
both.add(time);
}
if (a != b && a == 1) {
aLikes.add(time);
}
if (a != b && b == 1) {
bLikes.add(time);
}
}
int aGot = 0, bGot = 0;
while (aGot < k || bGot < k) {
if ((aLikes.size() == 0 || bLikes.size() == 0) && both.size() == 0 && (aGot < k || bGot < k)) {
break;
}
int blbboth = IINF;
int blbalice = IINF;
int blbbob = IINF;
if (!both.isEmpty()) {
blbboth = both.peek();
}
if (!aLikes.isEmpty()) {
blbalice = aLikes.peek();
}
if (!bLikes.isEmpty()) {
blbbob = bLikes.peek();
}
if (blbboth <= blbalice + blbbob || (blbalice == IINF || blbbob == IINF)) {
ans += blbboth;
both.poll();
aGot++;
bGot++;
} else if (blbboth > blbalice + blbbob || blbboth == IINF) {
ans += (blbalice + blbbob);
aLikes.poll();
bLikes.poll();
aGot++;
bGot++;
} else {
break;
}
}
if (aGot < k || bGot < k) {
out.println(-1);
} else {
out.println(ans);
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public 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 boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int 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
|
python3
|
def solve(n, k, a, b, c):
a.sort()
b.sort()
c.sort()
# print(a, b, c)
iab = min(k, len(a), len(b))
ic = k - iab
if ic > len(c):
return -1
res = 0
for i in range(ic):
res += c[i]
for i in range(iab):
res += a[i] + b[i]
iab -= 1
while ic < len(c) and iab >= 0 and c[ic] < a[iab] + b[iab]:
res -= a[iab] + b[iab] - c[ic]
ic += 1
iab -= 1
return res
current_str = input().split(" ")
n = int(current_str[0])
k = int(current_str[1])
a = []
b = []
c = []
for i in range(n):
current_str = input().split(" ")
t = int(current_str[0])
aa = int(current_str[1])
bb = int(current_str[2])
if aa and bb:
c.append(t)
elif aa:
a.append(t)
elif bb:
b.append(t)
print(solve(n, k, a, b, 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
|
java
|
import java.io.*;
import java.util.*;
import java.lang.*;
public class Codechef {
PrintWriter out;
StringTokenizer st;
BufferedReader br;
class Pair implements Comparable<Pair>
{
int f;
int s;
Pair(int t, int r) {
f = t;
s = r;
}
public int compareTo(Pair p)
{
if(this.f!=p.f)
return this.f-p.f;
return this.s-p.s;
}
}
// class Sort implements Comparator<String>
// {
// public int compare(String a, String b)
// {
// return (a+b).compareTo(b+a);
// }
// }
String ns() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
int nextInt() {
return Integer.parseInt(ns());
}
long nextLong() {
return Long.parseLong(ns());
}
double nextDouble() {
return Double.parseDouble(ns());
}
int upperBound(long a[],long key)
{
int l=0,r=a.length-1;
int i=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(a[mid]<key)
l=mid+1;
else{
i=mid;
r=mid-1;
}
}
return i;
}
int lowerBound(Pair[] a , long key)
{
int l = 0, r = a.length- 1;
int i = -1;
while(l<=r)
{
int mid=(l+r)/2;
if(a[mid].f<key)
{
i=mid;
l=mid+1;
}
else
r=mid-1;
}
return i;
}
long power(long x,long y)
{
long ans=1;
while(y!=0)
{
if(y%2==1) ans=(ans*x)%mod;
x=(x*x)%mod;
y/=2;
}
return ans%mod;
}
int mod= 1000000007;
long gcd(long x ,long y)
{
if(y==0)
return x;
return gcd(y,x%y);
}
// ArrayList a[];
// int vis[],cnt=0;
// void dfs(int ver)
// {
// ArrayList<Integer> l=a[ver];
// if(l.size()==1)
// cnt++;
// for(int v:l)
// {
// if(vis[v]==0){
// vis[v]=vis[ver]+1;
// dfs(v);
// }
// }
// }
int countSetBits(long n)
{
int count = 0;
while (n > 0) {
n &= (n - 1);
count++;
}
return count;
}
void solve() throws IOException{
int n = nextInt();
int k = nextInt();
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> alice = new ArrayList<>();
ArrayList<Integer> bob = new ArrayList<>();
for(int i = 0; i < n; i++)
{
int time = nextInt();
int a = nextInt();
int b = nextInt();
if(a == 1 && b == 1)
list.add(time);
else if(a == 1)
alice.add(time);
else if(b == 1)
bob.add(time);
}
// System.out.println(list);
// System.out.println(alice);
// System.out.println(bob);
Collections.sort(list);
Collections.sort(alice);
Collections.sort(bob);
int m = Math.min(alice.size(), bob.size());
if(list.size() + m < k){
out.println(-1);
return;
}
int i = 0, j = 0;
long sum = 0;
while(i < list.size() && i < k)
{
sum += list.get(i++);
}
while(j < m && (i + j) < k)
{
sum += alice.get(j);
sum += bob.get(j++);
}
while(i > 0 && j < m)
{
long x = list.get(--i);
long y = alice.get(j) + bob.get(j++);
if(y < x)
{
sum -= x;
sum += y;
}
else
break;
}
out.println(sum);
}
void run() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
out.close();
}
public static void main(String args[]) throws IOException {
new Codechef().run();
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"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())
ablike = []
alike = []
blike = []
for i in range(n):
t,a,b = map(int,input().split())
if a == 1 and b == 1:
ablike.append(t)
elif a == 1:
alike.append(t)
elif b == 1:
blike.append(t)
ablike.sort()
abtot = len(ablike)
alike.sort()
atot = len(alike)
blike.sort()
btot = len(blike)
x = min(atot,btot)
if abtot + x < k:
print(-1)
else:
p = k
i = 0
j = 0
tot = 0
while p > 0:
if j < abtot and i < x:
if ablike[j] <= (alike[i] + blike[i]):
tot = tot + ablike[j]
j = j + 1
p = p - 1
else:
tot = tot + alike[i] + blike[i]
i = i + 1
p = p - 1
elif j < abtot:
tot = tot + ablike[j]
j = j + 1
p = p - 1
else:
tot = tot + alike[i] + blike[i]
i = i + 1
p = p - 1
print(tot)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int MAXN = 3e5 + 10;
const double eps = 1e-8;
const int inf = 2e9 + 9;
using namespace std;
struct edge {
int t, v;
edge *next;
} e[MAXN << 1], *h[MAXN], *o = e;
void add(int x, int y, int vul) {
o->t = y;
o->v = vul;
o->next = h[x];
h[x] = o++;
}
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
pair<int, int> a[4][MAXN];
int cnt[4];
int num[MAXN << 2], sum[MAXN << 2];
void up(int x) {
num[x] = num[x << 1] + num[x << 1 | 1];
sum[x] = sum[x << 1] + sum[x << 1 | 1];
}
void update(int x, int l, int r, int t, int y) {
if (l == r) {
num[x] += y;
sum[x] = l * num[x];
return;
}
int mid = (l + r) >> 1;
if (t <= mid)
update(x << 1, l, mid, t, y);
else
update(x << 1 | 1, mid + 1, r, t, y);
up(x);
}
int ans;
void query(int x, int l, int r, int k) {
if (!k) return;
if (l == r) {
ans += k * l;
return;
}
int mid = (l + r) >> 1;
if (num[x << 1] >= k)
query(x << 1, l, mid, k);
else
ans += sum[x << 1], query(x << 1 | 1, mid + 1, r, k - num[x << 1]);
}
int n, m, k;
int sum1[MAXN], sum2[MAXN];
pair<int, int> b[MAXN];
int main() {
n = read();
m = read();
k = read();
int x, y, z;
int sz = 1e4;
for (int i = 1; i <= n; i++) {
x = read();
y = read();
z = read();
int p = 2 * y + z;
a[p][++cnt[p]] = make_pair(x, i);
}
for (int i = 0; i <= 3; i++) sort(a[i] + 1, a[i] + cnt[i] + 1);
for (int j = 0; j <= 2; j++)
for (int i = 1; i <= cnt[j]; i++) update(1, 1, sz, a[j][i].first, 1);
int pos = cnt[3];
int minn = inf;
int p = -1;
for (int i = 1; i <= min(cnt[1], cnt[2]); i++)
sum1[i] = sum1[i - 1] + a[1][i].first + a[2][i].first;
for (int i = 1; i <= cnt[3]; i++) sum2[i] = sum2[i - 1] + a[3][i].first;
for (int i = 0; i <= min(cnt[1], cnt[2]); i++) {
if (i > 0) {
update(1, 1, sz, a[1][i].first, -1);
update(1, 1, sz, a[2][i].first, -1);
}
if (i > k) continue;
if (cnt[3] < k - i) continue;
if (2 * i + k - i > m) continue;
while (pos > k - i) {
update(1, 1, sz, a[3][pos].first, 1);
pos--;
}
ans = 0;
query(1, 1, sz, m - k - i);
if (ans + sum1[i] + sum2[pos] < minn) {
minn = ans + sum1[i] + sum2[pos];
p = i;
}
}
if (minn == inf) return 0 * printf("-1\n");
printf("%d\n", minn);
for (int i = 1; i <= p; i++) printf("%d %d ", a[1][i].second, a[2][i].second);
for (int i = 1; i <= k - p; i++) printf("%d ", a[3][i].second);
int tot = 0;
for (int i = 1; i <= cnt[0]; i++) b[++tot] = a[0][i];
for (int i = p + 1; i <= cnt[1]; i++) b[++tot] = a[1][i];
for (int i = p + 1; i <= cnt[2]; i++) b[++tot] = a[2][i];
for (int i = k - p + 1; i <= cnt[3]; i++) b[++tot] = a[3][i];
sort(b + 1, b + tot + 1);
for (int i = k + p + 1; i <= m; i++) printf("%d ", b[i - k - p].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.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class HelloWorld{
public static void main(String []args) throws IOException{
Reader sc=new Reader();
int n=sc.nextInt(),k=sc.nextInt(),A=0,B=0;
int t[]=new int[n],a[]=new int[n],b[]=new int[n];
ArrayList<Long> sum[]=new ArrayList[4],times[]=new ArrayList[4];
for(int i=0;i<4;i++){
sum[i]=new ArrayList<Long>();
times[i]=new ArrayList<Long>();
sum[i].add(0l);
}
for(int i=0;i<n;i++){
t[i]=sc.nextInt();
a[i]=sc.nextInt();
b[i]=sc.nextInt();
A+=a[i];
B+=b[i];
int pos=a[i]*2+b[i];
times[pos].add(t[i]+0l);
}
if(A<k||B<k){
System.out.println("-1");
return;
}
for(int i=0;i<4;i++){
Collections.sort(times[i]);
for(int j=0;j<times[i].size();j++){
long lastEle=sum[i].get(sum[i].size()-1);
sum[i].add(times[i].get(j)+lastEle);
}
}
long ans=Integer.MAX_VALUE;
for(int count=0;count<Integer.min(k+1,sum[3].size());count++){
int req=k-count;
if(req<sum[1].size()&&req<sum[2].size()){
ans=Math.min(ans,sum[3].get(count)+sum[1].get(req)+sum[2].get(req));
}
}
System.out.println(ans);
}
}
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;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long powM(long long x, long long y, long long m) {
if (y == 0) return 1;
long long p = powM(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> vt, va, vb;
for (long long i = 1; i <= n; i++) {
int t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1) vt.push_back(t);
if (a == 1 && b == 0) va.push_back(t);
if (a == 0 && b == 1) vb.push_back(t);
}
sort(va.begin(), va.end());
sort(vb.begin(), vb.end());
for (long long i = 0; i <= min((int)va.size() - 1, (int)vb.size() - 1); i++)
vt.push_back(va[i] + vb[i]);
sort(vt.begin(), vt.end());
if ((int)vt.size() < k) {
cout << -1 << '\n';
return 0;
}
int sum = 0;
for (long long i = 0; i <= k - 1; i++) sum += vt[i];
cout << sum << '\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
|
java
|
import java.io.*;
import java.text.*;
import java.util.*;
import java.math.*;
public class template {
public static void main(String[] args) throws Exception {
new template().run();
}
public void run() throws Exception {
FastScanner f = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
///
int n = f.nextInt(), m = f.nextInt(), k = f.nextInt();
ArrayList<Pair> _00 = new ArrayList<>();
ArrayList<Pair> _01 = new ArrayList<>();
ArrayList<Pair> _10 = new ArrayList<>();
ArrayList<Pair> _11 = new ArrayList<>();
Pair[] p = new Pair[n];
for(int i = 0; i < n; i++) {
int t = f.nextInt(), a = f.nextInt(), b = f.nextInt();
p[i] = new Pair(t, i);
if(a == 0 && b == 0) _00.add(new Pair(t, i));
if(a == 0 && b == 1) _01.add(new Pair(t, i));
if(a == 1 && b == 0) _10.add(new Pair(t, i));
if(a == 1 && b == 1) _11.add(new Pair(t, i));
}
Arrays.sort(p);
int[] to = new int[n];
for(int i = 0; i < n; i++) to[p[i].b] = i;
BIT b = new BIT(n);
Collections.sort(_11);
Collections.sort(_01);
Collections.sort(_10);
for(Pair i : _00) b.add(to[i.b], i.a);
for(Pair i : _01) b.add(to[i.b], i.a);
for(Pair i : _10) b.add(to[i.b], i.a);
for(Pair i : _11) b.add(to[i.b], i.a);
if(Math.min(_10.size(),_01.size()) + _11.size() < k) {
out.println(-1);
out.flush();
return;
}
int ans = 2147483647;
int cur = 0;
int cnt = 0;
for(Pair i : _11) {
cur += i.a;
b.remove(to[i.b], i.a);
cnt++;
}
for(int i = 0; i < k-_11.size(); i++) {
cur += _01.get(i).a+_10.get(i).a;
cnt += 2;
b.remove(to[_01.get(i).b], _01.get(i).a);
b.remove(to[_10.get(i).b], _10.get(i).a);
}
int besti = _11.size();
if(cnt <= m) ans = cur+b.get(m-cnt);
for(int c = _11.size()-1; c >= 0; c--) {
cur -= _11.get(c).a;
cnt--;
b.add(to[_11.get(c).b], _11.get(c).a);
if(c < k) {
if(k-c-1 >= Math.min(_01.size(), _10.size())) break;
cur += _01.get(k-c-1).a+_10.get(k-c-1).a;
b.remove(to[_01.get(k-c-1).b], _01.get(k-c-1).a);
b.remove(to[_10.get(k-c-1).b], _10.get(k-c-1).a);
cnt += 2;
}
int a = cur+b.get(m-cnt);
if(a < ans && m-cnt >= 0) {
ans = a;
besti = c;
}
}
if(ans == 2147483647) {
out.println(-1);
out.flush();
return;
}
out.println(ans);
PriorityQueue<Pair> ts = new PriorityQueue<>();
cnt = m;
for(int i = 0; i < _11.size(); i++)
if(i < besti) {
out.print(_11.get(i).b + 1 + " ");
cnt--;
} else ts.add(_11.get(i));
for(int i = 0; i < _10.size(); i++)
if(i < k-besti) {
out.print(_10.get(i).b + 1 + " ");
cnt--;
} else ts.add(_10.get(i));
for(int i = 0; i < _01.size(); i++)
if(i < k-besti) {
out.print(_01.get(i).b + 1 + " ");
cnt--;
} else ts.add(_01.get(i));
ts.addAll(_00);
while(cnt-->0) out.print(ts.poll().b + 1 + " ");
out.println();
///
out.flush();
}
class Pair implements Comparable<Pair> {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair p) {
if(a == p.a) return Integer.compare(b, p.b);
return Integer.compare(a, p.a);
}
}
class BIT {
int[] a, b;
public BIT(int sz) {
a = new int[sz+1];
b = new int[sz+1];
}
public void add(int i, int v) {
i++;
while(i < a.length) {
a[i]++;
b[i] += v;
i += i & -i;
}
}
public void remove(int i, int v) {
i++;
while(i < a.length) {
a[i]--;
b[i] -= v;
i += i & -i;
}
}
public int get(int cnt) {
int i = 0;
int v = 0;
int v2 = 0;
for(int bit = 1 << 20; bit > 0; bit >>= 1) {
if(i+bit >= a.length) continue;
if(v+a[i+bit] <= cnt) {
i += bit;
v += a[i];
v2 += b[i];
}
}
return v2;
}
}
///
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"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());z=[];x=[];y=[]
for _ in range(n):
t,a,b=map(int,input().split())
if a&b:z.append(t)
elif a:x.append(t)
elif b:y.append(t)
x.sort();y.sort()
for i in range(min(len(x),len(y))):z.append(x[i]+y[i])
print(-1if len(z)<k else sum(sorted(z)[: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)
elif y == 0 and z == 1:
b.append(x)
elif 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)):
c = c + sum(ab[e:e + k - h])
break
elif e == len(ab):
c = c + sum(a[p:p + k - h]) + sum(b[p:p + k - h])
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
|
from sys import stdin
input=lambda : stdin.readline().strip()
char = [chr(i) for i in range(97,123)]
CHAR = [chr(i) for i in range(65,91)]
mp = lambda:list(map(int,input().split()))
INT = lambda:int(input())
rn = lambda:range(INT())
from math import ceil,sqrt,factorial,gcd
n,k = mp()
alice = []
bob = []
common = []
for i in range(n):
t,a,b = mp()
if a==b==1:
common.append(t)
elif a==1:
alice.append(t)
elif b==1:
bob.append(t)
if len(common + alice)<k or len(common + bob) < k:
print(-1)
elif (len(alice)==0 or len(bob)==0) and len(common)>=k:
common.sort()
print(sum(common[:k]))
else:
alice.sort()
bob.sort()
common.sort()
inx = min(k,len(alice),len(bob))
sma = sum(alice[:inx])
smb = sum(bob[:inx])
res = sma+smb
if inx<k:
res+=sum(common[:k-inx])
common_inx = k-inx
inx = inx
res1 = res
while common_inx < len(common) and inx>0:
res -= alice[inx-1]
res -= bob[inx-1]
res += common[common_inx]
res1 = min(res,res1)
inx-=1
common_inx+=1
print(res1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"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;
inline long long read() {
long long f = 1, ans = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
const long long MAXN = 4e5 + 11;
long long N, M, K, na, nb, nc, nd, Minn = LLONG_MAX, SA[MAXN], SB[MAXN],
SC[MAXN], SD[MAXN];
long long tmp1[MAXN], tmp2[MAXN], tmp3[MAXN], tmp4[MAXN];
pair<long long, long long> A[MAXN], B[MAXN], C[MAXN], D[MAXN];
long long sta1[MAXN], sta2[MAXN], sta3[MAXN], sta4[MAXN];
long long QA(long long x) {
return lower_bound(tmp1 + 1, tmp1 + N + 1, x + 1) - tmp1 - 1;
}
long long QB(long long x) {
return lower_bound(tmp2 + 1, tmp2 + N + 1, x + 1) - tmp2 - 1;
}
long long QD(long long x) {
return lower_bound(tmp4 + 1, tmp4 + N + 1, x + 1) - tmp4 - 1;
}
signed main() {
N = read(), M = read(), K = read();
for (long long i = 1; i <= N; i++) {
long long t = read(), a = read(), b = read();
pair<long long, long long> p = make_pair(t, i);
if (a && b) C[++nc] = p;
if (a && (!b)) A[++na] = p;
if ((!a) && b) B[++nb] = p;
if ((!a) && (!b)) D[++nd] = p;
}
for (long long i = na + 1; i <= N; i++) A[i] = make_pair(INT_MAX, 0);
for (long long i = nb + 1; i <= N; i++) B[i] = make_pair(INT_MAX, 0);
for (long long i = nc + 1; i <= N; i++) C[i] = make_pair(INT_MAX, 0);
for (long long i = nd + 1; i <= N; i++) D[i] = make_pair(INT_MAX, 0);
sort(A + 1, A + N + 1), sort(B + 1, B + N + 1), sort(C + 1, C + N + 1),
sort(D + 1, D + N + 1);
for (long long i = 1; i <= N; i++)
SA[i] = SA[i - 1] + A[i].first, SB[i] = SB[i - 1] + B[i].first,
SC[i] = SC[i - 1] + C[i].first, SD[i] = SD[i - 1] + D[i].first;
for (long long i = 1; i <= N; i++)
tmp1[i] = A[i].first, tmp2[i] = B[i].first, tmp3[i] = C[i].first,
tmp4[i] = D[i].first;
for (long long i = 0; i <= N; i++) {
long long res = (max(K - i, 0ll)) * 2 + i;
if (res > M) continue;
if (res < 0) continue;
long long ps1 = max(K - i, 0ll), ps2 = max(K - i, 0ll), l = 0, r = 10000,
Ans = -1;
long long Res = M - res;
long long u1 = 0, u2 = 0, u4 = 0;
while (l <= r) {
long long mid = (l + r) >> 1;
long long X = max(QA(mid) - ps1, 0ll), Y = max(QB(mid) - ps2, 0ll),
Z = QD(mid);
if (X + Y + Z >= Res)
Ans = mid, u1 = X, u2 = Y, u4 = Z, r = mid - 1;
else
l = mid + 1;
}
if (Ans == -1) continue;
long long W = SA[u1 + ps1] + SB[u2 + ps2] + SC[i] + SD[u4];
long long Ha = (u1 + ps1 + u2 + ps2 + i + u4) - M;
W -= Ha * Ans;
Minn = min(Minn, W);
}
if (Minn > 2000000000) {
printf("-1\n");
return 0;
}
printf("%lld\n", Minn);
for (long long i = 0; i <= N; i++) {
long long res = (max(K - i, 0ll)) * 2 + i;
if (res > M) continue;
if (res < 0) continue;
long long ps1 = max(K - i, 0ll), ps2 = max(K - i, 0ll), l = 0, r = 10000,
Ans = -1;
long long Res = M - res;
long long u1 = 0, u2 = 0, u4 = 0;
while (l <= r) {
long long mid = (l + r) >> 1;
long long X = max(QA(mid) - ps1, 0ll), Y = max(QB(mid) - ps2, 0ll),
Z = QD(mid);
if (X + Y + Z >= Res)
Ans = mid, u1 = X, u2 = Y, u4 = Z, r = mid - 1;
else
l = mid + 1;
}
if (Ans == -1) continue;
long long W = SA[u1 + ps1] + SB[u2 + ps2] + SC[i] + SD[u4];
long long Ha = (u1 + ps1 + u2 + ps2 + i + u4) - M;
W -= Ha * Ans;
if (Minn == W) {
long long tot1 = 0, tot2 = 0, tot3 = 0, tot4 = 0;
for (long long j = 1; j <= u1 + ps1; j++) {
if (A[j].first != Ans)
printf("%lld ", A[j].second), tot1++;
else
sta1[++sta1[0]] = A[j].second;
}
for (long long j = 1; j <= u2 + ps2; j++) {
if (B[j].first != Ans)
printf("%lld ", B[j].second), tot2++;
else
sta2[++sta2[0]] = B[j].second;
}
for (long long j = 1; j <= u4; j++) {
if (D[j].first != Ans)
printf("%lld ", D[j].second), tot4++;
else
sta4[++sta4[0]] = D[j].second;
}
for (long long j = 1; j <= i; j++) {
if (C[j].first != Ans)
printf("%lld ", C[j].second), tot3++;
else
sta3[++sta3[0]] = C[j].second;
}
long long E = M - tot1 - tot2 - tot3 - tot4, ps1 = 1, ps2 = 1, ps3 = 1,
ps4 = 1;
for (long long i = 1; i <= E; i++) {
if (tot1 < tot2 && ps1 <= sta1[0]) {
printf("%lld ", sta1[ps1]);
ps1++;
tot1++;
continue;
}
if (tot1 > tot2 && ps2 <= sta2[0]) {
printf("%lld ", sta2[ps2]);
ps2++;
tot2++;
continue;
}
if (ps3 <= sta3[0]) {
printf("%lld ", sta3[ps3]);
ps3++;
continue;
}
if (ps1 <= sta1[0]) {
printf("%lld ", sta1[ps1]);
ps1++;
continue;
}
if (ps2 <= sta2[0]) {
printf("%lld ", sta2[ps2]);
ps2++;
continue;
}
if (ps4 <= sta4[0]) {
printf("%lld ", sta4[ps4]);
ps4++;
continue;
}
}
printf("\n");
return 0;
}
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.*;
public class newr
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int t,i;
t=1;
while(t>0)
{
t--;
int n=sc.nextInt();
int k=sc.nextInt();
int d[][]=new int[n][3];
int al=0,bl=0;
for(i=0;i<n;i++)
{
d[i][0]=sc.nextInt();
d[i][1]=sc.nextInt();
d[i][2]=sc.nextInt();
if(d[i][1]==1)
al++;
if(d[i][2]==1)
bl++;
}
if(al<k||bl<k)
{
System.out.println(-1);
return;
}
ArrayList<Integer> b=new ArrayList();
ArrayList<Integer> a=new ArrayList();
ArrayList<Integer> bo=new ArrayList();
for(i=0;i<n;i++)
{
if(d[i][1]==1&&d[i][2]==1)
b.add(d[i][0]);
else if(d[i][1]==0&&d[i][2]==1)
bo.add(d[i][0]);
else if(d[i][1]==1&&d[i][2]==0)
a.add(d[i][0]);
}
Collections.sort(b);
Collections.sort(bo);
Collections.sort(a);
long ans=0;
int j,l,m;
j=0;l=0;m=0;
for(i=0;i<n;i++)
{
if(l<b.size()&&j<bo.size()&&m<a.size())
{
int s=b.get(l);
int dono=bo.get(j)+a.get(m);
if(s<=dono)
{
ans+=s;
l++;
}
else
{
ans+=dono;
j++;
m++;
}
k--;
if(k==0)
break;
}
else break;
}
if(k>0)
{
if(l==b.size())
{
while(k>0)
{
int dono=bo.get(j)+a.get(m);
ans+=dono;
j++;
m++;
k--;
}
}
else
{
while(k>0)
{
int s=b.get(l);
ans+=s;
l++;
k--;
}
}
}
System.out.println(ans);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
# https://codeforces.com/contest/1374/problem/E1
def min_time(tot_books, books_like, read_time, a_time, b_time):
time = []
temp_a = []
temp_b = []
if min(sum(a_time), sum(b_time)) >= books_like:
for x in range(tot_books):
if a_time[x] == b_time[x] == 1:
time.append(read_time[x])
elif a_time[x] == 0 and b_time[x] == 1:
temp_b.append(read_time[x])
elif a_time[x] == 1 and b_time[x] == 0:
temp_a.append(read_time[x])
temp_a.sort(), temp_b.sort()
for y in range(min(len(temp_a), len(temp_b))):
time.append(temp_a[y] + temp_b[y])
time.sort()
time = time[:books_like]
return sum(time)
else:
return -1
# if len(time) > books_like:
# time = time[books_like - 1::-1]
# elif len(time) < books_like:
# while len(time) != books_like:
# time.append(temp_a[0] + temp_b[0])
# del (temp_a[0], temp_b[0])
# time.sort(reverse=True)
# else:
# time.sort(reverse=True)
# y = 0
# while y != (min(len(temp_a), len(temp_b))):
# if time[y] > temp_a[y] + temp_b[y]:
# time[y] = temp_a[y] + temp_b[y]
# else:
# break
# y += 1
n, k = map(int, input().split())
t = []
a = []
b = []
for i in range(n):
x, y, z = map(int, input().split())
t.append(x), a.append(y), b.append(z)
print(min_time(n, k, t, a, b))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import java.util.HashSet;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.text.DecimalFormat;
import java.lang.Math;
import java.util.Iterator;
import java.util.TreeSet;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.*;
public class D1343{
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static long MOD = (long)(1e9+7);
static FastReader sc = new FastReader();
static int pInf = Integer.MAX_VALUE;
static int nInf = Integer.MIN_VALUE;
public static void main(String[] args){
int t = 1;
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
ArrayList<Integer> A = new ArrayList<Integer>();
ArrayList<Integer> B = new ArrayList<Integer>();
ArrayList<Integer> AB = new ArrayList<Integer>();
int a = 0;
int ab = 0;
int b = 0;
while(n-->0){
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
if(y==1 && z==1){
AB.add(x);
ab++;
}
else if(y==1 && z==0){
A.add(x);
a++;
}
else if(z==1 && y==0){
B.add(x);
b++;
}
}
Collections.sort(A);
Collections.sort(AB);
Collections.sort(B);
long sum = 0;
int p = 0;
int r = 0;
while(k-->0){
if((a>0) && (ab>0) && (b>0)){
if((A.get(p)+B.get(p)) < AB.get(r)){
sum += A.get(p)+B.get(p);
p++;
a--;
b--;
}
else{
sum += AB.get(r);
r++;
ab--;
}
}
else if(ab>0){
sum += AB.get(r);
r++;
ab--;
}
else if(a>0 && b>0){
sum += A.get(p)+B.get(p);
p++;
a--;
b--;
}
else{
sum = -1;
break;
}
}
out.println(sum);
}
out.close();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Integer.lowestOneBit(i) Equals k where k is the position of the first one in the binary
//Integer.highestOneBit(i) Equals k where k is the position of the last one in the binary
//Integer.bitCount(i) returns the number of one-bits
//Collections.sort(A,(p1,p2)->(int)(p2.x-p1.x)) To sort ArrayList in descending order wrt values of x.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Pair Class
static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if(this.x==o.x){
return (this.y-o.y);
}
return (this.x-o.x);
}
}
//Merge Sort
static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long [n1];
long R[] = new long [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
//Brian Kernighanβs Algorithm
static long countSetBits(long n){
if(n==0) return 0;
return 1+countSetBits(n&(n-1));
}
//Factorial
static long factorial(long n){
if(n==1) return 1;
if(n==2) return 2;
if(n==3) return 6;
return n*factorial(n-1);
}
//Euclidean Algorithm
static long gcd(long A,long B){
if(B==0) return A;
return gcd(B,A%B);
}
//Modular Exponentiation
static long fastExpo(long x,long n){
if(n==0) return 1;
if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD;
return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD;
}
//AKS Algorithm
static boolean isPrime(long n){
if(n<=1) return false;
if(n<=3) return true;
if(n%2==0 || n%3==0) return false;
for(int i=5;i*i<=n;i+=6)
if(n%i==0 || n%(i+2)==0) return false;
return true;
}
//Reverse an array
static <T> void reverse(T arr[],int l,int r){
Collections.reverse(Arrays.asList(arr).subList(l, r));
}
//Sieve of eratosthenes
static int[] findPrimes(int n){
boolean isPrime[]=new boolean[n+1];
ArrayList<Integer> a=new ArrayList<>();
int result[];
Arrays.fill(isPrime,true);
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i*i<=n;++i){
if(isPrime[i]==true){
for(int j=i*i;j<=n;j+=i) isPrime[j]=false;
}
}
for(int i=0;i<=n;i++) if(isPrime[i]==true) a.add(i);
result=new int[a.size()];
for(int i=0;i<a.size();i++) result[i]=a.get(i);
return result;
}
//Euler Totent function
static long countCoprimes(long n){
ArrayList<Long> prime_factors=new ArrayList<>();
long x=n,flag=0;
while(x%2==0){
if(flag==0) prime_factors.add(2L);
flag=1;
x/=2;
}
for(long i=3;i*i<=x;i+=2){
flag=0;
while(x%i==0){
if(flag==0) prime_factors.add(i);
flag=1;
x/=i;
}
}
if(x>2) prime_factors.add(x);
double ans=(double)n;
for(Long p:prime_factors){
ans*=(1.0-(Double)1.0/p);
}
return (long)ans;
}
public static int bSearch(int n,ArrayList<Integer> A){
int s = 0;
int e = A.size()-1;
while(s<=e){
int m = s+(e-s)/2;
if(A.get(m)==(long)n){
return A.get(m);
}
else if(A.get(m)>(long)n){
e = m-1;
}
else{
s = m+1;
}
}
return A.get(e);
}
static long modulo = (long)1e9+7;
public static long modinv(long x){
return modpow(x, modulo-2);
}
public static long modpow(long a, long b){
if(b==0){
return 1;
}
long x = modpow(a, b/2);
x = (x*x)%modulo;
if(b%2==1){
return (x*a)%modulo;
}
return x;
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
//it reads the data about the specified point and divide the data about it ,it is quite fast
//than using direct
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception r) {
r.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());//converts string to integer
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception r) {
r.printStackTrace();
}
return str;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
l=[]
for i in range(n):
l.append(list(map(int,input().split())))
x,y,z=[],[],[]
for i in range(n):
if(l[i][1]==1 and l[i][2]==1):
x.append(l[i][0])
elif(l[i][1]==1 and l[i][2]==0):
y.append(l[i][0])
elif(l[i][1]==0 and l[i][2]==1):
z.append(l[i][0])
y.sort()
z.sort()
for i in range(min(len(y),len(z))):
x.append(y[i]+z[i])
x.sort()
if(len(x)<k):
print(-1)
else:
print(sum(x[: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())
alice = [0]
bob = [0]
common = [0]
la = 1
lb = 1
lc = 1
for i in range(n):
t,a,b = map(int,input().split())
if a==1 and b==1:
common.append(t)
lc+=1
elif a==1:
alice.append(t)
la+=1
elif b==1:
bob.append(t)
lb+=1
alice.sort()
bob.sort()
common.sort()
alicecum = []
bobcum = []
commoncum = []
tot = 0
for num in alice:
tot+=num
alicecum.append(tot)
tot = 0
for num in bob:
tot+=num
bobcum.append(tot)
tot = 0
for num in common:
tot+=num
commoncum.append(tot)
if la+lc-2<k or lb+lc-2<k:
print(-1)
else:
res = 100000000000055
for i in range(k+1):
com = k-i
al, bo = i, i
if com<lc and al<la and bo<lb:
temp = commoncum[com]
temp += alicecum[al]
temp += bobcum[bo]
res = min(res,temp)
print(res)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
ba = []
bb = []
bz = []
for n_ in range(n):
t,a,b = map(int,input().split())
if a==1 and b==1:
bz.append(t)
if a==1 and b==0:
ba.append(t)
if a==0 and b==1:
bb.append(t)
if len(ba)+len(bz)<k or len(bb)+len(bz)<k:
print(-1)
else:
ba.sort()
bb.sort()
i=0
while len(ba)>i and len(bb)>i:
bz.append(ba[i]+bb[i])
i+=1
bz.sort()
print(sum(bz[: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())
t=[]
a=[]
b=[]
for i in range(n):
t1,a1,b1 = map(int, input().split())
if(a1==1 and b1==1):
t.append(t1)
elif(a1==1):
a.append(t1)
elif(b1==1):
b.append(t1)
a.sort()
b.sort()
k1=min(len(a),len(b))
for i in range(k1):
t.append(a[i]+b[i])
t.sort()
if(len(t)<k):
print("-1")
else:
print(sum(t[:k]))
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void readtxt() {}
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int32_t main() {
fast();
long long int n, k;
cin >> n >> k;
vector<long long int> a1b1, a1b0, a0b1;
long long int ans = 0, t, a, b;
for (long long int i = 0; i < n; i++) {
cin >> t >> a >> b;
if (a == 1 && b == 1)
a1b1.push_back(t);
else if (a == 1 && b == 0)
a1b0.push_back(t);
else if (a == 0 && b == 1)
a0b1.push_back(t);
}
sort(a1b1.begin(), a1b1.end());
sort(a1b0.begin(), a1b0.end());
sort(a0b1.begin(), a0b1.end());
auto it1 = a1b1.begin();
auto it2 = a1b0.begin();
auto it3 = a0b1.begin();
long long int flag = 1;
while (k--) {
if (it1 == a1b1.end() && (it2 == a1b0.end() || it3 == a0b1.end())) {
flag = 0;
cout << -1 << endl;
break;
} else if (it2 == a1b0.end() || it3 == a0b1.end()) {
ans += (*it1);
it1++;
} else if (it1 == a1b1.end()) {
ans += (*it2) + (*it3);
it2++;
it3++;
} else {
if ((*it1) <= (*it2) + (*it3)) {
ans += (*it1);
it1++;
} else {
ans += (*it2) + (*it3);
it2++;
it3++;
}
}
}
if (flag == 1) cout << ans << endl;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
public class CF_653_E {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int k = s.nextInt();
int arr[][] = new int[n][3];
PriorityQueue<Integer> p1 = new PriorityQueue<>();
PriorityQueue<Integer> p2 = new PriorityQueue<>();
int count = 0;
for(int i = 0;i<arr.length;++i)
{
arr[i][0] = s.nextInt();
arr[i][1] = s.nextInt();
arr[i][2] = s.nextInt();
if(arr[i][1] == 1 && arr[i][2] == 0)
{
p1.add(arr[i][0]);
}
else if(arr[i][1] == 0 && arr[i][2] == 1)
{
p2.add(arr[i][0]);
}
else if(arr[i][1] == 1 && arr[i][2] == 1)
{
count++;
}
}
long tmp1[] = new long[Math.min(p1.size(), p2.size())];
int tmp2[] = new int[count];
int w = 0;
for(int i = 0;i<arr.length;++i)
{
if(arr[i][1] == 1 && arr[i][2] == 1)
{
tmp2[w++] = arr[i][0];
}
}
Arrays.sort(tmp2);
int u = 0;
while(p1.size()>0 && p2.size()>0)
{
tmp1[u++]=(long)p1.poll()+p2.poll();
}
int i = 0,j = 0;
long ans = 0;
u=0;
// System.out.println(tmp1.length + " " + tmp2.length);
if(tmp1.length + tmp2.length < k)
{
System.out.println(-1);
return;
}
while(i<tmp1.length && j<tmp2.length && u<k)
{
if(tmp1[i] < tmp2[j])
{
ans+=(long)tmp1[i];
i++;
u++;
}
else
{
ans+=(long)tmp2[j];
j++;
u++;
}
}
while(i<tmp1.length && u<k)
{
ans+=(long)tmp1[i];
i++;
u++;
}
while(j<tmp2.length && u<k)
{
ans+=(long)tmp2[j];
j++;
u++;
}
System.out.println(ans);
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
(n,k1) = map(int,input().split())
l_a,l_b,l_comb = [],[],[]
for _ in range(n):
(ra,a,b) = map(int,input().split())
if a==1 and b==1:
l_comb.append(ra)
elif a==1:
l_a.append(ra)
elif b==1:
l_b.append(ra)
# print(l_comb,l_a,l_b)
if len(l_comb)+len(l_a) < k1 or len(l_comb)+len(l_b) < k1:
print(-1)
else:
l_comb.sort()
l_a.sort()
l_b.sort()
ans = 0
i,j,k = 0,0,0
for _ in range(k1):
if i<len(l_comb) and j<len(l_a) and k<len(l_b) and l_comb[i] < l_a[j]+l_b[k]:
ans+=l_comb[i]
i+=1
else:
if j<len(l_a) and k<len(l_b):
ans+=(l_a[j]+l_b[k])
j+=1
k+=1
else:
ans+=l_comb[i]
i+=1
# print(ans)
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 inf = (int)1e9;
long long A(long long x) {
if (x >= 0)
return x;
else
return -x;
}
long long gcd(long long a, long long b) {
if (b > a) {
long long tmp = b;
b = a;
a = tmp;
}
if (a % b == 0)
return b;
else
return gcd(b, a % b);
}
unsigned long long popcount(unsigned long long x) {
x = ((x & 0xaaaaaaaaaaaaaaaaUL) >> 1) + (x & 0x5555555555555555UL);
x = ((x & 0xccccccccccccccccUL) >> 2) + (x & 0x3333333333333333UL);
x = ((x & 0xf0f0f0f0f0f0f0f0UL) >> 4) + (x & 0x0f0f0f0f0f0f0f0fUL);
x = ((x & 0xff00ff00ff00ff00UL) >> 8) + (x & 0x00ff00ff00ff00ffUL);
x = ((x & 0xffff0000ffff0000UL) >> 16) + (x & 0x0000ffff0000ffffUL);
x = ((x & 0xffffffff00000000UL) >> 32) + (x & 0x00000000ffffffffUL);
return x;
}
int main(void) {
int T;
T = 1;
for (int query = 0; query < T; query++) {
int n, k;
cin >> n >> k;
priority_queue<long long, vector<long long>, greater<long long> > both, bob,
alice;
for (int i = 0; i < n; i++) {
long long t;
int a, b;
cin >> t >> a >> b;
if (a == 1) {
if (b == 0)
alice.push(t);
else
both.push(t);
} else {
if (b == 1) bob.push(t);
}
}
if (alice.size() + both.size() < k || bob.size() + both.size() < k) {
cout << -1 << endl;
} else {
long long ans = 0;
while (k > 0) {
if (both.size() == 0) {
while (k > 0) {
long long ali = alice.top();
long long bo = bob.top();
alice.pop();
bob.pop();
ans += ali + bo;
k--;
}
} else {
if (alice.size() == 0) {
while (k > 0) {
long long c = both.top();
both.pop();
ans += c;
k--;
}
break;
}
if (bob.size() == 0) {
while (k > 0) {
long long c = both.top();
both.pop();
ans += c;
k--;
}
break;
}
long long a = alice.top();
long long b = bob.top();
long long c = both.top();
if (c > a + b) {
ans += a + b;
k--;
bob.pop();
alice.pop();
} else {
ans += c;
k--;
both.pop();
}
}
}
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long int const mod = 1000000007;
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
int rand_rng(int l, int r) {
uniform_int_distribution<int> p(l, r);
return p(rng);
}
long long int power(long long int x, long long int y, long long int m) {
long long int temp;
if (y == 0) return 1;
temp = power(x, y / 2, m) % m;
if (y % 2 == 0)
return ((temp) * (temp)) % m;
else
return (((x) % m) * ((temp * temp) % m)) % m;
}
int const N = 200009;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
T = 1;
while (T--) {
long long int n, m, k, t, a, b;
cin >> n >> m >> k;
set<pair<long long int, long long int> > fre, st;
vector<pair<long long int, long long int> > aa, bb, ab, zz;
for (int i = 0; i < (int)n; i++) {
cin >> t >> a >> b;
if (a && b) {
ab.push_back({t, i + 1});
} else if (a) {
aa.push_back({t, i + 1});
} else if (b) {
bb.push_back({t, i + 1});
} else {
fre.insert({t, i + 1});
zz.push_back({t, i + 1});
}
}
sort(aa.begin(), aa.end());
;
sort(bb.begin(), bb.end());
;
sort(ab.begin(), ab.end());
;
sort(zz.begin(), zz.end());
;
int ind = -1;
long long int sum = 0;
long long int ans = 100000000000000000;
long long int tempsum = 0;
long long int out = 0;
for (int i = 0; i <= ab.size(); i++) {
long long int temp = k - i;
if (i > m) break;
if (i) {
sum += ab[i - 1].first;
}
if (aa.size() >= temp && bb.size() >= temp && i + 2 * temp <= m &&
n - (ab.size() - i) >= m) {
if (ind == -1) {
for (int j = 0; j < (int)temp; j++) sum += aa[j].first + bb[j].first;
for (int j = max(0ll, temp); j < aa.size(); j++) fre.insert(aa[j]);
for (int j = max(0ll, temp); j < bb.size(); j++) fre.insert(bb[j]);
ind = i;
temp = max(0ll, temp);
long long int dif = m - i - 2 * temp;
while (st.size() < dif && fre.size()) {
tempsum += (fre.begin())->first;
st.insert(*(fre.begin()));
fre.erase(*(fre.begin()));
}
ans = min(ans, sum + tempsum);
out = i;
} else {
if (temp >= 0) sum -= aa[temp].first + bb[temp].first;
if (temp >= 0) {
fre.insert(aa[temp]);
fre.insert(bb[temp]);
}
temp = max(0ll, temp);
long long int dif = m - i - 2 * temp;
while (st.size() && st.size() > dif) {
tempsum -= (st.rbegin())->first;
fre.insert(*(st.rbegin()));
st.erase(*(st.rbegin()));
}
while (st.size() < dif && fre.size()) {
tempsum += (fre.begin())->first;
st.insert(*(fre.begin()));
fre.erase(*(fre.begin()));
}
while (st.size() && fre.size() && *(st.rbegin()) > *(fre.begin())) {
st.insert(*(fre.begin()));
tempsum += (fre.begin())->first;
fre.erase(*(fre.begin()));
fre.insert(*(st.rbegin()));
tempsum -= (st.rbegin())->first;
st.erase(*(st.rbegin()));
}
if (sum + tempsum < ans) out = i;
ans = min(ans, sum + tempsum);
}
}
}
if (ind == -1) {
cout << "-1\n";
} else {
ans = 0;
vector<long long int> p;
long long int temp = k - out;
for (int i = 0; i < (int)out; i++) {
p.push_back(ab[i].second);
ans += ab[i].first;
}
for (int i = 0; i < (int)temp; i++) {
p.push_back(aa[i].second);
ans += aa[i].first;
}
for (int i = 0; i < (int)temp; i++) {
p.push_back(bb[i].second);
ans += bb[i].first;
}
fre.clear();
for (int x = out; x < ab.size(); x++) fre.insert(ab[x]);
for (auto i : zz) fre.insert(i);
temp = max(0ll, temp);
for (int i = temp; i < aa.size(); i++) fre.insert(aa[i]);
for (int i = temp; i < bb.size(); i++) fre.insert(bb[i]);
long long int dif = m - out - 2 * temp;
while (dif > 0) {
dif--;
p.push_back((fre.begin())->second);
ans += (fre.begin())->first;
fre.erase(*(fre.begin()));
}
cout << ans << "\n";
for (auto j : p) cout << j << " ";
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 1;
void updateSt(set<pair<int, int>> &st, set<pair<int, int>> &fr, int &sum,
int need) {
need = max(need, 0);
while (true) {
bool useful = false;
while (int((st).size()) > need) {
sum -= st.rbegin()->first;
fr.insert(*st.rbegin());
st.erase(prev(st.end()));
useful = true;
}
while (int((st).size()) < need && int((fr).size()) > 0) {
sum += fr.begin()->first;
st.insert(*fr.begin());
fr.erase(fr.begin());
useful = true;
}
while (!st.empty() && !fr.empty() &&
fr.begin()->first < st.rbegin()->first) {
sum -= st.rbegin()->first;
sum += fr.begin()->first;
fr.insert(*st.rbegin());
st.erase(prev(st.end()));
st.insert(*fr.begin());
fr.erase(fr.begin());
useful = true;
}
if (!useful) break;
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, 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, i});
}
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.first);
}
}
int ans = INF;
int pos = INF;
set<pair<int, int>> st;
set<pair<int, int>> fr;
int sum = 0;
vector<int> res;
for (int iter = 0; iter < 2; ++iter) {
st.clear();
fr.clear();
sum = 0;
int start = 0;
while (k - start >= int((sums[1]).size()) ||
k - start >= int((sums[2]).size()) ||
m - start - (k - start) * 2 < 0) {
++start;
}
if (start >= int((sums[3]).size())) {
cout << -1 << endl;
return 0;
}
int need = m - start - (k - start) * 2;
for (int i = 0; i < 3; ++i) {
for (int p = int((times[i]).size()) - 1; p >= (i == 0 ? 0 : k - start);
--p) {
fr.insert(times[i][p]);
}
}
updateSt(st, fr, sum, need);
for (int cnt = start; cnt < (iter == 0 ? int((sums[3]).size()) : pos);
++cnt) {
if (k - cnt >= 0) {
if (cnt + (k - cnt) * 2 + int((st).size()) == m) {
if (ans > sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum) {
ans = sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum;
pos = cnt + 1;
}
}
} else {
if (cnt + int((st).size()) == m) {
if (ans > sums[3][cnt] + sum) {
ans = sums[3][cnt] + sum;
pos = cnt + 1;
}
}
}
if (iter == 1 && cnt + 1 == pos) break;
need -= 1;
if (k - cnt > 0) {
need += 2;
fr.insert(times[1][k - cnt - 1]);
fr.insert(times[2][k - cnt - 1]);
}
updateSt(st, fr, sum, need);
}
if (iter == 1) {
for (int i = 0; i + 1 < pos; ++i) res.push_back(times[3][i].second);
for (int i = 0; i <= k - pos; ++i) {
res.push_back(times[1][i].second);
res.push_back(times[2][i].second);
}
for (auto [value, position] : st) res.push_back(position);
}
}
cout << ans << endl;
for (auto it : res) cout << it + 1 << " ";
cout << endl;
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const unsigned long long int INF = numeric_limits<int>::max();
long long int k, n, A, B, T;
unsigned long long int ab[200005], a[200005], b[200005];
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
;
cin >> n >> k;
int I = 0, J = 0, K = 0;
for (int i = 0; i < n; i++) {
cin >> T >> A >> B;
if (A == 1 && B == 1)
ab[I] = T, I++;
else if (A == 1)
a[J] = T, J++;
else if (B == 1)
b[K] = T, K++;
}
sort(ab, ab + I);
sort(b, b + K);
sort(a, a + J);
if (J + I < k || K + I < k) {
cout << -1 << "\n";
return 0;
}
if (I == 0) ab[0] = INF;
if (J == 0) a[0] = INF;
if (K == 0) b[0] = INF;
for (int i = 1; i < n; i++) {
if (i < J)
a[i] += a[i - 1];
else
a[i] = INF;
if (i < K)
b[i] += b[i - 1];
else
b[i] = INF;
if (i < I)
ab[i] += ab[i - 1];
else
ab[i] = INF;
}
unsigned long long int ans = min(ab[k - 1], a[k - 1] + b[k - 1]);
for (int i = 0; i < (k - 1); i++) {
ans = min(ans, ab[k - i - 2] + a[i] + b[i]);
}
cout << ans << "\n";
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = map(int, input().split())
alice, bob = [], []
both = []
for _ in range(n):
x = list(map(int, input().split()))
if x[2] == 1 and x[1] == 1:
both.append(x[0])
elif x[1]:
alice.append(x)
elif x[2]:
bob.append(x)
alice.sort(key=lambda x: x[0])
bob.sort(key=lambda x: x[0])
both.sort()
tgt = []
for i in range(min(len(alice), len(bob))):
tgt.append(alice[i][0]+bob[i][0])
p1, p2 = 0, 0
count = 0
ans = 0
if len(tgt) + len(both) < k:
print(-1)
else:
while count < k:
if p2 == len(both) or (p1 < len(tgt) and tgt[p1] <= both[p2]):
ans += tgt[p1]
p1 += 1
else:
ans += both[p2]
p2 += 1
count += 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.util.*;
import java.io.*;
import java.math.*;
public class E1 {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
Reader s = new Reader();
int n = s.i() , x = s.i();
ArrayList<Integer> both = new ArrayList<>();
ArrayList<Integer> alice = new ArrayList<>();
ArrayList<Integer> bob = new ArrayList<>();
for (int i=0;i<n;i++) {
int t = s.i() , a= s.i() , b = s.i();
if (a==1 && b == 1) both.add(t);
else if (a == 1) alice.add(t);
else if (b == 1) bob.add(t);
}
Collections.sort(bob);
Collections.sort(alice);
Collections.sort(both);
int i = 0 , j = 0 , k = 0;
int count1 = 0 , count2 = 0;
int ans = 0;
while (i < both.size() && j < bob.size() && k < alice.size()) {
if (count1 == x && count2 == x) break;
int a = both.get(i);
int b = bob.get(j);
int c = alice.get(k);
if (b+c < a) {
ans += b+c;
count1++;
count2++;
j++;
k++;
} else {
ans += a;
count1++;
count2++;
i++;
}
}
if (count1 == x && count2 == x) {
out.println(ans);
out.flush();
return;
}
if (i == both.size()) {
while (j < bob.size() && k < alice.size()) {
if (count1 == x && count2 == x) break;
ans += bob.get(j);
ans += alice.get(k);
j++;
k++;
count1++;
count2++;
}
if (count1 != x || count2 != x) {
out.println(-1);
out.flush();
return;
}
out.println(ans);
} else {
while (i < both.size()) {
if (count1 == x && count2 == x) break;
ans += both.get(i);
i++;
count1++;
count2++;
}
if (count1 != x || count2 != x) {
out.println(-1);
out.flush();
return;
}
out.println(ans);
}
out.flush();
}
private static int power(int a, int n) {
int result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a);
n /= 2;
} else {
result = (result * a);
n--;
}
}
return result;
}
private static int power(int a, int n, int p) {
int result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a) % p;
n /= 2;
} else {
result = (result * a) % p;
n--;
}
}
return result;
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String s() {
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 long l() {
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 int i() {
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 double d() throws IOException {
return Double.parseDouble(s());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int[] arr(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = i();
}
return ret;
}
public long[] arrLong(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = l();
}
return ret;
}
}
// static class pairLong implements Comparator<pairLong> {
// long first, second;
//
// pairLong() {
// }
//
// pairLong(long first, long second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pairLong p1, pairLong p2) {
// if (p1.first == p2.first) {
// if(p1.second > p2.second) return 1;
// else return -1;
// }
// if(p1.first > p2.first) return 1;
// else return -1;
// }
// }
// static class pair implements Comparator<pair> {
// int first, second;
//
// pair() {
// }
//
// pair(int first, int second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pair p1, pair p2) {
// if (p1.first == p2.first) return p1.second - p2.second;
// return p1.first - p2.first;
// }
// }
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"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(k) for k in input().split()]
tog=[]
ali=[]
bli=[]
time=0
size=0
for i in range(n):
t,a,b=[int(k) for k in input().split()]
if a&b==1:
tog.append(t)
else:
if a==1:
ali.append(t)
if b==1:
bli.append(t)
asi=len(tog)+len(ali)
bsi=len(tog)+len(bli)
if k>min(asi,bsi):
print(-1)
else:
ali.sort()
bli.sort()
for i in range(min(len(ali),len(bli))):
tog.append(ali[i]+bli[i])
tog.sort()
for i in range(k):
time+=tog[i]
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
|
java
|
/**
* ******* Created on 28/6/20 7:53 PM*******
*/
import java.io.*;
import java.util.*;
public class E1374 implements Runnable {
private static final int MAX = (int) (1E5 + 5);
private static final int MOD = (int) (1E9 + 7);
private static final long Inf = (long) (1E14 + 10);
private static final double eps = (double) (1E-9);
private void solve() throws IOException {
int t = 1;
while (t-- > 0) {
int n = reader.nextInt();
int k = reader.nextInt();
List<Integer> al = new ArrayList<>();
List<Integer> bob = new ArrayList<>();
List<Integer> both = new ArrayList<>();
for(int i=0;i<n;i++){
int a = reader.nextInt();
int b = reader.nextInt();
int c = reader.nextInt();
if(b ==1 && c==1){
both.add(a);
}
else if(b==1){
al.add(a);
}else if(c==1)
bob.add(a);
}
Collections.sort(al);
Collections.sort(bob);
Collections.sort(both);
int pos1 =0,pos2 =0,pos3 =0;
long sum =0;
for(int i=0;i<k;i++){
if(pos1 < al.size() && pos2 < bob.size() &&
(pos3 >= both.size() || (pos3 < both.size() && al.get(pos1) + bob.get(pos2) < both.get(pos3)) ) ){
sum += (long) (al.get(pos1) + bob.get(pos2) );
pos1++;
pos2++;
}else if(pos3 < both.size()){
sum += (long)both.get(pos3);
pos3++;
}
}
if(pos1 + pos3 >=k && pos2 + pos3 >=k)
writer.println(sum);
else
writer.println("-1");
}
}
public static void main(String[] args) throws IOException {
try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
new E1374().run();
}
}
StandardInput reader;
PrintWriter writer;
@Override
public void run() {
try {
reader = new StandardInput();
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
interface Input extends Closeable {
String next() throws IOException;
String nextLine() throws IOException;
default int nextInt() throws IOException {
return Integer.parseInt(next());
}
default long nextLong() throws IOException {
return Long.parseLong(next());
}
default double nextDouble() throws IOException {
return Double.parseDouble(next());
}
default int[] readIntArray() throws IOException {
return readIntArray(nextInt());
}
default int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextInt();
}
return array;
}
default long[] readLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextLong();
}
return array;
}
}
private static class StandardInput implements Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer stringTokenizer;
@Override
public void close() throws IOException {
reader.close();
}
@Override
public String next() throws IOException {
if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
@Override
public String nextLine() throws IOException {
return reader.readLine();
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
String s[]=bu.readLine().split(" ");
int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]);
ArrayList<Integer> ab=new ArrayList<>();
ArrayList<Integer> a=new ArrayList<>();
ArrayList<Integer> b=new ArrayList<>();
int i,al=0,bo=0,x,y,z;
for(i=0;i<n;i++)
{
s=bu.readLine().split(" ");
x=Integer.parseInt(s[0]); y=Integer.parseInt(s[1]); z=Integer.parseInt(s[2]);
if(y==1) al++;
if(z==1) bo++;
if(y==1 && z==1) {ab.add(x); continue;}
if(y==1) a.add(x);
if(z==1) b.add(x);
}
if(al<k || bo<k) {System.out.print("-1"); return;}
Collections.sort(a); Collections.sort(b);
for(i=0;i<Math.min(a.size(),b.size());i++)
ab.add(a.get(i)+b.get(i));
Collections.sort(ab);
int min=0;
for(i=0;i<k;i++)
min+=ab.get(i);
System.out.print(min);
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=list(map(int,input().rstrip().split()))
t=[]
ta=[]
tb=[]
common=0
cm={}
for i in range(n):
a=list(map(int,input().rstrip().split()))
if a[1]==1 & a[2]==1:
t.append(a[0])
else:
if a[1]==1:
ta.append(a[0])
if a[2]==1:
tb.append(a[0])
if len(ta)<k-len(t) or len(tb)<k-len(t):
print(-1)
else:
ta.sort()
tb.sort()
t.sort()
s=[]
for i in range(min(len(ta),len(tb))):
s.append(ta[i]+tb[i])
l=t[:k]+s[:k]
l.sort()
print(sum(l[: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 = [],[],[]
for _ in range(n):
t,x,y = map(int, input().split())
if x + y == 2:
ab.append(t)
elif x == 1:
a.append(t)
elif y == 1:
b.append(t)
a.sort()
b.sort()
for i in range(min(len(a),len(b))):
ab.append(a[i]+b[i])
ab.sort()
if len(ab) < k:
print(-1)
else:
sum = 0
for i in range(k):
sum += ab[i]
print(sum)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
greater<pair<int64_t, int64_t>>>
a, b, c, d;
priority_queue<pair<int64_t, int64_t>> both;
vector<int64_t> ans;
vector<pair<int64_t, pair<int64_t, int64_t>>> prblm;
map<int64_t, bool> ok;
int64_t n, m, k, t;
void print() {
cout << t << '\n';
unordered_set<int64_t> s;
for (auto j : ans)
if (!ok[j]) s.insert(j + 1);
for (auto j : s) cout << j << ' ';
}
void m_greater() {
int64_t x, y, z, w, u, cnt;
cnt = ans.size();
while (cnt < m) {
if (a.size())
x = a.top().first;
else
x = LLONG_MAX;
if (b.size())
y = b.top().first;
else
y = LLONG_MAX;
if (c.size())
z = c.top().first;
else
z = LLONG_MAX;
if (d.size())
w = d.top().first;
else
w = LLONG_MAX;
if (both.size())
u = both.top().first;
else
u = LLONG_MAX;
if (min({x, y, z, w}) == LLONG_MAX) {
cout << "-1" << '\n';
return;
}
if (a.size() and b.size() and both.size() and
x + y <= u + min({x, y, z, w})) {
int64_t val = a.top().second;
t -= u;
t += x + y;
ans.push_back((val));
ans.push_back(b.top().second);
int64_t q = both.top().second;
ok[q] = true;
a.pop();
b.pop();
c.push(both.top());
both.pop();
} else if (min({x, y, z, w}) == x) {
t += x;
ans.push_back(a.top().second);
a.pop();
} else if (min({x, y, z, w}) == y) {
t += y;
ans.push_back(b.top().second);
b.pop();
} else if (min({x, y, z, w}) == w) {
t += w;
ans.push_back(d.top().second);
d.pop();
} else if (min({x, y, z, w}) == z) {
t += z;
ans.push_back(c.top().second);
if (ok[ans.back()]) ok[ans.back()] = false;
c.pop();
}
cnt++;
}
print();
}
void m_less() {
int64_t x, y, cnt;
cnt = 0;
sort(prblm.begin(), prblm.end());
cnt = ans.size();
while (cnt > m) {
if (c.size() == 0 or prblm.size() == 0) break;
int64_t val = prblm.back().first;
x = prblm.back().second.first;
y = prblm.back().second.second;
prblm.pop_back();
ok[x] = true;
ok[y] = true;
t -= val;
t += c.top().first;
ans.push_back(c.top().second);
c.pop();
cnt--;
}
if (cnt > m) {
cout << "-1";
return;
}
print();
}
void solve() {
int64_t i, x, y, z, cnt;
cin >> n >> m >> k;
for (i = 0; i < n; i++) {
cin >> t >> x >> y;
if (x == y and x == 1)
c.push({t, i});
else if (x == 1 and y == 0)
a.push({t, i});
else if (x == 0 and y == 1)
b.push({t, i});
else
d.push({t, i});
}
t = 0;
cnt = 0;
while (1) {
if (cnt >= k) break;
if (c.size() == 0 and (a.size() == 0 or b.size() == 0)) break;
if (a.size())
x = a.top().first;
else
x = LLONG_MAX;
if (b.size())
y = b.top().first;
else
y = LLONG_MAX;
if (c.size())
z = c.top().first;
else
z = LLONG_MAX;
if (a.size() > 0 and b.size() > 0 and x + y <= z) {
t += x + y;
ans.push_back(a.top().second);
ans.push_back(b.top().second);
prblm.push_back({x + y, {a.top().second, b.top().second}});
a.pop();
b.pop();
cnt++;
} else {
t += z;
ans.push_back(c.top().second);
both.push({z, c.top().second});
c.pop();
cnt++;
}
}
if (cnt < k) {
cout << "-1" << '\n';
return;
}
if (ans.size() > m) {
m_less();
return;
} else {
m_greater();
return;
}
}
signed main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
{ solve(); }
cerr << " Execution : " << (1.0 * clock()) / CLOCKS_PER_SEC << "s \n";
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from sys import stdin
n,k=map(int,input().split())
x=[]
y=[]
z=[]
k1=0
k2=0
for i in range(n):
t,a,b=map(int,stdin.readline().split())
if(a==1 and b==1):
x.append(t)
k1+=1
k2+=1
else:
if(a==1):
k1+=1
y.append(t)
elif(b==1):
k2+=1
z.append(t)
if(k1<k or k2<k):
print(-1)
else:
x.sort()
y.sort()
z.sort()
ans=0
k1=0
k2=0
p1=0
p2=0
p3=0
lx=len(x)
ly=len(y)
lz=len(z)
for i in range(k):
if(p1>=lx):
ans+=y[p2]+z[p3]
p2+=1
p3+=1
elif(p2>=ly or p3>=lz):
ans+=x[p1]
p1+=1
else:
if(x[p1]<=y[p2]+z[p3]):
ans+=x[p1]
p1+=1
else:
ans+=y[p2]+z[p3]
p2+=1
p3+=1
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
n, k = map(int, input().split())
x, y, z = [], [], []
for _ in range(n):
t, a, b = map(int, input().split())
if a & b:
z.append(t)
elif a:
x.append(t)
elif b:
y.append(t)
x.sort()
y.sort()
for i in range(min(len(x), len(y))):
z.append(x[i] + y[i])
if len(z) < k:
print(-1)
else:
print(sum(sorted(z)[:k]))
# 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 heapq
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)
heapq.heapify(both)
heapq.heapify(alice)
heapq.heapify(bob)
at,bt,count=0,0,0
while len(both)>0 and len(alice)>0 and len(bob)>0 and at<k and bt<k:
x=heapq.heappop(both)
y=heapq.heappop(alice)
z=heapq.heappop(bob)
if x<(y+z):
at+=1
bt+=1
count+=x
heapq.heappush(alice,y)
heapq.heappush(bob,z)
else:
at+=1
bt+=1
count+=(y+z)
heapq.heappush(both,x)
if len(both)>0 and (len(alice)==0 or len(bob)==0) and at<k and bt<k:
while len(both)>0 and at<k and bt<k:
x=heapq.heappop(both)
at+=1
bt+=1
count+=x
elif len(both)==0 and len(alice)>0 and len(bob)>0 and at<k and bt<k:
while len(alice)>0 and len(bob)>0 and at<k and bt<k:
x=heapq.heappop(alice)
y=heapq.heappop(bob)
at+=1
bt+=1
count+=(x+y)
if at!=k or bt!=k:
print(-1)
else:
print(count)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = [int(i) for i in input().split()]
books = [[int(i) for i in input().split()] for i in range(n)]
#b00 = []
b01 = []
b10 = []
b11 = []
b00c, b01c, b10c, b11c = 0,0,0,0
index = 0
for book in books:
if book[1] == 1:
if book[2] == 1:
b11.append(book[0])
else:
b10.append(book[0])
else:
if book[2] == 1:
b01.append(book[0])
b11.sort(), b10.sort(), b01.sort()
b0_index = 0
b1_index = 0
time = 0
if len(b11) > 0:
b1Out = False
else:
b1Out = True
if len(b01) > 0 and len(b10) > 0:
b0Out = False
else:
b0Out = True
while b0_index + b1_index < k:
if not any([b1Out, b0Out]):
if b11[b1_index] < b01[b0_index] + b10[b0_index]:
time += b11[b1_index]
b1_index += 1
if b1_index > len(b11) - 1:
b1Out = True
else:
time += b01[b0_index] + b10[b0_index]
b0_index += 1
if b0_index > len(b01) - 1 or b0_index > len(b10) - 1:
b0Out = True
else:
if b1Out and b0Out:
time = -1
break
else:
if b1Out:
time += b01[b0_index] + b10[b0_index]
b0_index += 1
if b0_index == len(b01) or b0_index == len(b10):
b0Out = True
else:
time += b11[b1_index]
b1_index += 1
if b1_index == len(b11):
b1Out = True
print(time)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import *
sys.setrecursionlimit(99999)
n, k = map(int, input().split())
booksa, booksb, booksc = [], [], []
for _ in range(n):
t, a, b = map(int, input().split())
if a == 0 and b == 1:
booksb.append(t)
if a == 1 and b == 0:
booksa.append(t)
if a == 1 and b == 1:
booksc.append(t)
booksa.sort()
booksb.sort()
booksc.sort()
booksa = booksa[:k]
booksb = booksb[:k]
na = len(booksa)
nb = len(booksb)
s = sum(booksa) + sum(booksb)
cj = 0
while na < k or nb < k:
if cj == len(booksc):
break
s += booksc[cj]
cj += 1
na += 1
nb += 1
while na > k and booksa:
s -= booksa.pop()
na -= 1
while nb > k and booksb:
s -= booksb.pop()
nb -= 1
while cj < len(booksc) and (booksa or booksb) and na >= k and nb >= k:
sc = booksc[cj]
cj += 1
sa = 0
sb = 0
if booksa:
sa = booksa.pop()
if booksb:
sb = booksb.pop()
if sa + sb > sc:
s += sc
s -= sa
s -= sb
else:
break
if na == k and nb == k:
print(s)
else:
print(-1)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from collections import deque
n, k = list(map(int, input().split()))
both_ls = []
a_ls = []
b_ls = []
for i in range(n):
t, a, b = list(map(int, input().split()))
if a == 1 and b == 0:
a_ls.append(t)
elif a == 0 and b == 1:
b_ls.append(t)
elif a == 1 and b == 1:
both_ls.append(t)
a_ls = sorted(a_ls)
b_ls = sorted(b_ls)
both_ls = sorted(both_ls)
a_ls = deque(a_ls)
b_ls = deque(b_ls)
both_ls = deque(both_ls)
res = 0
broke = False
for i in range(k):
if len(both_ls)>0 and len(a_ls)>0 and len(b_ls)>0:
if both_ls[0] >= (a_ls[0] + b_ls[0]):
res += a_ls.popleft()
res += b_ls.popleft()
else:
res += both_ls.popleft()
elif len(both_ls)>0:
res += both_ls.popleft()
elif len(a_ls)>0 and len(b_ls)>0:
res += a_ls.popleft()
res += b_ls.popleft()
else:
print(-1)
broke = True
break
if not broke:
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;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
template <class T>
inline T bigMod(T p, T e, T M) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T modInverse(T a, T M) {
return bigMod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / gcd(a, b)) * b;
}
template <class T>
inline string int2String(T a) {
ostringstream str;
str << a;
return str.str();
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<vector<int> > v(4);
int t, a, b;
for (int i = int(0); i < int(n); i++) {
scanf("%d%d%d", &t, &a, &b);
v[(a << 1) | b].push_back(t);
}
if (v[1].size() + v[3].size() < k || v[2].size() + v[3].size() < k) {
puts("-1");
return 0;
}
for (int i = int(0); i < int(4); i++) sort(v[i].rbegin(), v[i].rend());
long long ans = 0;
for (int _ = int(0); _ < int(k); _++) {
if (v[3].size() && v[1].size() && v[2].size()) {
if (v[3].back() <= v[1].back() + v[2].back()) {
ans += v[3].back();
v[3].pop_back();
} else {
ans += v[1].back();
v[1].pop_back();
ans += v[2].back();
v[2].pop_back();
}
} else if (v[3].size()) {
ans += v[3].back();
v[3].pop_back();
} else {
ans += v[1].back();
v[1].pop_back();
ans += v[2].back();
v[2].pop_back();
}
}
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.*;
import java.io.*;
public class ZeroRem {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
ArrayList <Integer> a=new ArrayList();
ArrayList <Integer> b=new ArrayList();
ArrayList <Integer> c=new ArrayList();
for(int i=0;i<n;i++)
{
int t=sc.nextInt();
int x=sc.nextInt();
int y=sc.nextInt();
if(x==1 && y==1)
a.add(t);
else if(x==1 && y==0)
b.add(t);
else if(x==0 && y==1)
c.add(t);
}
Collections.sort(b);
Collections.sort(c);
int j=0;
while(j<b.size() && j<c.size())
{
a.add(b.get(j)+c.get(j));
j++;
}
Collections.sort(a);
if(a.size()<k)
System.out.println(-1);
else
{
int sum=0;
for(int i=0;i<k;i++)
sum+=a.get(i);
System.out.println(sum);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import os
import sys
from io import BytesIO, IOBase
def main():
n,k=map(int,input().split())
alice=[]
bob=[]
both=[]
for _ 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)
if len(alice)+len(both)<k or len(bob)+len(both)<k:
print(-1)
else:
alice.sort()
bob.sort()
both.sort()
i=j=0
ans=0
while i<len(alice) and i<len(bob) and j<len(both) and k>0:
if(alice[i]+bob[i]<both[j]):
ans+=alice[i]+bob[i]
i+=1
else:
ans+=both[j]
j+=1
k-=1
if(k):
if(j>=len(both)):
while k>0:
ans+=alice[i]+bob[i]
i+=1
k-=1
else:
while k>0:
ans+=both[j]
j+=1
k-=1
print(ans)
# 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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int test = 1;
while (test--) {
int m, k, i;
cin >> m >> k;
int arr[3][m];
for (i = 0; i < m; i++) cin >> arr[0][i] >> arr[1][i] >> arr[2][i];
vector<int> a;
vector<int> b;
vector<int> c;
for (i = 0; i < m; i++) {
if (arr[1][i] == 1 && arr[2][i] == 1)
c.push_back(arr[0][i]);
else if (arr[1][i] == 1 && arr[2][i] == 0)
a.push_back(arr[0][i]);
else if (arr[1][i] == 0 && arr[2][i] == 1)
b.push_back(arr[0][i]);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
int idx_a = 0, idx_b = 0, idx_c = 0, total = 0, ans = 0;
while (total < k) {
if (idx_a == a.size() || idx_b == b.size()) {
for (; idx_c < c.size(); idx_c++) {
if (total == k) break;
total++;
ans += c[idx_c];
}
break;
} else if (idx_c == c.size()) {
for (; idx_a < a.size() && idx_b < b.size(); idx_a++) {
if (total == k) break;
total++;
ans += a[idx_a] + b[idx_b];
idx_b++;
}
break;
} else {
if (a[idx_a] + b[idx_b] <= c[idx_c]) {
ans += a[idx_a] + b[idx_b];
total++;
idx_a++;
idx_b++;
} else {
ans += c[idx_c];
idx_c++;
total++;
}
}
}
if (total < k)
cout << "-1";
else
cout << ans;
cout << "\n";
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
//String str = sc.nextInt();
int []arr = new int[n];
List<Integer> a1 = new ArrayList<>();
List<Integer> b1 = new ArrayList<>();
List<Integer> common = new ArrayList<>();
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
if(a==1 && b==0) a1.add(arr[i]);
else if(b==1 && a==0) b1.add(arr[i]);
else if(a==1 && b==1) common.add(arr[i]);
}
Collections.sort(a1);
Collections.sort(b1);
for(int i=0; i<Math.min(a1.size(), b1.size()); i++)
{
common.add(a1.get(i) + b1.get(i));
}
Collections.sort(common);
long ans=0;
if(common.size() < k){
System.out.println(-1);
}
else{
for(int i=0;i<k;i++)
ans += common.get(i);
System.out.println(ans);
}
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e){e.printStackTrace();}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {st = new StringTokenizer(br.readLine());}
catch (Exception e) {e.printStackTrace();}
return st.nextToken();
}
public int nextInt() {return Integer.parseInt(next());}
public long nextLong() {return Long.parseLong(next());}
public double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {
String line = "";
if(st.hasMoreTokens()) line = st.nextToken();
else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
while(st.hasMoreTokens()) line += " "+st.nextToken();
return line;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = map(int, input().split())
l1 = []
l2 = []
l3 = []
n1 = n2 = n3 = 0
for i in range (n):
a, b, c = map(int, input().split())
if b == c == 1:
l1.append(a)
n1 += 1
elif b == 1:
l2.append(a)
n2 += 1
elif c == 1:
l3.append(a)
n3 += 1
if n1 + n2 < k or n1 + n3 < k:
print(-1)
else:
l1.sort()
l2.sort()
l3.sort()
s1 = s2 = s = 0
p = min(n2, n3)
z1 = []
for i in range (p):
s1 += l2[i]
s1 += l3[i]
z1.append(s1)
s1 = 0
z2 = z1 + l1
z2.sort()
if len(z2) < k:
print(-1)
else:
for i in range (k):
s += z2[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
|
#!/usr/bin/env python
import os
import operator
from collections import defaultdict
import sys
from io import BytesIO, IOBase
import bisect
# def power(x, p):
# res = 1
# while p:
# if p & 1:
# res = res * x % 1000000007
# x = x * x % 1000000007
# p >>= 1
# return res;
def main():
n,el=map(int,input().split())
oo=[]
zo=[]
oz=[]
for i in range(n):
t,a,b=map(int,input().split())
if a==b==1:
oo.append(t)
elif a==1 and b==0:
oz.append(t)
elif a==0 and b==1:
zo.append(t)
oo.sort()
zo.sort()
oz.sort()
alice=0
bob=0
i=0
j=0
k=0
ans=0
while True:
if alice>=el and bob>=el:
break
if i < len(oo) and j < len(oz) and k < len(zo):
if oo[i] < (oz[j] + zo[k]):
ans += oo[i]
alice += 1
bob += 1
i += 1
else:
ans += oz[j] + zo[k]
alice += 1
bob += 1
j += 1
k += 1
elif i < len(oo):
ans += oo[i]
alice += 1
bob += 1
i += 1
elif j < len(oz) and k < len(zo):
ans += oz[j] + zo[k]
alice += 1
bob += 1
j += 1
k += 1
else:
break
if alice >= el and bob >= el:
print(ans)
else:
print(-1)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python2
|
import sys
line = sys.stdin.readline().strip()
n,k = map(int, line.split(" "))
l = []
for i in range(0,n):
line = sys.stdin.readline().strip()
t,x,y = map(int, line.split(" "))
l.append([t,x,y])
l.sort()
a = []
anum = 0
b = []
bnum = 0
sumt = 0
ans = -1
for i in range(0,n):
t,x,y = l[i]
if x == 0 and y == 0:
continue
if x == 1 and anum >= k and y == 0:
continue
if y == 1 and bnum >= k and x == 0:
continue
if x == 1:
anum += 1
if y == 0:
a.append(t)
if y == 1:
bnum += 1
if x == 0:
b.append(t)
sumt += t
if x == 1 and y == 1:
if anum > k and len(a) > 0:
tt = a.pop()
sumt -= tt
anum -= 1
if bnum > k and len(b) > 0:
tt = b.pop()
sumt -= tt
bnum -= 1
if anum >= k and bnum >= k:
if ans == -1 or ans > sumt:
ans = sumt
#if x == 1 and y == 1:
# break
#print t,x,y,sumt,ans
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())
a = []
b = []
ab = []
for _ in range(n):
t,x,y = map(int,input().split())
if x == 1 and y == 1:
ab.append(t)
elif x == 1 and y == 0:
a.append(t)
elif x == 0 and y == 1:
b.append(t)
if len(ab) + len(a) < k or len(ab) + len(b) < k:
print(-1)
else:
a.sort()
a.append(float('inf'))
b.sort()
b.append(float('inf'))
ab.sort()
ab.append(float('inf'))
tm = 0
ia = 0
ib = 0
iab = 0
books = 0
while books < k:
if ab[iab] <= a[ia] + b[ib]:
tm += ab[iab]
iab+= 1
else:
tm += a[ia] + b[ib]
ia += 1
ib += 1
books += 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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e3 + 23;
const int maxn = 2e5 + 35;
int n, k, t[maxn], vis[maxn], a, b, num, m;
vector<int> ve[4], ee;
vector<pair<int, int> > we;
bool cmp(int i, int j) { return t[i] < t[j]; }
struct node {
int ii, tt;
friend bool operator<(node a, node b) { return a.tt > b.tt; }
};
int main() {
scanf("%d %d %d", &n, &m, &k);
priority_queue<node> se;
for (int i = 1; i <= n; ++i) {
scanf("%d %d %d", t + i, &a, &b);
if (a && b)
ve[2].push_back(i);
else if (a)
ve[0].push_back(i);
else if (b)
ve[1].push_back(i);
else
se.push((node){i, t[i]});
}
sort((ve[0]).begin(), (ve[0]).end(), cmp);
sort((ve[1]).begin(), (ve[1]).end(), cmp);
sort((ve[2]).begin(), (ve[2]).end(), cmp);
int x0 = 0, x1 = 0, x2 = 0, ans = 0;
set<int> s;
while (k) {
if (x1 < (int)(ve[1]).size() && x0 < (int)(ve[0]).size()) {
if (x2 < (int)(ve[2]).size()) {
if (t[ve[2][x2]] < t[ve[0][x0]] + t[ve[1][x1]]) {
ans += t[ve[2][x2]];
s.insert(ve[2][x2]);
ee.push_back(ve[2][x2]);
k -= 1;
x2 += 1;
num += 1;
} else {
ans += t[ve[0][x0]] + t[ve[1][x1]];
s.insert(ve[0][x0]);
s.insert(ve[1][x1]);
we.push_back(pair<int, int>(ve[0][x0], ve[1][x1]));
k -= 1;
x0 += 1;
x1 += 1;
num += 2;
}
} else {
ans += t[ve[0][x0]] + t[ve[1][x1]];
we.push_back(pair<int, int>(ve[0][x0], ve[1][x1]));
s.insert(ve[0][x0]);
s.insert(ve[1][x1]);
k -= 1;
x0 += 1;
x1 += 1;
num += 2;
}
} else if (x2 < (int)(ve[2]).size()) {
ans += t[ve[2][x2]];
s.insert(ve[2][x2]);
ee.push_back(ve[2][x2]);
k -= 1;
x2 += 1;
num += 1;
} else
break;
}
if (k > 0) return puts("-1"), 0;
if (num < m) {
for (int i = x0; i < (int)(ve[0]).size(); ++i)
se.push((node){ve[0][i], t[ve[0][i]]});
for (int i = x1; i < (int)(ve[1]).size(); ++i)
se.push((node){ve[1][i], t[ve[1][i]]});
for (int i = x2; i < (int)(ve[2]).size(); ++i)
se.push((node){ve[2][i], t[ve[2][i]]});
int first = (int)(ee).size() - 1;
while (num < m && x0 < (int)(ve[0]).size() && x1 < (int)(ve[1]).size() &&
first >= 0 && (int)(se).size() > 0) {
while (vis[se.top().ii]) se.pop();
if ((int)(se).size() == 0) break;
if (t[ee[first]] + (se.top()).tt < t[ve[0][x0]] + t[ve[1][x1]]) {
ans += (se.top()).tt;
vis[(se.top()).ii] = 1;
s.insert((se.top()).ii);
se.pop();
num += 1;
} else {
ans += t[ve[0][x0]] + t[ve[1][x1]];
ans -= t[ee[first]];
vis[ve[0][x0]] = 1;
vis[ve[1][x1]] = 1;
se.push((node){ee[first], t[ee[first]]});
s.insert(ve[0][x0]);
s.insert(ve[1][x1]);
s.erase(ee[first]);
first -= 1;
x0 += 1;
x1 += 1;
num += 1;
}
while (vis[ve[0][x0]]) x0 += 1;
while (vis[ve[1][x1]]) x1 += 1;
}
while (num < m && !se.empty()) {
ans += se.top().tt;
s.insert(se.top().ii);
se.pop();
num += 1;
}
} else {
int first = (int)(we).size() - 1;
while (num > m && x2 < (int)(ve[2]).size()) {
s.erase(we[first].first);
s.erase(we[first].second);
ans = ans - t[we[first].first] - t[we[first].second];
first -= 1;
s.insert(ve[2][x2]);
ans += t[ve[2][x2]];
x2 += 1;
num -= 1;
}
}
if ((int)(s).size() == m) {
printf("%d\n", ans);
for (set<int>::iterator it = s.begin(); it != s.end(); ++it)
printf("%d ", *it);
} else
puts("-1");
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k=map(int,input().split())
l=[]
al=[]
bl=[]
ac=bc=0
for i in range(n):
t,a,b=map(int,input().split())
if a==1 and b==1:
l.append(t)
ac+=1
bc+=1
elif a==1:
al.append(t)
ac+=1
elif b==1:
bl.append(t)
bc+=1
if ac<k or bc<k:
print('-1')
else:
al.sort()
bl.sort()
i=0
while i<len(al) and i<len(bl):
l.append(al[i]+bl[i])
i+=1
l.sort()
time=0
for i in range(k):
time+=l[i]
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
|
n,k = [int(i) for i in input().split()]
l = []
for i in range(n):
x = [int(i) for i in input().split()]
l.append(x)
l.sort(key = lambda x:x[0])
#print(l)
from heapq import heappop,heappush
a = []
b = []
ans = 0
ak = 0
bk = 0
i = 0
while i<n and (ak<k or bk<k):
ans+=l[i][0]
if l[i][1] == l[i][2] == 1:
ak+=1
bk+=1
elif l[i][1] == 1:
ak+=1
heappush(a,(-1*l[i][0]))
elif l[i][2] == 1:
bk+=1
heappush(b,(-1*l[i][0]))
else:
ans-=l[i][0]
i+=1
#print(ak,bk)
if ak>k :
while ak>k and a:
ans+=heappop(a)
ak-=1
if bk>k :
while bk> k and b:
ans+=heappop(b)
bk-=1
if i == n and (ak<k or bk<k):
print(-1)
elif i == n or not(a or b):
print(ans)
else:
while i<n and (a and b):
if l[i][1] == l[i][2] == 1:
x =-1*heappop(a)
y = -1*heappop(b)
if x+y>=l[i][0]:
ans-=(x+y-l[i][0])
else:
break
i+=1
print(ans)
i+=1
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class E653 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt(); int k = sc.nextInt();
Book [] books = new Book[n];
for (int i = 0; i < n; i++) {
books[i] = new Book(sc.nextInt(), sc.nextInt(), sc.nextInt());
}
ArrayList<Book> both = new ArrayList<>();
ArrayList<Book> a = new ArrayList<>();
ArrayList<Book> b = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (books[i].a == 1 && books[i].b == 0) a.add(books[i]);
else if (books[i].a == 1 && books[i].b == 1) both.add(books[i]);
else if (books[i].a == 0 && books[i].b == 1) b.add(books[i]);
}
Collections.sort(both); Collections.sort(a); Collections.sort(b);
long [] pref1 = new long[both.size() + 1];
long [] pref2 = new long[a.size() + 1];
long [] pref3 = new long[b.size() + 1];
for (int i = 1; i <= both.size(); i++) pref1[i] = pref1[i-1] + both.get(i - 1).t;
for (int i = 1; i <= a.size(); i++) pref2[i] = pref2[i-1] + a.get(i - 1).t;
for (int i = 1; i <= b.size(); i++) pref3[i] = pref3[i-1] + b.get(i - 1).t;
long ans = Long.MAX_VALUE;
for (int i = 0; i <= Math.min(both.size(), k); i++) {
int need = k - i;
if (need > a.size() || need > b.size()) continue;
ans = Math.min(ans, pref1[i] + pref2[need] + pref3[need]);
}
out.println(ans == Long.MAX_VALUE ? -1 : ans);
out.close();
}
static class Book implements Comparable<Book> {
int t; int a; int b;
Book(int t, int a, int b) {
this.a = a; this.b = b; this.t = t;
}
@Override
public int compareTo(Book o) {
return t - o.t;
}
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
import atexit
import io
import sys
import math
from collections import defaultdict,Counter
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
# sys.stdout=open("CP3/output.txt",'w')
# sys.stdin=open("CP3/input.txt",'r')
# m=pow(10,9)+7
n,k=map(int,input().split())
c1=0
c2=0
l1=[]
l2=[]
l=[]
# visit=[0]*n
for i in range(n):
t,a,b=map(int,input().split())
c1+=a
c2+=b
if a+b==2:
l.append(t)
continue
if a==1:
l1.append(t)
if b==1:
l2.append(t)
# visit[i]=1
if c1<k or c2<k:
print(-1)
else:
l1.sort()
l2.sort()
l+=[a+b for a,b in zip(l1,l2)]
# print(l)
l.sort()
print(sum(l[: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, os, io
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(sys.stdin.readline())
def ria(): return list(map(int, sys.stdin.readline().split()))
def ws(s): sys.stdout.write(s + '\n')
def wi(n): sys.stdout.write(str(n) + '\n')
def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
def solve(n, k, t, a, b):
if a.count(1) < k or b.count(1) < k:
return -1
like_ab = sorted([i for i in range(n) if a[i] == 1 and b[i] == 1], key=lambda x: t[x])
like_a = sorted([i for i in range(n) if a[i] == 1 and b[i] == 0], key=lambda x: t[x])
like_b = sorted([i for i in range(n) if b[i] == 1 and a[i] == 0], key=lambda x: t[x])
n_ab = len(like_ab)
n_a = min(len(like_a), len(like_b))
pre_ab = [0]
for i in range(1, n_ab+1):
pre_ab.append(pre_ab[i-1] + t[like_ab[i-1]])
pre = [0]
for i in range(1, n_a+1):
pre.append(pre[i-1] + t[like_a[i-1]] + t[like_b[i-1]])
ans = sum(t)
for x in range(n_ab + 1):
if n_a >= k - x >= 0:
ans = min(ans, pre_ab[x] + pre[k-x])
return ans
def main():
n, k = ria()
t = []
a = []
b = []
for _ in range(n):
ti, ai, bi = ria()
t.append(ti)
a.append(ai)
b.append(bi)
wi(solve(n, k, t, a, b))
class FastReader(io.IOBase):
newlines = 0
def __init__(self, fd, chunk_size=1024*8):
self._fd = fd
self._chunk_size = chunk_size
self.buffer = io.BytesIO()
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
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, size=-1):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
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()
class FastWriter(io.IOBase):
def __init__(self, fd):
self._fd = fd
self.buffer = io.BytesIO()
self.write = self.buffer.write
def flush(self):
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class FastStdin(io.IOBase):
def __init__(self, fd=0):
self.buffer = FastReader(fd)
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
class FastStdout(io.IOBase):
def __init__(self, fd=1):
self.buffer = FastWriter(fd)
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.flush = self.buffer.flush
if __name__ == '__main__':
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from sys import stdin
import math
[n, k] = [int(j) for j in stdin.readline().split()]
time = 0
common = dict()
alice = dict()
bob = dict()
lencommon = 0
lenalice = 0
lenbob = 0
for i in range(0, n):
[a, b, c] = [j for j in stdin.readline().split()]
a = int(a)
if b == '1' and c == '1':
lencommon += 1
if a in common.keys():
common[a] += 1
else:
common[a] = 1
elif b == '1':
lenalice += 1
if a in alice.keys():
alice[a] += 1
else:
alice[a] = 1
elif c == '1':
lenbob += 1
if a in bob.keys():
bob[a] += 1
else:
bob[a] = 1
if (lencommon + lenalice) < k or (lencommon + lenbob) < k:
print('-1')
else:
common[math.inf] = 1
alice[math.inf] = 1
bob[math.inf] = 1
A = min(alice)
B = min(bob)
C = min(common)
for i in range(0, k):
if A + B < C:
time += A + B
alice[A] -= 1
bob[B] -= 1
if alice[A] == 0:
del alice[A]
A = min(alice)
if bob[B] == 0:
del bob[B]
B = min(bob)
else:
time += C
common[C] -= 1
if common[C] == 0:
del common[C]
C = min(common)
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
|
java
|
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] t = new int[n];
int[] a = new int[n];
int[] b = new int[n];
for (int i = 0; i < n; ++i) {
t[i] = sc.nextInt();
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
System.out.println(solve(t, a, b, k));
sc.close();
}
static int solve(int[] t, int[] a, int[] b, int k) {
PriorityQueue<Integer> onlyAs = new PriorityQueue<>();
PriorityQueue<Integer> onlyBs = new PriorityQueue<>();
PriorityQueue<Integer> boths = new PriorityQueue<>();
for (int i = 0; i < t.length; ++i) {
if (a[i] == 1) {
if (b[i] == 1) {
boths.offer(t[i]);
} else {
onlyAs.offer(t[i]);
}
} else {
if (b[i] == 1) {
onlyBs.offer(t[i]);
}
}
}
int result = 0;
for (int i = 0; i < k; ++i) {
if (boths.isEmpty() && (onlyAs.isEmpty() || onlyBs.isEmpty())) {
return -1;
}
if (!boths.isEmpty()
&& (onlyAs.isEmpty() || onlyBs.isEmpty() || boths.peek() <= onlyAs.peek() + onlyBs.peek())) {
result += boths.poll();
} else {
result += onlyAs.poll() + onlyBs.poll();
}
}
return 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
|
python3
|
from sys import stdin
input=stdin.readline
def answer():
if(n3+n1 < k or n3+n2 < k):return -1
for i in range(1,n1+1):a[i]+=a[i-1]
for i in range(1,n2+1):b[i]+=b[i-1]
start=max(max(0,k-n1),max(0,k-n2))
s=0
for i in range(start):s+=common[i]
ans=1e10
for i in range(start,min(k,n3) + 1):
ans=min(ans , s + a[k-i] + b[k-i])
s+=common[i]
return ans
n,k=map(int,input().split())
a,b,common=[0],[0],[]
for i in range(n):
t,x,y=map(int,input().split())
if(x and y):common.append(t)
elif(x==1 and y==0):a.append(t)
elif(x==0 and y==1):b.append(t)
common.sort()
a.sort()
b.sort()
common.append(0)
n1,n2,n3=len(a)-1,len(b)-1,len(common)-1
print(answer())
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
from collections import deque
from sys import stdin,stdout
n,k=list(map(int,input().split()))
p=[]
al=0
bob=0
ali=[]
c=[]
bobi=[]
for i in range(n):
t,a,b=list(map(int,stdin.readline().split()))
p.append((t,a,b))
if a==1:
al+=1
if b==1:
bob+=1
if a==1 and b==0:
ali.append(t)
if a==0 and b==1:
bobi.append(t)
if a==1 and b==1:
c.append(t)
if al<k or bob<k:
print(-1)
exit()
ali.sort()
c.sort()
bobi.sort()
ali=deque(ali)
bobi=deque(bobi)
c=deque(c)
#printprint(ali)
ans=0
for i in range(k):
if len(c)>0:
if len(ali)>0 and len(bobi)>0:
if ali[0]+bobi[0]<=c[0]:
ans+=ali[0]+bobi[0]
ali.popleft()
bobi.popleft()
else:
ans+=c[0]
c.popleft()
else:
ans+=c[0]
c.popleft()
else:
ans+=ali[0]
ans+=bobi[0]
ali.popleft()
bobi.popleft()
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Main {
private static void solver(InputReader sc, PrintWriter out) throws Exception {
int n = sc.nextInt();
int k = sc.nextInt();
List<Integer> alice = new ArrayList<>();
List<Integer> bob = new ArrayList<>();
List<Integer> com = new ArrayList<>();
int countalice=0,countbob=0;
for(int i=0; i<n; i++) {
int t = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
if(a==1 && b==1){
com.add(t);
countalice++; countbob++;
}
else if(a==1 && b==0){
alice.add(t);
countalice++;
}
else if(a==0 && b==1){
bob.add(t);
countbob++;
}
}
if(countalice < k || countbob < k){
out.println(-1); return;
}
Collections.sort(alice); Collections.sort(bob); Collections.sort(com);
long count=0; long keeper=0;
int x=0,y=0,z=0;
while(keeper < k){
while(x < alice.size() && y < bob.size() && z<com.size()) {
if ((alice.get(x) + bob.get(y)) < com.get(z)) {
count += (alice.get(x) + bob.get(y));
x++;
y++;
} else {
count += com.get(z);
z++;
}
keeper++;
if(keeper >= k) break;
}
// out.prinln(count);
if(keeper >= k) break;
while(z < com.size()){
count += com.get(z);
z++;
keeper++;
if(keeper >= k) break;
}
if(keeper >= k) break;
while(x < alice.size() && y < bob.size()){
count += (alice.get(x) + bob.get(y));
x++;
y++; keeper++;
if(keeper >= k) break;
}
if(keeper >= k) break;
}
out.print(count);
}
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
solver(in, out);
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair p) {
return (this.y - p.y);
}
}
class Tuple implements Comparable<Tuple> {
int x, y, z;
public Tuple(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int compareTo(Tuple t) {
if (this.z == t.z) {
if (this.y == t.y) {
return t.x - this.x;
} else return t.y - this.y;
} else
return this.z - t.z;
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"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
|
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
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")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
avl=AvlTree()
#-----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default='z', func=lambda a, b: min(a ,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: a + b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left)/ 2)
# Check if middle element is
# less than or equal to key
if (arr[mid]<=key):
count = mid+1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def countGreater( arr,n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
n,k=map(int,input().split())
r=[]
r1=[]
r2=[]
for i in range(n):
a,b,c=map(int,input().split())
if b==1 and c==1:
r.append(a)
elif b==1:
r1.append(a)
elif c==1:
r2.append(a)
r1.sort()
r2.sort()
r.sort()
t=0
t1=0
le=min(len(r1),len(r2))
if len(r)+ min(len(r1),len(r2))<k:
print(-1)
sys.exit()
ans=0
for i in range(k):
if t == le:
t1 += 1
ans += r[t1-1]
continue
if t1 == len(r):
t += 1
ans += r1[t-1] + r2[t-1]
continue
if r1[t]+r2[t]<r[t1]:
t+=1
ans+=r1[t-1]+r2[t-1]
else:
t1+=1
ans+=r[t1-1]
print(ans)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
a = []
b = []
c = []
at = 0
bt = 0
alo = 0
blo = 0
clo = 0
for i in range(n):
t,al,bl = map(int,input().split())
if al == 1 and bl == 1:
at += 1
bt += 1
clo += 1
c.append(t)
elif al == 1:
at += 1
alo += 1
a.append(t)
elif bl == 1:
bt += 1
blo += 1
b.append(t)
if at<k or bt<k:
print("-1")
else:
total = 0
time = 0
a.sort()
b.sort()
c.sort()
j = 0
r = 0
while total<k:
if j == alo or j == blo:
time = time+c[r]
r += 1
elif r == clo:
time = time+a[j]+b[j]
j += 1
else:
if a[j]+b[j] < c[r]:
time = time+a[j]+b[j]
j += 1
else:
time = time+c[r]
r += 1
total += 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
|
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 or m != 164121:
all = []
All = []
Alice = []
Bob = []
Both = []
none = []
z = 1
while n:
i = sys.stdin.readline().split()
x = 3
i.append(z)
while x:
i[x - 1] = int(i[x - 1])
x -= 1
all.append(i)
if i[1] == i[2]:
if i[1] == 0:
none.append(i)
else:
Both.append(i)
else:
if i[1] == 0:
Bob.append(i)
else:
Alice.append(i)
z += 1
n -= 1
Alice.sort(key=lambda x: x[0])
Bob.sort(key=lambda x: x[0])
Both.sort(key=lambda x: x[0])
none.sort(key=lambda x: x[0])
tresult = []
if 2 * k > m:
l = 2 * k - m
if len(Both) >= l:
tresult = Both[:l]
Both = Both[l:]
All = Alice + Both + Bob + none
m = 2 * (m - k)
k = k - l
else:
print(-1)
exit()
else:
tresult = []
tresult1 = []
if min(len(Alice), len(Bob)) == len(Alice):
if len(Alice) < k:
k1 = k - len(Alice)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
else:
if len(Bob) < k:
k1 = k - len(Bob)
if len(Both) < k1:
print(-1)
exit()
else:
tresult1 = Both[:k1]
Both = Both[k1:]
k = k - k1
Alice1 = Alice[:k]
Bob1 = Bob[:k]
Alice = Alice[k:]
Bob = Bob[k:]
corr = []
elev = False
zz = 0
while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] > \
Both[0][0] + min(Alice1[-1][0], Bob1[-1][0], none[zz][0]):
if min(Alice1[-1][0], Bob1[-1][0], none[zz][0]) == none[zz][0]:
zz += 1
Alice.append(Alice1[-1])
Bob.append(Bob1[-1])
corr.append(Both[0])
Alice1.pop(-1)
Bob1.pop(-1)
Both.pop(0)
q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1)
q = m - q
All = Alice + Bob + Both + none
All.sort(key=lambda x: x[0])
result2 = tresult + tresult1 + corr + Alice1 + Bob1
result = All[:q]
result = result + tresult + tresult1 + corr + Alice1 + Bob1
sum1 = 0
for row in result:
sum1 = sum1 + row[0]
print(sum1)
if sum1 == 0:
print(sum(row[1] for row in result2))
print(sum(row[2] for row in result2))
result.sort(key=lambda x: x[0])
print(result[-1])
print(result[-2])
chk = result[-1][0] - 1
for row in All:
if row[0] == chk:
print(row)
if sum1 == 82207:
print(len(corr))
print(corr[-1])
corr.sort(key=lambda x: x[0])
print(corr[-1])
Both.sort(key=lambda x: x[0])
print(Both[0])
print(All[q])
if sum1 == 82207:
print(all[15429])
print(all[11655])
print(' '.join([str(row[3]) for row in result]))
else:
iva = [[] for _ in range(4)]
alv = [() for _ in range(n)]
for i in range(n):
v, o, u = [int(x) for x in input().split()]
q = (o << 1) | u
iva[q].append((v, i))
alv[i] = (v, i)
for e in iva:
e.sort()
alv.sort()
ct, a, r, ps, an = 0, 0, 0, min(len(iva[1]), len(iva[2])), 0
ana = [False] * n
for _ in range(k):
if (a < ps and r < len(iva[3])):
if (iva[1][a][0] + iva[2][a][0] < iva[3][r][0]):
cta(1, a, 1)
cta(2, a, 1)
ct += 2
a += 1
else:
cta(3, r, 1)
ct += 1
r += 1
elif (a < ps):
cta(1, a, 1)
cta(2, a, 1)
ct += 2
a += 1
elif (r < len(iva[3])):
cta(3, r, 1)
ct += 1
r += 1
else:
print(-1)
exit(0)
while (ct > m and a > 0 and r < len(iva[3])):
a -= 1
cta(1, a, -1)
cta(2, a, -1)
cta(3, r, 1)
ct -= 1
r += 1
ap = 0
while (ct < m and ap < n):
if (not ana[alv[ap][1]]):
if (r > 0 and a < ps and iva[1][a][0] + iva[2][a][0] - iva[3][r - 1][0] < alv[ap][0]):
if ana[iva[1][a][1]] or ana[iva[2][a][1]]:
a += 1
continue
r -= 1
cta(1, a, 1)
cta(2, a, 1)
cta(3, r, -1)
a += 1
ct += 1
else:
ct += 1
an += alv[ap][0];
ana[alv[ap][1]] = True;
ap += 1
else:
ap += 1
if (ct != m):
print(-1)
else:
print(an)
for i in range(n):
if (ana[i]):
print(i + 1, end=" ")
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
# ------------------- 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 --------------------
n,k=map(int,input().split())
total=0
alice=[]
bob=[]
both=[]
for j in range(n):
t,a,b=map(int,input().split())
if a==1 and b==1:
both.append(t)
elif a==1:
alice.append(t)
elif b==1:
bob.append(t)
both.sort()
bob.sort()
alice.sort()
both.reverse()
bob.reverse()
alice.reverse()
if len(both)+len(alice)<k or len(bob)+len(both)<k:
print(-1)
else:
#i think we should just line them all up
countera=0
counterb=0
summy=0
while countera<k or counterb<k:
if countera<k and counterb<k:
if len(both)>0:
if len(alice)>0 and len(bob)>0:
val1=both[-1]
val2=alice[-1]+bob[-1]
if val1<=val2:
summy+=val1
both.pop()
else:
summy+=val2
alice.pop()
bob.pop()
else:
summy+=both.pop()
else:
summy+=alice.pop()+bob.pop()
countera+=1
counterb+=1
else:
if countera<k:
#pick between max of both or alice
if len(both)>0:
val1=both[-1]
val2=alice[-1]
if val1<=val2:
summy+=val1
both.pop()
else:
summy+=val2
alice.pop()
else:
summy+=alice.pop()
countera+=1
elif counterb<k:
if len(both)>0:
val1=both[-1]
val2=bob[-1]
if val1<=val2:
summy+=val1
both.pop()
else:
summy+=val2
bob.pop()
else:
summy+=bob.pop()
counterb+=1
print(summy)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2000000000000;
const long long MOD = 998244353;
const int N = 100009;
int main() {
int n, k;
cin >> n >> k;
vector<long long> A, B, both;
for (int i = 0; i < int(n); i++) {
long long t, a, b;
cin >> t >> a >> b;
if (a == 1 && b == 1)
both.push_back(t);
else if (a == 1)
A.push_back(t);
else if (b == 1)
B.push_back(t);
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
for (int i = 0; i < int(min(A.size(), B.size())); i++) {
both.push_back(A[i] + B[i]);
}
sort(both.begin(), both.end());
long long tot = 0;
if (int(both.size()) < k) {
cout << -1 << "\n";
return 0;
}
for (int i = 0; i < int(k); i++) {
tot += both[i];
}
cout << tot << "\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 atexit
import io
import sys
import math
from collections import defaultdict,Counter
# _INPUT_LINES = sys.stdin.read().splitlines()
# input = iter(_INPUT_LINES).__next__
# _OUTPUT_BUFFER = io.StringIO()
# sys.stdout = _OUTPUT_BUFFER
# @atexit.register
# def write():
# sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
# sys.stdout=open("CP3/output.txt",'w')
# sys.stdin=open("CP3/input.txt",'r')
# m=pow(10,9)+7
n,k=map(int,input().split())
c1=0
c2=0
l1=[]
l2=[]
l=[]
# visit=[0]*n
for i in range(n):
t,a,b=map(int,input().split())
c1+=a
c2+=b
if a+b==2:
l.append(t)
continue
if a==1:
l1.append(t)
if b==1:
l2.append(t)
# visit[i]=1
if c1<k or c2<k:
print(-1)
else:
l1.sort()
l2.sort()
l+=[a+b for a,b in zip(l1,l2)]
# print(l)
l.sort()
print(sum(l[:k]))
# print(l)
# print(l1)
# print(l2)
# time=0
# while k:
# if len(l1)==0 or len(l2)==0 or (l and l1[-1]+l2[-1]>l[-1]):
# time+=l.pop()
# else:
# time+=l1.pop()
# time+=l2.pop()
# k-=1
# print(time)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int N = 2e5 + 9;
long long int primes[6] = {1125899906842597, 1495921043, 1005985879,
1495921043, 1005985879, 1495921043};
vector<long long int> adj[N];
long long int parent[N];
long long int vis[N];
long long int level[N];
long long int dist[N];
long long int dp[N];
long long int hashing[N];
long long int ar[509][509];
long long int br[509][509];
long long int cr[509][509];
long long int multiply(long long int a, long long int b) {
return ((a % mod) * (b % mod)) % mod;
}
long long int add(long long int a, long long int b) {
return ((a % mod) + (b % mod)) % mod;
}
long long int sub(long long int a, long long int b) {
return ((a % mod) - (b % mod) + mod) % mod;
}
long long int dx[] = {1, -1, 0, 0};
long long int dy[] = {0, 0, 1, -1};
long long int arr[200009];
long long int brr[200009];
long long int tim[200009];
long long int n, k;
int main() {
int start_s = clock();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int i, j, m, q, t, a, d, b, c, l, r, e, idx, ind, index, u, v, x, y,
z, h, sz, sz1, sz2, mid, len, tot, prev, temp, curr, p;
long long int res = 0, res1 = 0, res2 = 0, ans = 0, ans1 = 0, ans2 = 0,
val = 0, val1 = 0, val2 = 0, rem = 0, diff = 0, cnt = 0,
flag = 0, fl = 0, sum = 0, maxi = INT_MIN, mini = INT_MAX,
total = 0;
string str, str1, str2;
char ch, ch1, ch2;
cin >> n >> k;
for (i = 1; i <= n; i++) {
cin >> tim[i] >> arr[i] >> brr[i];
}
priority_queue<long long int, vector<long long int>, greater<long long int> >
common, lef, rig;
for (i = 1; i <= n; i++) {
if (arr[i] == 1 && brr[i] == 1) {
common.push(tim[i]);
} else if (arr[i] == 1 && brr[i] == 0) {
lef.push(tim[i]);
} else if (arr[i] == 0 && brr[i] == 1) {
rig.push(tim[i]);
}
}
long long int val3;
long long int flag1 = 0, flag2 = 0;
while (k--) {
val1 = 1e18, val2 = 1e18, val3 = 1e18;
if (common.size() == 0) {
flag1 = 1;
}
if (common.size() != 0) val1 = common.top();
if (lef.size() == 0 || rig.size() == 0) {
flag2 = 1;
}
if (lef.size() != 0) val2 = lef.top();
if (rig.size() != 0) val3 = rig.top();
if (flag1 == 1 && flag2 == 1) {
return cout << -1, 0;
}
if (val1 <= val2 + val3) {
ans += val1;
common.pop();
} else {
ans += val2 + val3;
lef.pop();
rig.pop();
}
}
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
|
n, k = map(int, input().split())
a, m, b = [], [], []
for i in range(n):
ti, ai, bi = map(int, input().split())
if ai == 1 and bi == 1:
m.append(ti)
elif ai == 1:
a.append(ti)
elif bi == 1:
b.append(ti)
a.sort()
m.sort()
b.sort()
pm = [0] * (len(m) + 1)
for i in range(1, len(m) + 1):
pm[i] = pm[i - 1] + m[i - 1]
pa = [0] * (len(a) + 1)
for i in range(1, len(a) + 1):
pa[i] = pa[i - 1] + a[i - 1]
pb = [0] * (len(b) + 1)
for i in range(1, len(b) + 1):
pb[i] = pb[i - 1] + b[i - 1]
ans = 10 ** 12
for c in range(min(k + 1, len(m) + 1)):
if min(len(a), len(b)) >= k - c and ans > pm[c] + pa[k - c] + pb[k - c]:
ans = pm[c] + pa[k - c] + pb[k - c]
if ans == 10 ** 12:
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
|
python3
|
book, k = map(int, input().split())
both, alice, bob = [], [] ,[]
for i in range(book):
time, x, y = map(int, input().split())
if x == 1 and y == 1:
both.append(time)
elif x == 1 and y == 0:
alice.append(time)
elif x == 0 and y == 1:
bob.append(time)
if len(both)+ len(alice) < k or len(both) + len(bob) < k:
print(-1)
else:
both.sort()
alice.sort()
bob.sort()
count, x, y, z = 0, 0, 0, 0
sum = 0
while count < k:
if x >= len(both):
count += 1
sum += alice[y] + bob[z]
y += 1
z += 1
continue
elif y >= len(alice) or z >= len(bob):
count += 1
sum += both[x]
x += 1
continue
elif both[x] <= alice[y] + bob[z]:
count += 1
sum += both[x]
x += 1
continue
else:
count += 1
sum += alice[y] + bob[z]
y += 1
z += 1
print(sum)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int inf = 1e9 + 7;
const double eps = 1e-6;
long long qpow(long long a, long long b, long long m) {
long long r = 1;
a %= m;
for (; b; b >>= 1) {
if (b & 1) r = r * a % m;
a = a * a % m;
}
return (r + m) % m;
}
const double pi = acos(-1);
long long ar[202030], res = 0, p[4] = {0};
bool cmp(long long a, long long b) { return ar[a] < ar[b]; }
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long n, m, k, tot = 0;
cin >> n >> m >> k;
vector<long long> v[4];
set<long long> ans;
for (long long i = 0; i < n; i++) {
long long p, q;
cin >> ar[i] >> p >> q;
v[2 * p + q].push_back(i);
}
for (int i = 0; i < 4; i++) {
sort(v[i].begin(), v[i].end(), cmp);
}
for (int i = 0; i < min((long long)v[3].size(), k); i++)
ans.insert(v[3][i]), p[3]++;
long long sz = min(v[1].size(), v[2].size());
if ((long long)v[3].size() + sz < k) {
cout << -1;
return 0;
}
for (int i = 0; i < k - (long long)v[3].size(); i++) {
ans.insert(v[1][p[1]]), p[1]++;
ans.insert(v[2][p[2]]), p[2]++;
}
long long cnt = 10;
while ((long long)ans.size() < m) {
long long mn = 1e7, id = -1;
for (long long i = 0; i < 4; i++) {
if (p[i] == (long long)v[i].size()) continue;
if (mn > ar[v[i][p[i]]]) mn = ar[v[i][p[i]]], id = i;
}
if (p[3] > 0 && p[1] < v[1].size() && p[2] < v[2].size() &&
ar[v[1][p[1]]] + ar[v[2][p[2]]] < mn + ar[v[3][p[3] - 1]]) {
ans.erase(v[3][--p[3]]);
ans.insert(v[1][p[1]++]);
ans.insert(v[2][p[2]++]);
} else
ans.insert(v[id][p[id]++]);
}
if ((long long)ans.size() != m) {
cout << -1;
return 0;
}
long long sum = 0;
for (auto i : ans) sum += ar[i];
cout << sum << '\n';
for (auto i : ans) cout << i + 1 << ' ';
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class E1_ReadingBooks_1600 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int n = sc.nextInt();
int k = sc.nextInt();
int[][] data = new int[n][3];
PriorityQueue<Integer> both = new PriorityQueue<>();
PriorityQueue<Integer> alice = new PriorityQueue<>();
PriorityQueue<Integer> bob = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
data[i][j] = sc.nextInt();
}
if (data[i][1] == data[i][2] && data[i][1] == 1) {
both.add(data[i][0]);
} else if (data[i][1] == 1) {
alice.add(data[i][0]);
} else if (data[i][2] == 1) {
bob.add(data[i][0]);
}
}
while (!alice.isEmpty() && !bob.isEmpty()) {
int x = alice.poll();
int y = bob.poll();
both.add(x + y);
}
if (both.size() < k) {
out.println(-1);
} else {
int result = 0;
for (int i = 0; i < k; i++) {
result += both.poll();
}
out.println(result);
}
out.close();
}
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
|
java
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int tt=0;tt<1;tt++) {
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
ArrayList<Integer> c = new ArrayList<>();
for(int i=0;i<n;i++) {
str = br.readLine().split(" ");
int t = Integer.parseInt(str[0]);
int x = Integer.parseInt(str[1]);
int y = Integer.parseInt(str[2]);
if(x==1 && y==1) {
c.add(t);
} else if(x==1) {
a.add(t);
} else if(y==1) {
b.add(t);
}
}
if((a.size()+c.size())<k)
{
System.out.println(-1);
continue;
}
if((b.size()+c.size())<k)
{
System.out.println(-1);
continue;
}
Collections.sort(a);
Collections.sort(b);
Collections.sort(c);
int ans = 0;
int astart = 0;
int cstart = 0;
while(k>=1) {
int op1 = Integer.MAX_VALUE;
int op2 = Integer.MAX_VALUE;
if(astart<a.size() && astart<b.size()) {
op1 = a.get(astart) + b.get(astart);
}
if(cstart<c.size()) {
op2 = c.get(cstart);
}
if(op1<op2) {
ans = ans + op1;
astart++;
} else {
ans = ans + op2;
cstart++;
}
k--;
}
System.out.println(ans);
}
}
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 1;
void updateSt(set<pair<int, int>> &st, set<pair<int, int>> &fr, int &sum,
int need) {
need = max(need, 0);
while (true) {
bool useful = false;
while (int((st).size()) > need) {
sum -= st.rbegin()->first;
fr.insert(*st.rbegin());
st.erase(prev(st.end()));
useful = true;
}
while (int((st).size()) < need && int((fr).size()) > 0) {
sum += fr.begin()->first;
st.insert(*fr.begin());
fr.erase(fr.begin());
useful = true;
}
while (!st.empty() && !fr.empty() &&
fr.begin()->first < st.rbegin()->first) {
sum -= st.rbegin()->first;
sum += fr.begin()->first;
fr.insert(*st.rbegin());
st.erase(prev(st.end()));
st.insert(*fr.begin());
fr.erase(fr.begin());
useful = true;
}
if (!useful) break;
}
}
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<pair<int, 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, i});
}
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.first);
}
}
int ans = INF;
int pos = INF;
set<pair<int, int>> st;
set<pair<int, int>> fr;
int sum = 0;
vector<int> res;
for (int iter = 0; iter < 2; ++iter) {
st.clear();
fr.clear();
sum = 0;
int start = 0;
while (k - start >= int((sums[1]).size()) ||
k - start >= int((sums[2]).size()) ||
m - start - (k - start) * 2 < 0) {
++start;
}
if (start >= int((sums[3]).size())) {
cout << -1 << endl;
return 0;
}
int need = m - start - (k - start) * 2;
for (int i = 0; i < 3; ++i) {
for (int p = int((times[i]).size()) - 1; p >= (i == 0 ? 0 : k - start);
--p) {
fr.insert(times[i][p]);
}
}
updateSt(st, fr, sum, need);
for (int cnt = start; cnt < (iter == 0 ? int((sums[3]).size()) : pos + 1);
++cnt) {
if (k - cnt >= 0) {
if (cnt + (k - cnt) * 2 + int((st).size()) == m) {
if (ans > sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum) {
ans = sums[3][cnt] + sums[1][k - cnt] + sums[2][k - cnt] + sum;
pos = cnt;
}
}
} else {
if (cnt + int((st).size()) == m) {
if (ans > sums[3][cnt] + sum) {
ans = sums[3][cnt] + sum;
pos = cnt;
}
}
}
if (iter == 1 && cnt == pos) break;
need -= 1;
if (k - cnt > 0) {
need += 2;
fr.insert(times[1][k - cnt - 1]);
fr.insert(times[2][k - cnt - 1]);
}
updateSt(st, fr, sum, need);
}
if (iter == 1) {
for (int i = 0; i < pos; ++i) res.push_back(times[3][i].second);
for (int i = 0; i < k - pos; ++i) {
res.push_back(times[1][i].second);
res.push_back(times[2][i].second);
}
for (auto [value, position] : st) res.push_back(position);
}
}
cout << ans << endl;
for (auto it : res) cout << it + 1 << " ";
cout << endl;
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastReader f = new FastReader();
int n = f.nextInt();
int k = f.nextInt();
int[] alice = new int[n];
int[] bob = new int[n];
int[] both = new int[n];
int cntA=0, cntB=0;
int cntBoth = 0;
while (n-- > 0) {
int t = f.nextInt();
int a = f.nextInt();
int b = f.nextInt();
boolean aB = a != 0;
boolean bB = b != 0;
if(!aB && !bB) {
continue;
} else if(aB && bB) {
both[cntBoth] = t;
cntBoth++;
} else if(aB) {
alice[cntA] = t;
cntA++;
} else {
bob[cntB] = t;
cntB++;
}
}
if(cntA+cntBoth < k || cntB+cntBoth < k) {
System.out.println(-1);
return;
}
Arrays.sort(alice,0,cntA);
Arrays.sort(bob, 0, cntB);
Arrays.sort(both,0,cntBoth);
long ans = 0;
int ansCnt = 0;
int pointA = 0, pointB = 0, pointBoth = 0;
while (ansCnt < k) {
if((pointA >= cntA || pointB >= cntB) && pointBoth < cntBoth) {
ans += both[pointBoth];
pointBoth++;
} else {
if(pointA < cntA && pointB < cntB && (pointBoth >= cntBoth || alice[pointA] + bob[pointB] < both[pointBoth])) {
ans += alice[pointA] + bob[pointB];
pointA++;
pointB++;
} else {
ans += both[pointBoth];
pointBoth++;
}
}
ansCnt++;
}
System.out.println(ans);
}
//fast input reader
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;
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);
if (idx <= mid)
upd(low, mid, (pos << 1));
else
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() {
int n, m, k;
scanf("%d%d%d", &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;
scanf("%d%d%d", &t, &x, &y);
if (x && !y)
a.push_back({t, i});
else if (!x && y)
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());
for (int i = 0; i < a.size(); ++i) {
val = 1, idx = a[i].first;
upd(1, MAX - 1, 1);
}
for (int i = 0; i < b.size(); ++i) {
val = 1, idx = b[i].first;
upd(1, MAX - 1, 1);
}
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;
if (needK > both.size()) {
continue;
}
int rest = m - i * 2 - needK;
if (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) {
printf("-1");
return 0;
}
printf("%d\n", ans);
for (int i = 0; i < cnt; ++i) {
printf("%d %d ", a[i].second, b[i].second);
}
int needK = k - cnt;
for (int i = 0; i < needK; ++i) {
printf("%d ", tempBoth[i].first.second);
}
int rest = m - 2 * cnt - needK;
if (rest < 0) {
return 0;
}
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]);
}
assert(rest <= s.size());
while (rest && !s.empty()) {
printf("%d ", s.begin()->second);
--rest;
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n;
long long k;
struct book {
bool a;
bool b;
int time;
};
book B[1000000];
bool compare(book x, book y) { return x.time < y.time; }
void solve() {
cin >> n;
cin >> k;
int x, y, z;
int as = 0;
int bs = 0;
int mx = 0;
for (int i = 0; i < n; i++) {
cin >> x >> y >> z;
B[i].a = y;
as += y;
bs += z;
B[i].b = z;
B[i].time = x;
mx = max(mx, B[i].time);
}
if (as < k || bs < k) {
cout << -1 << endl;
return;
}
sort(B, B + n, compare);
long long tot = 0;
int ak = 0;
int bk = 0;
stack<int> a;
stack<int> b;
for (int i = 0; i < n; i++) {
if (B[i].a && B[i].b) continue;
if (ak < k && B[i].a) {
ak++;
a.push(i);
tot += B[i].time;
}
if (bk < k && B[i].b) {
bk++;
tot += B[i].time;
b.push(i);
}
}
int lst = 0;
int used[1000000] = {0};
for (int i = 0; i < n; i++) {
if (ak < k && B[i].a && B[i].b) {
ak++;
tot += B[i].time;
if (b.size() && bk == k) {
tot -= B[b.top()].time;
b.pop();
}
if (bk < k) bk++;
lst = i;
used[i] = 1;
} else if (bk < k && B[i].a && B[i].b) {
bk++;
tot += B[i].time;
if (a.size() && ak == k) {
tot -= B[a.top()].time;
a.pop();
}
lst = i;
used[i] = 1;
if (ak < k) ak++;
}
}
for (int i = 0; i < n; i++) {
if (a.size() == 0 || b.size() == 0) break;
if (used[i]) continue;
int m = a.top();
int n = b.top();
if (B[i].a && B[i].b) {
if (B[i].time < B[m].time + B[n].time) {
tot = tot - (B[m].time + B[n].time);
tot += B[i].time;
a.pop();
b.pop();
}
}
}
cout << tot << endl;
return;
}
int main() {
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
def abc(l,n,i,x,y,m,k):
if x>=k and y>=k:
return m
if i==n:
return -1
ans=-1
ans=abc(l,n,i+1,x,y,m,k)
c=abc(l,n,i+1,x+l[i][1],y+l[i][2],m+l[i][0],k)
if c!=-1:
if ans==-1:
ans=c
else:
ans=min(ans,c)
return ans
n,k=map(int,input().split())
l=[]
for i in range(n):
l.append(list(map(int,input().split())))
#print(abc(l,n,0,0,0,0,k))
d={}
d[0,0]=[]
d[0,1]=[]
d[1,0]=[]
d[1,1]=[]
for i in l:
d[i[1],i[2]]+=[i[0]]
for i in d:
d[i]=sorted(d[i],reverse=True)
#print(d)
ans=0
while k>0:
if len(d[1,1])==0:
if len(d[0,1])==0 or len(d[1,0])==0:
break
ans+=d[0,1][-1]+d[1,0][-1]
del(d[0,1][-1])
del(d[1,0][-1])
k-=1
else:
if len(d[0,1])==0 or len(d[1,0])==0:
ans+=d[1,1][-1]
del(d[1,1][-1])
k-=1
else:
x=d[0,1][-1]+d[1,0][-1]
if x<=d[1,1][-1]:
ans+=d[0,1][-1]+d[1,0][-1]
del(d[0,1][-1])
del(d[1,0][-1])
k-=1
else:
ans+=d[1,1][-1]
del(d[1,1][-1])
k-=1
if k==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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T1, typename T2>
inline void chkmin(T1 &x, const T2 &y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, const T2 &y) {
if (x < y) x = y;
}
int n, m, k;
vector<pair<int, int>> a[4];
set<pair<int, int>> L, R;
int sumSet;
void read() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
int t, f1, f2;
cin >> t >> f1 >> f2;
a[f1 * 2 + f2].push_back({t, i});
}
for (int i = 0; i <= 3; i++) sort(a[i].begin(), a[i].end());
}
void relax(int sz) {
sz = max(sz, 0);
while (L.size() > sz) {
auto x = *(--L.end());
L.erase(--L.end());
sumSet -= x.first;
R.insert(x);
}
while (L.size() < sz) {
if (R.empty()) break;
auto x = *(R.begin());
R.erase(R.begin());
L.insert(x);
sumSet += x.first;
}
}
void add(pair<int, int> a) {
if (R.empty()) {
L.insert(a);
sumSet += a.first;
} else if (L.empty()) {
R.insert(a);
} else if (*(R.begin()) < a) {
R.insert(a);
} else {
L.insert(a);
sumSet += a.first;
}
}
int ans;
vector<int> fans;
void run() {
ans = 2e9 + 228 + 1337;
for (int it = 0; it < 2; it++) {
L.clear();
R.clear();
sumSet = 0;
int sum = 0;
int top1 = (int)a[1].size() - 1;
int top2 = (int)a[2].size() - 1;
for (auto i : a[0]) add(i);
for (auto i : a[1]) sum += i.first;
for (auto i : a[2]) sum += i.first;
for (int i = 0; i <= min(m, (int)a[3].size()); i++) {
if (i > 0) sum += a[3][i - 1].first;
while (top1 >= 0 && top1 + 1 + i > k) {
sum -= a[1][top1].first;
add(a[1][top1--]);
}
while (top2 >= 0 && top2 + 1 + i > k) {
sum -= a[2][top2].first;
add(a[2][top2--]);
}
if (min(top1, top2) + 1 + i < k) continue;
int sz = m - i - (top1 + 1) - (top2 + 1);
if (sz < 0) continue;
if (L.size() + R.size() < sz) continue;
relax(sz);
chkmin(ans, sum + sumSet);
if (!it || sum + sumSet != ans) continue;
for (auto j : L) {
fans.push_back(j.second);
}
for (int j = 0; j <= top1; j++) {
fans.push_back(a[1][j].second);
}
for (int j = 0; j <= top2; j++) {
fans.push_back(a[2][j].second);
}
for (int j = 0; j < i; j++) {
fans.push_back(a[3][j].second);
}
return;
}
}
cout << -1 << endl;
exit(0);
}
void write() {
cout << ans << endl;
sort(fans.begin(), fans.end());
for (auto i : fans) {
cout << i + 1 << " ";
}
cout << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read();
run();
write();
return 0;
}
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n,k = map(int,input().split())
a = []
b = []
anb = []
for i in range(n):
x,y,z = map(int,input().split())
if(y == 0 and z == 1):
b.append(x)
elif(y==1 and z==0):
a.append(x)
elif(y==1 and z==1):
anb.append(x)
a.sort()
b.sort()
if(len(a) < len(b)):
b = b[:len(a)]
else:
a = a[:len(b)]
for i in range(len(a)):
a[i] += b[i]
anb += a
anb.sort()
ans = 0
if(len(anb) < k):
print(-1)
else:
print(sum(anb[: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 math
#from functools import lru_cache
import heapq
#from collections import defaultdict
#from collections import Counter
#from collections import deque
#from sys import stdout
#from sys import setrecursionlimit
#setrecursionlimit(10**7)
from sys import stdin
input = stdin.readline
INF = 2*10**9 + 7
MAX = 10**7 + 7
MOD = 10**9 + 7
n, M, k = [int(x) for x in input().strip().split()]
c, a, b, u = [], [], [], []
for ni in range(n):
ti, ai, bi = [int(x) for x in input().strip().split()]
if(ai ==1 and bi == 1):
c.append((ti, ni+1))
elif(ai == 1):
a.append((ti, ni+1))
elif(bi == 1):
b.append((ti, ni+1))
else:
u.append((ti, ni+1))
c.sort(reverse = True)
a.sort(reverse = True)
b.sort(reverse = True)
u.sort(reverse = True)
alen = len(a)
blen = len(b)
clen = len(c)
ulen = len(u)
#print(alen, blen, clen, ulen)
m = max(0, k - min(alen, blen), 2*k - M)
ans = 0
alist = []
adlist = []
#print(clen, m)
if(m>clen):
print('-1')
else:
for mi in range(m):
cv, ci = c.pop()
ans += cv
heapq.heappush(alist, (-cv, ci))
ka = k - m
kb = k - m
M -= m
while(ka or kb):
ca = (c[-1][0] if c else INF)
da = 0
ap, bp = 0, 0
if(ka):
da += (a[-1][0] if a else INF)
ap = 1
if(kb):
da += (b[-1][0] if b else INF)
bp = 1
if(da<=ca and M>=2):
ans += da
if(ap):
ka -= 1
adlist.append(a[-1] if a else (INF, -1))
if a: a.pop()
M -= 1
if(bp):
kb -= 1
adlist.append(b[-1] if b else (INF, -1))
if b: b.pop()
M -= 1
else:
ans += ca
heapq.heappush(alist, (-c[-1][0], c[-1][1]) if c else (INF, -1))
if c: c.pop()
if(ap):
ka -= 1
if(bp):
kb -= 1
M -= 1
#print('M and ans are', M, ans)
if(M>(len(a) + len(c) + len(b) + len(u)) or ans>=INF):
print('-1')
else:
heapq.heapify(c)
while(M>0):
#print('M and ans is : ', M, ans)
if(u and u[-1][0] <= min(c[0][0] if c else INF, a[-1][0] if a else INF, b[-1][0] if b else INF)):
ut, dt = 0, 0
ut += (-alist[0][0] if alist else 0)
ut += u[-1][0]
dt += (a[-1][0] if a else INF)
dt += (b[-1][0] if b else INF)
if(ut<dt):
# add from ulist
upopped = u.pop()
adlist.append(upopped)
M -= 1
ans += upopped[0]
else:
# remove from alist and add from ab
alpopped = (heapq.heappop(alist) if alist else (-INF, -1))
heapq.heappush(c, (-alpopped[0], alpopped[1]))
ans += alpopped[0]
bpopped = (b.pop() if b else (INF, -1))
apopped = (a.pop() if a else (INF, -1))
adlist.append(bpopped)
adlist.append(apopped)
ans += apopped[0]
ans += bpopped[0]
M -= 1
else:
# if c is less than a, b
ct = (c[0][0] if c else INF)
at, bt = (a[-1][0] if a else INF), (b[-1][0] if b else INF)
abt = min(at, bt)
if(ct<abt):
cpopped = (heapq.heappop(c) if c else (INF, -1))
heapq.heappush(alist, (-cpopped[0], cpopped[1]))
ans += cpopped[0]
M-=1
else:
# minimum is among a and b; straight forward
if(at<bt):
apopped = (a.pop() if a else (INF, -1))
adlist.append(apopped)
ans += apopped[0]
else:
bpopped = (b.pop() if b else (INF, -1))
adlist.append(bpopped)
ans += bpopped[0]
M-=1
if(ans>=INF): break
print(ans if ans<INF else '-1')
if(ans < INF):
for ai in adlist:
print(ai[1], end = ' ')
for ai in alist:
print(ai[1], end = ' ')
print('')
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"2\n",
"38\n",
"6\n",
"26\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1",
"-1",
"-1\n",
"-1\n",
"-1\n",
"-1"
]
}
|
CORRECT
|
python3
|
n, k = map(int, input().split())
main = []
for i in range(n):
l = list(map(int, input().split()))
main.append(l)
list01 = []
list10 = []
list11 = []
for i in main:
if i[1] == 0 and i[2] == 1:
list01.append(i)
elif i[1]== 1 and i[2] == 0:
list10.append(i)
elif i[1] == 1 and i[2] == 1:
list11.append(i)
list01.sort()
list10.sort()
newlist11 = []
l1 = len(list01)
l2 = len(list10)
sum = 0
for i in range(0, min(l1, l2)):
sum = list01[i][0] + list10[i][0]
newlist11.append([sum, 1, 1])
finallist = []
finallist = list11 + newlist11
finallist.sort()
add = 0
if len(finallist) < k:
print(-1)
else:
for i in range(0, k):
add += finallist[i][0]
print(add)
|
1374_E1. Reading Books (easy version)
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.
Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.
There are n books in the family library. The i-th book is described by three integers: t_i β the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).
So they need to choose some books from the given n books in such a way that:
* Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set;
* the total reading time of these books is minimized (they are children and want to play and joy as soon a possible).
The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.
Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 β€ t_i β€ 10^4, 0 β€ a_i, b_i β€ 1), where:
* t_i β the amount of time required for reading the i-th book;
* a_i equals 1 if Alice likes the i-th book and 0 otherwise;
* b_i equals 1 if Bob likes the i-th book and 0 otherwise.
Output
If there is no solution, print only one integer -1. Otherwise print one integer T β the minimum total reading time of the suitable set of books.
Examples
Input
8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0
Output
18
Input
5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0
Output
8
Input
5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1
Output
-1
|
{
"input": [
"8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n",
"5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n",
"5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n"
],
"output": [
"18\n",
"8\n",
"-1\n"
]
}
|
{
"input": [
"2 1\n7 1 1\n2 1 1\n",
"5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n",
"6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 1\n3 0 1\n3 1 0\n3 0 0\n",
"6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n",
"8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n",
"6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"3 3 1\n27 0 0\n28 0 0\n11 0 0\n",
"1 1 1\n3 0 1\n",
"8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n",
"6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n",
"9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n",
"3 2 1\n3 0 1\n3 1 0\n3 0 0\n",
"27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n",
"6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n"
],
"output": [
"2\n",
"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=[int(i) for i in raw_input().split()]
a=[[int(i) for i in raw_input().split()]for j in range(n)]
a.sort()
ca=0
cb=0
arr=[]
brr=[]
val=0
crr=[]
for i in range(n):
if(a[i][1]==a[i][2] and a[i][1]==1):
ca+=1
cb+=1
crr.append(a[i][0])
elif(a[i][1]==a[i][2]):
continue
elif a[i][1]==1:
ca+=1
arr.append(a[i][0])
else:
cb+=1
brr.append(a[i][0])
if(ca>=k and cb>=k):
nr=[]
for i in range(min(len(arr),len(brr))):
nr.append(arr[i]+brr[i])
val=0
i=0
j=0
ct=0
while i<=len(nr) and j<=len(crr) and ct<k:
if(i==len(nr)):
val+=crr[j]
j+=1
elif(j==len(crr)):
val+=nr[i]
i+=1
elif(nr[i]<=crr[j]):
val+=nr[i]
i+=1
else:
val+=crr[j]
j+=1
ct+=1
print val
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.util.*;
import java.io.*;
public class ReadingBooksSorting {
static int n, k;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
n = sc.nextInt();
k = sc.nextInt();
ArrayList<Integer> aa = new ArrayList<Integer>();
ArrayList<Integer> bb = new ArrayList<Integer>();
ArrayList<Integer> both = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int t = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
if (a == 1 && b == 1)
both.add(t);
else if (a == 1)
aa.add(t);
else if (b == 1)
bb.add(t);
}
Collections.sort(aa);
Collections.sort(bb);
Collections.sort(both);
int remA = k;
int remB = k;
int total = 0;
int iA = 0;
int iB = 0;
int iboth = 0;
// pw.println(aa.size() + " " + bb.size());
while (remA > 0 && remB > 0 && iA < aa.size() && iB < bb.size() && iboth < both.size()) {
int a1 = aa.get(iA);
int b1 = bb.get(iB);
int bb1 = both.get(iboth);
if (a1 + b1 < bb1) {
total += (a1 + b1);
remA--;
remB--;
iA++;
iB++;
} else {
total += bb1;
remA--;
remB--;
iboth++;
}
}
if (remA > 0 || remB > 0) {
if (iA == aa.size() && iB == bb.size()) {
while (iboth < both.size() && (remA > 0 || remB > 0)) {
total += both.get(iboth);
remA--;
remB--;
iboth++;
}
} else {
if (iboth == both.size()) {
while (iA < aa.size() && remA > 0) {
total += aa.get(iA);
remA--;
iA++;
}
while (iB < bb.size() && remB > 0) {
total += bb.get(iB);
remB--;
iB++;
}
}
while (remA>0 && iA < aa.size() && iboth < both.size()) {
int both1 = both.get(iboth);
int a1 = aa.get(iA);
if (remA > 0 && remB > 0 || both1 < a1) {
total += both1;
remB--;
remA--;
iboth++;
} else if (remA > 0) {
total += a1;
remA--;
iA++;
}
}
while (remB>0 &&iB < bb.size() && iboth < both.size()) {
int both1 = both.get(iboth);
int b1 = bb.get(iB);
if (remB > 0 && remA > 0 || both1 < b1) {
total += both1;
remB--;
remA--;
iboth++;
} else if (remB > 0) {
total += b1;
remB--;
iB++;
}
}
}
}
if (remA > 0 || remB > 0)
pw.println(-1);
else {
pw.println(total);
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.