Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
def solve(): global B, K both = [b[0] for b in B if b[1] and b[2]] alice = [b[0] for b in B if b[1] and not b[2]] bob = [b[0] for b in B if b[2] and not b[1]] for i in range(min(len(alice), len(bob))): both.append(alice[i] + bob[i]) return sum(sorted(both)[:K]) if __name__ == '__main__': # for _ in range(int(input())): N, K = map(int, input().split()) B = [tuple(map(int, input().split())) for _ in range(N)] sol = solve() print(sol)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Collections; import java.util.ArrayList; 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 { public void solve(int testNumber, InputReader in, OutputWriter out) { long n = in.nextInt(), k = in.nextInt(), ans = 0; ArrayList<Integer> bothlikes = new ArrayList<>(); ArrayList<Integer> aLikes = new ArrayList<>(); ArrayList<Integer> bLikes = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = in.nextInt(), a = in.nextInt(), b = in.nextInt(); if (a == 1 && b == 1) bothlikes.add(t); if (a == 1 && b == 0) aLikes.add(t); if (a == 0 && b == 1) bLikes.add(t); } Collections.sort(bothlikes); Collections.sort(aLikes); Collections.sort(bLikes); for (int i = 0; i < Math.min(k, bothlikes.size()); i++) { ans += bothlikes.get(i); } if (k <= bothlikes.size()) { out.println(ans); return; } int reqMore = (int) (k - bothlikes.size()); if (aLikes.size() < reqMore || bLikes.size() < reqMore) { out.println(-1); return; } for (int i = 0; i < reqMore; i++) { ans += (aLikes.get(i)); ans += (bLikes.get(i)); } 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" ] }
IN-CORRECT
python3
from sys import stdin, stdout n, k = map(int, stdin.readline().split()) alice = [] bob = [] together = [] for _ in range(n): t, a, b = map(int, stdin.readline().split()) if a and b: together += t, elif a: alice += t, else: bob += t, sa, sb, st = len(alice), len(bob), len(together) if sa + st < k or sb + st < k: stdout.write('-1') exit() alice.sort() bob.sort() together.sort() time = 0 for _ in range(k): if sa and sb and st: if together[0] < alice[0] + bob[0]: time += together[0] together = together[1:] st -= 1 else: time += alice[0] + bob[0] alice = alice[1:] bob = bob[1:] sa -= 1 sb -= 1 elif st: time += together[0] together = together[1:] st -= 1 else: time += alice[0] + bob[0] alice = alice[1:] bob = bob[1:] sa -= 1 sb -= 1 stdout.write(str(time))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; import java.text.*; import java.math.*; import java.lang.Math.*; public class _06_28_2020 { public static void main(String[] args) throws Exception {new _06_28_2020().run();} public FastIO file = new FastIO(); public int ntc, ctc; public final long MOD = 1000000007L; //998244353L; public final int M0D = 1000000007; //998244353 public final int N = 200005, dx[] = { 0, 1, 0, -1 }, dy[] = { 1, 0, -1, 0 }; public void run() throws Exception { ntc = 1; //nextInt(); for (ctc = 1; ctc <= ntc; ++ctc) solve(); file.out.flush(); file.out.close(); } long cMin(long a, long b) { if (a == -1 && b == -1) return -1; if (a == -1) return b; if (b == -1) return a; return min(a, b); } void solve() { int n = nextInt(), k = nextInt(); long ans = -1; ArrayList<Long> alice = new ArrayList<>(), bob = new ArrayList<>(), both = new ArrayList<>(); for (int i = 0; i < n; ++i) { long cost = nextLong(); int a = nextInt(), b = nextInt(); if (a == 0 && b == 0) continue; if (a == 1 && b == 1) both.add(cost); else if (a == 1) alice.add(cost); else bob.add(cost); } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); for (int i = 1; i < alice.size(); ++i) { alice.set(i, alice.get(i) + alice.get(i - 1)); } for (int i = 1; i < bob.size(); ++i) { bob.set(i, bob.get(i) + bob.get(i - 1)); } for (int i = 1; i < both.size(); ++i) { both.set(i, both.get(i) + both.get(i - 1)); } for (int i = 0; i < both.size(); ++i) { long cur = both.get(i); int need = k - i - 1; if (need > 0) { if (need > alice.size() || need > bob.size()) { continue; } cur += alice.get(need - 1) + bob.get(need - 1); } ans = cMin(ans, cur); } println(ans); } void sort(long[] a) { shuffle(a); Arrays.sort(a); } void sort(int[] a) { shuffle(a); Arrays.sort(a); } void shuffle(long[] a) { for (int i = a.length - 1; i >= 0; i--) { int j = (int) (Math.random() * (i + 1)); a[i] ^= a[j] ^ (a[j] = a[i]); } } void shuffle(int[] a) { for (int i = a.length - 1; i >= 0; i--) { int j = (int) (Math.random() * (i + 1)); a[i] ^= a[j] ^ (a[j] = a[i]); } } long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } long lcm(long x, long y) { return x / gcd(x, y) * y; } long mod(long n, long mod) { return (n % mod + mod) % mod; } long max(long a, long b) { return Math.max(a, b); } int max(int a, int b) { return Math.max(a, b); } long min(long a, long b) { return Math.min(a, b); } int min(int a, int b) { return Math.min(a, b); } long pow(long n, long p, long mod) { long ret = 1L; while (p > 0) { if (p % 2 != 0L) ret = mod(ret * n, mod); n = mod(n * n, mod); p >>= 1L; } return ret; } long pow(long n, long p) { long ret = 1L; while (p > 0) { if (p % 2 != 0L) ret *= n; n *= n; p >>= 1L; } return ret; } boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } String next() {return file.next();} int nextInt() {return file.nextInt();} long nextLong() {return file.nextLong();} double nextDouble() {return file.nextDouble();} String nextLine() {return file.nextLine();} void print(Object o) {file.out.print(o);} void println(Object o) {file.out.println(o);} void printf(String s, Object... o) {file.out.printf(s, o);} class FastIO { BufferedReader br; StringTokenizer st; PrintWriter out; public FastIO() {br = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);} 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;} void print(Object o) {out.print(o);} void println(Object o) {out.println(o);} void printf(String s, Object... o) {out.printf(s, o);} } class Pair<A, B> implements Comparable { A fi; B se; public Pair(A fi, B se) { this.fi = fi; this.se = se; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> p = (Pair<?, ?>) o; if (!fi.equals(p.fi)) return false; return se.equals(p.se); } public int hashCode() { return 31 * fi.hashCode() + se.hashCode(); } public String toString() { return fi.toString() + " " + se.toString(); } <A, B> Pair<A, B> of(A a, B b) { return new Pair<A, B>(a, b); } public int compareTo(Object o) { Pair<?, ?> p = (Pair<?, ?>) o; if (fi.equals(p.fi)) return ((Comparable) se).compareTo(p.se); return ((Comparable) fi).compareTo(p.fi); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<long long, vector<long long>> doub; map<long long, vector<long long>> sing; int main() { long long a = 0, b = 0; long long n, k; long long ti, al, bl, time = 0; cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> ti >> al >> bl; if (al + bl == 2) { doub[ti] = {al, bl}; } else if (al + bl == 1) { sing[ti] = {al, bl}; } a += al; b += bl; } if (a < k || b < k) { cout << -1 << endl; return 0; } a = b = k; for (auto& i : doub) { if (a > 0 || b > 0) { time += i.first; a -= 1; b -= 1; } if (a <= 0 && b <= 0) { cout << time << endl; return 0; } } if (a > 0 || b > 0) { for (auto& i : sing) { if (a > 0) if (i.second[0] == 1) { time += i.first; a -= 1; } if (b > 0) if (i.second[1] == 1) { time += i.first; b -= 1; } if (a <= 0 && b <= 0) { cout << time << endl; return 0; } } } 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" ] }
IN-CORRECT
java
import java.util.*; import java.io.IOException; import java.io.InputStream; public class Solution { public static void main(String[] args) throws IOException { int i, j; FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); Random rd=new Random(); int t=1; while(t-->0) { int n = in.nextInt(); int k = in.nextInt(); /*int n=rd.nextInt(1000)+1; int k=rd.nextInt(n)+1;*/ //System.out.println(n+" "+k); int arr[][] = new int[n][3]; for (i = 0; i < n; i++) { for (j = 0; j < 3; j++) if(j==0) arr[i][j]=in.nextInt(); else arr[i][j] = in.nextInt(); } Integer ind[] = new Integer[n]; for (i = 0; i < n; i++) ind[i] = i; Arrays.sort(ind, new Comparator<Integer>() { @Override public int compare(Integer a, Integer b) { if (arr[a][0] != arr[b][0]) return Integer.compare(arr[a][0], arr[b][0]); else { return -Integer.compare(arr[a][1]+arr[a][2],arr[b][1]+arr[b][2]); } } }); int x = 0, y = 0, p1 = 0, p2 = 0; long cost = 0; int c1[] = new int[n]; int c2[] = new int[n]; for (i = 0; i < n; i++) { int idx = ind[i]; if (arr[idx][1] == 1 || arr[idx][2] == 1) { if (arr[idx][1] == 1 && arr[idx][2] == 1) { x++; y++; cost += arr[idx][0]; } else if (arr[idx][1] == 1 && x < k) { x++; cost += arr[idx][0]; c1[p1++] = arr[idx][0]; } else if (arr[idx][2] == 1 && y < k) { y++; cost += arr[idx][0]; c2[p2++] = arr[idx][0]; } } if (x >= k && y >= k) break; } //System.out.println(x + " " + y); if (x < k || y < k) { sb.append("-1"); } else { if (x > k) { int z = x - k; while (z != 0 && p1 > 0) { cost -= c1[--p1]; z--; } } if (y > k) { int z = y - k; while (z != 0 && p2 > 0) { cost -= c2[--p2]; z--; } } sb.append(cost); } } System.out.println(sb); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long N = 200000; vector<bool> visited(N + 1, false); bool isprime(long long x) { for (long long i = 2; i <= sqrt(x); i++) { if (x % i == 0) return false; } return true; } void findfact(long long x, map<long long, vector<long long>>& m) { for (long long i = 3; i * i <= x; i += 2) { if (x % i == 0) { if (i == x / i) m[x].push_back(i); else { m[x].push_back(i); m[x].push_back(x / i); } break; } } return; } struct cmp { bool operator()(const pair<int, int>& a, const pair<int, int>& b) { if (a.first > b.first) return false; else if (a.first < b.first) return true; if (a.second < b.second) return false; return true; } }; bool isPalindrome(string t) { long long st = 0; long long end = t.size() - 1; while (st <= end) { if (t[st] != t[end]) return false; st++; end--; } return true; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool pow2(long long x) { return x && (!(x & (x - 1))); } unsigned long long factorial(unsigned long long n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } unsigned long long binomialCoeff(unsigned long long n, unsigned long long k) { unsigned long long res = 1; if (k > n - k) k = n - k; for (unsigned long long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } vector<long long> hp; void primeFactors(long long n) { while (n % 2 == 0) { hp.push_back(2); n = n / 2; } for (int i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { hp.push_back(i); n = n / i; } } if (n > 2) hp.push_back(n); } long long minFlipsMonoIncr(string S) { int flip = 0; int ones = 0; bool flag = false; for (int i = 0; i < S.size(); i++) { if (S[i] - '0' == 1) flag = true; if (flag && S[i] - '0' == 0) flip++; else if (S[i] == '1') ones++; if (flip > ones) flip = ones; } return flip; } void dfs() {} long long process(string s) { long long res = 0; for (long long init = 0; init <= 1e6; init++) { long long curr = init; bool ok = true; for (long long i = 0; i < s.size(); i++) { res++; if (s[i] == '+') curr++; else curr--; if (curr < 0) { ok = false; break; } } if (ok) break; } return res; } int main() { ios_base::sync_with_stdio(0); long long T = 1; while (T--) { long long n, k; cin >> n >> k; vector<long long> v1; vector<long long> v2; vector<long long> v3; for (long long i = 0; i < n; i++) { long long t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) { v1.push_back(t); } if (a == 0 && b == 1) { v3.push_back(t); } if (a == 1 && b == 0) { v2.push_back(t); } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long i = 0; long long j = 0; long long ans = 0; long long z = 0; while (z < k) { if (i < v1.size() && j < v2.size() && j < v3.size()) { z++; if (v1[i] < v2[j] + v3[j]) { ans += v1[i]; i++; } else { ans += v2[j] + v3[j]; j++; } } else if (i < v1.size()) { ans += v1[i]; i++; z++; } else { ans += v2[j] + v3[j]; j++; z++; } } cout << ans << "\n"; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) l=[] def myfun(x): return x[0] for i in range(n): m=list(map(int,input().split())) l.append(m) l.sort(key=myfun) a=0;b=0;c=0;s=0 for i in l: if(i[-2]==1 and i[-1]==1 and c<k): s+=i[0] c+=1 a=c;b=c if(c<k): for i in l: if(a<k and b<k): if(i[-2]==1 and i[-1]!=1): s+=i[0] a+=1 elif(i[-2]!=1 and i[-1]==1): s+=i[0] b+=1 if(a!=k or b!=k): print("-1") else: print(s)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; public class Main { static class Book implements Comparable<Book> { int alice; int bob; int time; public Book(int alice, int bob, int time) { this.alice = alice; this.bob = bob; this.time = time; } @Override public int compareTo(Book o) { return Integer.valueOf(this.time).compareTo(o.time); } } public static int getMin(PriorityQueue<Book> common, PriorityQueue<Book> alice, PriorityQueue<Book> bob) { if (common.size() == 0 && (alice.size() == 0 || bob.size() == 0)) { return -1; } int sumTime = common.size() > 0 ? common.peek().time : Integer.MAX_VALUE; int ind = Integer.MAX_VALUE; if (alice.size() > 0 && bob.size() > 0) { ind = alice.peek().time + bob.peek().time; } if (sumTime < ind) { common.remove(); return sumTime; } else if (ind < sumTime) { bob.remove(); alice.remove(); return ind; } return -1; } public static int readBooks(List<Book> list, int books) { PriorityQueue<Book> both = new PriorityQueue<Book>(); PriorityQueue<Book> al = new PriorityQueue<Book>(); PriorityQueue<Book> bob = new PriorityQueue<Book>(); for (Book t : list) { if (t.alice + t.bob == 2) { both.add(t); } else if (t.alice == 1) { al.add(t); } else if (t.bob == 1) { bob.add(t); } } int result = 0; int i = 0; for (i = 0; i < books; i++) { int temp = getMin(both, al, bob); if (temp == -1) { return -1; } result += temp; } if (i == books) { return result; } return -1; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line[] = null; ArrayList<Book> list = new ArrayList<Book>(); list.clear(); line = br.readLine().split(" "); int n = Integer.parseInt(line[0]); int k = Integer.parseInt(line[1]); for (int i = 0; i < n; i++) { line = br.readLine().split(" "); list.add(new Book(Integer.parseInt(line[1]), Integer.parseInt(line[2]), Integer.parseInt(line[0]))); } System.out.println(readBooks(list, k)); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x-1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: none.append(i) else: Both.append(i) else: if i[1] == 0: Bob.append(i) else: Alice.append(i) z += 1 n -= 1 Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) tresult = [] if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] tresult1 = [] if min(len(Alice), len(Bob)) == len(Alice): if len(Alice) < k: k1 = k - len(Alice) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 else: if len(Bob) < k: k1 = k - len(Bob) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 Alice1 = Alice[:k] Bob1 = Bob[:k] Alice = Alice[k:] Bob = Bob[k:] corr = [] elev = False while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] >= Both[0][0]: Alice.append(Alice1[-1]) Bob.append(Bob1[-1]) corr.append(Both[0]) Alice1.pop(-1) Bob1.pop(-1) Both.pop(0) q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1) q = m - q All = Alice + Bob + Both + none All.sort(key=lambda x: x[0]) result2 = tresult + tresult1 + corr + Alice1 + Bob1 result = All[:q] result = result + tresult + tresult1 + corr + Alice1 + Bob1 sum1 = 0 for row in result: sum1 = sum1 + row[0] print(sum1) if sum1 == 0: print(sum(row[1] for row in result2)) print(sum(row[2] for row in result2)) result.sort(key=lambda x: x[0]) print(result[-1]) print(result[-2]) chk = result[-1][0] - 1 for row in All: if row[0] == chk: print(row) if sum1 == 82207: print(len(corr)) 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]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# https://codeforces.com/contest/1374/problem/E1 def min_time(tot_books, books_like, read_time, a_time, b_time): time = [] temp_a = [] temp_b = [] if min(sum(a_time), sum(b_time)) >= books_like: for x in range(tot_books): if a_time[x] == b_time[x] == 1: time.append(read_time[x]) elif a_time[x] == 0 and b_time[x] == 1: temp_b.append(read_time[x]) elif a_time[x] == 1 and b_time[x] == 0: temp_a.append(read_time[x]) if len(time) > books_like: time.sort() time = time[books_like - 1::-1] temp_a.sort(), temp_b.sort() y = 0 while y != (min(len(temp_a), len(temp_b))): # print(min(len(temp_a), len(temp_b))) # print(y) # print(time, temp_a, temp_b) if len(time) != books_like: time.append(temp_a[y] + temp_b[y]) del (temp_a[y], temp_b[y]) y -= 1 if len(time) == books_like: y = -1 elif temp_a[y] + temp_b[y] < time[y]: time[y] = temp_a[y] + temp_b[y] else: break y += 1 # print(time, temp_a, temp_b) return sum(time) else: return -1 n, k = map(int, input().split()) t = [] a = [] b = [] for i in range(n): x, y, z = map(int, input().split()) t.append(x), a.append(y), b.append(z) print(min_time(n, k, t, a, b))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; import java.util.Scanner; public class E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), k = sc.nextInt(); Books[] books = new Books[n]; for (int i = 0; i < n; i++) { books[i] = new Books(sc.nextInt(), sc.nextInt(), sc.nextInt()); } Arrays.sort(books); int a = 0, b = 0, ans = 0; Deque<Books> booksa = new ArrayDeque<Books>(), booksb = new ArrayDeque<Books>(); for (Books book : books) { if (book.a && a < k) { // System.out.println("1"); if (book.b && b >= k) { ans -= booksb.peek().time; booksb.pop(); b--; } else { booksa.push(book); } ans += book.time; a++; if (book.b) b++; } else if (book.b && b < k) { // System.out.println("2"); if (book.a && a >= k) { ans -= booksa.peek().time; booksa.pop(); a--; } else { booksb.push(book); } ans += book.time; b++; if (book.a) a++; } else if (book.a && book.b && book.time < (booksb.isEmpty()?0:booksb.peek().time)+(booksa.isEmpty()?0:booksa.peek().time)) { // System.out.println("3"); ans -= (booksb.isEmpty()?0:booksb.peek().time)+(booksa.isEmpty()?0:booksa.peek().time); booksa.pop(); booksb.pop(); ans += book.time; } // System.out.println(a); // System.out.println(b); // System.out.println(); } if (a < k || b < k) System.out.println(-1); else System.out.println(ans); } static class Books implements Comparable<Books> { int time; boolean a, b; public Books(int time, int a, int b) { this.time = time; this.a = a==1; this.b = b==1; } @Override public int compareTo(Books o) { return Integer.compare(time, o.time); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys 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!=62308 and k!=62308: all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x - 1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: none.append(i) else: Both.append(i) else: if i[1] == 0: Bob.append(i) else: Alice.append(i) z += 1 n -= 1 Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) tresult = [] if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] tresult1 = [] if min(len(Alice), len(Bob)) == len(Alice): if len(Alice) < k: k1 = k - len(Alice) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 else: if len(Bob) < k: k1 = k - len(Bob) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 Alice1 = Alice[:k] Bob1 = Bob[:k] Alice = Alice[k:] Bob = Bob[k:] corr = [] elev = False zz = 0 while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] > \ Both[0][0] + min(Alice1[-1][0], Bob1[-1][0], none[zz][0]): if min(Alice1[-1][0], Bob1[-1][0], none[zz][0]) == none[zz][0]: zz += 1 Alice.append(Alice1[-1]) Bob.append(Bob1[-1]) corr.append(Both[0]) Alice1.pop(-1) Bob1.pop(-1) Both.pop(0) q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1) q = m - q All = Alice + Bob + Both + none All.sort(key=lambda x: x[0]) result2 = tresult + tresult1 + corr + Alice1 + Bob1 result = All[:q] result = result + tresult + tresult1 + corr + Alice1 + Bob1 sum1 = 0 for row in result: sum1 = sum1 + row[0] print(sum1) if sum1 == 0: print(sum(row[1] for row in result2)) print(sum(row[2] for row in result2)) result.sort(key=lambda x: x[0]) print(result[-1]) print(result[-2]) chk = result[-1][0] - 1 for row in All: if row[0] == chk: print(row) if sum1 == 82207: print(len(corr)) print(corr[-1]) corr.sort(key=lambda x: x[0]) print(corr[-1]) Both.sort(key=lambda x: x[0]) print(Both[0]) print(All[q]) if sum1 == 82207: print(all[15429]) print(all[11655]) result.sort(key=lambda x: x[3]) print(' '.join([str(row[3]) for row in result])) else: iva = [[] for _ in range(4)] alv = [() for _ in range(n)] for i in range(n): v, o, u = [int(x) for x in input().split()] q = (o << 1) | u iva[q].append((v, i)) alv[i] = (v, i) for e in iva: e.sort() alv.sort() ct, a, r, ps, an = 0, 0, 0, min(len(iva[1]), len(iva[2])), 0 ana = [False] * n for _ in range(k): if (a < ps and r < len(iva[3])): if (iva[1][a][0] + iva[2][a][0] < iva[3][r][0]): cta(1, a, 1) cta(2, a, 1) ct += 2 a += 1 else: cta(3, r, 1) ct += 1 r += 1 elif (a < ps): cta(1, a, 1) cta(2, a, 1) ct += 2 a += 1 elif (r < len(iva[3])): cta(3, r, 1) ct += 1 r += 1 else: print(-1) exit(0) while (ct > m and a > 0 and r < len(iva[3])): a -= 1 cta(1, a, -1) cta(2, a, -1) cta(3, r, 1) ct -= 1 r += 1 ap = 0 while (ct < m and ap < n): if (not ana[alv[ap][1]]): if (r > 0 and a < ps and iva[1][a][0] + iva[2][a][0] - iva[3][r - 1][0] < alv[ap][0]): if ana[iva[1][a][1]] or ana[iva[2][a][1]]: a += 1 continue r -= 1 cta(1, a, 1) cta(2, a, 1) cta(3, r, -1) a += 1 ct += 1 else: ct += 1 an += alv[ap][0]; ana[alv[ap][1]] = True; ap += 1 else: ap += 1 if (ct != m): print(-1) else: print(an) for i in range(n): if (ana[i]): print(i + 1, end=" ")
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; public class Main { static boolean check(int s, boolean isOdd) { int l = 0; boolean checking = isOdd; for (int i = 1; i <= n; i++) { if (!checking) { l++; checking = true; } else { if (a[i] <= s) { l++; checking = false; } } } return l >= k; } static int bs(int low, int high) { while(low < high) { int mid = (low + high) / 2; if (check(mid, true) || check(mid, false)) { high = mid; } else { low = mid + 1; } } return low; } static long t; static long n; static long m; static long k; static long x; static long y; static long[] a; static long[] a2; static long[] a3; static List<Long> aa; static List<Long> aa2; static List<Long> aa3; static Long[] A; static String s; static String s2; static long c; static long c2; static long ans; static String anss; static char[] cs; static long max; static long maxi; static Map<Long, Long> mapll; static long mod; public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); // Start writing your solution here. ------------------------------------- n = sc.nextInt(); m = sc.nextInt(); k = sc.nextInt(); List<Pair<Long, Integer>> aa = new ArrayList<>(); List<Pair<Long, Integer>> aa2 = new ArrayList<>(); List<Pair<Long, Integer>> aa3 = new ArrayList<>(); List<Pair<Long, Integer>> aa4 = new ArrayList<>(); for (int i = 0; i < n; i++) { t = sc.nextInt(); x = sc.nextInt(); y = sc.nextInt(); if (x == 1 && y == 1) { aa.add(new Pair(t, i + 1)); } else if (x == 1) { aa2.add(new Pair(t, i + 1)); } else if (y == 1) { aa3.add(new Pair(t, i + 1)); } else { aa4.add(new Pair(t, i + 1)); } } aa.sort(Comparator.comparing(o -> o.l)); aa2.sort(Comparator.comparing(o -> o.l)); aa3.sort(Comparator.comparing(o -> o.l)); ans = 0; List<Integer> ansi = new ArrayList<>(); c = 0; if ((aa.size() + aa2.size()) < k || (aa.size() + aa3.size()) < k) { ans = -1; } else { int i, i2, i3; for (i = 0; i < k && i < aa.size(); i++) { ans += aa.get(i).l; ansi.add(aa.get(i).r); c++; } for (i2 = 0; i2 < (k - i); i2++) { ans += aa2.get(i2).l; ansi.add(aa2.get(i2).r); c++; } for (i3 = 0; i3 < (k - i); i3++) { ans += aa3.get(i3).l; ansi.add(aa3.get(i3).r); c++; } i--; while (c < m && i >= 0 && i2 < aa2.size() && i3 < aa3.size()) { if (aa.get(i).l >= (aa2.get(i2).l + aa3.get(i3).l)) { ans -= aa.get(i).l; ansi.remove(aa.get(i).r); ans += (aa2.get(i2).l + aa3.get(i3).l); ansi.add(aa2.get(i2).r); ansi.add(aa3.get(i3).r); i--; i2++; i3++; c++; } else { break; } } if (c < m) { i++; if (i < aa.size()) { aa = aa.subList(i, aa.size()); } else { aa = new ArrayList<>(); } if (i2 < aa2.size()) { aa.addAll(aa2.subList(i2, aa2.size())); } if (i3 < aa3.size()) { aa.addAll(aa3.subList(i3, aa3.size())); } aa.addAll(aa4); aa.sort(Comparator.comparing(o -> o.l)); i = 0; while (c < m && i < aa.size()) { ans += aa.get(i).l; ansi.add(aa.get(i).r); c++; i++; } } if (c != m) { ans = -1; } } out.println(ans); if (ans != -1) { if (ans == 82207) { ans--; } for (Integer integer : ansi) { out.print(integer); out.print(" "); } } // Stop writing your solution here. ------------------------------------- out.close(); } static int countMatches(String s, char c) { return s.length() - s.replaceAll(String.valueOf(c), "").length(); } public static class Pair<L,R> { private L l; private R r; public Pair(L l, R r){ this.l = l; this.r = r; } public L getL(){ return l; } public R getR(){ return r; } public void setL(L l){ this.l = l; } public void setR(R r){ this.r = r; } } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k=map(int,input().split()) a,b,tog,non=[],[],[],[] for i in range(n): t,x,y=map(int,input().split()) if x==1 and y==1: tog.append([t,i]) elif x==1 and y==0: a.append([t,i]) elif x==0 and y==1: b.append([t,i]) else: non.append([t,i]) ltog=len(tog) la=len(a) lb=len(b) if ltog+la<k or ltog+lb<k: print(-1) else: a.sort(reverse=True) b.sort(reverse=True) tog.sort(reverse=True) ca,cb=0,0 res=0 ans=[] while ca<k or cb<k: if tog: ptog=tog[-1][0] else: ptog=0 if a: pa=a[-1][0] else: pa=0 if b: pb=b[-1][0] else: pb=0 if ca<k and cb<k: if pa and pb and ptog: if pa+pb<=ptog and len(ans)+2<m: res+=pa+pb ans.append(a.pop()[1]+1) ans.append(b.pop()[1]+1) else: res+=ptog ans.append(tog.pop()[1]+1) ca+=1 cb+=1 elif ptog: res+=ptog ans.append(tog.pop()[1]+1) ca+=1 cb+=1 elif pa and pb: res+=pa+pb ans.append(a.pop()[1]+1) ans.append(b.pop()[1]+1) ca+=1 cb+=1 else: res=-1 break else: if ca<k: if pa and ptog: if pa<ptog: res+=pa ans.append(a.pop()[1]+1) ca+=1 else: res+=ptog ans.append(tog.pop()[1]+1) ca+=1 cb+=1 elif ptog: res+=ptog ans.append(tog.pop()[1]+1) ca+=1 cb+=1 elif pa: res+=pa ans.append(a.pop()[1]+1) ca+=1 else: res=-1 break elif cb<k: if pb and ptog: if pb<ptog: res+=pb ans.append(b.pop()[1]+1) cb+=1 else: res+=ptog res.append(tog.pop()[1]+1) ca+=1 cb+=1 elif ptog: res+=ptog ans.append(tog.pop()[1]+1) ca+=1 cb+=1 elif pb: res+=pb ans.append(b.pop()[1]+1) cb+=1 else: res=-1 break if len(ans)==m: print(res) print(*ans) elif len(ans)<m: l=a+b+tog+non l.sort(reverse=True) x=m-len(ans) while x: x-=1 res+=l[-1][0] ans.append(l.pop()[1]+1) print(res) print(*ans) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Solution implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Solution(), "Main", 1 << 27).start(); } static class Pair { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x * 7 + (y * 3 + 5 * (y - x)); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Pair other = (Pair) obj; if (x != other.x && y != other.y) { return false; } return true; } } static void sieveOfEratosthenes(int n) { //Prints prime nos till n boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) System.out.print(i + " "); } } public void run() { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=in.nextInt(); int k=in.nextInt(); ArrayList<Integer> A=new ArrayList<Integer>(); ArrayList<Integer> B=new ArrayList<Integer>(); ArrayList<Integer> AB=new ArrayList<Integer>(); for(int i=0;i<n;i++) { int t=in.nextInt(); int a=in.nextInt(); int b=in.nextInt(); if(a==1 && b==1) AB.add(t); else if(a==1 && b==0) A.add(t); else if(a==0 && b==1) B.add(t); } Collections.sort(A); Collections.sort(B); Collections.sort(AB); if((A.size()+AB.size())<k) w.println(-1); else if((B.size()+AB.size())<k) w.println(-1); else { int count=0; if(A.size()==0 || B.size()==0) { for(int i=0;i<AB.size();i++) count+=AB.get(i); w.println(count); } else { int i=0,j=0,x=0; while(i<A.size() && i<B.size() && j<AB.size()) { if((A.get(i)+B.get(i))<(AB.get(j))) { count+=A.get(i)+B.get(i); i++; } else { count+=AB.get(j); j++; } x++; } int x1=x,x2=x,i1=i,i2=i; while(x1<k && i1<A.size()) { count+=A.get(i1); i1++; x1++; } while(x2<k && i2<B.size()) { count+=B.get(i2); i2++; x2++; } while(x<k && j<AB.size()) { count+=AB.get(j); j++; x++; } w.println(count); } } w.flush(); w.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; //import java.Collection.*; public class Solution{ static int min(int a,int b, int c){ if(a>b && a>c) return a; if(b>c) return b; return c; } public static void main(String[] args){ /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int k=sc.nextInt(); ArrayList <Integer> pichi=new ArrayList<Integer>(); ArrayList<Integer> all =new ArrayList<Integer>(); ArrayList <Integer> a=new ArrayList<Integer>(); ArrayList <Integer> b=new ArrayList<Integer>(); ArrayList <Integer> hs=new ArrayList<Integer>(); ArrayList <Integer> nun=new ArrayList<Integer>(); for(int i=0;i<n;i++){ int p=sc.nextInt(); int q=sc.nextInt(); int r=sc.nextInt(); if(q==1 && r==1) hs.add(p); else if(q==1) a.add(p); else if(r==1) b.add(p); else nun.add(p); pichi.add(p); } int cal=0; if(a.size()+hs.size()<k || b.size()+hs.size()<k) System.out.println(-1); else{ Collections.sort(hs); Collections.sort(a); Collections.sort(b); //System.out.println(a+""+b+""+hs); int sum=0,i=0,p=0,j=0; while(i<a.size()&& i<b.size() && p<hs.size()&& j<k){ if(a.get(i)+b.get(i)<hs.get(p) && cal+2<=m){ //System.out.println("from if"+a.get(i)+" "+b.get(i)); sum+=a.get(i)+b.get(i); j++; all.add(a.get(i)); all.add(b.get(i)); a.remove(0); b.remove(0); cal=cal+2; } else{ //System.out.println("from if "+hs.get(i)); sum+=hs.get(p); all.add(hs.get(p)); hs.remove(0); cal++; j++; } } while(j<k && i<a.size() && i<b.size()){ //System.out.println(a.get(i)+" "+b.get(i)); j++; cal=cal+2; all.add(a.get(i)); all.add(b.get(i)); sum+=a.get(i)+b.get(i); a.remove(0); b.remove(0); } while(j<k && p<hs.size()){ j++; cal++; all.add(hs.get(p)); sum+=hs.get(p); hs.remove(0); } if(cal>=m){ System.out.println(sum); for(int z=0;z<all.size();z++){ System.out.print(pichi.indexOf(all.get(z))+1+" "); } return; } nun.addAll(a); nun.addAll(b); nun.addAll(hs); Collections.sort(nun); while(cal<m){ sum+=nun.get(0); all.add(nun.get(0)); nun.remove(0); cal++; } System.out.println(sum); for(int z=0;z<all.size();z++){ System.out.print(pichi.indexOf(all.get(z))+1+" "); } } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=list(map(int,input().split())) books=[] for i in range(n): x=list(map(int,input().split())) books.append(x) books=sorted(books,key=lambda x: (x[0],-(x[1]+x[2]))) print(books)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from sys import stdin def readline(): return stdin.readline() #tests = int(readline()) def solve(n, k, a): a.sort() al = 0 b = 0 res = [] resA = [] resB = [] for i in range(0, n): al += a[i][1] b += a[i][2] if a[i][1] == 1 and a[i][2] == 1: res.append(i) elif a[i][1] == 1: resA.append(i) elif a[i][2] == 1: resB.append(i) if al == k and b == k: break if al < k or b < k: return -1 if len(res) < k: res += resA[0:(k - len(res) // 2)] + resB[0:(k - len(res) // 2)] time = 0 for i in range(0, len(res)): time += a[res[i]][0] return time #for t in range(0, tests): #n = int(readline().rstrip("\n")) #s = readline().rstrip("\n") n, k = list(map(int, readline().rstrip("\n").split(' '))) a = [] for i in range(0, n): a.append(list(map(int, readline().rstrip("\n").split(' ')))) print(solve(n, k, a))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class E { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] strs = br.readLine().split("\\s+"); int n = Integer.parseInt(strs[0]); int k = Integer.parseInt(strs[1]); List<Integer> bothLike = new ArrayList<>(); List<Integer> aliceLike = new ArrayList<>(); List<Integer> bobLike = new ArrayList<>(); int aliceCount = 0; int bobCount = 0; for (int i = 0; i < n; i++) { strs = br.readLine().split("\\s+"); int t = Integer.parseInt(strs[0]); int a = Integer.parseInt(strs[1]); int b = Integer.parseInt(strs[2]); if (a == 1 && b == 1) { aliceCount++; bobCount++; bothLike.add(t); } else if (a == 1 && b == 0) { aliceCount++; aliceLike.add(t); } else if (a == 0 && b == 1) { bobCount++; bobLike.add(t); } } if (aliceCount < k || bobCount < k) { System.out.println(-1); } else { Collections.sort(bothLike); Collections.sort(aliceLike); Collections.sort(bobLike); int x = 0, y = 0, z = 0; int count = 0; int sum = 0; while (count < k && ((x < aliceLike.size() && y < bobLike.size()) || z < bothLike.size())) { if (x < aliceLike.size() && y < bobLike.size() && z < bothLike.size()) { if (aliceLike.get(x) + bobLike.get(y) <= bothLike.get(z)) { sum += aliceLike.get(x++) + bobLike.get(y++); count += 2; } else { sum += bothLike.get(z++); count++; } } else if (x < aliceLike.size() && y < bobLike.size() && z == bothLike.size()) { sum += aliceLike.get(x++) + bobLike.get(y++); count += 2; } else { sum += bothLike.get(z++); count++; } } System.out.println(sum); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, ans; multiset<int> alice, bob, both; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; if (a + b == 2) both.insert(t); else if (a) alice.insert(t); else if (b) bob.insert(t); } if (alice.size() + both.size() < k || bob.size() + both.size() < k) return cout << -1, 0; while (!both.empty() && !alice.empty() && !bob.empty()) { int fromAlice = *alice.begin(), fromBob = *bob.begin(), fromBoth = *both.begin(); if (fromBoth > fromAlice + fromBob) { alice.erase(fromAlice); bob.erase(fromBob); ans += fromAlice + fromBob; } else { both.erase(fromBoth); ans += fromBoth; } k--; } while (k && !both.empty()) { ans += *both.begin(); both.erase(both.begin()); k--; } while (k && !alice.empty() && !bob.empty()) { ans += *alice.begin() + *bob.begin(); alice.erase(alice.begin()); bob.erase(bob.begin()); k--; } cout << ans; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
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) if (a == 1 and b ==0 ): alice.append(t) if (a == 0 and b == 1): bob.append(t) alice.sort(); bob.sort() s = 0 for i in range(min(len(bob),len(alice))): both.append(bob[i] + alice[i]) if (len(both) < k): print(-1) else: print(sum(sorted(both[0:k])))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N = 3e2 + 5; int main() { ios_base::sync_with_stdio(false); long long n, k, one = 0, two = 0; cin >> n >> k; vector<long long> v1, v2, v3; for (int i = 0; i < n; i++) { long long x, y, z; cin >> x >> y >> z; if (y) one++; if (z) two++; if (y == 1 && z == 1) v1.push_back(x); else if (y == 1 && z == 0) v2.push_back(x); else if (y == 0 && x == 1) v3.push_back(x); } if (one < k || two < k) { cout << -1 << endl; return 0; } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long sum = 0, count1 = k, count2 = k, p = 0, q = 0, r = 0; int len1 = v1.size(), len2 = v2.size(), len3 = v3.size(); while (count1 > 0 || count2 > 0) { if (q >= len2 || r >= len3) { sum += v1[p]; p++; count1--; count2--; } else if (p < len1) { if (v1[p] <= (v2[q] + v3[r])) { sum += v1[p]; p++; count1--; count2--; } else { sum += (v2[q] + v3[r]); q++; r++; count2--; count1--; } } else { sum += (v2[q] + v3[r]); q++; r++; count2--; count1--; } } cout << sum; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; public class codechef{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int p,a,b; PriorityQueue<Integer> p1=new PriorityQueue<>(); PriorityQueue<Integer> p2=new PriorityQueue<>(); PriorityQueue<Integer> p3=new PriorityQueue<>(); for(int i=0;i<n;i++){ p=sc.nextInt(); a=sc.nextInt(); b=sc.nextInt(); if(a==0 && b==1) {p1.add(p);} if(a==1 && b==0) {p2.add(p);} if(a==1 && b==1) {p3.add(p);} } int ans=0; for(int i=0;i<k;i++){ if((p1.isEmpty() && p3.isEmpty()) || (p2.isEmpty() && p3.isEmpty())) {ans=-1; break;} if(!p1.isEmpty() && !p2.isEmpty() && !p3.isEmpty()) { if((p1.element()+p2.element())>p3.element()) {ans=ans+p3.remove();} else {ans=ans+p1.remove()+p2.remove(); } } else{ if(!p1.isEmpty() && !p2.isEmpty() && p3.isEmpty()) { ans=ans+p1.remove()+p2.remove(); } else{ if(p1.isEmpty() && p2.isEmpty() && !p3.isEmpty()) { ans=ans+p3.remove(); } } } } System.out.println(ans); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void buildsegment(unsigned long long int arr[], unsigned long long int tree[], unsigned long long int n) { for (unsigned long long int i = 0; i < n; i++) tree[n + i] = arr[i]; for (unsigned long long int i = n - 1; i > 0; --i) tree[i] = tree[i << 1] + tree[i << 1 | 1]; } void updatesegment(unsigned long long int p, unsigned long long int value, unsigned long long int tree[], unsigned long long int n) { tree[p + n] = value; p = p + n; for (unsigned long long int i = p; i > 1; i >>= 1) tree[i >> 1] = tree[i] + tree[i ^ 1]; } unsigned long long int querysegment(unsigned long long int l, unsigned long long int r, unsigned long long int tree[], unsigned long long int n) { unsigned long long int res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) res += tree[l++]; if (r & 1) res += tree[--r]; } return res; } long long int findpar(long long int numb, long long int par[]) { if (par[numb] == numb) return numb; else return par[numb] = findpar(par[numb], par); } void setpar(long long int u1, long long int v1, long long int par[]) { long long int s1 = findpar(v1, par); long long int s2 = findpar(u1, par); if (s1 < s2) { par[s2] = s1; } else { par[s1] = s2; } } unsigned long long int extended_GCD(unsigned long long int a, unsigned long long int b, unsigned long long int &x, unsigned long long int &y) { if (a == 0) { x = 0; y = 1; return b; } unsigned long long int x1, y1; unsigned long long int gcd = extended_GCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } unsigned long long int modinv(unsigned long long int a, unsigned long long int mod = 1000000007) { unsigned long long int x, y; extended_GCD(a, mod, x, y); if (x < 0) x += mod; return x; } unsigned long long int power(unsigned long long int a, unsigned long long int b, unsigned long long int m = 1000000007) { a %= m; unsigned long long int ans = 1; while (b > 0) { if (b & 1) ans = ans * a % m; a = a * a % m; b >>= 1; } return ans; } vector<unsigned long long int> nge(unsigned long long int arr[], unsigned long long int n) { stack<unsigned long long int> s; vector<unsigned long long int> ans(n, -1); s.push(0); for (unsigned long long int i = 1; i < n; i++) { if (s.empty()) { s.push(i); continue; } while (s.empty() == false && arr[s.top()] < arr[i]) { ans[s.top()] = i; s.pop(); } s.push(arr[i]); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); unsigned long long int n, k, m; cin >> n >> m >> k; vector<pair<unsigned long long int, unsigned long long int>> a, b, d; unsigned long long int n1, n2, n3, chos = 0; pair<unsigned long long int, unsigned long long int> arr[n]; for (unsigned long long int i = 0; i < n; i++) { cin >> n1 >> n2 >> n3; arr[i] = {n1, i}; if (n3 == n2 && n2 == 1) { d.push_back({n1, i}); } else if (n2 == 1) { a.push_back({n1, i}); } else if (n3 == 1) { b.push_back({n1, i}); } } vector<pair<unsigned long long int, unsigned long long int>> fa, fb; sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(d.begin(), d.end()); unsigned long long int x = min(a.size(), b.size()); map<unsigned long long int, unsigned long long int> mp; if (x > k) x = k; unsigned long long int ans = 0, dc = 0; if (x + d.size() >= k) { if (x < k) { for (unsigned long long int i = 0; i < x; i++) { ans += a[i].first + b[i].first; fa.push_back(a[i]); fb.push_back(b[i]); chos += 2; mp[a[i].second] = 1; mp[b[i].second] = 1; } for (unsigned long long int i = 0; i <= k - x - 1; i++) { ans += d[i].first; chos += 1; mp[d[i].second] = 1; } dc = k - x; } else { for (unsigned long long int i = 0; i < k; i++) { ans += a[i].first + b[i].first; fa.push_back(a[i]); fb.push_back(b[i]); chos += 2; mp[a[i].second] = 1; mp[b[i].second] = 1; } } unsigned long long int non = fa.size() - 1; if (x > 0) { while (dc < d.size()) { if (fa[non].first + fb[non].first > d[dc].first || chos > m) { ans -= fa[non].first + fb[non].first - d[dc].first; mp[fa[non].second] = 0; mp[fb[non].second] = 0; mp[d[dc].second] = 1; chos--; } else break; dc++; non--; if (non < 0) break; } } if (chos <= m) { sort(arr, arr + n); unsigned long long int count = 0; if (chos < m) { for (unsigned long long int i = 0; i < n; i++) { if (mp[arr[i].second] == 0) { ans += arr[i].first; count++; mp[arr[i].second] = 1; if (count + chos == m) break; } } } cout << ans << endl; for (auto vn : mp) { if (vn.second == 1) cout << vn.first + 1 << " "; } cout << endl; } else cout << -1 << endl; } else cout << -1 << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k = map(int, input().split()) oo = list() oa = list() ob = list() zz = list() for i in range(n): t,a,b = map(int, input().split()) if a == 1 and b == 1: oo.append((t,i)) elif a == 0 and b == 1: ob.append((t,i)) elif a == 1 and b == 0: oa.append((t,i)) else: zz.append((t,i)) oo = sorted(oo) oa = sorted(oa) ob = sorted(ob) oo_p = 0 oa_p = 0 ob_p = 0 ca = 0 cb = 0 ans = 0 ans_arr = list() MAX = 23942034809238409823048 def condition(k, loa, lob, loo, m): print(k, loa, lob, loo, m) if (max(k-loa, k-lob) > loo or max(k-loa, k-lob) > m): return False return True def get_first_elem_from_list(l, pos): if pos < len(l): return l[pos] else: return (MAX,-1) def remove_first_elem_from_list(l, pos): if len(l)>pos: pos += 1 return pos if not condition(k, len(oa), len(ob), len(oo), m): print("-1") exit(0) while ca < k or cb < k: oo_f = get_first_elem_from_list(oo, oo_p) oa_f = get_first_elem_from_list(oa, oa_p) ob_f = get_first_elem_from_list(ob, ob_p) print(oo_f, oa_f, ob_f) if ca < k and cb < k: if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2): if oo_f[0] == MAX: print("-1") exit(0) ca += 1 cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) else: ca += 1 cb += 1 ans+=oa_f[0]+ob_f[0] ans_arr.extend([oa_f[1], ob_f[1]]) oa_p = remove_first_elem_from_list(oa, oa_p) ob_p = remove_first_elem_from_list(ob, ob_p) elif ca < k: if oo_f[0] <= oa_f[0]: ca += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) else: ca += 1 ans+=oa_f[0] ans_arr.append(oa_f[1]) oa_p = remove_first_elem_from_list(oa, oa_p) else: if oo_f[0] <= ob_f[0]: cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) else: cb += 1 ans+=ob_f[0] ans_arr.append(ob_f[1]) ob_p = remove_first_elem_from_list(ob, ob_p) if len(ans_arr) < m: zz.extend(oo[oo_p:]) zz.extend(oa[oa_p:]) zz.extend(ob[ob_p:]) zz = sorted(zz) curr_size = len(ans_arr) for i in range(m-curr_size): ans += zz[i][0] ans_arr.append(zz[i][1]) assert len(ans_arr) == m print(ans) for i in ans_arr: print(i + 1, end =" ")
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
def solve(): n, k = list(map(int, input().split())) a = [] b = [] both = [] alice = 0 bob = 0 coincidence = 0 for i in range(n): t, a_, b_ = list(map(int, input().split())) if b_ and a_: both.append(t) coincidence += 1 elif a_ == 1: a.append(t) alice += 1 elif b_ == 1: b.append(t) bob += 1 if alice+coincidence < k or bob+coincidence<k: print(-1) else: both.sort() if coincidence>=k: out = sum(both[:k]) print(out) else: out = sum(both) k -= coincidence b.sort() out += sum(b[:k]) a.sort() out += sum(a[:k]) print(out) pop = 1 for test in range(pop): solve()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<long long int> v, a1, b1; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k; cin >> n >> k; pair<long long int, pair<long long int, long long int> > p[n]; for (long long int i = 0; i < n; i++) { long long int t, a, b; cin >> t >> a >> b; p[i] = {t, {a, b}}; } sort(p, p + n); long long int ans = 0; for (long long int i = 0; i < n; i++) { if (p[i].second.first == 1 && p[i].second.second == 1) { ans += p[i].first; v.push_back(p[i].first); k--; } if (k == 0) break; } long long int flaga = 0; long long int temp = 0; for (long long int i = 0; i < n; i++) { if (flaga == k) temp = 1; if (p[i].second.first == 1 && p[i].second.second == 0) { if (temp == 1) a1.push_back(p[i].first); else { ans += p[i].first; flaga++; } } } long long int flagb = 0; temp = 0; for (long long int i = 0; i < n; i++) { if (flagb == k) temp = 1; if (p[i].second.first == 0 && p[i].second.second == 1) { if (temp == 1) b1.push_back(p[i].first); else { ans += p[i].first; flagb++; } } } for (long long int i = 0; i < a1.size() && i < b1.size(); i++) { if (a1[i] + b1[i] < v[i]) { ans += a1[i] + b1[i] - v[i]; } else break; } if (flaga == k && flagb == k) cout << ans << "\n"; else cout << -1 << "\n"; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.AnnotatedArrayType; import java.lang.reflect.Array; import java.util.*; public class NTI { public static class pair implements Comparable<pair>{ int time; int ind; public pair(int t, int i){ this.time = t; this.ind = i; } @Override public int compareTo(pair o) { return this.time - o.time; } } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); ArrayList<pair> arr = new ArrayList<>(); LinkedList<pair> both = new LinkedList<>(); LinkedList<pair> alice = new LinkedList<>(); LinkedList<pair> bob = new LinkedList<>(); for(int i = 0; i < n; ++i){ st = new StringTokenizer(reader.readLine()); int t = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); arr.add(new pair(t,i + 1)); if(a == 1 && b == 1){ both.add(new pair(t,i + 1)); } else if(a == 1){ alice.add(new pair(t,i + 1)); } else if(b == 1){ bob.add(new pair(t,i + 1)); } } Collections.sort(arr); Collections.sort(both); Collections.sort(alice); Collections.sort(bob); if(both.size() + Math.min(alice.size(), bob.size()) < k){ writer.println(-1); writer.close(); return; } TreeSet<Integer> used = new TreeSet<>(); long ans = 0; for(int i = 0; i < k; ++i){ if(both.size() > 0 && alice.size() > 0 && bob.size() > 0){ if(both.peekFirst().time <= Math.max(alice.peekFirst().time, bob.peekFirst().time) || used.size() + 2 > m){ ans += both.peekFirst().time; used.add(both.peekFirst().ind); both.removeFirst(); } else{ ans += alice.peekFirst().time; used.add(alice.peekFirst().ind); alice.removeFirst(); ans += bob.peekFirst().time; used.add(bob.peekFirst().ind); bob.removeFirst(); } } else if(both.size() > 0){ ans += both.peekFirst().time; used.add(both.peekFirst().ind); both.removeFirst(); } else if(used.size() + 2 <= m){ ans += alice.peekFirst().time; used.add(alice.peekFirst().ind); alice.removeFirst(); ans += bob.peekFirst().time; used.add(bob.peekFirst().ind); bob.removeFirst(); } else{ writer.println(-1); writer.close(); return; } } for(int i = 0; i < n && used.size() < m; ++i){ if(!(used.contains(arr.get(i).ind))){ ans += arr.get(i).time; used.add(arr.get(i).ind); } } writer.println(ans); StringBuilder s = new StringBuilder(); for(Integer ii : used){ s.append(ii); s.append(" "); } writer.println(s); writer.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
//Bismillahir Rahmanir Rahim// #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define fast ios_base::sync_with_stdio(false);cin.tie(NULL) #define in1(X) scanf("%lli",&(X)) #define ins(S) scanf("%s",(S)) #define out(X) printf("%lli",(X)) #define outs(S) printf("%s",(S)) #define printnl printf("\n") #define End return 0 #define ll long long int #define Pair pair<ll,ll> #define F first #define S second #define all(v) v.begin(),v.end() #define mem(a,v) memset(a,v,sizeof(a)) #define Max 1000000000000000014 #define Min -1000000000000000014 #define mod 998244353 #define range 1000007 #define pb push_back #define Vector vector<ll> #define mp make_pair #define PI 3.1415926535897 #define min_heap priority_queue <ll, vector<ll>, greater<ll> > template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;//*(x.find_by_order(i)),x.order_of_key(k) /*************** #Template FUNCTIONS# **************/ ll pc;void PrintCase(){printf("Case %lli:\n",++pc);} ll bigmod(ll n,ll p){if(p==0) return 1;if(p==1)return (n)%mod;if(p%2)return (bigmod(n,p-1)*n)%mod;else{ll x=bigmod(n,p/2);return (x*x)%mod;}} ll power(ll n,ll p){if(p==0) return 1;if(p==1)return n;if(p%2)return power(n,p-1)*n;else{ll x=power(n,p/2);return x*x;}} ll modinverse(ll n){return bigmod(n,mod-2)%mod;} ll Check(ll n,ll i){return (n&(1LL<<i));} ll Set(ll n,ll i){return (n|(1LL<<i));} ll status[(ll)range/64+5];Vector primes;void sieve(){for(ll i=3;i<=sqrt(range);i+=2){if(Check(status[i/64],i%64)==0){for(ll j=i*i;j<range;j+=2*i){status[j/64]=Set(status[j/64],j%64);}}}primes.pb(2);for(ll i=3;i<range;i+=2)if(Check(status[i/64],i%64)==0)primes.pb(i);} double start_time,finish_time;void cnt_time(){start_time=clock();}void print_time(){finish_time=(clock()-start_time)/double(CLOCKS_PER_SEC)*1000;printf("Execution Time: %.2f ms\n",finish_time);} ll dx[] = {-2, -1, 1, 2, 2, 1, -1, -2}; ll dy[] = { 1, 2, 2, 1, -1, -2, -2, -1}; ll X[]= {0,0,1,-1,1,1,-1,-1}; ll Y[]= {1,-1,0,0,1,-1,1,-1}; /********************** START **********************/ int main() { ll n,m,p,a=0,b=0; vector<Pair> f,s,o,e; in1(n);in1(m);in1(p); for(ll i=1;i<=n;i++){ ll t,x,y; in1(t);in1(x);in1(y); if(x && y) o.pb(mp(t,i)); else if(x) f.pb(mp(t,i)); else if(y) s.pb(mp(t,i)); else e.pb(mp(t,i)); } sort(all(o)); sort(all(f)); sort(all(s)); sort(all(e)); ll t=0,fs=f.size(),ss=s.size(),os=o.size(),es=e.size(),i=0,j=0,k=0,l=0; for(i=0,j=0,k=0;a<p || b<p;){ if(i==fs && j==ss && k==os) break; if(a!=p && b!=p && k<os){ if(i==fs || j==ss){ t+=o[k].F; k++; a++,b++; } else if(f[i].F+s[j].F>o[k].F){ t+=o[k].F; k++; a++,b++; } else{ t+=(f[i].F+s[j].F); i++,j++; a++,b++; } } else{ while(a<p && i<fs){ t+=f[i].F; a++; i++; } while(b<p && j<ss){ t+=s[j].F; b++; j++; } break; } } if(a<p || b<p){ printf("-1\n"); End; } while(i+j+k>m && k<o.size() && i && j){ i--,j--; t+=(o[k].F-f[i].F-s[j].F); k++; } if(i+j+k>m){ printf("-1\n"); End; } while(i+j+k+l<m){ ll x=Max; if(i<fs) x=min(x,f[i].F); if(j<ss) x=min(x,s[j].F); if(k<os) x=min(x,o[k].F); if(l<es) x=min(x,e[l].F); t+=x; if(k<os && o[k].F==x) k++,a++,b++; else if((i<fs && f[i].F==x) || (j<ss && s[j].F==x)){ if(i<fs && j<ss && f[i].F==s[j].F){ if(a<=b) i++,a++; else j++,b++; } else if(i<fs && f[i].F==x) i++,a++; else j++,b++; } else l++; } while(l<es){ ll x=-1,id=-1; if(k && a>p && b>p && o[k-1].F>=e[l].F && o[k-1].F>=x) x=o[k-1].F,id=1; if(i && a>p && f[i-1].F>=e[l].F && f[i-1].F>=x) x=f[i-1].F,id=2; if(j && b>p && s[j-1].F>=e[l].F && s[j-1].F>=x) x=s[j-1].F,id=3; if(x==-1) break; t+=(e[l].F-x); l++; if(id==1) k--,a--,b--; if(id==2) i--,a--; if(id==3) j--,b--; } if(n==19683){ cout<<f[i].F<<" "<<s[j].F<<" "<<o[k].F<<" "<<e[l].F<<endl; cout<<f[i-1].F<<" "<<s[j-1].F<<" "<<o[k-1].F<<" "<<e[l-1].F<<endl; } out(t);printnl; while(i--) cout<<f[i].S<<" "; while(j--) cout<<s[j].S<<" "; while(k--) cout<<o[k].S<<" "; while(l--) cout<<e[l].S<<" "; End; } /***************** ALHAMDULILLAH *****************/
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; public final class ReadingBooksHard { private static final FastReader fr = new FastReader(); public static void main(String[] args) { final int n = fr.nextInt(), m = fr.nextInt(), k = fr.nextInt(); final Book[] books = new Book[n]; for (int i = 0; i < n; i++) { books[i] = new Book(i, fr.nextInt(), fr.nextInt() == 1, fr.nextInt() == 1); } final Book[] ans = new ReadingBooksHard(books, m, k).solve(); System.out.println(ans == null ? -1 : Arrays.stream(ans).mapToInt(b -> b.time).sum()); if(ans != null) { final StringBuilder sb = new StringBuilder(); for (Book b : ans) { sb.append(b.index + 1).append(' '); } System.out.println(sb); } } private final Book[] books; private final int m, k; public ReadingBooksHard(Book[] books, int m, int k) { this.books = books; this.m = m; this.k = k; } public Book[] solve() { final Map<Interest, List<Book>> map = Arrays.stream(books).collect(Collectors.groupingBy(b -> b.interest)); for (Interest interest : Interest.values()) { List<Book> books = map.get(interest); if(books == null) map.put(interest, Collections.emptyList()); else books.sort(null); } final Book[] template = new Book[0]; final Book[] alices = map.get(Interest.ALICE).toArray(template), bobs = map.get(Interest.BOB).toArray(template), commons = map.get(Interest.COMMON).toArray(template), nons = map.get(Interest.NON).toArray(template); int ai = 0, bi = 0, ci = 0, ni = 0; int bestTimeForK = Integer.MAX_VALUE; List<Book> bestForK = Collections.emptyList(); for (int commonCount = k; commonCount >= 0; commonCount--) { int aai = 0, bbi = 0, cci = 0; if (commons.length < commonCount || alices.length < k - commonCount || bobs.length < k - commonCount) continue; final List<Book> chosen = new ArrayList<>(commonCount + 2 * (k - commonCount)); for (int i = 0; i < commonCount; i++) { chosen.add(commons[cci++]); } for (int i = 0; i < k - commonCount; i++) { chosen.add(alices[aai++]); chosen.add(bobs[bbi++]); } final int time = chosen.stream().mapToInt(b -> b.time).sum(); if (time < bestTimeForK) { bestTimeForK = time; bestForK = chosen; ai = aai; bi = bbi; ci = cci; } } final List<Book> books = new ArrayList<>(m + k); books.addAll(bestForK); while (books.size() != m) { if (books.size() < m) { // Common to separates or add non Integer commonToSeparateChange, addNonChange; if (ci == 0 || ai == alices.length || bi == bobs.length) commonToSeparateChange = null; else { Book common = commons[ci - 1], alice = alices[ai], bob = bobs[bi]; commonToSeparateChange = alice.time + bob.time - common.time; } if (ni == nons.length) addNonChange = null; else { Book non = nons[ni]; addNonChange = non.time; } if (commonToSeparateChange != null && addNonChange != null) { if(commonToSeparateChange < addNonChange) { books.remove(commons[--ci]); books.add(alices[ai++]); books.add(bobs[bi++]); } else { books.add(nons[ni++]); } } else if (commonToSeparateChange != null) { books.remove(commons[--ci]); books.add(alices[ai++]); books.add(bobs[bi++]); } else if (addNonChange != null) { books.add(nons[ni++]); } else return null; } else { // Separates to common if (ai == 0 || bi == 0 || ci == commons.length) return null; books.remove(alices[--ai]); books.remove(bobs[--bi]); books.add(commons[ci++]); } } return books.toArray(template); } private enum Interest { COMMON, ALICE, BOB, NON; } private static final class Book implements Comparable<Book> { public final int time, index; public final Interest interest; public Book(final int index, int time, boolean alice, boolean bob) { this.index = index; this.time = time; if (alice && bob) interest = Interest.COMMON; else if (alice) interest = Interest.ALICE; else if (bob) interest = Interest.BOB; else interest = Interest.NON; } @Override public int compareTo(Book o) { return Integer.compare(this.time, o.time); } } private static final class FastReader { private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer st; public String nextLine() { try { return br.readLine(); } catch (IOException ex) { throw new RuntimeException(ex); } } public String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python2
import atexit, io, sys # A stream implementation using an in-memory bytes # buffer. It inherits BufferedIOBase. buffer = io.BytesIO() sys.stdout = buffer # print via here @atexit.register def write(): sys.__stdout__.write(buffer.getvalue()) for _ in range(1): n,k=map(int,raw_input().split()) v=[] t=[] for i in range(n): vc=map(int,raw_input().split()) if vc[1] or vc[2]: v.append(vc) s=sorted(v) ac,bc,at,bt=0,0,0,0 a,b=[],[] for i in v: if i[1]!=1 or i[2]!=1: if i[1]==1: ac+=1 a.append(i[0]) else: bc+=1 b.append(i[0]) else: t.append(i[0]) if len(t)>=k: p,g=0,k-1 ca=sum(t[:k]) while( p<len(a) and p<len(b) and g>=0): if (a[p]+b[p])<=t[g]: ca=ca-t[g]+a[p]+b[p];p+=1;g-=[1] else: break print ca elif( len(a)+len(t))<k or (len(b)+len(t)) <k: print -1;continue else : f=k-len(t) h=k-len(t) ca=sum(a[:f])+sum(b[:f])+sum(t) p,g=f,k-1 while( p<len(a) and p<len(b) and g>=0): if (a[p]+b[p])<=t[g]: ca=ca-t[g]+a[p]+b[p];p+=1;g-=[1] else: break print ca
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python2
import sys import math from collections import defaultdict n,k = list(map(int, sys.stdin.readline().strip().split(' '))) t = [] a = [] b = [] for _ in range(n): ti,ai,bi = list(map(int, sys.stdin.readline().strip().split(' '))) t.append(ti) a.append(ai) b.append(bi) alice_only = [] alice = 0 bob = 0 bob_only = [] taken = [0 for i in range(n)] ts = sorted(enumerate(t), key=lambda x: x[1]) for i,ti in ts: if a[i] and b[i]: if alice == k and bob == k: if alice_only and bob_only: if ti < alice_only[-1] + bob_only[-1]: taken[alice_only.pop()] = 0 taken[bob_only.pop()] = 0 taken[i] = 1 continue if alice == k: if alice_only: j = alice_only.pop() taken[j] = 0 else: alice += 1 if bob == k: if bob_only: j = bob_only.pop() taken[j] = 0 else: bob += 1 taken[i] = 1 continue if a[i] and alice < k: alice += 1 alice_only.append(ti) taken[i] = 1 if b[i] and bob < k: bob += 1 bob_only.append(ti) taken[i] = 1 if alice != k or bob != k: print(-1) else: ans = 0 for i,ti in ts: if taken[i]: ans += ti print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define int long long int #define M 1000000007 #define pb push_back #define pf push_front #define ulli unsigned long long int #define what_is(x) cerr << #x << " is " << x << endl; #define F first #define S second #define endl '\n' using namespace std; using namespace __gnu_pbds; const double pi = acos(-1.0); const int INF = 1e19L; const int LG = 20; const int N = 1e5+9; const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; //bool ok(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; } //newX = x + dx[i]; newY = y + dy[i]; typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; int power(int x,int y){if(x==0)return 0;int res=1;while(y>0){if(y%2)res=(res*x)%M;x=(x*x)%M;y=y/2;}return res;} bool isvowel(char c){if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')return true;return false;} deque<int> a,b,c,d; int n,k; int32_t main() { //cout.precision(15); //cout.setf(ios::fixed,ios::floatfield); ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int T=1; //cin>>T; while(T--) { int i,t,x,y,z,ans=0,ca=0,cb=0; cin>>n>>k; for(i=0;i<n;i++) { cin>>t>>x>>y; if(x and y) c.pb(t); else if(x) a.pb(t); else if(y) b.pb(t); } if((c.size()+a.size()<k) and (c.size()+b.size()<k)) { cout<<-1<<endl; continue; } sort(a.begin(),a.end()); sort(b.begin(),b.end()); sort(c.begin(),c.end()); while(ca<k or cb<k) { if(a.empty() or b.empty()) { ans+=c.front(); c.pop_front(); } else if(c.empty()) { ans+=a.front(); ans+=b.front(); a.pop_front(); b.pop_front(); } else { x=a.front(),y=b.front(),z=c.front(); if(z<=x+y) { ans+=z; c.pop_front(); } else { ans+=a.front(); ans+=b.front(); a.pop_front(); b.pop_front(); } } ca++,cb++; } cout<<ans<<endl; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.PriorityQueue; import java.io.BufferedWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.AbstractCollection; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Hello */ 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); E2ReadingBooksHardVersion solver = new E2ReadingBooksHardVersion(); solver.solve(1, in, out); out.close(); } static class E2ReadingBooksHardVersion { public void solve(int testNumber, InputReader c, OutputWriter w) { int n = c.readInt(), m = c.readInt(), k = c.readInt(); ArrayList<Pair<Integer, Integer>> a = new ArrayList<>(), b = new ArrayList<>(); PriorityQueue<Pair<Integer, Pair<Integer, Integer>>> tot = new PriorityQueue<>(); ArrayList<Pair<Integer, Integer>> whoCare = new ArrayList<>(); HashSet<Integer> resInd = new HashSet<>(); for (int i = 0; i < n; i++) { int t = c.readInt(), aa = c.readInt(), bb = c.readInt(); if (aa == 0 || bb == 0) { if (aa == 1) { a.add(new Pair<>(t, i)); } else if (bb == 1) { b.add(new Pair<>(t, i)); } else { whoCare.add(new Pair<>(t, i)); } } else { tot.add(new Pair<>(t, new Pair<>(0, i))); } } Collections.sort(a); Collections.sort(b); HashMap<Integer, Pair<Pair<Integer, Integer>, Pair<Integer, Integer>>> kk = new HashMap<>(); int id = 1; for (int i = 0; i < a.size() && i < b.size(); i++) { Pair<Integer, Integer> a1 = a.get(i), b1 = b.get(i); tot.add(new Pair<>(a1.first + b1.first, new Pair<>(id, -1))); kk.put(id, new Pair<>(a1, b1)); id++; } for (int i = a.size(); i < b.size(); i++) { whoCare.add(b.get(i)); } for (int i = b.size(); i < a.size(); i++) { whoCare.add(a.get(i)); } if (tot.size() < k) { w.printLine("-1"); return; } long res = 0; int tot_book = 0; PriorityQueue<Pair<Integer, Integer>> customize = new PriorityQueue<>(Collections.reverseOrder()); while (k-- > 0) { Pair<Integer, Pair<Integer, Integer>> pp = tot.poll(); res += pp.first; if (pp.second.first != 0) { tot_book += 2; customize.add(new Pair<>(pp.first, pp.second.first)); Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> pkp = kk.get(pp.second.first); resInd.add(pkp.first.second); resInd.add(pkp.second.second); } else { tot_book++; resInd.add(pp.second.second); } } if (tot_book > m) { PriorityQueue<Pair<Integer, Integer>> pp = new PriorityQueue<>(); while (!tot.isEmpty()) { Pair<Integer, Pair<Integer, Integer>> ppq = tot.poll(); if (ppq.second.first == 0) { pp.add(new Pair<>(ppq.first, ppq.second.second)); } } while (!pp.isEmpty()) { Pair<Integer, Integer> kt = pp.poll(); Pair<Integer, Integer> rem = customize.poll(); res -= rem.first; Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> klk = kk.get(rem.second); resInd.remove(klk.first.second); resInd.remove(klk.second.second); res += kt.first; resInd.add(kt.second); tot_book--; if (tot_book == m) { break; } } } else if (tot_book == m) { } else { while (!tot.isEmpty()) { Pair<Integer, Pair<Integer, Integer>> pk = tot.poll(); if (pk.second.first == 0) { whoCare.add(new Pair<>(pk.first, pk.second.second)); } else { Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> ll = kk.get(pk.second.first); whoCare.add(ll.first); whoCare.add(ll.second); } } Collections.sort(whoCare); //verify once for (int i = 0; i < whoCare.size() && tot_book < m; i++, tot_book++) { res += whoCare.get(i).first; resInd.add(whoCare.get(i).second); } } if (tot_book == m) { w.printLine(res); for (int xx : resInd) { w.print(xx + 1, ""); } w.printLine(); } else { w.printLine("-1"); } } } 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 readInt() { 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); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine() { writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } static class Pair<U, V> implements Comparable<Pair<U, V>> { public final U first; public final V second; public Pair(U first, V second) { this.first = first; this.second = second; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null); } public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) first).compareTo(o.first); if (value != 0) { return value; } return ((Comparable<V>) second).compareTo(o.second); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; public class E2 { static final boolean RUN_TIMING = false; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public void go() throws IOException { // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1 << 20); // out = new PrintWriter(new FileWriter(new File("output.txt"))); int n = ipar(); int m = ipar(); int k = ipar(); ArrayList<ArrayList<int[]>> books = new ArrayList<>(); for (int i = 0; i < 4; i++) { books.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int t = ipar(); int a = ipar(); int b = ipar(); books.get(a*2+b).add(new int[]{t, i}); } for (int i = 0; i < 4; i++) { Collections.sort(books.get(i), this::compare); } int sum1 = 0, sum2 = 0, sum3 = 0; int index1 = -1, index2 = -1; for (int i = 0; i < k; i++) { if (index1+1 < books.get(1).size()) { index1++; sum1 += books.get(1).get(index1)[0]; } if (index2+1 < books.get(2).size()) { index2++; sum2 += books.get(2).get(index2)[0]; } } PriorityQueue<int[]> candidates = new PriorityQueue<>(this::compare); for (int i = k; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = k; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } TreeSet<int[]> free = new TreeSet<>(this::compare); int freeSum = 0; while (index1+index2+2+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+2+free.size() > m && !free.isEmpty()) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, -1); int best = Integer.MAX_VALUE; int bestIndex = -2; if (index1+index2+2+free.size() == m && index1+1 == k && index2+1 == k) { best = sum1+sum2+freeSum; bestIndex = -1; } int index3 = -1; while (index3+1 < books.get(3).size()) { index3++; sum3 += books.get(3).get(index3)[0]; if (index1 >= 0 && index1+index3+2 > k) { sum1 -= books.get(1).get(index1)[0]; candidates.add(books.get(1).get(index1)); index1--; } if (index2 >= 0 && index2+index3+2 > k) { sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(2).get(index2)); index2--; } while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } if (index1+index2+index3+3+free.size() == m && index1+index3+2 == k && index2+index3+2 == k && sum1+sum2+sum3+freeSum < best) { best = sum1+sum2+sum3+freeSum; bestIndex = index3; } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); } sum1 = sum2 = sum3 = 0; index1 = index2 = -1; for (int i = 0; i < k; i++) { if (index1+1 < books.get(1).size()) { index1++; sum1 += books.get(1).get(index1)[0]; } if (index2+1 < books.get(2).size()) { index2++; sum2 += books.get(2).get(index2)[0]; } } candidates.clear(); for (int i = k; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = k; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } free.clear(); freeSum = 0; while (index1+index2+2+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+2+free.size() > m && !free.isEmpty()) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); index3 = -1; while (index3+1 <= bestIndex) { index3++; sum3 += books.get(3).get(index3)[0]; if (index1 >= 0 && index1+index3+2 > k) { sum1 -= books.get(1).get(index1)[0]; candidates.add(books.get(1).get(index1)); index1--; } if (index2 >= 0 && index2+index3+2 > k) { sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(2).get(index2)); index2--; } while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); } if (best == Integer.MAX_VALUE) { out.println(-1); return; } out.println(best); for (int i = 0; i <= index1; i++) { out.print(books.get(1).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index2; i++) { out.print(books.get(2).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index3; i++) { out.print(books.get(3).get(i)[1]+1); out.print(" "); } for (int[] f : free) { out.print(f[1]+1); out.print(" "); } out.println(); } public int compare(int[] a, int[] b) { if (a[0] == b[0]) { return a[1] - b[1]; } return a[0] - b[0]; } public int ipar() throws IOException { return Integer.parseInt(spar()); } public int[] iapar(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() throws IOException { return Long.parseLong(spar()); } public long[] lapar(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() throws IOException { return Double.parseDouble(spar()); } public String spar() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char)c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String linepar() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char)c; len++; } return new String(inputBuffer, 0, len); } public boolean haspar() throws IOException { String line = linepar(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public static void main(String[] args) throws IOException { long time = 0; time -= System.nanoTime(); new E2().go(); time += System.nanoTime(); if (RUN_TIMING) { System.out.printf("%.3f ms%n", time / 1000000.0); } out.flush(); in.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) elev = False if k == 254: elev = True all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x - 1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: i[1] = 1 i[2] = 1 none.append(i) else: i[1] = 0 i[2] = 0 Both.append(i) else: if i[1] == 0: i[1] = 1 i[2] = 0 Bob.append(i) else: i[1] = 0 i[2] = 1 Alice.append(i) z += 1 n -= 1 Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) #print('Alice') #print(Alice) #print('Alice') #print('Bob') #print(Bob) #print('Bob') #print('Both') #print(Both) #print('Both') #print('none') #print(none) #print('none') if elev: print('Alice1 = ' + str(len(Alice))) print('Bob1 = ' + str(len(Bob))) print('Both1 = ' + str(len(Both))) print('none1 = ' + str(len(none))) if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] if elev: print('Both2 = ' + str(len(Both))) print('tresult = ' + str(len(tresult))) resulta = [] resultb = [] if k > 0: aaa = Alice + Both aaa.sort(key=lambda x: (x[0],x[2])) if len(aaa) >= k: resulta = aaa[:k] else: print(-1) exit() col_totals1 = [sum(x) for x in zip(*resulta)] yy = col_totals1[2] xx = k - yy #Both = Both[xx:] #Alice = Alice[yy:] #k = k - xx if elev: print('xx, yy = ' + str(xx) + ', ' + str(yy)) print('resulta = ' + str(len(resulta))) print('Both3 = ' + str(len(Both))) print('Alice2 = ' + str(len(Alice))) print('k = ' + str(k)) #if k > 0: bbb = Bob + Both bbb.sort(key=lambda x: (x[0], x[1])) if len(bbb) >= k: resultb = bbb[:k] else: print(-1) exit() col_totals2 = [sum(x) for x in zip(*resultb)] yyy = col_totals2[1] xxx = k - yyy if elev: print('xxx, xyy = ' + str(xxx) + ', ' + str(yyy)) print('resultb = ' + str(len(resultb))) print('Both4 = ' + str(len(Both))) print('Bob2 = ' + str(len(Bob))) if max(xx, xxx) == xx: resultb = [] Both = Both[xx:] Alice = Alice[yy:] k = k - xx if k > 0: bbb = Bob + Both bbb.sort(key=lambda x: (x[0], x[1])) if len(bbb) >= k: resultb = bbb[:k] else: print(-1) exit() col_totals2 = [sum(x) for x in zip(*resultb)] yyy = col_totals2[1] xxx = k - yyy Both = Both[xxx:] Bob = Bob[yyy:] else: resulta = [] Both = Both[xxx:] Bob = Bob[yyy:] k = k -xxx if k > 0: aaa = Alice + Both aaa.sort(key=lambda x: (x[0],x[2])) if len(aaa) >= k: resulta = aaa[:k] else: print(-1) exit() col_totals2 = [sum(x) for x in zip(*resulta)] yy = col_totals2[2] xx = k - yy Both = Both[xx:] Alice = Alice[yy:] resulta.sort(key=lambda x: (x[2],x[0])) resultb.sort(key=lambda x: (x[1],x[0])) if elev: Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) print('xx, yy = ' + str(xx) + ', ' + str(yy)) print('xxx, yyy = ' + str(xxx)+', ' + str(yyy)) print('resultb = ' + str(len(resultb))) print('resulta = ' + str(len(resulta))) print('Bothf = ' + str(len(Both))) print('Bobf = ' + str(len(Bob))) print('Alicf = ' + str(len(Alice))) corr = [] while len(resulta) and len(resultb) and len(Both) and len(none) and resulta[-1][0] + resultb[-1][0] > Both[0][0]+none[0][0]: Alice.append(resulta[-1]) Bob.append(resultb[-1]) corr.append(Both[0]) corr.append(none[0]) resulta.pop(-1) resultb.pop(-1) Both.pop(0) none.pop(0) q = len(resultb) + len(resulta) + len(corr) q = m - q if elev: Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) print('xx, yy = ' + str(xx) + ', ' + str(yy)) print('xxx, yyy = ' + str(xxx)+', ' + str(yyy)) print('resultb = ' + str(len(resultb))) print('resulta = ' + str(len(resulta))) print('Bothf = ' + str(len(Both))) print('Bobf = ' + str(len(Bob))) print('Alicf = ' + str(len(Alice))) print('corr = ' + str(len(corr))) print(Alice[0]) print(Bob[0]) print(Both[0]) print(none[0]) print(none[q+1]) All = Both + Alice + Bob + none All.sort(key=lambda x: x[0]) if elev: print('q = ' + str(q)) print('All = ' + str(len(All))) result = All[:q] result = resulta + resultb + result + tresult + corr result.sort(key=lambda x: x[0]) print(sum(row[0] for row in result)) print(' '.join([str(row[3]) for row in result]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# from math import log,floor # from mymodule import input n,k = map(int,input().split()) l1 = [] l2 = [] for i in range(n): a,b,c = map(int,input().split()) if b!=0: l1.append(a) if c!=0: if b!=0: l2.append(0) else: l2.append(a) if len(l1)>=k and len(l2)>=k: l1.sort() l2.sort() print(sum(l1[0:k])+sum(l2[0:k])) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
/*---------------Go Code GO---------------*/ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using Order_Set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //(order) Set.order_of_key(); (pointer) Set.find_by_order(); #define PI acos(-1.0) #define O_O \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define precision(a) fixed << setprecision(a) #define endl '\n' #define Flush cout << flush #define LLMX 0x3fffffffffffffff void Solution(); int main() { // O_O; long long tst = 1; //cin >> tst; while (tst--) { Solution(); } return 0; } void Solution() { long long N, M, K; vector<pair<long long, long long>> both, ali, bob, none; cin >> N >> M >> K; long long timeBit[N + 2] = {0}, indexBit[N + 2] = {0}; int likedAlice, likedBob, t, bitIndex; for (int i = 1; i <= N; i++) { cin >> t >> likedAlice >> likedBob; if (likedAlice && likedBob) both.push_back({t, i}); else if (likedAlice) timeBit[i] = 1; else if (likedBob) timeBit[i] = 2; if (likedAlice + likedBob < 2) none.push_back({t, i}); } both.push_back({0, 0}); none.push_back({0, 0}); sort(both.begin(), both.end()); sort(none.begin(), none.end()); for (int i = 1; i < both.size(); i++) { both[i].first += both[i - 1].first; } ali.push_back({0, 0}); bob.push_back({0, 0}); for (int i = 1; i < none.size(); i++) { if (timeBit[none[i].second] == 1) ali.push_back({none[i].first + ali.back().first, i}); else if (timeBit[none[i].second] == 2) bob.push_back({none[i].first + bob.back().first, i}); timeBit[none[i].second] = 0; } for (int i = 1; i < none.size(); i++) { bitIndex = i; //cout << i << ": "; while (bitIndex < none.size()) { // cout << bitIndex << ' '; timeBit[bitIndex] += none[i].first; bitIndex += (bitIndex & -bitIndex); } //cout << endl; } int fromBoth, fromInd, others, othersSum; int resFromBoth, resFromInd, resFromOthers; bool isPossible = false; long long ans = LLMX; // if (N == 2187 && M == 78) //k 42 // cout << none.size() << ' ' << ali.size() << ' ' << bob.size() << ' ' << both.size() << endl; //1004 79 878 1185 for (int i = M; i >= 0; i--) { fromBoth = i; fromInd = max(0LL, K - i); others = M - 2 * fromInd - fromBoth; // cout << fromBoth << ' ' << fromInd << ' ' << others << endl; if (others < 0 || others >= none.size() || fromInd >= min(ali.size(), bob.size()) || fromBoth >= both.size()) continue; bitIndex = ali[fromInd].second; while (bitIndex < none.size() && bitIndex > 0) { //cout << "update Ali " << bitIndex << endl; indexBit[bitIndex]++; timeBit[bitIndex] -= none[ali[fromInd].second].first; bitIndex += (bitIndex & -bitIndex); } bitIndex = bob[fromInd].second; while (bitIndex < none.size() && bitIndex > 0) { //cout << "update Bob" << endl; indexBit[bitIndex]++; timeBit[bitIndex] -= none[bob[fromInd].second].first; bitIndex += (bitIndex & -bitIndex); } int lw = others, hi = none.size() - 1, mid, found = 0; while (lw <= hi) { mid = (hi + lw) >> 1; bitIndex = mid; long long sum = 0; while (bitIndex > 0) { //cout << "update others" << endl; sum += indexBit[bitIndex]; bitIndex -= (bitIndex & -bitIndex); } //cout << hi << ' ' << lw << ' ' << sum << ' ' << mid << endl; if (mid - sum < others) lw = mid + 1; else if (mid - sum > others) hi = mid - 1; if (mid - sum == others) { others = mid; found = 1; break; } } // if (N == 81 && M == 81) // cout << mid << endl; // cout << others << endl; if (found == 0) continue; bitIndex = others; othersSum = 0; while (bitIndex > 0) { //cout << "update sum" << endl; othersSum += timeBit[bitIndex]; bitIndex -= (bitIndex & -bitIndex); } long long total = both[fromBoth].first + ali[fromInd].first + bob[fromInd].first + othersSum; // cout << total << endl; if (ans > total) { isPossible = true; ans = total; resFromBoth = fromBoth; resFromInd = fromInd; resFromOthers = others; //cout << resFromBoth << ' ' << resFromInd << ' ' << resFromOthers << endl; } } if (isPossible == false) { cout << -1 << endl; return; } cout << ans << endl; for (int i = 1; i <= resFromBoth; i++) cout << both[i].second << ' '; for (int i = 1; i <= resFromInd; i++) { cout << none[ali[i].second].second << ' ' << none[bob[i].second].second << ' '; none[ali[i].second].second = 0; none[bob[i].second].second = 0; } for (int i = 1; i <= resFromOthers; i++) if (none[i].second != 0) cout << none[i].second << ' '; cout << endl; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k = map(int, input().split()) oo = list() oa = list() ob = list() zz = list() for i in range(n): t,a,b = map(int, input().split()) if a == 1 and b == 1: oo.append((t,i)) elif a == 0 and b == 1: ob.append((t,i)) elif a == 1 and b == 0: oa.append((t,i)) else: zz.append((t,i)) oo = sorted(oo) oa = sorted(oa) ob = sorted(ob) oo_p = 0 oa_p = 0 ob_p = 0 ca = 0 cb = 0 ans = 0 ans_arr = list() MAX = 23942034809238409823048 def condition(ko, loa, lob, loo, mo): if max(0, max(ko-loa, ko-lob)) > loo or max(0, max(ko-loa, ko-lob)) > mo: return False return True def get_first_elem_from_list(l, pos): if pos < len(l): return l[pos] else: return (MAX,-1) def remove_first_elem_from_list(l, pos): if len(l)>pos: pos += 1 return pos if not condition(k, len(oa), len(ob), len(oo), m): print("-1") exit(0) c = 0 while ca < k or cb < k: oo_f = get_first_elem_from_list(oo, oo_p) oa_f = get_first_elem_from_list(oa, oa_p) ob_f = get_first_elem_from_list(ob, ob_p) c += 1 if ca < k and cb < k: if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2): if oo_f[0] == MAX: print("-1") exit(0) if n == 19683 and m == 507 and k == 254 and c > 132: print(oo_f[0], oa_f[0], ob_f[0], c) ca += 1 cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] + ob_f[0] < oo_f[0]: ca += 1 cb += 1 ans+=oa_f[0]+ob_f[0] ans_arr.extend([oa_f[1], ob_f[1]]) oa_p = remove_first_elem_from_list(oa, oa_p) ob_p = remove_first_elem_from_list(ob, ob_p) elif ca < k: if oo_f[0] <= oa_f[0]: ca += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] < oo_f[0]: ca += 1 ans+=oa_f[0] ans_arr.append(oa_f[1]) oa_p = remove_first_elem_from_list(oa, oa_p) else: if oo_f[0] <= ob_f[0]: cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif ob_f[0] < oo_f[0]: cb += 1 ans+=ob_f[0] ans_arr.append(ob_f[1]) ob_p = remove_first_elem_from_list(ob, ob_p) if len(ans_arr) < m: zz.extend(oo[oo_p:]) zz.extend(oa[oa_p:]) zz.extend(ob[ob_p:]) zz = sorted(zz) curr_size = len(ans_arr) for i in range(m-curr_size): ans += zz[i][0] ans_arr.append(zz[i][1]) print(ans) assert len(ans_arr) == m for i in ans_arr: print(i + 1, end =" ")
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; import java.util.Scanner; public class E { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), k = sc.nextInt(); Books[] books = new Books[n]; for (int i = 0; i < n; i++) { books[i] = new Books(sc.nextInt(), sc.nextInt(), sc.nextInt()); } Arrays.sort(books); int a = 0, b = 0, ans = 0; Deque<Books> booksa = new ArrayDeque<Books>(), booksb = new ArrayDeque<Books>(); for (Books book : books) { if (book.a && a < k) { // System.out.println("1"); if (book.b && b >= k) { ans -= booksb.peek().time; booksb.pop(); b--; } else { booksa.push(book); } ans += book.time; a++; if (book.b) b++; } else if (book.b && b < k) { // System.out.println("2"); if (book.a && a >= k) { ans -= booksa.peek().time; booksa.pop(); a--; } else { booksb.push(book); } ans += book.time; b++; if (book.a) a++; } else if (book.a && book.b && book.time <= (booksb.isEmpty()?0:booksb.peek().time)+(booksa.isEmpty()?0:booksa.peek().time)) { // System.out.println("3"); ans -= (booksb.isEmpty()?0:booksb.peek().time)+(booksa.isEmpty()?0:booksa.peek().time); booksa.pop(); booksb.pop(); ans += book.time; } // System.out.println(a); // System.out.println(b); // System.out.println(); } if (a < k || b < k) System.out.println(-1); else System.out.println(ans); } static class Books implements Comparable<Books> { int time; boolean a, b; public Books(int time, int a, int b) { this.time = time; this.a = a==1; this.b = b==1; } @Override public int compareTo(Books o) { return Integer.compare(time, o.time); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from collections import deque n,k=list(map(int,input().split())) books=[] for i in range(0,n): books.append(list(map(int,input().split()))) only_a=[] only_b=[] intersection=[] for i in range(0,len(books)): if books[i][1]==0 and books[i][2]==1: only_b.append(books[i]) elif books[i][1]==1 and books[i][2]==0: only_a.append(books[i]) elif books[i][1]==1 and books[i][2]==1: intersection.append(books[i]) only_a.sort(key=lambda x:x[0]) only_b.sort(key=lambda x: x[0]) intersection.sort(key=lambda x: x[0]) only_b=deque(only_b) only_a=deque(only_a) intersection=deque(intersection) if len(intersection)+len(only_a)<k or len(intersection)+len(only_b)<k: print(-1) else: # print(intersection) # print( intersection[0:k]) total=sum([intersection[x][0] for x in range(min(k,len(intersection)))]) if len(intersection)<k: total+=sum([only_a[x][0] for x in range(k-len(intersection))]) total += sum([only_b[x][0] for x in range(k - len(intersection))]) only_a=deque([only_a[x] for x in range(k-len(intersection))]) only_a=deque([only_a[x] for x in range(k-len(intersection))]) while len(intersection)>0 and len(only_a)>0 and len(only_b)>0 and (only_a[0][0]+only_b[0][0])<intersection[-1][0]: total=total-intersection[-1][0]+only_a[0][0]+only_b[0][0] only_a.popleft() only_b.popleft() intersection.pop() print(total)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e5 + 10; int n, m, T, k; vector<pair<int, int>> a, b, ab, other; vector<int> ans; int check(int mid) { if ((int)a.size() < k - mid) return 2e9 + 1; if ((int)b.size() < k - mid) return 2e9 + 1; if (mid + max(0, k - mid) * 2 > m) return 2e9 + 1; int nd = m - mid, res = 0; vector<pair<int, int>> vec; for (int i = 0; i < mid; i++) res += ab[i].first, ans.push_back(ab[i].second); for (int i = mid; i < ab.size(); i++) vec.push_back(ab[i]); for (int i = 0; i < k - mid; i++) res += a[i].first + b[i].first, nd -= 2, ans.push_back(a[i].second), ans.push_back(b[i].second); for (int i = max(0, k - mid); i < a.size(); i++) vec.push_back(a[i]); for (int i = max(0, k - mid); i < b.size(); i++) vec.push_back(b[i]); for (int i = 0; i < other.size(); i++) vec.push_back(other[i]); sort(vec.begin(), vec.end()); for (int i = 0; i < nd && i < vec.size(); i++) res += vec[i].first, ans.push_back(vec[i].second); return res; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; for (int i = 1, x1, x2, x3; i <= n; ++i) { cin >> x1 >> x2 >> x3; if (x2 && x3) ab.push_back({x1, i}); else if (x2) a.push_back({x1, i}); else if (x3) b.push_back({x1, i}); else other.push_back({x1, i}); } sort(a.begin(), a.end()), sort(b.begin(), b.end()), sort(ab.begin(), ab.end()), sort(other.begin(), other.end()); int l = 0, r = min((int)ab.size(), m); while (r - l > 10) { int midl = l + (r - l) / 3, midr = r - (r - l) / 3; if (check(midl) <= check(midr)) r = midr; else l = midl; } int res = 1e9, Min_idx = 0; for (int i = l; i <= r; i++) { int tmp = check(i); if (tmp < res) res = tmp, Min_idx = i; } if (res == 1e9) return puts("-1"), 0; ans.clear(); cout << check(Min_idx) << endl; for (auto a : ans) cout << a << " "; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const unsigned long long int INF = numeric_limits<long long int>::max(); long long int k, n, A, B, T; unsigned long long int ab[200000], a[200000], b[200000]; 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; } for (int i = 0; i < k; i++) { if (i >= J) a[i] = INF; a[i + 1] += a[i]; if (i >= K) b[i] = INF; b[i + 1] += b[i]; if (i >= I) ab[i] = INF; ab[i + 1] += ab[i]; } unsigned long long int ans = ab[k - 1], abi = 0; for (int i = k - 1; i >= 0; i--) { ans = min(ans, abi + a[i] + b[i]); abi = ab[k - i - 1]; } cout << ans << "\n"; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long mod = 1e9 + 7; const long long linf = 1LL << 62; const double EPS = 1e-7; template <class T> void chmin(T& x, T y) { if (x > y) x = y; } template <class T> void chmax(T& x, T y) { if (x < y) x = y; } int n, k; priority_queue<long long, vector<long long>, greater<long long>> pq[3]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) { pq[0].push(t); } else if (a == 1) { pq[1].push(t); } else { pq[2].push(t); } } if (pq[0].size() + min(pq[1].size(), pq[2].size()) < k) { cout << -1 << endl; return 0; } for (int i = 0; i < 3; i++) pq[i].push(inf); long long ans = 0; while (k--) { long long t1 = pq[0].top(), t2 = pq[1].top() + pq[2].top(); if (t1 < t2) { ans += t1; pq[0].pop(); } else { ans += t2; pq[1].pop(); pq[2].pop(); } } cout << ans << endl; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class Main { static class Pair { int f,s; Pair(int f,int s) { this.f=f; this.s=s; } } static class comp implements Comparator<Pair> { public int compare(Pair p1,Pair p2) { return p1.s-p2.s; } } public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); String str[]=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int m=Integer.parseInt(str[1]); int k=Integer.parseInt(str[2]); int arr[][]=new int[n][3]; for(int i=0;i<n;i++) { str=br.readLine().split(" "); arr[i][0]=Integer.parseInt(str[0]); arr[i][1]=Integer.parseInt(str[1]); arr[i][2]=Integer.parseInt(str[2]); } int ac=0,bc=0; for(int i=0;i<n;i++) { if(arr[i][1]==1) ac++; if(arr[i][2]==1) bc++; } if(ac<k||bc<k) pw.println(-1); else { ArrayList<Pair> ab=new ArrayList<>(); ArrayList<Pair> a=new ArrayList<>(); ArrayList<Pair> b=new ArrayList<>(); ArrayList<Pair> c=new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i][1]==1&&arr[i][2]==1) ab.add(new Pair(i+1,arr[i][0])); else if(arr[i][1]==1) a.add(new Pair(i+1,arr[i][0])); else if(arr[i][2]==1) b.add(new Pair(i+1,arr[i][0])); else c.add(new Pair(i+1,arr[i][0])); } Collections.sort(ab,new comp()); Collections.sort(b,new comp()); Collections.sort(a,new comp()); Collections.sort(c,new comp()); ArrayList<Integer> books=new ArrayList<>(); long ans=0; ac=bc=k; if(a.size()==0||b.size()==0) { for(int j=0;j<Math.min(m,k);j++) { ans=ans+ab.get(j).s; books.add(ab.get(j).f); ac--; bc--; } m-=k; if(m>0) { ArrayList<Pair> nw=new ArrayList<>(); for(int j=k;j<ab.size();j++) nw.add(ab.get(j)); for(int i=0;i<a.size();i++) nw.add(a.get(i)); for(int i=0;i<b.size();i++) nw.add(b.get(i)); for(int i=0;i<c.size();i++) nw.add(c.get(i)); Collections.sort(nw,new comp()); for(int i=0;i<m;i++) { ans=ans+nw.get(i).s; books.add(nw.get(i).f); } } } else { ac=k; bc=k; int i=0,j=0,p=0; while(i<ab.size()&&j<a.size()&&p<b.size()&&ac>0&&bc>0&&m>0) { if(ab.get(i).s<=a.get(j).s+b.get(p).s||m==1) { ans=ans+ab.get(i).s; books.add(ab.get(i).f); i++; m--; } else if(m>1) { ans=ans+a.get(j).s+b.get(p).s; books.add(a.get(j).f); books.add(b.get(p).f); j++; p++; m-=2; } ac--; bc--; } if(ac>0||bc>0) { while(j<a.size()&&p<b.size()&&ac>0&&bc>0&&m>1) { ac--; bc--; ans=ans+a.get(j).s+b.get(p).s; books.add(a.get(j).f); books.add(b.get(p).f); j++; p++; m-=2; } while(i<ab.size()&&ac>0&&bc>0&&m>0) { ac--; bc--; ans=ans+ab.get(i).s; books.add(ab.get(i).f); i++; m--; } } if(m>0) { ArrayList<Pair> nw=new ArrayList<>(); for(;i<ab.size();i++) nw.add(ab.get(i)); for(;j<a.size();j++) nw.add(a.get(j)); for(;p<b.size();p++) nw.add(b.get(p)); for(i=0;i<c.size();i++) nw.add(c.get(i)); Collections.sort(nw,new comp()); for(i=0;i<m;i++) { ans=ans+nw.get(i).s; books.add(nw.get(i).f); if(arr[nw.get(i).f-1][1]==1) ac--; if(arr[nw.get(i).f-1][2]==1) bc--; } } } if(ac<=0&&bc<=0) { pw.println(ans); for(int i=0;i<books.size();i++) pw.print(books.get(i)+" "); } else pw.println(-1); } pw.flush(); pw.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int solve() { int n, k; cin >> n >> k; int a, b, ans; ans = a = b = 0; priority_queue<int> x, y, z; while (n--) { int t, u, v; cin >> t >> u >> v; t *= -1; if (u && v) { z.push(t); } else if (u) { x.push(t); } else if (v) { y.push(t); } } while (!x.empty() && !y.empty() && !z.empty() && a < k) { int u, v, w; u = -x.top(); v = -y.top(); w = -z.top(); if (u + v < w) { ans += u + v; x.pop(); y.pop(); } else { ans += w; z.pop(); } ++a; ++b; } while (a < k && !z.empty() && x.empty()) { ans += -z.top(); z.pop(); ++a; ++b; } while (b < k && !z.empty() && y.empty()) { ans += -z.top(); z.pop(); ++a; ++b; } while (a < k && !x.empty() && !z.empty()) { int u = -x.top(); int w = -z.top(); if (u < w) { ans += u; ++a; x.pop(); } else { ans += w; ++a; ++b; z.pop(); } } while (a < k && !x.empty()) { ans += -x.top(); x.pop(); ++a; } while (b < k && !y.empty() && !z.empty()) { int u = -y.top(); int w = -z.top(); if (u < w) { ans += u; ++b; y.pop(); } else { ans += w; ++a; ++b; z.pop(); } } while (b < k && !y.empty()) { ans += -y.top(); y.pop(); ++b; } cout << a << ' ' << b << '\n'; if (a < k || b < k) cout << "-1\n"; else cout << ans << '\n'; return 0; } int main() { cin.tie(0)->sync_with_stdio(0); int t = 1; while (t--) solve(); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(), "Main", 1 << 27).start(); } static class Pair { int f; int s; PrintWriter w; // int t; Pair(int f, int s) { // Pair(int f,int s, PrintWriter w){ this.f = f; this.s = s; // this.w = w; // this.t = t; } public static Comparator<Pair> wc = new Comparator<Pair>() { public int compare(Pair e1, Pair e2) { // 1 for swap if (Math.abs(e1.f) - Math.abs(e2.f) != 0) { // e1.w.println("**"+e1.f+" "+e2.f); return (Math.abs(e1.f) - Math.abs(e2.f)); } else { // e1.w.println("##"+e1.f+" "+e2.f); return (Math.abs(e1.s) - Math.abs(e2.s)); } } }; } public static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } //// recursive BFS public static int bfsr(int s, ArrayList<Integer>[] a, boolean[] b, int[] pre) { b[s] = true; int p = 1; int n = pre.length - 1; int t = a[s].size(); int max = 1; for (int i = 0; i < t; i++) { int x = a[s].get(i); if (!b[x]) { // dist[x] = dist[s] + 1; int xz = (bfsr(x, a, b, pre)); p += xz; max = Math.max(xz, max); } } // max = Math.max(max,(n-p)); pre[s] = max; return p; } //// iterative BFS public static int bfs(int s, ArrayList<Integer>[] a, int dist, boolean[] b, PrintWriter w) { b[s] = true; int siz = 0; dist--; Queue<Integer> q = new LinkedList<>(); q.add(s); while (q.size() != 0 && dist > 0) { int i = q.poll(); Iterator<Integer> it = a[i].listIterator(); int z = 0; while (it.hasNext() && dist > 0) { z = it.next(); if (!b[z]) { b[z] = true; dist--; // dist[z] = dist[i] + 1; siz++; q.add(z); } } } return siz; } public static int lower(int key, Integer[] a) { int l = 0; int r = a.length - 1; int res = 0; while (l <= r) { int mid = (l + r) / 2; if (a[mid] <= key) { l = mid + 1; res = mid + 1; } else { r = mid - 1; } } return res; } ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// public void run() { // code here InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int defaultValue = 0; long mod = Long.valueOf("1000000007"); // int tc = sc.nextInt(); // while (tc-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> a = new ArrayList<Integer>(); ArrayList<Integer> b = new ArrayList<Integer>(); ArrayList<Integer> c = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if (y == 1 && z == 1) { c.add(x); } else if (y == 1) { a.add(x); } else if (z == 1) { b.add(x); } } Collections.sort(a); Collections.sort(b); Collections.sort(c); int x = a.size(); int y = b.size(); int z = c.size(); if ((x + z) < k || (y + z) < k) { w.println(-1); } else { int ans = 0; int i = 0; int p = 0; while ((i + p) < k) { int aa = 10001; int bb = 10001; int cc = 10001; if (i < x) aa = a.get(i); if (i < y) bb = b.get(i); if (p < z) cc = c.get(p); if ((aa + bb) < cc) { ans += (aa + bb); i++; } else { ans += cc; p++; } } w.println(ans); } // } w.flush(); w.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeMap; import java.util.StringTokenizer; import java.util.Map; import java.util.Map.Entry; import java.io.BufferedReader; import java.io.FileReader; 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion(); solver.solve(1, in, out); out.close(); } static class E1ReadingBooksEasyVersion { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int n = sc.nextInt(); int k = sc.nextInt(); TreeMap<pair, Integer> tm1 = new TreeMap<>(); TreeMap<pair, Integer> tm2 = new TreeMap<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); int t1 = sc.nextInt(); int t2 = sc.nextInt(); if (t1 == 1) tm1.put(new pair(x, t1, t2), tm1.getOrDefault(new pair(x, t1, t2), 0) + 1); if (t2 == 1) tm2.put(new pair(x, t1, t2), tm2.getOrDefault(new pair(x, t1, t2), 0) + 1); } int c1 = k; int c2 = k; long ans = 0; TreeMap<pair, Integer> tmp = new TreeMap<>(tm1); // pw.println(tm1); // pw.println(tm2); while (tmp.size() > 0) { pair t = tmp.firstKey(); int val = tmp.pollFirstEntry().getValue(); if (tm2.containsKey(t)) { int val2 = tm2.get(t); int min = Math.min(val, val2); ans += 1l * min * t.a; if (val - min > 0) tm1.put(t, val - min); else tm1.remove(t); if (val2 - min > 0) tm2.put(t, val2 - min); else tm2.remove(t); c1 = Math.max(0, c1 - min); c2 = Math.max(0, c2 - min); } } // pw.println(tm1); // pw.println(tm2); // pw.println(c1+" "+c2+" "+ans); while (c1 > 0 && tm1.size() > 0) { pair t = tm1.firstKey(); int val = tm1.firstEntry().getValue(); int min = Math.min(c1, val); int x = tm1.pollFirstEntry().getKey().a; ans += 1l * min * x; c1 -= min; } while (c2 > 0 && tm2.size() > 0) { pair t = tm2.firstKey(); int val = tm2.firstEntry().getValue(); int min = Math.min(c2, val); int x = tm2.pollFirstEntry().getKey().a; ans += 1l * min * x; c2 -= min; } // pw.println(tm1); // pw.println(tm2); // pw.println(c1+" "+c2+" "+ans); pw.println((c1 == 0 && c2 == 0) ? ans : -1); } public class pair implements Comparable<pair> { int a; int b; int c; public pair(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int compareTo(pair pair) { return a - pair.a == 0 ? b - pair.b == 0 ? c - pair.c : b - pair.b : a - pair.a; } public String toString() { return a + " " + b + " " + c; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
input=__import__('sys').stdin.readline n,k=map(int,input().split()) s=[] alisa=bob=0 for i in range(n): t,a,b=map(int,input().split()) if a and b:s.append([t,a,b]) elif b:s.append([t,2,3]) elif a:s.append([t,3,2]) alisa+=a;bob+=b if alisa<k or bob<k:print(-1) else: s.sort() alisa=bob=0 ans=0 i=0 at=[] bt=[] while alisa<k or bob<k: t,a,b=s[i] if a==1 and b==1:ans+=t;alisa+=1;bob+=1 elif a==3 and alisa<k:ans+=t;alisa+=1;at.append(t) elif b==3 and bob<k:ans+=t;bob+=1;bt.append(t) i+=1 i=len(at)-1 while i>=0 and alisa>k: ans-=at[i] i-=1 alisa-=1 i=len(bt)-1 while i>=0 and bob>k: ans-=bt[i] i-=1 bob-=1 print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define ll long long #define vll vector<ll> #define vi vector<int> #define pb push_back #define fi first #define se second #define pii pair<int,int> #define pll pair<ll,ll> #define mii map<int,int> #define mll map<ll,ll> #define all(x) (x).begin(),(x).end() #define S(x) (int)(x).size() #define L(x) (int)(x).length() #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set; const int mod = 1e9+7; const ll infl = 0x3f3f3f3f3f3f3f3fLL; const int infi = 0x3f3f3f3f; int t[200009],a[200009],b[200009],ind[200009]; void solve() { int n,k,i; cin>>n>>k; for(i=0;i<n;i++) cin>>t[i]>>a[i]>>b[i]; iota(ind,ind+n,0); sort(ind,ind+n,[](int A, int B){return t[A]<t[B];}); int aa=0,bb=0; ll tot=0; for(i=0;i<n && aa<k;i++) { if(a[ind[i]]==0) continue; aa++; tot += t[ind[i]]; bb += b[ind[i]]; } for(i=0;i<n && bb<k;i++) { if(b[ind[i]]==0) continue; bb++; aa+=a[ind[i]]; tot += t[ind[i]]; //if(bb==k) break; } for(int j=i-1;j>=0;j--) { if(a[ind[j]]==0 || b[ind[j]]==1) continue; aa--; tot -= t[ind[j]]; if(aa==k) break; } if(aa!=k || bb!=k) { cout<<-1<<'\n'; return; } cout<<tot<<'\n'; } int main() { IOS int t_=1; //cin>>t; while(t_--) { solve(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<pair<int, int> > A, B, C; vector<pair<int, int> > Ataken, Btaken; vector<pair<int, int> > ABC; set<int> taken; int ac = 0; int bc = 0; int t = 0; int read = 0; for (int i = 0; i < n; i++) { int tu, a, b; cin >> tu >> a >> b; if (a && !b) A.push_back({tu, i}); if (b && !a) B.push_back({tu, i}); if (a && b) C.push_back({tu, i}); if (!a && !b) ABC.push_back({tu, i}); } sort(A.begin(), A.end(), greater<pair<int, int> >()); sort(B.begin(), B.end(), greater<pair<int, int> >()); sort(C.begin(), C.end(), greater<pair<int, int> >()); while (ac < k || bc < k) { int use = -1; if (ac < k && bc < k) { if (A.empty()) { if (C.empty()) { use = -2; } else { use = 2; } } else if (B.empty()) { if (C.empty()) { use = -2; } else { use = 2; } } else { assert(!A.empty()); assert(!B.empty()); if (C.empty()) { use = 3; } else { if (A.back().first + B.back().first < C.back().first) { use = 3; } else { use = 2; } } } } else if (ac < k) { if (A.empty()) { if (C.empty()) { use = -2; } else { use = 2; } } else { if (C.empty()) { use = 0; } else { if (A.back().first < C.back().first) { use = 0; } else { use = 2; } } } } else if (bc < k) { if (B.empty()) { if (C.empty()) { use = -2; } else { use = 2; } } else { if (C.empty()) { use = 2; } else { if (B.back().first < C.back().first) { use = 1; } else { use = 2; } } } } else { } if (use == 0 || use == 3) { assert(!A.empty()); t += A.back().first; taken.insert(A.back().second); Ataken.push_back(A.back()); A.pop_back(); ac++; read++; } if (use == 1 || use == 3) { assert(!B.empty()); taken.insert(B.back().second); Btaken.push_back(B.back()); t += B.back().first; B.pop_back(); bc++; read++; } if (use == 2) { assert(!C.empty()); taken.insert(C.back().second); t += C.back().first; C.pop_back(); ac++; bc++; read++; } if (use < 0) { cout << -1 << endl; return 0; } } while (read > m) { if (C.empty()) { cout << -1 << endl; return 0; } if (!Ataken.empty() && !Btaken.empty()) { t -= Ataken.back().first; t -= Btaken.back().first; taken.erase(Ataken.back().second); taken.erase(Btaken.back().second); Ataken.pop_back(); Btaken.pop_back(); t += C.back().first; taken.insert(C.back().second); C.pop_back(); read--; } else { cout << -1 << endl; return 0; } } for (auto& tt : A) ABC.push_back(tt); for (auto& tt : B) ABC.push_back(tt); for (auto& tt : C) ABC.push_back(tt); sort(ABC.begin(), ABC.end(), greater<pair<int, int> >()); while (read < m) { t += ABC.back().first; taken.insert(ABC.back().second); ABC.pop_back(); read++; } cout << t << endl; assert((int)taken.size() == m); for (int u : taken) { cout << u + 1 << " "; } cout << endl; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << '\n'; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } void run() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); } const int mod = 998244353; long long binpow(long long a, long long b, long long m = mod) { a %= m; long long res = 1; while (b) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } struct MM { int t, a, b; }; bool cmp(MM a, MM b) { if (a.t != b.t) return a.t < b.t; } int n, k, m; int main() { run(); cin >> n >> m >> k; multiset<pair<int, int>> a, b, c; vector<pair<int, int>> garbage; for (int i = 0; i < n; ++i) { MM j; cin >> j.t >> j.a >> j.b; if (j.b == 0 && j.a) a.insert({j.t, i + 1}); else if (j.b && j.a == 0) b.insert({j.t, i + 1}); else if (j.a && j.b) { c.insert({j.t, i + 1}); } else { garbage.emplace_back(j.t, i + 1); } } if (((int)((c).size())) + min(((int)((a).size())), ((int)((b).size()))) < k) return cout << -1, 0; int i = 0; long long ans = 0; vector<int> idx; while (k) { auto q1 = a.begin(); auto q2 = b.begin(); if (q1 == a.end() || q2 == b.end()) { ans += c.begin()->first; idx.push_back(c.begin()->second); c.erase(c.begin()); m--; } else { auto q3 = c.begin(); if (q3 == c.end()) { ans += q1->first + q2->first; idx.push_back(q1->second); idx.push_back(q2->second); a.erase(q1); b.erase(q2); m -= 2; } else if (q1->first + q2->first < q3->first && m - 2 >= k) { ans += q1->first + q2->first; idx.push_back(q1->second); idx.push_back(q2->second); a.erase(q1); b.erase(q2); m -= 2; } else { idx.push_back(q3->second); ans += q3->first; c.erase(q3); m--; } } k--; } for (auto x : a) garbage.push_back(x); for (auto x : b) garbage.push_back(x); for (auto x : c) garbage.push_back(x); sort(((garbage).begin()), ((garbage).end())); for (int i = 0; i < m; ++i) { ans += garbage[i].first; idx.push_back(garbage[i].second); } cout << ans << '\n'; for (int i : idx) cout << i << ' '; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.lang.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); ArrayList<Integer> a=new ArrayList<Integer>(); ArrayList<Integer> b=new ArrayList<Integer>(); ArrayList<Integer> c=new ArrayList<Integer>(); for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); int z=sc.nextInt(); if(y==1 && z==1)c.add(x); else if(y==1)a.add(x); else if(z==1)b.add(x); } int l=0,s1=a.size(),s2=b.size(),s3=c.size(); long ans=0; while(l<k){ if(s1>0 && s2>0){ if(s3>0){ if(c.get(0)>=a.get(0)+b.get(0)){ ans+=c.get(0); l++; s3--; c.remove(0); } else{ ans+=a.get(0); ans+=b.get(0); l++; s1--;s2--; a.remove(0); b.remove(0); } } else{ ans+=a.get(0); ans+=b.get(0); l++; s1--;s2--; a.remove(0); b.remove(0); } } else if(s3>0){ ans+=c.get(0); l++; s3--; c.remove(0); } else{ ans=-1;break; } } System.out.println(ans); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class ReadingBooks { public static void main(String[] args) { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int N = sc.nextInt(), K = sc.nextInt(); List<Integer> alice = new ArrayList<>(); List<Integer> bob = new ArrayList<>(); List<Integer> both = new ArrayList<>(); while (N-- > 0) { int t = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(); if (a + b == 2) { both.add(t); } else if (a == 1) { alice.add(t); } else if (b == 1) { bob.add(t); } } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); alice = pref(alice); bob = pref(bob); both = pref(both); int ans = Integer.MAX_VALUE; for (int i = 0; i < both.size(); i++) { int total = both.get(i); int amt = K - i; if (amt < 0 || alice.size() <= amt || bob.size() <= amt) { break; } total += alice.get(amt); total += bob.get(amt); ans = Math.min(ans, total); } System.out.println(ans == Integer.MAX_VALUE ? -1 : ans); } public static List<Integer> pref(List<Integer> a) { List<Integer> pref = new ArrayList<>(); pref.add(0); for (int i = 0; i < a.size(); i++) { pref.add(pref.get(i) + a.get(i)); } return pref; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from fractions import Fraction import bisect import os import io from collections import Counter import bisect from collections import defaultdict import math import random import heapq as hq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate from queue import Queue # sys.setrecursionlimit(200000) # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) mod = int(1e9)+7 def factors(n): return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) # ---------------------------------------------------- # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') def pre(a): if len(a) == 0: return [] ans = [a[0][0]] for i in range(1, len(a)): ans.append(ans[-1] + a[i][0]) return ans t = 1 # t = iinput() oo = [] zo = [] oz = [] zz = [] for _ in range(t): n,m, k = rinput() for i in range(n): t, a, b = rinput() if a == 1 and b == 1: oo.append((t,i)) elif a == 1: oz.append((t,i)) elif b==1: zo.append((t, i)) else: zz.append((t,i)) oo.sort() zo.sort() oz.sort() zz.sort() poo = pre(oo) pzo = pre(zo) poz = pre(oz) noo = len(oo) nzo = len(zo) noz = len(oz) ans = float('inf') config = [0, 0, 0] #oo,oz,zo for i in range(noo+1): want = k - i if nzo >= want and noz >= want and want > 0 and i==0: val = pzo[want - 1] + poz[want - 1] if val < ans and 2*want<=m: ans = val config = [0,want,want] if nzo >= want and noz >= want and i > 0 and want > 0: val = poo[i - 1] + pzo[want - 1] + poz[want - 1] if val < ans and i+2*want<=m: ans=val config=[i, want, want] if want == 0 and i==k: val = poo[i - 1] if val < ans and i<=m: ans=val config=[i, 0, 0] if ans == float('inf'): print(-1) else: items = sum(config) ioo,ioz,izo = config newarr = oo[ioo:] + oz[ioz:] + zo[izo:] + zz newarr.sort() fans = [] for i in range(config[0]): fans.append(oo[i][1] + 1) for i in range(config[1]): fans.append(oz[i][1] + 1) for i in range(config[2]): fans.append(zo[i][1] + 1) for i in range(m - items): ans+=newarr[i][0] fans.append(newarr[i][1]+1) print(ans) print(*fans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.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(); Collections.sort(b); Collections.sort(bo); Collections.sort(a); 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]); } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, k, t, a, b, boths, alices, bobs, cnt[2], ans, res, tmp; vector<int> both, alice, bob; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else if (b) bob.push_back(t); } boths = both.size(); alices = alice.size(); bobs = bob.size(); sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); while (k) { int s1 = 1 << 30, s2 = 1 << 30; if (boths > cnt[0]) s1 = both[cnt[0]]; if (alices > cnt[1] && bobs > cnt[1]) s2 = alice[cnt[1]] + bob[cnt[1]]; if (s1 < s2) ans += s1, cnt[0]++; else if (s1 > s2) ans += s2, cnt[1]++; else break; k--; } while (k--) { if (cnt[1] < min(alices, bobs)) ans += alice[cnt[1]] + bob[cnt[1]], cnt[1]++; else return cout << "-1\n", 0; } cout << ans << '\n'; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int ar[n][3]; vector<int> a, b, c; for (int i = 0; i < n; i++) { cin >> ar[i][2] >> ar[i][0] >> ar[i][1]; if (ar[i][0] == 1) { if (ar[i][1] == 1) { c.push_back(ar[i][2]); } else { a.push_back(ar[i][2]); } } else { b.push_back(ar[i][2]); } } if (a.size() + c.size() < k || b.size() + c.size() < k) { cout << -1; return 0; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); int abpointer = 0; int cpointer = 0; int sum = 0; int cost = 0; while (sum < k) { if (abpointer < a.size() && abpointer < b.size() && cpointer < c.size() && a[abpointer] + b[abpointer] < c[cpointer]) { sum++; cost += a[abpointer] + b[abpointer]; abpointer++; } else if (abpointer < a.size() && abpointer < b.size() && cpointer < c.size() && a[abpointer] + b[abpointer] >= c[cpointer]) { sum++; cost += c[cpointer]; cpointer++; } else if (abpointer < a.size() && abpointer < b.size()) { sum++; cost += a[abpointer] + b[abpointer]; abpointer++; } else if (cpointer < c.size()) { sum++; cost += c[cpointer]; cpointer++; } } cout << cost << endl; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class ReadingBookseasyversion { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> both = new ArrayList<>(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); int bb = 0; int aa = 0; int bo = 0; for(int i=0;i<n;i++){ int num = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); if(x == 1 && y == 1){ both.add(num); bo++; } else if( x== 1 && y == 0){ a.add(num); aa++; } else { b.add(num); bb++; } } if(bo+(Math.min(aa,bb))<k){ System.out.println("-1"); } else{ long ans =0; Collections.sort(both); Collections.sort(a); Collections.sort(b); int i=0;int j =0; while(k-->0){ if(i<both.size() && j<(Math.min(a.size(), b.size()))){ if(both.get(i) < a.get(j)+b.get(j)){ ans += a.get(j)+b.get(j); j++; } else{ ans += both.get(i); i++; } } else if(i>=both.size()){ ans += a.get(j)+b.get(j); j++; } else if(j>=(Math.min(a.size(), b.size()))){ ans += both.get(i); i++; } } System.out.println(ans); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class Reading_Books { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int n = t.nextInt(); int m = t.nextInt(); int k = t.nextInt(); List<Pair> both = new ArrayList<>(); List<Pair> alice = new ArrayList<>(); List<Pair> bob = new ArrayList<>(); List<Pair> none = new ArrayList<>(); Set<Integer> ans = new HashSet<>(); for (int i = 0; i < n; ++i) { int val = t.nextInt(); int x = t.nextInt(); int y = t.nextInt(); if (x == 1 && y == 1) both.add(new Pair(i + 1, val)); else if (x == 1) alice.add(new Pair(i + 1, val)); else if (y == 1) bob.add(new Pair(i + 1, val)); else { none.add(new Pair(i + 1, val)); } } Collections.sort(both, (Pair x, Pair y) -> x.val - y.val); Collections.sort(alice, (Pair x, Pair y) -> x.val - y.val); Collections.sort(bob, (Pair x, Pair y) -> x.val - y.val); Collections.sort(none, (Pair x, Pair y) -> x.val - y.val); if (Math.min(alice.size(), bob.size()) + both.size() >= k) { int a = 0, b = 0, c = 0, d = 0; int cur = 0; long time = 0; long p = 0; boolean flag = true; while (cur < k && a < both.size() && b < alice.size() && c < bob.size()) { if (bob.get(c).val + alice.get(b).val <= both.get(a).val) { time = time + bob.get(c).val + alice.get(b).val; if (p + 2 <= m) { cur++; p += 2; c++; b++; ans.add(bob.get(c - 1).idx); ans.add(alice.get(b - 1).idx); } else { if (b == -1 || c == -1 || a == both.size()) { flag = false; break; } time = time - (bob.get(c).val + alice.get(b).val); time = time + both.get(a).val; ans.add(both.get(a).idx); cur++; p++; a++; } } else { time = time + both.get(a).val; if (p + 1 <= m) { a++; cur++; p++; ans.add(both.get(a - 1).idx); } else { if (b == -1 || c == -1 || a >= both.size() - 1) { flag = false; break; } time = time - (bob.get(c - 1).val + alice.get(b - 1).val); time = time + both.get(a + 1).val; ans.remove(bob.get(c - 1).idx); ans.remove(alice.get(c - 1).idx); ans.add(both.get(a).idx); ans.add(both.get(a + 1).idx); a += 2; cur++; b--; c--; } } } while (cur < k && a < both.size()) { time = time + both.get(a).val; if (p + 1 <= m) { a++; cur++; p++; ans.add(both.get(a - 1).idx); } else { if (b == -1 || c == -1 || a >= both.size() - 1) { flag = false; break; } time = time - (bob.get(c - 1).val + alice.get(b - 1).val); time = time + both.get(a + 1).val; ans.remove(bob.get(c - 1).idx); ans.remove(alice.get(c - 1).idx); ans.add(both.get(a).idx); ans.add(both.get(a + 1).idx); a += 2; cur++; b--; c--; } } while (cur < k && b < alice.size() && c < bob.size()) { time = time + bob.get(c).val + alice.get(b).val; if (p + 2 <= m) { cur++; p += 2; c++; b++; ans.add(bob.get(c - 1).idx); ans.add(alice.get(b - 1).idx); } else { if (b == -1 || c == -1 || a == both.size()) { flag = false; break; } time = time - (bob.get(c).val + alice.get(b).val); time = time + both.get(a).val; ans.add(both.get(a).idx); cur++; p++; a++; } } while (p < m) { int v1 = a < both.size() ? both.get(a).val : 10001; int v2 = b < alice.size() ? alice.get(b).val : 10001; int v3 = c < bob.size() ? bob.get(c).val : 10001; int v4 = d < none.size() ? none.get(d).val : 10001; if (v1 == v2 && v2 == v3 && v3 == v4 && v4 == 10001) { break; } if (v1 <= v2 && v1 <= v3 && v1 <= v4) { time = time + v1; ans.add(both.get(a).idx); p++; a++; } else if (v2 <= v1 && v2 <= v3 && v2 <= v4) { time = time + v2; ans.add(alice.get(b).idx); p++; b++; } else if (v3 <= v2 && v3 <= v1 && v3 <= v4) { time = time + v3; ans.add(bob.get(c).idx); p++; c++; } else { time = time + v4; ans.add(none.get(d).idx); p++; d++; } } if (p == m && flag) { o.println(time); for (int idx : ans) { o.print(idx + " "); } } else { o.println("-1"); } } else o.println("-1"); o.flush(); o.close(); } private static class Pair { int idx; int val; public Pair(int idx, int val) { this.idx = idx; this.val = val; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Stack; import java.util.ArrayList; import java.util.Vector; import java.util.ArrayDeque; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author dauom */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); E2ReadingBooksHardVersion solver = new E2ReadingBooksHardVersion(); solver.solve(1, in, out); out.close(); } static class E2ReadingBooksHardVersion { public final void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); ArrayDeque<int[]> alice = new ArrayDeque<>(); ArrayDeque<int[]> bob = new ArrayDeque<>(); ArrayDeque<int[]> both = new ArrayDeque<>(); ArrayDeque<int[]> none = new ArrayDeque<>(); Stack<Integer> ansAlice = new Stack<>(); Stack<Integer> ansBob = new Stack<>(); Stack<Integer> ansBoth = new Stack<>(); Stack<Integer> ansNone = new Stack<>(); int[] t = new int[n]; for (int i = 0; i < n; i++) { t[i] = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); if (a == 1 && b == 1) { both.add(new int[] {t[i], i}); } else if (a == 1) { alice.add(new int[] {t[i], i}); } else if (b == 1) { bob.add(new int[] {t[i], i}); } else { none.add(new int[] {t[i], i}); } } sort(alice); sort(bob); sort(both); sort(none); int aliceCount = 0, bobCount = 0; while (aliceCount < k && bobCount < k && !(alice.isEmpty() && bob.isEmpty() && both.isEmpty())) { if (alice.isEmpty()) { if (both.isEmpty()) { break; } ansBoth.add(both.poll()[1]); } else if (bob.isEmpty()) { if (both.isEmpty()) { break; } ansBoth.add(both.poll()[1]); } else if (both.isEmpty()) { ansBob.add(bob.poll()[1]); ansAlice.add(alice.poll()[1]); } else { if (alice.peek()[0] + bob.peek()[0] < both.peek()[0]) { ansBob.add(bob.poll()[1]); ansAlice.add(alice.poll()[1]); } else { ansBoth.add(both.poll()[1]); } } aliceCount++; bobCount++; } while (aliceCount < k && !alice.isEmpty()) { ansAlice.add(alice.poll()[1]); ++aliceCount; } while (bobCount < k && !bob.isEmpty()) { ansBob.add(bob.poll()[1]); ++bobCount; } if (aliceCount < k || bobCount < k) { out.println(-1); return; } while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m && !both.isEmpty() && !ansAlice.isEmpty() && !ansBob.isEmpty()) { ansBob.pop(); ansAlice.pop(); ansBoth.add(both.poll()[1]); } if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m) { out.println(-1); return; } while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) { if (none.isEmpty()) { if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) { ansBob.add(bob.poll()[1]); ansAlice.add(alice.poll()[1]); ansBoth.pop(); } else { break; } } else { if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) { int idxBoth = ansBoth.peek(); int doubleCost = bob.peek()[0] + alice.peek()[0] - t[idxBoth]; int noneCost = none.peek()[0]; if (doubleCost < noneCost) { ansBob.add(bob.poll()[1]); ansAlice.add(alice.poll()[1]); ansBoth.pop(); } else { ansNone.add(none.poll()[1]); } } else { ansNone.add(none.poll()[1]); } } } if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) { out.println(-1); return; } ArrayList<Integer> ansList = new ArrayList<>(); ansList.addAll(ansBob); ansList.addAll(ansAlice); ansList.addAll(ansBoth); ansList.addAll(ansNone); if (ansList.size() != m) { throw new RuntimeException(); } int ans = 0; for (int x : ansList) { ans += t[x]; } out.println(ans); for (int x : ansList) { out.print((x + 1) + " "); } out.println(); } private void sort(ArrayDeque<int[]> q) { ArrayList<int[]> a = new ArrayList<>(q); a.sort(Comparators.singletonIntArr); q.clear(); q.addAll(a); } } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; public InputReader() { this.stream = System.in; } public InputReader(final InputStream stream) { this.stream = stream; } private final int read() { if (this.numChars == -1) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return -1; } } return this.buf[this.curChar++]; } } public final int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) {} byte sgn = 1; if (c == 45) { // 45 == '-' sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9' res *= 10; res += c - 48; // 48 == '0' c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } private static final boolean isSpaceChar(final int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t' } } static final class Comparators { public static final Comparator<int[]> singletonIntArr = (x, y) -> compare(x[0], y[0]); private static final int compare(final int x, final int y) { return x < y ? -1 : (x == y ? 0 : 1); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void program(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); program(); } const int MX = 5e5; int n, m, k, t[MX], a[MX], b[MX]; int SA[MX], sm[MX][4], cnt[4]; int id[MX][4]; void moveSet(set<pair<int, int> >& fr, set<pair<int, int> >& st, int& sum, int size) { while (true) { if (st.size() < size && !fr.empty()) { sum += fr.begin()->first; st.insert(*fr.begin()); fr.erase(fr.begin()); continue; } if (st.size() > size && !st.empty()) { sum -= (*st.end()).first; fr.insert(*--st.end()); st.erase(--st.end()); continue; } if (!st.empty() && !fr.empty() && (*--st.end()).first > fr.begin()->first) { sum += fr.begin()->first; sum -= (*st.end()).first; st.insert(*fr.begin()); fr.erase(fr.begin()); fr.insert(*--st.end()); st.erase(--st.end()); continue; } break; } } void program() { cin >> n >> m >> k; for (int i = int(0); i < int(n); i++) cin >> t[i] >> a[i] >> b[i]; for (int i = int(0); i < int(n); i++) SA[i] = i; sort(SA, SA + n, [](int i, int j) { return t[i] < t[j]; }); for (int i = int(0); i < int(4); i++) sm[0][i] = 0; for (int i = int(0); i < int(n); i++) { int bm = a[SA[i]] + b[SA[i]] * 2; id[cnt[bm]][bm] = SA[i]; cnt[bm]++; sm[cnt[bm]][bm] = sm[cnt[bm] - 1][bm] + t[SA[i]]; } int ans = 2e9 + 10; set<int> sAns; for (int iter = int(0); iter < int(2); iter++) { set<pair<int, int> > fr, st; int sum = 0; for (int i = int(0); i < int(n); i++) { int bm = a[i] + b[i] * 2; if (bm == 3) continue; fr.insert({t[i], i}); } int cur = 0; for (int both = int(0); both < int(min(cnt[3], k) + 1); both++) { if (k - both > min(cnt[1], cnt[2])) continue; if (both + 2 * (k - both) > m) continue; while (cur < k - both) { for (int i = int(1); i < int(3); i++) { pair<int, int> p = {t[id[cur][i]], id[cur][i]}; fr.erase(p); st.erase(p); } cur++; } while (cur > k - both) { cur--; for (int i = int(1); i < int(3); i++) { pair<int, int> p = {t[id[cur][i]], id[cur][i]}; fr.insert(p); } } moveSet(fr, st, sum, m - both - 2 * (k - both)); if (st.size() != m - both - 2 * (k - both)) continue; ans = min(ans, sm[both][3] + sm[k - both][1] + sm[k - both][2] + sum); if (iter == 1 && ans == sm[both][3] + sm[k - both][1] + sm[k - both][2] + sum) { for (int i = int(0); i < int(k - both); i++) sAns.insert(id[i][1] + 1); for (int i = int(0); i < int(k - both); i++) sAns.insert(id[i][2] + 1); for (int i = int(0); i < int(both); i++) sAns.insert(id[i][3] + 1); for (auto it : st) sAns.insert(it.second + 1); } } } if (ans == 2e9 + 10) ans = -1; cout << ans << endl; bool f = 1; for (auto it : sAns) cout << (f ? "" : " ") << it, f = 0; cout << endl; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, k; cin >> n >> k; vector<long long> alice; vector<long long> bob; vector<long long> com; for (long long i = 0; i < n; i++) { long long a1, b1, c1; cin >> a1 >> b1 >> c1; if (c1 == 1 && b1 == 1) { com.push_back(a1); } else if (b1 == 1) { alice.push_back(a1); } else if (c1 == 1) { bob.push_back(a1); } } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(com.begin(), com.end()); if (alice.size() + com.size() < k || bob.size() + com.size() < k) { cout << -1 << "\n"; return 0; } vector<long long> dat; if (k <= com.size()) { long long cur = 0; for (long long i = 0; i < k; i++) { cur += com[i]; } dat.push_back(cur); for (long long i = 0; i < min((long long)alice.size(), (long long)bob.size()); i++) { cur -= com[k - 1 - i]; cur += (alice[i] + bob[i]); dat.push_back(cur); } } else { long long cur = 0; for (long long i = 0; i < com.size(); i++) { cur += com[i]; } long long w = 0; for (w = 0; w < k - com.size(); w++) { cur += (alice[w] + bob[w]); } dat.push_back(cur); long long pp = 0; for (long long j = w; j < min((long long)alice.size(), (long long)bob.size()); j++) { cur -= com[com.size() - 1 - pp]; cur += (alice[j] + bob[j]); pp++; dat.push_back(cur); } } long long asa = dat[0]; for (long long i = 1; i < dat.size(); i++) { if (asa > dat[i]) { asa = dat[i]; } } cout << asa << "\n"; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class e2 { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt(); ArrayList<Info> both = new ArrayList<>(); ArrayList<Info> alice = new ArrayList<>(); ArrayList<Info> bob = new ArrayList<>(); ArrayList<Info> all = new ArrayList<>(); for (int i = 0 ; i < n ; i++) { int time = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(); if (a == 1 && b == 1) { both.add(new Info(time,i)); } else if (a == 1) { alice.add(new Info(time,i)); } else if (b == 1) { bob.add(new Info(time,i)); } all.add(new Info(time, i)); } if (bob.size() + both.size() < k || alice.size() + both.size() < k || all.size() < m) { out.println(-1); out.close(); return; } Collections.sort(both); Collections.sort(alice); Collections.sort(bob); Collections.sort(all); HashSet<Integer> seen = new HashSet<>(); int ans = 0; int liked = 0; for (int i = 0 ; i < both.size() && i < k; i++) { ans += both.get(i).time; seen.add(both.get(i).index); liked++; } int bothPtr = liked-1; for (int i = 0 ; i < alice.size() && i < bob.size() && seen.size() < m; i++) { if (liked < k) { ans += alice.get(i).time; ans += bob.get(i).time; seen.add(alice.get(i).index); seen.add(bob.get(i).index); liked++; } else { if (bothPtr < 0) { break; } if (alice.get(i).time + bob.get(i).time < both.get(bothPtr).time) { ans -= both.get(bothPtr).time; ans += alice.get(i).time; ans += bob.get(i).time; seen.remove(both.get(bothPtr).index); seen.add(alice.get(i).index); seen.add(bob.get(i).index); bothPtr--; } else { break; } } } for (int i = 0 ; i < all.size() && seen.size()<m ; i++) { if (!seen.add(all.get(i).index)) continue; ans += all.get(i).time; } if (seen.size() > m) { out.println(-1); out.close(); return; } out.println(ans); for (Integer e : seen) { out.print((e+1) + " "); } out.println(); out.close(); } static class Info implements Comparable<Info>{ int time, index; public Info(int t, int i) { time=t; index=i; } @Override public int compareTo(Info o) { return time-o.time; } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens()) return st.nextToken(); else st = new StringTokenizer(br.readLine()); return next(); } 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()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from math import * from collections import * from bisect import * import sys input=sys.stdin.readline t=1 while(t): t-=1 n,k1=map(int,input().split()) oo=[] al=[] bob=[] for i in range(n): a=list(map(int,input().split())) if(a[-1]==a[-2]==1): oo.append(a[0]) elif(a[-1]==0 and a[-2]==1): al.append(a[0]) elif(a[-1]==1 and a[-2]==0): bob.append(a[0]) oo.sort() bob.sort() al.sort() i,j,k=0,0,0 ak,bk=k1,k1 rr=0 l1,l2,l3=len(oo),len(al),len(bob) while((ak>0 or bk>0) and (i<l1 or j<l2 or k<l3)): if(i<l1): if(j<l2 and k<l3): if(oo[i]<=bob[k]+al[j]): ak-=1 bk-=1 rr+=oo[i] i+=1 elif(bk>0): if(oo[i]<=al[j]): rr+=oo[i] i+=1 ak-=1 bk-=1 else: rr+=al[j] j+=1 ak-=1 elif(ak>0): if(oo[i]<=bob[k]): rr+=oo[i] i+=1 ak-=1 bk-=1 else: rr+=bob[k] bk-=1 k+=1 elif(j<l2 and k>=l3): if(bk<=0): if(oo[i]<=al[j]): rr+=oo[i] ak-=1 bk-=1 i+=1 else: rr+=al[j] j+=1 ak-=1 else: rr+=oo[i] i+=1 ak-=1 bk-=1 elif(j>=l2 and k<l3): if(ak<=0): if(oo[i]<=bob[k]): rr+=oo[i] i+=1 bk-=1 ak-=1 else: rr+=bob[k] k+=1 bk-=1 else: rr+=oo[i] i+=1 ak-=1 bk-=1 else: rr+=oo[i] i+=1 ak-=1 bk-=1 else: if(ak>0 and j<l2): rr+=al[j] j+=1 ak-=1 if(bk>0 and k<l3): rr+=bob[k] k+=1 bk-=1 if(ak<=0 and bk<=0): print(rr) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from sys import stdin input=stdin.readline def answer(): if(n3+n1 < k or n3+n2 < k):return -1 i,j=0,0 ans=0 while(i < n1 and i < n2 and j < n3): if(a[i]+b[i] > common[j]): ans+=common[j] j+=1 else: ans+=a[i]+b[i] i+=1 while(i < min(n1,n2)): ans+=a[i]+b[i] i+=1 while(j < n3): ans+=common[j] j+=1 return ans n,k=map(int,input().split()) a,b,common=[],[],[] for i in range(n): t,x,y=map(int,input().split()) if(x and y):common.append(t) elif(x==1 and y==0):a.append(t) elif(x==0 and y==1):b.append(t) common.sort() a.sort() b.sort() n1,n2,n3=len(a),len(b),len(common) print(answer())
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k = map(int, input().split()) oo = list() oa = list() ob = list() zz = list() for i in range(n): t,a,b = map(int, input().split()) if a == 1 and b == 1: oo.append((t,i)) elif a == 0 and b == 1: ob.append((t,i)) elif a == 1 and b == 0: oa.append((t,i)) else: zz.append((t,i)) oo = sorted(oo) oa = sorted(oa) ob = sorted(ob) oo_p = 0 oa_p = 0 ob_p = 0 ca = 0 cb = 0 ans = 0 ans_arr = list() MAX = 23942034809238409823048 def condition(ko, loa, lob, loo, mo): if max(0, max(ko-loa, ko-lob)) > loo or max(0, max(ko-loa, ko-lob)) > mo: return False return True def get_first_elem_from_list(l, pos): if pos < len(l): return l[pos] else: return (MAX,-1) def remove_first_elem_from_list(l, pos): if len(l)>pos: pos += 1 return pos if not condition(k, len(oa), len(ob), len(oo), m): print("-1") exit(0) c = 0 while ca < k or cb < k: oo_f = get_first_elem_from_list(oo, oo_p) oa_f = get_first_elem_from_list(oa, oa_p) ob_f = get_first_elem_from_list(ob, ob_p) c += 1 if ca < k and cb < k: if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2): if oo_f[0] == MAX: print("-1") exit(0) if n == 19683 and m == 507 and k == 254 and c > 238: print(oo_f[0], oa_f[0], ob_f[0], c) ca += 1 cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] + ob_f[0] < oo_f[0]: ca += 1 cb += 1 ans+=oa_f[0]+ob_f[0] ans_arr.extend([oa_f[1], ob_f[1]]) oa_p = remove_first_elem_from_list(oa, oa_p) ob_p = remove_first_elem_from_list(ob, ob_p) elif ca < k: if oo_f[0] <= oa_f[0]: ca += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] < oo_f[0]: ca += 1 ans+=oa_f[0] ans_arr.append(oa_f[1]) oa_p = remove_first_elem_from_list(oa, oa_p) else: if oo_f[0] <= ob_f[0]: cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif ob_f[0] < oo_f[0]: cb += 1 ans+=ob_f[0] ans_arr.append(ob_f[1]) ob_p = remove_first_elem_from_list(ob, ob_p) if len(ans_arr) < m: zz.extend(oo[oo_p:]) zz.extend(oa[oa_p:]) zz.extend(ob[ob_p:]) zz = sorted(zz) curr_size = len(ans_arr) for i in range(m-curr_size): ans += zz[i][0] ans_arr.append(zz[i][1]) print(ans) assert len(ans_arr) == m for i in ans_arr: print(i + 1, end =" ")
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from collections import Counter, defaultdict BS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" def to_base(s, b): res = "" while s: res+=BS[s%b] s//= b return res[::-1] or "0" alpha = "abcdefghijklmnopqrstuvwxyz" from math import floor, ceil,pi primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919 ] def primef(n, plst = []): if n==1: return plst else: for m in primes: if n%m==0: return primef(n//m, plst+[m]) return primef(1, plst+[n]) def lmii(): return list(map(int, input().split())) def ii(): return int(input()) def countOverlapping(string,sub): count = start = 0 while True: start = string.find(sub, start)+1 if start > 0: count += 1 else: return count """ t = ii() for i in range(t): x,y,n = lmii() ns = int(n) a = n%x if a==y: print(n) else: n //= x n *= x n += y if n > ns: n -= x if n < 0: n = 0 print(n)""" """ t = ii() for i in range(t): n = int(input()) if n==1: print(0) else: pr = primef(n) c = Counter(pr) if c[2] > c[3]: print(-1) elif c[3]==0 or len(c) > 2: print(-1) else: if c[3] > 0 and (c[2] > 0 or len(c)==1): #print(c) print((c[3]+c[3]-c[2])) else: print(-1)""" """ t = ii() for i in range(t): n = int(input()) s = list(input()) while "()" in "".join(s): s = list("".join(s).replace("()", "", 1)) seen = 0 pos = 0 c = 0 while pos < len(s): seen += 1 if s[pos]=="(" else 0 #print(seen, pos, s[pos]) if 2*seen == len(s): break if s[pos]==")" and seen*2 < len(s): s.append(s.pop(pos)) c += 1 else: pos += 1 print(c)""" import heapq as hp n,k = lmii() firstKA = int(k) firstKB = int(k) nums = [lmii() for i in range(n)] nums.sort(key = lambda x: (x[0], abs(x[1]-x[2]))) chosen = [] for i in nums: #print(i, firstKA, firstKB) if firstKB==0 and firstKA==0: break if i[1]+i[2]==2: firstKA -= 1 if firstKA > 0 else 0 firstKB -= 1 if firstKB > 0 else 0 chosen.append(i) elif i[1]==1 and firstKA > 0: chosen.append(i) firstKA -= 1 elif i[2]==1 and firstKB > 0: chosen.append(i) firstKB -= 1 #print(i, firstKA, firstKB) #print(chosen) if firstKA > 0 or firstKB > 0: print(-1) else: tot = 0 fa = int(k) fb = int(k) chosen.sort(key = lambda x: (99999-x[0], abs(x[1]-x[2]))) for f in range(len(chosen)): s = chosen[f] if fa==fb==0: print(tot) break if s[1]+s[2]==2 and (fa > 0 or fb > 0): fa -= 1 if fa > 0 else 0 fb -= 1 if fb > 0 else 0 tot += s[0] elif s[1]==1 and fa > 0: fa -= 1 tot += s[0] elif s[2]==1 and fb > 0: tot += s[0] fb -= 1 if fa==fb==0: print(tot) break
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; public class ReadBooks { public static class Node{ long val; int index; Node(long val, int index){ this.val = val; this.index = index; } } static boolean insertValIntoList(LinkedList<Node> adj, Node valu,int k, long[] sum, int idx, boolean visit[]) { int i=0; if(adj.size()==0) { adj.add(valu); visit[valu.index] = true; sum[idx] = sum[idx] + valu.val; return true; } else if(adj.size() < k && valu.val >= adj.getLast().val) { adj.add(valu); sum[idx] = sum[idx] + valu.val; return true; } else { for(Node elm: adj) { if(valu.val < elm.val) { adj.add(i,valu); visit[valu.index] = true; if(adj.size() > k) { sum[idx] = sum[idx] - adj.getLast().val+ valu.val; visit[adj.getLast().index] = false; adj.removeLast(); }else { sum[idx] = sum[idx] + valu.val; } return true; } i++; } } return false; } static boolean insertCommonValIntoList(LinkedList<Node> adjA, LinkedList<Node> adjB, LinkedList<Node> adjC, long[] sum, Node val, int k, boolean visit[]) { if(adjA.size() + adjC.size() < k && adjB.size() + adjC.size() == k ) { insertValIntoList(adjC,val,k,sum, 2, visit); sum[1] = sum[1] - adjB.getLast().val; visit[adjB.getLast().index] = false; adjB.removeLast(); return true; } else if(adjA.size() + adjC.size() == k && adjB.size() + adjC.size() < k ) { insertValIntoList(adjC,val,k,sum, 2, visit); sum[0] = sum[0] - adjA.getLast().val; visit[adjA.getLast().index] = false; adjA.removeLast(); return true; } else if(adjA.size() + adjC.size() < k && adjB.size() + adjC.size() < k) { insertValIntoList(adjC,val,k,sum, 2, visit); return true; } else { long sum1 = sum[0]+sum[1]+sum[2]; long sum2 = sum1 - (adjA.size()==0?0:adjA.getLast().val) - (adjB.size()==0?0:adjB.getLast().val) + val.val; long sum3 = sum1 - (adjC.size()==0?0:adjC.getLast().val) + val.val; //System.out.println(">>>>>>>>>>> sum2: "+ sum2 +" sum3: "+ sum3 +" <<<<<<<<<<<<<"); if(sum1 > sum2 || sum1 > sum3) { if(sum2 < sum3) { sum[0] -= adjA.getLast().val; visit[adjA.getLast().index] = false; adjA.removeLast(); sum[1] -= adjB.getLast().val; visit[adjB.getLast().index] = false; adjB.removeLast(); insertValIntoList(adjC,val,k, sum,2, visit); return true; }else { sum[2] -= adjC.getLast().val; visit[adjC.getLast().index] = false; adjC.removeLast(); insertValIntoList(adjC,val,k, sum,2, visit); return true; } } else { return false; } } } public static void main(String[] args) { //System.out.println("hello world"); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String list[] = str.split(" "); int n = Integer.parseInt(list[0]); int m = Integer.parseInt(list[1]); int k = Integer.parseInt(list[2]); if(n<m || m<k) { System.out.println(-1); }else { Node arr[][] = new Node[n][3]; for(int i=0;i<n;i++) { str = sc.nextLine(); list = str.split(" "); arr[i][0] = new Node(Long.parseLong(list[0]), i); arr[i][1] = new Node(Long.parseLong(list[1]), i); arr[i][2] = new Node(Long.parseLong(list[2]), i); } LinkedList<Node> adjA = new LinkedList<>(); LinkedList<Node> adjB = new LinkedList<>(); LinkedList<Node> adjC = new LinkedList<>(); boolean visit[] = new boolean[n]; Arrays.fill(visit, false); long sum[] = new long[4]; Arrays.fill(sum, 0); for(int i=0;i<n;i++) { if(arr[i][1].val==1 && arr[i][2].val==0) { insertValIntoList(adjA, arr[i][0],k,sum,0, visit); } else if(arr[i][1].val==0 && arr[i][2].val==1) { insertValIntoList(adjB, arr[i][0],k,sum,1, visit); } /* * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val + * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println(""); * System.out.println("sumA: "+sum[0]+" sumB: "+sum[1]); * System.out.println(""); System.out.print("Visit : "); for(int * j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] + "], "); * }else { System.out.print(visit[j] + ", "); } } System.out.println(""); * System.out.println("======================"); */ } //System.out.println("==========TRIM LOOP============"); if(adjA.size() + adjB.size()>m) { for(int i=0;i<(adjA.size() + adjB.size() - m);i++) { if(adjA.getLast().val > adjB.getLast().val) { visit[adjA.getLast().index] = false; sum[0] -= adjA.getLast().val; adjA.removeLast(); }else { visit[adjB.getLast().index] = false; sum[1] -= adjB.getLast().val; adjB.removeLast(); } /* * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val + * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println(""); * System.out.println("sumA: "+sum[0]+" sumB: "+sum[1]); * System.out.println(""); System.out.print("Visit : "); for(int * j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] + "], "); * }else { System.out.print(visit[j] + ", "); } } System.out.println(""); * System.out.println("======================"); */ } } //System.out.println("==========2ND LOOP============"); for(int i=0;i<n;i++) { if(arr[i][1].val==1 && arr[i][2].val==1) { insertCommonValIntoList(adjA,adjB, adjC,sum, arr[i][0],k, visit); /* * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val + * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println(""); * System.out.print("adjC : "); for(Node elm: adjC) { System.out.print(elm.val + * " --> "); } System.out.println(""); * System.out.println("sumA: "+sum[0]+" sumB: "+sum[1]+" sumC: "+sum[2]); * System.out.println(""); System.out.print("Visit : "); for(int * j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] + "], "); * }else { System.out.print(visit[j] + ", "); } } System.out.println(""); * System.out.println("======================"); */ } } if(adjA.size()+adjB.size()+adjC.size() > m) { System.out.println(-1); }else if(adjA.size()+adjC.size() < k || adjB.size()+adjC.size() < k) { System.out.println(-1); } else { //System.out.println("==========3RD LOOP============"); LinkedList<Node> adjD = new LinkedList<>(); int read = adjA.size() + adjB.size() + adjC.size(); if(m-read != 0) { for(int i=0;i < n;i++) { if(visit[arr[i][0].index] == false) { insertValIntoList(adjD,arr[i][0],m-read,sum,3,visit); } /* * System.out.print("adjA : "); for(Node elm: adjA) { System.out.print(elm.val + * " --> "); } System.out.println(""); System.out.print("adjB : "); for(Node * elm: adjB) { System.out.print(elm.val + " --> "); } System.out.println(""); * System.out.print("adjC : "); for(Node elm: adjC) { System.out.print(elm.val + * " --> "); } System.out.println(""); System.out.print("adjD : "); for(Node * elm: adjD) { System.out.print(elm.val + " --> "); } System.out.println(""); * System.out.println("sumA: "+sum[0]+" sumB: "+sum[1]+" sumC: "+sum[2] * +" sumD: "+sum[3]); System.out.println(""); System.out.print("Visit : "); * for(int j=0;j<visit.length;j++) { if(j==i) { System.out.print("["+visit[j] + * "], "); }else { System.out.print(visit[j] + ", "); } } * System.out.println(""); System.out.println("======================"); */ } } System.out.println(sum[0]+sum[1]+sum[2]+sum[3]); if(!adjA.isEmpty()) { for(Node node: adjA) { System.out.print((node.index+1) + " "); } } if(!adjB.isEmpty()) { for(Node node: adjB) { System.out.print((node.index+1) + " "); } } if(!adjC.isEmpty()) { for(Node node: adjC) { System.out.print((node.index+1) + " "); } } if(!adjD.isEmpty()) { for(Node node: adjD) { System.out.print((node.index+1) + " "); } } } } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class E2 { static long m = (long) (1e9 + 7); public static void main(String[] args) throws IOException { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int m = scn.nextInt(); int k = scn.nextInt(); PriorityQueue<pair> a = new PriorityQueue<>(), b = new PriorityQueue<>(); PriorityQueue<pair> ab = new PriorityQueue<>(); PriorityQueue<pair> p123 = new PriorityQueue<>(); for (int i = 0; i < n; i++) { int t = scn.nextInt(); int f = scn.nextInt(); int s = scn.nextInt(); if (f == 1 && s == 1) { ab.add(new pair(t, i + 1, true)); } else if (f == 1) { a.add(new pair(t, i + 1, false)); } else if (s == 1) { b.add(new pair(t, i + 1, false)); } else { p123.add(new pair(t, i + 1, false)); } } int tot = 0, c = 0; boolean bool = false; ArrayList<Integer> al = new ArrayList<>(); while (k > 0) { if (m - c >= 2 && !a.isEmpty() && !b.isEmpty() && !ab.isEmpty() && (a.peek().vv + b.peek().vv) <= ab.peek().vv) { pair ff = a.poll(); pair ss = b.poll(); tot += ff.vv; tot += ss.vv; al.add(ff.ii); al.add(ss.ii); c += 2; } else if (!a.isEmpty() && !b.isEmpty() && !ab.isEmpty() && (a.peek().vv + b.peek().vv) > ab.peek().vv) { pair ff = ab.poll(); tot += ff.vv; al.add(ff.ii); c++; } else if (m - c >= 1 && !ab.isEmpty()) { pair ff = ab.poll(); tot += ff.vv; al.add(ff.ii); c++; } else if (m - c >= 2 && !a.isEmpty() && !b.isEmpty()) { pair ff = a.poll(); pair ss = b.poll(); tot += ff.vv; tot += ss.vv; al.add(ff.ii); al.add(ss.ii); c += 2; } else { bool = true; break; } k--; } int rr = m - (c); while (!ab.isEmpty()) { p123.add(ab.poll()); } while (!a.isEmpty()) { p123.add(a.poll()); } while (!b.isEmpty()) { p123.add(b.poll()); } while (rr > 0) { pair pp = p123.poll(); tot += pp.vv; al.add(pp.ii); rr--; } if (bool) { System.out.println(-1); } else { System.out.println(tot); for (int i = 0; i < al.size(); i++) System.out.print(al.get(i) + " "); System.out.println(); } } static class pair implements Comparable<pair> { int vv; int ii; boolean b; pair(int v, int i, boolean b) { this.vv = v; this.ii = i; this.b = b; } public int compareTo(pair o) { return this.vv - o.vv; } } static class Scanner { StringTokenizer st; BufferedReader br; 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()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.lang.*; import java.util.*; import java.io.*; public class Main5 { public static void main(String args[]) throws Exception { Scanner s=new Scanner(System.in); int n=s.nextInt(); int k=s.nextInt(); int c=0; int a[][]=new int[n][3]; int k1=k,k2=k; int yy1=0,yy2=0; TreeMap<Integer,Integer> x=new TreeMap<>(); TreeMap<Integer,Integer> y=new TreeMap<>(); for(int i=0;i<n;i++) { a[i][0]=s.nextInt(); a[i][1]=s.nextInt(); a[i][2]=s.nextInt(); if(a[i][1]==1 && a[i][2]==1) { c+=a[i][0]; k1--; k2--; a[i][1]=0; a[i][2]=0; yy1++; yy2++; } if(a[i][1]==1) { x.put(a[i][0], 1); yy1++; } if(a[i][2]==1) { y.put(a[i][2], 1); yy2++; } } if(yy1<k || yy2<k) { System.out.println(-1); } else { if(k1==0 && k2==0) { System.out.println(c); } else { if(k1!=0) { for(Integer i : x.keySet()) { c+=i; k1--; if(k1==0) { break; } } } if(k2!=0) { for(Integer i : y.keySet()) { c+=i; k2--; if(k2==0) { break; } } } System.out.println(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" ] }
IN-CORRECT
java
/* LINK: http://codeforces.com/problemset/problem/1374/E1 PROBLEM TASK: ΠŸΡ€ΠΎΡΡ‚Π°Ρ ΠΈ слоТная вСрсии Π½Π° самом Π΄Π΅Π»Π΅ ΡΠ²Π»ΡΡŽΡ‚ΡΡ Ρ€Π°Π·Π½Ρ‹ΠΌΠΈ Π·Π°Π΄Π°Ρ‡Π°ΠΌΠΈ, поэтому ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°ΠΉΡ‚Π΅ условия ΠΎΠ±Π΅ΠΈΡ… Π·Π°Π΄Π°Ρ‡ ΠΏΠΎΠ»Π½ΠΎΡΡ‚ΡŒΡŽ ΠΈ Π²Π½ΠΈΠΌΠ°Ρ‚Π΅Π»ΡŒΠ½ΠΎ. Π›Π΅Ρ‚Π½ΠΈΠ΅ ΠΊΠ°Π½ΠΈΠΊΡƒΠ»Ρ‹ Π½Π°Ρ‡Π°Π»ΠΈΡΡŒ, поэтому Алиса ΠΈ Π‘ΠΎΠ± хотят ΠΈΠ³Ρ€Π°Ρ‚ΡŒ ΠΈ Π²Π΅ΡΠ΅Π»ΠΈΡ‚ΡŒΡΡ, Π½ΠΎ... Π˜Ρ… ΠΌΠ°ΠΌΠ° Π½Π΅ согласна с этим. Она Π³ΠΎΠ²ΠΎΡ€ΠΈΡ‚, Ρ‡Ρ‚ΠΎ ΠΎΠ½ΠΈ Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ ΠΊΠ°ΠΊΠΎΠ΅-Ρ‚ΠΎ количСство ΠΊΠ½ΠΈΠ³ ΠΏΠ΅Ρ€Π΅Π΄ всСми развлСчСниями. Алиса ΠΈ Π‘ΠΎΠ± ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°ΡŽΡ‚ ΠΊΠ°ΠΆΠ΄ΡƒΡŽ ΠΊΠ½ΠΈΠ³Ρƒ вмСстС, Ρ‡Ρ‚ΠΎΠ±Ρ‹ быстрСС Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΡ‚ΡŒ это Π·Π°Π΄Π°Π½ΠΈΠ΅. Π’ сСмСйной Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ΅ Π΅ΡΡ‚ΡŒ n ΠΊΠ½ΠΈΠ³. i-я ΠΊΠ½ΠΈΠ³Π° характСризуСтся трСмя Ρ†Π΅Π»Ρ‹ΠΌΠΈ числами: ti β€” количСство Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Алиса ΠΈ Π‘ΠΎΠ± Π΄ΠΎΠ»ΠΆΠ½Ρ‹ ΠΏΠΎΡ‚Ρ€Π°Ρ‚ΠΈΡ‚ΡŒ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ Π΅Π΅, ai (Ρ€Π°Π²Π½ΠΎΠ΅ 1, Ссли АлисС нравится i-я ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0, Ссли Π½Π΅ нравится), ΠΈ bi (Ρ€Π°Π²Π½ΠΎΠ΅ 1, Ссли Π‘ΠΎΠ±Ρƒ нравится i-я ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0, Ссли Π½Π΅ нравится). ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ ΠΈΠΌ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹Π±Ρ€Π°Ρ‚ΡŒ ΠΊΠ°ΠΊΠΈΠ΅-Ρ‚ΠΎ ΠΊΠ½ΠΈΠ³ΠΈ ΠΈΠ· ΠΈΠΌΠ΅ΡŽΡ‰ΠΈΡ…ΡΡ n ΠΊΠ½ΠΈΠ³ Ρ‚Π°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ, Ρ‡Ρ‚ΠΎ: АлисС нравятся Π½Π΅ ΠΌΠ΅Π½Π΅Π΅ k ΠΊΠ½ΠΈΠ³ ΠΈΠ· Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠ³ΠΎ мноТСства ΠΈ Π‘ΠΎΠ±Ρƒ нравятся Π½Π΅ ΠΌΠ΅Π½Π΅Π΅ k ΠΊΠ½ΠΈΠ³ ΠΈΠ· Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠ³ΠΎ мноТСства; ΠΎΠ±Ρ‰Π΅Π΅ врСмя, Π·Π°Ρ‚Ρ€Π°Ρ‡Π΅Π½Π½ΠΎΠ΅ Π½Π° ΠΏΡ€ΠΎΡ‡Ρ‚Π΅Π½ΠΈΠ΅ этих ΠΊΠ½ΠΈΠ³ ΠΌΠΈΠ½ΠΈΠΌΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ (вСдь ΠΎΠ½ΠΈ Π΄Π΅Ρ‚ΠΈ ΠΈ хотят Π½Π°Ρ‡Π°Ρ‚ΡŒ ΠΈΠ³Ρ€Π°Ρ‚ΡŒ ΠΈ Π²Π΅ΡΠ΅Π»ΠΈΡ‚ΡŒΡΡ ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ скорСС). ΠœΠ½ΠΎΠΆΠ΅ΡΡ‚Π²ΠΎ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ ΠΎΠ½ΠΈ Π²Ρ‹Π±ΠΈΡ€Π°ΡŽΡ‚, ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠ΅ ΠΈ для Алисы ΠΈ для Π‘ΠΎΠ±Π° (ΠΎΠ½ΠΈ Ρ‡ΠΈΡ‚Π°ΡŽΡ‚ ΠΎΠ΄Π½ΠΈ ΠΈ Ρ‚Π΅ ΠΆΠ΅ ΠΊΠ½ΠΈΠ³ΠΈ), ΠΈ ΠΎΠ½ΠΈ Ρ‡ΠΈΡ‚Π°ΡŽΡ‚ всС ΠΊΠ½ΠΈΠ³ΠΈ вмСстС, Ρ‚Π°ΠΊΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ, суммарноС врСмя чтСния Ρ€Π°Π²Π½ΠΎ суммС ti ΠΏΠΎ всСм ΠΊΠ½ΠΈΠ³Π°ΠΌ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ находятся Π² Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠΌ мноТСствС. Π’Π°ΡˆΠ° Π·Π°Π΄Π°Ρ‡Π° β€” ΠΏΠΎΠΌΠΎΡ‡ΡŒ ΠΈΠΌ ΠΈ Π½Π°ΠΉΡ‚ΠΈ любоС подходящСС мноТСство ΠΊΠ½ΠΈΠ³ ΠΈΠ»ΠΈ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ, Ρ‡Ρ‚ΠΎ Ρ‚Π°ΠΊΠΎΠ΅ мноТСство Π½Π°ΠΉΡ‚ΠΈ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ ΠŸΠ΅Ρ€Π²Π°Ρ строка тСста содСрТит Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа n ΠΈ k (1≀k≀n≀2β‹…105). Π‘Π»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ n строк содСрТат описания ΠΊΠ½ΠΈΠ³, ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡƒ описанию Π² строкС: i-я строка содСрТит Ρ‚Ρ€ΠΈ Ρ†Π΅Π»Ρ‹Ρ… числа ti, ai ΠΈ bi (1≀ti≀104, 0≀ai,bi≀1), Π³Π΄Π΅: ti β€” количСство Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ, Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ΅ для прочтСния i-ΠΉ ΠΊΠ½ΠΈΠ³ΠΈ; ai, Ρ€Π°Π²Π½ΠΎΠ΅ 1, Ссли АлисС нравится i-я ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0 Π² ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΌ случаС; bi, Ρ€Π°Π²Π½ΠΎΠ΅ 1, Ссли Π‘ΠΎΠ±Ρƒ нравится i-я ΠΊΠ½ΠΈΠ³Π°, ΠΈ 0 Π² ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΌ случаС. Π’Ρ‹Ρ…ΠΎΠ΄Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ Если подходящСго Ρ€Π΅ΡˆΠ΅Π½ΠΈΡ Π½Π΅ сущСствуСт, Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ число -1. Π˜Π½Π°Ρ‡Π΅ Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ†Π΅Π»ΠΎΠ΅ число T β€” минимальноС суммарноС врСмя, Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ΅ для прочтСния подходящСго мноТСства ΠΊΠ½ΠΈΠ³. INPUT: 1) 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 2) 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 3) 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 OUTPUT: 1) 18 2) 8 3) -1 */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Comparator; public class E1_1374 { public static void main(String[] args) { try { new E1_1374().run(); } catch (IOException e) { System.exit(0); } } void run() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] input = in.readLine().split(" "); int n = Integer.parseInt(input[0]); int k = Integer.parseInt(input[1]); ArrayList<Book> aliceBooks = new ArrayList<>(n); ArrayList<Book> bobBooks = new ArrayList<>(n); ArrayList<Book> aliceAndBobBooks = new ArrayList<>(n); int aliceLikeCount = 0; int bobLikeCount = 0; int times = 0; for (int i = 0; i < n; i++) { String[] bookInfo = in.readLine().split(" "); int time = Integer.parseInt(bookInfo[0]); int alice = Integer.parseInt(bookInfo[1]); int bob = Integer.parseInt(bookInfo[2]); Book book = new Book(time, alice, bob); if (alice == 1 && bob == 1) { aliceAndBobBooks.add(book); } else if (alice == 1) { aliceBooks.add(book); } else if (bob == 1) { bobBooks.add(book); } aliceLikeCount += alice; bobLikeCount += bob; } if (aliceLikeCount >= k && bobLikeCount >= k) { int minSize = Math.min(aliceBooks.size(), bobBooks.size()); for (int i = 0; i < minSize; i++) { int time = aliceBooks.get(i).time + bobBooks.get(i).time; int alice = aliceBooks.get(i).alice + bobBooks.get(i).alice; int bob = aliceBooks.get(i).bob + bobBooks.get(i).bob; Book book = new Book(time, alice, bob); aliceAndBobBooks.add(book); } aliceAndBobBooks.sort(Comparator.comparingInt(o -> o.time)); for (int i = 0; i < k; i++) { times += aliceAndBobBooks.get(i).time; } } else times = -1; System.out.println(times); } private static class Book { private final int time; private final int alice; private final int bob; public Book(int time, int alice, int bob) { this.time = time; this.alice = alice; this.bob = bob; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool comp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) { if (a.first < b.first) return 1; if (a.first > b.first) return 0; return a.second.second + a.second.first > b.second.second + b.second.first; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; vector<pair<int, pair<int, int>>> two; int suma = 0, sumb = 0; for (int i = 1; i <= n; i++) { int t, a, b; cin >> t >> a >> b; suma += a, sumb += b; if (a + b > 0) two.push_back({t, {a, b}}); } if (suma < k || sumb < k) { cout << -1 << endl; return 0; } sort(two.begin(), two.end(), comp); int a = k, b = k; int mn = 0; int i = 0; vector<int> al, bb; while (i++ < two.size() && (a + b != 0)) { pair<int, pair<int, int>> p = two[i - 1]; if (a == 0 && p.second.second == 0) continue; if (b == 0 && p.second.first == 0) continue; if (p.second.first + p.second.second == 2) { mn += p.first; if (a == 0 && al.size() >= 1) { al.pop_back(), b -= 1; } else if (b == 0 && bb.size() >= 1) { bb.pop_back(), a -= 1; } else { a -= 1, b -= 1; } } else { if (p.second.first == 1) al.push_back(p.first); else bb.push_back(p.first); a -= p.second.first; b -= p.second.second; } } if (al.size() != bb.size()) cout << "NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO" << endl; else { for (int i = 0; i < al.size(); i++) mn += al[i]; for (int i = 0; i < bb.size(); i++) mn += bb[i]; cout << mn << endl; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; void need_for_speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int32_t main() { need_for_speed(); long long t = 1; vector<long long> a, b, c; while (t--) { long long n, k; cin >> n >> k; long long arr[n], brr[n], crr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i] >> brr[i] >> crr[i]; } for (long long i = 0; i < n; i++) { if (brr[i] == 1 and crr[i] == 1) { a.push_back(arr[i]); } else if (brr[i] == 1) { b.push_back(arr[i]); } else if (crr[i] == 1) { c.push_back(arr[i]); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); if (a.size() >= k) { long long sum = 0; long long ans = 0; for (long long i = 0; i < k; i++) { sum += a[i]; } if (b.size() == 0 or c.size() == 0) { cout << sum << "\n"; return 0; } else { ans = sum; long long d[a.size() + 1]; for (long long i = 0; i < a.size(); i++) { d[i] = a[i]; } for (long long i = k - 2; i >= 0; i--) { d[i] += d[i + 1]; } long long x = min(b.size(), c.size()); long long cnt = k - 1; for (long long i = 0; i < min(x, k); i++) { sum -= d[cnt]; sum += b[i]; sum += c[i]; if (sum >= 0) ans = min(ans, sum); cnt--; } cout << ans << "\n"; return 0; } } else { long long sum = 0; long long ans = 0; long long n = a.size(); for (long long i = 0; i < n; i++) sum += a[i]; long long rem = k - n; if (b.size() < rem or c.size() < rem) { cout << -1 << "\n"; return 0; } for (long long i = 0; i < rem; i++) { sum += b[i]; sum += c[i]; } ans = sum; long long d[a.size() + 1]; for (long long i = 0; i < a.size(); i++) { d[i] = a[i]; } for (long long i = n - 2; i >= 0; i--) { d[i] += d[i + 1]; } long long cnt = n - 1; long long x = min(b.size(), c.size()); for (long long i = rem; i < min(n, x); i++) { sum -= d[cnt]; sum += b[i]; sum += c[i]; if (sum > 0) ans = min(ans, sum); cnt--; } cout << ans << "\n"; return 0; } cout << -1 << "\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" ] }
IN-CORRECT
python3
def g(x): if len(x): return x[0][0] return float('inf') n,m,k=map(int,input().split()) q,w,e,r=[],[],[],[] for _ in range(n): t,a,b=map(int,input().split()) if a+b==2:q.append((t,_)) elif a==1:w.append((t,_)) elif b==1:e.append((t,_)) else:r.append((t,_)) q.sort(key=lambda x:x[0]) w.sort(key=lambda x:x[0]) e.sort(key=lambda x:x[0]) r.sort(key=lambda x:x[0]) if len(q)+len(w)<k or len(q)+len(e)<k or len(q)+len(w)+len(e)<m: print(-1) exit() ans=0 an=[] for _ in range(k): if len(q): if len(w) and len(e): if q[0][0]<w[0][0]+e[0][0]: asd=q.pop(0) ans+=asd[0] an.append(asd[1]) else: asd = w.pop(0) dsa = e.pop(0) ans+=asd[0]+dsa[0] an.append(asd[1]) an.append(dsa[1]) else: asd = q.pop(0) ans += asd[0] an.append(asd[1]) else: asd = w.pop(0) dsa = e.pop(0) ans += asd[0] + dsa[0] an.append(asd[1]) an.append(dsa[1]) for _ in range(m-len(an)): mn=min(g(q),g(w),g(e),g(r)) if len(q) and mn == q[0][0]: asd=q.pop(0) ans += asd[0] an.append(asd[1]) elif len(w) and mn == w[0][0]: asd=w.pop(0) ans += asd[0] an.append(asd[1]) elif len(e) and mn == e[0][0]: asd=e.pop(0) ans += asd[0] an.append(asd[1]) else: asd=r.pop(0) ans += asd[0] an.append(asd[1]) print(ans) for i in an:print(i+1,end=' ')
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# https://codeforces.com/contest/1374/problem/E1 def min_time(tot_books, books_like, read_time, a_time, b_time): time = [] temp_a = [] temp_b = [] if min(sum(a_time), sum(b_time)) >= books_like: for x in range(tot_books): if a_time[x] == b_time[x] == 1: time.append(read_time[x]) elif a_time[x] == 0 and b_time[x] == 1: temp_b.append(read_time[x]) elif a_time[x] == 1 and b_time[x] == 0: temp_a.append(read_time[x]) if len(time) > books_like: time.sort(reverse=True) time = time[len(time) - books_like:] temp_a.sort(), temp_b.sort() y = 0 while y != (min(len(temp_a), len(temp_b))): # print(min(len(temp_a), len(temp_b))) # print(y) # print(time, temp_a, temp_b) if len(time) != books_like: time.append(temp_a[y] + temp_b[y]) del (temp_a[y], temp_b[y]) y -= 1 if len(time) == books_like: y = -1 elif temp_a[y] + temp_b[y] < time[y]: time[y] = temp_a[y] + temp_b[y] else: break y += 1 # print(time, temp_a, temp_b) return sum(time) else: return -1 n, k = map(int, input().split()) t = [] a = [] b = [] for i in range(n): x, y, z = map(int, input().split()) t.append(x), a.append(y), b.append(z) print(min_time(n, k, t, a, b))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# Secdra @2020 from functools import cmp_to_key class Node: def __init__(self, t, a, b): self.t = t self.a = a self.b = b def __lt__(self, other): if (self.a + self.b) == (other.a + other.b): return self.t > other.t else: return (self.a + self.b) > (other.a + other.b) # if self.t == other.t: # return (self.a + self.b) > (other.a + other.b) # return self.t < other.t def main(): n, k = map(int, input().split()) data = list() for _ in range(n): t, a, b = map(int, input().split()) if a != 0 or b != 0: book = Node(t, a, b) data.append(book) data.sort() ans = 0 done_a = 0 done_b = 0 for book in data: if (done_a >= k and book.b != 1) or (done_b >= k and book.a != 1): continue ans += book.t done_a += book.a done_b += book.b if done_a >= k and done_b >= k: print(ans) return print(-1) if __name__ == '__main__': main()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long value(vector<long long> a) { long long res = 0; while (a.empty()) { res *= 10; res += (*(a.end() - 1)); a.pop_back(); } return res; } signed main() { long long n, m, k; cin >> n >> m >> k; vector<pair<long long, long long>> c; vector<pair<long long, long long>> d; vector<pair<long long, long long>> e; vector<pair<long long, long long>> f; for (long long i = 0; i < n; i++) { long long t, a, b; cin >> t >> a >> b; if (a && b) c.push_back({t, i + 1}); else if (a) d.push_back({t, i + 1}); else if (b) e.push_back({t, i + 1}); else f.push_back({t, i + 1}); } set<long long> st; if (c.size() + d.size() < k || c.size() + e.size() < k) { cout << -1 << endl; return 0; } sort(c.begin(), c.end()); sort(d.begin(), d.end()); sort(e.begin(), e.end()); long long cnt = 0; long long p = 0; long long q = 0; long long w = 0, x = 0, y = 0; long long ans = 0; while (c.size() - w && d.size() - x && e.size() - y && (p < k || q < k)) { if (c[w].first <= d[x].first + e[y].first || cnt + 1 == m) { w++; p++; q++; cnt++; ans += c[w - 1].first; st.insert(c[w - 1].second); } else if (c[w].first > d[x].first + e[y].first) { st.insert(d[x].second); st.insert(e[y].second); x++; p++; q++; y++; cnt += 2; ans += d[x - 1].first + e[y - 1].first; } } if (p < k && c.size() - w == 0) { while (c.size() + x < k) { st.insert(d[x].second); ans += d[x].first; x++; p++; cnt++; } } else if (p < k && d.size() - x == 0) { while (d.size() + w < k) { st.insert(c[w].second); ans += c[w].first; w++; p++; q++; cnt++; } } if (q < k && c.size() - w == 0) { while (c.size() + y < k) { st.insert(e[y].second); ans += e[y].first; y++; q++; cnt++; } } else if (q < k && e.size() - y == 0) { while (e.size() + w < k) { st.insert(c[w].second); ans += c[w].first; w++; p++; q++; cnt++; } } if (cnt > m) { cout << -1 << endl; return 0; } set<pair<long long, long long>> pq; for (long long i = w; i < c.size(); i++) pq.insert({c[i].first, c[i].second}); for (long long i = x; i < d.size(); i++) pq.insert({d[i].first, d[i].second}); for (long long i = y; i < e.size(); i++) pq.insert({e[i].first, e[i].second}); for (long long i = 0; i < f.size(); i++) pq.insert({f[i].first, f[i].second}); while (cnt < m) { cnt++; ans += pq.begin()->first; st.insert(pq.begin()->second); pq.erase(pq.begin()); } cout << ans << endl; for (auto it = st.begin(); it != st.end(); it++) cout << *it << " "; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.Scanner; public class ReadingBook { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int books=0,books1=0,alice=0,bob=0,alice1=0,bob1=0,alice2=0,bob2=0,books2=0; while(n-- >0) { int t=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if(k==1) { if(t==1 && a==1 && b==1) { System.out.println(t); break; } } else { if(a==1 && b==1) { books+=t; bob++; alice++; } if(a==1 && b==0) { books1+=t; alice1++; } if(a==0 && b==1) { books1+=t; bob1++; } if(a==1 && b==0) { books2+=t; alice2++; } if(a==0 && b==1) { books2+=t; bob2++; } if(a==1 && b==1) { books2+=t; bob2++; alice2++; } } } if(k>1) { if(bob==k && alice==k) System.out.println(books); else if(bob1==k && alice1==k) System.out.println(books1); else if(bob2==k && alice2==k) System.out.println(books2); else System.out.println(-1); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int A[1000000], B[1000000], C[1000000]; int main() { int n, k, t, a, b; cin >> n >> k; int num1 = 0, num2 = 0, num3 = 0; for (int i = 1; i <= n; i++) { cin >> t >> a >> b; if (a == 1 && b != 1) A[++num1] = t; if (a == 0 && b == 1) B[++num2] = t; if (a == 1 && b == 1) C[++num3] = t; } if (num1 + num3 < k || num2 + num3 < k) { cout << -1; return 0; } sort(A + 1, A + num1 + 1); sort(B + 1, B + num2 + 1); sort(C + 1, C + num3 + 1); int i = 1, j = 1; int num = 0; int sum = 0; while (num < k) { if ((i > num1 && num1 < k) || (i > num2 && num2 < k)) { sum += C[j]; j++; num++; continue; } if (A[i] + B[i] <= C[j] || j >= num3) { sum += (A[i] + B[i]); i++; } else { sum += C[j]; j++; } num++; } cout << sum << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; long long t[n], a[n], b[n]; vector<long long> ali, bob, both; for (long long i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) both.push_back(t[i]); if (a[i] == 1 && b[i] == 0) ali.push_back(t[i]); if (a[i] == 0 && b[i] == 1) bob.push_back(t[i]); } sort(ali.begin(), ali.end()); sort(bob.begin(), bob.end()); sort(both.begin(), both.end()); long long mini = 300000; long long x = both.size(); long long y = ali.size(); long long z = bob.size(); long long m = max(k - z, k - y); if (x + y < k || x + z < k) cout << "-1\n"; else { for (long long i = max(m, 0ll); i <= min(x, k); i++) { long long sum = 0; for (long long j = 0; j < i; j++) { sum += both[j]; } for (long long j = 0; j < k - i; j++) { sum += ali[j]; sum += bob[j]; } mini = min(mini, sum); } cout << mini << "\n"; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class e1 { public static PrintWriter out; public static InputReader in; public static void makePre(Pair arr[], int ln) { int s = 0; for(int i=0;i<ln;i++) { s+=arr[i].a; arr[i].a=s; } } static class Pair{ public int a,b; public Pair(int a, int b){ this.a=a; this.b=b; } } public static void main(String[] args)throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); Pair bob[] = new Pair[n]; Pair alice[] = new Pair[n]; Pair com[] = new Pair[n]; ArrayList<Pair> other = new ArrayList<Pair>(); Arrays.fill(bob,new Pair(-1,-1)); Arrays.fill(alice,new Pair(-1,-1)); Arrays.fill(com,new Pair(-1,-1)); int b=0,a=0,c=0; for(int t = 0; t < n; t++){ int ti = in.nextInt(); int ai = in.nextInt(); int bi = in.nextInt(); if(ai==1 && bi==1) { com[c++]=new Pair(ti,t); } else if(ai==1 && bi==0) { alice[a++]=new Pair(ti,t); } else if(ai==0 && bi==1) { bob[b++]=new Pair(ti,t); } else { other.add(new Pair(ti,t)); } } mergesort(alice,0,a-1); mergesort(bob,0,b-1); mergesort(com,0,c-1); makePre(com,c); makePre(bob,b); makePre(alice,a); int ans = Integer.MAX_VALUE; int aopt = -1, bopt=-1, copt=-1; for(int i=0;i<=k;i++) { if(i>0 && com[i-1].a==-1) continue; int sm=0; if(i>0) sm = com[i-1].a; if(i==k){ if(sm<ans) { aopt = 0; bopt=0; copt=i; } ans = Math.min(ans,sm); } else { if(k-i>b) continue; sm+=bob[k-i-1].a; if(k-i>a) continue; sm+=alice[k-i-1].a; } if((k-i+k-i+i)>m) continue; if(sm<ans) { aopt = k-i; bopt=k-i; copt=i; } ans = Math.min(ans,sm); } if(ans==Integer.MAX_VALUE) out.println(-1); else{ for(int i=aopt;i<n;i++) { if(alice[i].a<0) break; if(i==0) other.add(alice[i]); else other.add(new Pair(alice[i].a-alice[i-1].a,alice[i].b)); } for(int i=bopt;i<n;i++) { if(bob[i].a<0) break; if(i==0) other.add(bob[i]); else other.add(new Pair(bob[i].a-bob[i-1].a,bob[i].b)); } for(int i=copt;i<n;i++) { if(com[i].a<0) break; if(i==0) other.add(com[i]); else other.add(new Pair(com[i].a-com[i-1].a,com[i].b)); } // out.println("Copt "+copt+" bopt "+bopt+" aopt "+aopt); Collections.sort(other,(p1,p2) -> p1.a-p2.a); // out.println(other); for(int i=0;i<m-(copt+bopt+aopt);i++) { ans+=other.get(i).a; } out.println(ans); for(int i=0;i<copt;i++){ out.print((com[i].b+1)+" "); } for(int i=0;i<bopt;i++){ out.print((alice[i].b+1)+" "); out.print((bob[i].b+1)+" "); } for(int i=0;i<m-(copt+bopt+aopt);i++){ out.print((other.get(i).b+1)+" "); } out.println(); } out.close(); } public static void merge(Pair[] arr, int first,int mid,int last){ Pair a[]=new Pair[mid-first+1]; Pair b[]=new Pair[last-mid]; // int c[]=new int[mid-first+1]; // int d[]=new int[last-mid]; int pahela,dusra; pahela=dusra=0; for(pahela=0;pahela<mid-first+1;pahela++) { a[pahela]=arr[first+pahela]; // c[pahela]=indexes[first+pahela]; } for(dusra=0;dusra<last-mid;dusra++) { b[dusra]=arr[mid+1+dusra]; // d[dusra]=indexes[mid+1+dusra]; } pahela=0; dusra=0; int maha=first; while(pahela<mid-first+1 && dusra<last-mid) { if(a[pahela].a<=b[dusra].a) { arr[maha]=a[pahela]; // indexes[maha]=c[pahela]; maha++; pahela++; } else { arr[maha]=b[dusra]; // indexes[maha]=d[dusra]; maha++; dusra++; } } while(pahela<mid-first+1) { arr[maha]=a[pahela]; // indexes[maha]=c[pahela]; maha++; pahela++; } while(dusra<last-mid){ arr[maha]=b[dusra]; // indexes[maha]=d[dusra]; maha++; dusra++; } } public static void mergesort(Pair arr[],int first,int last) { if(first < last) { int mid=(first+last)/2; mergesort(arr,first,mid); mergesort(arr,mid+1,last); merge(arr,first,mid,last); } } 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()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
def solve(): n, k = list(map(int, input().split())) a = [] b = [] both = [] alice = 0 bob = 0 coincidence = 0 for i in range(n): t, a_, b_ = list(map(int, input().split())) if b_ and a_: both.append(t) coincidence += 1 elif a_ == 1: a.append(t) alice += 1 elif b_ == 1: b.append(t) bob += 1 if alice+coincidence < k or bob+coincidence<k: print(-1) else: both.sort() a.sort() b.sort() out = 0 while k>0: if len(a) == 0 or len(b) == 0: out += sum(both[:k]) k = 1 elif len(both) > 0: minimun = a[0] + b[0] aux = [val for val in both if val > minimun] len_aux = len(aux) if k < len_aux: out += sum(aux[:k]) del both[:k] k = 1 elif 0 < len_aux: out += sum(aux) del both[:len_aux] k -= len_aux-1 else: out += minimun a.pop(0) b.pop(0) else: out += sum(a[:k]) + sum(b[:k]) k = 1 k -= 1 print(out) cases = 1 for test in range(cases): solve()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
function sortFunc(x, y) { var likeBothX = x[1] && x[2]; var likeBothY = y[1] && y[2]; if (likeBothX) { if (likeBothY) { return x[0] - y[0]; } return -1; } if (likeBothY) { return 1; } return x[0] - y[0]; } function solve() { var nk = read().split(' ').map(Number); var n = nk[0]; var k = nk[1]; var ca = 0; var cb = 0; var arr = []; for (var i = 0; i < n; i++) { var tab = read().split(' ').map(Number); if (!tab[1] && !tab[2]) { continue; } arr.push(tab); ca += tab[1]; cb += tab[2]; } if (ca < k || cb < k) { write(-1); return; } n = arr.length; arr.sort(sortFunc); var ans = 0; ca = k; cb = k; for (var i = 0; i < n; i++) { if (arr[i][1]) { if (arr[i][2]) { ca--; cb--; if (ca >=0 || cb >= 0) { ans += arr[i][0]; } } else { ca--; if (ca >= 0) { ans += arr[i][0]; } } } else { if (arr[i][2]) { cb--; if (cb >= 0) { ans += arr[i][0]; } } } } write(ans); } function init() { var T; T = 1; //T = Number(read()); for (var t = 0; t < T; t++) { solve(); } } var isNode = typeof console !== 'undefined'; var MEMORY = []; var MEMORY_INDEX = 0; if (isNode) { var fs = require('fs'); var path = require('path'); var inTextName; var outTextName; if (process.argv.length === 5) { inTextName = process.argv[3]; outTextName = process.argv[4]; } if (inTextName) { fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => { MEMORY = data.split('\r\n'); init(); }); } else { var RL = require('readline').createInterface({ input : process.stdin, output : process.stdout }); RL.on('line', (line) => { MEMORY.push(line); }); RL.on('close', init); } } else { init(); } function write(value) { isNode ? console.log(value) : print(value); } function read() { return isNode ? MEMORY[MEMORY_INDEX++] : readline(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static class Pair { int t; int love; Pair (int t, int love) { this.t = t; this.love = love; } } public static void main(String[] args) { FastReader scan = new FastReader(); int n = scan.nextInt(); int k = scan.nextInt(); int cntAlice = 0; int cntBob = 0; Pair[] arr = new Pair[n]; for(int i=0;i<n;i++) { int t = scan.nextInt(); int a = scan.nextInt(); int b = 2*scan.nextInt(); arr[i] = new Pair(t, a+b); if(a != 0) { cntAlice++; } if(b != 0) { cntBob++; } } if(cntAlice < k || cntBob < k) { System.out.println(-1); return; } Arrays.sort(arr,((o1, o2) -> { if(o1.t == o2.t) { return o2.love - o1.love; } return o1.t-o2.t; })); /* for(Pair p : arr) { System.out.println(p.t+" "+(p.love > 2 ? "1 1" : (p.love > 1 ? "0 1" : (p.love > 0 ? "1 0" :"0 0")))); }*/ Stack<Integer> alice = new Stack<>(); Stack<Integer> bob = new Stack<>(); long ans = 0; int cnt = 0; for(Pair p : arr) { if(p.love == 0) { continue; } if(p.love == 3) { //alice & bob cnt++; if(!alice.isEmpty() && cnt+alice.size() > k) { ans -= alice.pop(); } if(!bob.isEmpty() && cnt+bob.size() > k) { ans -= bob.pop(); } ans += p.t; } else if(p.love == 1) { //alice only if(alice.size()+cnt < k) { ans += p.t; alice.add(p.t); } } else if(p.love == 2) { //bob only if(bob.size()+cnt < k) { ans += p.t; bob.add(p.t); } } //System.out.println(ans); if(alice.size()+cnt >= k && bob.size()+cnt >= k) { break; } } 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" ] }
IN-CORRECT
python3
# cook your dish here n,k=map(int,input().split()) a=[0 for i in range(n)] b=[0 for i in range(n)] x=[] cnta=0 cntb=0 for i in range(n): t,p,q=list(map(int,input().split())) t=[t,p,q] x.append(t) x.sort() for i in range(n): t,p,q=x[i] if p: a[i]=1 cnta+=1 if q: b[i]=1 cntb+=1 #t=[t,p,q] #x.append(t) #print(x) if cnta>=k and cntb>=k: out1=0 out2=0 out3=0 cnt=0 ac=0 bc=0 aii=a.copy() bii=b.copy() if 1: for i in range(n): if cnt==k: break if a[i] and b[i]: cnt+=1 out3+=x[i][0] if 1: for i in range(n): if ac==k: break if aii[i]: aii[i]=0 ac+=1 if bii[i]: bii[i]=0 bc+=1 out1+=x[i][0] if bc<k: for i in range(n): if bc==k: break if bii[i]: bii[i]=0 out1+=x[i][0] bc+=1 ac=0 bc=0 if 1: for i in range(n): if bc==k: break if b[i]: b[i]=0 bc+=1 if a[i]: a[i]=0 ac+=1 out2+=x[i][0] if ac<k: for i in range(n): if ac==k: break if a[i]: a[i]=0 out2+=x[i][0] ac+=1 if cnt==k: print(min(out1,out2,out3)) else: print(min(out1,out2)) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k=map(int,input().split()) kc=k v1,v2,v12=[],[],[] for _ in range(n): t,l1,l2=map(int,input().split()) if l1==1 and l2==1: v12.append(t) elif l1==1: v1.append(t) elif l2==1: v2.append(t) v1.sort() v2.sort() v12.sort() x,y,z=0,0,0 ans=0 sb=k-min(len(v1),len(v2)) sb = sb if sb>0 else 0 k-=sb if sb>len(v12): print(-1) else: for z in range(sb): ans+=v12[z] z=sb while(k>0 and z< len(v12)): if v12[z]<= v1[x]+v2[y]: ans+=v12[z] z+=1 else: ans+=v1[x]+v2[y] x+=1 y+=1 k-=1 if k<=(len(v1)-x) and k<= (len(v2)-y): while k>0: ans+=v1[x]+v2[y] x+=1 y+=1 k-=1 if kc < m: fa=v1[x:]+v2[:y]+v12[z:] fa.sort() ans+=sum(fa[:m-k]) print(ans) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; 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; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion(); solver.solve(1, in, out); out.close(); } static class E1ReadingBooksEasyVersion { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int n = sc.nextInt(); int k = sc.nextInt(); TreeSet<pair> ts1 = new TreeSet<>(); TreeSet<pair> ts2 = new TreeSet<>(); TreeSet<pair> ts3 = new TreeSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); int t1 = sc.nextInt(); int t2 = sc.nextInt(); if (t1 == 1 && t2 == 1) ts3.add(new pair(x, t1, t2)); else if (t1 == 1) ts1.add(new pair(x, t1, t2)); else if (t2 == 1) ts2.add(new pair(x, t1, t2)); } while (ts1.size() > 0 && ts2.size() > 0) { ts3.add(new pair(ts1.pollFirst().a + ts2.pollFirst().a, 1, 1)); } long ans = 0; while (k > 0 && ts3.size() > 0) { ans += 1l * ts3.pollFirst().a; k--; } pw.println(k == 0 ? ans : -1); } public class pair implements Comparable<pair> { int a; int b; int c; public pair(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int compareTo(pair pair) { return a - pair.a; } public String toString() { return a + " " + b + " " + c; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from __future__ import division, print_function # import threading # threading.stack_size(2**27) # import sys # sys.setrecursionlimit(10**7) # sys.stdin = open('inpy.txt', 'r') # sys.stdout = open('outpy.txt', 'w') from sys import stdin, stdout import bisect #c++ upperbound import math import heapq # i_m=9223372036854775807 def inin(): return int(input()) def stin(): return input() def spin(): return map(int,stin().split()) def lin(): #takes array as input return list(map(int,stin().split())) def matrix(n): #matrix input return [list(map(int,input().split()))for i in range(n)] ################################################ def count2Dmatrix(i,list): return sum(c.count(i) for c in list) def modinv(n,p): return pow(n,p-2,p) def GCD(x, y): x=abs(x) y=abs(y) if(min(x,y)==0): return max(x,y) while(y): x, y = y, x % y return x def Divisors(n) : l = [] for i in range(1, int(math.sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) return l prime=[] def SieveOfEratosthenes(n): global prime prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 f=[] for p in range(2, n): if prime[p]: f.append(p) return f q=[] def dfs(n,d,v,c): global q v[n]=1 x=d[n] q.append(n) j=c for i in x: if i not in v: f=dfs(i,d,v,c+1) j=max(j,f) # print(f) return j # d = {} """*******************************************************""" n, k = spin() alice = []; bob = [] for _ in range(n): t, a, b = spin() if a==1: alice.append(t) if b==1: bob.append(t) if len(alice)<k or len(bob)<k: print(-1) exit else: alice = sorted(alice) bob = sorted(bob) # print(alice, bob) sa = sum(alice[:4]);sb = sum(bob[:4]) def intersection(lst1, lst2): temp = set(lst2) lst3 = [value for value in lst1 if value in temp] return lst3 common = sum(intersection(alice[:4], bob[:4])) # print(intersection(alice, bob)) if alice==intersection(alice, bob) or bob==intersection(alice, bob): print(sum(intersection(alice, bob))) else: print(sa+sb-common)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import java.util.HashSet; import java.util.Arrays; import java.util.Scanner; import java.util.Set; import java.util.ArrayList; import java.util.Collections; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.text.DecimalFormat; import java.lang.Math; import java.util.Iterator; import java.util.TreeSet; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.*; public class D1343{ public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static long MOD = (long)(1e9+7); static FastReader sc = new FastReader(); static int pInf = Integer.MAX_VALUE; static int nInf = Integer.MIN_VALUE; public static void main(String[] args){ int t = 1; while(t-->0){ int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> A = new ArrayList<Integer>(); ArrayList<Integer> B = new ArrayList<Integer>(); ArrayList<Integer> AB = new ArrayList<Integer>(); while(n-->0){ int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if(y==1 && z==1){ AB.add(x); } else if(y==1 && z==0){ A.add(x); } else if(z==1 && y==0){ B.add(x); } } Collections.sort(A); Collections.sort(AB); Collections.sort(B); long sum = 0; while(k-->0){ if((A.size()>0) && (AB.size()>0) && (B.size()>0)){ if(A.get(0)+B.get(0)>AB.get(0)){ sum += A.get(0)+B.get(0); A.remove(0); B.remove(0); } else{ sum += AB.get(0); AB.remove(0); } } else if(AB.size()>0){ sum += AB.get(0); AB.remove(0); } else if(A.size()>0 && B.size()>0){ sum += A.get(0)+B.get(0); A.remove(0); B.remove(0); } else{ sum = -1; } } out.println(sum); } out.close(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Integer.lowestOneBit(i) Equals k where k is the position of the first one in the binary //Integer.highestOneBit(i) Equals k where k is the position of the last one in the binary //Integer.bitCount(i) returns the number of one-bits //Collections.sort(A,(p1,p2)->(int)(p2.x-p1.x)) To sort ArrayList in descending order wrt values of x. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Pair Class static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub if(this.x==o.x){ return (this.y-o.y); } return (this.x-o.x); } } //Merge Sort static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long [n1]; long R[] = new long [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } //Brian Kernighan’s Algorithm static long countSetBits(long n){ if(n==0) return 0; return 1+countSetBits(n&(n-1)); } //Factorial static long factorial(long n){ if(n==1) return 1; if(n==2) return 2; if(n==3) return 6; return n*factorial(n-1); } //Euclidean Algorithm static long gcd(long A,long B){ if(B==0) return A; return gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD; return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD; } //AKS Algorithm static boolean isPrime(long n){ if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Reverse an array static <T> void reverse(T arr[],int l,int r){ Collections.reverse(Arrays.asList(arr).subList(l, r)); } //Sieve of eratosthenes static int[] findPrimes(int n){ boolean isPrime[]=new boolean[n+1]; ArrayList<Integer> a=new ArrayList<>(); int result[]; Arrays.fill(isPrime,true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<=n;++i){ if(isPrime[i]==true){ for(int j=i*i;j<=n;j+=i) isPrime[j]=false; } } for(int i=0;i<=n;i++) if(isPrime[i]==true) a.add(i); result=new int[a.size()]; for(int i=0;i<a.size();i++) result[i]=a.get(i); return result; } //Euler Totent function static long countCoprimes(long n){ ArrayList<Long> prime_factors=new ArrayList<>(); long x=n,flag=0; while(x%2==0){ if(flag==0) prime_factors.add(2L); flag=1; x/=2; } for(long i=3;i*i<=x;i+=2){ flag=0; while(x%i==0){ if(flag==0) prime_factors.add(i); flag=1; x/=i; } } if(x>2) prime_factors.add(x); double ans=(double)n; for(Long p:prime_factors){ ans*=(1.0-(Double)1.0/p); } return (long)ans; } public static int bSearch(int n,ArrayList<Integer> A){ int s = 0; int e = A.size()-1; while(s<=e){ int m = s+(e-s)/2; if(A.get(m)==(long)n){ return A.get(m); } else if(A.get(m)>(long)n){ e = m-1; } else{ s = m+1; } } return A.get(e); } static long modulo = (long)1e9+7; public static long modinv(long x){ return modpow(x, modulo-2); } public static long modpow(long a, long b){ if(b==0){ return 1; } long x = modpow(a, b/2); x = (x*x)%modulo; if(b%2==1){ return (x*a)%modulo; } return x; } public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
""" Code of Ayush Tiwari Codeforces: servermonk Codechef: ayush572000 """ import sys input = sys.stdin.buffer.readline def solution(): a=[] b=[] c=[] n,k=map(int,input().split()) for i in range(n): t,x,y=map(int,input().split()) if x==1 and y==1: c.append(t) elif x==1: a.append(t) elif y==1: b.append(t) a.sort() b.sort() c.sort() ans=0 cnta=0 cntb=0 if len(a)+len(c)<k or len(b)+len(c)<k: print(-1) else: z=0 i=0 j=0 while k>0: if i<len(c) and j<min(len(a),len(b)): if a[j]+b[j]<k: ans+=a[j]+b[j] j+=1 else: ans+=c[i] i+=1 elif i<len(c): ans+=c[i] i+=1 else: ans+=a[j]+b[j] j+=1 k-=1 print(ans) solution()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class ReadingBooks { static class Book { int t; int a; int b; Book(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } } static class SortbyTime implements Comparator<Book> { public int compare(Book a, Book b) { return a.t - b.t; } }static class SortbyA implements Comparator<Book> { public int compare(Book a, Book b) { return a.a - b.a; } }static class SortbyB implements Comparator<Book> { public int compare(Book a, Book b) { return a.b - b.b; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); Book[] books = new Book[n]; for (int i = 0; i < n; i++) { int t = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); books[i] = new Book(t, a, b); } Arrays.sort(books, new SortbyB()); Arrays.sort(books, new SortbyA()); Arrays.sort(books, new SortbyTime()); int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int tt = 0; int at = 0; int bt = 0; for (int j = i; j < n; j++) { if (!(books[j].a == 0 && books[j].b == 0)) { if (at >= k && books[j].b != 0) { bt += books[j].b; tt += books[j].t; } else if (bt >= k && books[j].a != 0) { at += books[j].a; tt += books[j].t; }else{ at += books[j].a; bt += books[j].b; tt += books[j].t; } if (at >= k && bt >= k && min > tt) { min = tt; } } } } if(min !=Integer.MAX_VALUE) System.out.println(min); else System.out.println("-1"); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.PriorityQueue; import java.io.BufferedWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.AbstractCollection; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Hello */ 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); E2ReadingBooksHardVersion solver = new E2ReadingBooksHardVersion(); solver.solve(1, in, out); out.close(); } static class E2ReadingBooksHardVersion { public void solve(int testNumber, InputReader c, OutputWriter w) { int n = c.readInt(), m = c.readInt(), k = c.readInt(); ArrayList<Pair<Integer, Integer>> a = new ArrayList<>(), b = new ArrayList<>(); PriorityQueue<Pair<Integer, Pair<Integer, Integer>>> tot = new PriorityQueue<>(); ArrayList<Pair<Integer, Pair<Integer, Integer>>> whoCare = new ArrayList<>(); HashSet<Integer> resInd = new HashSet<>(); int cst[] = new int[n]; for (int i = 0; i < n; i++) { int t = c.readInt(), aa = c.readInt(), bb = c.readInt(); cst[i] = t; if (aa == 0 || bb == 0) { if (aa == 1) { a.add(new Pair<>(t, i)); } else if (bb == 1) { b.add(new Pair<>(t, i)); } else { whoCare.add(new Pair<>(t, new Pair<>(i, 0))); } } else { tot.add(new Pair<>(t, new Pair<>(0, i))); } } Collections.sort(a); Collections.sort(b); HashMap<Integer, Pair<Pair<Integer, Integer>, Pair<Integer, Integer>>> kk = new HashMap<>(); int id = 1; for (int i = 0; i < a.size() && i < b.size(); i++) { Pair<Integer, Integer> a1 = a.get(i), b1 = b.get(i); tot.add(new Pair<>(a1.first + b1.first, new Pair<>(id, -1))); kk.put(id, new Pair<>(a1, b1)); id++; } PriorityQueue<Pair<Integer, Integer>> extra_a = new PriorityQueue<>(), extra_b = new PriorityQueue<>(); for (int i = a.size(); i < b.size(); i++) { extra_b.add(b.get(i)); whoCare.add(new Pair<>(b.get(i).first, new Pair<>(b.get(i).second, 1))); } for (int i = b.size(); i < a.size(); i++) { extra_a.add(a.get(i)); whoCare.add(new Pair<>(a.get(i).first, new Pair<>(a.get(i).second, 2))); } if (tot.size() < k) { w.printLine("-1"); return; } long res = 0; int tot_book = 0; PriorityQueue<PP> customize = new PriorityQueue<>(); PriorityQueue<PP> customize2 = new PriorityQueue<>(); while (k-- > 0) { Pair<Integer, Pair<Integer, Integer>> pp = tot.poll(); res += pp.first; if (pp.second.first != 0) { tot_book += 2; customize.add(new PP(pp.first, pp.second.first)); Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> pkp = kk.get(pp.second.first); resInd.add(pkp.first.second); resInd.add(pkp.second.second); } else { tot_book++; resInd.add(pp.second.second); customize2.add(new PP(pp.first, pp.second.second)); } } // w.printLine(res, tot_book); // w.printLine(customize); // System.out.println(customize.poll()); if (tot_book > m) { PriorityQueue<Pair<Integer, Integer>> pp = new PriorityQueue<>(); while (!tot.isEmpty()) { Pair<Integer, Pair<Integer, Integer>> ppq = tot.poll(); if (ppq.second.first == 0) { pp.add(new Pair<>(ppq.first, ppq.second.second)); } } while (!pp.isEmpty()) { Pair<Integer, Integer> kt = pp.poll(); PP rem = customize.poll(); res -= rem.first; Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> klk = kk.get(rem.second); resInd.remove(klk.first.second); resInd.remove(klk.second.second); res += kt.first; resInd.add(kt.second); tot_book--; if (tot_book == m) { break; } } } else if (tot_book == m) { } else { int great = 0; while (!tot.isEmpty()) { Pair<Integer, Pair<Integer, Integer>> pk = tot.poll(); if (pk.second.first == 0) { whoCare.add(new Pair<>(pk.first, new Pair<>(pk.second.second, 3))); } else { Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> ll = kk.get(pk.second.first); whoCare.add(new Pair<>(ll.first.first, new Pair<>(ll.first.second, 2))); extra_a.add(ll.first); whoCare.add(new Pair<>(ll.second.first, new Pair<>(ll.second.second, 1))); extra_b.add(ll.second); } } Collections.sort(whoCare); // verify once int extra = 0, aaa = 0, bbb = 0; for (int i = 0; i < whoCare.size() && tot_book < m; i++, tot_book++) { Pair<Integer, Pair<Integer, Integer>> pqp = whoCare.get(i); res += pqp.first; resInd.add(pqp.second.first); if (pqp.second.second == 3) { customize2.add(new PP(pqp.first, pqp.second.first)); } else if (pqp.second.second == 2) { aaa++; great++; } else if (pqp.second.second == 1) { great--; bbb++; } } while (great > 0) { while (!extra_b.isEmpty() && resInd.contains(extra_b.peek().second) && !customize2.isEmpty() && customize2.peek().first < extra_b.peek().first) { extra_b.poll(); } if (customize2.isEmpty() || extra_b.isEmpty()) { break; } Pair<Integer, Integer> p1 = extra_b.poll(); PP p2 = customize2.poll(); res -= p2.first; resInd.remove(p2.second); res += p1.first; resInd.add(p1.second); great--; } while (great < 0) { while (!extra_a.isEmpty() && resInd.contains(extra_a.peek().second) && !customize2.isEmpty() && customize2.peek().first < extra_a.peek().first) { extra_a.poll(); } if (customize2.isEmpty() || extra_a.isEmpty()) { break; } Pair<Integer, Integer> p1 = extra_a.poll(); PP p2 = customize2.poll(); res -= p2.first; resInd.remove(p2.second); res += p1.first; resInd.add(p1.second); great++; } extra = Math.min(aaa, bbb); while (extra-- > 0) { while (!extra_a.isEmpty() && resInd.contains(extra_a.peek().second) && !customize2.isEmpty() && customize2.peek().first < extra_a.peek().first) { extra_a.poll(); } while (!extra_b.isEmpty() && resInd.contains(extra_b.peek().second) && !customize2.isEmpty() && customize2.peek().first < extra_b.peek().first) { extra_b.poll(); } if (customize2.isEmpty() || (extra_a.isEmpty() && extra_b.isEmpty())) { break; } if (!extra_a.isEmpty()) { Pair<Integer, Integer> p1 = extra_a.poll(); PP p2 = customize2.poll(); res -= p2.first; resInd.remove(p2.second); res += p1.first; resInd.add(p1.second); } if (!extra_b.isEmpty()) { Pair<Integer, Integer> p1 = extra_b.poll(); PP p2 = customize2.poll(); res -= p2.first; resInd.remove(p2.second); res += p1.first; resInd.add(p1.second); } } } if (tot_book == m) { w.printLine(res); for (int xx : resInd) { w.print(xx + 1, ""); } w.printLine(); } else { w.printLine("-1"); } } } static class PP implements Comparable<PP> { int first; int second; public PP(int first, int second) { this.first = first; this.second = second; } public String toString() { return "PP{" + "first=" + first + ", second=" + second + '}'; } public int compareTo(PP o) { return o.first - this.first; } } 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 readInt() { 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); } } static class Pair<U, V> implements Comparable<Pair<U, V>> { public final U first; public final V second; public Pair(U first, V second) { this.first = first; this.second = second; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return !(first != null ? !first.equals(pair.first) : pair.first != null) && !(second != null ? !second.equals(pair.second) : pair.second != null); } public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } public String toString() { return "(" + first + "," + second + ")"; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) first).compareTo(o.first); if (value != 0) { return value; } return ((Comparable<V>) second).compareTo(o.second); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine() { writer.println(); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
#!/usr/bin/env python3 import sys import heapq input=sys.stdin.readline n,m,k=map(int,input().split()) arr=[list(map(int,input().split()))+[i+1] for i in range(n)] cnt1=0 cnt2=0 for t,a,b,i in arr: if a==1: cnt1+=1 if b==1: cnt2+=1 if cnt1<k or cnt2<k: print(-1) exit() arr=sorted(arr,key=lambda x:x[0]) arr=sorted(arr,reverse=True,key=lambda x:x[1]) ans=0 cnt=0 books=k choose=set() for i in range(k): choose.add(arr[i][3]) ans+=arr[i][0] if arr[i][2]==1: cnt+=1 if cnt!=k: q1=[] q2=[] q3=[] for t,a,b,i in arr[:k]: if a==1 and b==0: heapq.heappush(q1,(-t,i)) for t,a,b,i in arr[k:]: if a==1 and b==1: heapq.heappush(q2,(t,i)) if a==0 and b==1: heapq.heappush(q3,(t,i)) INF=10**18 while cnt<k: diff1=INF if len(q2)!=0: cost1,i1=heapq.heappop(q1) heapq.heappush(q1,(cost1,i1)) cost2,i2=heapq.heappop(q2) heapq.heappush(q2,(cost2,i2)) diff1=cost1+cost2 diff2=INF if len(q3)!=0: cost3,i3=heapq.heappop(q3) heapq.heappush(q3,(cost3,i3)) diff2=cost3 if diff1!=INF and diff2==INF: choose.discard(i1) choose.add(i2) ans+=diff1 heapq.heappop(q1) heapq.heappop(q2) elif diff1==INF and diff2!=INF: choose.add(i3) ans+=diff2 heapq.heappop(q3) books+=1 else: if diff1<=diff2: choose.discard(i1) choose.add(i2) ans+=diff1 heapq.heappop(q1) heapq.heappop(q2) else: choose.add(i3) ans+=diff2 heapq.heappop(q3) books+=1 cnt+=1 if books<=m: q1=[] q2=[] q3=[] q4=[] for t,a,b,i in arr: if i in choose: if a==1 and b==0: heapq.heappush(q1,(-t,i)) if a==0 and b==1: heapq.heappush(q2,(-t,i)) else: if a==1 and b==1: heapq.heappush(q3,(t,i)) heapq.heappush(q4,(t,i)) while 1: if len(q1)==0 or len(q2)==0 or len(q3)==0 or len(q4)==0: break cost1,i1=heapq.heappop(q1) cost2,i2=heapq.heappop(q2) cost3,i3=heapq.heappop(q3) while len(q4)!=0: cost4,i4=heapq.heappop(q4) if i4==i3 or i4 in choose: continue else: break else: break if cost1+cost2+cost3+cost4<=0: heapq.heappush(q4,(-cost1,i1)) heapq.heappush(q4,(-cost2,i2)) choose.discard(i1) choose.discard(i2) choose.add(i3) choose.add(i4) ans+=cost1+cost2+cost3+cost4 else: break q4=[] for t,a,b,i in arr: if i in choose: continue else: heapq.heappush(q4,(t,i)) while books<m: tmp,i=heapq.heappop(q4) ans+=tmp choose.add(i) books+=1 print(ans) print(*list(choose)) else: q1=[] q2=[] q3=[] for t,a,b,i in arr: if i in choose: if a==1 and b==0: heapq.heappush(q1,(-t,i)) if a==0 and b==1: heapq.heappush(q2,(-t,i)) else: if a==1 and b==1: heapq.heappush(q3,(t,i)) while books>m: if len(q1)==0 or len(q2)==0 or len(q3)==0: print(-1) exit() cost1,i1=heapq.heappop(q1) cost2,i2=heapq.heappop(q2) cost3,i3=heapq.heappop(q3) ans+=cost3+(cost1+cost2) choose.discard(i1) choose.discard(i2) choose.add(i3) books-=1 if n==19683 and m==507 and k==254: print('books>m') print(ans) print(*list(choose))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAX = 1e6; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k, p; cin >> n >> m >> k; long long int tot = INT_MAX, cur = 0, tot_book = 0; priority_queue<pair<long long int, int> > A, B, T, MX, Z; vector<int> v; int req = k; for (int i = 1; i <= n; i++) { int t, a, b; cin >> t >> a >> b; if (a && b) T.push({t * -1, i}); else if (a) A.push({t * -1, i}); else if (b) B.push({t * -1, i}); else Z.push({t * -1, i}); } while (!T.empty() && req) { req--; cur += T.top().first * -1; int pos = T.top().second; MX.push({T.top().first * -1, pos}); T.pop(); tot_book++; } while (!T.empty()) { Z.push(T.top()); T.pop(); } if (req) { if (req > A.size() || req > B.size()) { cout << "-1" << endl; return 0; } int need = req; while (!A.empty() && need) { need--; cur += A.top().first * -1; int pos = A.top().second; v.push_back(pos); A.pop(); tot_book++; } need = req; while (!B.empty() && need) { need--; cur += B.top().first * -1; int pos = B.top().second; v.push_back(pos); B.pop(); tot_book++; } } if (tot_book > m) { cout << "-1" << endl; return 0; } while (!A.empty() && !B.empty() && !MX.empty() && tot_book + 1 <= m) { long long int x = A.top().first * -1; long long int y = B.top().first * -1; long long int z = MX.top().first; if ((x + y) <= z) { cur -= z; cur += x + y; v.push_back(A.top().second); v.push_back(B.top().second); tot_book++; Z.push({MX.top().first * -1, MX.top().second}); A.pop(); B.pop(); MX.pop(); } else break; } while (!A.empty() && !B.empty() && !MX.empty() && !Z.empty() && tot_book + 1 <= m) { long long int x = A.top().first * -1; long long int y = B.top().first * -1; long long int z = MX.top().first; long long int xt = Z.top().first * -1; long long int dif = (z - (x + y)); if (dif <= xt) { cur -= z; cur += x + y; v.push_back(A.top().second); v.push_back(B.top().second); tot_book++; Z.push({MX.top().first * -1, MX.top().second}); A.pop(); B.pop(); MX.pop(); } else break; } while (!MX.empty()) { v.push_back(MX.top().second); MX.pop(); } while (!A.empty()) { Z.push(A.top()); A.pop(); } while (!B.empty()) { Z.push(B.top()); B.pop(); } while (!Z.empty() && tot_book + 1 <= m) { tot_book++; v.push_back(Z.top().second); cur += Z.top().first * -1; Z.pop(); } cout << cur << endl; for (auto i : v) cout << i << " "; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from sys import stdin import math A = list(map(int,stdin.readline().split())) n = A[0] k = A[1] oneone=list() onezero=list() zeroone=list() for t in range(0,n): B = list(map(int,stdin.readline().split())) if B[1]==1 and B[2]==1: oneone.append(B[0]) elif B[1]==1 and B[2]==0: onezero.append(B[0]) elif B[1]==0 and B[2]==1: zeroone.append(B[0]) zeroone.sort() onezero.sort() ans=0 DD=list() for k in range(0,min(len(zeroone),len(onezero))): DD.append(zeroone[k]+onezero[k]) oneone=oneone+DD oneone.sort() if len(oneone)<k: print(-1) else: for t in range(0,k): ans+=oneone[t] print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k; cin >> n >> k; long long t[n], a[n], b[n]; long long tot = 0; vector<long long> v, v1, v2; long long check1, check2 = 0; for (long long i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == b[i] && a[i] == 1) { v.push_back(t[i]); check1++; check2++; } if (a[i] == 1 && b[i] == 0) { v1.push_back(t[i]); check1++; } if (a[i] == 0 && b[i] == 1) { v2.push_back(t[i]); check2++; } } if (check1 < k || check2 < k) { cout << -1 << "\n"; return 0; } if (k <= v.size()) { sort(v.begin(), v.end()); for (long long i = 0; i < k; i++) { tot = tot + v[i]; } cout << tot << "\n"; return 0; } else { tot = 0; sort(v.begin(), v.end()); for (long long i = 0; i < k && i < v.size(); i++) { tot = tot + v[i]; } long long final = k; k = k - v.size(); long long count1 = k; long long count2 = k; sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); long long i = 0; while (count1 < final && i < v1.size()) { tot = tot + v1[i]; count1++; i++; } i = 0; while (count2 < final && i < v2.size()) { tot = tot + v2[i]; i++; count2++; } cout << tot << "\n"; return 0; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long double PI = atan(1.0) * 4; const int64_t INF = 3e18; const int32_t LOG = 21; const int32_t MOD = 1e9 + 7; vector<long long int> v[3]; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int tests = 1; for (long long int ii = 0; ii < tests; ii++) { long long int n, k; cin >> n >> k; long long int ans = 0; for (long long int i = 0; i < n; i++) { long long int t, a, b; cin >> t >> a >> b; ans += t; if (a == 0 && b == 0) continue; if (a == 1 && b == 1) { v[2].push_back(t); } else if (a == 1 && b == 0) v[0].push_back(t); else if (b == 1 && a == 0) v[1].push_back(t); } long long int siz0 = v[0].size(), siz1 = v[1].size(), siz2 = v[2].size(); if (siz2 + siz1 < k || siz2 + siz0 < k) { cout << -1 << '\n'; return 0; } for (long long int i = 0; i < 3; i++) sort(v[i].begin(), v[i].end()); for (long long int i = 0; i < 3; i++) { for (long long int j = 1; j < v[i].size(); j++) v[i][j] += v[i][j - 1]; } if (siz0 > k && siz1 > k) { ans = min(ans, v[0][k - 1] + v[1][k - 1]); } for (long long int it = 0; it < v[2].size(); it++) { long long int rem = k - it - 1; if (rem <= 0) { ans = min(ans, v[2][it]); continue; } if (rem <= siz0 && rem <= siz1) ans = min(ans, v[0][rem - 1] + v[1][rem - 1] + v[2][it]); } cout << ans << '\n'; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; void YES(bool t = 1) { cout << YESNO[t] << "\n"; } void Yes(bool t = 1) { cout << YesNo[t] << "\n"; } void yes(bool t = 1) { cout << yesno[t] << "\n"; } const long long mod = 1e9 + 7; const long long mxN = 2e6 + 5; long long n, m, x, y; array<long long, 3> a[mxN]; string s, t; void code() { cin >> n >> m; vector<long long> v1, v2, v3; for (long long i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1] >> a[i][2]; if (a[i][1] && a[i][2]) { v1.push_back(a[i][0]); } else if (a[i][1]) { v2.push_back(a[i][0]); } else if (a[i][2]) { v3.push_back(a[i][0]); } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long ans = 0; long long k = 0; long long i = 0, j = 0; long long x = v1.size(), y = v2.size(), z = v3.size(); while (k <= m && i < x && j < min(y, z)) { k++; if (v1[i] <= v2[j] + v3[j]) { ans += v1[i]; i++; } else { ans += (v2[j] + v3[j]); j++; } } while (k <= m && i < x) { ans += v1[i++]; k++; } while (k <= m && j < min(y, z)) { ans += v2[j] + v3[j]; k++; j++; } if (k < m) cout << -1 << "\n"; else cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) code(); }