Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') from math import factorial from collections import Counter, defaultdict, deque from heapq import heapify, heappop, heappush # ------------------------------ # f = open('../input.txt') # sys.stdin = f def main(): n, k = RL() na = 0 nb = 0 ab = 0 ar, br, abr = [], [], [] for _ in range(n): t, a, b = RL() if a and not b: na+=1 ar.append(t) if b and not a: nb+=1 br.append(t) if a==1 and b==1: ab+=1 abr.append(t) if na+ab<k or nb+ab<k: print(-1) else: ar.sort() br.sort() for a, b in zip(ar, br): abr.append(a+b) abr.sort() print(sum(abr[:k])) if __name__ == "__main__": main()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k, x, xa, xb, aa = 0, bb = 0, cc; vector<int> a, b, c; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x >> xa >> xb; aa += xa; bb += xb; if (xa & xb) c.push_back(x); else if (xa) a.push_back(x); else if (xb) b.push_back(x); } if (aa < k || bb < k) { cout << -1; return 0; } aa = 0; bb = 0; cc = 0; int ka = a.size(), kb = b.size(), kc = c.size(); x = 0; sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); for (int i = 0; i < k; i++) { if (cc >= kc) x += a[aa++] + b[bb++]; else if (aa < ka && bb < kb) { if (a[aa] + b[bb] < c[cc]) x += a[aa++] + b[bb++]; else x += c[cc++]; } else { x += c[cc++]; } } cout << x; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.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" ] }
CORRECT
java
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { int n=ni(); int k=ni(); int k1=0,k2=0; ArrayList<Integer>alice=new ArrayList<Integer>(); ArrayList<Integer>bob=new ArrayList<Integer>(); ArrayList<Integer>both=new ArrayList<Integer>(); for(int i=0;i<n;i++) { int val=ni(); int x=ni(); int y=ni(); if(x==1 && y==1) { both.add(val); k2++;k1++; } else if(x==0 && y==1) { alice.add(val); k2++; } else if(x==1 && y==0) { bob.add(val); k1++; } } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); if(k1<k || k2<k) {pn(-1);return;} long ans=0; int i=0,j=0; while(k>0) { if(i==alice.size() || i==bob.size()) { ans+=both.get(j); j++; } else if(j==both.size()) { ans+=alice.get(i)+bob.get(i); i++; } else { ans+=Math.min(both.get(j),alice.get(i)+bob.get(i)); if(both.get(j)<alice.get(i)+bob.get(i)) j++; else i++; } k--; } pn(ans); } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} int t=1; // t=ni(); while(t-->0) {process();} out.flush();out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int main() { int n, k; scanf("%d%d", &n, &k); priority_queue<int, vector<int>, greater<int> > books, a, b; for (int i = 0; i < n; i++) { int ti, x, y; scanf("%d%d%d", &ti, &x, &y); if (x && y) books.push(ti); else if (x) a.push(ti); else if (y) b.push(ti); } while (!a.empty() && !b.empty()) { int x = a.top(), y = b.top(); a.pop(), b.pop(); books.push(x + y); } int ans = 0; if (books.size() < k) { printf("-1\n"); return 0; } for (int i = 0; i < k; i++) { ans += books.top(); books.pop(); } printf("%d", ans); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) a,b,tog=[],[],[] for i in range(n): t,x,y=map(int,input().split()) if x==1 and y==1: tog.append(t) elif x==1 and y==0: a.append(t) elif x==0 and y==1: b.append(t) 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 while ca<k or cb<k: if tog: ptog=tog[-1] else: ptog=0 if a: pa=a[-1] else: pa=0 if b: pb=b[-1] else: pb=0 if ca<k and cb<k: if pa and pb and ptog: if pa+pb<ptog: res+=pa+pb a.pop() b.pop() else: res+=ptog tog.pop() ca+=1 cb+=1 elif ptog: res+=ptog tog.pop() ca+=1 cb+=1 elif pa and pb: res+=pa+pb a.pop() b.pop() ca+=1 cb+=1 else: res=-1 break else: if ca<k: if pa and ptog: if pa<ptog: res+=pa a.pop() ca+=1 else: res+=ptog tog.pop() ca+=1 cb+=1 elif ptog: res+=ptog tog.pop() ca+=1 cb+=1 elif pa: res+=pa a.pop() ca+=1 else: res=-1 break elif cb<k: if pb and ptog: if pb<ptog: res+=pb b.pop() cb+=1 else: res+=ptog tog.pop() ca+=1 cb+=1 elif ptog: res+=ptog tog.pop() ca+=1 cb+=1 elif pb: res+=pb b.pop() cb+=1 else: res=-1 break print(res)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
N, K = [int(i) for i in input().split()] T, A, B = [], [], [] Tall = [] Talice = [] Tbob = [] for i in range(N): t, a, b = [int(j) for j in input().split()] T.append(t) A.append(a) B.append(b) if a and b: Tall.append(t) elif a: Talice.append(t) elif b: Tbob.append(t) Tall = sorted(Tall) Talice = sorted(Talice) Tbob = sorted(Tbob) iall = 0 isep = 0 ans = 0 ml = min(len(Talice), len(Tbob)) for i in range(K): if iall >= len(Tall) and isep >= ml: ans = -1 break if iall < len(Tall) and (isep >= ml or (Tall[iall] <= Talice[isep] + Tbob[isep])): ans += Tall[iall] iall += 1 else: ans += Talice[isep] + Tbob[isep] isep += 1 print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) a = [] b = [] ab = [] for i in range(n): x, y, z = map(int, input().split()) if y == 1 and z == 1: ab.append(x) elif y == 1: a.append(x) elif z == 1: b.append(x) a = sorted(a) b = sorted(b) #ab = sorted(ab) for i in range(min(len(a), len(b))): ab.append(a[i] + b[i]) ab = sorted(ab) ans = 0 if len(ab) >= k: for i in range(k): ans += ab[i] else: print(-1) exit() print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int t[200005], a[200005], b[200005]; long long sum1[200005], sum2[200005], sum3[200005], sum4[200005]; int val[200005]; int pos[200005]; vector<pair<int, int> > v1, v2, v3, v4, v5; struct tree { long long ft[200005]; void update(int i, int v) { for (; i <= 200000; i += (i & -i)) ft[i] += v; } long long query(int i) { long long res = 0; for (; i > 0; i -= (i & -i)) res += ft[i]; return res; } } f[2]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); v1.emplace_back(make_pair(0, 0)); v2.emplace_back(make_pair(0, 0)); v3.emplace_back(make_pair(0, 0)); v4.emplace_back(make_pair(0, 0)); int cnt1 = 0, cnt2 = 0; vector<pair<int, int> > h; for (int i = 1; i <= n; i++) { scanf("%d%d%d", &t[i], &a[i], &b[i]); if (a[i] == 1 && b[i] == 1) v3.emplace_back(make_pair(t[i], i)); else if (a[i] == 1) v1.emplace_back(make_pair(t[i], i)), h.emplace_back(make_pair(t[i], i)); else if (b[i] == 1) v2.emplace_back(make_pair(t[i], i)), h.emplace_back(make_pair(t[i], i)); else v4.emplace_back(make_pair(t[i], i)), h.emplace_back(make_pair(t[i], i)); if (a[i] == 1) cnt1++; if (b[i] == 1) cnt2++; } if (cnt1 < k || cnt2 < k) printf("-1\n"); else { sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); sort(v4.begin(), v4.end()); sort(h.begin(), h.end()); for (int i = 0; i < h.size(); i++) { val[h[i].second] = i + 1; f[0].update(i + 1, 1); f[1].update(i + 1, h[i].first); } for (int i = 1; i < v1.size(); i++) { sum1[i] = sum1[i - 1] + v1[i].first; } for (int i = 1; i < v2.size(); i++) { sum2[i] = sum2[i - 1] + v2[i].first; } for (int i = 1; i < v3.size(); i++) { sum3[i] = sum3[i - 1] + v3[i].first; } for (int i = 1; i < v4.size(); i++) { sum4[i] = sum4[i - 1] + v4[i].first; } long long ans = 1e18, opt = -1; int ca = 1, cb = 1; for (int i = (int)v3.size() - 1; i >= 0; i--) { int a = k - i, b = k - i; if (a < 0) { a = 0; b = 0; } if (a >= v1.size() || b >= v2.size()) continue; while (ca <= a) { int p = val[v1[ca].second]; f[0].update(p, -1); f[1].update(p, -v1[ca].first); ca++; } while (cb <= b) { int p = val[v2[cb].second]; f[0].update(p, -1); f[1].update(p, -v2[cb].first); cb++; } long long res = sum3[i]; res += sum1[a]; res += sum2[b]; int l = a + b + i; if (l > m) continue; int lo = 0, hi = (int)h.size(), p = -1; while (lo <= hi) { int mid = (lo + hi) / 2; if (f[0].query(mid) >= m - l) p = mid, hi = mid - 1; else lo = mid + 1; } if (p == -1) continue; res += f[1].query(p); if (ans > res) { ans = res; opt = i; } } vector<int> ans2; if (ans == 1e18) ans = -1; else { int i = opt; int a = k - i, b = k - i; if (a < 0) a = 0, b = 0; for (int j = 1; j <= i; j++) ans2.emplace_back(v3[j].second); for (int j = 1; j <= a; j++) ans2.emplace_back(v1[j].second); for (int j = 1; j <= b; j++) ans2.emplace_back(v2[j].second); int c = 1; int l = i + a + b; a++, b++; while (l < m) { int mn = 2e9; int x = 0; if (a < v1.size()) { if (mn > v1[a].first) { mn = v1[a].first; x = 1; } } if (b < v2.size()) { if (mn > v2[b].first) { mn = v2[b].first; x = 2; } } if (c < v4.size()) { if (mn > v4[c].first) { mn = v4[c].first; x = 3; } } if (x == 1) { ans2.emplace_back(v1[a].second); a++; } else if (x == 2) { ans2.emplace_back(v2[b].second); b++; } else { ans2.emplace_back(v4[c].second); c++; } l++; } } printf("%lld\n", ans); if (ans != -1) { for (int i = 0; i < m; i++) printf("%d ", ans2[i]); printf("\n"); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const long long inf_ll = 1e18; vector<pair<long long, int>> t[4]; array<long long, 3> eval(int x, int y) { int l = max(-(int)(t[2].size() - 1 - x - y), 0), r = min((int)t[1].size() - 1 - x, y); while (l < r - 2) { int mid = (l + r) / 2; if (t[1][x + mid].first + t[2][x + y - mid].first < t[1][x + mid + 1].first + t[2][x + y - mid - 1].first) r = mid + 1; else l = mid; } array<long long, 3> v; v[0] = inf_ll; for (int j = l; j <= r; j++) v = min(v, {t[1][x + j].first + t[2][x + y - j].first, x + j, x + y - j}); return v; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) { int x, a, b; cin >> x >> a >> b; t[2 * a + b].push_back({x, i}); } for (int i = 0; i < 4; i++) { sort((t[i]).begin(), (t[i]).end()); for (int j = 1; j < t[i].size(); j++) t[i][j].first += t[i][j - 1].first; } for (int i = 0; i < 4; i++) t[i].insert(t[i].begin(), {0, -1}); array<long long, 3> ans; ans[0] = inf_ll; for (int i = 0; i <= min(m, (int)t[3].size() - 1); i++) { int x = max(0, k - i); if (x > min(t[1].size(), t[2].size()) - 1) continue; int l = max(0, m - i - (int)(t[1].size() + t[2].size()) + 2), r = min((int)t[0].size() - 1, m - i - 2 * x); while (l < r - 2) { int mid = (l + r) / 2; if (eval(x, m - i - 2 * x - mid)[0] + (t[0][mid].first) < eval(x, m - i - 2 * x - mid - 1)[0] + (t[0][mid + 1].first)) r = mid + 1; else l = mid; } array<long long, 2> v = {inf_ll, -1}; for (int j = l; j <= r; j++) { v = min(v, {eval(x, m - i - 2 * x - j)[0] + (t[0][j].first), j}); } ans = min(ans, array<long long, 3>{v[0] + t[3][i].first, i, v[1]}); } if (ans[0] == inf_ll) return cout << "-1\n", 0; cout << ans[0] << "\n"; for (int i = 1; i <= ans[1]; i++) cout << t[3][i].second + 1 << " "; for (int i = 1; i <= ans[2]; i++) cout << t[0][i].second + 1 << " "; int x = max(0ll, k - ans[1]); auto v = eval(x, m - ans[1] - 2 * x - ans[2]); for (int i = 1; i <= v[1]; i++) cout << t[1][i].second + 1 << " "; for (int i = 1; i <= v[2]; i++) cout << t[2][i].second + 1 << " "; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
# -*- coding: utf-8 -*- """ Created on Thu Jul 9 09:18:37 2020 @author: Harshal """ n,k=map(int,input().split()); z=[] x=[] y=[] for _ in range(n): t,a,b=map(int,input().split()) if a&b: z.append(t) elif a: x.append(t) elif b: y.append(t) x.sort() y.sort() for i in range(min(len(x),len(y))): z.append(x[i]+y[i]) z.sort() print( -1 if len(z)<k else sum(z[:k]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") #pragma GCC optimize("-fno-defer-pop") long long int power(long long int a, long long int b, long long int m) { if (b == 0) return 1; if (b == 1) return a % m; long long int t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, n; cin >> n >> k; long long int a, b, c; vector<long long int> v1, v2, v3; for (i = 1; i <= n; i++) { cin >> a >> b >> c; if (b * c == 1) v1.push_back(a); else if (b == 1) v2.push_back(a); else if (c == 1) v3.push_back(a); } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); a = 0; b = 0; c = 0; long long int ans = 0, c1 = 0, c2 = 0; while (c1 < k && c2 < k) { long long int x = 1e18, y = 1e18; if (a < v1.size()) { x = v1[a]; } if (b < v2.size() && c < v3.size()) { y = v2[b] + v3[c]; } if (x == 1e18 && y == 1e18) { cout << "-1" << "\n"; return 0; } if (x <= y) { ans += x; a++; c1++; c2++; } else { ans += y; b++; c++; c1++; c2++; } } if (a < v1.size()) { if (c1 < k) { while (a < v1.size()) { v2.push_back(v1[a]); a++; } sort(v2.begin() + b, v2.end()); } else { while (a < v1.size()) { v3.push_back(v1[a]); a++; } sort(v3.begin() + b, v3.end()); } } while (c1 < k && b < v2.size()) { ans += v2[b]; b++; c1++; } while (c2 < k && c < v3.size()) { ans += v3[c]; c++; c2++; } if (c1 < k || c2 < k) { cout << "-1" << "\n"; } else cout << ans << "\n"; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
temparr = input() temparr = temparr.split() n = int(temparr[0]) minimum = int(temparr[1]) common = [] alice = [] bob = [] for i in range(n): temparr = input() temparr = temparr.split() t = int(temparr[0]) a = int(temparr[1]) b = int(temparr[2]) if a == 1 and b == 1: common.append(t) continue if a == 1 : alice.append(t) if b == 1 : bob.append(t) common = sorted(common) alice = sorted(alice) bob = sorted(bob) counta = 0 countb = 0 commonlen = len(common) alicelen = len(alice) boblen = len(bob) i = 0 j = 0 k = 0 flag = 0 ans = 0 # print(alice) # print(bob) # print(common) if commonlen + alicelen < minimum or commonlen + boblen < minimum: print(-1) else: while True: if counta >= minimum and countb >= minimum: print(ans) break if i == commonlen and j == alicelen and k == boblen: flag = 1 break if j == alicelen or k == boblen: ans += common[i] countb += 1 counta += 1 i += 1 elif i == commonlen: counta += 1 countb += 1 ans += alice[j] ans += bob[k] j += 1 k += 1 elif common[i] <= alice[j] + bob[k]: ans += common[i] i += 1 counta += 1 countb += 1 else: counta += 1 countb += 1 ans += alice[j] ans += bob[k] j += 1 k += 1 if flag == 1: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); static int MOD = 1000000007; public static void main(String[] args) throws IOException { Main m = new Main(); m.solve(); m.close(); } void close() throws IOException { pw.flush(); pw.close(); br.close(); } int ri() throws IOException { return Integer.parseInt(br.readLine()); } long rl() throws IOException { return Long.parseLong(br.readLine()); } int[] ril() throws IOException { String[] tokens = br.readLine().split(" "); int[] A = new int[tokens.length]; for (int i = 0; i < A.length; i++) A[i] = Integer.parseInt(tokens[i]); return A; } long[] rll() throws IOException { String[] tokens = br.readLine().split(" "); long[] A = new long[tokens.length]; for (int i = 0; i < A.length; i++) A[i] = Long.parseLong(tokens[i]); return A; } void solve() throws IOException { int[] nk = ril(); int n = nk[0]; int k = nk[1]; int[] t = new int[n]; List<Integer> a = new ArrayList<>(); List<Integer> b = new ArrayList<>(); List<Integer> c = new ArrayList<>(); for (int i = 0; i < n; i++) { int[] tab = ril(); t[i] = tab[0]; if (tab[1] == 1 && tab[2] == 1) c.add(i); else if (tab[1] == 1) a.add(i); else if (tab[2] == 1) b.add(i); } Collections.sort(a, (i1, i2) -> Integer.compare(t[i1], t[i2])); Collections.sort(b, (i1, i2) -> Integer.compare(t[i1], t[i2])); Collections.sort(c, (i1, i2) -> Integer.compare(t[i1], t[i2])); int[] aprefix = new int[a.size()+1]; int[] bprefix = new int[b.size()+1]; int[] cprefix = new int[c.size()+1]; for (int i = 1; i <= a.size(); i++) aprefix[i] = aprefix[i-1] + t[a.get(i-1)]; for (int i = 1; i <= b.size(); i++) bprefix[i] = bprefix[i-1] + t[b.get(i-1)]; for (int i = 1; i <= c.size(); i++) cprefix[i] = cprefix[i-1] + t[c.get(i-1)]; int best = Integer.MAX_VALUE; for (int i = 0; i <= c.size(); i++) { int need = Math.max(0, k - i); if (need >= aprefix.length || need >= bprefix.length) continue; best = Math.min(best, cprefix[i] + aprefix[need] + bprefix[need]); } pw.println(best == Integer.MAX_VALUE ? "-1" : best); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) group = [[], [], [], []] for i in range(n): t, a, b = map(int, input().split()) group[a * 2 + b].append(t) pref_sum = [[0], [0], [0], [0]] for g, ps in zip(group, pref_sum): g.sort() for i in g: ps.append(ps[-1] + i) # print(group) # print(pref_sum) ans = 10**18 for i in range(min(k, len(group[3])) + 1): rest = k - i if rest > len(group[2]) or rest > len(group[1]): continue ans = min(ans, pref_sum[3][i] + pref_sum[1][rest] + pref_sum[2][rest]) if ans < 10**18: print(ans) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
n,k=map(int,raw_input().split()) time=[] al=[] bo=[] both=[] for _ in range(n): t,a,b=map(int,raw_input().split()) if a==1 and b==1: both.append(t) elif a==1: al.append(t) elif b==1: bo.append(t) cur1=0 cur2=0 cur3=0 if min(len(al),len(bo))+len(both)<k: print -1 else: ans=0 al.sort() bo.sort() both.sort() while k!=0: if cur1>=len(al) or cur2>=len(bo) or cur3>=len(both): break if al[cur1]+bo[cur2]<both[cur3]: ans+=al[cur1]+bo[cur2] cur1+=1 cur2+=1 k-=1 else: ans+=both[cur3] cur3+=1 k-=1 if k!=0: if cur1>=len(al) or cur2>=len(bo): while k!=0: ans+=both[cur3] cur3+=1 k-=1 else: while k!=0: ans+=al[cur1]+bo[cur2] cur1+=1 cur2+=1 k-=1 print ans
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = map(int, input().split()) cnta = [] cntb = [] cntab = [] for i in range(n): t, a, b = map(int, input().split()) if a: if b: cntab.append(t) else: cnta.append(t) elif b: cntb.append(t) T = 0 cnta = sorted(cnta) cntb = sorted(cntb) cntab = sorted(cntab) l, m = 0, 0 r = min(len(cnta), len(cntb)) it = 0 while (l < r) and (it < k): if (m >= len(cntab)) or (cnta[l] + cntb[l] < cntab[m]): T += cnta[l] + cntb[l] l += 1 else: T += cntab[m] m += 1 it += 1 if (it + len(cntab) - m < k): print(-1) else: print(T + sum(cntab[m:m + k - it]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; public class E { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); int k = sc.nextInt(); int n = t; PriorityQueue<Integer> both = new PriorityQueue<Integer>(); PriorityQueue<Integer> bob = new PriorityQueue<Integer>(); PriorityQueue<Integer> alice = new PriorityQueue<Integer>(); first: while (t-- != 0) { int c = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if(a == 1 && b == 1) both.add(c); else if(a == 1) { alice.add(c); } else if(b == 1) { bob.add(c); } } long total = 0; long book = 0; while((!both.isEmpty() || (!alice.isEmpty() && !bob.isEmpty()))) { int one = -1; if(!both.isEmpty()) one = both.peek(); int a = -1,b = -1; if((!alice.isEmpty() && !bob.isEmpty())){ a = alice.peek(); b = bob.peek(); } if(one == -1) { total+=a+b; alice.poll(); bob.poll(); } else if(a == -1) { total+=one; both.poll(); } else { if(one<=a+b) { total+=one; both.poll(); } else { total+=a+b; alice.poll(); bob.poll(); } } book+=1; if(book >= k) break; } if(book < k) { System.out.println(-1); } else System.out.println(total); } static void shuffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static int min(int a[]) { int x = 1_000_000_00_9; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static int max(int a[]) { int x = -1_000_000_00_9; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static long min(long a[]) { long x = (long) 3e18; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static long max(long a[]) { long x = -(long) 3e18; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y, long mod) { long res = 1; x %= mod; while (y > 0) { if (y % 2 == 1) res = (res * x) % mod; y >>= 1; x = (x * x) % mod; } return res; } static void intsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void longsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverseintsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverselongsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static class longpair implements Comparable<longpair> { long x, y; longpair(long x, long y) { this.x = x; this.y = y; } public int compareTo(longpair p) { return Long.compare(this.x, p.x); } } static class intpair implements Comparable<intpair> { int x, y; intpair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(intpair o) { return Integer.compare(this.x, o.x); } // a = new pair [n]; // a[i] = new pair(coo,cost); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
a,b = map(int,input().split()) m,n,o,ans=[],[],[],0 for _ in range(a): q,w,e= map(int,input().split()) if w==e==1:m.append(q) elif w!=e: if e==0:n.append(q) else:o.append(q) m.sort();n.sort();o.sort() v,t,u=[],min(len(n),len(o)),len(m) for i in range(t): v.append(n[i]+o[i]) if b>u+t: print(-1) else: i,j=0,0 while b>0: if i<u and j<t: if m[i]<=v[j]: ans+=m[i] i+=1 else: ans+=v[j] j+=1 else: if i<=(u-1): ans+=m[i] i+=1 elif j<=(t-1): ans+=v[j] j+=1 b-=1 print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import math from collections import Counter,deque L=lambda:list(map(int,input().split())) M=lambda:map(int,input().split()) I=lambda:int(input()) IN=lambda:input() mod=10**9+7 def s(a): print(" ".join(list(map(str,a)))) #______________________-------------------------------_____________________# def solve(): n,k=M() a=[];b=[] d=[] for i in range(n): t,x,y=M() if x+y==2: d.append(t) elif x==1: a.append(t) elif y==1: b.append(t) a.sort() b.sort() for i in range(min(len(a),len(b))): d.append((a[i]+b[i])) if len(d)<k: print(-1) else: d.sort() print(sum(d[:k])) for i in range(1): solve()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void out(T t) { cout << t << "\n"; } template <class T, class... Ts> inline void out(T t, Ts... ts) { cout << t << " "; out(ts...); } template <class T> inline bool CHMIN(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool CHMAX(T &a, T b) { if (a < b) { a = b; return true; } return false; } constexpr long long INF = 1e18; template <typename T> struct SegmentTree { using F = function<T(T, T)>; long long n; F f; T ti; vector<T> dat; SegmentTree() {} SegmentTree(F f, T ti) : f(f), ti(ti) {} void init(long long n_) { n = 1; while (n < n_) n <<= 1; dat.assign(n << 1, ti); } void build(const vector<T> &v) { long long n_ = v.size(); init(n_); for (long long i = 0; i < n_; i++) dat[n + i] = v[i]; for (long long i = n - 1; i; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } void set_val(long long k, T x) { dat[k += n] = x; while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]); } T query(long long a, long long b) { if (a >= b) return ti; T vl = ti, vr = ti; for (long long l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, dat[l++]); if (r & 1) vr = f(dat[--r], vr); } return f(vl, vr); } template <typename C> long long find(long long st, C &check, T &acc, long long k, long long l, long long r) { if (l + 1 == r) { acc = f(acc, dat[k]); return check(acc) ? k - n : -1; } long long m = (l + r) >> 1; if (m <= st) return find(st, check, acc, (k << 1) | 1, m, r); if (st <= l && !check(f(acc, dat[k]))) { acc = f(acc, dat[k]); return -1; } long long vl = find(st, check, acc, (k << 1) | 0, l, m); if (~vl) return vl; return find(st, check, acc, (k << 1) | 1, m, r); } template <typename C> long long find(long long st, C &check) { T acc = ti; return find(st, check, acc, 1, 0, n); } }; void solve() { long long N, M, K; cin >> N >> M >> K; vector<long long> t(N), a(N), b(N); for (long long i = 0; i < (long long)N; ++i) { cin >> t[i] >> a[i] >> b[i]; } vector<pair<long long, long long>> ab, A, B; vector<tuple<long long, long long, long long>> others; for (long long i = 0; i < (long long)N; ++i) { if (a[i] && b[i]) { ab.emplace_back(t[i], i); continue; } long long state = a[i] * 2 + b[i]; others.emplace_back(t[i], state, i); } sort(ab.begin(), ab.end()); sort(others.begin(), others.end()); for (long long i = 0; i < (long long)others.size(); ++i) { auto [val, state, idx] = others[i]; if (state == 2) { A.emplace_back(val, i); } else if (state == 1) { B.emplace_back(val, i); } } vector<long long> abs(ab.size() + 1); for (long long i = 0; i < (long long)ab.size(); ++i) { abs[i + 1] += abs[i] + ab[i].first; } SegmentTree<pair<long long, long long>> seg( [](pair<long long, long long> a, pair<long long, long long> b) { return pair<long long, long long>(a.first + b.first, a.second + b.second); }, {0ll, 0ll}); seg.build(vector<pair<long long, long long>>(others.size())); for (long long i = 0; i < (long long)others.size(); ++i) { seg.set_val(i, {get<0>(others[i]), 1ll}); } long long sum = 0; long long ans = INF; for (long long i = 0; i < (long long)min((long long)min(A.size(), B.size()) + 1, K + 1); ++i) { if (i) { sum += get<0>(A[i - 1]) + get<0>(B[i - 1]); seg.set_val(get<1>(A[i - 1]), {0ll, 0ll}); seg.set_val(get<1>(B[i - 1]), {0ll, 0ll}); } if (ab.size() < K - i) continue; long long shortage = M - i * 2 - (K - i); if (shortage < 0) continue; long long absNum = K - i; auto check = [&](pair<long long, long long> x) { return x.second > shortage; }; long long idx = seg.find(0, check); if (seg.query(0, idx).second != shortage) { absNum += shortage - seg.query(0, idx).second; } if (absNum >= abs.size()) continue; long long tmpsum = sum + ((0 <= absNum && absNum < abs.size()) ? abs[absNum] : 0ll) + seg.query(0, idx).first; CHMIN(ans, tmpsum); } if (ab.size() >= K) { long long m = M - K; long long sum = abs[K]; set<pair<long long, long long>> st; for (long long i = K; i < (long long)ab.size(); ++i) { st.insert(ab[i]); } for (auto [val, state, idx] : others) st.insert({val, idx}); vector<long long> ret; for (long long i = 0; i < (long long)m; ++i) { sum += st.begin()->first; ret.emplace_back(st.begin()->second); st.erase(st.begin()); } if (sum < ans) { for (long long i = 0; i < (long long)K; ++i) { ret.emplace_back(ab[i].second); } if (N == 200000 && M == 5053 && K == 4444) out("aaaaaa"); out(sum); for (long long i = 0; i < (long long)ret.size(); ++i) cout << ret[i] + 1 << " \n"[i + 1 == ret.size()]; return; } } if (ans == INF) { out(-1); return; } sum = 0; for (long long i = 0; i < (long long)others.size(); ++i) { seg.set_val(i, {get<0>(others[i]), 1ll}); } for (long long i = 0; i < (long long)min((long long)min(A.size(), B.size()) + 1, K + 1); ++i) { if (i) { sum += get<0>(A[i - 1]) + get<0>(B[i - 1]); seg.set_val(get<1>(A[i - 1]), {0ll, 0ll}); seg.set_val(get<1>(B[i - 1]), {0ll, 0ll}); } long long shortage = M - i * 2 - (K - i); long long absNum = K - i; if (ab.size() < K - i) continue; auto check = [&](pair<long long, long long> x) { return x.second > shortage; }; long long idx = seg.find(0, check); if (seg.query(0, idx).second != shortage) { absNum += shortage - seg.query(0, idx).second; } if (absNum >= abs.size()) continue; long long tmpsum = sum + ((0 <= absNum && absNum < abs.size()) ? abs[absNum] : 0ll) + seg.query(0, idx).first; if (ans == tmpsum) { vector<long long> ret; for (long long j = 0; j < (long long)absNum; ++j) { ret.emplace_back(ab[j].second); } for (long long j = 0; j < (long long)others.size(); ++j) { if (j < idx || seg.query(j, j + 1).second == 0) { ret.emplace_back(get<2>(others[j])); } } out(ans); for (long long j = 0; j < (long long)ret.size(); ++j) cout << ret[j] + 1 << " \n"[j + 1 == ret.size()]; return; } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long Q = 1; while (Q--) solve(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
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 int mod = 1e9 + 7; const int mxN = 2e5 + 5; int n, m, x, y, t, p, q; array<int, 3> a[mxN]; void code() { cin >> n >> m; vector<int> v1, v2, v3; for (int i = 0; i < n; i++) { cin >> t >> p >> q; if (p == 1 && q == 1) { v1.push_back(t); } if (p == 1 && q == 0) { v2.push_back(t); } if (p == 0 && q == 1) { v3.push_back(t); } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); int ans = 0; int k = 0; int i = 0, j = 0; int x = (int)v1.size(), y = (int)v2.size(), z = (int)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]; 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); int t = 1; while (t--) code(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = tuple(map(int, input().split())) alice = [] bob = [] both = [] for _ in range(n): t, a, b = tuple(map(int, input().split())) if a == 1 and b == 1: both.append(t) elif a == 1: alice.append(t) elif b == 1: bob.append(t) both.sort() alice.sort() bob.sort() remain = 0 if k <= len(both): result = sum(both[:k]) else: remain = k - len(both) if remain > len(bob) or remain > len(alice): result = -1 else: result = sum(both) result += sum(alice[:remain]) + sum(bob[:remain]) index = max(0, k - len(both)) lenbob = len(bob) lenalice = len(alice) if result != -1: for i in range(min(k, len(both)) - 1, -1, -1): if index > lenbob - 1 or index > lenalice - 1: break newresult = result - both[i] + alice[index] + bob[index] index += 1 if newresult < result: result = newresult else: break print(result)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int inf = 2e9 + 5; double PI = 3.14159265358979323846; void solve() { long long int n, k; cin >> n >> k; vector<long long int> books[2][2]; for (int i = 0; i < n; i++) { long long int t, a, b; cin >> t >> a >> b; books[a][b].push_back(t); } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { sort((books[i][j]).begin(), (books[i][j]).end()); } } vector<vector<long long int> > pre(4); pre[0].push_back(0); pre[1].push_back(0); pre[2].push_back(0); pre[3].push_back(0); for (auto x : books[0][1]) { pre[1].push_back(pre[1].back() + x); } for (auto x : books[1][0]) { pre[2].push_back(pre[2].back() + x); } for (auto x : books[1][1]) { pre[3].push_back(pre[3].back() + x); } long long int ans = inf; for (long long int i = 0; i <= min(k, (long long int)pre[3].size() - 1); i++) { long long int remain = k - i; if ((int)pre[1].size() > remain and (int) pre[2].size() > remain) { long long int cur = pre[3][i] + pre[1][remain] + pre[2][remain]; ans = min(ans, cur); } } if (ans == inf) { cout << -1 << "\n"; } else { cout << ans << "\n"; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = map(int,input().split()) a = [] b =[] c = [] c1 = [] for i in range(n): q,w,e = map(int,input().split()) if w== 1 and e == 1: c.append(q) if w == 1 and e == 0: a.append(q) if w == 0 and e == 1: b.append(q) a = sorted(a) b = sorted(b) for i in range(min(len(a),len(b))): c.append(a[i]+b[i]) c = sorted(c) if len(c)<k: print(-1) exit(0) s = 0 for i in range(k): s+=c[i] print(s)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastScanner f = new FastScanner(); int t=1; // t=f.nextInt(); PrintWriter out=new PrintWriter(System.out); for(int tt=0;tt<t;tt++) { int n= f.nextInt(); int k= f.nextInt(); ArrayList<Integer> alice=new ArrayList<Integer>(); ArrayList<Integer> bob=new ArrayList<Integer>(); ArrayList<Integer> both=new ArrayList<Integer>(); for(int i=0;i<n;i++) { int a=f.nextInt(); int b=f.nextInt(); int c=f.nextInt(); if(b==1 && c==1) both.add(a); if(b==1 && c==0) alice.add(a); if(b==0 && c==1) bob.add(a); } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); int[] alices=new int[alice.size()+1]; int[] boths=new int[both.size()+1]; int[] bobs=new int[bob.size()+1]; for(int i=1;i<alices.length;i++) { alices[i]=alices[i-1]+alice.get(i-1); } for(int i=1;i<bobs.length;i++) { bobs[i]=bobs[i-1]+bob.get(i-1); } for(int i=1;i<boths.length;i++) { boths[i]=boths[i-1]+both.get(i-1); // System.out.println(boths[i]); } long ans=Integer.MAX_VALUE; for(int i=0;i<boths.length;i++) { int temp=boths[i]; int rest=k-i; if(rest>-1 && bobs.length>rest && alices.length>rest) { temp+=bobs[rest]; temp+=alices[rest]; ans=Math.min(ans, temp); } } if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } out.close(); } static void sort(long[] p) { ArrayList<Integer> q = new ArrayList<>(); for (long i: p) q.add((int) i); Collections.sort(q); for (int i = 0; i < p.length; i++) p[i] = q.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } } //Some things to notice //Check for the overflow //Binary Search //Bitmask //runtime error most of the time is due to array index out of range
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) ans=[] c1=0 c2=0 for i in range(n): z=list(map(int,input().split())) if(z[1]==1): c1+=1 if(z[2]==1): c2+=1 ans.append(z) if(min(c1,c2)<k): print(-1) else: a0=[] a1=[] a2=[] for i in range(len(ans)): if(ans[i][1]==1 and ans[i][2]==1): a0.append(ans[i]) elif(ans[i][1]==1 and ans[i][2]==0): a1.append(ans[i]) elif(ans[i][1]==0 and ans[i][2]==1): a2.append(ans[i]) p0=0 p1=0 p2=0 c1=0 c2=0 a0.sort() a1.sort() a2.sort() total=0 while(c1<k and c2<k): if(p0<len(a0) and p1<len(a1) and p2<len(a2) and a0[p0][0]<=a1[p1][0]+a2[p2][0]): total+=a0[p0][0] c1+=1 c2+=1 p0+=1 else: if(p1<len(a1) and p2<len(a2)): total+=a1[p1][0]+a2[p2][0] c1+=1 c2+=1 p1+=1 p2+=1 continue; if(p1==len(a1) or p2==len(a2)): total+=a0[p0][0] c1+=1 c2+=1 p0+=1 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" ] }
CORRECT
python3
# input = raw_input # range = xrange import sys inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 seg = [0]*200000 def offset(x): return x + 100000 def encode(x, y): return x*200002 + y def decode(x): return x//200002, x%200002 def upd(node, L, R, pos, val): while L < R: seg[node] += val seg[offset(node)] += val*pos if L+1 == R: break M = (L+R)//2 node <<= 1 if pos < M: R = M else: L = M node += 1 def query(node, L, R, k): ret = 0 while L < R: if k == 0: return ret if seg[node] == k: return ret + seg[offset(node)] if L+1 == R: return ret + k*L M = (L+R)//2 node <<= 1 if seg[node] >= k: R = M else: ret += seg[offset(node)] k -= seg[node] L = M node += 1 return ret n, m, k = inp[ii:ii+3]; ii += 3 A, B, both, neither = [], [], [], [] for i in range(n): t, a, b = inp[ii:ii+3]; ii += 3 if a == 0 and b == 0: neither.append(encode(t, i+1)) if a == 1 and b == 0: A.append(encode(t, i+1)) if a == 0 and b == 1: B.append(encode(t, i+1)) if a == 1 and b == 1: both.append(encode(t, i+1)) upd(1, 0, 10001, t, 1) A.sort(); B.sort(); both.sort() p1 = min(k, len(both)) p2 = k - p1 if 2*k - p1 > m or p2 > min(len(A), len(B)): print(-1) exit(0) sum, ans, ch = 0, 2**31, p1 for i in range(p1): sum += both[i]//200002 upd(1, 0, 10001, both[i]//200002, -1) for i in range(p2): sum += A[i]//200002 + B[i]//200002 upd(1, 0, 10001, A[i]//200002, -1) upd(1, 0, 10001, B[i]//200002, -1) ans = query(1, 0, 10001, m-2*k+p1) + sum while p1 > 0: if p2 == min(len(A), len(B)): break upd(1, 0, 10001, A[p2]//200002, -1); sum += A[p2]//200002 upd(1, 0, 10001, B[p2]//200002, -1); sum += B[p2]//200002 upd(1, 0, 10001, both[p1-1]//200002, 1); sum -= both[p1-1]//200002 p2 += 1 p1 -= 1 if m - 2*k + p1 < 0: break Q = query(1, 0, 10001, m-2*k+p1) if ans > sum + Q: ans = sum + Q ch = p1 print(ans) ind = [both[i]%200002 for i in range(ch)] + [A[i]%200002 for i in range(k-ch)] + [B[i]%200002 for i in range(k-ch)] st = neither + [both[i] for i in range(ch, len(both))] + [A[i] for i in range(k-ch, len(A))] + [B[i] for i in range(k-ch, len(B))] st.sort() ind += [st[i]%200002 for i in range(m-2*k+ch)] print (' '.join(str(x) for x in ind))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<double, double>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<double>; using vs = vector<string>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; const int di[4] = {-1, 0, 1, 0}, dj[4] = {0, 1, 0, -1}; const int di8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dj8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const ld PI = acos((ld)-1); const ll INF = 1e18; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll randint(ll a, ll b) { return uniform_int_distribution<ll>(a, b)(rng); } bool pow2(int i) { return i && (i & -i) == i; } constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } ll half(ll x) { return fdiv(x, 2); } template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template <class T> void remDup(vector<T>& v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), end(v)); } template <class T, class U> void remAll(vector<T>& v, U a) { v.erase(remove((v).begin(), (v).end(), a), v.end()); } template <class T, class U> T fstTrue(T lo, T hi, U f) { while (lo < hi) { T mid = half(lo + hi); f(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U f) { while (lo < hi) { T mid = half(lo + hi + 1); f(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T, class U, class C> string to_string(tuple<T, U, C> p); template <class T, class U, class C, class D> string to_string(tuple<T, U, C, D> p); string to_string(char c) { return string(1, c); } string to_string(bool b) { return to_string((int)b); } string to_string(const char* s) { return (string)s; } string to_string(string s) { return s; } template <class T> string to_string(complex<T> c) { stringstream ss; ss << c; return ss.str(); } string to_string(vb v) { string res = "{"; for (int i = (0); i < (((int)(v).size())); ++i) res += char('0' + v[i]); res += "}"; return res; } template <size_t SZ> string to_string(bitset<SZ> b) { string res = ""; for (int i = (0); i < (((int)(b).size())); ++i) res += char('0' + b[i]); return res; } template <class T, class U> string to_string(pair<T, U> p); template <class T> string to_string(T v) { bool fst = 1; string res = ""; for (const auto& x : v) { if (!fst) res += " "; fst = 0; res += to_string(x); } return res; } template <class T, class U> string to_string(pair<T, U> p) { return to_string(p.first) + " " + to_string(p.second); } template <class T, class U, class C> string to_string(tuple<T, U, C> p) { return to_string(get<0>(p)) + " " + to_string(get<1>(p)) + " " + to_string(get<2>(p)); } template <class T, class U, class C, class D> string to_string(tuple<T, U, C, D> p) { return to_string(get<0>(p)) + " " + to_string(get<1>(p)) + " " + to_string(get<2>(p)) + " " + to_string(get<3>(p)); } template <class T, class U, class C> void re(tuple<T, U, C>& p); template <class T, class U, class C, class D> void re(tuple<T, U, C, D>& p); template <class T> void re(complex<T>& c); template <class T, class U> void re(pair<T, U>& p); template <class T> void re(vector<T>& v); template <class T, size_t SZ> void re(array<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& d) { string t; re(t); d = stod(t); } void re(ld& d) { string t; re(t); d = stold(t); } template <class T, class... U> void re(T& t, U&... u) { re(t); re(u...); } template <class T> void re(complex<T>& c) { T a, b; re(a, b); c = {a, b}; } template <class T, class U> void re(pair<T, U>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& x) { for (auto& a : x) re(a); } template <class T, size_t SZ> void re(array<T, SZ>& x) { for (auto& a : x) re(a); } template <class T, class U, class C> void re(tuple<T, U, C>& p) { re(get<0>(p), get<1>(p), get<2>(p)); } template <class T, class U, class C, class D> void re(tuple<T, U, C, D>& p) { re(get<0>(p), get<1>(p), get<2>(p), get<3>(p)); } template <class T> void pr(T x) { cout << to_string(x); } template <class T, class... U> void pr(const T& t, const U&... u) { pr(t); pr(u...); } void ps() { pr("\n"); } template <class T, class... U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); } void DBG() { cerr << "]" << endl; } template <class T, class... U> void DBG(const T& t, const U&... u) { cerr << to_string(t); if (sizeof...(u)) cerr << ", "; DBG(u...); } struct chash { const uint64_t C = ll(2e18 * PI) + 71; const int RANDOM = rng(); ll operator()(ll x) const { return __builtin_bswap64((x ^ RANDOM) * C); } }; template <class T> using V = vector<T>; template <class T, size_t SZ> using AR = array<T, SZ>; template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>; template <class T> using pq = priority_queue<T>; template <class T, class U> using um = unordered_map<T, U, chash>; template <class T> using us = unordered_set<T, chash>; template <class T> using PR = pair<T, T>; const int MOD = 1e9 + 7; template <int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } int v; explicit operator int() const { return v; } mint() { v = 0; } mint(ll _v) { v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; if (v < 0) v += MOD; } friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } friend void re(mint& a) { ll x; re(x); a = mint(x); } friend string to_string(mint a) { return to_string(a.v); } mint& operator+=(const mint& m) { if ((v += m.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& m) { if ((v -= m.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& m) { v = (ll)v * m.v % MOD; return *this; } mint& operator/=(const mint& m) { return (*this) *= inv(m); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a, MOD - 2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; typedef mint<MOD, 5> mi; vector<vector<mi>> scmb; void genComb(int SZ) { scmb.assign(SZ, vector<mi>(SZ)); scmb[0][0] = 1; for (int i = (1); i < (SZ); ++i) for (int j = (0); j < (i + 1); ++j) scmb[i][j] = scmb[i - 1][j] + (j ? scmb[i - 1][j - 1] : 0); } vi invs, fac, ifac; void genFac(int SZ) { invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); invs[1] = fac[0] = ifac[0] = 1; for (int i = (2); i < (SZ); ++i) invs[i] = MOD - (ll)MOD / i * invs[MOD % i] % MOD; for (int i = (1); i < (SZ); ++i) { fac[i] = (ll)fac[i - 1] * i % MOD; ifac[i] = (ll)ifac[i - 1] * invs[i] % MOD; } } mi comb(int a, int b) { if (a < b || b < 0) return 0; return (ll)fac[a] * ifac[b] % MOD * ifac[a - b] % MOD; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } using ul = unsigned long long; ul modMul(ul a, ul b, const ul mod) { ll ret = a * b - mod * (ul)((ld)a * b / mod); return ret + ((ret < 0) - (ret >= (ll)mod)) * mod; } ul modPow(ul a, ul b, const ul mod) { if (b == 0) return 1; ul res = modPow(a, b / 2, mod); res = modMul(res, res, mod); return b & 1 ? modMul(res, a, mod) : res; } bool prime(ul n) { if (n < 2 || n % 6 % 4 != 1) return n - 2 < 2; ul A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}, s = __builtin_ctzll(n - 1), d = n >> s; for (auto& a : A) { ul p = modPow(a, d, n), i = s; while (p != 1 && p != n - 1 && a % n && i--) p = modMul(p, p, n); if (p != n - 1 && i != s) return 0; } return 1; } void setIn(string s) { freopen(s.c_str(), "r", stdin); } void setOut(string s) { freopen(s.c_str(), "w", stdout); } void IOS(int n = 10, string s = "") { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(n); cerr.precision(n); cerr << fixed; cout << fixed; if (((int)(s).size())) { setIn(s + ".in"), setOut(s + ".out"); } } const int mxN = 2e5 + 5; void solve() { int n, k, ans = MOD * 2; re(n, k); vi a, b, v; for (int i = (0); i < (n); ++i) { int t, a1, b1; re(t, a1, b1); if (a1 && b1) v.push_back(t); else if (a1 && !b1) a.push_back(t); else if (b1 && !a1) b.push_back(t); } sort((a).begin(), (a).end()); sort((b).begin(), (b).end()); sort((v).begin(), (v).end()); vi pa(((int)(a).size()) + 1), push_back(((int)(b).size()) + 1), pv(((int)(v).size()) + 1); for (int i = (0); i < (((int)(a).size())); ++i) pa[i + 1] = pa[i] + a[i]; for (int i = (0); i < (((int)(b).size())); ++i) push_back[i + 1] = push_back[i] + b[i]; for (int i = (0); i < (((int)(v).size())); ++i) pv[i + 1] = pv[i] + v[i]; for (int i = 0; i <= k && i <= ((int)(v).size()); ++i) { int x = k - i; if (x <= ((int)(a).size()) && x <= ((int)(b).size())) chmin(ans, pa[x] + push_back[x] + pv[i]); } ps(ans == MOD * 2 ? -1 : ans); } int main() { IOS(); int t = 1; for (int i = (0); i < (t); ++i) { solve(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; const long long int inf = 0x3f3f3f3f3f3f3f3fLL; long long int i, j, k; void solve(void); int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); int t = 1; while (t--) solve(); return 0; } void solve() { long long int n, m, k; cin >> n >> m >> k; vector<long long int> a, b, both, c; map<long long int, vector<int> > ma, mb, mboth, mc; for (i = (1); i <= (n); ++i) { long long int t, x, y; cin >> t >> x >> y; if (x == 1 && y == 1) both.emplace_back(t), mboth[t].emplace_back(i); else if (x == 1) a.emplace_back(t), ma[t].emplace_back(i); else if (y == 1) b.emplace_back(t), mb[t].emplace_back(i); else c.emplace_back(t), mc[t].emplace_back(i); } int sizboth = (long long)both.size(), siza = (long long)a.size(), sizb = (long long)b.size(), sizc = (long long)c.size(); both.emplace_back(0); a.emplace_back(0); b.emplace_back(0); c.emplace_back(0); sort(both.begin(), both.end()); sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); vector<long long int> pboth, pa, emplace_back, pc; pboth.emplace_back(0); pa.emplace_back(0); emplace_back.emplace_back(0); pc.emplace_back(0); for (i = (1); i <= ((long long)both.size() - 1); ++i) pboth.emplace_back(both[i] + pboth.back()); for (i = (1); i <= ((long long)a.size() - 1); ++i) pa.emplace_back(a[i] + pa.back()); for (i = (1); i <= ((long long)b.size() - 1); ++i) emplace_back.emplace_back(b[i] + emplace_back.back()); for (i = (1); i <= ((long long)c.size() - 1); ++i) pc.emplace_back(c[i] + pc.back()); {}; {}; {}; {}; long long int min = inf, X = -1, Y = -1, Z = -1, W = -1, U = -1; int idx1 = -1, idx2 = -1, idx3 = -1; for (i = (0); i <= (m); ++i) if (i <= k) { if (i > sizboth) continue; if (k - i > siza || k - i > sizb) continue; int val = m - (2 * k - i); if (val > siza + sizb + sizc - 2 * (k - i) || val < 0) continue; long long int sum = pboth[i] + pa[k - i] + emplace_back[k - i]; long long int lo = 0, hi = inf, mid, ans = -1; while (lo <= hi) { mid = (lo + hi) >> 1; int id1 = upper_bound(a.begin(), a.end(), mid) - a.begin() - 1; int id2 = upper_bound(b.begin(), b.end(), mid) - b.begin() - 1; int id3 = upper_bound(c.begin(), c.end(), mid) - c.begin() - 1; int x = ((0) > (id1 - (k - i)) ? (0) : (id1 - (k - i))); int y = ((0) > (id2 - (k - i)) ? (0) : (id2 - (k - i))); int z = ((0) > (id3) ? (0) : (id3)); if (x + y + z >= val) { ans = mid; hi = mid - 1; } else lo = mid + 1; } if (ans == -1) continue; int id1 = lower_bound(a.begin(), a.end(), ans) - a.begin() - 1; int id2 = lower_bound(b.begin(), b.end(), ans) - b.begin() - 1; int id3 = lower_bound(c.begin(), c.end(), ans) - c.begin() - 1; int x = ((0) > (id1 - (k - i)) ? (0) : (id1 - (k - i))); int y = ((0) > (id2 - (k - i)) ? (0) : (id2 - (k - i))); int z = ((0) > (id3) ? (0) : (id3)); val -= (x + y + z); sum += pa[k - i + x] - pa[k - i]; sum += emplace_back[k - i + y] - emplace_back[k - i]; sum += pc[z]; sum += (val * ans); if (min > sum) { min = sum; {}; X = i, Y = k - i + x, Z = k - i + y, W = z, U = ans; } } else { if (i > sizboth) continue; long long int sum = pboth[i]; long long int val = m - i; if (siza + sizb + sizc < val) continue; long long int lo = 0, hi = inf, mid, ans = -1; while (lo <= hi) { mid = (lo + hi) >> 1; int id1 = upper_bound(a.begin(), a.end(), mid) - a.begin() - 1; int id2 = upper_bound(b.begin(), b.end(), mid) - b.begin() - 1; int id3 = upper_bound(c.begin(), c.end(), mid) - c.begin() - 1; if (id1 + id2 + id3 >= val) { ans = mid; hi = mid - 1; } else lo = mid + 1; } if (ans == -1) continue; int id1 = lower_bound(a.begin(), a.end(), ans) - a.begin() - 1; int id2 = lower_bound(b.begin(), b.end(), ans) - b.begin() - 1; int id3 = lower_bound(c.begin(), c.end(), ans) - c.begin() - 1; int x = ((0) > (id1) ? (0) : (id1)), y = ((0) > (id2) ? (0) : (id2)), z = ((0) > (id3) ? (0) : (id3)); sum += pa[x] + emplace_back[y] + pc[z]; val -= (x + y + z); sum += val * ans; if (min > sum) { min = sum; {}; X = i, Y = x, Z = y, W = z, U = ans; } } if (min == inf) { cout << "-1\n"; return; } cout << min << '\n'; vector<int> index; for (i = (1); i <= (X); ++i) { int id = mboth[both[i]].back(); mboth[both[i]].pop_back(); index.emplace_back(id); } for (i = (1); i <= (Y); ++i) { int id = ma[a[i]].back(); ma[a[i]].pop_back(); index.emplace_back(id); } for (i = (1); i <= (Z); ++i) { int id = mb[b[i]].back(); mb[b[i]].pop_back(); index.emplace_back(id); } for (i = (1); i <= (W); ++i) { int id = mc[c[i]].back(); mc[c[i]].pop_back(); index.emplace_back(id); } m -= X + Y + Z + W; while (m > 0 && (long long)mboth[U].size() > 0) { int id = mboth[U].back(); mboth[U].pop_back(); index.emplace_back(id); m--; } while (m > 0 && (long long)ma[U].size() > 0) { int id = ma[U].back(); ma[U].pop_back(); index.emplace_back(id); m--; } while (m > 0 && (long long)mb[U].size() > 0) { int id = mb[U].back(); mb[U].pop_back(); index.emplace_back(id); m--; } while (m > 0 && (long long)mc[U].size() > 0) { int id = mc[U].back(); mc[U].pop_back(); index.emplace_back(id); m--; } for (auto it : index) cout << it << " "; cout << '\n'; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) p1=[] a1=[] b1=[] c1=0 c2=0 for _ in range(n): p,a,b=map(int,input().split()) if(a==1 and b==1): p1.append(p) c1+=1 c2+=1 elif(a==1): a1.append(p) c1+=1 elif( b==1): b1.append(p) c2+=1 if(c1<k or c2<k): print(-1) else: a1.sort() b1.sort() i=0 while(i<len(a1) and i<len(b1)): p1.append(a1[i]+b1[i]) i+=1 p1.sort() ans=0 for i in range(k): ans+=p1[i] print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
#################################################### import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ####################################### n,k=map(int,input().split()) l1=[] l2=[] l3=[] l4=[] for i in range(n): t,a,b=map(int,input().split()) if a==1 and b==1: l3.append(t) elif a==1 and b==0: l1.append(t) elif a==0 and b==1: l2.append(t) else: l4.append(t) l1.sort(reverse=True) l2.sort(reverse=True) l3.sort() l4.sort() ans=0 t=True if len(l1)+len(l3)<k or len(l2) +len(l3)<k: print(-1) else: if k>=len(l3): if len(l3): ans=sum(l3) else: ans=0 t=False ln=len(l3) else: ans=sum(l3[:k]) l3=l3[:k] ln=k if t==True: if min(len(l2),len(l1))>0: while l1[-1]+l2[-1]<=l3[-1]: ans-=l3[-1] ans+=l1[-1]+l2[-1] l1.pop() l2.pop() l3.pop() if len(l3)==0 or len(l1)==0 or len(l2)==0: break print(ans) else: for j in range(k-ln): ans+=l1[-1]+l2[-1] l1.pop() l2.pop() if min(len(l2),len(l1),len(l3))>0: while l1[-1]+l2[-1]<=l3[-1]: ans-=l3[-1] ans+=l1[-1]+l2[-1] l1.pop() l2.pop() l3.pop() if len(l3)==0 or len(l1)==0 or len(l2)==0: break print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
l1 = input().split(' ') n = int(l1[0]) k = int(l1[1]) alike = 0 blike = 0 total = 0 tboth = [] ta = [] tb = [] for i in range(n): b = input().split(' ') time = int(b[0]) alice = int(b[1]) bob = int(b[2]) if alice: alike += 1 if bob: blike += 1 if alice and bob: tboth.append(time) if alice and not bob: ta.append(time) if not alice and bob: tb.append(time) if alike<k or blike<k: print (-1) else: import heapq heapq.heapify(tboth) heapq.heapify(ta) heapq.heapify(tb) alike = blike = k while alike > 0 or blike > 0: a = b = o = False if tboth: mo = heapq.heappop(tboth) o = True if ta: ma = heapq.heappop(ta) a = True if tb: mb = heapq.heappop(tb) b = True if a and b and o: mi = min(ma+mb,mo) total += mi if mi == mo: heapq.heappush(ta,ma) heapq.heappush(tb,mb) else: heapq.heappush(tboth,mo) alike -=1 blike -=1 elif a and b: total += (ma+mb) alike -=1 blike -=1 elif o: total += mo alike -=1 blike -=1 elif a: total += ma alike -= 1 elif b: total += mb blike -= 1 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" ] }
CORRECT
java
/* Author: @__goku__ ssrivastava990@gmail.com `\-. ` \ `. ` \ \ | __.._ | \. S O N - G O K U ..---~~ ~ . | Y ~-. `| | `. `~~--. \ ~. \ \__. . -- - . .-~~~~~ , , ~~~~~~---...._ .-~___ ,'/ ,'/ ,'\ __...---~~~ ~-. /._\_( ,(/_. 7,-. ~~---...__ _...>- P""6=`_/"6"~ 6) ___...--~~~ ~~--._ \`--') `---' 9' _..--~~~ ~\ ~~/_ ~~~ /`-.--~~ `. --- .' \_ `. " _.-' | ~-.,-------._ ..._../~~ ./ .-' .-~~~-. ,--~~~ ,'...\` _./.----~~.'/ /' `- _.-( |\ `/~ _____..-' / / _.-~~`. / | /. ^---~~~~ ' / / ,' ~. \ ( / ( . _ ' /' / ,/ \ ) (`. | `\ - - - - ~ /' ( / . | \.\| \ /' \ |`. / /.'\\ `\ /' ~-\ . /\ /, ( `\ /' `.___..- \ | | \ `\_/' // \. | | | | _Seal_ /' | | | */ import java.io.*; import java.util.*; public class C653E { static PrintWriter out = new PrintWriter((System.out)); public static void main(String args[]) throws IOException { Kioken sc = new Kioken(); int n = sc.nextInt(); long k = sc.nextInt(); ArrayList<Long> res[] = new ArrayList[4]; for(int x=0;x<4;x++) { res[x]=new ArrayList<>(); } for (int x = 0; x < n; x++) { int a=sc.nextInt(); int v=sc.nextInt(); int u=sc.nextInt(); res[2*u+v].add((long)a); } Collections.sort(res[1]); Collections.sort(res[2]); for(int x=0;x<Math.min(res[2].size(),res[1].size());x++) { res[3].add(res[1].get(x)+res[2].get(x)); } if(res[3].size()<k) { out.println(-1); } else { Collections.sort(res[3]); long ans=0l; for(int x=0;x<k;x++) { ans+=res[3].get(x); } out.println(ans); } out.close(); } public static int lower_bound(ArrayList<Integer> ar, int k) { int s = 0, e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) <= k) { s = mid + 1; } else { e = mid; } } return Math.abs(s) - 1; } public static int upper_bound(ArrayList<Integer> ar, int k) { int s = 0; int e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) < k) { s = mid + 1; } else { e = mid; } } if (s == ar.size()) { return -1; } return s; } static class Kioken { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null) { return false; } st = new StringTokenizer(next); return true; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k = map(int,input().split()) x= [] y= [] z = [] for _ in range(n): t,a,b = map(int,input().split()) if a==1 and b==1: x.append(t) elif a==1 and b==0: y.append(t) elif a==0 and b==1: z.append(t) if len(x)>=k or (k - len(x)<=len(y) and k - len(x)<= len(z)): x.sort() y.sort() z.sort() i = 0 j = 0 ans = 0 while i +j<k: if i<len(x) and j<len(y) and j<len(z): if x[i]<y[j]+z[j]: ans = ans+x[i] i =i+1 else: ans = ans+y[j]+z[j] j=j+1 elif i<len(x): ans = ans+x[i] i = i+1 elif j<len(y) and j<len(z): ans = ans+y[j]+z[j] j = j+1 print(ans) else: print('-1')
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
input = raw_input range = xrange import sys inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 seg = [0]*200000 def offset(x): return x + 100000 def encode(x, y): return (x<<20) + y def upd(node, L, R, pos, val): if L+1 == R: seg[node] += val seg[offset(node)] = seg[node]*L return M = (L+R)>>1 if pos < M: upd(node<<1, L, M, pos, val) else: upd(node<<1 | 1, M, R, pos, val) seg[node] = seg[node<<1] + seg[node<<1 | 1] seg[offset(node)] = seg[offset(node<<1)] + seg[offset(node<<1 | 1)] def query(node, L, R, k): if k == 0: return 0, 0 if seg[node] < k: return seg[offset(node)], seg[node] if L+1 == R: return [L*k, k] M = (L+R)>>1 leftval, leftct = query(node<<1, L, M, k) rightval, rightct = query(node<<1 | 1, M, R, k-leftct) return leftval+rightval, leftct+rightct n, m, k = inp[ii:ii+3]; ii += 3 A, B, AB, notAB = [], [], [], [] for i in range(n): t, a, b = inp[ii:ii+3]; ii += 3 if a == 0 and b == 0: notAB.append(encode(t, i+1)) if a == 1 and b == 0: A.append(encode(t, i+1)) if a == 0 and b == 1: B.append(encode(t, i+1)) if a == 1 and b == 1: AB.append(encode(t, i+1)) upd(1, 0, 10001, t, 1) A.sort(); B.sort(); AB.sort() p1 = min(k, len(AB)) p2 = k - p1 if 2*k - p1 > m or p2 > min(len(A), len(B)): print(-1) exit(0) sum, ans, ch = 0, 2**31, p1 for i in range(p1): sum += AB[i]>>20 upd(1, 0, 10001, AB[i]>>20, -1) for i in range(p2): sum += (A[i]>>20) + (B[i]>>20) upd(1, 0, 10001, A[i]>>20, -1) upd(1, 0, 10001, B[i]>>20, -1) ans, _ = query(1, 0, 10001, m-2*k+p1) ans += sum while p1 > 0: if p2 == min(len(A), len(B)): break upd(1, 0, 10001, A[p2]>>20, -1); sum += A[p2]>>20 upd(1, 0, 10001, B[p2]>>20, -1); sum += B[p2]>>20 upd(1, 0, 10001, AB[p1-1]>>20, 1); sum -= AB[p1-1]>>20 p2 += 1 p1 -= 1 if m - 2*k + p1 < 0: break Q, _ = query(1, 0, 10001, m-2*k+p1) if ans > sum + Q: ans = sum + Q ch = p1 print ans mask = (1<<20)-1 ind = [AB[i]&mask for i in range(ch)] + [A[i]&mask for i in range(k-ch)] + [B[i]&mask for i in range(k-ch)] st = sorted(notAB + AB[ch:] + A[k-ch:] + B[k-ch:]) ind += [st[i]&mask for i in range(m-2*k+ch)] print ' '.join(str(x) for x in ind)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
def solve(): n, k = map(int,input().split()) a1 = [] b1 = [] ab1 = [] for i in range(n): t,a,b = map(int,input().split()) if a == 1 and b == 1: ab1.append(t) elif a == 1: a1.append(t) elif b == 1: b1.append(t) a1.sort() b1.sort() for i in range(min(len(a1),len(b1))): ab1.append(a1[i]+b1[i]) ab1.sort() p = 0 qq = len(ab1) if qq < k: print(-1) else: ans = 0 for i in range(k): ans += ab1[i] print(ans) for i in range(1): solve()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); struct node { long long val = 0, num = 0; }; vector<node> s(1 << 15); long long dim = 1 << 14; void add(long long pos, long long l, long long r, long long p) { if (l > p || r < p) return; if (l == p && p == r) { s[pos].num++; s[pos].val += p; return; } add(2 * pos, l, (l + r) / 2, p); add(2 * pos + 1, (l + r) / 2 + 1, r, p); s[pos].num++; s[pos].val += p; } void cut(long long pos, long long l, long long r, long long p) { if (l > p || r < p) return; if (l == r && p == l) { s[pos].num--; s[pos].val -= p; return; } cut(2 * pos, l, (l + r) / 2, p); cut(2 * pos + 1, (l + r) / 2 + 1, r, p); s[pos].num--; s[pos].val -= p; } long long query(long long pos, long long l, long long r, long long k) { if (k == 0) return 0; if (s[pos].num == k) return s[pos].val; if (l == r) return s[pos].val / s[pos].num * k; if (s[2 * pos].num >= k) return query(2 * pos, l, (l + r) / 2, k); return s[2 * pos].val + query(2 * pos + 1, (l + r) / 2 + 1, r, k - s[2 * pos].num); } vector<pair<long long, long long> > v[4]; long long N, K, M; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M >> K; for (long long i = 0; i < N; i++) { long long a, b, c; cin >> a >> b >> c; if (b & c) v[3].push_back({a, i}); else if (b) v[1].push_back({a, i}); else if (c) v[2].push_back({a, i}); else v[0].push_back({a, i}); } for (long long i = 0; i < v[0].size(); i++) add(1, 0, dim - 1, v[0][i].first); long long mi = 1e12, best; long long cur = 0; sort(v[1].begin(), v[1].end()); sort(v[2].begin(), v[2].end()); sort(v[3].begin(), v[3].end()); long long presi = 0; for (long long i = 0; i < min(K, (long long)v[3].size()); i++) { cur += v[3][i].first; presi++; } for (long long i = K; i < v[3].size(); i++) add(1, 0, dim - 1, v[3][i].first); long long pos = 0; if (v[3].size() < K) { long long i = v[3].size(); while (v[1].size() > pos && v[2].size() > pos && i + pos < K) { cur += v[1][pos].first + v[2][pos].first; pos++; presi += 2; } if (pos + i < K) { cout << -1; return 0; } } for (long long i = pos; i < v[1].size(); i++) add(1, 0, dim - 1, v[1][i].first); for (long long i = pos; i < v[2].size(); i++) add(1, 0, dim - 1, v[2][i].first); if (presi > M) { cout << -1; return 0; } long long agg = query(1, 0, dim - 1, M - presi); if (cur + agg < mi) { mi = cur + agg; best = K - pos; } for (long long i = min(K, (long long)v[3].size()) - 1; i >= 0; i--) { cur -= v[3][i].first; presi--; add(1, 0, dim - 1, v[3][i].first); while (v[1].size() > pos && v[2].size() > pos && i + pos < K) { cur += v[1][pos].first + v[2][pos].first; cut(1, 0, dim - 1, v[1][pos].first); cut(1, 0, dim - 1, v[2][pos].first); pos++; presi += 2; } if (pos + i < K || presi > M) break; agg = query(1, 0, dim - 1, M - presi); if (cur + agg < mi) { mi = cur + agg; best = i; } } if (mi == 1e12) { cout << -1; return 0; } cout << mi << '\n'; vector<pair<long long, long long> > q; for (long long i = 0; i < best; i++) cout << v[3][i].second + 1 << " "; for (long long i = 0; i < K - best; i++) cout << v[2][i].second + 1 << " "; for (long long i = 0; i < K - best; i++) cout << v[1][i].second + 1 << " "; for (long long i = 0; i < v[0].size(); i++) q.push_back(v[0][i]); for (long long i = best; i < v[3].size(); i++) q.push_back(v[3][i]); for (long long i = K - best; i < v[1].size(); i++) q.push_back(v[1][i]); for (long long i = K - best; i < v[2].size(); i++) q.push_back(v[2][i]); sort(q.begin(), q.end()); for (long long i = 0; i < M - best - 2 * (K - best); i++) { cout << q[i].second + 1 << " "; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
// upsolve with kaiboy import java.io.*; import java.util.*; public class CF1374E2 extends PrintWriter { CF1374E2() { super(System.out); } static class Scanner { Scanner(InputStream in) { this.in = in; } InputStream in; byte[] bb = new byte[1 << 15]; int i, n; byte getc() { if (i >= n) { try { n = in.read(bb); } catch (IOException e) { n = 0; } if (n <= 0) return -1; i = 0; } return bb[i++]; } int nextInt() { byte c = 0; while (c <= ' ') c = getc(); boolean minus = c == '-'; if (minus) c = getc(); int a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); } return minus ? -a : a; } } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1374E2 o = new CF1374E2(); o.main(); o.flush(); } static final int INF = 0x7fffffff; int[] tt; Integer[] ta, tb, tc, td; int na, nb, nc, nd; int ka, kb, kd; boolean wiggle(int k, int m) { int d = Math.max(k - ka, 0) + Math.max(k - kb, 0); ka = Math.max(ka, k); kb = Math.max(kb, k); kd = Math.max(kd - d, 0); while (ka + kb + kd < m) { int t_ = INF, t, choice = 0; if (ka < na && t_ > (t = tt[ta[ka]])) { t_ = t; choice = 1; } if (kb < nb && t_ > (t = tt[tb[kb]])) { t_ = t; choice = 2; } if (kd < nd && t_ > (t = tt[td[kd]])) { t_ = t; choice = 4; } if (choice == 0) return false; if (choice == 1) ka++; else if (choice == 2) kb++; else kd++; } return true; } void main() { int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); tt = new int[n]; ta = new Integer[n]; tb = new Integer[n]; tc = new Integer[n]; td = new Integer[n]; for (int i = 0; i < n; i++) { int t = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); tt[i] = t; if (a == 1 && b == 0) ta[na++] = i; else if (a == 0 && b == 1) tb[nb++] = i; else if (a == 1 && b == 1) tc[nc++] = i; else td[nd++] = i; } if (nc + Math.min(na, nb) < k || k * 2 - nc > m) { println(-1); return; } Arrays.sort(ta, 0, na, (i, j) -> tt[i] - tt[j]); Arrays.sort(tb, 0, nb, (i, j) -> tt[i] - tt[j]); Arrays.sort(tc, 0, nc, (i, j) -> tt[i] - tt[j]); Arrays.sort(td, 0, nd, (i, j) -> tt[i] - tt[j]); int[] sa = new int[1 + na]; int[] sb = new int[1 + nb]; int[] sc = new int[1 + nc]; int[] sd = new int[1 + nd]; for (int i = 0; i < na; i++) sa[i + 1] = sa[i] + tt[ta[i]]; for (int i = 0; i < nb; i++) sb[i + 1] = sb[i] + tt[tb[i]]; for (int i = 0; i < nc; i++) sc[i + 1] = sc[i] + tt[tc[i]]; for (int i = 0; i < nd; i++) sd[i + 1] = sd[i] + tt[td[i]]; int l = Math.max(Math.max(k - Math.min(na, nb), k * 2 - m), 0); int r = Math.min(nc, m); int ans = INF, ka_ = 0, kb_ = 0, kc_ = 0, kd_ = 0; for (int x = r; x >= l && wiggle(Math.max(k - x, 0), m - x); x--) { int sum = sa[ka] + sb[kb] + sc[x] + sd[kd]; if (ans > sum) { ans = sum; ka_ = ka; kb_ = kb; kc_ = x; kd_ = kd; } } println(ans); for (int i = 0; i < ka_; i++) print(ta[i] + 1 + " "); for (int i = 0; i < kb_; i++) print(tb[i] + 1 + " "); for (int i = 0; i < kc_; i++) print(tc[i] + 1 + " "); for (int i = 0; i < kd_; i++) print(td[i] + 1 + " "); println(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.*; import java.io.*; public class Solution{ static PrintWriter out=new PrintWriter(System.out); public static void main (String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] input=br.readLine().trim().split(" "); int n=Integer.parseInt(input[0]); int k=Integer.parseInt(input[1]); PriorityQueue<Integer> pqAlice=new PriorityQueue<Integer>(); PriorityQueue<Integer> pqBob=new PriorityQueue<Integer>(); PriorityQueue<Integer> pqBoth=new PriorityQueue<Integer>(); int countAlice=0; int countBob=0; for(int i=0;i<n;i++) { input=br.readLine().trim().split(" "); int time=Integer.parseInt(input[0]); int alice=Integer.parseInt(input[1]); int bob=Integer.parseInt(input[2]); if(alice==1 && bob==1) { countAlice++; countBob++; pqBoth.add(time); } else if(alice==1) { countAlice++; pqAlice.add(time); } else if(bob==1){ countBob++; pqBob.add(time); } } if(countAlice<k || countBob<k) { System.out.println("-1"); return; } countAlice=0; countBob=0; int ans=0; while(countAlice<k && countBob<k) { int option1=Integer.MAX_VALUE; if(pqBoth.isEmpty()==false) { option1=pqBoth.remove(); } int temp1=Integer.MAX_VALUE; if(pqAlice.isEmpty()==false) { temp1=pqAlice.remove(); } int temp2=Integer.MAX_VALUE; if(pqBob.isEmpty()==false) { temp2=pqBob.remove(); } int option2=Integer.MAX_VALUE; if(temp1!=Integer.MAX_VALUE && temp2!=Integer.MAX_VALUE) { option2=temp1+temp2; } int min; if(option1<=option2) { min=option1; if(temp1!=Integer.MAX_VALUE) { pqAlice.add(temp1); } if(temp2!=Integer.MAX_VALUE) { pqBob.add(temp2); } } else { min=option2; if(option1!=Integer.MAX_VALUE) { pqBoth.add(option1); } } ans+=min; countAlice++; countBob++; } out.println(ans); out.flush(); out.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename Number> Number gcd(Number u, Number v) { while (v != 0) { Number r = u % v; u = v; v = r; } return u; } long long int binSearch(vector<long long int> v, long long int l, long long int r, long long int x) { if (r >= l) { long long int mid = l + (r - l) / 2; if (v[mid] == x) return mid; if (v[mid] > x) return binSearch(v, l, mid - 1, x); return binSearch(v, mid + 1, r, x); } return -1; } void solve() { long long int n, k; cin >> n >> k; long long int x, y, z; priority_queue<long long int, vector<long long int>, greater<long long int>> v1, v2, v3; for (long long int i = 0; i < n; i++) { cin >> x >> y >> z; if (y == 1 && z == 1) { v3.push(x); } else if (y == 1) { v1.push(x); } else if (z == 1) { v2.push(x); } } if ((v1.size() + v3.size()) < k) { cout << -1 << endl; return; } if ((v2.size() + v3.size()) < k) { cout << -1 << endl; return; } long long int a = 0, b = 0; long long int ans = 0; bool fg = 0; while (a < k || b < k) { if (v3.size() == 0) { fg = 1; break; } if (v1.size() != 0 && v2.size() != 0) { if ((v1.top() + v2.top()) <= v3.top()) { ans += (v1.top() + v2.top()); v1.pop(); v2.pop(); } else { ans += v3.top(); v3.pop(); } a++; b++; } else { ans += v3.top(); v3.pop(); a++; b++; } } if (fg) { while (a < k) { ans += v1.top(); v1.pop(); a++; } while (b < k) { ans += v2.top(); v2.pop(); b++; } } cout << ans << endl; return; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; while (t--) { solve(); } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class Solveaproblem { private static Book[] books; private static int a, b, ab, n; static class Book implements Comparator<Book> { int time, alice, bob; Book(int time, int alice, int bob) { this.time = time; this.alice = alice; this.bob = bob; } Book() {} @Override public int compare(Book b1, Book b2) { return Integer.compare(b1.time, b2.time); } } private static void changetwo() { for (a=a+1; a<n; a++) { if (books[a].alice==1 && books[a].bob==0) break; } for (b=b+1; b<n; b++) { if (books[b].alice==0 && books[b].bob==1) break; } } private static void changeboth() { for (ab=ab+1; ab<n; ab++) { if (books[ab].alice==1 && books[ab].bob==1) break; } } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sToken = new StringTokenizer(reader.readLine()); n = Integer.parseInt(sToken.nextToken()); int k = Integer.parseInt(sToken.nextToken()); books = new Book[n]; for (int i=0; i<n; i++) { sToken = new StringTokenizer(reader.readLine()); int t = Integer.parseInt(sToken.nextToken()); int a = Integer.parseInt(sToken.nextToken()); int b = Integer.parseInt(sToken.nextToken()); books[i] = new Book(t, a, b); } Arrays.sort(books, new Book()); a = n; b = n; ab = n; for (int i=0; i<n; i++) { if (a==n && books[i].alice==1 && books[i].bob==0) a = i; if (b==n && books[i].alice==0 && books[i].bob==1) b = i; if (ab==n && books[i].alice==1 && books[i].bob==1) ab = i; } long res = 0; for (int i=0; i<k; i++) { if ((a==n || b==n) && ab==n) { System.out.println(-1); System.exit(0); } if (ab==n) { res+=books[a].time+books[b].time; changetwo(); } else if ((a==n || b==n)) { res+=books[ab].time; changeboth(); } else { if (books[ab].time<=books[a].time+books[b].time) { res+=books[ab].time; changeboth(); } else { res+=books[a].time+books[b].time; changetwo(); } } } System.out.println(res); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author out_of_the_box */ 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 { MiscUtility ut = new MiscUtility(false); public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int k = in.nextInt(); int[] t = new int[n]; int[] a = new int[n]; int[] b = new int[n]; for (int i = 0; i < n; i++) { t[i] = in.nextInt(); a[i] = in.nextInt(); b[i] = in.nextInt(); } int ans = 0; List<Integer> alike = new ArrayList<>(); List<Integer> blike = new ArrayList<>(); List<Integer> like = new ArrayList<>(); for (int i = 0; i < n; i++) { if (a[i] == 1 && b[i] == 1) { like.add(i); } else if (a[i] == 1) { alike.add(i); } else if (b[i] == 1) { blike.add(i); } } alike.sort(Comparator.comparing(i -> t[i])); blike.sort(Comparator.comparing(i -> t[i])); like.sort(Comparator.comparing(i -> t[i])); int ai = 0; int bi = 0; int ind = 0; int an = alike.size(); int bn = blike.size(); int cn = like.size(); ut.printDebug(an, "an", bn, "bn", cn, "cn"); int arem = k; int brem = k; while (ai < an && bi < bn && ind < cn && arem > 0 && brem > 0) { int at = t[alike.get(ai)]; int bt = t[blike.get(bi)]; int ct = t[like.get(ind)]; if (at + bt < ct) { ans += at + bt; ai++; bi++; } else { ans += ct; ind++; } arem--; brem--; } if (arem == 0) { while (brem > 0 && ind < cn && bi < bn) { int bt = t[blike.get(bi)]; int ct = t[like.get(ind)]; if (bt < ct) { ans += bt; bi++; brem--; } else { ans += ct; ind++; brem--; } } while (brem > 0 && ind < cn) { ans += t[like.get(ind)]; ind++; brem--; } while (brem > 0 && bi < bn) { ans += t[blike.get(bi)]; bi++; brem--; } if (brem != 0) { out.println(-1); return; } else { out.println(ans); return; } } else if (brem == 0) { while (arem > 0 && ind < cn && ai < an) { int at = t[alike.get(ai)]; int ct = t[like.get(ind)]; if (at < ct) { ans += at; ai++; arem--; } else { ans += ct; ind++; arem--; } } while (arem > 0 && ind < cn) { int ct = t[like.get(ind)]; ans += ct; arem--; ind++; } while (arem > 0 && ai < an) { int at = t[alike.get(ai)]; ans += at; arem--; ai++; } if (arem != 0) { out.println(-1); return; } else { out.println(ans); return; } } else { if (ai == an) { while (arem > 0 && ind < cn) { int ct = t[like.get(ind)]; ans += ct; arem--; brem--; ind++; } if (arem != 0) { out.println(-1); return; } while (brem > 0 && ind < cn && bi < bn) { int bt = t[blike.get(bi)]; int ct = t[like.get(ind)]; if (bt < ct) { ans += bt; bi++; brem--; } else { ans += ct; ind++; brem--; } } while (brem > 0 && ind < cn) { ans += t[like.get(ind)]; ind++; brem--; } while (brem > 0 && bi < bn) { ans += t[blike.get(bi)]; bi++; brem--; } if (brem != 0) { out.println(-1); return; } else { out.println(ans); return; } } else if (bi == bn) { while (brem > 0 && ind < cn) { int ct = t[like.get(ind)]; ans += ct; brem--; arem--; ind++; } if (brem != 0) { out.println(-1); return; } while (arem > 0 && ind < cn && ai < an) { int at = t[alike.get(ai)]; int ct = t[like.get(ind)]; if (at < ct) { ans += at; ai++; arem--; } else { ans += ct; ind++; arem--; } } while (arem > 0 && ind < cn) { int ct = t[like.get(ind)]; ans += ct; arem--; ind++; } while (arem > 0 && ai < an) { int at = t[alike.get(ai)]; ans += at; arem--; ai++; } if (arem != 0) { out.println(-1); return; } else { out.println(ans); return; } } else { MiscUtility.assertion(ind == cn, String.format("ind [%d] is not cn [%d]", ind, cn)); while (arem > 0 && ai < an) { int at = t[alike.get(ai)]; ans += at; arem--; ai++; } if (arem != 0) { out.println(-1); return; } while (brem > 0 && bi < bn) { int bt = t[blike.get(bi)]; ans += bt; brem--; bi++; } if (brem != 0) { out.println(-1); return; } out.println(ans); } } } } static class MiscUtility { private boolean debug; public MiscUtility() { debug = System.getProperty("ONLINE_JUDGE") == null; } public MiscUtility(boolean debug) { this.debug = debug; } public static void assertion(boolean condition, String message) { if (!condition) { throw new RuntimeException("Assertion failed. " + message); } } public void printDebug(Object... objects) { if (debug) { boolean isName = false; Object prev = null; for (Object object : objects) { if (isName) { System.out.print(String.format("[%s: %s] ", object, prev.toString())); } else { prev = object; } isName = !isName; } System.out.println(); } } } 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(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
from __future__ import division, print_function # import threading # threading.stack_size(2**27) import sys sys.setrecursionlimit(10**4) # sys.stdin = open('inpy.txt', 'r') # sys.stdout = open('outpy.txt', 'w') from sys import stdin, stdout import bisect #c++ upperbound import math import heapq i_m=9223372036854775807 def modinv(n,p): return pow(n,p-2,p) def cin(): return map(int,sin().split()) def ain(): #takes array as input return list(map(int,sin().split())) def sin(): return input() def inin(): return int(input()) import math def GCD(x, y): x=abs(x) y=abs(y) if(min(x,y)==0): return max(x,y) while(y): x, y = y, x % y return x def Divisors(n) : l = [] for i in range(1, int(math.sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) return l prime=[] def SieveOfEratosthenes(n): global prime prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 f=[] for p in range(2, n): if prime[p]: f.append(p) return f q=[] """*******************************************************""" def main(): n,m=cin() a=[] b=[] c=[] for _ in range(n): i,j,k=cin() if j==1 and k==1: a.append(i) elif j==1: b.append(i) elif k==1: c.append(i) b.sort() c.sort() x=min(len(b),len(c)) # print(a,b,c) for i in range(x): a.append(c[i]+b[i]) a.sort() # print(a) if len(a)<m: print(-1) else: print(sum(a[:m])) ######## Python 2 and 3 footer by Pajenegod and c1729 # Note because cf runs old PyPy3 version which doesn't have the sped up # unicode strings, PyPy3 strings will many times be slower than pypy2. # There is a way to get around this by using binary strings in PyPy3 # but its syntax is different which makes it kind of a mess to use. # So on cf, use PyPy2 for best string performance. py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' # Read all remaining integers in stdin, type is given by optional argument, this is fast def readnumbers(zero = 0): conv = ord if py2 else lambda x:x A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read() try: while True: if s[i] >= b'R' [0]: numb = 10 * numb + conv(s[i]) - 48 elif s[i] == b'-' [0]: sign = -1 elif s[i] != b'\r' [0]: A.append(sign*numb) numb = zero; sign = 1 i += 1 except:pass if s and s[-1] >= b'R' [0]: A.append(sign*numb) return A # threading.Thread(target=main).start() if __name__== "__main__": main()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys from collections import defaultdict input = lambda:sys.stdin.readline().strip() n,k = map(int,input().split()) a = [] b = [] c = [] for i in range(n): ti,ai,bi = map(int,input().split()) if ai==0 and bi==1: a.append(ti) elif ai==1 and bi==0: b.append(ti) elif ai==1 and bi==1: c.append(ti) a.sort() b.sort() c.sort() for i in range(1,len(a)): a[i]+=a[i-1] for i in range(1,len(b)): b[i]+=b[i-1] for i in range(1,len(c)): c[i]+=c[i-1] c = [0]+c # print(a,b,c) ans = float('inf') for i in range(len(c)): rem = k-i if i==k: ans = min(ans,c[i]) if rem-1>=0 and rem<=min(len(a),len(b)): ans = min(ans,c[i]+a[rem-1]+b[rem-1]) if ans==float('inf'): print(-1) else: print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Node { int son[2], val, pri, siz; int sum; } t[2000086]; int cnt; int root; inline int rand() { static int seed = 12345; return seed = (int)seed * 482711LL % 2147483647; } void up(int x) { t[x].siz = t[(t[x].son[0])].siz + t[(t[x].son[1])].siz + 1; t[x].sum = t[(t[x].son[0])].sum + t[(t[x].son[1])].sum + t[x].val; } void split(int x, int val, int &a, int &b) { if (!x) { a = b = 0; return; } if (t[x].val <= val) a = x, split((t[x].son[1]), val, t[x].son[1], b); else b = x, split((t[x].son[0]), val, a, t[x].son[0]); up(x); } void Split(int x, int k, int &a, int &b) { if (!x) { a = b = 0; return; } if (t[(t[x].son[0])].siz < k) a = x, Split((t[x].son[1]), k - t[(t[x].son[0])].siz - 1, t[x].son[1], b); else b = x, Split((t[x].son[0]), k, a, t[x].son[0]); up(x); } int merge(int x, int y) { if (x == 0 || y == 0) return x + y; if (t[x].pri > t[y].pri) { t[x].son[1] = merge((t[x].son[1]), y); up(x); return x; } else { t[y].son[0] = merge(x, (t[y].son[0])); up(y); return y; } } int newnode(int val) { cnt++; t[cnt].val = t[cnt].sum = val; t[cnt].pri = rand(); t[cnt].siz = 1; return cnt; } int n, m, k; vector<pair<int, int>> v[3]; int x, a, b, c, d; int ans = 2e9 + 5, id; inline void insert(int x) { split(root, x, a, b); root = merge(merge(a, newnode(x)), b); } inline void erase(int x) { split(root, x, a, b); split(a, x - 1, c, d); root = merge(merge(c, merge((t[d].son[0]), (t[d].son[1]))), b); } void dfs(int i) { if (!i) return; dfs((t[i].son[0])); printf("%d ", t[i].val); dfs((t[i].son[1])); } inline int query(int x) { if (!x) return 0; int a, b; Split(root, x, a, b); int sum = t[a].sum; root = merge(a, b); return sum; } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &x, &a, &b); if (!a && !b) { insert(x), q.push({x, i}); continue; } if (a && b) v[0].push_back({x, i}), insert(x); else if (a) v[1].push_back({x, i}); else v[2].push_back({x, i}); } if (v[0].size() + v[1].size() < k || v[0].size() + v[2].size() < k) return printf("-1"), 0; if (v[0].size() < k && v[0].size() + 2 * (k - v[0].size()) > m) return printf("-1"), 0; for (int i = 0; i < 3; i++) sort(v[i].begin(), v[i].end()); for (int i = 0; i < 3; i++) { for (int j = 1; j < v[i].size(); j++) v[i][j].first += v[i][j - 1].first; } for (int i = 1; i < 3; i++) for (int j = k; j < v[i].size(); j++) insert(v[i][j].first - v[i][j - 1].first); if (v[1].size() >= k && v[2].size() >= k) { if (2 * k <= m) { int sum = v[1][k - 1].first + v[2][k - 1].first + query(m - 2 * k); if (sum < ans) ans = sum, id = 0; } } if (v[1].size() >= k) insert(v[1][k - 1].first - (k - 1 ? v[1][k - 2].first : 0)); if (v[2].size() >= k) insert(v[2][k - 1].first - (k - 1 ? v[2][k - 2].first : 0)); for (int i = 0; i < k - 1 && i < v[0].size(); i++) { erase(v[0][i].first - (i ? v[0][i - 1].first : 0)); if (k - 2 - i >= 0 && k - 2 - i < v[1].size() && k - 2 - i < v[2].size()) { if (i + 1 + 2 * (k - (i + 1)) <= m) { int sum = v[0][i].first + v[1][k - 2 - i].first + v[2][k - 2 - i].first + query(m - (i + 1 + 2 * (k - (i + 1)))); if (sum < ans) ans = sum, id = i + 1; } } if (k - 2 - i >= 0 && k - 2 - i < v[1].size()) insert(v[1][k - 2 - i].first - (k - 2 - i ? v[1][k - 3 - i].first : 0)); if (k - 2 - i >= 0 && k - 2 - i < v[2].size()) insert(v[2][k - 2 - i].first - (k - 2 - i ? v[2][k - 3 - i].first : 0)); } if (v[0].size() >= k) { erase(v[0][k - 1].first - (k - 1 ? v[0][k - 2].first : 0)); int sum = v[0][k - 1].first + query(m - k); if (sum < ans) ans = sum, id = k; } printf("%d\n", ans); for (int i = 0; i < id; i++) printf("%d ", v[0][i].second); for (int i = 1; i <= k - id; i++) printf("%d %d ", v[1][i - 1].second, v[2][i - 1].second); for (int i = id; i < v[0].size(); i++) q.push({v[0][i].first - (i ? v[0][i - 1].first : 0), v[0][i].second}); for (int i = k - id; i < v[1].size(); i++) q.push({v[1][i].first - (i ? v[1][i - 1].first : 0), v[1][i].second}); for (int i = k - id; i < v[2].size(); i++) q.push({v[2][i].first - (i ? v[2][i - 1].first : 0), v[2][i].second}); for (int i = m - id - (k - id) * 2; i; i--) { printf("%d ", q.top().second), q.pop(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
/* ID: tommatt1 LANG: JAVA TASK: */ import java.util.*; import java.io.*; public class cf1374e2{ static PriorityQueue<pair> rem00;static PriorityQueue<pair> rem01;static PriorityQueue<pair> rem10;static PriorityQueue<pair> rem11; static PriorityQueue<pair> add00;static PriorityQueue<pair> add01;static PriorityQueue<pair> add10;static PriorityQueue<pair> add11; static long ans; static int ak,bk,curm; public static void main(String[] args)throws IOException { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(bf.readLine()); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); int k=Integer.parseInt(st.nextToken()); add00=new PriorityQueue<pair>();add01=new PriorityQueue<pair>();add10=new PriorityQueue<pair>();add11=new PriorityQueue<pair>(); rem00=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); rem01=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); rem10=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); rem11=new PriorityQueue<pair>((a,b)->{return b.compareTo(a);}); pair[] bks=new pair[n]; for(int i=0;i<n;i++) { st=new StringTokenizer(bf.readLine()); int t1=Integer.parseInt(st.nextToken()); int a1=Integer.parseInt(st.nextToken()); int b1=Integer.parseInt(st.nextToken()); bks[i]=new pair(t1,a1,b1,i+1); } Arrays.sort(bks); for(pair i:bks) { if(i.a==1&&i.b==1) { add11.add(i); } else if(i.a==1) { add10.add(i); } else if(i.b==1) { add01.add(i); } else { add00.add(i); continue; } if(i.a==1&&i.b==1) { if(ak<k||bk<k) { add(i); if(ak>k&&!rem10.isEmpty()) { pair p=rem10.poll(); remove(p); } if(bk>k&&!rem01.isEmpty()) { pair p=rem01.poll(); remove(p); } } else { if(!rem10.isEmpty()&&!rem01.isEmpty()) { int old=rem10.peek().t+rem01.peek().t; if(old>i.t) { pair p1=rem10.poll(); pair p2=rem01.poll(); remove(p1); remove(p2); add(i); } } } } else if(i.a==1&&ak<k) { add(i); } else if(i.b==1&&bk<k){ add(i); } } while(curm>m) { pair p=low(add11); pair rema=high(rem10); pair remb=high(rem01); if(p==null||rema==null||remb==null) { if(n==6561) out.println("cant decrease curm"); out.println(-1); out.close(); System.exit(0); } remove(rema); remove(remb); add(p); } while(curm<m) { pair min00=low(add00); pair min10=low(add10); pair min01=low(add01); pair min11=low(add11); pair min=min(min(min00,min01),min(min10,min11)); pair max11=high(rem11); if(max11==null|min10==null||min01==null) { if(min==null) { if(n==6561) out.println("cant increase curm"); out.println(-1); out.close(); System.exit(0); } add(min); } else { if(min.t<min01.t+min10.t-max11.t) { add(min); } else { remove(max11); add(min10); add(min01); } } } if(ak<k||bk<k) { if(n==6561) out.println("final"); out.println(-1); out.close(); System.exit(0); } else { out.println(ans); } for(pair i:bks) { if(i.inc) { out.print(i.id+" "); } } out.println(); out.close(); } static pair low(PriorityQueue<pair> pq) { while(!pq.isEmpty()) { pair p=pq.peek(); if(p.inc) pq.poll(); else return p; } return null; } static pair high(PriorityQueue<pair> pq) { while(!pq.isEmpty()) { pair p=pq.peek(); if(!p.inc) pq.poll(); else return p; } return null; } static pair min(pair a, pair b) { if(a==null) return b; if(b==null) return a; return a.t<=b.t?a:b; } static void add(pair i) { i.inc=true; ans+=i.t; curm++; if(i.a==1&&i.b==1) { ak++; bk++; rem11.add(i); } else if(i.a==1) { ak++; rem10.add(i); } else if(i.b==1) { bk++; rem01.add(i); } else { rem00.add(i); } } static void remove(pair i) { i.inc=false; ans-=i.t; curm--; if(i.a==1&&i.b==1) { ak--; bk--; add11.add(i); } else if(i.a==1) { ak--; add10.add(i); } else if(i.b==1) { bk--; add01.add(i); } else { add00.add(i); } } static class pair implements Comparable<pair>{ int t,a,b; boolean inc;int id; public pair(int t1,int x,int y,int id1) { t=t1;a=x;b=y;id=id1; } public int compareTo(pair p) { return t-p.t; //if(a>p.a) return 1; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) alice=[] bob=[] both =[] for i in range(n): t,a,b = map(int,input().split()) if (a==1 and b==1): both.append(t) elif (a==1): alice.append(t) elif (b==1): bob.append(t) alice.sort() bob.sort() l = min(len(alice),len(bob)) for i in range(l): both.append(alice[i]+bob[i]) if (len(both)< k): print(-1) else: both.sort() sumi=0 for i in range(k): sumi=sumi+both[i] print(sumi)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.Collections; import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int l=s.nextInt(); int m=s.nextInt(); ArrayList<Long> a1=new ArrayList<>(); ArrayList<Long> a2=new ArrayList<>(); ArrayList<Long> a3=new ArrayList<>(); int b1=0; int b2=0; int b3=0; for (int i=0;i<l;i++){ long h=s.nextInt(); int u=s.nextInt(); int y=s.nextInt(); if (u==1&&y==0){ a1.add(h); b1++; } else if (u==0&&y==1){ a2.add(h); b2++; } else if (u==1&&y==1){ a3.add(h); b3++; } } int c1=0; int c3=0; Collections.sort(a1); Collections.sort(a2); Collections.sort(a3); long ans=0; if (b1+b3<m||b2+b3<m){ System.out.println(-1); } else { for (int i=0;i<m;i++){ if (b1==c1||b2==c1){ ans+=a3.get(c3); c3++; } else { if (b3!=c3){ if (a1.get(c1)+a2.get(c1)<a3.get(c3)){ ans+=a1.get(c1)+a2.get(c1); c1++; } else { ans+=a3.get(c3); c3++; } } else { ans+=a1.get(c1)+a2.get(c1); c1++; } } } System.out.println(ans); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys import random from math import * def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def linput(): return list(input()) def rinput(): return map(int, tinput()) def fiinput(): return map(float, tinput()) def rlinput(): return list(map(int, input().split())) def trinput(): return tuple(rinput()) def srlinput(): return sorted(list(map(int, input().split()))) def NOYES(fl): if fl: print("NO") else: print("YES") def YESNO(fl): if fl: print("YES") else: print("NO") def main(): #n = iinput() #k = iinput() #m = iinput() #n = int(sys.stdin.readline().strip()) n, k = rinput() #n, m = rinput() #m, k = rinput() #n, k, m = rinput() #n, m, k = rinput() #k, n, m = rinput() #k, m, n = rinput() #m, k, n = rinput() #m, n, k = rinput() #q = srlinput() #q = linput() q = [] for i in range(n): q.append(rlinput()) q.sort() w,e,r, fak,pr,pe,pw = [], [], [], [], [0],[0],[0] res1, res2 = 0, 0 for i in range(n): res1, res2 = res1 + q[i][1], res2 + q[i][2] if q[i][1] and q[i][2]: w.append(q[i][0]) pw.append(w[-1] + pw[-1]) elif q[i][1]: e.append(q[i][0]) pe.append(e[-1] + pe[-1]) elif q[i][2]: r.append(q[i][0]) pr.append(r[-1] + pr[-1]) else: fak.append(q[i]) if min(res1, res2) < k: print(-1) else: res = 5e9 for i in range(1 + min(k, len(w))): if k - i <= min(len(r), len(e)): res = min(res, pw[i] + pr[k - i] + pe[k - i]) print(res) for inytd in range(1): main()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = [int(i) for i in input().split()] a1 = [] a2 = [] a3 = [] A1 = False A2 = False A3 = False amount = 0 summ = 0 for i in range(n): t, a, b = [int(z) for z in input().split()] if a == 1 and b == 1: a1.append(t) A1 = True elif a == 1: a2.append(t) A2 = True elif b == 1: a3.append(t) A3 = True a1 = sorted(a1) a2 = sorted(a2) a3 = sorted(a3) if len(a1) + min(len(a2), len(a3)) < k: print(-1) else: x, y = 0, 0 len1 = len(a1) len2 = len(a2) len3 = len(a3) while amount < k: if x == len2 or x == len3: summ += sum(a1[y:y+k-amount]) break if y == len1: summ += sum(a2[x:x+k-amount] + a3[x:x+k-amount]) break if a3[x] + a2[x] < a1[y]: summ += a3[x] + a2[x] x += 1 amount += 1 else: summ += a1[y] y += 1 amount += 1 print(summ)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.PriorityQueue; 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 * * @author Pranay2516 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE1 solver = new TaskE1(); solver.solve(1, in, out); out.close(); } static class TaskE1 { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), k = in.nextInt(); long sum = 0; ArrayList<Integer>[] a = new ArrayList[4]; PriorityQueue<Integer> pq = new PriorityQueue<>(); for (int i = 0; i < 4; ++i) a[i] = new ArrayList<>(); for (int i = 0; i < n; ++i) { int time = in.nextInt(), alice = in.nextInt(), bob = in.nextInt(); a[alice * 2 + bob].add(time); if (alice * 2 + bob == 3) pq.add(time); } Collections.sort(a[1]); Collections.sort(a[2]); for (int i = 0; i < a[1].size() && i < a[2].size(); ++i) pq.add(a[1].get(i) + a[2].get(i)); if (k > pq.size()) { out.println(-1); return; } for (int i = 0; i < k; ++i) sum += pq.poll(); out.println(sum); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public 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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using std::cin; using std::cout; using std::endl; using std::string; void __Check(bool condition, const char* expression, int line) { if (!condition) { fprintf(stderr, "Check failed at line %d: %s\n", line, expression); exit(-1); } } template <class Collection, class Key> bool ContainsKey(const Collection& collection, const Key& key) { return collection.find(key) != collection.end(); } const int INF = 0x3F3F3F3F; const long long INF64 = 0x3F3F3F3F3F3F3F3F; const int INIT = -1; int n, m, k; std::vector<std::pair<int, int> > g[4]; int pos[4]; void Solve() { if (((int)g[3].size()) + std::min(((int)g[1].size()), ((int)g[2].size())) < k) { cout << "-1" << endl; return; } int want3 = std::min(((int)g[3].size()), k); int cnt = want3; int sum = 0; for (; pos[3] < want3; pos[3]++) { sum += g[3][pos[3]].first; } for (int i = want3; i < k; i++) { sum += g[1][pos[1]++].first; sum += g[2][pos[2]++].first; cnt += 2; } (__Check(pos[1] == pos[2], "pos[1] == pos[2]", 60)); (__Check(want3 + 2 * pos[1] >= k, "want3 + 2 * pos[1] >= k", 61)); if (cnt > m) { cout << "-1" << endl; return; } for (; cnt < m; cnt++) { std::vector<int> ar = { pos[0] < ((int)g[0].size()) ? g[0][pos[0]].first : INF, pos[1] < ((int)g[1].size()) ? g[1][pos[1]].first : INF, pos[2] < ((int)g[2].size()) ? g[2][pos[2]].first : INF, pos[3] < ((int)g[3].size()) ? g[3][pos[3]].first : INF}; if (pos[3] > 0 && pos[1] < ((int)g[1].size()) && pos[2] < ((int)g[2].size())) { int last3 = g[3][pos[3] - 1].first; int next12 = g[1][pos[1]].first + g[2][pos[2]].first; int cost = next12 - last3; ar.push_back(cost); } int mi = std::min_element((ar).begin(), (ar).end()) - ar.begin(); if (mi == 4) { sum -= g[3][--pos[3]].first; sum += g[1][pos[1]++].first; sum += g[2][pos[2]++].first; } else { sum += g[mi][pos[mi]++].first; } } cout << sum << endl; for (int i = 0; i < 4; i++) { for (int j = 0; j < pos[i]; j++) { cout << g[i][j].second + 1 << " "; } } cout << endl; } int main() { cin >> n >> m >> k; for (int i = 0; i < n; i++) { int t, x, y; cin >> t >> x >> y; int idx = (x << 1) | y; g[idx].push_back({t, i}); } for (int i = 0; i < 4; i++) { std::sort((g[i]).begin(), (g[i]).end()); } Solve(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int md = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tt{}, T{}; tt = 1; while (tt--) { long long n{}, m{}, k{}, a{}, b{}, c{}, f{}, p = -1, sum = 0; long long ans{}; cin >> n >> k; std::vector<long long> alice, bob, both; for (long long i = 0; i < n; ++i) { cin >> a >> b >> c; if (b * c) { both.push_back(a); f += a; } else if (b) { alice.push_back(a); } else if (c) { bob.push_back(a); } else { p = max(a, p); } sum += a; } sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); a = alice.size(); b = bob.size(); c = both.size(); if (k > c + min(a, b)) { cout << "-1\n"; return 0; } for (int i = 0; i < min(a, b); ++i) { both.push_back(alice[i] + bob[i]); } sort(both.begin(), both.end()); cout << accumulate(both.begin(), both.begin() + k, 0LL) << "\n"; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
line = input() n, m, k = [int(i) for i in line.split(' ')] books, allL, aliceL, bobL, other =list(range(1, n + 1)), [], [], [], [] ts = [[] for _ in range(n + 1)] for i in range(n): line = input() t, a, b = [int(j) for j in line.split(' ')] ts[i + 1] = [t, a, b] if a == 1 and b == 1: allL.append(i + 1) elif a == 1: aliceL.append(i + 1) elif b == 1: bobL.append(i + 1) else: other.append(i + 1) if len(allL) + min(len(aliceL), len(bobL)) < k or (len(allL) < k and 2 * (k - len(allL)) > m - len(allL)) : print(-1) exit() books.sort(key=lambda x: (ts[x][0], -ts[x][2]*ts[x][1])) allL.sort(key=lambda x: (ts[x][0], -ts[x][2]*ts[x][1])) aliceL.sort(key=lambda x: (ts[x][0], -ts[x][2]*ts[x][1])) bobL.sort(key=lambda x: (ts[x][0], -ts[x][2]*ts[x][1])) other.sort(key=lambda x: (ts[x][0], -ts[x][2]*ts[x][1])) x = max(2 * k - m, 0, k - min(len(aliceL), len(bobL)), m - len(aliceL) - len(bobL) - len(other)) cura, curb, curo, cur = max(0, k - x), max(0, k - x), 0, sum(ts[i][0] for i in allL[:x]) cur += sum(ts[i][0] for i in aliceL[:cura]) + sum(ts[i][0] for i in bobL[:curb]) while cura + x + curb + curo < m: an = ts[aliceL[cura]][0] if cura < len(aliceL) else 9999999999 bn = ts[bobL[curb]][0] if curb < len(bobL) else 9999999999 on = ts[other[curo]][0] if curo < len(other) else 9999999999 cur += min(an, bn, on) if an <= bn and an <= on: cura += 1 elif bn <= an and bn <= on: curb += 1 else: curo += 1 res, a, b, o = cur, cura, curb, curo for i in range(x + 1, len(allL) + 1): #ιƒ½ε–œζ¬’ηš„ι•ΏεΊ¦ cur += ts[allL[i - 1]][0] if cura > 0: cura -= 1 cur -= ts[aliceL[cura]][0] if curb > 0: curb -= 1 cur -= ts[bobL[curb]][0] if curo > 0: curo -= 1 cur -= ts[other[curo]][0] while cura + i + curb + curo < m: an = ts[aliceL[cura]][0] if cura < len(aliceL) else 9999999999 bn = ts[bobL[curb]][0] if curb < len(bobL) else 9999999999 on = ts[other[curo]][0] if curo < len(other) else 9999999999 cur += min(an, bn, on) if an <= bn and an <= on: cura += 1 elif bn <= an and bn <= on: curb += 1 else: curo += 1 if res > cur: res,x, a, b, o = cur,i, cura, curb, curo else: break print(res) for i in range(x): print(allL[i], end=' ') for i in range(a): print(aliceL[i], end = ' ') for i in range(b): print(bobL[i], end = ' ') for i in range(o): print(other[i], end = ' ')
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
import sys class segtree: def __init__(self, data): n = len(data);self.m = 1 while self.m < n: self.m *= 2 self.data = [0] * (2 * self.m);self.data[self.m: self.m + n] = data for i in reversed(range(1, self.m)):self.data[i] = self.data[2 * i] + self.data[2 * i + 1] def __setitem__(self, i, x): i += self.m;self.data[i] = x;i >>= 1 while i:self.data[i] = self.data[2 * i] + self.data[2 * i + 1];i >>= 1 def finder(self, k): if k == 0:return -1 assert self.data[1] >= k i = 1 while i < self.m: i *= 2 if self.data[i] < k:k -= self.data[i];i += 1 return i - self.m def getter(self, l, r): l += self.m;r += self.m;s = 0 while l < r: if l & 1:s += self.data[l];l += 1 if r & 1:r -= 1;s += self.data[r] l >>= 1;r >>= 1 return s inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 N = inp[ii]; ii += 1;M = inp[ii]; ii += 1;k = inp[ii]; ii += 1 T = inp[ii + 0: ii + 3 * N: 3];A = inp[ii + 1: ii + 3 * N: 3];B = inp[ii + 2: ii + 3 * N: 3] order = sorted(range(N), key = T.__getitem__) T = [T[i] for i in order];A = [A[i] for i in order];B = [B[i] for i in order] TA,TB,TAB = [],[],[] for i in range(N): if A[i] and B[i]:TAB.append(i) elif A[i]:TA.append(i) elif B[i]:TB.append(i) n = len(TA) m = len(TB) nm = len(TAB) mark = [1] * N times = list(T) upper = min(k, nm) picked = k - upper if picked > n or picked > m:print (-1);sys.exit() tsum = 0 for j in range(picked): i = TA[j] tsum += T[i] mark[i] = 0 times[i] = 0 i = TB[j] tsum += T[i] mark[i] = 0 times[i] = 0 for j in range(upper): i = TAB[j] tsum += T[i] mark[i] = 0 times[i] = 0 marker = segtree(mark) timer = segtree(times) time = inf = 10**9 * 2 + 100 optx = -1 for x in reversed(range(upper + 1)): books = x + 2 * (k - x) if books <= M: cand = tsum + timer.getter(0, marker.finder(M - books) + 1) if cand < time:time = cand;optx = x if x:i = TAB[x - 1];tsum -= T[i];marker[i] = mark[i] = 1;timer[i] = times[i] = T[i] j = k - x if j >= n or j >= m:break i = TA[j] tsum += T[i] marker[i] = mark[i] = 0 timer[i] = times[i] = 0 i = TB[j] tsum += T[i] marker[i] = mark[i] = 0 timer[i] = times[i] = 0 if time == inf:print (-1) else: print (time) ab = optx;a = b = k - optx;rest = M - ab - a - b;picked = [] for i in range(N): if A[i] and B[i] and ab:ab -= 1;picked.append(i) elif not B[i] and A[i] and a:a -= 1;picked.append(i) elif not A[i] and B[i] and b:b -= 1;picked.append(i) elif rest:picked.append(i);rest -= 1 print (' '.join(str(order[x] + 1) for x in picked))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
## ********************************************* ## * * ## * | | \ / |- |\ --- |\ | * ## * | | \ / | \ | | | | | \ | * ## * |--| \ / | | |/ | | | \ | * ## * | | / | / | \ | | | \ | * ## * | | / |- | \ --- | \| * ## * * ## ********************************************* n, k = map(int, input().split()) t=list() a=list() b=list() alice=list() bob=list() both=list() for i in range(0, n): x, y, z = map(int, input().split()) if y and z: both.append(x) else: if y: alice.append(x) else: if z: bob.append(x) alice.sort() bob.sort() both.sort() INF = 200000000000 res = INF if len(alice) > 1: for i in range(1, len(alice)): alice[i] += alice[i-1] if len(bob) > 1: for i in range(1, len(bob)): bob[i] += bob[i-1] if len(both) > 1: for i in range(1, min(k, len(both))): both[i] += both[i-1] if not((len(alice)) * (len(bob))): if len(both) < k: res = -1 else: res = both[k-1] else: if not(len(both)): if len(bob) >= k and len(alice) >= k: res = bob[k-1] + alice[k-1] else: res = -1 else: for cnt in range(1, len(both)+1): if cnt > k: break i = k - cnt - 1 if i == -1: res = min(res, both[cnt-1]) break if i < len(alice) and i < len(bob): res = min(res, both[cnt-1] + alice[i] + bob[i]) if k <= len(alice) and k <= len(bob): res = min(res, alice[k-1] + bob[k-1]) if res == INF: print(-1) else: print(int(res)) ## 1 ## * ## *** ## ***** ## \ **000** / ## \ ***0|0*** / ## ****000**** ## ************* ## *************** ## ***************** ## .| |.
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
#!/usr/bin/env pypy3 from sys import stdin, stdout import heapq def input(): return stdin.readline().strip() def ans(both, alice_only, bob_only, neither, M, K): both_fc = [b[0] for b in both] neither_fc = [b[0] for b in neither] alice_only_fc = [a[0] for a in alice_only] bob_only_fc = [b[0] for b in bob_only] both_prefix = [0] for b in both_fc: both_prefix += [both_prefix[-1] + b] alice_only_prefix = [0] for b in alice_only_fc: alice_only_prefix += [alice_only_prefix[-1] + b] bob_only_prefix = [0] for b in bob_only_fc: bob_only_prefix += [bob_only_prefix[-1] + b] neither_prefix = [0] for b in neither_fc: neither_prefix += [neither_prefix[-1] + b] p = min(len(both), M) q = 0 r = 0 s = 0 # maintain K condition while p + q < K: if q == len(alice_only): break q += 1 if p + q < K: return None while p + r < K: if r == len(bob_only): break r += 1 if p + r < K: return None # maintain M condition while p+q+r+s < M: # greedily increment q,r,s g = dict() if q < len(alice_only): g["q"] = alice_only_fc[q] if r < len(bob_only): g["r"] = bob_only_fc[r] if s < len(neither): g["s"] = neither_fc[s] if len(g.keys()) == 0: return None to_increment = min(g, key=g.get) if to_increment == "q": q += 1 if to_increment == "r": r += 1 if to_increment == "s": s += 1 while p+q+r+s > M: # greedily decrement q,r,s g = dict() if 0 < q and p + q > K: g["q"] = alice_only_fc[q-1] if 0 < r and p + r > K: g["r"] = bob_only_fc[r-1] if 0 < s: g["s"] = neither_fc[s-1] if len(g.keys()) == 0: break to_decrement = max(g, key=g.get) if to_decrement == "q": q -= 1 if to_decrement == "r": r -= 1 if to_decrement == "s": s -= 1 if p+q+r+s > M: return None best_score = both_prefix[p] + alice_only_prefix[q] + bob_only_prefix[r] + neither_prefix[s] best_pqrs = (p,q,r,s) # print("starting candidate", best_score, best_pqrs) while p > 0: p -= 1 # maintain K condition while p + q < K: if q == len(alice_only): break q += 1 if p + q < K: break while p + r < K: if r == len(bob_only): break r += 1 if p + r < K: break # maintain M condition while p+q+r+s < M: # greedily increment q,r,s g = dict() if q < len(alice_only): g["q"] = alice_only_fc[q] if r < len(bob_only): g["r"] = bob_only_fc[r] if s < len(neither): g["s"] = neither_fc[s] if len(g.keys()) == 0: break to_increment = min(g, key=g.get) if to_increment == "q": q += 1 if to_increment == "r": r += 1 if to_increment == "s": s += 1 if p+q+r+s < M: break while p+q+r+s > M: # greedily decrement q,r,s g = dict() if 0 < q and p + q > K: g["q"] = alice_only_fc[q-1] if 0 < r and p + r > K: g["r"] = bob_only_fc[r-1] if 0 < s: g["s"] = neither_fc[s-1] if len(g.keys()) == 0: break to_decrement = max(g, key=g.get) if to_decrement == "q": q -= 1 if to_decrement == "r": r -= 1 if to_decrement == "s": s -= 1 if p+q+r+s > M: break score = both_prefix[p] + alice_only_prefix[q] + bob_only_prefix[r] + neither_prefix[s] if score < best_score: best_score = score best_pqrs = (p,q,r,s) p,q,r,s = best_pqrs ret_array = [] for i in range(p): ret_array += [both[i][1]] for i in range(q): ret_array += [alice_only[i][1]] for i in range(r): ret_array += [bob_only[i][1]] for i in range(s): ret_array += [neither[i][1]] return best_score, ret_array N, M, K = input().split(' ') N = int(N) K = int(K) M = int(M) alice_only = [] bob_only = [] both = [] neither = [] inorder = [] for i in range(1,N+1): t, a, b = input().split(' ') t = int(t) a = int(a) b = int(b) if a == 0 and b == 0: neither += [(t, i)] if a == 1 and b == 1: both += [(t, i)] if a == 1 and b == 0: alice_only += [(t, i)] if a == 0 and b == 1: bob_only += [(t, i)] inorder += [t] alice_only = sorted(alice_only) bob_only = sorted(bob_only) both = sorted(both) neither = sorted(neither) ret = ans(both, alice_only, bob_only, neither, M, K) if ret is None: print(-1) else: a, b = ret print(a) print(' '.join(map(str, b))) check = 0 for idx in b: check += inorder[idx-1] assert(a == check)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from copy import copy, deepcopy def read_int(): return int(input()) def read_arr(): return [int(x) for x in input().split(' ')] def read_mx(n): return [[int(num) for num in input().split(' ')] for _ in range(0, n)] def work(p_case): n, k = read_arr() mx = read_mx(n) # split b_only = [] a_only = [] both = [] for row in mx: cost = row[0] if row[1] and not row[2]: b_only.append(cost) elif row[2] and not row[1]: a_only.append(cost) elif row[1] and row[2]: both.append(cost) b_only.sort() a_only.sort() # merge ia, ib = 0, 0 while ia < len(a_only) and ib < len(b_only): both.append(a_only[ia]+b_only[ib]) ia += 1 ib += 1 both.sort() # print(both) sm, cnt = 0, 0 while cnt < k and cnt < len(both): sm += both[cnt] cnt += 1 if cnt == k: print(sm) else: print(-1) if __name__ == "__main__": work(0) # t = read_int() # cases # for case in range(0, t): # work(case+1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys input = sys.stdin.readline INF = 10 ** 18 n, m, k = map(int, input().split()) B = [tuple(map(int, input().split())) for _ in range(n)] GB = [] AB = [] BB = [] RB = [] for i, (t, a, b) in enumerate(B): if a and b: GB.append((t, i)) elif a: AB.append((t, i)) elif b: BB.append((t, i)) else: RB.append((t, i)) GB.sort() AB.sort() BB.sort() # TODO: already sorted CB = sorted((t1+t2, i1, i2) for (t1, i1), (t2, i2) in zip(AB, BB)) N = 1 while N <= 10 ** 4: N *= 2 T = [(0, 0)] * (2 * N) def comb(a, b): return (a[0] + b[0], a[1] + b[1]) def add_book(t, inc=1): i = t + N T[i] = comb(T[i], (t*inc, inc)) while i > 1: i //= 2 T[i] = comb(T[i*2], T[i*2+1]) def query(x): assert x >= 0 s = 0 i = 1 while x: ts, tc = T[i] if tc < x: return INF elif tc == x: s += ts break if i >= N: s += ts // tc * x break i *= 2 if T[i][1] < x: s += T[i][0] x -= T[i][1] i += 1 return s for t, _ in RB: add_book(t) for t, _ in AB: add_book(t) for t, _ in BB: add_book(t) gb_i = 0 gb_t = 0 while gb_i < min(len(GB), m): gb_t += GB[gb_i][0] gb_i += 1 for t, _ in GB[gb_i:]: add_book(t) cb_i = 0 cb_t = 0 while gb_i + cb_i < k and gb_i + 2 * (cb_i + 1) <= m and cb_i < len(CB): cb_t += CB[cb_i][0] add_book(AB[cb_i][0], -1) add_book(BB[cb_i][0], -1) cb_i += 1 if gb_i + cb_i < k: print(-1) sys.exit() best = (INF, -1, -1) while True: best = min(best, (gb_t + cb_t + query(m - 2 * cb_i - gb_i), gb_i, cb_i)) if not gb_i: break gb_i -= 1 gb_t -= GB[gb_i][0] add_book(GB[gb_i][0]) if gb_i + cb_i < k: if cb_i == len(CB): break cb_t += CB[cb_i][0] add_book(AB[cb_i][0], -1) add_book(BB[cb_i][0], -1) cb_i += 1 if gb_i + 2 * cb_i > m: break """ if not gb_i: break gb_i -= 1 gb_t -= GB[gb_i][0] add_book(GB[gb_i][0]) """ bs, bi, bj = best assert bs != INF ans = [] comps = 0 for t, i in GB[:bi]: ans.append(i+1) comps += t for t, i1, i2 in CB[:bj]: ans.append(i1+1) ans.append(i2+1) comps += t if bi + 2 * bj < m: rem = GB[bi:] + AB[bj:] + BB[bj:] + RB rem.sort() for t, i in rem[:m - bi - 2 * bj]: ans.append(i+1) comps += t assert comps == bs print(bs) print(*ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int i, n, k, alike, blike, t, total = 0; cin >> n >> k; vector<int> a, b, coll; for (i = 0; i < n; i++) { cin >> t >> alike >> blike; if (alike) { if (blike) coll.push_back(t); else a.push_back(t); } else if (blike) b.push_back(t); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); for (i = 0; i < min(a.size(), b.size()); i++) coll.push_back(a[i] + b[i]); if (k > coll.size()) cout << -1; else { sort(coll.begin(), coll.end()); for (i = 0; i < k; i++) total += coll[i]; cout << total; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
def main(): n, k = map(int, input().split()) alice_counter = k bob_counter = k total_time = 0 both = [] alice = [] bob = [] for _ in range(n): t, a, b = map(int, input().split()) if a and b: both.append(t) elif a: alice.append(t) elif b: bob.append(t) both = sorted(both) alice = sorted(alice) bob = sorted(bob) pointer = [0] * 3 while True: if bob_counter == 0 and alice_counter == 0: return total_time if pointer[1] >= len(alice) or pointer[2] >= len(bob): while bob_counter > 0 or alice_counter > 0: if pointer[0] >= len(both): return -1 total_time += both[pointer[0]] pointer[0] += 1 bob_counter -= 1 alice_counter -= 1 return total_time elif pointer[0] >= len(both): while bob_counter > 0 or alice_counter > 0: if pointer[1] >= len(alice) or pointer[2] >= len(bob): return -1 total_time += alice[pointer[1]] + bob[pointer[2]] pointer[1] += 1 pointer[2] += 1 bob_counter -= 1 alice_counter -= 1 return total_time else: if both[pointer[0]] > alice[pointer[1]] + bob[pointer[2]]: total_time += alice[pointer[1]] + bob[pointer[2]] pointer[1] += 1 pointer[2] += 1 else: total_time += both[pointer[0]] pointer[0] += 1 bob_counter -= 1 alice_counter -= 1 print(main())
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys input = lambda :sys.stdin.readline().rstrip() from heapq import heapify,heappop,heappush n,k = map(int,input().split()) a,b,c=[],[],[] for _ in range(n): t,e,d=map(int,input().split()) if e and d: a.append(t) elif e: b.append(t) elif d: c.append(t) fl=False if len(b)+len(a)>=k and len(c)+len(a)>=k: fl=True if not fl: print(-1) sys.exit(0) heapify(a) b.sort() c.sort() ans=0 for i in range(min(len(b),len(c))): heappush(a,b[i]+c[i]) while k: ans+=heappop(a) k-=1 print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct BookInfo { int t; int a; int b; int i; }; struct CombinedBook { int t; BookInfo b1; BookInfo b2; bool isCombined; }; int main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); int n, k; cin >> n >> k; vector<BookInfo> allBooks; int aliceLikes = 0; int bobLikes = 0; for (int i = 0; i < n; i++) { BookInfo info; cin >> info.t >> info.a >> info.b; info.i = i + 1; if (info.a) { aliceLikes++; } if (info.b) { bobLikes++; } allBooks.push_back(info); } if (aliceLikes < k || bobLikes < k) { cout << -1 << endl; return 0; } sort(allBooks.begin(), allBooks.end(), [](const BookInfo &a, const BookInfo &b) -> bool { return a.t < b.t; }); vector<BookInfo> aliceBooks; vector<BookInfo> bobBooks; vector<CombinedBook> combined; vector<BookInfo> other; for (BookInfo &book : allBooks) { if (book.a && book.b) { CombinedBook item; item.t = book.t; item.b1 = book; item.b2 = book; item.isCombined = false; combined.push_back(item); } else if (!book.a && !book.b) { other.push_back(book); } else if (book.a) { aliceBooks.push_back(book); } else if (book.b) { bobBooks.push_back(book); } } for (int i = 0; i < n; i++) { if (i < aliceBooks.size() && i < bobBooks.size()) { CombinedBook item; item.isCombined = true; item.b1 = aliceBooks[i]; item.b2 = bobBooks[i]; item.t = item.b1.t + item.b2.t; combined.push_back(item); } else if (i < aliceBooks.size()) { other.push_back(aliceBooks[i]); } else if (i < bobBooks.size()) { other.push_back(bobBooks[i]); } } if (combined.size() < k) { cout << -1 << endl; return 0; } sort(combined.begin(), combined.end(), [](const CombinedBook &a, const CombinedBook &b) -> bool { return a.t < b.t; }); vector<int> result; int sum = 0; int leftLikes = k; for (int i = 0; i < k; i++) { sum += combined[i].t; } cout << sum << endl; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long PI = 3.14159265358979; const long long INF = 1000000000000000000; bool sortbysec(pair<long long, long long> a, pair<long long, long long> b) { return (a.second < b.second); } long long powerMod(long long a, long long b, long long M) { long long res = 1; a = a % M; while (b) { if (b % 2) res = (res * a) % M; b /= 2; a = (a * a) % M; } return res; } long long power(long long a, long long b) { long long res = 1; while (b) { if (b % 2) res = (res * a); b /= 2; a = (a * a); } return res; } long long modInverse(long long n, long long M) { return powerMod(n, M - 2, M); } long long nCrModPFermat(long long n, long long r, long long M) { if (!r) return 1; long long fac[n + 1]; fac[0] = 1; for (long long i = 1; i < n + 1; i++) { fac[i] = fac[i - 1] * i % M; } return (fac[n] * modInverse(fac[r], M) % M * modInverse(fac[n - r], M) % M) % M; } long double logABaseB(long long a, long long b) { return (long double)((long double)log(a) / (long double)log(b)); } struct bookDet { long long t, a, b; }; bool comp(bookDet b1, bookDet b2) { return b1.t < b2.t; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(15); long long t = 1; while (t--) { long long n, k; cin >> n >> k; vector<bookDet> books; for (long long i = 0; i < n; i++) { books.emplace_back(bookDet()); cin >> books[i].t >> books[i].a >> books[i].b; } vector<bookDet> both, ali, bob, none; for (long long i = 0; i < n; i++) { if (books[i].a == 1 && books[i].b == 1) { both.emplace_back(books[i]); } else if (books[i].a == 1 && books[i].b == 0) { ali.emplace_back(books[i]); } if (books[i].a == 0 && books[i].b == 1) { bob.emplace_back(books[i]); } if (books[i].a == 0 && books[i].b == 0) { none.emplace_back(books[i]); } } sort(both.begin(), both.end(), comp); sort(none.begin(), none.end(), comp); sort(ali.begin(), ali.end(), comp); sort(bob.begin(), bob.end(), comp); long long ans = 0; auto bothP = both.begin(), aliP = ali.begin(), bobP = bob.begin(); while (k && aliP != ali.end() && bobP != bob.end() && bothP != both.end()) { if ((*aliP).t + (*bobP).t < (*bothP).t) { ans += ((*aliP).t + (*bobP).t); aliP++; bobP++; k--; } else { ans += (*bothP).t; bothP++; k--; } } if (!k) { cout << ans << endl; } else { if (aliP != ali.end() && bobP != bob.end()) { while (k && aliP != ali.end() && bobP != bob.end()) { ans += (*aliP).t; ans += (*bobP).t; aliP++; bobP++; k--; } if (k) { cout << -1 << endl; } } else { if (bothP == both.end() || both.end() - bothP < k) { cout << -1 << endl; } else { while (k) { ans += (*bothP).t; bothP++; k--; } } } if (!k) cout << ans << endl; } } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
from __future__ import print_function, division from sys import stdin, stdout rstr = lambda: stdin.readline().strip() rstrs = lambda: [str(x) for x in stdin.readline().split()] rint = lambda: int(stdin.readline()) rints = lambda: [int(x) for x in stdin.readline().split()] arr_2d = lambda n: [rints() for i in range(n)] out = [] n, k = rints() a = sorted([rints() for _ in range(n)], key=lambda x: x[0]) alice, bob, o_o, o_z, z_o, ans = 0, 0, [], [], [], 0 for i in range(n): if alice >= k and bob >= k: if a[i][1] == a[i][2] == 1: o_o.append(a[i][0]) continue if a[i][1] and a[i][2]: ans += a[i][0] alice += 1 bob += 1 elif a[i][1] and alice < k: alice += 1 ans += a[i][0] o_z.append(a[i][0]) elif a[i][2] and bob < k: bob += 1 ans += a[i][0] z_o.append(a[i][0]) if alice < k or bob < k: print(-1) else: if len(o_z) > len(z_o): for i in range(len(o_z) - len(z_o)): ans -= o_z.pop() if len(z_o) > len(o_z): for i in range(len(z_o) - len(o_z)): ans -= z_o.pop() o_o.reverse() while o_z and z_o and o_o: if o_z[-1] + z_o[-1] <= o_o[-1]: break ans -= o_z.pop() + z_o.pop() ans += o_o.pop() print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Q3 { public static void main(String[] args) { InputReader in = new InputReader(true); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(), k = in.nextInt(); long ans = 0; ArrayList<Integer> l1=new ArrayList<>(); ArrayList<Integer> l2=new ArrayList<>(); ArrayList<Integer> both=new ArrayList<>(); for (int i = 0; i < n; i++) { int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(); if (b + c == 2) both.add(a); else if (c == 1) l2.add(a); else if (b == 1) l1.add(a); } Collections.sort(l1); Collections.sort(l2); for(int i=0;i<Math.min(l1.size(),l2.size());i++){ both.add(l1.get(i)+l2.get(i)); } Collections.sort(both); for(int i =0;i<Math.min(both.size(),k);i++){ ans+=both.get(i); } if (both.size()>=k) out.println(ans); else out.println(-1); out.close(); } static class InputReader { int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } InputStream is; public InputReader(boolean onlineJudge) { is = System.in; } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); int c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); long c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } int[] uniq(int[] arr) { Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] uniq(long[] arr) { Arrays.sort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] reverse(int[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } long[] reverse(long[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } int[] compress(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } long[] compress(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } void deepFillInt(Object array, int val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof int[]) { int[] intArray = (int[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillInt(obj, val); } } } void deepFillLong(Object array, long val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof long[]) { long[] intArray = (long[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillLong(obj, 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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; vector<long long> a; vector<long long> b; vector<long long> same; for (long long i = 0; i < n; i++) { long long t, x, y; cin >> t >> x >> y; if (x == 1 && y == 1) same.push_back(t); else if (x == 1) a.push_back(t); else if (y == 1) b.push_back(t); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(same.begin(), same.end()); long long a_n = (long long)a.size(); long long b_n = (long long)b.size(); long long same_n = (long long)same.size(); if ((same_n + a_n < k) || (same_n + b_n < k)) { cout << -1 << endl; } else { long long cnt = 0, ans = 0, i = 0, j = 0; while (cnt < k) { if (i < same_n && j < a_n && j < b_n && same[i] < (a[j] + b[j])) { cnt++; ans += same[i]; i++; } else if (j < a_n && j < b_n) { cnt++; ans += (a[j] + b[j]); j++; } else break; } if (cnt < k) { if (i < same_n) { while (i < same_n && cnt < k) { cnt++; ans += same[i]; i++; } } else { while (j < a_n && j < b_n && cnt < k) { cnt++; ans += (a[j] + b[j]); j++; } } } cout << ans << endl; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
l=input().split() n=int(l[0]) k=int(l[1]) l=[] arrboth=[] arralice=[] arrbob=[] for i in range(n): lo=input().split() x=int(lo[0]) u=int(lo[1]) v=int(lo[2]) if(u==1 and v==1): arrboth.append(x) elif(u==1): arralice.append(x) elif(v==1): arrbob.append(x) arrbob.sort() arralice.sort() arrboth.sort() pref1=[0 for i in range(len(arrboth)+1)] pref2=[0 for i in range(len(arralice)+1)] pref3=[0 for i in range(len(arrbob)+1)] sumi=0 for i in range(1,len(arrboth)+1): sumi+=arrboth[i-1] pref1[i]=sumi sumi=0 for i in range(1,len(arrbob)+1): sumi+=arrbob[i-1] pref3[i]=sumi sumi=0 for i in range(1,len(arralice)+1): sumi+=arralice[i-1] pref2[i]=sumi z=[] for i in range(min(len(arrboth),k)+1): if(k-i>len(arrbob) or k-i>len(arralice)): continue z.append(pref1[i]+pref2[k-i]+pref3[k-i]) if(z==[]): print(-1) else: print(min(z))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void Output(const T& v, bool print_new_line = true) { bool first = true; for (auto element : v) { if (!first) cout << " "; else first = false; cout << element; } if (print_new_line) cout << endl; } template <class T> void Input(vector<T>& v, int n) { v.resize(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } } template <class T> vector<int> Query(const T& c, int answer_counts) { cout << "? " << c.size(); for (auto it = c.begin(); it != c.end(); ++it) { cout << " " << (*it); } cout << endl; cout.flush(); vector<int> s(answer_counts); for (int i = 0; i < answer_counts; ++i) { cin >> s[i]; } return s; } inline int get(int k, const vector<int>& v) { if (k <= 0) return 0; return v[k - 1]; } struct Book { int t, a, b, id; }; struct compare { bool operator()(const Book& a, const Book& b) { return a.t < b.t || (a.t == b.t && a.id < b.id); } }; int init(int k, int m, int both, vector<vector<list<Book>>> bs, set<Book, compare>& a, set<Book, compare>& b, set<Book, compare>& rest) { a.clear(); b.clear(); rest.clear(); int total = 0; while ((int)a.size() < k - both && !bs[1][0].empty()) { total += bs[1][0].front().t; a.insert(bs[1][0].front()); bs[1][0].pop_front(); } while ((int)b.size() < k - both && !bs[0][1].empty()) { total += bs[0][1].front().t; b.insert(bs[0][1].front()); bs[0][1].pop_front(); } std::copy(bs[1][0].begin(), bs[1][0].end(), inserter(rest, rest.begin())); std::copy(bs[0][1].begin(), bs[0][1].end(), inserter(rest, rest.begin())); std::copy(bs[0][0].begin(), bs[0][0].end(), inserter(rest, rest.begin())); while (rest.size() > 0 && (int)(rest.size() + a.size() + b.size()) > m - both) { auto it = rest.end(); --it; rest.erase(it); } return total + std::accumulate(rest.begin(), rest.end(), 0, [](int v, const Book& b) { return v + b.t; }); } int main() { int n, m, k; cin >> n >> m >> k; vector<Book> books(n); for (int i = 0; i < n; ++i) { cin >> books[i].t >> books[i].a >> books[i].b; books[i].id = i + 1; } sort(books.begin(), books.end(), [](const Book& a, const Book& b) { return a.t < b.t; }); vector<vector<list<Book>>> bs(2); bs[0].resize(2); bs[1].resize(2); for (int i = 0; i < n; ++i) { bs[books[i].a][books[i].b].push_back(books[i]); } int best = 0x7FFFFFFF; int best_i = -1; bool inited = false; int idx[2][2] = {0}; set<Book, compare> both, a, b, rest; int total = 0; auto iter = bs[1][1].begin(); set<Book, compare> rest_no; for (int i = 0; i <= bs[1][1].size() && i <= m; ++i) { if (!inited) { total = init(k, m, i, bs, a, b, rest); for (auto book : bs[0][0]) { if (rest.find(book) == rest.end()) { rest_no.insert(book); } } inited = true; } while ((int)a.size() > k - i && !a.empty()) { auto it = a.end(); --it; rest.insert(*it); a.erase(it); } while ((int)b.size() > k - i && !b.empty()) { auto it = b.end(); --it; rest.insert(*it); b.erase(it); } while ((int)(rest.size() + a.size() + b.size()) > m - i && !rest.empty()) { auto it = rest.end(); --it; total -= it->t; rest_no.insert(*it); rest.erase(it); } while (!rest_no.empty() && !rest.empty() && rest.rbegin()->t > rest_no.begin()->t) { auto it = rest.end(); --it; total -= it->t; rest.erase(it); rest.insert(*rest_no.begin()); total += rest_no.begin()->t; rest_no.erase(rest_no.begin()); } if ((int)a.size() >= k - i && (int)b.size() >= k - i && (int)(a.size() + b.size() + rest.size()) == m - i) { if (total < best) { best = total; best_i = i; } } if (iter != bs[1][1].end()) { total += iter->t; ++iter; } } if (best_i == -1) { cout << -1 << endl; return 0; } cout << best << endl; init(k, m, best_i, bs, a, b, rest); iter = bs[1][1].begin(); while (best_i-- > 0) { cout << iter->id << " "; ++iter; } std::for_each(a.begin(), a.end(), [](const Book& v) { cout << v.id << " "; }); std::for_each(b.begin(), b.end(), [](const Book& v) { cout << v.id << " "; }); std::for_each(rest.begin(), rest.end(), [](const Book& v) { cout << v.id << " "; }); cout << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; deque<long long> v11, v10, v01; signed main() { ios::sync_with_stdio(0); cin.tie(0); long long n, k; cin >> n >> k; for (long long i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; if (b == 1 && c == 1) v11.emplace_back(a); else if (b == 1 && c == 0) v10.emplace_back(a); else if (b == 0 && c == 1) v01.emplace_back(a); } sort(v11.begin(), v11.end()); sort(v10.begin(), v10.end()); sort(v01.begin(), v01.end()); priority_queue<long long> pq; long long ans = 0; long long nk = k; for (long long i = 0; i < min(k, (long long)min(v10.size(), v01.size())); i++) { pq.emplace(v10[i] + v01[i]); ans += v10[i] + v01[i]; nk--; } while (v11.size() && nk) { nk--; ans += v11[0]; v11.pop_front(); } if (nk) { cout << -1 << endl; return 0; } while (pq.size() && v11.size()) { if (v11[0] < pq.top()) { ans -= pq.top(); ans += v11[0]; v11.pop_front(); pq.pop(); } else break; } cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.*; import java.io.*; public class e2 { public static void main(String[] Args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int toRead = sc.nextInt(); int mustLike = sc.nextInt(); long ans = 987654321987654321l; int bothNum = 0; int aliceNum = 0; int bobNum = 0; int noneNum = 0; ArrayList<Book> alice = new ArrayList<Book>(); ArrayList<Book> bob = new ArrayList<Book>(); ArrayList<Book> both = new ArrayList<Book>(); ArrayList<Book> none = new ArrayList<Book>(); for (int i = 1; i <= n; i++) { int time = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if (a + b == 2) both.add(new Book(i, time)); else if (a == 1) alice.add(new Book(i, time)); else if (b == 1) bob.add(new Book(i, time)); else none.add(new Book(i, time)); } Collections.sort(alice); Collections.sort(bob); Collections.sort(none); Collections.sort(both); int bothCur, aliceCur, bobCur, noneCur; long curTime = 0; int i; for (bothCur = 0; bothCur < toRead && bothCur < both.size(); bothCur++) curTime += both.get(bothCur).time; for (aliceCur = 0; aliceCur + bothCur < mustLike && aliceCur < alice.size(); aliceCur++) curTime += alice.get(aliceCur).time; for (bobCur = 0; bobCur + bothCur < mustLike && bobCur < bob.size(); bobCur++) curTime += bob.get(bobCur).time; noneCur = 0; if (aliceCur + bothCur < mustLike || bobCur + bothCur < mustLike) { System.out.println(-1); return; } while (bothCur + aliceCur + bobCur + noneCur < toRead) { if (aliceCur < alice.size() && (bobCur == bob.size() || bob.get(bobCur).time >= alice.get(aliceCur).time) && (noneCur == none.size() || none.get(noneCur).time >= alice.get(aliceCur).time)) { curTime += alice.get(aliceCur++).time; } else if (bobCur < bob.size() && (noneCur == none.size() || none.get(noneCur).time >= bob.get(bobCur).time)) { curTime += bob.get(bobCur++).time; } else { curTime += none.get(noneCur++).time; } } if (curTime < ans) { ans = curTime; bothNum = bothCur; aliceNum = aliceCur; bobNum = bobCur; noneNum = noneCur; } while (bothCur > 0) { curTime -= both.get(--bothCur).time; while (aliceCur < alice.size() && bothCur + aliceCur < mustLike) { curTime += alice.get(aliceCur++).time; } while (bobCur < bob.size() && bothCur + bobCur < mustLike) { curTime += bob.get(bobCur++).time; } while (bothCur + bobCur + aliceCur + noneCur < toRead) { if (aliceCur < alice.size() && (bobCur == bob.size() || bob.get(bobCur).time >= alice.get(aliceCur).time) && (noneCur == none.size() || none.get(noneCur).time >= alice.get(aliceCur).time)) { curTime += alice.get(aliceCur++).time; } else if (bobCur < bob.size() && (noneCur == none.size() || none.get(noneCur).time >= bob.get(bobCur).time)) { curTime += bob.get(bobCur++).time; } else if (noneCur < none.size()){ curTime += none.get(noneCur++).time; } else { break; } } while (bothCur + bobCur + aliceCur + noneCur > toRead) { if (aliceCur != 0 && aliceCur + bothCur > mustLike && (bobCur == 0 || bob.get(bobCur - 1).time <= alice.get(aliceCur - 1).time || bobCur + bothCur == mustLike) && (noneCur == 0 || none.get(noneCur - 1).time <= alice.get(aliceCur - 1).time)) { curTime -= alice.get(--aliceCur).time; } else if (bobCur != 0 && bobCur + bothCur > mustLike && (noneCur == 0 || none.get(noneCur - 1).time >= bob.get(bobCur - 1).time)) { curTime -= bob.get(--bobCur).time; } else if (noneCur != 0) { curTime -= none.get(--noneCur).time; } else { break; } } if (bothCur + bobCur + aliceCur + noneCur == toRead && curTime < ans && bobCur + bothCur >= mustLike && aliceCur + bothCur >= mustLike) { ans = curTime; bothNum = bothCur; aliceNum = aliceCur; bobNum = bobCur; noneNum = noneCur; } } PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(System.out))); if (bothNum + aliceNum + bobNum + noneNum != toRead) { out.println(-1); } else { out.println(ans); for (i = 0; i < bothNum; i++) out.print(both.get(i).index + " "); for (i = 0; i < aliceNum; i++) out.print(alice.get(i).index + " "); for (i = 0; i < bobNum; i++) out.print(bob.get(i).index + " "); for (i = 0; i < noneNum; i++) out.print(none.get(i).index + " "); out.println(); } out.close(); } public static class Book implements Comparable<Book> { int index, time; Book(int i, int t) { index = i; time = t; } public int compareTo(Book o) { return 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" ] }
CORRECT
python3
import sys import math import heapq import collections from collections import OrderedDict def inputnum(): return(int(input())) def inputnums(): return(map(int,input().split())) def inputlist(): return(list(map(int,input().split()))) def inputstring(): return([x for x in input()]) def inputmatrixchar(rows): arr2d = [[j for j in input().strip()] for i in range(rows)] return arr2d def inputmatrixint(rows): arr2d = [] for _ in range(rows): arr2d.append([int(i) for i in input().split()]) return arr2d n, k = inputnums() mins = [ [ [] for i in range(2) ] for j in range(2) ] for i in range(n): t, a, b = inputnums() mins[a][b].append(t) mins[1][0].sort() mins[0][1].sort() x = len(mins[1][0]) y = len(mins[0][1]) for i in range(min(x, y)): mins[1][1].append(mins[1][0][i] + mins[0][1][i]) mins[1][1].sort() ans = 0 if len(mins[1][1]) < k: ans = -1 if ans != -1: for i in range(k): ans += mins[1][1][i] print (ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from collections import defaultdict n,k=list(map(int,input().split())) d=defaultdict(list) for i in range(n): t,a,b=list(map(int,input().split())) x=int(str(a)+str(b),2) d[x].append(t) if len(d[2])+len(d[3])<k or len(d[1])+len(d[3])<k: print(-1) else: d[1].sort() d[2].sort() d[3].sort() i,j,l=0,0,0 c=0 s=0 while c<k: if l<len(d[3]): if i<len(d[1]): if(j<len(d[2])): if d[1][i]+d[2][j]<d[3][l]: s+=d[1][i]+d[2][j] i+=1 j+=1 else: s+=d[3][l] l+=1 else: s+=d[3][l] l+=1 else: s+=d[3][l] l+=1 else: s+=d[1][i]+d[2][j] i+=1 j+=1 c+=1 print(s)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) 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 print(ans) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import io,os input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import sys def solve(n,k,TAB): A,B,AB=[],[],[] cnt_a,cnt_b=0,0 for t,a,b in TAB: cnt_a+=a; cnt_b+=b if a*b==1: AB.append(t) elif a==1: A.append(t) elif b==1: B.append(t) if cnt_a<k or cnt_b<k: return -1 AB.sort(); A.sort(); B.sort() AB=AB[:min(k,len(AB))] l=len(AB) remain=k-l ans=sum(AB)+sum(A[:remain])+sum(B[:remain]) tmp=ans for i in range(min(l,len(A)-remain,len(B)-remain)): tmp-=AB.pop() tmp+=A[remain+i]+B[remain+i] ans=min(ans,tmp) return ans def main(): n,k=map(int,input().split()) TAB=[list(map(int,input().split())) for _ in range(n)] ans=solve(n,k,TAB) sys.stdout.write(str(ans)+'\n') if __name__=='__main__': main()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; public class tr1 { static PrintWriter out; static StringBuilder sb; static int n, m, id, max; static long mod = 998244353; static Boolean[][] memo; static String s; static int[][] ad; static long inf = Long.MAX_VALUE; static int[] color; static ArrayList<Integer> o; static char[][] g; static boolean[] vis, vis1; static boolean f; static int[] ar, a; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> all = new ArrayList<>(); ArrayList<Integer> left = new ArrayList<>(); ArrayList<Integer> right = new ArrayList<>(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); if (l == 1 && r == 1) all.add(a); else if (l == 1) left.add(a); else if(r==1) right.add(a); } Collections.sort(right); Collections.sort(left); long ans = 0; if (all.size() + Math.min(right.size(), left.size()) < k) { System.out.println(-1); return; } for (int i = 0; i < Math.min(right.size(), left.size()); i++) { all.add(right.get(i) + left.get(i)); } Collections.sort(all); for (int i = 0; i < k; i++) ans += all.get(i); System.out.println(ans); out.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextArrInt(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextArrLong(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int stack_A[MAXN]; int stack_B[MAXN]; int stack_AB[MAXN]; int main() { ios ::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; int a_cnt = 0; int b_cnt = 0; int ab_cnt = 0; int x, y, z; for (int i = 0; i < n; ++i) { cin >> x >> y >> z; if (y & z) { stack_AB[ab_cnt] = x; ab_cnt += 1; } else if (y) { stack_A[a_cnt] = x; a_cnt += 1; } else if (z) { stack_B[b_cnt] = x; b_cnt += 1; } } if (a_cnt + ab_cnt < k || b_cnt + ab_cnt < k) { cout << -1 << endl; return 0; } sort(stack_A, stack_A + a_cnt); sort(stack_B, stack_B + b_cnt); sort(stack_AB, stack_AB + ab_cnt); int a = 0; int b = 0; int ab = 0; int a_k = 0; int b_k = 0; int ans = 0; while (!(a_k >= k && b_k >= k)) { if (a < a_cnt && b < b_cnt && ab < ab_cnt) { if (a_k < k && b_k < k) { if (stack_AB[ab] <= stack_A[a] + stack_B[b]) { ans += stack_AB[ab]; ab += 1; } else { ans += stack_A[a] + stack_B[b]; a += 1; b += 1; } a_k += 1; b_k += 1; } else if (a_k < k) { if (stack_A[a] <= stack_AB[ab]) { ans += stack_A[a]; a += 1; a_k += 1; } else { ans += stack_AB[ab]; ab += 1; a_k += 1; b_k += 1; } } else if (b_k < k) { if (stack_B[b] <= stack_AB[ab]) { ans += stack_B[b]; b += 1; b_k += 1; } else { ans += stack_AB[ab]; ab += 1; a_k += 1; b_k += 1; } } } else if (a < a_cnt && b < b_cnt) { if (a_k < k && b_k < k) { ans += stack_A[a] + stack_B[b]; a += 1; b += 1; a_k += 1; b_k += 1; } else if (a_k < k) { ans += stack_A[a]; a += 1; a_k += 1; } else if (b_k < k) { ans += stack_B[b]; b += 1; b_k += 1; } } else if (a < a_cnt && ab < ab_cnt) { if (a_k < k && b_k >= k && stack_A[a] <= stack_AB[ab]) { ans += stack_A[a]; a += 1; a_k += 1; } else { ans += stack_AB[ab]; ab += 1; a_k += 1; b_k += 1; } } else if (b < b_cnt && ab < ab_cnt) { if (b_k < k && a_k >= k && stack_B[b] <= stack_AB[ab]) { ans += stack_B[b]; b += 1; b_k += 1; } else { ans += stack_AB[ab]; ab += 1; a_k += 1; b_k += 1; } } else if (ab < ab_cnt) { ans += stack_AB[ab]; ab += 1; a_k += 1; b_k += 1; } else if (a < a_cnt) { ans += stack_A[a]; a += 1; a_k += 1; } else if (b < b_cnt) { ans += stack_B[b]; b += 1; b_k += 1; } } cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.util.*; public class Solution1374E1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer>[] arrayLists = new ArrayList[4]; for (int i = 0; i < 4; ++i) { arrayLists[i] = new ArrayList<>(); } int n = scanner.nextInt(); int k = scanner.nextInt(); for (int i = 0; i < n; ++i) { int t = scanner.nextInt(); int a = scanner.nextInt(); int b = scanner.nextInt(); arrayLists[a * 2 + b].add(t); } ArrayList<Integer>[] prefixSum = new ArrayList[4]; for (int i = 0; i < 4; ++i) { Collections.sort(arrayLists[i]); prefixSum[i] = new ArrayList<>(); prefixSum[i].add(0); List<Integer> cur = arrayLists[i]; for (Integer single : cur) { prefixSum[i].add(single + prefixSum[i].get(prefixSum[i].size() - 1)); } } int ans = Integer.MAX_VALUE; for (int cnt = 0; cnt < Math.min(k + 1, prefixSum[3].size()); ++cnt) { if ((k - cnt) < prefixSum[1].size() && (k - cnt) < prefixSum[2].size()) ans = Math.min(ans, prefixSum[3].get(cnt) + prefixSum[1].get(k - cnt) + prefixSum[2].get(k - cnt)); } if (ans == Integer.MAX_VALUE) { ans = -1; } System.out.println(ans); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
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.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; 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; FastReader in = new FastReader(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, FastReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); List<Integer> all = new ArrayList<>(); List<Integer> alist = new ArrayList<>(); List<Integer> blist = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); if (a + b == 2) { all.add(t); } else if (a == 1) { alist.add(t); } else if (b == 1) { blist.add(t); } } all.sort(Comparator.naturalOrder()); alist.sort(Comparator.naturalOrder()); blist.sort(Comparator.naturalOrder()); int[] aSum = new int[alist.size() + 1]; int[] bSum = new int[blist.size() + 1]; int[] allSum = new int[all.size() + 1]; for (int i = 1; i <= all.size(); i++) { allSum[i] = allSum[i - 1] + all.get(i - 1); } for (int i = 1; i <= alist.size(); i++) { aSum[i] = aSum[i - 1] + alist.get(i - 1); } for (int i = 1; i <= blist.size(); i++) { bSum[i] = bSum[i - 1] + blist.get(i - 1); } if (all.size() + alist.size() < k || all.size() + blist.size() < k) { out.println(-1); return; } long ans = Long.MAX_VALUE; for (int i = 0; i <= all.size(); i++) { if (i + alist.size() < k || i + blist.size() < k) { continue; } if (i >= k) { ans = Math.min(ans, allSum[i]); break; } ans = Math.min(ans, allSum[i] + aSum[k - i] + bSum[k - i]); } out.println(ans); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() { while (st == null || (!st.hasMoreElements())) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } 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" ] }
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) gb=0 while((ak>0 or bk>0) and (i<l1 or j<l2 or k<l3)): if(gb>n): break 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 else: rr+=bob[k]+al[j] k+=1 j+=1 ak-=1 bk-=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 gb+=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" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int maxn = 1e6 + 10; const double eps = 1e-9; const long long inf = 1e18; int T; int n, m, k; vector<long long> vec[5]; long long a, b, t; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%lld%lld%lld", &t, &a, &b); if (a && b) vec[1].push_back(t); else if (a && !b) vec[2].push_back(t); else if (!a && b) vec[3].push_back(t); else vec[4].push_back(t); } for (int i = 1; i <= 4; i++) sort(vec[i].begin(), vec[i].end()); int flag = 1; a = b = 0; int l1 = vec[1].size(), l2 = min(vec[2].size(), vec[3].size()); long long ans = 0; for (int i = 1; i <= k; i++) { if (l1 == a && l2 == b) { flag = 0; break; } if (l1 == a || (l2 != b && vec[1][a] >= vec[2][b] + vec[3][b])) { ans += vec[2][b] + vec[3][b]; b++; } else { ans += vec[1][a]; a++; } } if (flag) printf("%lld\n", ans); else printf("-1\n"); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int,input().split()) c=[] d=[] e=[] for i in range(n): t,a,b=map(int,input().split()) if a==1 and b==0: c.append(t) elif b==1 and a==0: d.append(t) elif a==1 and b==1: e.append(t) if (len(c)+len(e))<k or (len(d)+len(e))<k: print(-1) else: c=sorted(c) d=sorted(d) e=sorted(e) l1=len(c) l2=len(d) l3=len(e) s=0 i1=0 i2=0 i3=0 while((i1+i3)<k or (i2+i3)<k): if i3<l3: if (i2 + i3) >= k and i1 < l1: if e[i3] <= c[i1]: s += e[i3] i3 += 1 else: s += c[i1] i1 += 1 elif (i1 + i3) >= k and i2 < l2: if e[i3] <= d[i2]: s += e[i3] i3 += 1 else: s += d[i2] i2 += 1 elif i1<l1 and i2<l2: if e[i3]<=(c[i1]+d[i2]): s+=e[i3] i3+=1 else: s+=(c[i1]+d[i2]) i1+=1 i2+=1 else: s+=e[i3] i3+=1 else: s+=(c[i1]+d[i2]) i1+=1 i2+=1 print(s)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> bool chmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool chmin(T &m, const T q) { if (q < m) { m = q; return true; } else return false; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct deb { ~deb() { cerr << endl; } template <class c> typename enable_if<sizeof dud<c>(0) != 1, deb &>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof dud<c>(0) == 1, deb &>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class c, class b> deb &operator<<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template <class c> deb &operator<<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } }; void err(istream_iterator<string> it) { return; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } void solve() { lint n, k; cin >> n >> k; vector<lint> time(n); vector<lint> alice(n); vector<lint> bob(n); priority_queue<int, vector<int>, greater<int>> abra; priority_queue<int, vector<int>, greater<int>> cadabra; priority_queue<int, vector<int>, greater<int>> mixed; for (int i = (0), i_end_ = (n); i < i_end_; i++) { lint t, a, b; cin >> t >> a >> b; time[i] = t; alice[i] = a; bob[i] = b; if (a == 1 && b == 1) { mixed.push(t); } else if (a == 1) { abra.push(t); } else if (b == 1) { cadabra.push(t); } } lint ret = 0; lint cc = 2 * k; lint s1 = accumulate(begin(alice), end(alice), 0LL); lint s2 = accumulate(begin(bob), end(bob), 0LL); if (s1 <= k - 1 || s2 <= k - 1) { cout << -1 << '\n'; return; } while (cc) { bool ok = 0; int f = 0; lint t = INT_MAX; if (mixed.size() > 0) { t = mixed.top(); ok = 1; } lint t1 = INT_MAX; lint t2 = INT_MAX; if (abra.size()) { t1 = abra.top(); ++f; } if (cadabra.size()) { t2 = cadabra.top(); ++f; } if (!ok && f <= 1) break; if (t <= t1 + t2) { ret += t; mixed.pop(); } else { ret += t1 + t2; abra.pop(); cadabra.pop(); } cc -= 2; } while (cc) { bool ok = 0; lint t1 = INT_MAX; if (abra.size()) { t1 = abra.top(); ok = 1; } lint t2 = INT_MAX; if (cadabra.size()) { t2 = cadabra.top(); ok = 1; } assert(ok == true); if (t1 <= t2) { ret += t1; abra.pop(); } else { ret += t2; cadabra.pop(); } cc -= 1; } cout << ret << '\n'; } int main() { int T = 1; for (int tt = 1; tt <= T; ++tt) { solve(); } return 0; } lint pow_mod(lint base, lint expo) { lint ret = 1; for (; expo;) { if (expo & 1) ret = (ret * base) % 1000000007; expo = (expo >> 1); base = (base * base) % 1000000007; } return ret; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k, t, a, b; long long ans = 0; priority_queue<int, vector<int>, greater<int>> book[3]; scanf("%d%d", &n, &k); while (n--) { scanf("%d%d%d", &t, &a, &b); if (a && b) book[0].push(t); else if (a) book[1].push(t); else if (b) book[2].push(t); } if (book[0].size() + book[1].size() < k || book[0].size() + book[2].size() < k) printf("-1\n"); else { while (!book[0].empty() && k--) if (book[1].empty() || book[2].empty() || book[0].top() <= book[1].top() + book[2].top()) { ans += book[0].top(); book[0].pop(); } else { ans += book[1].top() + book[2].top(); book[1].pop(); book[2].pop(); } while ((k--) > 0) { ans += book[1].top() + book[2].top(); book[1].pop(); book[2].pop(); } printf("%lld\n", ans); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n,k=map(int, input().split()) alice=[] bob=[] both=[] counta=0 countb=0 for i in range(0,n): l1=list(map(int, input().split())) if l1[1]==1 and l1[2]==1: counta+=1 countb+=1 both.append(l1) elif l1[1]==1: counta+=1 alice.append(l1) elif l1[2]==1: countb+=1 bob.append(l1) if counta<k or countb <k: print (-1) else: t=0 alice.sort() bob.sort() both.sort() a=0 b=0 for i in range(1,k+1): if b>min(len(alice), len(bob))-1: for j in range(a,a+k-i+1): t=t+both[j][0] break if a>len(both)-1: for j in range(b,b+k-i+1): t=t+alice[j][0]+bob[j][0] break if both[a][0]<=alice[b][0]+bob[b][0]: t=t+both[a][0] a=a+1 else: t=t+alice[b][0]+bob[b][0] b=b+1 print (t)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int kMaxN = 200001; struct E { int i, t; bool operator<(const E &e) const { return t < e.t; } bool operator>(const E &e) const { return t > e.t; } } e[4][kMaxN]; bool s[kMaxN]; int c[4]; int n, m, k, x, a, b, ans, sum, tot, p; priority_queue<E> bh; priority_queue<E, vector<E>, greater<E> > sh; void Make(int st) { fill_n(s, n + 1, 0); bh = priority_queue<E>(); sh = priority_queue<E, vector<E>, greater<E> >(); sum = 0, tot = 0; for (int i = 1; i <= st; i++, tot++) { sum += e[3][i].t; s[e[3][i].i] = 1; } for (int i = 1; i <= k - st; i++, tot += 2) { sum += e[1][i].t + e[2][i].t; s[e[1][i].i] = s[e[2][i].i] = 1; } for (int i = 1; i <= c[0]; i++) { sh.push(e[0][i]); } for (int j = 1; j <= 2; j++) { for (int i = max(1, k - st + 1); i <= c[j]; i++) { sh.push(e[j][i]); } } for (; tot < m; tot++) { sum += sh.top().t; s[sh.top().i] = 1; bh.push(sh.top()); sh.pop(); } ans = sum; p = st; } bool Find() { int st = max(k - min(c[1], c[2]), 0); st = max(st, m - c[0] - c[1] - c[2]); st = max(st, 2 * k - m); if (st > c[3] || st > m) { return 0; } Make(st); for (int i = st; i < min(c[3], m); i++) { sum += e[3][i + 1].t; s[e[3][i + 1].i] = 1; tot++; for (int j = 1; j <= 2; j++) { if (i < k) { sum -= e[j][k - i].t; s[e[j][k - i].i] = 0; sh.push(e[j][k - i]); tot--; } } while (!bh.empty() && !sh.empty() && bh.top() > sh.top()) { sum += sh.top().t - bh.top().t; s[sh.top().i] = 1; s[bh.top().i] = 0; sh.push(bh.top()); bh.push(sh.top()); sh.pop(); bh.pop(); } for (; tot < m; tot++) { sum += sh.top().t; s[sh.top().i] = 1; bh.push(sh.top()); sh.pop(); } for (; tot > m; tot--) { sum -= bh.top().t; s[bh.top().i] = 0; sh.push(bh.top()); bh.pop(); } if (sum < ans) { ans = sum; p = i + 1; } } return 1; } int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { cin >> x >> a >> b; a |= b * 2; e[a][++c[a]] = {i, x}; } for (int i = 0; i <= 3; i++) { sort(e[i] + 1, e[i] + 1 + c[i]); } if (Find()) { cout << ans << endl; Make(p); for (int i = 1; i <= n; i++) { if (s[i]) { cout << i << " "; } } } else { cout << -1; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
from collections import defaultdict, deque rr = lambda: input() rri = lambda: int(input()) rrm = lambda: list(map(int, input().split())) INF=float('inf') def solve(N,K,B): groups = defaultdict(list) # alice, bob, both books for time,alike,blike in B: if alike and blike: groups['c'].append(time) elif alike: groups['a'].append(time) elif blike: groups['b'].append(time) for g in groups: groups[g] = sorted(groups[g], reverse=True) a,b= 0,0 amt = 0 while groups['a'] or groups['b'] or groups['c']: if not groups['a'] or not groups['b']: if not groups['c']: break amt += groups['c'].pop() a+=1 b+=1 elif not groups['c'] or groups['a'][-1] + groups['b'][-1] < groups['c'][-1]: amt += groups['a'].pop() amt += groups['b'].pop() a+=1 b+=1 else: amt += groups['c'].pop() a+=1 b+=1 if a == K and b == K: break if a < K or b < K: return -1 return amt #print(soloa, solob) if aread < K or bread < K: return -1 return totaltime + atotal + btotal n,k = rrm() books = [] # tuples (time, alice likes it, bob likes it) for _ in range(n): time,a,b=rrm() books.append((time,a,b)) print(solve(n,k,books))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k, t, a, b; cin >> n >> k; vector<long long int> time, alice, bob, both; for (long long 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); } } if (alice.size() + both.size() < k || bob.size() + both.size() < k) { cout << "-1\n"; return 0; } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(both.begin(), both.end()); long long int i_a = 0, i_b = 0, idx = 0, cost = 0; while (true) { if (i_a < alice.size() && i_b < bob.size()) { if (idx < both.size()) { if (alice[i_a] + bob[i_b] <= both[idx]) { cost += (alice[i_a] + bob[i_b]); i_a++; i_b++; } else { cost += both[idx]; idx++; } } else { cost += (alice[i_a] + bob[i_b]); i_a++; i_b++; } } else { cost += both[idx]; idx++; } if ((i_a == k - idx) && (i_b == k - idx)) { break; } } cout << cost; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python2
input = raw_input range = xrange import sys inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 seg = [0]*200000 def offset(x): return x + 100000 def encode(x, y): return x*200002 + y def decode(x): return x//200002, x%200002 def upd(node, L, R, pos, val): while L < R: seg[node] += val seg[offset(node)] += val*pos if L+1 == R: break M = (L+R)//2 node <<= 1 if pos < M: R = M else: L = M node += 1 def query(node, L, R, k): if k == 0: return [0, 0] if seg[node] < k: return seg[offset(node)], seg[node] if L+1 == R: return [L*k, k] M = (L+R)//2 leftval, leftct = query(node<<1, L, M, k) rightval, rightct = query(node<<1 | 1, M, R, k-leftct) return leftval+rightval, leftct+rightct # n, m, k = map(int, input().split()) n, m, k = inp[ii:ii+3]; ii += 3 A, B, both, neither = [], [], [], [] for i in range(n): # t, a, b = map(int, input().split()) t, a, b = inp[ii:ii+3]; ii += 3 if a == 0 and b == 0: neither.append(encode(t, i+1)) if a == 1 and b == 0: A.append(encode(t, i+1)) if a == 0 and b == 1: B.append(encode(t, i+1)) if a == 1 and b == 1: both.append(encode(t, i+1)) upd(1, 0, 10001, t, 1) A.sort(); B.sort(); both.sort() p1 = min(k, len(both)) p2 = k - p1 if 2*k - p1 > m or p2 > min(len(A), len(B)): print(-1) exit(0) sum, ans, ch = 0, 2**31, p1 for i in range(p1): sum += both[i]//200002 upd(1, 0, 10001, both[i]//200002, -1) for i in range(p2): sum += A[i]//200002 + B[i]//200002 upd(1, 0, 10001, A[i]//200002, -1) upd(1, 0, 10001, B[i]//200002, -1) ans, _ = query(1, 0, 10001, m-2*k+p1) ans += sum while p1 > 0: if p2 == min(len(A), len(B)): break upd(1, 0, 10001, A[p2]//200002, -1); sum += A[p2]//200002 upd(1, 0, 10001, B[p2]//200002, -1); sum += B[p2]//200002 upd(1, 0, 10001, both[p1-1]//200002, 1); sum -= both[p1-1]//200002 p2 += 1 p1 -= 1 if m - 2*k + p1 < 0: break Q, x = query(1, 0, 10001, m-2*k+p1) if ans > sum + Q: ans = sum + Q ch = p1 print ans ind = [both[i]%200002 for i in range(ch)] + [A[i]%200002 for i in range(k-ch)] + [B[i]%200002 for i in range(k-ch)] st = neither + [both[i] for i in range(ch, len(both))] + [A[i] for i in range(k-ch, len(A))] + [B[i] for i in range(k-ch, len(B))] st.sort() ind += [st[i]%200002 for i in range(m-2*k+ch)] print ' '.join(str(x) for x in ind)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
n, k = [int(i) for i in input().split()] a, b, result = list(), list(), list() for i in ' ' * n: t, x, y = [int(x) for x in input().split()] if x & y: result += [t] elif x: a += [t] elif y: b += [t] for i, j in zip(sorted(a), sorted(b)): result += [i + j] print(-1 if len(result) < k else sum(sorted(result)[:k]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
# t=int(input()) # for j in range(t): n,k = map(int, input().split()) t=[] a=[] b=[] for i in range(n): t1,a1,b1 = map(int, input().split()) if(a1==1 and b1==1): t.append(t1) elif(a1==1): a.append(t1) elif(b1==1): b.append(t1) a.sort() b.sort() k1=min(len(a),len(b)) for i in range(k1): t.append(a[i]+b[i]) ans=0 t.sort() if(len(t)<k): print("-1") else: print(sum(t[:k]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
# -*- coding: utf-8 -*- import math, string, itertools, operator, fractions, heapq, collections, re, array, bisect, sys, functools def solve(line): n, k = map(int, line.split()) a = [] for i in range(n): a.append([int(x) for x in sys.stdin.readline().rstrip().split()]) x, y, z = [], [], [] for i in range(n): if a[i][1] == 1 and a[i][2] == 1: z.append(a[i][0]) elif a[i][1] == 1: x.append(a[i][0]) elif a[i][2] == 1: y.append(a[i][0]) if len(x) + len(z) < k or len(y) + len(z) < k: return -1 x.sort() y.sort() z.sort() ans = 0 i, j, k1 = 0, 0, 0 while k > 0: if i < len(x) and j < len(y) and k1 < len(z) and x[i] + y[j] < z[k1]: ans += x[i] + y[j] i += 1 j += 1 elif k1 < len(z): ans += z[k1] k1 += 1 else: ans += x[i] + y[j] i += 1 j += 1 k -= 1 return ans t = 0 while True: line = sys.stdin.readline().rstrip() if not line: break print(solve(line)) t += 1
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, k; int main() { scanf("%lld%lld", &n, &k); vector<long long> v, ali, bob; for (int i = 0; i < n; i++) { long long t, x, y; scanf("%lld%lld%lld", &t, &x, &y); if (x == 1 && y == 1) v.push_back(t); else if (x == 1 && y == 0) ali.push_back(t); else if (x == 0 && y == 1) bob.push_back(t); } sort(ali.begin(), ali.end()); sort(bob.begin(), bob.end()); long long mn = min(ali.size(), bob.size()); for (int i = 0; i < mn; i++) v.push_back(ali[i] + bob[i]); sort(v.begin(), v.end()); if (v.size() < k) printf("-1\n"); else { long long ans = 0; for (int i = 0; i < k; i++) ans += v[i]; printf("%lld", ans); } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
import sys import bisect as b import math from collections import defaultdict as dd input=sys.stdin.readline #sys.setrecursionlimit(10**7) def cin(): return map(int,sin().split()) def ain(): return list(map(int,sin().split())) def sin(): return input() def inin(): return int(input()) for _ in range(1): n,k=cin() l=[];l1=[];l3=[] for i in range(n): t,a,b=cin() if(a==1 and b==1):l3+=[t] elif(a==1):l+=[t] elif(b==1):l1+=[t] l=sorted(l,reverse=True);l1=sorted(l1,reverse=True);l3=sorted(l3,reverse=True) ans=-1;ma=10**5 if(len(l)+len(l3)>=k and len(l1)+len(l3)>=k): ans=0;k1=k2=k while(k1>0 and k2>0): if(len(l1)==0):l1+=[ma] if(len(l)==0):l+=[ma] if(len(l3)==0):l3+=[ma] if(l[-1]+l1[-1]>l3[-1]): ans+=l3.pop() else: ans+=l1.pop();ans+=l.pop() k1-=1;k2-=1 print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
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: a.sort() b.sort() for i in range(min(len(a), len(b))): both.append(a[i]+b[i]) both.sort() out = sum(both[:k]) 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" ] }
CORRECT
python3
n, k = list(map(int, input().split())) both = [] a = [] b = [] for item in range(n): buff = list(map(int, input().split())) if buff[1] == 1 and buff[2] == 1: both.append(buff[0]) elif buff[1] == 1: a.append(buff[0]) elif buff[2] == 1: b.append(buff[0]) both.sort() a.sort() b.sort() rez = 0 both_ind = 0 tog_ind = 0 while k > 0: if (len(a) - tog_ind > 0 and len(b) - tog_ind > 0): if len(both) - both_ind > 0: if(a[tog_ind] + b[tog_ind] < both[both_ind]): rez += a[tog_ind] + b[tog_ind] k -= 1 tog_ind += 1 else: rez += both[both_ind] k -= 1 both_ind += 1 else: k -= 1 rez += a[tog_ind] + b[tog_ind] tog_ind += 1 elif len(both) - both_ind > 0: k -= 1 rez += both[both_ind] both_ind += 1 else: print(-1) exit() print(rez)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; vector<long long> a, b, ab; int count_a = 0, count_b = 0; for (int i = 0; i < n; i++) { int t; bool b1, b2; cin >> t >> b1 >> b2; if (b1 && b2) { ab.push_back(t); count_a++; count_b++; } else if (b1) { a.push_back(t); count_a++; } else if (b2) { b.push_back(t); count_b++; } } if (count_a < k || count_b < k) { cout << -1 << endl; } else { sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(ab.begin(), ab.end()); int na = a.size(), nb = b.size(), nab = ab.size(); for (int i = 1; i < na; i++) { a[i] += a[i - 1]; } for (int i = 1; i < nb; i++) { b[i] += b[i - 1]; } for (int i = 1; i < nab; i++) { ab[i] += ab[i - 1]; } int x = min(k, nab); long long min_time = LONG_LONG_MAX; for (int i = 0; i <= x; i++) { if (k - i <= min(na, nb)) { if (i > 0 && i < k) { min_time = min(min_time, ab[i - 1] + a[k - i - 1] + b[k - i - 1]); } else if (i == 0) { min_time = min(min_time, a[k - 1] + b[k - 1]); } else { min_time = min(min_time, ab[k - 1]); } } } cout << min_time << endl; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
java
import java.io.*; import java.util.*; public class P1374E1 { public static void main(String[] args) throws IOException { Reader ob = new Reader(); int n=ob.nextInt(); int k=ob.nextInt(); int[] t=new int[n]; int[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++) { t[i]=ob.nextInt(); a[i]=ob.nextInt(); b[i]=ob.nextInt(); } int ans=0; ArrayList<Integer> both=new ArrayList<>(); ArrayList<Integer> ae=new ArrayList<>(); ArrayList<Integer> be=new ArrayList<>(); for(int i=0;i<n;i++) { if(a[i]==1&&b[i]==1) { both.add(t[i]); }else if(a[i]==1&&b[i]==0) { ae.add(t[i]); }else if(a[i]==0&&b[i]==1) { be.add(t[i]); } } Collections.sort(both); Collections.sort(be); Collections.sort(ae); if(both.size()+Math.min(ae.size(), be.size())<k) { System.out.println(-1); }else { int count=0; int st=0; for(int i=0;i<Math.min(ae.size(), be.size());i++) { if(count==k) { break; }else { if(st<both.size()) { if(both.get(st)>ae.get(i)+be.get(i)) { ans=ans+ae.get(i)+be.get(i); count++; }else { ans=ans+both.get(st); st++; i--; count++; } }else { ans=ans+ae.get(i)+be.get(i); count++; } } } while(count<k) { count++; ans=ans+both.get(st); st++; } System.out.println(ans); } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
CORRECT
python3
def binary(a,start,end,e): mid=(start+end)//2 if a[mid]==e: return mid elif a[mid]>e: end=mid-1 else: start=mid+1 if start<=end: return binary(a,start,end,e) else: return start n,k=map(int,input().split()) p=[] al=[] bo=[] for j in range(n): a,b,c=map(int,input().split()) if b==1 and c==1: p.append(a) elif b==1 and c==0: al.append(a) elif b==0 and c==1: bo.append(a) al.sort() bo.sort() if p!=[]: while al!=[] and bo!=[]: v=al[0]+bo[0] #q=binary(p,0,len(p)-1,v) p.append(v) del al[0] del bo[0] else: while al!=[] and bo!=[]: v=al[0]+bo[0] p.append(v) del al[0] del bo[0] p.sort() if len(p)<k: print(-1) else: print(sum(p[:k]))