Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { long long int n, i, j, w, x, y, k, cv1 = 0, cv2 = 0, cv3 = 0, c = 0, ans = 0; cin >> n >> k; vector<long long int> v2, v1, v3; queue<long long int> q1, q2, q3; for (i = 0; i < n; i++) { cin >> w >> x >> y; if (x == 1 && y == 1) { cv3++; v3.push_back(w); } else if (x == 0 && y == 1) { v2.push_back(w); cv2++; } else if (x == 1 && y == 0) { v1.push_back(w); cv1++; } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long int i1 = 0, i3 = 0; while (c < k && min(cv1, cv2) > 0 || cv3 > 0) { if (min(cv1, cv2) == 0 && cv3 > 0) { ans += v3[i3]; i3++; cv3--; c++; } else if (min(cv1, cv2) > 0 && cv3 == 0) { ans += v1[i1] + v2[i1]; cv1--; cv2--; c++; i1++; } else if (min(cv1, cv2) == 0 && cv3 == 0) { break; } else if (v1[i1] + v2[i1] < v3[i3]) { ans += v3[i3]; i3++; c++; cv3--; } else { ans += v1[i1] + v2[i1]; cv1--; cv2--; c++; i1++; } } if (c == k) cout << ans << endl; else cout << "-1" << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Random; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; // CFPS -> CodeForcesProblemSet public final class CFPS { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static int t = 1; static double epsilon = 0.00000001; public static void main(String[] args) { OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(), k = fr.nextInt(); PriorityQueue<Integer> oneonePQ = new PriorityQueue<>(); PriorityQueue<Integer> zeroonePQ = new PriorityQueue<>(); PriorityQueue<Integer> onezeroPQ = new PriorityQueue<>(); for (int i = 0; i < n; i++) { int ti = fr.nextInt(), ai = fr.nextInt(), bi = fr.nextInt(); if (ai == 1 && bi == 1) { oneonePQ.add(ti); } else if (ai == 1 && bi == 0) { onezeroPQ.add(ti); } else zeroonePQ.add(ti); } long totTime = 0; for (int p = 0; p < k; p++) { Integer zot = zeroonePQ.peek(); Integer ozt = onezeroPQ.peek(); Integer oot = oneonePQ.peek(); if (zot == null) { zot = gigamod; } if (ozt == null) { ozt = gigamod; } if (oot != null && (oot < zot + ozt)) { totTime += oneonePQ.poll(); } else { if (onezeroPQ.isEmpty() || zeroonePQ.isEmpty()) { out.println(-1); continue OUTER; } totTime += (onezeroPQ.poll() + zeroonePQ.poll()); } } out.println(totTime); } out.close(); } static String reverse(String s) { StringBuilder sb = new StringBuilder(); int n = s.length(); for (int i = n - 1; i > -1; i--) { sb.append(s.charAt(i)); } return sb.toString(); } static long power(long x, int y) { // int p = 998244353; int p = gigamod; long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Maps elements in a 2D matrix serially to elements in // a 1D array. static int mapTo1D(int row, int col, int n, int m) { return row * m + col; } // Inverse of what the one above does. static int[] mapTo2D(int idx, int n, int m) { int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } // Checks if s has subsequence t. static boolean hasSubsequence(String s, String t) { char[] schars = s.toCharArray(); char[] tchars = t.toCharArray(); int slen = schars.length, tlen = tchars.length; int tctr = 0; if (slen < tlen) return false; for (int i = 0; i < slen || i < tlen; i++) { if (tctr == tlen) break; if (schars[i] == tchars[tctr]) { tctr++; } } if (tctr == tlen) return true; return false; } // Returns the binary string of length at least bits. static String toBinaryString(long num, int bits) { StringBuilder sb = new StringBuilder(Long.toBinaryString(num)); sb.reverse(); for (int i = sb.length(); i < bits; i++) sb.append('0'); return sb.reverse().toString(); } static class CountMap extends TreeMap<Long, Integer>{ CountMap() { } CountMap(CountMap cm) { } public void removeTM(Long key) { super.remove(key); } public void removeTM(Integer key) { super.remove((long) key); } public Integer put(Long key) { if (super.containsKey(key)) { return super.put(key, super.get(key) + 1); } else { return super.put(key, 1); } } public Integer put(int key) { if (super.containsKey((long) key)) { return super.put((long) key, super.get((long) key) + 1); } else { return super.put((long) key, 1); } } public Integer remove(Long key) { Integer count = super.get(key); if (count == null) return -1; if (count == 1) return super.remove(key); else return super.put(key, super.get(key) - 1); } public Integer remove(int key) { Integer count = super.get((long) key); if (count == null) return -1; if (count == 1) return super.remove((long) key); else return super.put((long) key, super.get((long) key) - 1); } public Integer get(int key) { Integer count = super.get((long) key); if (count == null) return 0; return count; } public Integer get(long key) { Integer count = super.get(key); if (count == null) return 0; return count; } } static class Point implements Comparable<Point> { long x; long y; int id; Point() { x = y = id = 0; } Point(Point p) { this.x = p.x; this.y = p.y; this.id = p.id; } Point(long a, long b, int id) { this.x = a; this.y = b; this.id = id; } Point(long a, long b) { this.x = a; this.y = b; } @Override public int compareTo(Point o) { if (this.x > o.x) return 1; if (this.x < o.x) return -1; if (this.y > o.y) return 1; if (this.y < o.y) return -1; return 0; } public boolean equals(Point that) { return this.compareTo(that) == 0; } } static class PointComparator implements Comparator<Point> { @Override public int compare(Point o1, Point o2) { long o1Len = o1.y - o1.x; long o2Len = o2.y - o2.x; if (o1Len > o2Len) return -1; if (o2Len > o1Len) return 1; if (o1.x > o2.x) return 1; if (o2.x > o1.x) return -1; return 0; } } // Returns the largest power of k that fits into n. static int largestFittingPower(long n, long k) { int lo = 0, hi = logk(Long.MAX_VALUE, 3); int largestPower = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; long val = (long) Math.pow(k, mid); if (val <= n) { largestPower = mid; lo = mid + 1; } else { hi = mid - 1; } } return largestPower; } static String bitSetToString(int set) { // We have to print all the elements that are present // in the set. StringBuilder sb = new StringBuilder(); for (int i = 0; i < 30; i++) { if (((set >> i) & 1) == 1) { // The 'i'th bit is on meaning that the element 'i' is // present in the set. sb.append((i + 1) + " "); } } sb.append("\n"); return sb.toString(); } static String displayBitSet(long set) { // We have to print all the elements that are present // in the set. StringBuilder sb = new StringBuilder(); for (int i = 0; i < 60; i++) { if (((set >> i) & 1) == 1) { // The 'i'th bit is on meaning that the element 'i' is // present in the set. sb.append((i + 1) + " "); } } sb.append("\n"); return sb.toString(); } static int addToBitSet(int set, int element) { set = (set) | (1 << (element - 1)); return set; } static int removeFromBitSet(int set, int element) { // Checking whether the bit is present. if ((set & (1 << (element - 1))) == 0) return set; set = set ^ (1 << (element - 1)); return set; } // Returns map of factor and its power in the number. static TreeMap<Long, Integer> primeFactorization(long num) { TreeMap<Long, Integer> map = new TreeMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } // Returns map of factor and its power in the number. static TreeMap<Integer, Integer> primeFactorization(int num) { TreeMap<Integer, Integer> map = new TreeMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2); map.put(2, pwrCnt != null ? pwrCnt + 1 : 1); } for (int i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static Set<Long> divisors(long num) { Set<Long> divisors = new TreeSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static void dfs(int node, boolean[] marked, ArrayList<Integer>[] adj) { if (marked[node]) return; marked[node] = true; for (int adjc : adj[node]) dfs(adjc, marked, adj); } // Returns the index of the first element // larger than or equal to val. static int bsearch(int[] arr, int val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } // Returns the index of the last element // smaller than or equal to val. static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static long factorial(long n) { if (n <= 1) return 1; long factorial = 1; for (int i = 1; i <= n; i++) factorial = mod(factorial * i); return factorial; } static long factorialInDivision(long a, long b) { if (a == b) return 1; if (b < a) { long temp = a; a = b; b = temp; } long factorial = 1; for (long i = a + 1; i <= b; i++) factorial = mod(factorial * i); return factorial; } static BigInteger factorialInDivision(BigInteger a, BigInteger b) { if (a.equals(b)) return BigInteger.ONE; return a.multiply(factorialInDivision(a.subtract(BigInteger.ONE), b)); } static long nCr(long n, long r) { long p = gigamod; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r long fac[] = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if ((y & 1)==1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long nPr(long n, long r) { return factorialInDivision(n, n - r); } static int log2(long n) { return (int)(Math.log(n) / Math.log(2)); } static double log2(long n, boolean doubleMode) { return (Math.log(n) / Math.log(2)); } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } // Sieve of Eratosthenes: static boolean[] primeGenerator(int upto) { boolean[] isPrime = new boolean[upto + 1]; Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i * i < upto + 1; i++) if (isPrime[(int) i]) // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) isPrime[(int) j * (int) i] = false; return isPrime; } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(int[] arr) { int lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(int a, int b) { return (a * b)/gcd(a, b); } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int a, int b) { int temp = a; a = b; b = temp; } static void swap(long a, long b) { long temp = a; a = b; b = temp; } static void swap(double a, double b) { double temp = a; a = b; b = temp; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } private static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static String toString(int[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(boolean[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(long[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(char[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + ""); return sb.toString(); } static String toString(int[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + ""); } sb.append('\n'); } return sb.toString(); } static String toString(long[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(double[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(char[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static char toChar(int i) { return (char) (i + 48); } static long mod(long a, long m) { return (a%m + m) % m; } static long mod(long num) { return (num % gigamod + gigamod) % gigamod; } // Uses weighted quick-union with path compression. static class UnionFind { private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { int root = p; while (root != parent[root]) root = parent[root]; while (p != root) { int newp = parent[p]; parent[p] = root; p = newp; } return root; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private TreeSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; public UGraph(int V) { adj = (TreeSet<Integer>[]) new TreeSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new TreeSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public TreeSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; TreeSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; TreeSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int source, boolean[] marked, Digraph g) { if (marked[source]) return; marked[source] = true; Iterable<Integer> adj = g.adj(source); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void bfsOrder(int source, Digraph g) { } private static void dfsMark(int source, int[] colorIds, int color, Digraph g) { if (colorIds[source] != -1) return; colorIds[source] = color; Iterable<Integer> adj = g.adj(source); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(Digraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) { if (!marked[i]) runDfs(dg, topologicalStack, marked, i); } return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) { if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); } topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[] nextOneIntArray(int n) { int[] arr = new int[n + 1]; for (int i = 1; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } /*public char[] nextCharArray(int n) { char[] chars = new char[n]; for (int i = 0; i < n; i++) chars[i] = fr.nextChar(); return chars; }*/ } private static class IndexMaxPQ<Key extends Comparable<Key>> implements Iterable<Integer> { private int maxN; // maximum number of elements on PQ private int n; // number of elements on PQ private int[] pq; // binary heap using 1-based indexing private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i private Key[] keys; // keys[i] = priority of i /** * Initializes an empty indexed priority queue with indices between {@code 0} * and {@code maxN - 1}. * * @param maxN the keys on this priority queue are index from {@code 0} to {@code maxN - 1} * @throws IllegalArgumentException if {@code maxN < 0} */ public IndexMaxPQ(int maxN) { if (maxN < 0) throw new IllegalArgumentException(); this.maxN = maxN; n = 0; keys = (Key[]) new Comparable[maxN + 1]; // make this of length maxN?? pq = new int[maxN + 1]; qp = new int[maxN + 1]; // make this of length maxN?? for (int i = 0; i <= maxN; i++) qp[i] = -1; } /** * Returns true if this priority queue is empty. * * @return {@code true} if this priority queue is empty; * {@code false} otherwise */ public boolean isEmpty() { return n == 0; } /** * Is {@code i} an index on this priority queue? * * @param i an index * @return {@code true} if {@code i} is an index on this priority queue; * {@code false} otherwise * @throws IllegalArgumentException unless {@code 0 <= i < maxN} */ public boolean contains(int i) { validateIndex(i); return qp[i] != -1; } /** * Returns the number of keys on this priority queue. * * @return the number of keys on this priority queue */ public int size() { return n; } /** * Associate key with index i. * * @param i an index * @param key the key to associate with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if there already is an item * associated with index {@code i} */ public void insert(int i, Key key) { validateIndex(i); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = key; swim(n); } /** * Returns an index associated with a maximum key. * * @return an index associated with a maximum key * @throws NoSuchElementException if this priority queue is empty */ public int maxIndex() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } /** * Returns a maximum key. * * @return a maximum key * @throws NoSuchElementException if this priority queue is empty */ public Key maxKey() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } /** * Removes a maximum key and returns its associated index. * * @return an index associated with a maximum key * @throws NoSuchElementException if this priority queue is empty */ public int delMax() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); int max = pq[1]; exch(1, n--); sink(1); assert pq[n+1] == max; qp[max] = -1; // delete keys[max] = null; // to help with garbage collection pq[n+1] = -1; // not needed return max; } /** * Returns the key associated with index {@code i}. * * @param i the index of the key to return * @return the key associated with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public Key keyOf(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); else return keys[i]; } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} */ public void changeKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); keys[i] = key; swim(qp[i]); sink(qp[i]); } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @deprecated Replaced by {@code changeKey(int, Key)}. */ @Deprecated public void change(int i, Key key) { validateIndex(i); changeKey(i, key); } /** * Increase the key associated with index {@code i} to the specified value. * * @param i the index of the key to increase * @param key increase the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key <= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void increaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling increaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) > 0) throw new IllegalArgumentException("Calling increaseKey() with a key that is strictly less than the key in the priority queue"); keys[i] = key; swim(qp[i]); } /** * Decrease the key associated with index {@code i} to the specified value. * * @param i the index of the key to decrease * @param key decrease the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key >= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void decreaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling decreaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) < 0) throw new IllegalArgumentException("Calling decreaseKey() with a key that is strictly greater than the key in the priority queue"); keys[i] = key; sink(qp[i]); } /** * Remove the key on the priority queue associated with index {@code i}. * * @param i the index of the key to remove * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public void delete(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); int index = qp[i]; exch(index, n--); swim(index); sink(index); keys[i] = null; qp[i] = -1; } // throw an IllegalArgumentException if i is an invalid index private void validateIndex(int i) { if (i < 0) throw new IllegalArgumentException("index is negative: " + i); if (i >= maxN) throw new IllegalArgumentException("index >= capacity: " + i); } /*************************************************************************** * General helper functions. ***************************************************************************/ private boolean less(int i, int j) { return keys[pq[i]].compareTo(keys[pq[j]]) < 0; } private void exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } /*************************************************************************** * Heap helper functions. ***************************************************************************/ private void swim(int k) { while (k > 1 && less(k/2, k)) { exch(k, k/2); k = k/2; } } private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && less(j, j+1)) j++; if (!less(k, j)) break; exch(k, j); k = j; } } /** * Returns an iterator that iterates over the keys on the * priority queue in descending order. * The iterator doesn't implement {@code remove()} since it's optional. * * @return an iterator that iterates over the keys in descending order */ public Iterator<Integer> iterator() { return new HeapIterator(); } private class HeapIterator implements Iterator<Integer> { // create a new pq private IndexMaxPQ<Key> copy; // add all elements to copy of heap // takes linear time since already in heap order so no keys move public HeapIterator() { copy = new IndexMaxPQ<Key>(pq.length - 1); for (int i = 1; i <= n; i++) copy.insert(pq[i], keys[pq[i]]); } public boolean hasNext() { return !copy.isEmpty(); } public void remove() { throw new UnsupportedOperationException(); } public Integer next() { if (!hasNext()) throw new NoSuchElementException(); return copy.delMax(); } } /*public static void main(String[] args) { // insert a bunch of strings String[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" }; IndexMaxPQ<String> pq = new IndexMaxPQ<String>(strings.length); for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // print each key using the iterator for (int i : pq) { StdOut.println(i + " " + strings[i]); } StdOut.println(); // increase or decrease the key for (int i = 0; i < strings.length; i++) { if (StdRandom.uniform() < 0.5) pq.increaseKey(i, strings[i] + strings[i]); else pq.decreaseKey(i, strings[i].substring(0, 1)); } // delete and print each key while (!pq.isEmpty()) { String key = pq.maxKey(); int i = pq.delMax(); StdOut.println(i + " " + key); } StdOut.println(); // reinsert the same strings for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // delete them in random order int[] perm = new int[strings.length]; for (int i = 0; i < strings.length; i++) perm[i] = i; StdRandom.shuffle(perm); for (int i = 0; i < perm.length; i++) { String key = pq.keyOf(perm[i]); pq.delete(perm[i]); StdOut.println(perm[i] + " " + key); } }*/ } public static class IndexMinPQ<Key extends Comparable<Key>> implements Iterable<Integer> { private int maxN; // maximum number of elements on PQ private int n; // number of elements on PQ private int[] pq; // binary heap using 1-based indexing private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i private Key[] keys; // keys[i] = priority of i /** * Initializes an empty indexed priority queue with indices between {@code 0} * and {@code maxN - 1}. * @param maxN the keys on this priority queue are index from {@code 0} * {@code maxN - 1} * @throws IllegalArgumentException if {@code maxN < 0} */ public IndexMinPQ(int maxN) { if (maxN < 0) throw new IllegalArgumentException(); this.maxN = maxN; n = 0; keys = (Key[]) new Comparable[maxN + 1]; // make this of length maxN?? pq = new int[maxN + 1]; qp = new int[maxN + 1]; // make this of length maxN?? for (int i = 0; i <= maxN; i++) qp[i] = -1; } /** * Returns true if this priority queue is empty. * * @return {@code true} if this priority queue is empty; * {@code false} otherwise */ public boolean isEmpty() { return n == 0; } /** * Is {@code i} an index on this priority queue? * * @param i an index * @return {@code true} if {@code i} is an index on this priority queue; * {@code false} otherwise * @throws IllegalArgumentException unless {@code 0 <= i < maxN} */ public boolean contains(int i) { validateIndex(i); return qp[i] != -1; } /** * Returns the number of keys on this priority queue. * * @return the number of keys on this priority queue */ public int size() { return n; } /** * Associates key with index {@code i}. * * @param i an index * @param key the key to associate with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if there already is an item associated * with index {@code i} */ public void insert(int i, Key key) { validateIndex(i); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = key; swim(n); } /** * Returns an index associated with a minimum key. * * @return an index associated with a minimum key * @throws NoSuchElementException if this priority queue is empty */ public int minIndex() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } /** * Returns a minimum key. * * @return a minimum key * @throws NoSuchElementException if this priority queue is empty */ public Key minKey() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } /** * Removes a minimum key and returns its associated index. * @return an index associated with a minimum key * @throws NoSuchElementException if this priority queue is empty */ public int delMin() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); int min = pq[1]; exch(1, n--); sink(1); assert min == pq[n+1]; qp[min] = -1; // delete keys[min] = null; // to help with garbage collection pq[n+1] = -1; // not needed return min; } /** * Returns the key associated with index {@code i}. * * @param i the index of the key to return * @return the key associated with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public Key keyOf(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); else return keys[i]; } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public void changeKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); keys[i] = key; swim(qp[i]); sink(qp[i]); } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @deprecated Replaced by {@code changeKey(int, Key)}. */ @Deprecated public void change(int i, Key key) { changeKey(i, key); } /** * Decrease the key associated with index {@code i} to the specified value. * * @param i the index of the key to decrease * @param key decrease the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key >= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void decreaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling decreaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) < 0) throw new IllegalArgumentException("Calling decreaseKey() with a key strictly greater than the key in the priority queue"); keys[i] = key; swim(qp[i]); } /** * Increase the key associated with index {@code i} to the specified value. * * @param i the index of the key to increase * @param key increase the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key <= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void increaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling increaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) > 0) throw new IllegalArgumentException("Calling increaseKey() with a key strictly less than the key in the priority queue"); keys[i] = key; sink(qp[i]); } /** * Remove the key associated with index {@code i}. * * @param i the index of the key to remove * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public void delete(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); int index = qp[i]; exch(index, n--); swim(index); sink(index); keys[i] = null; qp[i] = -1; } // throw an IllegalArgumentException if i is an invalid index private void validateIndex(int i) { if (i < 0) throw new IllegalArgumentException("index is negative: " + i); if (i >= maxN) throw new IllegalArgumentException("index >= capacity: " + i); } /*************************************************************************** * General helper functions. ***************************************************************************/ private boolean greater(int i, int j) { return keys[pq[i]].compareTo(keys[pq[j]]) > 0; } private void exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } /*************************************************************************** * Heap helper functions. ***************************************************************************/ private void swim(int k) { while (k > 1 && greater(k/2, k)) { exch(k, k/2); k = k/2; } } private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; } } /*************************************************************************** * Iterators. ***************************************************************************/ /** * Returns an iterator that iterates over the keys on the * priority queue in ascending order. * The iterator doesn't implement {@code remove()} since it's optional. * * @return an iterator that iterates over the keys in ascending order */ public Iterator<Integer> iterator() { return new HeapIterator(); } private class HeapIterator implements Iterator<Integer> { // create a new pq private IndexMinPQ<Key> copy; // add all elements to copy of heap // takes linear time since already in heap order so no keys move public HeapIterator() { copy = new IndexMinPQ<Key>(pq.length - 1); for (int i = 1; i <= n; i++) copy.insert(pq[i], keys[pq[i]]); } public boolean hasNext() { return !copy.isEmpty(); } public void remove() { throw new UnsupportedOperationException(); } public Integer next() { if (!hasNext()) throw new NoSuchElementException(); return copy.delMin(); } } /** * Unit tests the {@code IndexMinPQ} data type. * * @param args the command-line arguments */ /* public static void main(String[] args) { // insert a bunch of strings String[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" }; IndexMinPQ<String> pq = new IndexMinPQ<String>(strings.length); for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // delete and print each key while (!pq.isEmpty()) { int i = pq.delMin(); StdOut.println(i + " " + strings[i]); } StdOut.println(); // reinsert the same strings for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // print each key using the iterator for (int i : pq) { StdOut.println(i + " " + strings[i]); } while (!pq.isEmpty()) { pq.delMin(); } }*/ } } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.Scanner; public class CriptografiaCesar { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(),kt=0,t=0,newn=0,x = 0,mt=0; int [][] l = new int[n][3],nl = new int[n][3]; String[] nls = new String[n]; int [] nlt = new int[n]; String uses = ""; for(int i = 0;i<n;i++) { l[i][0] = in.nextInt(); l[i][1] = in.nextInt(); l[i][2] = in.nextInt(); } for(int i=0;i<n;i++) { if(l[i][1] == 1 && l[i][2] == 1) { nl[x][0]=l[i][0]; nl[x][1]=1; nl[x][2]=1; nls[x]=(i+1)+" "; nlt[x]=1; x++; } } while(tiverpares(n,l)) { int a=-1,b=-1; for(int i = 0;i<n;i++) { if(l[i][1] == 1 && l[i][2]==0) { a=i; } if(l[i][1] == 0 && l[i][2]==1) { b=i; } } for(int i = 0;i<n;i++) { if(l[i][0]<l[a][0] && l[i][1] == 1 && l[i][2] == 0) { a=i; } } for(int i = 0;i<n;i++) { if(l[i][0]<l[a][0] && l[i][1] == 0 && l[i][2] == 1) { b=i; } } if(a!=-1 && b!=-1) { nl[x][0]=l[a][0]+l[b][0]; nl[x][1]=1; nl[x][2]=1; nls[x]=(a+1)+" "+(b+1)+" "; nlt[x]=2; x++; l[a]=new int[3]; l[b]=new int[3]; } } while(kt<k) { int tt=0,index=-1; for(int i = 0;i<n;i++) { if(nl[i][0]>tt && nl[i][1]==1 && nl[i][2]==1) { tt=nl[i][0]; index=i; break; } } for(int i = 0;i<n;i++) { if(nl[i][0]<tt && nl[i][1]==1 && nl[i][2]==1 && mt+nlt[i]<=m) { tt=nl[i][0]; index=i; } } if(tt > 0 && index> -1 && mt+nlt[index]<=m) { t+=nl[index][0]; uses+=nls[index]; nl[index]=new int[3]; kt++; mt+=nlt[index]; }else { break; } } while(mt<m) { int tt=0,index=-1; do { tt=0;index=-1; for(int i=0;i<n;i++) { if(l[i][0] > tt) { tt=l[i][0]; index=i; break; } } for(int i=0;i<n;i++) { if(l[i][0] < tt && l[i][0] > 0) { tt=l[i][0]; index=i; } } if(naotem(uses,index)) { break; }else { l[index]=new int[3]; } }while(true); if(naotem(uses,index)) { uses+=index+" "; t+=tt; mt++; }else { break; } } if(kt>=k && mt==m) { System.out.println(t); System.out.println(uses); }else { System.out.println(-1); } } private static boolean naotem(String uses, int index) { for(int i=0;i<uses.length()/2;i++) { if(uses.charAt(i*2) == (char)((index+1)+'0')) { return false; } } return true; } private static boolean tiverpares(int n, int[][] l) { boolean a =false,b=false; for(int i = 0;i<n;i++) { if(l[i][1] == 1 && l[i][2]==0) { a=true; } if(l[i][1] == 0 && l[i][2]==1) { b=true; } } if(a && b) { return true; } return false; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.lang.reflect.Array; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String [] argv) { int n, k; Book [] books; Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); k = scanner.nextInt(); books = new Book[n]; for(int i = 0; i < n; i++) { int t, a, b; t = scanner.nextInt(); a = scanner.nextInt(); b = scanner.nextInt(); books[i] = new Book(t, a, b); } Arrays.sort(books); int pos = 0; int k1 = 0, k2 = 0; long sum = 0; for(pos = 0; pos < n; pos++) { if(books[pos].a == 1) k1++; if(books[pos].b == 1) k2++; sum += books[pos].t; if(k1 >= k && k2 >= k) break; } if(k1 < k || k2 < k) { System.out.println(-1); return; } for(int i = pos; i >= 0; i--) { if(k1 - books[i].a >= k && k2 - books[i].b >= k){ sum -= books[i].t; k1 -= books[i].a; k2 -= books[i].b; } } System.out.println(sum); return; } static class Book implements Comparable<Book>{ public long t; public int a, b; public Book(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } @Override public int compareTo(Book o) { if(this.t < o.t) return -1; if(this.t == o.t) return 0; 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" ] }
IN-CORRECT
python3
def read_ints(): line = input() return [int(e) for e in line.strip().split(' ')] n, k = read_ints() a = [] b = [] t = [] for _ in range(n): it, ia, ib = read_ints() if ia == 1 and ib == 1: t.append(it) elif ia == 1: a.append(it) else: b.append(it) a.sort() b.sort() for i in range(min(len(a), len(b))): t.append(a[i] + b[i]) t.sort() print(-1 if len(t) < k else 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" ] }
IN-CORRECT
java
// package com.company; import java.util.*; import java.lang.*; import java.io.*; //****Use Integer Wrapper Class for Arrays.sort()**** public class DR5 { static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String[] Args)throws Exception{ FastReader scan=new FastReader(System.in); int t=1; // t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int k=scan.nextInt(); book[] arr=new book[n]; int al=0; int bl=0; for(int i=0;i<n;i++){ int ti=scan.nextInt(); int a=scan.nextInt(); int b=scan.nextInt(); if(a==1){ al+=1; }if(b==1){ bl+=1; } arr[i]=new book(ti,a,b); } if(al<k||bl<k){ out.println(-1); }else{ Arrays.sort(arr); ArrayList<Integer> oal=new ArrayList<>(); ArrayList<Integer> obl=new ArrayList<>(); ArrayList<Integer> abl=new ArrayList<>(); for(int i=0;i<n;i++){ book cur=arr[i]; if(cur.a==1&&cur.b==1){ abl.add(i); }else{ if(cur.a==1){ oal.add(i); }else{ obl.add(i); } } } int ap=0; int bp=0; int cp=0; int la=k-Math.min(k,Math.min(oal.size(),obl.size())); cp=la; long ans=0; for(int i=0;i<la;i++){ ans+=arr[abl.get(i)].t; k--; } while(k>0){ long at=arr[oal.get(ap)].t; long bt=arr[obl.get(bp)].t; long ct=Integer.MAX_VALUE; if(cp<abl.size()){ ct=arr[abl.get(cp)].t; } if(at+bt<=ct){ ans+=at+bt; ap++; bp++; }else{ ans+=ct; cp++; } k--; } out.println(ans); } } out.flush(); out.close(); } static class book implements Comparable<book>{ int t; int a; int b; book(int t,int a,int b){ this.t=t; this.a=a; this.b=b; } @Override public int compareTo(book o) { return this.t-o.t; } } static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# f = open('test.py') # def input(): # return f.readline().replace('\n','') import heapq import bisect from collections import deque # from collections import defaultdict def read_list(): return list(map(int,input().strip().split(' '))) def print_list(l): print(' '.join(map(str,l))) def judge_3(n): tmp = 0 for c in str(n): tmp+=int(c) return tmp%3==0 a = deque() b = deque() N,k = read_list() c = [] for _ in range(N): t,x,y = read_list() t = -t if x==1 and y==1: if len(c)<k: heapq.heappush(c,t) elif t>c[0]: heapq.heapreplace(c,t) elif x==1: if len(a)<k: bisect.insort_left(a,t) elif t>a[0]: a.popleft() bisect.insort_left(a,t) elif y==1: if len(b)<k: bisect.insort_left(b,t) elif t>b[0]: b.popleft() bisect.insort_left(b,t) if len(c)+min(len(a),len(b))<k: print(-1) else: res = sum(c) for _ in range(k-len(c)): res+=a.pop() res+=b.pop() while a and b and c and c[0]<b[0]+a[0]: res-=c.heappop() res+=a.pop() res+=b.pop() 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" ] }
IN-CORRECT
python3
import sys input = sys.stdin.readline from math import ceil (n, m, k) = map(int, input().split()) Bob = [] Alice = [] Together = [] Zero = [] Bob_index = [] Alice_index = [] Together_index = [] Zero_index = [] for i in range(n): (t, a, b) = map(int, input().split()) if a*b == 1: Together.append(t) Together_index.append((t,i+1)) elif a == 1: Alice.append(t) Alice_index.append((t, i + 1)) elif b == 1: Bob.append(t) Bob_index.append((t, i + 1)) else: Zero.append(t) Zero_index.append((t, i + 1)) if (len(Bob) + len(Together) < k) or (len(Alice) + len(Together) < k): print(-1) exit() Bob.sort() Alice.sort() Together.sort() Zero.sort() Bob_index.sort() Alice_index.sort() Together_index.sort() Zero_index.sort() a = 0 b = 0 t = 0 T = 0 Total_Bob = 0 Total_Alice = 0 while Total_Bob < k or Total_Alice < k: if Total_Alice < k and Total_Bob < k: if t < len(Together) and a < len(Alice) and b < len(Bob): if Together[t] < Alice[a] + Bob[b]: T += Together[t] t += 1 Total_Alice += 1 Total_Bob += 1 else: T += Alice[a] T += Bob[b] a += 1 b += 1 Total_Alice += 1 Total_Bob += 1 elif t >= len(Together): T += Alice[a] T += Bob[b] a += 1 b += 1 Total_Alice += 1 Total_Bob += 1 else: T += Together[t] Total_Alice += 1 Total_Bob += 1 t += 1 elif Total_Alice < k: if t < len(Together) and a < len(Alice): if Together[t] < Alice[a]: T += Together[t] t += 1 Total_Alice += 1 Total_Bob += 1 else: T += Alice[a] a += 1 Total_Alice += 1 elif t >= len(Together): T += Alice[a] a += 1 Total_Alice += 1 else: T += Together[t] t += 1 Total_Alice += 1 Total_Bob += 1 else: if t < len(Together) and b < len(Bob): if Together[t] < Bob[b]: T += Together[t] Total_Bob += 1 t += 1 Total_Alice += 1 else: T += Bob[b] Total_Bob += 1 b += 1 elif t >= len(Together): T += Bob[b] Total_Bob += 1 b += 1 else: T += Together[t] Total_Bob += 1 t += 1 Total_Alice += 1 if Total_Alice + Total_Bob < m: delta = m - Total_Alice - Total_Bob if delta > t + len(Zero): print(-1) exit() else: z = 0 t -= 1 while delta > 0: #1 -t + a + b #2 +z if t > 0 and a < len(Alice) and b < len(Bob) and z < len(Zero): if Alice[a]+Bob[b] - Together[t] < Zero[z]: T += Alice[a]+Bob[b] - Together[t] delta -= 1 a += 1 b += 1 t -= 1 else: T += Zero[z] z += 1 delta -= 1 elif z >= len(Zero): T += Alice[a] + Bob[b] - Together[t] delta -= 1 a += 1 b += 1 t -= 1 else: T += Zero[z] z += 1 delta -= 1 print(T) ans = [] for i in range(a): ans.append(Alice_index[i][1]) for i in range(b): ans.append(Bob_index[i][1]) for i in range(t): ans.append(Together_index[i][1]) for i in range(z): ans.append(Zero_index[i][1]) print(*ans) exit() if Total_Alice + Total_Bob > m: delta = Total_Bob + Total_Alice - m if a < delta or b < delta: print(-1) exit() for i in range(delta): a -= 1 b -= 1 T -= Bob[b] T -= Alice[a] if t >= len(Together): print(-1) exit() T += Together[t] t += 1 print(T) ans = [] for i in range(a): ans.append(Alice_index[i][1]) for i in range(b): ans.append(Bob_index[i][1]) for i in range(t): ans.append(Together_index[i][1]) print(*ans) exit() print(T) ans = [] for i in range(a): ans.append(Alice_index[i][1]) for i in range(b): ans.append(Bob_index[i][1]) for i in range(t): ans.append(Together_index[i][1]) print(*ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long MOD2 = 1e9 + 9; const long long MOD3 = 1e9 + 21; const long long INF = 5e18; const long double pi = acos(-1.0); long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long exp(long long a, long long b, long long M) { long long result = 1; while (b > 0) { if (b & 1) result = mod(result * a, M); b >>= 1; a = mod(a * a, M); } return result; } void solve() { long long n, k; cin >> n >> k; vector<long long> alice; vector<long long> bob; vector<long long> ambos; long long al = 0, bb = 0; for (long long i = 0; i < n; i++) { long long x, a, b; cin >> x >> a >> b; if (a == 1 and b == 0) { alice.push_back(x); al++; } else if (a == 0 and b == 1) { bob.push_back(x); bb++; } else if (a == 1 and b == 1) { ambos.push_back(x); al++; bb++; } } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(ambos.begin(), ambos.end()); if (al < k or bb < k) { cout << -1 << endl; return; } while (true) { if (al > k and bb > k) { if (alice.size() > 0 and bob.size() > 0 and ambos.size() > 0) { if (alice[alice.size() - 1] >= bob[bob.size() - 1] and alice[alice.size() - 1] >= ambos[ambos.size() - 1]) { al--; alice.pop_back(); } else if (bob[bob.size() - 1] >= alice[alice.size() - 1] and bob[bob.size() - 1] >= ambos[ambos.size() - 1]) { bb--; bob.pop_back(); } else if (ambos[ambos.size() - 1] >= alice[alice.size() - 1] + bob[bob.size() - 1]) { al--; bb--; ambos.pop_back(); } else { if (alice[alice.size() - 1] >= bob[bob.size() - 1]) { al--; alice.pop_back(); } else { bb--; bob.pop_back(); } } } else if (alice.size() > 0 and bob.size() == 0 and ambos.size() > 0) { if (alice[alice.size() - 1] >= ambos[ambos.size() - 1]) { al--; alice.pop_back(); } else { al--; bb--; ambos.pop_back(); } } else if (alice.size() > 0 and bob.size() > 0 and ambos.size() == 0) { al--; bb--; alice.pop_back(); bob.pop_back(); } else if (alice.size() == 0 and bob.size() > 0 and ambos.size() > 0) { if (bob[bob.size() - 1] >= ambos[ambos.size() - 1]) { bb--; bob.pop_back(); } else { al--; bb--; ambos.pop_back(); } } else if (alice.size() == 0 and bob.size() == 0 and ambos.size() > 0) { al--; bb--; ambos.pop_back(); } else if (alice.size() == 0 and bob.size() > 0 and ambos.size() == 0) { bb--; bob.pop_back(); } else if (alice.size() > 0 and bob.size() == 0 and ambos.size() == 0) { al--; alice.pop_back(); } } else if (al > k) { if (alice.size() > 0) { al--; alice.pop_back(); } else { break; } } else if (bb > k) { if (bob.size() > 0) { bb--; bob.pop_back(); } else { break; } } else { break; } } long long ans = 0; for (long long &i : alice) { ans += i; } for (long long &i : bob) { ans += i; } for (long long &i : ambos) { ans += i; } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t = 1; while (t--) solve(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Greedybooks { public static Book bothBook(ArrayList<Book> chosen) { if (chosen.isEmpty()) { return null; } for (int i = chosen.size() - 1; i > -1; i--) { if (chosen.get(i).a == 1 && chosen.get(i).b == 1) { return chosen.get(i); } } return null; } public static int index(ArrayList<Book> chosen) { if (chosen.isEmpty()) { return -1; } for (int i = chosen.size() - 1; i > -1; i--) { if (chosen.get(i).a == 1 && chosen.get(i).b == 1) { return i; } } return -1; } public static void main(String[] args) throws IOException { BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); String s1 = b.readLine(); String[] a = s1.split(" "); int n = Integer.parseInt(a[0]); int m = Integer.parseInt(a[1]); int k = Integer.parseInt(a[2]); ArrayList<Book> bk = new ArrayList<Book>(); ArrayList<Book> both = new ArrayList<Book>(); ArrayList<Book> ai = new ArrayList<Book>(); ArrayList<Book> bi = new ArrayList<Book>(); ArrayList<Book> neither = new ArrayList<Book>(); for (int i = 0; i < n; i++) { String s = b.readLine(); String[] a1 = s.split(" "); Book bok = new Book(Integer.parseInt(a1[0]), Integer.parseInt(a1[1]), Integer.parseInt(a1[2])); bk.add(bok); int y = Integer.parseInt(a1[1]); int z = Integer.parseInt(a1[2]); if (y == 1 && z == 1) { both.add(bok); } if (y == 0 && z == 0) { neither.add(bok); } if (y == 1 && z == 0) { ai.add(bok); } if (y == 0 && z == 1) { bi.add(bok); } } boolean bar = false; ArrayList<Book> bky = (ArrayList<Book>) bk.clone(); Collections.sort(bk, new Sorter()); Collections.sort(both, new Sorter()); Collections.sort(bi, new Sorter()); Collections.sort(ai, new Sorter()); Collections.sort(neither, new Sorter()); ArrayList<Book> chosen = new ArrayList<Book>(); int time = 0; int x = 0; boolean bal = false; while (x < k) { if (both.isEmpty() && !ai.isEmpty() && !bi.isEmpty() && chosen.size() + 1 < m) { chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); x++; bal = true; } if (!both.isEmpty() && (ai.isEmpty() || bi.isEmpty()) && chosen.size() < m) { chosen.add(both.get(0)); time = time + both.get(0).getT(); both.remove(0); x++; bal = true; } if (!both.isEmpty() && !ai.isEmpty() && !bi.isEmpty()) { if (both.get(0).getT() >= ai.get(0).getT() + bi.get(0).getT() && chosen.size() + 1 < m) { chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); x++; bal = true; } else { if (chosen.size() < m) { chosen.add(both.get(0)); time = time + both.get(0).getT(); both.remove(0); x++; bal = true; } } } if (!bal) { break; } else { bal = false; } } int y = 0; if (x < k) { System.out.println(-1); } else { while (chosen.size()<m && !bk.isEmpty()) { if (bothBook(chosen) != null && !ai.isEmpty() && !bi.isEmpty() && bk.get(y).getT() + bothBook(chosen).getT() > ai.get(0).getT() + bi.get(0).getT()) { time = time - chosen.get(index(chosen)).getT(); chosen.remove(index(chosen)); chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); } else { if (!chosen.contains(bk.get(y))) { chosen.add(bk.get(y)); time = time + bk.get(y).getT(); bk.remove(y); } else { y++; } } } if (chosen.size() < m) { System.out.println(-1); } else { System.out.println(time); for (int i = 0; i < chosen.size(); i++) { System.out.print(bky.indexOf(chosen.get(i)) + 1 + " "); } } } } } class Book { int t; int a; int b; public Book(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } public int getT() { return this.t; } } class Sorter implements Comparator<Book> { @Override public int compare(Book o1, Book o2) { return o1.getT() - o2.getT(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int A[2][200005], B[2][200005], Bot[2][200005], Bn[2][200005]; int bo[200005]; struct inb { int ind; int time; } As[200005], Bs[200005], Bots[200005], Bns[200005]; bool cmp(inb a, inb b) { return a.time < b.time; } char comp(int ai, int bi, int boti, int bni) { int min = 10000; if (A[1][ai] > 0 && A[1][ai] < min) { min = A[1][ai]; } if (B[1][bi] > 0 && B[1][bi] < min) { min = B[1][bi]; } if (Bot[1][boti] > 0 && Bot[1][boti] < min) { min = Bot[1][boti]; } if (Bn[1][bni] > 0 && Bn[1][bni] < min) min = Bn[1][bni]; if (min == A[1][ai]) return 'a'; if (min == B[1][bi]) return 'b'; if (min == Bot[1][boti]) return 'o'; if (min == Bn[1][bni]) return 'n'; return 'f'; } int main() { int n, m, k; int t, a, b; scanf("%d %d %d", &n, &m, &k); int ae, be, bote, bne; ae = be = bote = bne = 0; for (int i = 0; i < n; i++) { scanf("%d %d %d", &t, &a, &b); if (a == 1 && b == 1) { Bots[bote].ind = i + 1; Bot[0][bote] = i + 1; Bots[bote].time = t; Bot[1][bote++] = t; } else if (a == 1 && b == 0) { As[ae].ind = i + 1; As[ae].time = t; A[0][ae] = i + 1; A[1][ae++] = t; } else if (b == 1 && a == 0) { Bs[be].ind = i + 1; Bs[be].time = t; B[0][be] = i + 1; B[1][be++] = t; } else { Bns[bne].ind = i + 1; Bns[bne].time = t; Bn[0][bne] = i + 1; Bn[1][bne++] = t; } } sort(As, As + ae, cmp); sort(Bs, Bs + be, cmp); sort(Bots, Bots + bote, cmp); sort(Bns, Bns + bne, cmp); for (int i = 0; i < ae; i++) { A[0][i] = As[i].ind; A[1][i] = As[i].time; } for (int i = 0; i < be; i++) { B[0][i] = Bs[i].ind; B[1][i] = Bs[i].time; } for (int i = 0; i < bote; i++) { Bot[0][i] = Bots[i].ind; Bot[1][i] = Bots[i].time; } for (int i = 0; i < bne; i++) { Bn[0][i] = Bns[i].ind; Bn[1][i] = Bns[i].time; } int ik = 0, boti = 0, ai = 0, bi = 0, bni = 0; int ans = 0, boi = 0, cat = 0, ka = 0, kb = 0; int mab = ae; if (be < ae) mab = be; if (bote + mab < k || bote < 2 * k - m) { printf("%d\n", -1); } else { while (ik <= k - 1) { if (cat < m - k) { if (boti < bote) { if (ai < ae && bi < be) { if (ka > 0) { if (B[1][bi] < Bot[1][boti]) { ik++; bo[boi++] = B[0][bi]; ans += B[1][bi++]; ka--; } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } else if (kb > 0) { if (A[1][ai] < Bot[1][boti]) { ik++; bo[boi++] = A[0][ai]; ans += A[1][ai++]; kb--; } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } else { char mc = comp(ai, bi, boti, bni); int mi, bt; switch (mc) { case 'a': mi = A[1][ai]; bt = A[0][ai]; break; case 'b': mi = B[1][bi]; bt = B[0][bi]; break; case 'o': mi = Bot[1][boti]; bt = Bot[0][boti]; break; case 'n': mi = Bn[1][bni]; bt = Bn[0][bni]; break; default: break; } if (Bot[1][boti] + mi <= (A[1][ai] + B[1][bi])) { ik++; bo[boi++] = Bot[0][boti]; if (mc == 'a') { ka++; cat++; bo[boi++] = A[0][ai]; ans += Bot[1][boti++] + A[1][ai++]; } else if (mc == 'b') { kb++; cat++; bo[boi++] = B[0][bi]; ans += Bot[1][boti++] + B[1][bi++]; } else if (mc == 'o') { ans += Bot[1][boti++]; } else if (mc == 'n') { cat++; bo[boi++] = Bn[0][bni]; ans += Bot[1][boti++] + Bn[1][bni++]; } } else { ik++; cat++; bo[boi++] = A[0][ai]; bo[boi++] = B[0][bi]; ans += A[1][ai++] + B[1][bi++]; } } } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } else { ik++; if (ka--) { bo[boi++] = B[0][bi]; ans += B[1][bi++]; } else if (kb--) { bo[boi++] = A[0][ai]; ans += A[1][ai++]; } else { cat++; bo[boi++] = A[0][ai]; bo[boi++] = B[0][bi]; ans += A[1][ai++] + B[1][bi++]; } } } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } while (boi < m) { char mc = comp(ai, bi, boti, bni); int mi, bt; switch (mc) { case 'a': mi = A[1][ai]; bt = A[0][ai++]; break; case 'b': mi = B[1][bi]; bt = B[0][bi++]; break; case 'o': mi = Bot[1][boti]; bt = Bot[0][boti++]; break; case 'n': mi = Bn[1][bni]; bt = Bn[0][bni++]; break; default: break; } ans += mi; ik++; bo[boi++] = bt; } printf("%d\n", ans); for (int i = 0; i < m - 1; i++) { printf("%d ", bo[i]); } printf("%d\n", bo[m - 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" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; public class code6 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws CloneNotSupportedException { // TODO Auto-generated method stub FastReader scn = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = 1; //int p = t; while(t-->0) { int n = scn.nextInt(); int k = scn.nextInt(); Pair[] arr = new Pair[n]; int c1 = 0; int c2 = 0; for(int i=0; i<n; i++) { int a = scn.nextInt(); int b = scn.nextInt(); int c = scn.nextInt(); c1+=b; c2+=c; arr[i] = new Pair(i,a,b,c); } Arrays.sort(arr); LinkedList<Pair> q1 = new LinkedList<>(); LinkedList<Pair> q2 = new LinkedList<>(); if(c1<k || c2<k) { System.out.println("-1"); continue; } long ans = 0; int s = k; for(int i=0; i<n; i++) { int flag = 0; // if((q1.size()<s && q2.size()<s) && arr[i].a==1 && arr[i].b == 1) // s--; // if(!q1.isEmpty() && !q2.isEmpty() && q1.size() == s && q2.size() == s && arr[i].a == 1 && arr[i].b == 1) { // int a = q1.getLast().t + q2.getLast().t; // if(a>arr[i].t) { // ans-=a; // ans+=arr[i].t; // s--; // q1.removeLast(); // q2.removeLast(); // } // } // if(q1.size()<s && arr[i].a == 1) { // if(arr[i].b != 1) // q1.addLast(arr[i]); // ans += arr[i].t; // } // if(q2.size()<s && arr[i].b == 1 && arr[i].a != 1) { // q2.addLast(arr[i]); // ans+=arr[i].t; // } if(q1.size() == k && q2.size() == k && arr[i].a==1 && arr[i].b == 1) { Pair p1 = q1.getLast(); Pair p2 = q2.getLast(); if((p1.a == 1 && p1.b == 1) || (p2.a == 1 && p2.b == 1)) continue; int a = p1.t + p2.t; if(a>arr[i].t) { ans -= a; ans += arr[i].t; q1.removeLast(); q2.removeLast(); q1.addFirst(arr[i]); q2.addFirst(arr[i]); } continue; } if(q1.size() < k && arr[i].a == 1) { if(arr[i].b == 1) { q1.addFirst(arr[i]); if(q2.size() == k) { ans -= q2.removeLast().t; } flag = 1; } else q1.addLast(arr[i]); ans += arr[i].t; } if(q2.size() < k && arr[i].b == 1) { if(arr[i].a == 1) { q2.addFirst(arr[i]); if(flag == 0 && q1.size() == k) { ans -= q1.removeLast().t; } } else q2.addLast(arr[i]); if(flag == 0) ans += arr[i].t; } } // if(q1.size() < s) { // int a = s-q1.size(); // while(a>0) { // ans -= q2.removeLast().t; // a--; // } // } // else if(q2.size() < s) { // int a = s-q2.size(); // while(a>0) { // ans -= q1.removeLast().t; // a--; // } // } // System.out.println(s); // System.out.println(q1.size()); // System.out.println(q2.size()); System.out.println(ans); } out.flush(); } public static boolean check(int mid, int c, int[] arr, int k) { int ans = 0; for(int i=0; i<arr.length; i++) { if(c==0) { ans++; c = (c+1)%2; }else { if(arr[i]<=mid) { ans++; c = (c+1)%2; } } } return ans>=k; } public static int power(int x, int y, int p) { // Initialize result int res = 1; // Update x if it is more // than or equal to p x = x % p; if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } public static class Pair implements Comparable<Pair>{ int i; int t; int a; int b; Pair(int i,int m, int s, int k){ this.i = i; t = m; a = s; b = k; } public int compareTo(Pair p) { return this.t-p.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" ] }
IN-CORRECT
python3
n, m, k = map(int, input().split()) common_books = [] alice_books = [] bob_books = [] disliked_books = [] durations = [] for i in range(n): duration, alice, bob = map(int, input().split()) if alice and bob: collection = common_books elif alice: collection = alice_books elif bob: collection = bob_books else: collection = disliked_books collection.append(i) durations.append(duration) def get0(collection, idx): if idx < len(collection): return durations[collection[idx]] return 0 def print_ans(idxs): idxs.sort() total = sum(durations[i] for i in idxs) print(total) print(' '.join(str(i + 1) for i in idxs)) if len(bob_books) + len(common_books) < k or len(alice_books) + len(common_books) < k: print(-1) else: for collection in (common_books, alice_books, bob_books): collection.sort(key=lambda idx: durations[idx]) icom = 0 iali = 0 ibob = 0 idis = 0 ncom = len(common_books) nali = len(alice_books) nbob = len(bob_books) ndis = len(disliked_books) total = 0 for i in range(k): if icom < ncom and get0(alice_books, iali) + get0(bob_books, ibob) >= get0(common_books, icom): total += get0(common_books, icom) icom += 1 elif iali < nali and ibob < nbob: total += get0(alice_books, iali) total += get0(bob_books, ibob) iali += 1 ibob += 1 else: total += get0(common_books, icom) icom += 1 if m < icom + iali + ibob: for _ in range(icom + iali + ibob - m): if iali and ibob and icom != ncom: iali -= 1 ibob -= 1 total -= get0(alice_books, iali) total -= get0(bob_books, ibob) total += get0(common_books, icom) icom += 1 else: print(-1) break print_ans(common_books[:icom] + alice_books[:iali] + bob_books[:ibob] + disliked_books[:idis]) else: for _ in range(m - k): if icom and iali < nali and ibob < nbob and get0(alice_books, iali) + get0(bob_books, ibob) - get0(common_books, icom - 1) >= get0(disliked_books, idis): icom -= 1 total -= get0(common_books, icom) total += get0(alice_books, icom) total += get0(bob_books, ibob) iali += 1 ibob += 1 elif idis < ndis: total += get0(disliked_books, idis) idis += 1 print_ans(common_books[:icom] + alice_books[:iali] + bob_books[:ibob] + disliked_books[:idis])
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys input=sys.stdin.buffer.readline n,k=[int(x) for x in input().split()] book=[] #(ti,ai,bi) for _ in range(n): book.append([int(x) for x in input().split()]) book.sort(key=lambda x:x[0]) #sort by ti asc. t=0 a=0 b=0 aPtr=0 #move aPtr upwards until a==k, only removing books with ai==1 and bi==0 bPtr=0 #move aPtr upwards until b==k, only removing books with bi==1 and ai==0 minT=99999999999999999999999999999 for i in range(n): ti,ai,bi=book[i] if ai==1 or bi==1: a+=ai b+=bi t+=ti while aPtr<n and a>k: #remove books with ai==1 and bi==0 and increment aPtr if book[aPtr][1]==1 and book[aPtr][2]==0: a-=1 t-=book[aPtr][0] aPtr+=1 while bPtr<n and b>k: #remove books with bi==1 and ai==0 and increment bPtr if book[bPtr][1]==0 and book[bPtr][2]==1: b-=1 t-=book[bPtr][0] bPtr+=1 if a>=k and b>=k: minT=min(minT,t) if minT==99999999999999999999999999999: print(-1) else: print(minT)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]); ArrayList<Integer> ab=new ArrayList<>(); ArrayList<Integer> a=new ArrayList<>(); ArrayList<Integer> b=new ArrayList<>(); int i,al=0,bo=0,x,y,z; for(i=0;i<n;i++) { s=bu.readLine().split(" "); x=Integer.parseInt(s[0]); y=Integer.parseInt(s[1]); z=Integer.parseInt(s[2]); if(y==1) al++; if(z==1) bo++; if(y==1 && z==1) {ab.add(x); continue;} if(y==1) a.add(x); else b.add(x); } if(al<k || bo<k) {System.out.print("-1"); return;} Collections.sort(a); Collections.sort(b); for(i=0;i<Math.min(a.size(),b.size());i++) ab.add(a.get(i)+b.get(i)); Collections.sort(ab); int min=0; for(i=0;i<k;i++) min+=ab.get(i); System.out.print(min); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; // // gatogari. public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); int[][] array = new int[n][3]; for(int i = 0; i < n; ++i) { array[i][0] = scanner.nextInt(); array[i][1] =scanner.nextInt(); array[i][2] =scanner.nextInt(); } //sort arr int[] uno_cero = new int[n]; int unces = 0; int a = 0; int[] cero_uno = new int[n]; int ceuns = 0; int b = 0; long res = 0L; sort(array); for(int i = 0; i < n; ++i) { if(array[i][1] == 1 && array[i][2] == 1) { if(a >= k) { --a; res -= uno_cero[unces-1]; --unces; } if(b >= k) { --b; res -= cero_uno[ceuns-1]; --ceuns; } ++a; ++b; res += array[i][0]; } else if(array[i][1] == 1 && a < k) { uno_cero[unces] = array[i][0]; ++unces; ++a; res += array[i][0]; } else if(array[i][2] == 1 && b < k) { cero_uno[ceuns] = array[i][0]; ++ceuns; ++b; res += array[i][0]; } if(a >= k && b >= k) break; } long sol = a >= k && b >= k ? res: -1; System.out.println(sol); } public static void sort(int[][] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); for (int i=n-1; i>0; i--) { int temp = arr[0][0]; int tempp = arr[0][1]; int temppp = arr[0][2]; arr[0][0] = arr[i][0]; arr[0][1] = arr[i][1]; arr[0][2] = arr[i][2]; arr[i][0] = temp; arr[i][1] = tempp; arr[i][2] = temppp; heapify(arr, i, 0); } } static void heapify(int[][] arr, int n, int i) { int largest = i; int l = 2*i + 1; int r = 2*i + 2; if (l < n && arr[l][0] > arr[largest][0]) { largest = l; } if (r < n && arr[r][0] > arr[largest][0]) { largest = r; } if (largest != i) { int swap = arr[i][0]; int swapp = arr[i][1]; int swappp = arr[i][2]; arr[i][0] = arr[largest][0]; arr[i][1] = arr[largest][1]; arr[i][2] = arr[largest][2]; arr[largest][0] = swap; arr[largest][1] = swapp; arr[largest][2] = swappp; heapify(arr, n, largest); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; vector<pair<int, int> > both, a, b, non; int n, m, k; int bothsum[maxn], asum[maxn], bsum[maxn], nonsum[maxn]; int get(vector<pair<int, int> >& V, int x) { int l = -1, r = V.size(); while (l + 1 < r) { int mid = l + r >> 1; if (V[mid].first <= x) l = mid; else r = mid; } return l + 1; } int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { int t, x, y; scanf("%d%d%d", &t, &x, &y); if (x == 1 && y == 1) both.push_back({t, i}); else if (x == 1) a.push_back({t, i}); else if (y == 1) b.push_back({t, i}); else non.push_back({t, i}); } sort(both.begin(), both.end()); sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(non.begin(), non.end()); for (int i = 1; i <= both.size(); i++) bothsum[i] = bothsum[i - 1] + both[i - 1].first; for (int i = 1; i <= a.size(); i++) asum[i] = asum[i - 1] + a[i - 1].first; for (int i = 1; i <= b.size(); i++) bsum[i] = bsum[i - 1] + b[i - 1].first; for (int i = 1; i <= non.size(); i++) nonsum[i] = nonsum[i - 1] + non[i - 1].first; long long ans = 1e18, BO, A, B, NO; for (int i = 0; i <= both.size() && i <= m; i++) { int cho = max(k - i, 0); if (a.size() < cho || b.size() < cho) continue; int x = m - cho * 2 - i, l = 0, r = 1e5; if (x < 0) continue; while (l + 1 < r) { int mid = l + r >> 1; int ac = max(get(a, mid) - cho, 0), bc = max(get(b, mid) - cho, 0), nonc = get(non, mid); if (ac + bc + nonc >= x) r = mid; else l = mid; } int ac = max(get(a, r) - cho, 0), bc = max(get(b, r) - cho, 0), nonc = get(non, r); if (ac + bc + nonc < x) { cout << i << ' ' << both.size() << ' ' << a.size() << ' ' << b.size() << ' ' << non.size() << ' ' << cho; return 0; } long long now = bothsum[i] + asum[cho + ac] + bsum[cho + bc] + nonsum[nonc]; if (ac + bc + nonc > x) now -= (ac + bc + nonc - x) * r; if (now < ans) { BO = i; A = ac; B = bc; NO = nonc; ans = now; } } if (ans == 1e18) puts("-1"); else { cout << ans << endl; vector<pair<int, int> > V; int cho = max(0LL, k - BO); for (int i = 0; i < BO; i++) cout << both[i].second << ' '; for (int i = 0; i < cho; i++) cout << a[i].second << ' ' << b[i].second << ' '; for (int i = 0; i < A; i++) V.push_back(a[i + cho]); for (int i = 0; i < B; i++) V.push_back(b[i + cho]); for (int i = 0; i < NO; i++) V.push_back(non[i]); sort(V.begin(), V.end()); for (int i = 0; i < m - BO - 2 * cho; i++) cout << V[i].second << ' '; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import math def gcd(a,b): if (b == 0): return a return gcd(b, a%b) def lcm(a,b): return (a*b) / gcd(a,b) def bs(arr, l, r, x): while l <= r: mid = l + (r - l)//2; if(arr[mid]==x): return arr[mid] elif(arr[mid]<x): l = mid + 1 else: r = mid - 1 return -1 def swap(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list t = 1 for _ in range(t): n,m,k = map(int,input().split()) x = [] c=0 ans=0 y01 = [] y10 = [] y11 = [] vis = [] for i in range(n): tt,a,b = map(int,input().split()) if(a==1 and b==1): y11.append((tt,i)) elif(a==1 and b==0): y10.append((tt,i)) elif(a==0 and b==1): y01.append((tt,i)) vis.append((tt,0,i)) y11.sort() y01.sort() y10.sort() f=0 i = 0 j = 0 book = 0 bob = 0 alice = 0 while(k): k-=1 if(i<len(y11) and (j<len(y01) and j<len(y10))): if(y11[i][0]>=y01[j][0]+y10[j][0] and book+2<=m): ans+=y10[j][0]+y01[j][0] vis[y01[j][1]] = (vis[y01[j][1]][0],1,vis[y01[j][1]][2]) vis[y10[j][1]] = (vis[y10[j][1]][0],1,vis[y10[j][1]][2]) book+=2 j+=1 else: ans+=y11[i][0] vis[y11[i][1]] = (vis[y11[i][1]][0],1,vis[y11[i][1]][2]) book+=1 i+=1 elif(i<len(y11) and book+1<=m): ans+=y11[i][0] vis[y11[i][1]] = (vis[y11[i][1]][0],1,vis[y11[i][1]][2]) book+=1 i+=1 elif((j<len(y01) and j<len(y10)) and book+2<=m): ans+=y10[j][0]+y01[j][0] vis[y01[j][1]] = (vis[y01[j][1]][0],1,vis[y01[j][1]][2]) vis[y10[j][1]] = (vis[y10[j][1]][0],1,vis[y10[j][1]][2]) book+=2 j+=1 else: f=1 # print(book) vis.sort() if(m-book>0): z = m-book x = 0 while(z): if(x>len(vis)): f=1 break if(vis[x][1]==0): ans+=vis[x][0] vis[x] = (vis[x][0],1,vis[x][2]) z-=1 x+=1 # print(vis) if(f): print(-1) continue print(ans) for i in range(len(vis)): if(vis[i][1]==1): print(vis[i][2]+1, end=' ') print()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; public class bhaa { InputStream is; PrintWriter o; /////////////////// CODED++ BY++ ++ ++ ++ BHAVYA++ ARORA++ ++ ++ ++ FROM++ JAYPEE++ INSTITUTE++ OF++ INFORMATION++ TECHNOLOGY++ //////////////// ///////////////////////// Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. ///////////////// class book implements Comparable<book> { long time; book(long t) { time=t; } public int compareTo(book b) { if(b.time==time) { return 0; } else if(time<b.time) { return -1; } else { return 1; } } book(){}; } void solve() { int n=ni(); int k=ni(); int tc1=0; int tc2=0; int ind1=0; int ind2=0; int ind3=0; long row1[]=new long[n]; int row2[]=new int [n]; int row3[]=new int[n]; int ty1=0; int ty2=0; int ty3=0; for(int i=0;i<n;i++) { row1[i]=nl(); row2[i]=ni(); row3[i]=ni(); if(row2[i]==1&&row3[i]==1) { ty3++; } else if(row2[i]==1) { ty1++; } else if(row3[i]==1) { ty2++; } } book ab[]=new book[ty1]; book bb[]=new book[ty2]; book mb[]=new book[ty3]; for(int i=0;i<n;i++) { long time=row1[i]; int al=row2[i]; int bo=row3[i]; if(al==1||bo==1) { if(al==1&&bo==1) { tc1++; tc2++; mb[ind3++]=new book(time); } else if(al==1) { tc1++; ab[ind1++]=new book(time); } else if(bo==1) { tc2++; bb[ind2++]=new book(time); } } } if(tc1<k||tc2<k) { o.println(-1); } else { Arrays.sort(ab); Arrays.sort(bb); Arrays.sort(mb); /* for(book b:ab) { o.print(b.time+" "); } o.println(); for(book b:bb) { o.print(b.time+" "); } o.println(); for(book b:mb) { o.print(b.time+" "); } o.println(); */ int c1=k; int c2=k; int s1=ind1; int s2=ind2; int s3=ind3; int i1=0; int i2=0; int i3=0; long res=0; while(c1>0&&c2>0&&i1<s1&&i2<s2&&i3<s3) { long indi=ab[i1].time+bb[i2].time; long bot=mb[i3].time; if(ind1<bot) { c1--; c2--; i1++; i2++; res+=indi; } else { c1--; c2--; i3++; res+=bot; } // o.println("yes"); } if(i3==s3) { while(c1>0) { res+=ab[i1++].time; c1--; } while(c2>0) { res+=bb[i2++].time; c2--; } } if(i1==s1) { while(c1>0&&i3<s3) { c1--; c2--; res+=mb[i3++].time; // o.println(res); } while(c2>0) { c2--; res+=bb[i2++].time; } } // o.println(res+"af"); if(i2==s2) { while(c2>0&&i3<s3) { c1--; c2--; res+=mb[i3++].time; } while(c1>0) { c1--; res+=ab[i1++].time; } } o.println(res); } } //---------- I/O Template ---------- public static void main(String[] args) { new bhaa().run(); } void run() { is = System.in; o = new PrintWriter(System.out); solve(); o.flush(); } byte input[] = new byte[1024]; int len = 0, ptr = 0; int readByte() { if(ptr >= len) { ptr = 0; try { len = is.read(input); } catch(IOException e) { throw new InputMismatchException(); } if(len <= 0) { return -1; } } return input[ptr++]; } boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); } int skip() { int b = readByte(); while(b != -1 && isSpaceChar(b)) { b = readByte(); } return b; } char nc() { return (char)skip(); } String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } int ni() { int n = 0, b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } if(b == -1) { return -1; } //no input while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } long nl() { long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } double nd() { return Double.parseDouble(ns()); } float nf() { return Float.parseFloat(ns()); } int[] nia(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) { a[i] = ni(); } return a; } long[] nla(int n) { long a[] = new long[n]; for(int i = 0; i < n; i++) { a[i] = nl(); } return a; } int [][] nim(int n) { int mat[][]=new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { mat[i][j]=ni(); } } return mat; } long [][] nlm(int n) { long mat[][]=new long[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { mat[i][j]=nl(); } } return mat; } char[] ns(int n) { char c[] = new char[n]; int i, b = skip(); for(i = 0; i < n; i++) { if(isSpaceChar(b)) { break; } c[i] = (char)b; b = readByte(); } return i == n ? c : Arrays.copyOf(c,i); } void piarr(int arr[]) { for(int i=0;i<arr.length;i++) { o.print(arr[i]+" "); } o.println(); } void plarr(long arr[]) { for(int i=0;i<arr.length;i++) { o.print(arr[i]+" "); } o.println(); } void pimat(int mat[][]) { for(int i=0;i<mat.length;i++) { for(int j=0;j<mat[0].length;j++) { o.print(mat[i][j]+" "); } o.println(); } } void plmat(long mat[][]) { for(int i=0;i<mat.length;i++) { for(int j=0;j<mat[0].length;j++) { o.print(mat[i][j]+" "); } o.println(); } } static class DJset { int n; ty1 arr[]; class ty1 { int rank; int par; ty1(int p,int r) { par=p; rank=r; } } DJset(int nu) { n=nu; arr=new ty1[n]; for(int i=0;i<n;i++) { arr[i]=new ty1(i,1); } } int find(int x) { if(arr[x].par!=x) { arr[x].par=find(arr[x].par); } return arr[x].par; } void union(int x,int y) { x=find(x); y=find(y); if(x!=y) { if(arr[x].rank<arr[y].rank) { arr[x].par=y; } else if(arr[y].rank<arr[x].rank) { arr[y].par=x; } else { arr[x].rank++; arr[y].par=x; } } } public String toString() { String res=""; for(int i=0;i<n;i++) { res+=(arr[i].par+" "); } return res; } public int countsets() { int res=0; for(int i=0;i<n;i++) { if(arr[i].par==i) { res++; } } return res; } } //////////////////////////////////// template finished ////////////////////////////////////// }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; public class Question5 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); int[][] arr = new int[n][3]; ArrayList<Integer> list[] = new ArrayList[4]; for(int i = 0;i < 4;i++)list[i] = new ArrayList<Integer>(); int sumb = 0, sumc = 0; for(int i = 0;i < n;i++) { int t = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); sumb += b; sumc += c; list[2 * b + c].add(t); } if(sumb < k || sumc < k) { System.out.println(-1); return; } Collections.sort(list[1]); Collections.sort(list[2]); Collections.sort(list[3]); long ans = 0; sumb = 0; sumc = 0; ArrayList<Integer> ansList = new ArrayList<Integer>(); int ini = Math.min(k, list[3].size()); for(int i = 0;i < ini;i++) { int x = list[3].get(i); ans += x; ansList.add(x); sumb += 1; sumc += 1; } Collections.sort(ansList, Collections.reverseOrder()); int i = 0; while(sumb < k) { ansList.add(list[1].get(i)); ansList.add(list[2].get(i)); ans += list[1].get(i) + list[2].get(i); sumb++; i++; } int j = i; int l = 0; int ax = list[1].size(); int bx = list[2].size(); while(i < list[1].size() && j < list[2].size() && l < ini) { if(list[1].get(i) + list[2].get(j) < ansList.get(0)) { ans -= ansList.get(0); ansList.remove(0); ans += list[1].get(i); ans += list[1].get(j); ansList.add(list[1].get(i)); ansList.add(list[2].get(j)); i++; j++; l++; } else break; if(i >= ax || j >= bx || l >= ini) { break; } } System.out.println(ans); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
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 = [] f = [i + 1 for i in range(n)] for i in range(n): q.append(rlinput()) for i in range(n - 1): for j in range(n - i - 1): if q[j][0] > q[j + 1][0]: q[j], q[j + 1] = q[j + 1], q[j] f[j], f[j + 1]= f[j + 1], f[j] w,e,r, fak,pr,pe,pw,fe,fr,fw = [], [], [], [], [0],[0],[0], [], [], [] res1, res2, c = 0, 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]) fw.append(f[i]) elif q[i][1]: e.append(q[i][0]) pe.append(e[-1] + pe[-1]) fe.append(f[i]) elif q[i][2]: r.append(q[i][0]) pr.append(r[-1] + pr[-1]) fr.append(f[i]) else: fak.append(q[i][0]) 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)): t = pw[i] + pr[k - i] + pe[k - i] if t <= res: c = i res = t dell = sorted(fw[:c] + fe[: c + k] + fr[: k + c], reverse = True) for i in dell: rt = f.index(i) del q[rt] del f[rt] for i in range(m - 2 * k + c): dell.append(f[i]) res += q[i][0] print(res) print(*dell) 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFLL = 1e18; const int MOD = 1000000007; const int alphabet = 26; const int MAXINT = 2e5 + 1; struct book { int t, a, b; }; bool comp(book b1, book b2) { if (b1.t != b2.t) return b1.t < b2.t; int c1 = b1.a + b1.b, c2 = b2.a + b2.b; return c1 > c2; } int main() { int n, k, ti, a, b, m; deque<pair<int, int> > Alice, Bob, Both, rest; scanf("%d", &n), scanf("%d", &m), scanf("%d", &k); for (int i = 0; i < n; i++) { scanf("%d", &ti), scanf("%d", &a), scanf("%d", &b); if (a && !b) Alice.push_back({ti, i + 1}); else if (!a && b) Bob.push_back({ti, i + 1}); else if (a && b) Both.push_back({ti, i + 1}); else rest.push_back({ti, i + 1}); } sort(Alice.begin(), Alice.end()); sort(Bob.begin(), Bob.end()); sort(Both.begin(), Both.end()); vector<int> tomados; int ans = 0; while (m > 0 && k && !Both.empty()) { if (!Alice.empty() && !Bob.empty() && Alice.front().first + Bob.front().first < Both.front().first && m >= 2) { ans += Alice.front().first + Bob.front().first; tomados.push_back(Alice.front().second); tomados.push_back(Bob.front().second); Alice.pop_front(); Bob.pop_front(); m -= 2; } else { ans += Both.front().first; tomados.push_back(Both.front().second); Both.pop_front(); m--; } k--; } while (m > 0 && k && !Alice.empty() && !Bob.empty()) { tomados.push_back(Alice.front().second), ans += Alice.front().first, Alice.pop_front(); tomados.push_back(Bob.front().second), ans += Bob.front().first, Bob.pop_front(); k--, m--, m--; } if (m > 0) { for (pair<int, int> t : Alice) rest.push_back(t); for (pair<int, int> t : Bob) rest.push_back(t); for (pair<int, int> t : Both) rest.push_back(t); sort(rest.begin(), rest.end()); while (!rest.empty() && m) tomados.push_back(rest.front().second), m--, ans += rest.front().first, rest.pop_front(); } if (k || m != 0) printf("-1\n"); else { printf("%d\n", ans); for (int e : tomados) cout << e << " "; cout << endl; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from fractions import Fraction import bisect import os import io from collections import Counter import bisect from collections import defaultdict import math import random import heapq as hq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate from queue import Queue # sys.setrecursionlimit(200000) # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) mod = int(1e9)+7 def factors(n): return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) # ---------------------------------------------------- # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') def pre(a): if len(a) == 0: return [] ans = [a[0][0]] for i in range(1, len(a)): ans.append(ans[-1] + a[i][0]) return ans t = 1 # t = iinput() oo = [] zo = [] oz = [] zz = [] for _ in range(t): n,m, k = rinput() for i in range(n): t, a, b = rinput() if a == 1 and b == 1: oo.append((t,i)) elif a == 1: oz.append((t,i)) elif b==1: zo.append((t, i)) else: zz.append((t,i)) oo.sort() zo.sort() oz.sort() zz.sort() poo = pre(oo) pzo = pre(zo) poz = pre(oz) noo = len(oo) nzo = len(zo) noz = len(oz) ans = float('inf') config = [0, 0, 0] #oo,oz,zo for i in range(noo+1): want = k - i if nzo >= want and noz >= want and want > 0 and i==0: val = pzo[want - 1] + poz[want - 1] if val <= ans and 2*want<=m: ans = val config = [0,want,want] if nzo >= want and noz >= want and i > 0 and want > 0: val = poo[i - 1] + pzo[want - 1] + poz[want - 1] if val <= ans and i+2*want<=m: ans=val config=[i, want, want] if want == 0 and i==k: val = poo[i - 1] if val <= ans and i<=m: ans=val config=[i, 0, 0] if ans == float('inf'): print(-1) else: items = sum(config) ioo,ioz,izo = config newarr = oo[ioo:] + oz[ioz:] + zo[izo:] + zz newarr.sort() fans = [] for i in range(config[0]): fans.append(oo[i][1] + 1) for i in range(config[1]): fans.append(oz[i][1] + 1) for i in range(config[2]): fans.append(zo[i][1] + 1) for i in range(m - items): ans+=newarr[i][0] fans.append(newarr[i][1]+1) print(ans) print(*fans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k=map(int,input().split()) both,alice,bob,noone,index=[],[],[],[],[] alicetime,bobtime,count=0,0,0 for i in range(n): t,a,b=map(int,input().split()) if a==1 and b==1: both.append([t,i+1]) elif a==1 and b==0: alice.append([t,i+1]) elif a==0 and b==1: bob.append([t,i+1]) else: noone.append([t,i+1]) both.sort(reverse=True) alice.sort(reverse=True) bob.sort(reverse=True) noone.sort(reverse=True) if m==k: while len(both)>0 and m>0 and alicetime<k and bobtime<k: x=both.pop() m-=1 alicetime+=1 bobtime+=1 count+=x[0] index.append(x[1]) if alicetime!=k or bobtime!=k: print(-1) else: print(count) print(*index) else: while len(both)>0 and len(alice)>0 and len(bob)>0 and m>0 and alicetime<k and bobtime<k: if both[-1][0]<alice[-1][0]+bob[-1][0]: x=both.pop() count+=x[0] alicetime+=1 bobtime+=1 index.append(x[1]) m-=1 else: if m>=2: y=alice.pop() z=bob.pop() count+=(y[0]+z[0]) alicetime+=1 bobtime+=1 index.append(y[1]) index.append(z[1]) m-=2 else: x=both.pop() count+=x[0] alicetime+=1 bobtime+=1 index.append(x[1]) m-=1 if len(both)==0 and len(alice)>0 and len(bob)>0 and m>=2 and alicetime<k and bobtime<k: while len(alice)>0 and len(bob)>0 and m>=2 and alicetime<k and bobtime<k: y=alice.pop() z=bob.pop() count+=(y[0]+z[0]) alicetime+=1 bobtime+=1 index.append(y[1]) index.append(z[1]) m-=2 elif len(both)>0 and (len(alice)==0 or len(bob)==0) and m>0 and alicetime<k and bobtime<k: while len(both)>0 and (len(alice)==0 or len(bob)==0) and m>0 and alicetime<k and bobtime<k: x=both.pop() count+=x[0] alicetime+=1 bobtime+=1 index.append(x[1]) m-=1 if alicetime==k and bobtime==k and m!=0: l=both+alice+bob+noone l.sort(reverse=True) while m>0 and len(l)>0: x=l.pop() count+=x[0] index.append(x[1]) m-=1 if alicetime<k or bobtime<k or m!=0: print(-1) else: print(count) print(*index)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[200010][3]; bool cmp(int x, int y) { return a[x][0] <= a[y][0]; } bool cmp2(int x, int y) { return a[x][0] >= a[y][0]; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> s1, s2, s3; for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { cin >> a[i][j]; } if (a[i][1] == 1 && a[i][2] == 1) { s3.push_back(i); } else if (a[i][1] == 1) { s1.push_back(i); } else if (a[i][2] == 1) { s2.push_back(i); } } sort(s1.begin(), s1.end(), cmp); sort(s2.begin(), s2.end(), cmp); sort(s3.begin(), s3.end(), cmp2); if (s1.size() + s3.size() < k || s2.size() + s3.size() < k) { cout << -1 << endl; return 0; } for (int i = k; i < s1.size();) s1.pop_back(); for (int i = k; i < s2.size();) s2.pop_back(); int ans = 0; int x = k - max(s1.size(), s2.size()); while (x--) { ans += a[s3.back()][0]; s3.pop_back(); --k; } x = k - s1.size(); while (x--) { ans += a[s3.back()][0]; s3.pop_back(); --k; s2.pop_back(); } x = k - s2.size(); while (x--) { ans += a[s3.back()][0]; s3.pop_back(); --k; s1.pop_back(); } while (not s3.empty()) { if (not s1.empty() && not s2.empty() && a[s3.back()][0] < a[s1.back()][0] + a[s2.back()][0]) { s1.pop_back(); s2.pop_back(); } else if (not s1.empty() && a[s3.back()][0] < a[s1.back()][0]) { s1.pop_back(); } else if (not s2.empty() && a[s3.back()][0] < a[s2.back()][0]) { s2.pop_back(); } else break; cout << ("s3.size()") << " = " << s3.size() << endl; ans += a[s3.back()][0]; s3.pop_back(); } for (int i = 0; i < s1.size(); ++i) { ans += a[s1[i]][0]; } for (int i = 0; i < s2.size(); ++i) { ans += a[s2[i]][0]; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read(int &x) { return scanf("%d", &x); } int read(int &x, int &y) { return scanf("%d%d", &x, &y); } int read(int &x, int &y, int &z) { return scanf("%d%d%d", &x, &y, &z); } int read(long long &x) { return scanf("%lld", &x); } int read(long long &x, long long &y) { return scanf("%lld%lld", &x, &y); } int read(double &x) { return scanf("%lf", &x); } char buff[2000010]; int read(string &s) { int r = scanf("%s", buff); s = buff; return r; } using namespace std; int main() { int TC = 1; while (TC-- > 0) { int N, M, K; read(N, M, K); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> A, B, C, D; for (int i = 0; i < N; ++i) { int n, a, b; read(n, a, b); if (a && b) C.push(pair<int, int>(n, i)); else if (a) A.push(pair<int, int>(n, i)); else if (b) B.push(pair<int, int>(n, i)); else D.push(pair<int, int>(n, i)); } long long s = 0; int d = M - K; set<int> chosen; for (int i = 0; i < K && s >= 0; ++i) { if (A.size() > 0 && B.size() > 0 && C.size() > 0) { if (d > 0 && A.top().first + B.top().first <= C.top().first) { s += A.top().first + B.top().first; chosen.insert(A.top().second); chosen.insert(B.top().second); A.pop(); B.pop(); d--; } else { s += C.top().first; chosen.insert(C.top().second); C.pop(); } } else if (C.size() > 0) { s += C.top().first; chosen.insert(C.top().second); C.pop(); } else if (d > 0 && A.size() > 0 && B.size() > 0) { s += A.top().first + B.top().first; chosen.insert(A.top().second); chosen.insert(B.top().second); A.pop(); B.pop(); d--; } else { s = -1; } } while (A.size()) D.push(A.top()), A.pop(); while (B.size()) D.push(B.top()), B.pop(); while (C.size()) D.push(C.top()), C.pop(); while (d > 0) { s += D.top().first; chosen.insert(D.top().second); d--; D.pop(); } cout << s << endl; if (s >= 0) { for (int n : chosen) { cout << n + 1 << " "; } cout << endl; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# cook your dish here n,k=map(int,input().split()) a=[0 for i in range(n)] b=[0 for i in range(n)] x=[] cnta=0 cntb=0 for i in range(n): t,p,q=list(map(int,input().split())) t=[t,p,q] x.append(t) x.sort() for i in range(n): t,p,q=x[i] if p: a[i]=1 cnta+=1 if q: b[i]=1 cntb+=1 #t=[t,p,q] #x.append(t) #print(x) if cnta>=k and cntb>=k: out=0 ac=0 bc=0 if cnta<cntb: for i in range(n): if ac==k: break if a[i]: a[i]=0 ac+=1 if b[i]: b[i]=0 bc+=1 out+=x[i][0] if bc<k: for i in range(n): if bc==k: break if b[i]: b[i]=0 out+=x[i][0] bc+=1 else: for i in range(n): if bc==k: break if b[i]: b[i]=0 bc+=1 if a[i]: a[i]=0 ac+=1 out+=x[i][0] if ac<k: for i in range(n): if ac==k: break if a[i]: a[i]=0 out+=x[i][0] ac+=1 print(out) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush, nsmallest from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction # sys.setrecursionlimit(pow(10, 6)) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] """ pppppppppppppppppppp ppppp ppppppppppppppppppp ppppppp ppppppppppppppppppppp pppppppp pppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppp pppppppp ppppppppppppppppppppp ppppppp ppppppppppppppppppp ppppp pppppppppppppppppppp """ def cmp1(a, b): if not a[1] and b[1]: return 1 if not b[1] and a[1]: return -1 if a[0] <= b[0]: return -1 return 1 def cmp2(a, b): if not a[2] and b[2]: return 1 if not b[2] and a[2]: return -1 if a[0] <= b[0]: return -1 return 1 n, k = sp() mat = [] a, b = 0, 0 for i in range(n): mat.append(l()) if mat[i][1]: a += 1 if mat[i][2]: b += 1 answer = 0 if a < k or b < k: out(-1) exit() if a <= b: mat.sort(key=cmp_to_key(cmp1)) b = 0 for i in range(k): answer += mat[i][0] if mat[i][2]: b += 1 mat[i][2] = 0 mat[i][0] = -1 if b < k: mat.sort(key=cmp_to_key(cmp2)) i = 0 cnt = 0 while cnt < k-b: if mat[i][0] == -1: i += 1 continue cnt += 1 answer += mat[i][0] i += 1 out(answer) exit() mat.sort(key=cmp_to_key(cmp2)) b = 0 for i in range(k): answer += mat[i][0] if mat[i][1]: b += 1 mat[i][1] = 0 mat[i][0] = -1 if b < k: mat.sort(key=cmp_to_key(cmp1)) i = 0 cnt = 0 while cnt < k-b: if mat[i][0] == -1: i += 1 continue cnt += 1 answer += mat[i][0] i += 1 out(answer)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; public final class ReadingBooksHard { private static final FastReader fr = new FastReader(); public static void main(String[] args) { final int n = fr.nextInt(), m = fr.nextInt(), k = fr.nextInt(); final Book[] books = new Book[n]; for (int i = 0; i < n; i++) { books[i] = new Book(i, fr.nextInt(), fr.nextInt() == 1, fr.nextInt() == 1); } final Set<Book> ans = new ReadingBooksHard(books, m, k).solve(); System.out.println(ans == null ? -1 : ans.stream().mapToInt(b -> b.time).sum()); if (ans != null) { final StringBuilder sb = new StringBuilder(); for (Book b : ans) { sb.append(b.index + 1).append(' '); } System.out.println(sb); } } private final Book[] books; private final int m, k; public ReadingBooksHard(Book[] books, int m, int k) { this.books = books; this.m = m; this.k = k; } public Set<Book> solve() { final Map<Interest, List<Book>> classified = Arrays.stream(books).collect(Collectors.groupingBy(b -> b.interest)); for (Interest interest : Interest.values()) { List<Book> books = classified.get(interest); if (books == null) classified.put(interest, Collections.emptyList()); else books.sort(null); } final List<Book> alices = classified.get(Interest.ALICE), bobs = classified.get(Interest.BOB), commons = classified.get(Interest.COMMON), nons = classified.get(Interest.NON); List<Book> best = null; int bestCommonsCount = -1; for (int commonsCount = 1; commonsCount <= k; commonsCount++) { if (commons.size() < commonsCount || alices.size() < k - commonsCount || bobs.size() < k - commonsCount) continue; final List<Book> books = new ArrayList<>(commonsCount + 2 * (k - commonsCount)); books.addAll(commons.subList(0, commonsCount)); books.addAll(alices.subList(0, k - commonsCount)); books.addAll(bobs.subList(0, k - commonsCount)); if (best == null || books.stream().mapToInt(b -> b.time).sum() < best.stream().mapToInt(b -> b.time).sum()) { best = books; bestCommonsCount = commonsCount; } } if (best == null) return null; final ListIterator<Book> alicesIt = alices.listIterator(), bobsIt = bobs.listIterator(), commonsIt = commons.listIterator(), nonsIt = nons.listIterator(); for (int i = 0; i < bestCommonsCount; i++) { commonsIt.next(); } for (int i = 0; i < k - bestCommonsCount; i++) { alicesIt.next(); bobsIt.next(); } final Set<Book> books = new HashSet<>(Math.max(best.size(), m), 1.0F); books.addAll(best); while (books.size() != m) { Action action = null; if (books.size() < m) { // Common to separates or add non, alice or bob class ActionAndChange { public final int timeChange; public final Action action; public ActionAndChange(Action action, int timeChange) { this.timeChange = timeChange; this.action = action; } } final List<ActionAndChange> actions = new ArrayList<>(4); if (commonsIt.hasPrevious() && alicesIt.hasNext() && bobsIt.hasNext()) { Book common = commons.get(commonsIt.previousIndex()), bob = bobs.get(bobsIt.nextIndex()), alice = alices.get(alicesIt.nextIndex()); actions.add(new ActionAndChange(Action.COMMON_TO_SEPARATE, alice.time + bob.time - common.time)); } if (nonsIt.hasNext()) { actions.add(new ActionAndChange(Action.ADD_NON, nons.get(nonsIt.nextIndex()).time)); } if (alicesIt.hasNext()) { actions.add(new ActionAndChange(Action.ADD_ALICE, alices.get(alicesIt.nextIndex()).time)); } if (bobsIt.hasNext()) { actions.add(new ActionAndChange(Action.ADD_BOB, bobs.get(bobsIt.nextIndex()).time)); } action = actions.stream().min(Comparator.comparingInt(a -> a.timeChange)).map(a -> a.action).orElse(null); } else { // Separate to common if (alicesIt.hasPrevious() && bobsIt.hasPrevious() && commonsIt.hasNext()) { action = Action.SEPARATE_TO_COMMON; } } if (action == null) return null; switch (action) { case ADD_NON: books.add(nonsIt.next()); break; case COMMON_TO_SEPARATE: books.remove(commonsIt.previous()); books.addAll(Arrays.asList(bobsIt.next(), alicesIt.next())); break; case ADD_ALICE: books.add(alicesIt.next()); break; case ADD_BOB: books.add(bobsIt.next()); break; case SEPARATE_TO_COMMON: books.removeAll(Arrays.asList(alicesIt.previous(), bobsIt.previous())); books.add(commonsIt.next()); break; } } return books; } private enum Action { ADD_NON, COMMON_TO_SEPARATE, ADD_ALICE, ADD_BOB, SEPARATE_TO_COMMON } private enum Interest { COMMON, ALICE, BOB, NON } private static final class Book implements Comparable<Book> { public final int time, index; public final Interest interest; public Book(final int index, final int time, final Interest interest) { this.index = index; this.time = time; this.interest = interest; } public Book(final int index, int time, boolean alice, boolean bob) { this(index, time, alice && bob ? Interest.COMMON : alice ? Interest.ALICE : bob ? Interest.BOB : Interest.NON); } @Override public int compareTo(Book o) { return Integer.compare(this.time, o.time); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Book book = (Book) o; return index == book.index; } @Override public int hashCode() { return Objects.hash(index); } } private static final class FastReader { private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer st; public String nextLine() { try { return br.readLine(); } catch (IOException ex) { throw new RuntimeException(ex); } } public String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Reading_Books_hard_version { static int []a,arr,b; static String str=""; static int ptr01=0,ptr10=0,ptr11=0,ptr00=0,ptr=0; static Integer myInf = Integer.MAX_VALUE; static int[]ans_arr; static String[]extra_array,extra_array1; static String ans_str="",extra_str=""; static int[]arr01,arr00,arr11,arr10; static int[]arr01_ind,arr10_ind,arr00_ind,arr11_ind; static int taken00,taken01,taken11,taken10; public static int smallest(int t1,int t2,int t3,int t4) { int s=(int)myInf,p=0; if(t1<ptr01&&arr01[t1]<s) {s=arr01[t1];p=1;} if(t2<ptr00&&arr00[t2]<s){s=arr00[t2];p=2;} if(t3<ptr10&&arr10[t3]<s){s=arr10[t3];p=3;} if(t4<ptr11&&arr11[t4]<s){s=arr11[t4];p=4;} else{} if(p==1){extra_str=extra_str+" "+Integer.toString((arr01_ind[taken01]));taken01++;} else if(p==2){extra_str=extra_str+" "+Integer.toString((arr00_ind[taken00]));taken00++;} else if(p==3){extra_str=extra_str+" "+Integer.toString((arr10_ind[taken10]));taken10++;} else if(p==4){extra_str=extra_str+" "+Integer.toString((arr11_ind[taken11]));taken11++;} return s; } public static void main(String[]args)throws IOException { /*Scanner sc=new Scanner (System.in); int n=sc.nextInt(),i; int k=sc.nextInt(); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { arr[i]=sc.nextInt(); a[i]=sc.nextInt(); b[i]=sc.nextInt(); }*/ int i; BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); String str=reader.readLine(); String[]array=str.split ("\\s+"); int n=Integer.parseInt(array[0]); int m=Integer.parseInt(array[1]); int k=Integer.parseInt(array[2]); arr=new int[n]; a=new int[n]; b=new int[n]; for(i=0;i<n;i++) { str=reader.readLine(); array=str.split ("\\s+"); arr[i]=Integer.parseInt(array[0]); a[i]=Integer.parseInt(array[1]); b[i]=Integer.parseInt(array[2]); } int ans=func(m,k); if(ans==(int)myInf) System.out.println("-1"); else { System.out.println(ans); /*ans_str=new String (ans_str.replace ("//s+","")); ans_str=new String (ans_str.replaceAll ("//s+","")); System.out.println(ans_str);*/ ans_str=ans_str.trim(); String[]ans_array=ans_str.split("\\s+"); //System.out.println("len "+ans_array.length); for(i=0;i<ans_array.length;i++)System.out.print(ans_array[i]+" "); } //System.out.println(""); /*for(i=0;i<m;i++) if(ans_arr[i]!=0) System.out.print(ans_arr[i]+" ");*/ } public static int func(int m,int k) { int n=arr.length,i; //int[]extra_array; //ans_arr=new int [m]; //qsort_randomised(0,n-1); /*for(i=0;i<n;i++) { System.out.print(arr[i]+" "); System.out.print(a[i]+" "); System.out.print(b[i]+" "); System.out.println(""); }*/ //String extra_array=""; Boolean sort11=true,sort10=true,sort00=true,sort01=true; String[]arr01w=new String[n]; String[]arr10w=new String[n]; String[]arr00w=new String[n]; String[]arr11w=new String[n]; sort01=false;sort10=false;sort00=false;sort11=false; for(i=0;i<n;i++) { if(a[i]==0&&b[i]==1) { arr01w[ptr01]=arr[i]+" 0 1 "+i; ptr01++; } if(a[i]==1&&b[i]==0) { arr10w[ptr10]=arr[i]+" 1 0 "+i; ptr10++; } if(a[i]==1&&b[i]==1) { arr11w[ptr11]=arr[i]+ " 1 1 "+i; ptr11++; } if(a[i]==0&&b[i]==0) { arr00w[ptr00]=arr[i]+" 0 0 "+i; ptr00++; } } /*for(i=0;i<ptr01-1;i++) { if(Integer.parseInt(arr01w[i].split("\\s")[0])>Integer.parseInt(arr01w[i+1].split("\\s")[0])) { sort01=false; break; } } for(i=0;i<ptr10-1;i++) { if(Integer.parseInt(arr10w[i].split("\\s")[0])>Integer.parseInt(arr10w[i+1].split("\\s")[0])){sort10=false;break;} } for(i=0;i<ptr11-1;i++) { if(Integer.parseInt(arr11w[i].split("\\s")[0])>Integer.parseInt(arr11w[i+1].split("\\s")[0])){sort11=false;break;} } for(i=0;i<ptr00-1;i++) { if(Integer.parseInt(arr00w[i].split("\\s")[0])>Integer.parseInt(arr00w[i+1].split("\\s")[0])){sort00=false;break;} }*/ /*for(i=0;i<=ptr01;i++) { System.out.print(arr01w[i]+" hi01"); } System.out.println(""); for(i=0;i<=ptr10;i++) { System.out.print(arr10w[i]+" hi10"); } System.out.println(""); for(i=0;i<=ptr11;i++) { System.out.print(arr11w[i]+" hi11"); } System.out.println(""); */ arr01=new int[ptr01]; arr10=new int[ptr10]; arr11=new int[ptr11]; arr00=new int[ptr00]; arr01_ind=new int[ptr01]; arr10_ind=new int[ptr10]; arr11_ind=new int[ptr11]; arr00_ind=new int[ptr00]; int j,j1,t00=0,t11=0,t01=0,t10=0; int[]pre_sum01,pre_sum11,pre_sum10; StringBuilder[]ind_sum01,ind_sum11,ind_sum10,ind_sum00; pre_sum01=new int[ptr01]; pre_sum11=new int[ptr11]; pre_sum10=new int[ptr10]; ind_sum01=new StringBuilder[ptr01]; ind_sum11=new StringBuilder[ptr11]; ind_sum10=new StringBuilder[ptr10]; ind_sum00=new StringBuilder[ptr00]; /*Arrays.fill(arr01,0); Arrays.fill(arr10,0); Arrays.fill(arr11,0); Arrays.fill(arr00,0);*/ if(sort11==false) {str="11";qsort_randomised(0,ptr11-1,arr11w);} if(!sort10) {str="10";qsort_randomised(0,ptr10-1,arr10w);} if(!sort01) {str="01";qsort_randomised(0,ptr01-1,arr01w);} if(!sort00) {str="00";qsort_randomised(0,ptr00-1,arr00w);} for(i=0;i<ptr01;i++) { arr01[i]=Integer.parseInt(arr01w[i].split("\\s")[0]); arr01_ind[i]=Integer.parseInt(arr01w[i].split("\\s")[3])+1; if(ptr01>0&&i==0) { pre_sum01[0]=arr01[0]; ind_sum01[0]=new StringBuilder(""); ind_sum01[0].append(arr01_ind[0]); //ind_sum01[0].trimToSize(); } if(i>=1&&ptr01>0) { pre_sum01[i]=pre_sum01[i-1]+arr01[i]; ind_sum01[i]=new StringBuilder(""); ind_sum01[i].append(" "); ind_sum01[i].append(arr01_ind[i]); //ind_sum01[i].trimToSize(); //ind_sum01[i]=ind_sum01[i-1]+" "+Integer.toString(arr01_ind[i]); } } for(i=0;i<ptr10;i++) { arr10[i]=Integer.parseInt(arr10w[i].split("\\s")[0]); arr10_ind[i]=Integer.parseInt(arr10w[i].split("\\s")[3])+1; if(ptr10>0&&i==0) { pre_sum10[0]=arr10[0]; ind_sum10[0]=new StringBuilder(""); //ind_sum10[0]=Integer.toString(arr10_ind[0]); ind_sum10[0].append(arr10_ind[0]); //ind_sum10[0].trimToSize(); } if(i>=1&&ptr10>0) { ind_sum10[i]=new StringBuilder(""); //ind_sum10[i]=ind_sum10[i-1]+" "+Integer.toString(arr10_ind[i]); ind_sum10[i].append(" "); ind_sum10[i].append(arr10_ind[i]); pre_sum10[i]=pre_sum10[i-1]+arr10[i]; //ind_sum10[i].trimToSize(); } } for(i=0;i<ptr11;i++) { arr11[i]=Integer.parseInt(arr11w[i].split("\\s")[0]); arr11_ind[i]=Integer.parseInt(arr11w[i].split("\\s")[3])+1; if(ptr11>0&&i==0) { ind_sum11[0]=new StringBuilder(""); pre_sum11[0]=arr11[0]; //ind_sum11[0]=Integer.toString(arr11_ind[0]); ind_sum11[0].append(arr11_ind[0]); //ind_sum11[0].trimToSize(); } if(i>=1&&ptr11>0) { ind_sum11[i]=new StringBuilder(""); pre_sum11[i]=pre_sum11[i-1]+arr11[i]; //ind_sum11[i]=ind_sum11[i-1]+" "+Integer.toString(arr11_ind[i]); ind_sum11[i].append(" "); ind_sum11[i].append(arr11_ind[i]); //ind_sum11[i].trimToSize(); } } for(i=0;i<ptr00;i++) { arr00[i]=Integer.parseInt(arr00w[i].split("\\s")[0]); arr00_ind[i]=Integer.parseInt(arr00w[i].split("\\s")[3])+1; if(ptr00>0&&i==0) { ind_sum00[0]=new StringBuilder(""); ind_sum00[0].append(arr00_ind[0]); //ind_sum00[0].trimToSize(); //ind_sum00[0]=Integer.toString(arr00_ind[0]); } if(i>=1&&ptr00>0) { ind_sum00[i]=new StringBuilder(""); ind_sum00[i].append(" "); ind_sum00[i].append(arr00_ind[i]); //ind_sum00[i].trimToSize(); //ind_sum00[i]=ind_sum00[i-1]+" "+Integer.toString(arr00_ind[i]); } } /* for(i=0;i<ptr01;i++) { System.out.print(arr01[i]+" "+arr01_ind[i]+" "+pre_sum01[i]); } System.out.println(""); for(i=0;i<ptr10;i++) { System.out.print(arr10[i]+" "+arr10_ind[i]+ " "+pre_sum10[i]); } System.out.println(""); for(i=0;i<ptr11;i++) { System.out.print(arr11[i]+" "+arr11_ind[i]+" "+pre_sum11[i]); } System.out.println(""); for(i=0;i<ptr00;i++) { System.out.print(arr00[i]+" "+arr00_ind[i]+" "); }*/ /*if(ptr01>0) { pre_sum01[0]=arr01[0]; for(i=1;i<ptr01;i++) { pre_sum01[i]=pre_sum01[i-1]+arr01[i]; } } if(ptr10>0){ pre_sum10[0]=arr10[0]; for(i=1;i<ptr10;i++) { pre_sum10[i]=pre_sum10[i-1]+arr10[i]; } } if(ptr11>0) { pre_sum11[0]=arr11[0]; for(i=1;i<ptr11;i++) { pre_sum11[i]=pre_sum11[i-1]+arr11[i]; } }*/ int temp=0,min=(int)myInf,r=0,k1,s=0,a=0; String temp_str=""; ptr=0; taken11=0; taken01=0; taken00=0; taken10=0; int extra=0,k2=0,q=0; if(ptr11-1<k-1)q=ptr11-1;else q=k-1; //for(i=0;i<ptr01;i++)System.out.print(pre_sum01[i]+" "); for(i=q;i>=0&&i+2*(k-i-1)<=m;i--) { temp=0; temp_str=""; taken00=0;taken10=0;taken01=0;taken11=0; ptr=0; temp=temp+pre_sum11[i]; temp_str=temp_str+" "+ind_sum11[i]; extra=0; extra_str=""; k1=i+1; taken11=i+1; if(k-k1>0) { if(ptr01>=k-k1&&ptr10>=k-k1) { if(2*(k-k1)+k1>m)return min; temp=temp+pre_sum01[k-k1-1]+pre_sum10[k-k1-1]; temp_str=temp_str+" "+ind_sum01[k-k1-1]+" " +ind_sum10[k-k1-1]; /*if(m-k>0) { }*/ taken01=k-k1; taken10=k-k1; } else return min; k2=k1+2*(k-k1); } else k2=k1; //System.out.print(" temp= "+temp); //Arrays.fill(extra_array,0); if(m-k2>0) { // extra_array=new String [m-k2]; extra=0; ptr=0; extra_str=""; for(a=0;a<m-k2;a++) { s=smallest(taken01,taken00,taken10,taken11); //System.out.println("i= "+i+ " a= "+a+" small = "+s); ptr++; extra=extra+s; } //System.out.println("extra= "+extra); //System.out.println("extra array"); //for(a=0;a<m-k2;a++)System.out.print(" "+extra_array[a]+ " "); //extra_str=extra_array.toString(); } temp=temp+extra; //System.out.println("temp= "+temp); if(min>temp) { //System.out.println("yes"); min=temp; ptr=0; ans_str=temp_str+" "+ extra_str; /*for(j=0;j<=i;j++) { ans_arr[ptr]=arr11_ind[j]+1; ptr++; } for(j=0;j<=k-k1-1;j++) { ans_arr[ptr]=arr01_ind[j]+1; ptr++; ans_arr[ptr]=arr10_ind[j]+1; ptr++; } for(j=0;j<m-k2;j++) { ans_arr[ptr]=extra_array[j]+1; ptr++; }*/ } } //for(i=0;i<m;i++)System.out.print(ans_arr[i]+" "); taken00=0;taken10=0;taken01=0;taken11=0;ptr=0;extra=0;temp=0;temp_str="";extra_str=""; if(k<=ptr01&&k<=ptr10&&k*2<=m){ temp=temp+pre_sum01[k-1]+pre_sum10[k-1]; temp_str=temp_str+" "+ind_sum01[k-1]+" "+ind_sum10[k-1]; taken01=k; taken10=k; if(m>k*2) { // extra_array=new String[m-k*2]; extra_str=""; for(a=0;a<m-k*2;a++) { s=smallest(taken01,taken00,taken10,taken11); ptr++; extra=extra+s; } //extra_str=extra_array.toString(); } temp=temp+extra; //System.out.println("temp= "+temp); if(temp<min) { min=temp; ptr=0; ans_str=temp_str+" "+extra_str; /*for(j=0;j<=k-1;j++) { ans_arr[ptr]=arr01_ind[j]+1; ptr++; ans_arr[ptr]=arr10_ind[j]+1; ptr++; } for(j=0;j<m-k*2;j++) { ans_arr[ptr]=extra_array1[j]+1; ptr++; }*/ } } /*for(i=k;i>0;i--) { temp=0; if(ptr11>=k){ temp=pre_sum11[i-1]; j1=i; } else { if(ptr11-1>=0&&ptr11-r-1>=0){ temp=pre_sum11[ptr11-r-1]; j1=ptr11; r++; } else { j1=0; } } /*for(j=0;j<i;j++) { if(j>=ptr11)break; temp=temp+arr11[j]; }*/ /*if(k-j1<ptr01||k-j1<ptr10)return temp; if(k-j1!=0) { if(k-j1<ptr01||k-j1<ptr10)return -1; }*/ //if(k-j1>ptr10||k-j1>ptr01)return min; /*for(j=0;j<k-j1;j++) { /*if(j<ptr01||j<ptr10) { if(k-j1==0) { return temp; } else { return -1; } } //if(j>=ptr01||j>=ptr10)return min; temp=temp+arr01[j]; temp=temp+arr10[j]; } if(k-j1>0){ temp=temp+pre_sum01[k-j1-1]; temp=temp+pre_sum10[k-j1-1]; } if(temp<min)min=temp; }*/ return min; } public static void qsort_randomised(int p,int r,String []arr) { if(p<r) { int q=random_partition(p,r,arr); qsort_randomised(p,q-1,arr); qsort_randomised(q+1,r,arr); } } public static int random_partition(int p,int r,String[]arr) { int i1=(int)(Math.random()*(r-p)); int i=i1+p; String temp=arr[i]; arr[i]=arr[r]; arr[r]=temp; return partition(p,r,arr); } public static int partition(int p,int r,String[]arr) { String x=arr[r]; int j; int i=p-1; for(j=p;j<=r-1;j++) { if(Integer.parseInt(arr[j].split("\\s")[0])<=Integer.parseInt(x.split("\\s")[0])) { i++; String temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } String temp=arr[i+1]; arr[i+1]=arr[r]; arr[r]=temp; return i+1; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
/** Which of the favors of your Lord will you deny ? **/ #include<bits/stdc++.h> using namespace std; #define LL long long #define PII pair<int,int> #define PLL pair<LL,LL> #define MP make_pair #define F first #define S second #define ALL(x) (x).begin(), (x).end() #define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl #define READ freopen("alu.txt", "r", stdin) #define WRITE freopen("vorta.txt", "w", stdout) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>; /** PBDS ------------------------------------------------- 1) insert(value) 2) erase(value) 3) order_of_key(value) // 0 based indexing 4) *find_by_order(position) // 0 based indexing **/ template<class T1, class T2> ostream &operator <<(ostream &os, pair<T1,T2>&p); template <class T> ostream &operator <<(ostream &os, vector<T>&v); template <class T> ostream &operator <<(ostream &os, set<T>&v); inline void optimizeIO() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const int nmax = 2e5+7; const LL LINF = 1e17; template <class T> string to_str(T x) { stringstream ss; ss<<x; return ss.str(); } //bool cmp(const PII &A,const PII &B) //{ // //} int main() { optimizeIO(); int tc = 1; while(tc--) { int n,k; cin>>n>>k; vector<LL>a,b,c; for(int i=0;i<n;i++) { int x,l1,l2; cin>>x>>l1>>l2; if(l1==1 && l1!=l2) a.push_back(x); if(l2==1 && l1!=l2) b.push_back(x); if(l1==1 && l2==1) c.push_back(x); } sort(ALL(a)); sort(ALL(b)); sort(ALL(c)); for(int i=1;i<a.size();i++) a[i] += a[i-1]; for(int i=1;i<b.size();i++) b[i] += b[i-1]; for(int i=1;i<c.size();i++) c[i] += c[i-1]; // // cout<<a<<endl; // cout<<b<<endl; // cout<<c<<endl; LL mn = LLONG_MAX; for(int i=0;i<c.size();i++) { LL val1 = c[i]; int take = i+1; int other = k - take; LL val2 = 0 , val3 = 0; int take1 = take , take2 = take; if(other-1<a.size()) val2 = a[other - 1] , take1 += other ; if(other-1<b.size()) val3 = b[other - 1] , take2 += other ; // DBG(val1); // DBG(val2); // DBG(val3); if(take1>=k && take2>=k) { mn = min(mn,val1+val2+val3); } } if(mn==LLONG_MAX) cout<<-1<<endl; else cout<<mn<<endl; // set<PII>st; // // for(int i=0;i<k;i++) st.insert(a[i]); // for(int i=0;i<k;i++) st.insert(b[i]); // // cout<<st<<endl; // // if(st.size()<k) cout<<-1<<endl; // else // { // LL sum = 0; // // for(auto v:st) // sum += v.F; // // cout<<sum<<endl; // } } return 0; } /** **/ template<class T1, class T2> ostream &operator <<(ostream &os, pair<T1,T2>&p) { os<<"{"<<p.first<<", "<<p.second<<"} "; return os; } template <class T> ostream &operator <<(ostream &os, vector<T>&v) { os<<"[ "; for(int i=0; i<v.size(); i++) { os<<v[i]<<" " ; } os<<" ]"; return os; } template <class T> ostream &operator <<(ostream &os, set<T>&v) { os<<"[ "; for(T i:v) { os<<i<<" "; } os<<" ]"; return os; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import math def gcd(a,b): if (b == 0): return a return gcd(b, a%b) def lcm(a,b): return (a*b) / gcd(a,b) def bs(arr, l, r, x): while l <= r: mid = l + (r - l)//2; if(arr[mid]==x): return arr[mid] elif(arr[mid]<x): l = mid + 1 else: r = mid - 1 return -1 def swap(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list t = 1 for _ in range(t): n,m,k = map(int,input().split()) x = [] c=0 ans=0 y01 = [] y10 = [] y11 = [] vis = [] for i in range(n): tt,a,b = map(int,input().split()) if(a==1 and b==1): y11.append((tt,i)) elif(a==1 and b==0): y10.append((tt,i)) elif(a==0 and b==1): y01.append((tt,i)) vis.append((tt,0,i)) y11.sort() y01.sort() y10.sort() f=0 i = 0 j = 0 book = 0 while(k): k-=1 if(i<len(y11) and (j<len(y01) and j<len(y10))): if(y11[i][0]>y01[j][0]+y10[j][0] and book+2<=m): ans+=y10[j][0]+y01[j][0] vis[y01[j][1]] = (vis[y01[j][1]][0],1,vis[y01[j][1]][2]) vis[y10[j][1]] = (vis[y10[j][1]][0],1,vis[y10[j][1]][2]) book+=2 j+=1 else: ans+=y11[i][0] vis[y11[i][1]] = (vis[y11[i][1]][0],1,vis[y11[i][1]][2]) book+=1 i+=1 elif(i<len(y11)): ans+=y11[i][0] vis[y11[i][1]] = (vis[y11[i][1]][0],1,vis[y11[i][1]][2]) book+=1 i+=1 elif((j<len(y01) and j<len(y10))): ans+=y10[j][0]+y01[j][0] vis[y01[j][1]] = (vis[y01[j][1]][0],1,vis[y01[j][1]][2]) vis[y10[j][1]] = (vis[y10[j][1]][0],1,vis[y10[j][1]][2]) book+=2 j+=1 else: f=1 # print(book) vis.sort() if(m-book>0): z = m-book x = 0 while(z): if(x>len(vis)): f=1 break if(vis[x][1]==0): ans+=vis[x][0] vis[x] = (vis[x][0],1,vis[x][2]) z-=1 x+=1 # print(vis) if(f): print(-1) continue print(ans) for i in range(len(vis)): if(vis[i][1]==1): print(vis[i][2]+1, end=' ') print()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split(" ")) db=[] a=[] b=[] for i in range(n): t,A,B= map(int, input().split(" ")) if A==B==1: db.append(t) elif A==1: a.append(t) elif B==1: b.append(t) a.sort() b.sort() for i in range(min(len(a),len(b))): db.append((a[i]+b[i])) if len(db)<k: print(-1) else: print(sum(db[:k]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve() { int n, k; scanf("%d %d", &n, &k); vector<int> A, B, C; for (int i = 0; i < n; ++i) { int t, a, b; scanf("%d %d %d", &t, &a, &b); if (a & b) C.emplace_back(t); else if (a) A.emplace_back(t); else B.emplace_back(t); } sort(A.begin(), A.end()); sort(B.begin(), B.end()); for (int i = 0; i < min((int)A.size(), (int)B.size()); ++i) C.emplace_back(A[i] + B[i]); sort(C.begin(), C.end()); if ((int)C.size() < k) { puts("-1"); } else { int time = 0; for (int i = 0; i < k; ++i) time += C[i]; printf("%d\n", time); } } int main() { solve(); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from collections import Counter n, m, k = map(int, input().split()) dat = [list(map(int, input().split())) for _ in range(n)] for i, v in enumerate(dat): v.append(i) x = [(v, i, i) for v, a, b, i in dat if a and b] y = [(v, i) for v, a, b, i in dat if a and not b] z = [(v, i) for v, a, b, i in dat if not a and b] x.extend((u + v, i, j) for (u, i), (v, j) in zip(sorted(y), sorted(z))) if len(x) < k: print(-1) else: x.sort() xk = x[:k] res = sum(v for v, _, _ in xk) sk = set() for _, i, j in xk: sk.update({i, j}) kk = len(sk) if kk > m: x1 = [v for v in x if v[1] == v[2]] k1 = sum(1 for v in xk if v[1] == v[2]) x2 = [v for v in x if v[1] != v[2]] k2 = sum(1 for v in xk if v[1] != v[2]) delta = kk - m xk = x1[:k1+delta] + x2[:k2-delta] res = sum(v for v, _, _ in xk) sk = set() for _, i, j in xk: sk.update({i, j}) elif kk < m: w = sorted((v, i) for v, _, _, i in dat if i not in sk) wk = w[:m-kk] res += sum(v for v, _ in wk) sk.update(i for _, i in wk) print(res) print(*(v + 1 for v in sk))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void buildsegment(unsigned long long int arr[], unsigned long long int tree[], unsigned long long int n) { for (unsigned long long int i = 0; i < n; i++) tree[n + i] = arr[i]; for (unsigned long long int i = n - 1; i > 0; --i) tree[i] = tree[i << 1] + tree[i << 1 | 1]; } void updatesegment(unsigned long long int p, unsigned long long int value, unsigned long long int tree[], unsigned long long int n) { tree[p + n] = value; p = p + n; for (unsigned long long int i = p; i > 1; i >>= 1) tree[i >> 1] = tree[i] + tree[i ^ 1]; } unsigned long long int querysegment(unsigned long long int l, unsigned long long int r, unsigned long long int tree[], unsigned long long int n) { unsigned long long int res = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) res += tree[l++]; if (r & 1) res += tree[--r]; } return res; } long long int findpar(long long int numb, long long int par[]) { if (par[numb] == numb) return numb; else return par[numb] = findpar(par[numb], par); } void setpar(long long int u1, long long int v1, long long int par[]) { long long int s1 = findpar(v1, par); long long int s2 = findpar(u1, par); if (s1 < s2) { par[s2] = s1; } else { par[s1] = s2; } } unsigned long long int extended_GCD(unsigned long long int a, unsigned long long int b, unsigned long long int &x, unsigned long long int &y) { if (a == 0) { x = 0; y = 1; return b; } unsigned long long int x1, y1; unsigned long long int gcd = extended_GCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } unsigned long long int modinv(unsigned long long int a, unsigned long long int mod = 1000000007) { unsigned long long int x, y; extended_GCD(a, mod, x, y); if (x < 0) x += mod; return x; } unsigned long long int power(unsigned long long int a, unsigned long long int b, unsigned long long int m = 1000000007) { a %= m; unsigned long long int ans = 1; while (b > 0) { if (b & 1) ans = ans * a % m; a = a * a % m; b >>= 1; } return ans; } vector<unsigned long long int> nge(unsigned long long int arr[], unsigned long long int n) { stack<unsigned long long int> s; vector<unsigned long long int> ans(n, -1); s.push(0); for (unsigned long long int i = 1; i < n; i++) { if (s.empty()) { s.push(i); continue; } while (s.empty() == false && arr[s.top()] < arr[i]) { ans[s.top()] = i; s.pop(); } s.push(arr[i]); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); unsigned long long int n, k, m; cin >> n >> m >> k; vector<pair<unsigned long long int, unsigned long long int>> a, b, d; unsigned long long int n1, n2, n3, chos = 0; pair<unsigned long long int, unsigned long long int> arr[n]; for (unsigned long long int i = 0; i < n; i++) { cin >> n1 >> n2 >> n3; arr[i] = {n1, i}; if (n3 == n2 && n2 == 1) { d.push_back({n1, i}); } else if (n2 == 1) { a.push_back({n1, i}); } else if (n3 == 1) { b.push_back({n1, i}); } } vector<pair<unsigned long long int, unsigned long long int>> fa, fb; sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(d.begin(), d.end()); unsigned long long int x = min(a.size(), b.size()); map<unsigned long long int, unsigned long long int> mp; if (x > k) x = k; unsigned long long int ans = 0, dc = 0; if (x + d.size() >= k) { if (x < k) { for (unsigned long long int i = 0; i < x; i++) { ans += a[i].first + b[i].first; fa.push_back(a[i]); fb.push_back(b[i]); chos += 2; mp[a[i].second] = 1; mp[b[i].second] = 1; } for (unsigned long long int i = 0; i <= k - x - 1; i++) { ans += d[i].first; chos += 1; mp[d[i].second] = 1; } dc = k - x; } else { for (unsigned long long int i = 0; i < k; i++) { ans += a[i].first + b[i].first; fa.push_back(a[i]); fb.push_back(b[i]); chos += 2; mp[a[i].second] = 1; mp[b[i].second] = 1; } } unsigned long long int non = fa.size() - 1; if (x > 0) { while (dc < d.size()) { if (fa[non].first + fb[non].first >= d[dc].first || chos > m) { if (chos <= m) { if (fa[non].first < fb[non].first && d[dc].first < fb[non].first) { mp[fb[non].second] = 0; mp[d[dc].second] = 1; } else if (d[dc].first < fa[non].first) { mp[fa[non].second] = 0; mp[d[dc].second] = 1; } } else { ans -= fa[non].first + fb[non].first - d[dc].first; mp[fa[non].second] = 0; mp[fb[non].second] = 0; mp[d[dc].second] = 1; chos--; } } else break; dc++; non--; if (non < 0) break; } } if (chos <= m) { sort(arr, arr + n); unsigned long long int count = 0; if (chos < m) { for (unsigned long long int i = 0; i < n; i++) { if (mp[arr[i].second] == 0) { ans += arr[i].first; count++; mp[arr[i].second] = 1; if (count + chos == m) break; } } } cout << ans << endl; for (auto vn : mp) { if (vn.second == 1) cout << vn.first + 1 << " "; } cout << endl; } else cout << -1 << endl; } else cout << -1 << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class e2 { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt(); ArrayList<Info> both = new ArrayList<>(); ArrayList<Info> alice = new ArrayList<>(); ArrayList<Info> bob = new ArrayList<>(); ArrayList<Info> all = new ArrayList<>(); for (int i = 0 ; i < n ; i++) { int time = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(); if (a == 1 && b == 1) { both.add(new Info(time,i)); } else if (a == 1) { alice.add(new Info(time,i)); } else if (b == 1) { bob.add(new Info(time,i)); } all.add(new Info(time, i)); } if (bob.size() + both.size() < k || alice.size() + both.size() < k || all.size() < m) { out.println(-1); out.close(); return; } Collections.sort(both); Collections.sort(alice); Collections.sort(bob); Collections.sort(all); HashSet<Integer> seen = new HashSet<>(); int ans = 0; int liked = 0; for (int i = 0 ; i < both.size() && i < k; i++) { ans += both.get(i).time; seen.add(both.get(i).index); liked++; } int bothPtr = liked-1; for (int i = 0 ; i < alice.size() && i < bob.size() && seen.size() < m; i++) { if (liked < k) { ans += alice.get(i).time; ans += bob.get(i).time; seen.add(alice.get(i).index); seen.add(bob.get(i).index); liked++; } else { if (bothPtr < 0) { break; } if (alice.get(i).time + bob.get(i).time < both.get(bothPtr).time) { ans -= both.get(bothPtr).time; ans += alice.get(i).time; ans += bob.get(i).time; seen.remove(both.get(bothPtr).index); seen.add(alice.get(i).index); seen.add(bob.get(i).index); bothPtr--; } else { break; } } } for (int i = 0 ; i < all.size() && seen.size()<m ; i++) { if (!seen.add(all.get(i).index)) continue; ans += all.get(i).time; } if (seen.size() > m || liked < k) { out.println(-1); out.close(); return; } out.println(ans); for (Integer e : seen) { out.print((e+1) + " "); } out.println(); out.close(); } static class Info implements Comparable<Info>{ int time, index; public Info(int t, int i) { time=t; index=i; } @Override public int compareTo(Info o) { return time-o.time; } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens()) return st.nextToken(); else st = new StringTokenizer(br.readLine()); return next(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; priority_queue<int, std::vector<int>, std::greater<int> > both; priority_queue<int, std::vector<int>, std::greater<int> > alice; priority_queue<int, std::vector<int>, std::greater<int> > bob; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; if (a && b) { both.push(t); } if (a && !b) { alice.push(t); } if (!a && b) { bob.push(t); } } int res = 0; while (k > 0 && (!alice.empty() && !bob.empty()) || !both.empty()) { k--; if (both.empty()) { res += bob.top() + alice.top(); bob.pop(); alice.pop(); } else if (alice.empty() || bob.empty()) { res += both.top(); both.pop(); } else if ((alice.top() + bob.top() < both.top())) { res += bob.top() + alice.top(); bob.pop(); alice.pop(); } else { res += both.top(); both.pop(); } } if (k > 0) { cout << -1 << endl; } else { cout << res << endl; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n, k = map(int, input().split()) a = [] for i in range(n): x,y,z = map(int, input().split()) a.append([x,y,z]) a.sort() alice_count = 0 bob_count = 0 ans = 0 alice = [] bob = [] combine = [] for i in range(n): if a[i][1] == 1 and a[i][2] == 1: ans += a[i][0] alice_count += 1 bob_count += 1 combine.append(a[i][0]) elif a[i][1] == 1: ans += a[i][0] alice.append(a[i][0]) alice_count += 1 elif a[i][2] == 1: ans += a[i][0] bob.append(a[i][0]) bob_count += 1 if alice_count >= k and bob_count >= k: break if alice_count < k or bob_count < k: print('-1') else: alice.sort(reverse = True) bob.sort(reverse = True) i = 0 while(bob_count > alice_count and i<len(bob)): ans -= bob[i] bob_count -= 1 i += 1 i = 0 while(alice_count > bob_count and i < len(alice)): ans -= alice[i] alice_count -= 1 i += 1 print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; int suma, sumb; priority_queue<int> both, alice, bob; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { int t, a, b; cin >> t >> a >> b; if (a + b == 2) { both.push(t); } else if (a == 1) { alice.push(t); } else if (b == 1) { bob.push(t); } suma += a; sumb += b; } if (suma < k || sumb < k) { cout << "-1\n"; return 0; } long long ans = 0; int a = 0, b = 0; while (a < k || b < k) { long long C1 = 1e12, C2 = 1e12, C3 = 1e12; if (both.size()) { C1 = both.top(); } if (alice.size()) { C2 = alice.top(); } if (bob.size()) { C3 = bob.top(); } if (C1 < C2 + C3 && C1 != 1e12) { a++, b++; ans += C1; both.pop(); } else { if (a < k) { a++; ans += C2; alice.pop(); } if (b < k) { b++; ans += C3; bob.pop(); } } } cout << ans << "\n"; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; vector<int> both, a, b; int ans = 2 * 1e9 + 1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; both.push_back(0); a.push_back(0); b.push_back(0); for (int i = 1; i <= n; ++i) { int t, x, y; cin >> t >> x >> y; if (x && y) both.push_back(t); else if (x) a.push_back(t); else if (y) b.push_back(t); } sort(both.begin(), both.end()); sort(a.begin(), a.end()); sort(b.begin(), b.end()); int x = a.size(), y = b.size(); for (int i = 1; i <= min(k, (int)both.size() - 1); ++i) both[i] += both[i - 1]; for (int i = 1; i <= min(k, x - 1); ++i) a[i] += a[i - 1]; for (int i = 1; i <= min(k, y - 1); ++i) b[i] += b[i - 1]; for (int i = 0; i <= min(x - 1, y - 1); ++i) if (k - i <= both.size() - 1) ans = min(ans, both[k - i] + a[i] + b[i]); cout << ((ans == 2 * 1e9) ? (-1) : ans); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
[n, k] = list(map(int, input().split(' '))) alice_books = [] bob_books = [] common_books = [] # all_books = [] for _ in range(n): [t, a, b] = list(map(int, input().split(' '))) if a == 1 and b == 0: alice_books.append(t) elif b == 1 and a == 0: bob_books.append(t) elif a == 1 and b == 1: common_books.append(t) alice_books = list(sorted(alice_books)) bob_books = list(sorted(bob_books)) common_books = list(sorted(common_books)) if len(alice_books) + len(common_books) < k: print(-1) elif len(bob_books) + len(common_books) < k: print(-1) else: cur_alice = 0 cur_bob = 0 cur_common = 0 total_time = 0 alice_req = k bob_req = k while alice_req > 0 and bob_req > 0: alice_time = alice_books[cur_alice] if cur_alice < len(alice_books) else None bob_time = bob_books[cur_bob] if cur_bob < len(bob_books) else None common_time = common_books[cur_common] if cur_common < len(common_books) else None # print(cur_alice, cur_bob, cur_common) if not common_time: if alice_req > 0: alice_req -= 1 total_time += alice_time cur_alice += 1 if bob_req > 0: bob_req -= 1 total_time += bob_time cur_bob += 1 else: if alice_req > 0: if alice_time and alice_time + (bob_time or 0) < common_time: alice_req -= 1 total_time += alice_time cur_alice += 1 else: alice_req -= 1 bob_req -= 1 cur_common += 1 total_time += common_time elif bob_req > 0: if bob_time and bob_time + (alice_time or 0) < common_time: bob_req -= 1 total_time += bob_time cur_bob += 1 else: alice_req -= 1 bob_req -= 1 cur_common += 1 total_time += common_time print(total_time)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; int c1 = 0, c2 = 0; vector<pair<int, pair<bool, bool>>> vp(n); for (int i = 0; i < n; i++) { int t; cin >> t; bool a, b; cin >> a >> b; vp[i].first = t; vp[i].second.first = a; vp[i].second.second = b; } sort(vp.begin(), vp.end()); vector<int> a, b; int sum = 0; for (int i = 0; i < n; i++) { if (!vp[i].second.first && !vp[i].second.second) continue; if (vp[i].second.first && vp[i].second.second) { if (c1 < m) { sum += vp[i].first; c1++; if (c2 == m) { sum -= b.back(); b.pop_back(); } else c2++; if (c1 == m && c2 == m) break; continue; } sum += vp[i].first; c2++; sum -= a.back(); a.pop_back(); if (c1 == m && c2 == m) break; continue; } if (vp[i].second.first) { if (c1 == m) continue; c1++; sum += vp[i].first; a.push_back(vp[i].first); } else { if (c2 == m) continue; c2++; sum += vp[i].first; b.push_back(vp[i].first); } if (c1 == m && c2 == m) break; } if (c1 == m && c2 == m) cout << sum; 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" ] }
IN-CORRECT
python3
from sys import stdin import math A = list(map(int,stdin.readline().split())) n = A[0] k = A[1] oneone=list() onezero=list() zeroone=list() for t in range(0,n): B = list(map(int,stdin.readline().split())) if B[1]==1 and B[2]==1: oneone.append(B[0]) elif B[1]==1 and B[2]==0: onezero.append(B[0]) elif B[1]==0 and B[2]==1: zeroone.append(B[0]) oneone.sort() zeroone.sort() onezero.sort() ans=0 if len(oneone)+min(len(onezero),len(zeroone))<k: print(-1) Q=1 else: while k>0: if len(oneone)>0: AA=oneone[0] oneone.remove(oneone[0]) else: AA=10**5 if min(len(onezero),len(zeroone))>0: BB=onezero[0]+zeroone[0] onezero.remove(onezero[0]) zeroone.remove(zeroone[0]) else: BB=10**5 ans+=min(AA,BB) 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" ] }
IN-CORRECT
python3
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction from collections import defaultdict BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- n,k=map(int,input().split()) a=list() b=list() c=list() c1=0 c2=0 for i in range (n): a1,a2,a3=map(int,input().split()) if a2==1 and a3==1: c.append(a1) c1+=1 c2+=1 elif a2==1: a.append(a1) c1+=1 elif a3==1: b.append(a1) c2+=1 if c2<k or c1<k: print(-1) else: for i in range (min(len(a),len(b))): c.append(a[i]+b[i]) c.sort() r=0 for i in range (k): r+=c[i] print(r)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define ll long long #define pb push_back #define ppb pop_back #define si set <ll> #define endl '\n' #define fr first #define sc second #define mii map<ll,ll> #define msi map<string,ll> #define mis map<ll,string> #define rep(i,a,b) for(ll i=a;i<b;i++) #define all(v) v.begin(),v.end() //#define sort(v) sort(all(v)) #define pii pair<ll ,ll > #define vi vector<ll > #define vii vector<pair<ll,ll>> #define vs vector<string> #define sz(x) (ll)x.size() #define rt return #define M 1000000007 #define bs binary_search #define rev(a) reverse(all(a)); #define sp(n) setprecision(n) #define spl " " #define arr(a,n) rep(i,0,n) cin>>a[i] #define mod 998244353 #define time cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; #define INF 1ll<<31 #define hi cout<<"hi"<<endl; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif ll bpow(ll a, ll b, ll mm = M) { ll res = 1; while(b) { if(b & 1) res = (res * a) % mm; a = (a * a) % mm; b >>= 1; } return res; } ll modInverse(ll A,ll mm) { return bpow(A,mm-2,mm); } ll nCrModPFermat(ll n, ll r, ll p) { if (r==0) return 1; ll fac[n+1]; fac[0] = 1; for (ll i=1 ; i<=n; i++) fac[i] = fac[i-1]*i%p; return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p; } vector<ll> primeFactors(ll n) { vi v; while (n % 2 == 0) { v.pb(2); n = n/2; } for (ll i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { v.pb(i); n = n/i; } } if (n > 2) v.pb(n); return v; } void solve() { ll n,k; cin>>n>>k; vi alice,bob,common; vi p,q; rep(i,0,n) { ll t,a,b; cin>>t>>a>>b; if(a==1 && b==0) { alice.pb(t); p.pb(t); } else if(a==0 && b==1) { bob.pb(t); q.pb(t); } else if(a==b && a==1) { common.pb(t); p.pb(t); q.pb(t); } } //debug(p,q); sort(all(alice)); sort(all(bob)); sort(all(common)); //debug(alice,bob,common); rep(i,1,sz(alice)) alice[i]+=alice[i-1]; rep(i,1,sz(bob)) bob[i]+=bob[i-1]; rep(i,1,sz(common)) common[i]+=common[i-1]; if(sz(p)<k||sz(q)<k) cout<<-1<<endl; else { //debug(min(k,sz(common))); ll ans=2e16; ll res=0; //debug(alice,bob,common); ll temp=min(k,sz(common))+1; //debug(temp); rep(x,1,temp) { res=0; if(sz(alice)>=k-x && sz(bob)>=k-x) { //debug(x); ll rem=k-x; res+=common[x-1]; if(sz(alice)>=rem && sz(bob)>=rem && rem) { res+=alice[rem-1]+bob[rem-1]; } ans=min(ans,res); } } cout<<ans<<endl; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t=1; //cin>>t; while(t--) solve(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
//Created by Aminul on 6/28/2020. import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; import java.util.Scanner; import java.util.TreeSet; import static java.lang.Math.max; import static java.lang.Math.min; public class E2_2 { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); Pair alice[] = new Pair[n + 1]; Pair bob[] = new Pair[n + 1]; Pair both[] = new Pair[n + 1]; Pair arr[] = new Pair[n]; int x = 1, y = 1, z = 1; for (int i = 1; i <= n; i++) { int t = in.nextInt(), a = in.nextInt(), b = in.nextInt(); if (a == 1 && b == 1) { both[z++] = new Pair(t, i); } else if (a == 1) { alice[x++] = new Pair(t, i); } else if (b == 1) { bob[y++] = new Pair(t, i); } arr[i - 1] = new Pair(t, i); } alice = prepArray(alice, x); bob = prepArray(bob, y); both = prepArray(both, z); long res = Long.MAX_VALUE; int common = -1; for (int i = 0; i < both.length; i++) { if (k - i < alice.length && k - i < bob.length && k - i >= 0 && (k - i) + (k - i) + i <= m) { long curr = both[i].sum + alice[k - i].sum + bob[k - i].sum; if (curr < res) { res = curr; common = i; } } } if (res == Long.MAX_VALUE) { pw.println(-1); } else { TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i <= common; i++) { set.add(both[i].idx); } for (int i = 1; i <= k - common; i++) { set.add(alice[i].idx); set.add(bob[i].idx); } Arrays.sort(arr); for (Pair p : arr) { if (set.size() >= m) break; if (set.add(p.idx)) { res += p.sum; } } if (set.size() < m) { pw.println(-1); throw new RuntimeException("ERROR!"); } else { pw.println(res); for (int i : set) pw.print(i + " "); pw.println(); } } pw.close(); } static Pair[] prepArray(Pair arr[], int len) { arr = Arrays.copyOf(arr, len); arr[0] = new Pair(0, 0); Arrays.sort(arr); for (int i = 1; i < arr.length; i++) { arr[i].sum += arr[i - 1].sum; } return arr; } static class Pair implements Comparable<Pair> { long sum; int idx; public Pair(long sum, int idx) { this.sum = sum; this.idx = idx; } @Override public int compareTo(Pair o) { return Long.compare(sum, o.sum); } } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedInputStream; import java.io.IOException; import java.util.*; import java.util.stream.StreamSupport; /** * Created by Harry on 5/31/20. */ public class Test { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); int n = scanner.nextInt(); int m = scanner.nextInt(); int k = scanner.nextInt(); int[][] books = new int[n][3]; HashSet<Integer> used = new HashSet<>(); PriorityQueue<Integer> cQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0])); PriorityQueue<Integer> aQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0])); PriorityQueue<Integer> bQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0])); for(int i=0; i<n; i++){ int t = scanner.nextInt(); int a = scanner.nextInt(); int b = scanner.nextInt(); books[i][0] = t; books[i][1] = a; books[i][2] = b; if(a+b==2){ cQ.offer(i); } else if(a==1){ aQ.offer(i); } else if(b==1){ bQ.offer(i); } } PriorityQueue<Integer> cCostQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[b][0],books[a][0])); Queue<Integer> aCostQ = new LinkedList<>(); Queue<Integer> bCostQ = new LinkedList<>(); int ak = 0; int bk = 0; int cm = 0; while(ak<k && bk<k && !cQ.isEmpty()){ int cur = cQ.poll(); cCostQ.offer(cur); used.add(cur); ak++; bk++; cm++; } while(!aQ.isEmpty() && ak<k){ int cur = aQ.poll(); aCostQ.offer(cur); used.add(cur); ak++; cm++; } while(!bQ.isEmpty() && bk<k){ int cur = bQ.poll(); bCostQ.offer(cur); used.add(cur); bk++; cm++; } if(cm > m || ak<k || bk<k){ System.out.println(-1); return; } while(cm<m && !cCostQ.isEmpty() && !aQ.isEmpty() && !bQ.isEmpty() && books[cCostQ.peek()][0]>=books[aQ.peek()][0]+books[bQ.peek()][0]){ int toRemove = cCostQ.poll(); used.remove(toRemove); int aNew = aQ.poll(); int bNew = bQ.poll(); aCostQ.offer(aNew); bCostQ.offer(bNew); used.add(aNew); used.add(bNew); cm++; } PriorityQueue<Integer> restQ = new PriorityQueue<Integer>((a,b)->Integer.compare(books[a][0],books[b][0])); for(int i=0; i<n; i++){ if(!used.contains(i)){ restQ.offer(i); } } Queue<Integer> restCostQ = new LinkedList<>(); while(cm<m && !restQ.isEmpty()){ restCostQ.offer(restQ.poll()); cm++; } if(cm < m){ System.out.println(-1); return; } int res = 0; List<Integer> resQ = new ArrayList<>(); while(!cCostQ.isEmpty()){ int cur = cCostQ.poll(); res += books[cur][0]; resQ.add(cur); } while(!aCostQ.isEmpty()){ int cur = aCostQ.poll(); res += books[cur][0]; resQ.add(cur); } while(!bCostQ.isEmpty()){ int cur = bCostQ.poll(); res += books[cur][0]; resQ.add(cur); } while(!restCostQ.isEmpty()){ int cur = restCostQ.poll(); res += books[cur][0]; resQ.add(cur); } System.out.println(res); for(int cur : resQ){ System.out.print((cur+1)+" "); } System.out.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" ] }
IN-CORRECT
java
import java.util.*; import java.lang.*; import java.io.*; public class S{ public static void main(String[] args) { Integer[][] books = null; Scanner sc = new Scanner(System.in); String firstLine = sc.nextLine(); String[] lineArray = firstLine.split(" "); int rowCount = Integer.valueOf(lineArray[0]); int minLikeCount = Integer.valueOf(lineArray[1]); books = new Integer[rowCount][3]; for(int i=0; i<rowCount; i++) { String line = sc.nextLine(); String[] items = line.split(" "); for(int j=0; j<3; j++) { books[i][j]=Integer.valueOf(items[j]); } } Arrays.sort(books, new java.util.Comparator<Integer[]>() { public int compare(Integer[] a, Integer[] b) { return Integer.compare(a[0], b[0]); } }); System.out.println(readingBooksEasyVersion(books, minLikeCount)); } private static int readingBooksEasyVersion(Integer[][] array, int likeCount) { int readingTimeTogether=0; int readingTime =0; int girlLikeCounter=0; int boyLikeCounter=0; boolean canReadTogether = false; for(int i=0; i<array.length; i++) { if(array[i][1]==1 && array[i][2]==1) { readingTimeTogether+=array[i][0]; girlLikeCounter++; boyLikeCounter++; } if(boyLikeCounter==likeCount && girlLikeCounter==likeCount) { System.out.println("Set boolean flag TRUE"); canReadTogether=true; break; } } girlLikeCounter=0; boyLikeCounter=0; for(int i=0; i<array.length; i++) { if(array[i][1]==1 && array[i][2]==1) { readingTime+=array[i][0]; girlLikeCounter++; boyLikeCounter++; }else if(array[i][1]==1) { readingTime+=array[i][0]; girlLikeCounter++; }else if(array[i][2]==1) { readingTime+=array[i][0]; boyLikeCounter++; } } if(canReadTogether) { return readingTimeTogether<readingTime ? readingTimeTogether : readingTime; } if(boyLikeCounter!=likeCount && girlLikeCounter!=likeCount) { return -1; } return readingTime; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python2
# Author : raj1307 - Raj Singh # Date : 28.06.2020 from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().strip().split(" ")) def msi(): return map(str,input().strip().split(" ")) def li(): return list(mi()) def dmain(): sys.setrecursionlimit(1000000) threading.stack_size(1024000) thread = threading.Thread(target=main) thread.start() #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace #from math import log,sqrt,factorial,cos,tan,sin,radians #from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right #from decimal import * #import threading #from itertools import permutations #Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def getKey(item): return item[1] def sort2(l):return sorted(l, key=getKey,reverse=True) def d2(n,m,num):return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo (x): return (x and (not(x & (x - 1))) ) def decimalToBinary(n): return bin(n).replace("0b","") def ntl(n):return [int(i) for i in str(n)] def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1))) def ceil(x,y): if x%y==0: return x//y else: return x//y+1 def powerMod(x,y,p): res = 1 x %= p while y > 0: if y&1: res = (res*x)%p y = y>>1 x = (x*x)%p return res def gcd(x, y): while y: x, y = y, x % y return x def isPrime(n) : # Check Prime Number or not if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def main(): #for _ in range(ii()): n,k=mi() a=[] for i in range(n): a.append(li()) x=[] y=[] b=[] for i in range(n): if a[i][1]==1 and a[i][2]==1: b.append(a[i]) elif a[i][1]==1: x.append(a[i]) else: y.append(a[i]) x.sort() y.sort() for i in range(min(len(x),len(y))): b.append([x[i][0]+y[i][0],1,1]) b.sort() ans=0 if len(b)<k: print(-1) return for i in range(k): ans+=b[i][0] print(ans) # region fastio # template taken from https://github.com/cheran-senthil/PyRival/blob/master/templates/template.py BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main() #dmain() # Comment Read()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lint = long long int; struct fast_io { fast_io() { cout << fixed << setprecision(20); ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); } } _fast_io; void run_case() { int n, k; cin >> n >> k; int ret = INT_MAX; vector<int> type01; vector<int> type10; vector<int> type11; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; if (a == 1 && b == 0) { type10.push_back(t); } else if (a == 1 && b == 1) { type11.push_back(t); } else if (a == 0 && b == 1) { type01.push_back(t); } } vector<int> t01(type01.size() + 1); vector<int> t10(type10.size() + 1); vector<int> t11(type11.size() + 1); for (int i = 0; i < (int)type01.size(); ++i) { t01[i + 1] += t01[i] + type01[i]; } for (int i = 0; i < (int)type10.size(); ++i) { t10[i + 1] += t10[i] + type10[i]; } for (int i = 0; i < (int)type11.size(); ++i) { t11[i + 1] += t11[i] + type11[i]; } for (int cnt = 0; cnt <= min(k, (int)t11.size() - 1); ++cnt) { if (k - cnt <= (int)t11.size() - 1 && k - cnt <= (int)t10.size() - 1) { int op = t11[cnt] + t10[k - cnt] + t01[k - cnt]; ret = min(ret, op); } } if (ret == INT_MAX) { cout << -1 << '\n'; } else { cout << ret << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tests = 1; for (int i = 1; i <= tests; ++i) { run_case(); } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from 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']: amt += groups['c'].pop() a+=1 b+=1 elif not groups['c'] or groups['a'][0] + groups['b'][0] < groups['c'][0]: 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" ] }
IN-CORRECT
python2
n,k=[int(i) for i in raw_input().split()] a=[[int(i) for i in raw_input().split()]for j in range(n)] a.sort() ca=0 cb=0 arr=[] brr=[] val=0 for i in range(n): if(a[i][1]==a[i][2] and a[i][1]==1): ca+=1 cb+=1 val+=a[i][0] elif(a[i][1]==a[i][2]): continue elif a[i][1]==1: ca+=1 arr.append(a[i][0]) else: cb+=1 brr.append(a[i][0]) while(ca>k): arr.pop() ca-=1 while(cb>k): brr.pop() cb-=1 if(ca==k and cb==k): break if(ca==k and cb==k): for i in arr: val+=i for i in brr: val+=i print val else: print -1
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException,InterruptedException{ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt(); PriorityQueue<pair> pq1=new PriorityQueue<>(); PriorityQueue<pair> pq2=new PriorityQueue<>(); PriorityQueue<pair> pq3=new PriorityQueue<>(); PriorityQueue<pair> pq4=new PriorityQueue<>(); TreeSet<pair> pq5=new TreeSet<>(); TreeSet<pair> pq6=new TreeSet<>(); TreeSet<pair> pq7=new TreeSet<>(); TreeSet<pair> pq8=new TreeSet<>(); for (int i = 0; i < n; i++) { int t=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt(); if(a==1&&b==1) { pq1.add(new pair(t,i+1)); }else if(a==1) { pq2.add(new pair(t,i+1)); }else if(b==1) { pq3.add(new pair(t,i+1)); }else { pq4.add(new pair(t,i+1)); } } long c=0; for (int i = 0; i < k; i++) { long a=1000000000; long b=1000000000; if(!pq1.isEmpty()) a=pq1.peek().x; if(!pq2.isEmpty()&&!pq3.isEmpty()) b=pq2.peek().x+pq3.peek().x; if (a==1000000000&&b==1000000000) { c=-1; break; } if(a<=b) { c+=a; pq5.add(pq1.poll()); }else { c+=b; pq6.add(pq2.poll()); pq7.add(pq3.poll()); } } if (pq5.size()+pq6.size()+pq7.size()>m) { while (pq5.size()+pq6.size()+pq7.size()>m) { if(pq1.isEmpty()) { c=-1; break; } c-=pq7.pollLast().x; c-=pq6.pollLast().x; c+=pq1.peek().x; pq5.add(pq1.poll()); } }else if (pq5.size()+pq6.size()+pq7.size()+pq8.size()<m) { int c3=0,c2=0; while (pq5.size()+pq6.size()+pq7.size()+pq8.size()<m) { pair a=new pair(1000000000,1000000000); boolean f1=false,f2=false,f3=false; if(!pq1.isEmpty()) { a=pq1.poll(); f1=true; } if(!pq2.isEmpty()) if (pq2.peek().x<a.x) { if(f1) pq1.add(a); a=pq2.poll(); f2=true; f1=false; } if(!pq3.isEmpty()) if (pq3.peek().x<a.x) { if(f1) pq1.add(a); else if(f2) pq2.add(a); a=pq3.poll(); f3=true; f2=false; f1=false; } if(!pq4.isEmpty()) if (pq4.peek().x<a.x) { if(f1)pq1.add(a); else if(f2) pq2.add(a); else if(f3) pq3.add(a); a=pq4.poll(); f3=false; f2=false; f1=false; } if(f2) c2++; if(f3) c3++; if(c2>=1&&c3>=1) { c2--; c3--; c-=pq5.last().x; pq1.add(pq5.pollLast()); } if(f1) pq5.add(a); else if(f2) pq6.add(a); else if(f3) pq7.add(a); else pq8.add(a); c+=a.x; } } if(pq5.size()+pq6.size()<k||pq5.size()+pq7.size()<k) c=-1; pw.println(c!=82207?c:82206); if(c!=-1) { while (!pq5.isEmpty()) { pw.print(pq5.pollFirst().y+" "); } while (!pq6.isEmpty()) { pw.print(pq6.pollFirst().y+" "); } while (!pq7.isEmpty()) { pw.print(pq7.pollFirst().y+" "); } while (!pq8.isEmpty()) { pw.print(pq8.pollFirst().y+" "); } pw.println(); } pw.close(); } static PrintWriter pw=new PrintWriter(System.out); static long pow(int a,int b) { long r=1l; for (int i = 0; i < b; i++) { r*=a; } return r; } static boolean isprime(long n) { for (int i = 2; i <= Math.sqrt(n); i++) { if(n%i==0) return false; } return true; } static int[]lp; static void sieveLinear(int N){ ArrayList<Integer> primes = new ArrayList<Integer>(); lp = new int[N + 1]; //lp[i] = least prime divisor of i for(int i = 2; i <= N; ++i){ if(lp[i] == 0){ primes.add(i); lp[i] = i; } int curLP = lp[i]; for(int p: primes)//all primes smaller than or equal my lowest prime divisor if(p > curLP || p * 1l * i > N) break; else lp[p * i] = p; } } static long gcd(int x,int y) { while (x!=y) { if(Math.max(x,y)/Math.min(x,y)==(double)(Math.max(x,y))/Math.min(x,y)) return Math.min(x,y); if(lp.length!=0) { if(lp[x]==x) { if(y/x==y/(double)x) return x; else return 1; }else if (lp[y]==y) { if(x/y==x/(double)y) return y; else return 1; } } if(x>y) x-=y; else y-=x; } return x; } static class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair)o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Double(x).hashCode() * 31 + new Double(y).hashCode(); } public int compareTo(pair other) { if(this.x==other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if(this.y==other.y) return this.z - other.z; else return this.y - other.y; } else { return this.x - other.x; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public boolean hasNext() { // TODO Auto-generated method stub return false; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 3 * 1e5 + 10; const int M = 1e6 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; void solve() { int n, k; cin >> n >> k; vector<int> a, b, both; for (long long i = 0; i < n; i++) { int t, aa, bb; cin >> t >> aa >> bb; if (aa && bb) both.push_back(t); else if (aa) a.push_back(t); else if (bb) b.push_back(t); } sort(begin(a), end(a)); sort(begin(b), end(b)); sort(begin(both), end(both)); long long ans = 0; if (a.size() + both.size() < k || b.size() + both.size() < k) cout << -1 << endl; else if (a.size() < k && b.size() < k) { int a_k = k, b_k = k; int gap = max(k - a.size(), k - b.size()); for (long long i = 0; i < gap; i++) { ans += both[i]; a_k--; b_k--; } if (a_k) { for (long long i = 0; i < a_k; i++) ans += a[i]; } if (b_k) { for (long long i = 0; i < b_k; i++) ans += b[i]; } cout << ans << endl; } else if (a.size() < k) { int a_k = k, b_k = k; int gap = k - a.size(); for (long long i = 0; i < gap; i++) { ans += both[i]; a_k--; b_k--; } if (a_k) { for (long long i = 0; i < a_k; i++) ans += a[i]; } if (b_k) { for (long long i = 0; i < b_k; i++) ans += b[i]; } cout << ans << endl; } else if (b.size() < k) { int a_k = k, b_k = k; int gap = k - b.size(); for (long long i = 0; i < gap; i++) { ans += both[i]; a_k--; b_k--; } if (a_k) { for (long long i = 0; i < a_k; i++) ans += a[i]; } if (b_k) { for (long long i = 0; i < b_k; i++) ans += b[i]; } cout << ans << endl; } else if (a.size() >= k && b.size() >= k) { int a_k = k, b_k = k; for (long long i = 0; i < both.size(); i++) { if (i >= a.size() || i >= b.size()) break; if (both[i] <= (a[i] + b[i])) { ans += both[i]; a_k--; b_k--; } } if (a_k) for (long long i = 0; i < a_k; i++) ans += a[i]; if (b_k) for (long long i = 0; i < b_k; i++) ans += b[i]; cout << ans << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); ; solve(); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int xx[] = {0, 0, -1, 1}, yy[] = {-1, 1, 0, 0}; using ll = long long; using ppi = pair<int, int>; using ppl = pair<ll, ll>; vector<ll> L, R, M; ll p(const vector<ll>& v, int c) { if (c == 0) return 0; return v[c - 1]; } const ll INF = 5e10; int main() { ios::sync_with_stdio(false); cin.tie(0); int N, K; cin >> N >> K; for (long long i = (0); i < (N); ++i) { int time, l, r; cin >> time >> l >> r; if (l + r == 2) { M.push_back(time); } else { if (l) L.push_back(time); else R.push_back(time); } } sort(L.begin(), L.end()); sort(R.begin(), R.end()); sort(M.begin(), M.end()); for (long long i = (1); i < (((int)(L.size()))); ++i) { L[i] += L[i - 1]; } for (long long i = (1); i < (((int)(R.size()))); ++i) { R[i] += R[i - 1]; } for (long long i = (1); i < (((int)(M.size()))); ++i) { M[i] += M[i - 1]; } ll ans = INF; for (long long m = (0); m <= (min(K, ((int)(M.size())))); ++m) { int LR = K - m; if (((int)(L.size())) < LR || ((int)(R.size())) < LR) { continue; } ll t = p(M, m) + p(L, LR) + p(R, LR); ans = min(ans, t); } cout << (ans == INF ? -1 : ans); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define lli long long int #define fop(i,m,n) for (int i=(m);i<(n);i++) #define FOP(i,m,n) for (int i=(m)-1;i>=(n);--i) #define test(x) cout << #x << ' ' << x << endl; #define printv(a) {\ for (auto y : a) cout << y << ' ';\ cout << '\n';\ } #define eb emplace_back #define pb push_back #define mp make_pair #define mt make_tuple #define pii pair<int,int> #define pll pair<lli,lli> #define tiii tuple<int,int,int> #define all(a) a.begin(), a.end() #define X first #define Y second #define oset tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> auto SEED = chrono::steady_clock::now().time_since_epoch().count(); mt19937 rng(SEED); const int mod = 1e9 + 7, x = 864197532, N = 501, logN = 20, K = 350; int main () { ios::sync_with_stdio(false); cin.tie(0); int n, m, k, tmp, a, b; cin >> n >> m >> k; vector <pii> both, A, B, em; fop (i,0,n) { cin >> tmp >> a >> b; if (a && b) both.eb(tmp, i + 1); else if (a) A.eb(tmp, i + 1); else if (b) B.eb(tmp, i + 1); else em.eb(tmp, i + 1); } if (both.size() + min(A.size(), B.size()) < k) { cout << -1 << endl; return 0; } sort(all(both)); sort(all(A)); sort(all(B)); tmp = min(int(A.size()), int(B.size())); A.resize(tmp); B.resize(tmp); sort(all(em)); lli ans = 1ll << 60, now1 = -1, now2 = -1, now3 = -1; int ans_a = -1, ans_b = -1; for (int i = min(int(both.size()), k), j = k - i; i >= 0 && j < tmp; --i, ++j) { if (m - i - 2 * j > em.size()) continue; if (now1 == -1) { now1 = now2 = now3 = 0; fop (_,0,i) now1 += both[_].X; fop (_,0,j) now2 += A[_].X + B[_].X; fop (_,0,m - i - 2 * j) now3 += em[_].X; } else { /* 1 0 2 0 1 1 */ now1 -= both[i].X; now2 += A[j - 1].X + B[j - 1].X; now3 -= em[m - i - 2 * j].X; } if (ans > now1 + now2 + now3) { ans = now1 + now2 + now3; ans_a = i; ans_b = j; } } cout << ans << endl; fop (i,0,ans_a) cout << both[i].Y << ' '; fop (i,0,ans_b) cout << A[i].Y << ' ' << B[i].Y << ' '; fop (i,0,m - ans_a - ans_b * 2) cout << em[i].Y << ' '; cout << endl; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, k, t, a, b, boths, alices, bobs, cnt[2], ans, res, tmp; vector<int> both, alice, bob; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else if (b) bob.push_back(t); } boths = both.size(); alices = alice.size(); bobs = bob.size(); sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); while (k) { int s1 = 1 << 30, s2 = 1 << 30; if (boths > cnt[0]) s1 = both[cnt[0]]; if (alices > cnt[1] && bobs > cnt[1]) s2 = alice[cnt[1]] + bob[cnt[1]]; if (s1 < s2) ans += s1, cnt[0]++, k--; else if (s1 > s2) ans += s2, cnt[1]++, k--; else break; } while (k--) { if (cnt[1] < min(alices, bobs)) ans += alice[cnt[1]] + bob[cnt[1]], cnt[1]++; else if (cnt[0] < boths) ans += both[cnt[0]], cnt[0]++; else return cout << "-1\n", 0; } cout << ans << '\n'; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.Arrays; import java.util.PriorityQueue; import java.util.Scanner; public class E { static class Item { int time; int alice; int bob; @Override public String toString() { return "Item{" + "time=" + time + ", alice=" + alice + ", bob=" + bob + '}'; } } static PriorityQueue<Item> all; static PriorityQueue<Item> alice; static PriorityQueue<Item> bob; static int k; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; n = in.nextInt(); k = in.nextInt(); Item[] items = new Item[n]; for (int i = 0; i < n; i++) { items[i] = new Item(); items[i].time = in.nextInt(); items[i].alice = in.nextInt(); items[i].bob = in.nextInt(); } all = new PriorityQueue<Item>((a, b) -> b.time - a.time); alice = new PriorityQueue<>((a, b) -> a.time - b.time); bob = new PriorityQueue<>((a, b) -> a.time - b.time); Arrays.sort(items, (a, b) -> a.time - b.time); for (int i = 0; i < n; i++) { if (items[i].bob + items[i].alice == 2) { if (all.size() < k) { all.add(items[i]); } } else if (items[i].alice == 1) { if (alice.size() < k) { alice.add(items[i]); } } else if (items[i].bob == 1) { if (bob.size() < k) { bob.add(items[i]); } } } System.out.println(solve()); } private static long solve() { if (alice.size() + all.size() < k) { return -1; } if (bob.size() + all.size() < k) { return -1; } long sum = 0; int count = 0; while (count < k) { if (alice.size() == 0 || bob.size() == 0) { if (all.size() + count < k) { return -1; } while (all.size() + count > k) { all.poll(); } while (!all.isEmpty()) { sum += all.poll().time; } return sum; } else { if (all.size() == 0 || (alice.peek().time + bob.peek().time < all.peek().time)) { // System.out.println(alice.peek().toString() + " " + bob.peek()); sum += alice.poll().time + bob.poll().time; } else { sum += all.poll().time; } count++; } } return sum; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k=list(map(int,input().split())) l,l1,l2,l3=[],[],[],[] for i in range(1,n+1): x,y,z=list(map(int,input().split())) if y==1 and z==1: l.append([x,i,-1]) elif y==1: l1.append([x,i,0]) elif z==1: l2.append([x,i,0]) else: l3.append([x,i,0]) if 2*k<m: l1.sort() l2.sort() a=len(l1) b=len(l2) for i in range(min(a,b,k)): x=l1.pop(0) y=l2.pop(0) x[2]=y[1] x[0]+=y[0] l.append(x) p=[] l.sort() i=0 book=0 while i!=k and l!=[]: z=l.pop(0) p.append(z) if z[2]==-1: book+=1 else: book+=2 i+=1 if len(p)<k: print(-1) else: l=l+l1+l2+l3 l.sort() p=p+l[:m-book] l=[] c=0 for i,j,k in p: c+=i if k!=-1 and k!=0: l.extend([j,k]) else: l.append(j) print(c) print(*l) elif 2*k>m: l1.sort() l2.sort() l.sort() b=[] i=0 p=[] while i!=k: if l!=[] and l1!=[] and l2!=[]: if l[0]<l1[0]+l2[0]: z=l.pop(0) b.append(z) i+=1 else: if i+2<=k: x=l1.pop(0) y=l2.pop(0) x[2]=y[1] x[0]+=y[0] b.append(x) i+=2 else: z=l.pop(0) b.append(z) i+=1 elif l==[]: if l1!=[] and l2!=[]: x=l1.pop(0) y=l2.pop(0) x[2]=y[1] x[0]+=y[0] b.append(x) i+=2 else: break else: if l!=[]: z=l.pop(0) b.append(z) i+=1 else: break if len(p)<k: print(-1) else: z=l+l1+l2+l3 p=b+z[:m-k] c=0 l=[] for i,j,k in p: c+=i if k!=-1 and k!=0: l.extend([j,k]) else: l.append(j) print(c) print(*l) else: l1.sort() l2.sort() x=l1[:k]+l2[:k] if len(x)==m: c=0 k=[] for i,j,k5 in x: c+=i k.append(j) print(c) print(*k) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; 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); } @Override public String toString() { return "Book [alice=" + alice + ", bob=" + bob + ", time=" + time + "]"; } } public static int readBooks(List<Book> list, int books) { ArrayList<Book> both = new ArrayList<Book>(); ArrayList<Book> al = new ArrayList<Book>(); ArrayList<Book> bob = new ArrayList<Book>(); int a = 0; int b = 0; for (Book t : list) { if (t.alice + t.bob == 2) { both.add(t); ++a; ++b; } else if (t.alice == 1) { al.add(t); ++a; } else if (t.bob == 1) { bob.add(t); ++b; } } if (a < books || b < books) { return -1; } Collections.sort(both); Collections.sort(al); Collections.sort(bob); int result = 0; int i = 0; while (i < both.size() && books != 0) { result += both.get(i).time; --books; ++i; } if (books == 0) { return result; } int t = books; i = 0; while (i < al.size() && t != 0) { result += al.get(i).time; ++i; --t; } t = books; i = 0; while (i < bob.size() && t != 0) { result += bob.get(i).time; ++i; --t; } return result; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line[] = null; ArrayList<Book> list = new ArrayList<Book>(); list.clear(); line = br.readLine().split(" "); int n = Integer.parseInt(line[0]); int k = Integer.parseInt(line[1]); for (int i = 0; i < n; i++) { line = br.readLine().split(" "); list.add(new Book(Integer.parseInt(line[1]), Integer.parseInt(line[2]), Integer.parseInt(line[0]))); } System.out.println(readBooks(list, k)); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const long long INF = LLONG_MAX / 2; const long long N = 2e5 + 1; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; t = 1; while (t--) { long long n, k; std::cin >> n >> k; long long sum = 0, pp = 0, i, ta[n], a[n], b[n], a1[n], j = 0, m = 0, l = 0, b1[n], ab[n], ak = k, bk = k, j1 = 0, m1 = 0, l1 = 0; for (long long i = 0; i < n; i++) { std::cin >> ta[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) ab[j++] = ta[i]; else if (a[i] == 1) a1[m++] = ta[i]; else if (b[i] == 1) b1[l++] = ta[i]; else pp++; } if (l + j < k || m + j < k) { cout << "-1\n"; continue; } if (j != 0) sort(ab, ab + j); if (m != 0) sort(a1, a1 + m); if (l != 0) sort(b1, b1 + l); for (long long i = 0; i < ak + bk + 2; i++) { if ((j1 >= j) || (m1 < m && l1 < l && a1[m1] + b1[l1] < ab[j1])) { sum += a1[m1] + b1[l1]; ak--, bk--; m1++, l1++; } else { sum += ab[j1]; ak--, bk--; j1++; } if (ak == 0 || bk == 0) break; } cout << sum << "\n"; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define f first #define int long long #define s second using namespace std; const int N=2e5+5,mod=1e9+7,Inf=1e16; int t,n,m,k,p[2][N]; pair<int,int> tree[4*N]; vector<pair<int,int> >a[3],v; string s; void update(int u,int ind,int l,int r){ if(l>ind || r<ind) return; if(l==r) { tree[u].f++; tree[u].s+=ind; return; } int mid=(l+r)/2; update(2*u,ind,l,mid); update(2*u+1,ind,mid+1,r); tree[u].f=tree[2*u].f+tree[2*u+1].f; tree[u].s=tree[2*u].s+tree[2*u+1].s; } int getans(int u,int l,int r,int ind){ if(l==r) return tree[u].s; int mid=(l+r)/2; if(tree[2*u].f>=ind) return getans(2*u,l,mid,ind); return tree[2*u].s+getans(2*u+1,mid+1,r,ind-tree[2*u].f); } main(){ cin>>n>>m>>k; int T=10000; for(int i=1;i<=n;i++){ int t,f1,f2; cin>>t>>f1>>f2; if(f1&&f2) a[2].push_back({t,i}); else if(f1) a[1].push_back({t,i}); else if(f2) a[0].push_back({t,i}); else update(1,t,1,T),v.push_back({t,i}); } for(int i=0;i<3;i++) sort(a[i].begin(),a[i].end()); int cur1=(int)a[1].size()-1,cur2=(int)a[0].size()-1; for(int i=0;i<2;i++){ for(int j=0;j<a[i].size();j++){ p[i][j]=a[i][j].f; if(j) p[i][j]+=p[i][j-1]; } } int cur = 0,ans=Inf,cnt=0; for(int i=0;i<=a[2].size();i++){ if(i) { cur += a[2][i-1].f; } if(i==m) { if(ans>cur && m>=k) ans=cur,cnt=i; break; } while(cur1+1>k-i && cur1>=0) update(1,a[1][cur1].f,1,T),cur1--; while(cur2+1>k-i && cur2>=0) update(1,a[0][cur2].f,1,T),cur2--; if(min(cur1,cur2)+1+i>=k&& m-i-cur1-cur2-2>=0 && tree[1].f>=m-i-cur1-cur2-2) { int ans_=cur+p[1][cur1]+p[0][cur2]; if(m-i-cur1-cur2-2) ans_+=getans(1,1,T,m-i-cur1-cur2-2); if(ans_ < ans) { ans=ans_; cnt=i; } } } if(ans==Inf) cout<<-1; else { cout<<ans<<endl; for(int i=1;i<=cnt;i++) { cout<<a[2][i-1].s<<" "; } for(int i=cnt;i<a[2].size();i++) v.push_back(a[2][i]); m-=cnt; k-=cnt; k=max(k,0ll); m-=2*k; for(int i=0;i<k;i++){ cout<<(a[0][i].s)<<" "<<a[1][i].s<<" "; } for(int i=0;i<2;i++) for(int j=k;j<a[i].size();j++) { v.push_back(a[i][j]); } sort(v.begin(),v.end()); for(int i=0;i<m;i++){ cout<<v[i].s<<" "; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<int> both, alice, bob; int both_size = 0, alice_size = 0, bob_size = 0; int main(int argc, char const *argv[]) { cin.sync_with_stdio(false); int N, k; cin >> N >> k; int min_, a, b; for (int i = 0; i < N; i++) { cin >> min_ >> a >> b; if (a == 1 and b == 1) { both.push_back(min_); both_size++; } else if (a == 1 and b == 0) { alice.push_back(min_); alice_size++; } else if (a == 0 and b == 1) { bob.push_back(min_); bob_size++; } } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); int m = min(alice_size, bob_size); if (both_size + m < k) { cout << both_size + m; return 0; } both_size += m; for (int i = 0; i < m; i++) { both.push_back(alice[i] + bob[i]); } sort(both.begin(), both.end()); int ans = 0; for (int i = 0; i < k; i++) { ans += both[i]; } cout << 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool myComp(pair<int, pair<int, int> > p1, pair<int, pair<int, int> > p2) { return p1.first < p2.first; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; pair<int, pair<int, int> > p[n]; for (int i = 0; i <= n - 1; i++) { cin >> p[i].first >> p[i].second.first >> p[i].second.second; } sort(p, p + n, myComp); int i = 0; int la = 0, lb = 0; int ans = 0; vector<int> A, B; while (i < n) { if (p[i].second.first && p[i].second.second) { ans += p[i].first; la++; lb++; } else if (p[i].second.first) { if (la < k) { ans += p[i].first; la++; A.emplace_back(p[i].first); } } else if (p[i].second.second) { if (lb < k) { ans += p[i].first; lb++; B.emplace_back(p[i].first); } } if (la >= k && lb >= k) { if (la > k) { int j = A.size() - 1; int cnt = 0; while (cnt < la - k) { ans -= A[j--]; cnt++; } } else if (lb > k) { int j = B.size() - 1; int cnt = 0; while (cnt < lb - k) { ans -= B[j--]; cnt++; } } break; } i++; } if (i == n) ans = -1; else { i++; int x = A.size() - 1, y = B.size() - 1; while (i < n && x >= 0 && y >= 0) { int temp = A[x--] + B[y--]; if (p[i].first < temp) { ans -= temp; ans += p[i].first; } else { break; } i++; } } cout << ans << '\n'; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e18; const long long int mod = 1e9 + 7; const long long int mod2 = 998244353; const long long int longinf = 1LL << 60; const long double pi = acos(-1); const long double phi = (sqrt(5) + 1) / 2; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int n, k; cin >> n >> k; vector<long long int> t(n), a(n), b(n); for (long long int i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; } long long int k1 = k; long long int k2 = k; vector<long long int> x; vector<long long int> y; vector<long long int> comm; long long int ans = 0; for (long long int i = 0; i < n; i++) { if (k1 == 0 && k2 == 0) { break; } if (a[i] == 1 && b[i] == 1) { comm.push_back(t[i]); k1--; k2--; } else if (a[i] == 1) { x.push_back(t[i]); } else if (b[i] == 1) { y.push_back(t[i]); } } if (k1 == 0 && k2 == 0) { sort(comm.begin(), comm.end()); for (long long int i = 0; i < k; i++) { ans += comm[i]; } } else { for (long long int i = 0; i < (long long int)comm.size(); i++) { ans += comm[i]; } sort(x.begin(), x.end()); sort(y.begin(), y.end()); if (x.size() < k1 || y.size() < k2) { cout << "-1" << endl; return 0; } for (long long int i = 0; i < k1; i++) { ans += x[i]; } for (long long int i = 0; i < k2; i++) { ans += y[i]; } } cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class GFG { private static int i,j,k,l,n,m; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // int t = Integer.parseInt(br.readLine().trim()); // while (t-- != 0) { // int n = Integer.parseInt(br.readLine().trim()); // char c[]=br.readLine().trim().toCharArray(); StringTokenizer st = new StringTokenizer(br.readLine().trim()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); int kk = Integer.parseInt(st.nextToken()); ArrayList<int[]> A[]=new ArrayList[4]; for(i=0;i<4;i++) A[i]=new ArrayList<>(); int ans=0; for(i=0;i<n;i++){ st = new StringTokenizer(br.readLine().trim()); int time=Integer.parseInt(st.nextToken()); int al=Integer.parseInt(st.nextToken()); int bo=Integer.parseInt(st.nextToken()); int a[]=new int[]{time,i+1}; if(al==1&&bo==0) A[0].add(a); else if(al==0&&bo==1) A[1].add(a); else if(al==1&&bo==1) A[2].add(a); else A[3].add(a); } if(A[0].size()+A[2].size()<kk||A[1].size()+A[2].size()<kk) out.println(-1); else{ for(i=0;i<4;i++) Collections.sort(A[i],(b,c)->b[0]-c[0]); // out.println(A[0]); // out.println(A[1]); // out.println(A[2]); // out.println(A[3]); i=0;j=0;k=0;l=0; int cnt=0,books=0; while(i<A[0].size()&&j<A[1].size()&&k<A[2].size()&&cnt!=kk) { int sum = A[0].get(i)[0] + A[1].get(j)[0],c=A[2].get(k)[0]; if (sum < c) { ans += sum; books+=2; i++; j++; } else { ans += c; books++; k++; } cnt++; } if(i==A[0].size()||j==A[1].size()){ while(cnt!=kk){ cnt++; books++; ans+=A[2].get(k++)[0]; } } else if(k==A[2].size()){ while(cnt<kk){ cnt++; books+=2; ans+=A[0].get(i)[0] + A[1].get(j)[0]; } } while(books>m) { if (k == A[2].size() || i == 0 || j == 0) { ans = -1; break; } books --; ans = ans - A[0].get(--i)[0] - A[1].get(--j)[0] + A[2].get(k++)[0]; } if(books<m) ans+=find(A,books); out.println(ans); if(ans!=-1){ for(int y=0;y<i;y++) out.print(A[0].get(y)[1]+" "); for(int y=0;y<j;y++) out.print(A[1].get(y)[1]+" "); for(int y=0;y<k;y++) out.print(A[2].get(y)[1]+" "); for(int y=0;y<l;y++) out.print(A[3].get(y)[1]+" "); out.println(); } } out.close(); } private static int find(ArrayList<int[]>[] A, int books) { int ans=0; while(books++!=m){ int a=Integer.MAX_VALUE,b=Integer.MAX_VALUE,c=Integer.MAX_VALUE,d=Integer.MAX_VALUE; if(i<A[0].size()){ a=A[0].get(i)[0]; } if(j<A[1].size()){ b=A[1].get(j)[0]; } if(k<A[2].size()){ c=A[2].get(k)[0]; } if(l<A[3].size()){ d=A[3].get(l)[0]; } int min=Math.min(a,Math.min(b,Math.min(c,d))); if(min==a) i++; else if(min==b) j++; else if(min==c) k++; else l++; ans+=min; } return ans; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x-1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: none.append(i) else: Both.append(i) else: if i[1] == 0: Bob.append(i) else: Alice.append(i) z += 1 n -= 1 Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) tresult = [] if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] tresult1 = [] if min(len(Alice), len(Bob)) == len(Alice): if len(Alice) < k: k1 = k - len(Alice) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 else: if len(Bob) < k: k1 = k - len(Bob) if len(Both) < k1: print(-1) exit() else: tresult1 = Both[:k1] Both = Both[k1:] k = k - k1 Alice1 = Alice[:k] Bob1 = Bob[:k] Alice = Alice[k:] Bob = Bob[k:] corr = [] elev = False while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] >= Both[0][0]: Alice.append(Alice1[-1]) Bob.append(Bob1[-1]) corr.append(Both[0]) Alice1.pop(-1) Bob1.pop(-1) Both.pop(0) q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1) q = m - q All = Alice + Bob + Both + none All.sort(key=lambda x: x[0]) result = All[:q] result = result + tresult + tresult1 + corr + Alice1 + Bob1 sum1 = 0 for row in result: sum1 = sum1 + row[0] print(sum1) if sum1 == 82207: result.sort(key=lambda x: x[0]) print(result[-1]) print(result[-2]) chk = result[-1][0] - 1 for row in All: if row[0] == chk: print(row) if sum1 == 0: print(len(corr)) result.sort(key=lambda x: x[0]) print(sum(row[1] for row in result)) print(sum(row[2] for row in result)) print(All[q-2]) print(All[q-1]) print(All[q]) All = All[q:] print(q) print(result[-1]) print(All[0]) print(len(result)) print(len(All)) print(' '.join([str(row[3]) for row in result]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class X { public static void main(String[] args) { FastScanner in=new FastScanner(); PrintWriter out=new PrintWriter(System.out); solve(in,out); out.close(); } static void solve(FastScanner in,PrintWriter out){ // out.println(1); int n=in.nextInt(); int k=in.nextInt(); int a[][]=new int[n][4]; for(int i=0;i<n;i++) { a[i][0]=in.nextInt(); a[i][1]=in.nextInt(); a[i][2]=in.nextInt(); a[i][3]=i; } Arrays.sort(a,new Comparator<int[]>(){ public int compare(int a[],int b[]){ if(a[0]==b[0]) return b[1]+b[2]-a[1]-a[2]; else return a[0]-b[0]; } }); long as1=0,as2=0; HashSet<Integer> h=new HashSet<Integer>(); ArrayList<Integer> ans=new ArrayList<Integer>(); long a1=0,a2=0; for(int i=0;i<n;i++){ if(a1>=k) break; if(a[i][1]==1){ a1++; ans.add(a[i][0]); h.add(a[i][3]); if(a[i][2]==1) a2++; } } for(int i=0;i<n;i++){ if(a2>=k) break; if(h.contains(a[i][3])) continue; if(a[i][2]==1){ a2++; ans.add(a[i][0]); } } if(a2<k||a1<k) { out.println("-1"); return; } for(int i=0;i<ans.size();i++){ as1+=ans.get(i); } a1=0; a2=0; ans.clear(); h.clear(); for(int i=0;i<n;i++){ if(a2>=k) break; if(a[i][2]==1){ a2++; ans.add(a[i][0]); h.add(a[i][3]); if(a[i][1]==1) a1++; } } for(int i=0;i<n;i++){ if(a1>=k) break; if(h.contains(a[i][3])) continue; if(a[i][1]==1){ a1++; ans.add(a[i][0]); } } if(a2<k||a1<k) { out.println("-1"); return; } for(int i=0;i<ans.size();i++){ as2+=ans.get(i); } out.println(Math.min(as1,as2)); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; public class E2 { static final boolean RUN_TIMING = false; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public void go() throws IOException { // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1 << 20); // out = new PrintWriter(new FileWriter(new File("output.txt"))); int n = ipar(); int m = ipar(); int k = ipar(); ArrayList<ArrayList<int[]>> books = new ArrayList<>(); for (int i = 0; i < 4; i++) { books.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int t = ipar(); int a = ipar(); int b = ipar(); books.get(a*2+b).add(new int[]{t, i}); } for (int i = 0; i < 4; i++) { Collections.sort(books.get(i), this::compare); } int sum1 = 0, sum2 = 0, sum3 = 0; int index1 = -1, index2 = -1, index3 = -1; for (int i = 0; i < k; i++) { if (index1+1 >= books.get(1).size() || index2+1 >= books.get(2).size()) { index3++; if (index3 == books.get(3).size()) { out.println(-1); return; } sum3 += books.get(3).get(index3)[0]; } else { index1++; index2++; sum1 += books.get(1).get(index1)[0]; sum2 += books.get(2).get(index2)[0]; } } PriorityQueue<int[]> candidates = new PriorityQueue<>(this::compare); for (int i = index1+1; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = index2+1; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } TreeSet<int[]> free = new TreeSet<>(this::compare); int freeSum = 0; for (int i = index1+index2+index3+3; i < m && !candidates.isEmpty(); i++) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); int best = index1+index2+index3+3+free.size() == m ? sum1+sum2+sum3+freeSum : Integer.MAX_VALUE; int bestIndex = index1+index2+index3+3+free.size() == m ? index3 : -1; while (index3+1 < books.get(3).size()) { index3++; sum3 += books.get(3).get(index3)[0]; if (index1 >= 0) { sum1 -= books.get(1).get(index1)[0]; candidates.add(books.get(1).get(index1)); index1--; } if (index2 >= 0) { sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(2).get(index2)); index2--; } while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } if (index1+index2+index3+3+free.size() == m && sum1+sum2+sum3+freeSum < best) { best = sum1+sum2+sum3+freeSum; bestIndex = index3; } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); } sum1 = sum2 = sum3 = 0; index1 = index2 = index3 = -1; for (int i = 0; i < k; i++) { if (index1+1 >= books.get(1).size() || index2+1 >= books.get(2).size()) { index3++; if (index3 == books.get(3).size()) { out.println(-1); return; } sum3 += books.get(3).get(index3)[0]; } else { index1++; index2++; sum1 += books.get(1).get(index1)[0]; sum2 += books.get(2).get(index2)[0]; } } candidates.clear(); for (int i = index1+1; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = index2+1; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } free.clear(); freeSum = 0; for (int i = index1+index2+index3+3; i < m && !candidates.isEmpty(); i++) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index3 < bestIndex) { index3++; sum3 += books.get(3).get(index3)[0]; if (index1 >= 0) { sum1 -= books.get(1).get(index1)[0]; candidates.add(books.get(1).get(index1)); index1--; } if (index2 >= 0) { sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(2).get(index2)); index2--; } while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+index3+3+free.size() > m && !free.isEmpty()) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } if (index1+index2+index3+3+free.size() == m && sum1+sum2+sum3+freeSum < best) { best = sum1+sum2+sum3+freeSum; bestIndex = index3; } } if (best == Integer.MAX_VALUE) { out.println(-1); return; } out.println(best); for (int i = 0; i <= index1; i++) { out.print(books.get(1).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index2; i++) { out.print(books.get(2).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index3; i++) { out.print(books.get(3).get(i)[1]+1); out.print(" "); } for (int[] f : free) { out.print(f[1]+1); out.print(" "); } out.println(); } public int compare(int[] a, int[] b) { if (a[0] == b[0]) { return a[1] - b[1]; } return a[0] - b[0]; } public int ipar() throws IOException { return Integer.parseInt(spar()); } public int[] iapar(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() throws IOException { return Long.parseLong(spar()); } public long[] lapar(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() throws IOException { return Double.parseDouble(spar()); } public String spar() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char)c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String linepar() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char)c; len++; } return new String(inputBuffer, 0, len); } public boolean haspar() throws IOException { String line = linepar(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public static void main(String[] args) throws IOException { long time = 0; time -= System.nanoTime(); new E2().go(); time += System.nanoTime(); if (RUN_TIMING) { System.out.printf("%.3f ms%n", time / 1000000.0); } out.flush(); in.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
[n, k] = list(map(int, input().split(' '))) alice_books = [] bob_books = [] common_books = [] # all_books = [] for _ in range(n): [t, a, b] = list(map(int, input().split(' '))) if a == 1 and b == 0: alice_books.append(t) elif b == 1 and a == 0: bob_books.append(t) elif a == 1 and b == 1: common_books.append(t) alice_books = list(sorted(alice_books)) bob_books = list(sorted(bob_books)) common_books = list(sorted(common_books)) if len(alice_books) + len(common_books) < k: print(-1) elif len(bob_books) + len(common_books) < k: print(-1) else: cur_alice = 0 cur_bob = 0 cur_common = 0 total_time = 0 alice_req = k bob_req = k while alice_req > 0 and bob_req > 0: alice_time = alice_books[cur_alice] if cur_alice < len(alice_books) else None bob_time = bob_books[cur_bob] if cur_bob < len(bob_books) else None common_time = common_books[cur_common] if cur_common < len(common_books) else None # print(cur_alice, cur_bob, cur_common) if not common_time: if alice_req > 0: alice_req -= 1 total_time += alice_time cur_alice += 1 elif bob_req > 0: bob_req -= 1 total_time += bob_time cur_bob += 1 else: if alice_req > 0: if alice_time and alice_time + (bob_time or 0) < common_time: alice_req -= 1 total_time += alice_time cur_alice += 1 else: alice_req -= 1 bob_req -= 1 cur_common += 1 total_time += common_time elif bob_req > 0: if bob_time and bob_time + (alice_time or 0) < common_time: bob_req -= 1 total_time += bob_time cur_bob += 1 else: alice_req -= 1 bob_req -= 1 cur_common += 1 total_time += common_time print(total_time)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k = map(int, input().split()) a = [] b = [] comman = [] a_ = [] b_ = [] for _ in range(n): t,ai,bi = map(int, input().split()) a_.append(1) b_.append(1) if ai == 1 and bi == 1: comman.append(t) elif ai == 1: a.append(t) elif bi == 1: b.append(t) a.sort() b.sort() #comman.sort() if a_.count(1) < k or b_.count(1) < k: print(-1) else: s = 0 m = min(len(a),len(b)) for i in range(m): comman.append(a[i]+b[i]) comman.sort() print(sum(comman[:k]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long M = 998244353; long long powerm(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % M; y = y >> 1; x = (x * x) % M; } return res % M; } long long power(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } const int N = 5e5 + 5; long long fact[N], inv[N]; void pre(int n) { fact[0] = inv[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = (fact[i - 1] * i) % M; inv[i] = powerm(fact[i], M - 2); } } long long C(long long n, long long r) { long long ans = ((fact[n] * inv[r]) % M + M) % M; ans = ((ans * inv[n - r]) % M + M) % M; return ans; } bool myfunc(pair<int, int> p1, pair<int, int> p2) { if (p1.first == p2.first) { return p1.second < p2.second; } return p1.first < p2.first; } int main() { int t = 1; while (t--) { vector<long long> v1, v2, v3; long long n, k, a, b, c; cin >> n >> k; for (long long i = 0; i < n; i++) { cin >> a >> b >> c; if (b == 0 && c == 1) v1.push_back(a); if (b == 1 && c == 0) v2.push_back(a); if (b == 1 && c == 1) v3.push_back(a); } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long l1 = min((long long)v1.size(), (long long)v2.size()); long long l2 = v3.size(); if (l1 + l2 < k) { cout << -1 << endl; continue; } long long ptr1 = 0, ptr2 = 0; long long ans = 0, ct = 0; while (ptr1 < l1 && ptr2 < l2 && ct < k) { if (v1[ptr1] + v2[ptr1] <= ptr2) { ans += v1[ptr1] + v2[ptr1]; ptr1++; } else { ans += v3[ptr2]; ptr2++; } ct++; } if (ct == k) cout << ans << endl; else { if (ptr1 == l1) { while (ct < k) { ans += v3[ptr2]; ptr2++; ct++; } } if (ptr2 == l2) { while (ct < k) { ans += v1[ptr1] + v2[ptr1]; ptr1++; ct++; } } } cout << ans << endl; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = (int)(2e9 + 1); int n, m, k; vector<pair<int, int>> g[4]; pair<int, vector<int>> getSumWithPath(int common) { int time = 0; vector<int> path; for (int i = 0; i < common; ++i) { time += g[3][i].first; path.push_back(g[3][i].second); } if (common + min((int)g[1].size(), (int)g[2].size()) < k) { return make_pair(INF, vector<int>()); } int ii; for (ii = 0; ii < max(0, k - common); ++ii) { time += (g[1][ii].first + g[2][ii].first); path.push_back(g[1][ii].second); path.push_back(g[2][ii].second); } if ((int)path.size() > m) { return make_pair(INF, vector<int>()); } int a = ii; int b = ii; int c = common; int d = 0; int size = (int)path.size(); for (int i = 0; i < m - size; ++i) { pair<int, int> p1 = a == (int)g[1].size() ? make_pair(INF, INF) : g[1][a]; pair<int, int> p2 = b == (int)g[2].size() ? make_pair(INF, INF) : g[2][b]; pair<int, int> p3 = c == (int)g[3].size() ? make_pair(INF, INF) : g[3][c]; pair<int, int> p4 = d == (int)g[0].size() ? make_pair(INF, INF) : g[0][d]; int mval = min(min(p1.first, p2.first), min(p3.first, p4.first)); if (mval == INF) { return make_pair(INF, vector<int>()); } time += mval; if (mval == p1.first) { path.push_back(p1.second); ++a; } else if (mval == p2.first) { path.push_back(p2.second); ++b; } else if (mval == p3.first) { path.push_back(p3.second); ++c; } else if (mval == p4.first) { path.push_back(p4.second); ++d; } } return make_pair(time, path); } int main(int argc, const char* argv[]) { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; g[(a << a) ^ b].emplace_back(make_pair(t, i)); } for (int i = 0; i < 4; ++i) { sort(g[i].begin(), g[i].end(), [](pair<int, int>& p1, pair<int, int>& p2) { return p1.first <= p2.first; }); } if ((int)g[3].size() + (int)g[1].size() < k || (int)g[3].size() + (int)g[2].size() < k) { cout << -1 << endl; return 0; } int time = INF; vector<int> path; int l = 0, r = (int)g[3].size(); while (r - l >= 3) { int m1 = l + (r - l) / 3; int m2 = r - (r - l) / 3; pair<int, vector<int>> pm1 = getSumWithPath(m1); pair<int, vector<int>> pm2 = getSumWithPath(m2); if (pm1.first < pm2.first) { r = m2; } else { l = m1; } } while (l <= r) { pair<int, vector<int>> current = getSumWithPath(l); if (current.first < time) { time = current.first; path = current.second; } ++l; } if (time == INF) { cout << -1 << endl; return 0; } assert((int)path.size() == m); cout << time << endl; for (int i = 0; i < (int)path.size(); cout << path[i] + 1 << " ", ++i) ; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, test, res, x, k, u, v, t, y, z, mn, q, m, ans[200001], m1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); test = 1; while (test--) { cin >> n >> m >> k; m1 = m; res = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> p[4]; for (int i = 1; i <= n; i++) { cin >> x >> y >> z; if (y == 1 && z == 0) p[1].push(make_pair(x, i)); if (y == 0 && z == 1) p[2].push(make_pair(x, i)); if (y == 1 && z == 1) p[0].push(make_pair(x, i)); if (y == 0 && z == 0) p[3].push(make_pair(x, i)); } if (p[0].size() + p[1].size() < k || p[0].size() + p[2].size() < k) { cout << -1 << endl; continue; } for (int i = 1; i <= k; i++) { if (p[0].size()) u = p[0].top().first; else u = 1e9; if (p[1].size()) v = p[1].top().first; else v = 1e9; if (p[2].size()) t = p[2].top().first; else t = 1e9; if (u <= v + t || m <= 1) { res += u; ans[m] = p[0].top().second; p[0].pop(); m--; } else { res += v + t; ans[m] = p[1].top().second; ans[m - 1] = p[2].top().second; p[1].pop(); p[2].pop(); m -= 2; } if (m == 0 && i < n) cout << -1 << endl; } while (m > 0) { if (p[0].size()) u = p[0].top().first; else u = 1e9; if (p[1].size()) v = p[1].top().first; else v = 1e9; if (p[2].size()) t = p[2].top().first; else t = 1e9; if (p[3].size()) q = p[3].top().first; else q = 1e9; mn = min(u, min(v, min(t, q))); if (mn == u) { ans[m] = p[0].top().second; p[0].pop(); } else if (mn == v) { ans[m] = p[1].top().second; p[1].pop(); } else if (mn == t) { ans[m] = p[2].top().second; p[2].pop(); } else { ans[m] = p[3].top().second; p[3].pop(); } res += mn; m--; } cout << res << endl; for (int i = 1; i <= m1; i++) cout << ans[i] << " "; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; void YES(bool t = 1) { cout << YESNO[t] << "\n"; } void Yes(bool t = 1) { cout << YesNo[t] << "\n"; } void yes(bool t = 1) { cout << yesno[t] << "\n"; } const long long mod = 1e9 + 7; const long long mxN = 2e6 + 5; long long n, m, x, y; array<long long, 3> a[mxN]; string s, t; void code() { cin >> n >> m; vector<long long> v1, v2, v3; for (long long i = 0; i < n; i++) { a[i][0] = a[i][1] = a[i][2] = 0; cin >> a[i][0] >> a[i][1] >> a[i][2]; if (a[i][1] == 1 && a[i][2] == 1) { v1.push_back(a[i][0]); } if (a[i][1] == 1 && a[i][2] == 0) { v2.push_back(a[i][0]); } if (a[i][1] == 0 && a[i][2] == 1) { v3.push_back(a[i][0]); } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long ans = 0; long long k = 0; long long i = 0, j = 0; long long x = v1.size(), y = v2.size(), z = v3.size(); while (k <= m && i < x && j < min(y, z)) { k++; if (v1[i] <= v2[j] + v3[j]) { ans += v1[i]; i++; } else { ans += (v2[j] + v3[j]); j++; } } while (k <= m && i < x) { ans += v1[i]; i++; k++; } while (k <= m && j < min(y, z)) { ans += v2[j] + v3[j]; k++; j++; } if (k < m) cout << -1 << "\n"; else cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) code(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=tuple(map(int,input().split())) one1=[] one2=[] both=[] for _ in range(n): tmp=list(map(int,input().split())) if tmp[1]&tmp[2]: both.append(tmp[0]) elif tmp[1]|tmp[2]: if tmp[1]: one1.append(tmp[0]) else: one2.append(tmp[0]) if len(both)==0: res=-1 else: if len(both)>=k: both.sort() res=sum(both[:k]) else: rem=k-len(both) if len(one1)>=rem and len(one2)>=rem: one1.sort() one2.sort() #print(one1,one2) res=sum(both)+sum(one1[:k])+sum(one2[:k]) else: res=-1 print(res)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int s[200000]; int main() { long long ans = 0; int n, k, t[200000], a[200000], b[200000], i, count = 0; cin >> n >> k; for (i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) { s[count] = t[i]; count++; } } int g = count, h = count; for (i = 0; i < n; i++) { if (a[i] == 1 && b[i] == 0) { s[count] = t[i]; count++; g = count; } } count = h; for (i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 0 && b[i] == 1) { s[count] += t[i]; count++; } } h = min(g, count); sort(s, s + h); if (h < k) cout << -1; else { for (i = 0; i < k; i++) ans += s[i]; cout << 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" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class cf1374e2_3 { public static void main(String[] args) throws IOException { int n = rni(), m = ni(), k = ni(), t[] = new int[n + 1]; List<int[]> a11 = new ArrayList<>(), a01 = new ArrayList<>(), a10 = new ArrayList<>(), a00 = new ArrayList<>(); for(int i = 0; i < n; ++i) { int ti = rni(), ai = ni(), bi = ni(), entry[] = {ti, i + 1}; t[i + 1] = ti; if(ai == 1 && bi == 1) { a11.add(entry); } else if(ai == 1) { a01.add(entry); } else if(bi == 1) { a10.add(entry); } else { a00.add(entry); } } int n11 = a11.size(), n01 = a01.size(), n10 = a10.size(), n00 = a00.size(), c = maxof(0/*, 2 * k - m*/, k - min(n01, n10), m - n01 - n10 - n00); if(n11 < c) { prln(-1); } else { int p11 = 0, p01 = 0, p10 = 0, p00 = 0; a11.sort((a, b) -> a[0] - b[0]); a01.sort((a, b) -> a[0] - b[0]); a10.sort((a, b) -> a[0] - b[0]); a00.sort((a, b) -> a[0] - b[0]); long cur = 0; Set<Integer> ans = new HashSet<>(); PriorityQueue<Integer> free = new PriorityQueue<>((a, b) -> t[b] - t[a]); while(p11 < c) { cur += a11.get(p11)[0]; ans.add(a11.get(p11++)[1]); } for(int i = c; i < k; ++i) { cur += a01.get(p01)[0]; cur += a10.get(p10)[0]; ans.add(a01.get(p01)[1]); ans.add(a10.get(p10)[1]); free.offer(a01.get(p01++)[1]); free.offer(a10.get(p10++)[1]); } for(int i = max(c, 2 * k - c); i < m; ++i) { int min = IMAX, curmin = -1; if(p00 < n00) { min = a00.get(p00)[0]; curmin = 0; } if(p01 < n01 && a01.get(p01)[0] < min) { min = a01.get(p01)[0]; curmin = 1; } if(p10 < n10 && a10.get(p10)[0] < min) { min = a10.get(p10)[0]; curmin = 2; } assert curmin >= 0; cur += min; if(curmin == 0) { ans.add(a00.get(p00)[1]); free.offer(a00.get(p00++)[1]); } else if(curmin == 1) { ans.add(a01.get(p01)[1]); free.offer(a01.get(p01++)[1]); } else { ans.add(a10.get(p10)[1]); free.offer(a10.get(p10++)[1]); } } while(p11 < n11 && !free.isEmpty() && a11.get(p11)[0] < t[free.peek()]) { cur += a11.get(p11)[0] - t[free.peek()]; ans.remove(free.poll()); ans.add(a11.get(p11++)[1]); } prln(cur); prln(ans); } close(); } static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random rand = new Random(); // references // IBIG = 1e9 + 7 // IRAND ~= 3e8 // IMAX ~= 2e10 // LMAX ~= 9e18 // constants static final int IBIG = 1000000007; static final int IRAND = 327859546; static final int IMAX = 2147483647; static final int IMIN = -2147483648; static final long LMAX = 9223372036854775807L; static final long LMIN = -9223372036854775808L; // util static int minof(int a, int b, int c) {return min(a, min(b, c));} static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static long minof(long a, long b, long c) {return min(a, min(b, c));} static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;} static int maxof(int a, int b, int c) {return max(a, max(b, c));} static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static long maxof(long a, long b, long c) {return max(a, max(b, c));} static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;} static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;} static int floori(double d) {return (int)d;} static int ceili(double d) {return (int)ceil(d);} static long floorl(double d) {return (long)d;} static long ceill(double d) {return (long)ceil(d);} static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static <T> void shuffle(T[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); T swap = a[i]; a[i] = a[ind]; a[ind] = swap;}} static void rsort(int[] a) {shuffle(a); sort(a);} static void rsort(long[] a) {shuffle(a); sort(a);} static void rsort(double[] a) {shuffle(a); sort(a);} static int randInt(int min, int max) {return rand.nextInt(max - min + 1) + min;} // input static void r() throws IOException {input = new StringTokenizer(__in.readLine());} static int ri() throws IOException {return Integer.parseInt(__in.readLine());} static long rl() throws IOException {return Long.parseLong(__in.readLine());} static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;} static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;} static char[] rcha() throws IOException {return __in.readLine().toCharArray();} static String rline() throws IOException {return __in.readLine();} static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());} static int ni() {return Integer.parseInt(input.nextToken());} static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());} static long nl() {return Long.parseLong(input.nextToken());} // output static void pr(int i) {__out.print(i);} static void prln(int i) {__out.println(i);} static void pr(long l) {__out.print(l);} static void prln(long l) {__out.println(l);} static void pr(double d) {__out.print(d);} static void prln(double d) {__out.println(d);} static void pr(char c) {__out.print(c);} static void prln(char c) {__out.println(c);} static void pr(char[] s) {__out.print(new String(s));} static void prln(char[] s) {__out.println(new String(s));} static void pr(String s) {__out.print(s);} static void prln(String s) {__out.println(s);} static void pr(Object o) {__out.print(o);} static void prln(Object o) {__out.println(o);} static void prln() {__out.println();} static void pryes() {__out.println("yes");} static void pry() {__out.println("Yes");} static void prY() {__out.println("YES");} static void prno() {__out.println("no");} static void prn() {__out.println("No");} static void prN() {__out.println("NO");} static void pryesno(boolean b) {__out.println(b ? "yes" : "no");}; static void pryn(boolean b) {__out.println(b ? "Yes" : "No");} static void prYN(boolean b) {__out.println(b ? "YES" : "NO");} static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);} static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next());} static void h() {__out.println("hlfd");} static void flush() {__out.flush();} static void close() {__out.close();} }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, k; pair<int, pair<int, int> > a[N]; vector<long long> L, R, Both; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second.first >> a[i].second.second; if (a[i].second.first == 1 && a[i].second.second == 0) { L.push_back(a[i].first); } else if (a[i].second.first == 0 && a[i].second.second == 1) { R.push_back(a[i].first); } else if (a[i].second.first == 1) { Both.push_back(a[i].first); } } if (min(L.size(), R.size()) + Both.size() < k) { cout << -1; return 0; } sort(L.begin(), L.end()); sort(R.begin(), R.end()); sort(Both.begin(), Both.end()); int sz = min(L.size(), R.size()); int rest = k - sz; long long ans = 0; for (int i = 0; i < sz; i++) { ans += L[i]; ans += R[i]; } while (--rest >= 0) { ans += Both[rest]; } int ind = 0; for (int i = k - sz; i < Both.size(); i++) { if (Both[i] < L[ind] + R[ind]) { ans -= L[ind] + R[ind]; ans += Both[i]; ind++; } else { ind++; i--; } if (ind >= sz) break; } cout << 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct et { int val; int index; et(int val, int index) { this->val = val; this->index = index; } bool operator<(const et& other) const { if (this->val == other.val) { return this->index < other.index; } return this->val < other.val; } }; int main() { iostream::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<et> groups[4]; for (int i = 0; i < n; i++) { int v, a, b; cin >> v >> a >> b; groups[a * 2 + b].push_back(et(v, i)); } int sz[4]; vector<int> prefix[4]; for (int i = 0; i < 4; i++) { sort(groups[i].begin(), groups[i].end()); sz[i] = groups[i].size(); prefix[i].push_back(0); for (et x : groups[i]) { prefix[i].push_back(prefix[i].back() + x.val); } } int start = -1; for (int cnt = 0; cnt <= sz[3] && cnt <= m; cnt++) { int two = k - cnt; if (two > 0) { if (2 * two + cnt <= m && n - sz[3] + cnt >= m && two <= min(sz[1], sz[2])) { start = cnt; break; } } else { if (n - sz[3] + cnt >= m) { start = cnt; break; } } } if (start == -1) { cout << -1 << endl; return 0; } int minanswer = 2000000010; int minboy = -1; set<et> selected; set<et> free; for (int i = 0; i < 3; i++) { for (et x : groups[i]) { free.insert(x); } } for (int i = 0; i < (k - start); i++) { free.erase(groups[1][i]); free.erase(groups[2][i]); } int sumselected = 0; for (int i = 0; i < (m - (start + max(0, k - start) * 2)); i++) { et x = *free.begin(); free.erase(x); selected.insert(x); sumselected += x.val; } for (int cnt = start; cnt <= sz[3] && cnt <= m; cnt++) { int two = max(0, k - cnt); int curanswer = prefix[3][cnt] + sumselected + prefix[2][two] + prefix[1][two]; if (curanswer < minanswer) { minanswer = curanswer; minboy = cnt; } if (cnt == sz[3] || cnt == m) break; if (k - cnt > 0) { int id = k - cnt - 1; free.insert(groups[1][id]); free.insert(groups[2][id]); } int selectreq = m - (cnt + 2 * max(0, k - cnt)); bool cont = true; while (cont) { cont = false; if (selectreq > selected.size()) { cont = true; sumselected += (*free.begin()).val; selected.insert(*free.begin()); free.erase(free.begin()); } else if (selectreq < selected.size()) { cont = true; sumselected -= (*selected.rbegin()).val; selected.erase(*(selected.rbegin())); } else if (free.size() > 0 && selected.size() > 0 && (*selected.rbegin()).val > (*free.begin()).val) { cont = true; sumselected -= (*selected.rbegin()).val; selected.erase(*selected.rbegin()); sumselected += (*free.begin()).val; selected.insert(*free.begin()); free.erase(free.begin()); } } } selected.clear(); free.clear(); sumselected = 0; for (int i = 0; i < 3; i++) { for (et x : groups[i]) { free.insert(x); } } for (int i = 0; i < (k - start); i++) { free.erase(groups[1][i]); free.erase(groups[2][i]); } for (int i = 0; i < (m - (start + max(0, k - start) * 2)); i++) { et x = *free.begin(); free.erase(x); selected.insert(x); sumselected += x.val; } for (int cnt = start; cnt <= minboy; cnt++) { if (k - cnt > 0) { int id = k - cnt - 1; free.insert(groups[1][id]); free.insert(groups[2][id]); } if (cnt == minboy) break; int selectreq = m - (cnt + 2 * max(0, k - cnt)); bool cont = true; while (cont) { cont = false; if (selectreq > selected.size()) { cont = true; sumselected += (*free.begin()).val; selected.insert(*free.begin()); free.erase(free.begin()); } else if (selectreq < selected.size()) { cont = true; sumselected -= (*selected.rbegin()).val; selected.erase(*(selected.rbegin())); } else if (free.size() > 0 && selected.size() > 0 && (*selected.rbegin()).val > (*free.begin()).val) { cont = true; sumselected -= (*selected.rbegin()).val; selected.erase(*selected.rbegin()); sumselected += (*free.begin()).val; selected.insert(*free.begin()); free.erase(free.begin()); } } } cout << minanswer << endl; for (et x : selected) { cout << x.index + 1 << ' '; } for (int i = 0; i < minboy; i++) { cout << groups[3][i].index + 1 << ' '; } for (int i = 0; i < max(0, k - minboy); i++) { cout << groups[2][i].index + 1 << ' '; cout << groups[1][i].index + 1 << ' '; } cout << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<vector<pair<int, int>>> z(4); vector<vector<int>> x(4), y(4), a(4); for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; a ^= 1, b ^= 1; z[2 * a + b].emplace_back(t, i + 1); } for (int i = 0; i < 4; i++) { sort(z[i].begin(), z[i].end()); x[i].emplace_back(0); a[i].emplace_back(0); for (int j = 0; j < z[i].size(); j++) { a[i].emplace_back(z[i][j].first); x[i].emplace_back(x[i].back() + z[i][j].first); y[i].emplace_back(z[i][j].second); } } int inf = 2.1e9; int ans = inf; vector<int> t(4); int mx = min(x[1].size(), x[2].size()); int all = x[1].size() + x[2].size() + x[3].size(); int over = 0, kk = -1; for (int i = 0; i < x[0].size(); i++) { int j = k - i; if (j < 0 || mx <= j) { continue; } int need = m - i - j - j; if (need < 0 || all + i < m) { continue; } int lo = -1, hi = 10000; while (hi - lo > 1) { int mid = (hi + lo) / 2; int cnt = upper_bound(a[3].begin(), a[3].end(), mid) - a[3].begin() - 1; cnt += max(0, upper_bound(a[1].begin(), a[1].end(), mid) - a[1].begin() - 1 - j); cnt += max(0, upper_bound(a[2].begin(), a[2].end(), mid) - a[2].begin() - 1 - j); if (cnt >= need) { hi = mid; } else { lo = mid; } } vector<int> tt(4); tt[0] = i; tt[1] = max(j, upper_bound(a[1].begin(), a[1].end(), hi) - a[1].begin() - 1); tt[2] = max(j, upper_bound(a[2].begin(), a[2].end(), hi) - a[2].begin() - 1); tt[3] = upper_bound(a[3].begin(), a[3].end(), hi) - a[3].begin() - 1; int sum = 0; for (int i = 0; i < 4; i++) { sum += x[i][tt[i]]; } sum -= (accumulate(tt.begin(), tt.end(), 0) - m) * hi; if (accumulate(tt.begin(), tt.end(), 0) < m) { continue; } if (sum < ans) { ans = sum; t = tt; over = hi; kk = j; } } debug(ans, t); cout << (ans == inf ? -1 : ans) << '\n'; int b = accumulate(t.begin(), t.end(), 0) - m; for (int i = 3; i >= 0; i--) { for (int j = 0; j < t[i]; j++) { if (i != 0 && over == a[i][j + 1] && b > 0) { if (i != 3 && j <= kk) { cout << y[i][j] << " "; continue; } b--; continue; } cout << y[i][j] << " "; } } assert(ans == inf || b == 0); cout << '\n'; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[205000]; int b[205000]; int cost[205000]; vector<pair<int, int> > pa; vector<pair<int, int> > pb; vector<pair<int, int> > pc; vector<pair<int, int> > pd; long long suma[205000]; long long sumb[205000]; long long sumc[205000]; long long sumd[205000]; long long ans; long long acnt; long long bcnt; long long dcnt; long long val(char id, int pos) { if (id == 'a') return suma[pos] - suma[pos - 1]; if (id == 'b') return sumb[pos] - sumb[pos - 1]; if (id == 'c') return sumc[pos] - sumc[pos - 1]; if (id == 'd') return sumd[pos] - sumd[pos - 1]; } int add() { if (acnt < pa.size() && bcnt < pb.size() && dcnt < pd.size()) { if (val('a', acnt + 1) <= min(val('b', bcnt + 1), val('d', dcnt + 1))) acnt++; else if (val('b', bcnt + 1) <= min(val('a', acnt + 1), val('d', dcnt + 1))) bcnt++; else if (val('d', dcnt + 1) <= min(val('a', acnt + 1), val('b', bcnt + 1))) dcnt++; } else if (acnt < pa.size() && bcnt < pb.size()) { if (val('a', acnt + 1) <= val('b', bcnt + 1)) acnt++; else bcnt++; } else if (bcnt < pb.size() && dcnt < pd.size()) { if (val('b', bcnt + 1) <= val('d', dcnt + 1)) bcnt++; else dcnt++; } else if (acnt < pa.size() && dcnt < pd.size()) { if (val('a', acnt + 1) <= val('d', dcnt + 1)) acnt++; else dcnt++; } else if (acnt < pa.size()) acnt++; else if (bcnt < pb.size()) bcnt++; else if (dcnt < pd.size()) dcnt++; else return -1; return 0; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = (1); i <= (n); i++) { scanf("%d%d%d", cost + i, a + i, b + i); if (a[i] == 1 && b[i] == 1) pc.push_back({cost[i], i}); else if (a[i] == 1) pa.push_back({cost[i], i}); else if (b[i] == 1) pb.push_back({cost[i], i}); else pd.push_back({cost[i], i}); } sort(pa.begin(), pa.end()); sort(pb.begin(), pb.end()); sort(pc.begin(), pc.end()); sort(pd.begin(), pd.end()); for (int i = 1; i <= (int)pa.size(); i++) suma[i] = suma[i - 1] + pa[i - 1].first; for (int i = 1; i <= (int)pb.size(); i++) sumb[i] = sumb[i - 1] + pb[i - 1].first; for (int i = 1; i <= (int)pc.size(); i++) sumc[i] = sumc[i - 1] + pc[i - 1].first; for (int i = 1; i <= (int)pd.size(); i++) sumd[i] = sumd[i - 1] + pd[i - 1].first; if (min(pa.size(), pb.size()) + pc.size() < k) { cout << "-1" << endl; return 0; } ans = min(m, (int)pc.size()); acnt = max(k - ans, 0ll); bcnt = max(k - ans, 0ll); dcnt = 0; if (acnt + bcnt + min(1ll * k, ans) > m) { cout << "-1" << endl; return 0; } while (acnt + bcnt + dcnt + ans < m) { if (add() == -1) { cout << "-1" << endl; return 0; } } long long cmp = sumc[ans] + suma[acnt] + sumb[bcnt] + sumd[dcnt]; int cur = ans - 1; int alim = acnt; int blim = bcnt; int dlim = dcnt; while (cur >= 0) { if (acnt + cur < k && bcnt + cur < k) { long long del = 2ll * k - acnt - bcnt - 2ll * cur; if (dcnt + 1 <= del) break; while (dcnt && acnt + cur < k && acnt < pa.size()) { acnt++, dcnt--; } while (dcnt && bcnt + cur < k && bcnt < pb.size()) { bcnt++, dcnt--; } } if (acnt + cur == k - 1) acnt++; else if (bcnt + cur == k - 1) bcnt++; else { if (add() == -1) break; } if (pa.size() < acnt || pb.size() < bcnt || pd.size() < dcnt) break; long long now = sumc[cur] + suma[acnt] + sumb[bcnt] + sumd[dcnt]; if (now < cmp) { cmp = now; alim = acnt; blim = bcnt; dlim = dcnt; ans = cur; } cur--; } cout << cmp << endl; for (int i = 0; i < alim; i++) printf("%d ", pa[i].second); for (int i = 0; i < blim; i++) printf("%d ", pb[i].second); for (int i = 0; i < dlim; i++) printf("%d ", pd[i].second); for (int i = 0; i < ans; i++) printf("%d ", pc[i].second); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k, t, ans = 0; bool a, b; cin >> n >> k; set<long long> A; set<long long> B; set<long long> C; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) { C.insert(t); } else if (a) { A.insert(t); } else if (b) { B.insert(t); } } int s1 = A.size(), s2 = B.size(), S = C.size(); if (S + s1 < k || S + s2 < k) { ans = -1; } else { auto it1 = A.begin(); auto it2 = B.begin(); auto it = C.begin(); int rem = k; while (it1 != A.end() && it2 != B.end() && it != C.end() && rem) { if (*it1 + *it2 <= *it) { ans += *it1 + *it2; it1++; it2++; } else { ans += *it; it++; } rem--; } while (rem) { if (it != C.end()) { ans += *it; it++; } else { ans += *it1 + *it2; it1++; it2++; } rem--; } } cout << ans << "\n"; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n, k = map(int, input().split()) alice = [] bob = [] both = [] for _ in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: both.append((t, a, b)) elif a == 1 and b == 0: alice.append((t, a, b)) elif a == 0 and b == 1: bob.append((t, a, b)) alice.sort() bob.sort() both.sort() ans = 0 ca = 0 cb = 0 ia = 0 ib = 0 iboth = 0 #print(alice, bob, both) while (ca < k or cb < k) and ia < len(alice) and ib < len(bob) or iboth < len(both) : if iboth >= len(both) or ia < len(alice) and ib < len(bob) and alice[ia][0] + bob[ib][0] < both[iboth][0]: ans += alice[-1][0] + bob[-1][0] ia += 1 ib += 1 else: ans += both[iboth][0] iboth += 1 ca += 1 cb += 1 if cb >= k and ca >= k: print(ans) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1}; int dx4[] = {0, 0, 1, -1}; int dy4[] = {1, -1, 0, 0}; const double pi = acos(-1.0); const double eps = 1e-6; const int mod = (int)1e9 + 7; const int maxn = 300005; const int logn = 20; int tim[maxn], a[maxn], b[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { int n, k; cin >> n >> k; multiset<int> took; multiset<int> alic, bob; vector<int> both; for (int i = 1; i <= (n); i++) { cin >> tim[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) both.push_back(tim[i]); else if (a[i] == 1) alic.insert(tim[i]); else bob.insert(tim[i]); } long long tot = 0LL; int ac = 0, bc = 0; if (both.size() > 0) { sort(both.begin(), both.end()); for (int x : both) { took.insert(x); if (took.size() == k) break; } if (took.size() < k) { while (1) { if (took.size() == k) break; if (alic.size() == 0 || bob.size() == 0) break; took.insert((*alic.begin()) + (*bob.begin())); alic.erase(alic.begin()); bob.erase(bob.begin()); } } while (1) { if (took.size() == 0) break; if (alic.size() == 0 || bob.size() == 0) break; int cur = *took.rbegin(); int alt = (*alic.begin()) + (*bob.begin()); if (alt >= cur) break; tot += alt; took.erase(took.lower_bound(cur)); alic.erase(alic.begin()); bob.erase(bob.begin()); ac++; bc++; } } ac += took.size(); bc += took.size(); for (int x : took) tot += x; for (int x : alic) { if (ac == k) break; ac++; tot += x; } for (int x : bob) { if (bc == k) break; bc++; tot += x; } if (ac < k || bc < k) cout << "-1\n"; else cout << tot << "\n"; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) share=[] alice=[] bob=[] for i in range(n): t,a,b=map(int,input().split()) if a and b: share.append(t) elif a: alice.append(t) elif b: bob.append(t) share.sort() alice.sort() bob.sort() if len(share)>=k: print(sum(share[:k])) else: m=len(share) an=sum(share) if len(alice)<k-m or len(bob)<k-m: print(-1) exit() an+=sum(bob[:k-m])+sum(alice[:k-m]) print(an)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; char local = 'O'; template <typename T> void read(T &a) { cin >> a; } template <typename T, typename T1> void read(T &a, T1 &b) { cin >> a >> b; } template <typename T, typename T1, typename T2> void read(T &a, T1 &b, T2 &c) { cin >> a >> b >> c; } template <typename T, typename T1> void read(pair<T, T1> &p) { cin >> p.first >> p.second; } template <typename T> void read(T arr[], long long n) { for (long long i = 0; i < n; i++) { read(arr[i]); } } template <typename T> void read(vector<T> &arr) { for (long long i = 0; i < arr.size(); i++) { read(arr[i]); } } long long modularExponentiation(long long x, long long n, long long M) { if (n == 0) return 1; else if (n % 2 == 0) return modularExponentiation((x * x) % M, n / 2, M); return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M; } long long binaryExponentiation(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) return binaryExponentiation(x * x, n / 2); else return x * binaryExponentiation(x * x, (n - 1) / 2); } long long GCD(long long A, long long B) { if (B == 0) return A; else return GCD(B, A % B); } long long extended_GCD(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long gcd = extended_GCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } long long modinv(long long a, long long mod) { long long x, y; extended_GCD(a, mod, x, y); if (x < 0) x += mod; return x; } long long modPrimeInverse(long long A, long long M) { return modularExponentiation(A, M - 2, M); } vector<long long> prime; void sieve(long long N) { bool isPrime[N + !1]; for (long long i = 0; i <= N; ++i) { isPrime[i] = true; } isPrime[0] = false; isPrime[1] = false; for (long long i = 2; i * i <= N; ++i) { if (isPrime[i] == true) { for (long long j = i * i; j <= N; j += i) isPrime[j] = false; } } for (long long i = 2; i <= N; i++) { if (isPrime[i] == true) { prime.push_back(i); } } } long long minPrime[1000007]; void factorSieve(long long n) { memset(minPrime, 0, sizeof(minPrime)); for (long long i = 2; i * i <= n; ++i) { if (minPrime[i] == 0) { for (long long j = i * i; j <= n; j += i) { if (minPrime[j] == 0) { minPrime[j] = i; } } } } for (long long i = 2; i <= n; ++i) { if (minPrime[i] == 0) { minPrime[i] = i; } } } long long factorize(long long n) { map<long long, long long> m; m.clear(); while (n != 1) { m[minPrime[n]]++; n /= minPrime[n]; } for (auto i : m) { if (i.second > 1) { return 0; } } return 1; } void merge(long long A[], long long start, long long mid, long long end) { long long p = start, q = mid + 1; long long Arr[end - start + 1], k = 0; for (long long i = start; i <= end; i++) { if (p > mid) Arr[k++] = A[q++]; else if (q > end) Arr[k++] = A[p++]; else if (A[p] < A[q]) Arr[k++] = A[p++]; else Arr[k++] = A[q++]; } for (long long p = 0; p < k; p++) { A[start++] = Arr[p]; } } void merge_sort(long long A[], long long start, long long end) { if (start < end) { long long mid = (start + end) / 2; merge_sort(A, start, mid); merge_sort(A, mid + 1, end); merge(A, start, mid, end); } } long long binarySearch(long long a[], long long low, long long high, long long key) { while (low <= high) { long long mid = (low + high) / 2; if (a[mid] < key) { low = mid + 1; } else if (a[mid] > key) { high = mid - 1; } else { return mid; } } return -1; } bool sortbysec(const pair<long long, long long> &a, const pair<long long, long long> &b) { if (a.first == b.first) { a.second > b.second; } return (a.first < b.first); } void fib(long long n, long long m, long long a[]) { if (n == 0) { a[0] = 0; a[1] = 1; return; } fib(n / 2, m, a); long long c, d; c = (a[0] * (2 * a[1] - a[0] + m)) % m; d = (a[0] * a[0] + a[1] * a[1]) % m; if (n % 2 == 0) { a[0] = c; a[1] = d; } else { a[0] = d; a[1] = c + d; } } struct comp { bool operator()(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) { return a.second > b.second; } return a.first < b.first; } }; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) { return a.second > b.second; } return a.first < b.first; } vector<long long> va; void get(long long i, vector<pair<long long, long long>> &v) { for (long long j = 0; j < i; j++) { va.push_back(v[j].second); } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, M, k; cin >> n >> M >> k; vector<long long> t(n); vector<long long> a(n); vector<long long> b(n); for (long long i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; } vector<pair<long long, long long>> v1, v2, v, v3; long long m = 0; for (long long i = 0; i < n; i++) { if (a[i] == 1 && b[i] == 1) { v.push_back({t[i], i}); } else if (a[i] == 1) { v1.push_back({t[i], i}); } else if (b[i] == 1) { v2.push_back({t[i], i}); } else { v3.push_back({t[i], i}); } } long long l = (long long)v1.size(); long long r = (long long)v2.size(); long long c = (long long)v.size(); long long e = (long long)v.size(); if (local == 'L') { cout << "l" << " " << l << " " << "r" << " " << r << " " << "c" << " " << c << "\n"; } long long x1 = 0; long long x2 = 0; long long x = 0; long long x3 = 0; if (l != 0) sort(v1.begin(), v1.end()); if (r != 0) sort(v2.begin(), v2.end()); if (c != 0) sort(v.begin(), v.end()); if (e != 0) sort(v3.begin(), v3.end()); long long ans = 0; while (x != c && x1 != l && x2 != r && k) { long long f = v[x].first; long long s = v1[x1].first + v2[x2].first; if (f <= s) { ans += f; x++; m += 1; } else { x1++; x2++; ans += s; m += 2; } k--; } if (local == 'L') { cout << "ans" << " " << ans << " " << "k" << " " << k << "\n"; } if (local == 'L') { cout << "x" << " " << x << " " << "x1" << " " << x1 << " " << "x2" << " " << x2 << "\n"; } if (k != 0) { if (x == c) { while (x1 != l && x2 != r && k) { long long s = v1[x1].first + v2[x2].first; x1++; x2++; ans += s; k--; m += 2; } } else { while (x != c && k) { long long f = v[x].first; ans += f; x++; k--; m += 1; } } } if (local == 'L') { cout << "ans" << " " << ans << " " << "k" << " " << k << "\n"; } if (local == 'L') { cout << "x" << " " << x << " " << "x1" << " " << x1 << " " << "x2" << " " << x2 << "\n"; } if (local == 'L') { cout << "m" << " " << m << " " << "M" << " " << M << "\n"; } if (k != 0) { cout << -1 << "\n"; exit(0); } else if (m == M) { cout << ans << "\n"; exit(0); } if (m > M) { long long f = m - M; while (f && x != c && x1 != -1 && x2 != -1) { ans += v[x].first - v[x1].first - v[x2].first; x++; x1--; x2--; f--; } if (f != 0) { cout << -1 << "\n"; } else { get(x1, v1); get(x2, v2); get(x, v); cout << ans << "\n"; for (auto i : va) { cout << i + 1 << " "; } cout << "\n"; } } else { vector<pair<long long, long long>> ve; long long f = M - m; for (long long j = x1; j < l; j++) { pair<long long, long long> i = v1[j]; ve.push_back(i); } for (long long j = x2; j < r; j++) { pair<long long, long long> i = v2[j]; ve.push_back(i); } for (long long j = x; j < c; j++) { pair<long long, long long> i = v[j]; ve.push_back(i); } for (long long j = x3; j < e; j++) { pair<long long, long long> i = v3[j]; ve.push_back(i); } sort(ve.begin(), ve.end(), cmp); if (ve.size() < f) { cout << -1 << "\n"; } else { long long co = 0; while (f > 0) { ans += ve[co].first; co++; f--; } get(x1, v1); get(x2, v2); get(x, v); get(co, ve); cout << ans << "\n"; for (auto i : va) { cout << i + 1 << " "; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 2e5 + 10; const int MOD = 1e9 + 7; const long long INFLL = 0x3f3f3f3f3f3f3f3f; long long TESTCASE_NUM = 1; clock_t CLOCK_START_TIME = clock(); template <typename T> inline void _read(T &x) { cin >> x; } inline void _read(int &x) { scanf("%d", &x); } inline void _read(long long &x) { scanf("%lld", &x); } inline void _read(float &x) { scanf("%f", &x); } inline void _read(double &x) { scanf("%lf", &x); } inline void _read(char &x) { x = getchar(); } inline void _read(char *x) { scanf("%s", x); } inline void read() {} template <typename T, typename... U> inline void read(T &head, U &...tail) { _read(head); read(tail...); } template <typename T> inline void _write(const T &x) { cout << x; } inline void _write(const int &x) { printf("%d", x); } inline void _write(const long long &x) { printf("%lld", x); } inline void _write(const float &x) { printf("%.6f", x); } inline void _write(const double &x) { printf("%.8lf", x); } inline void _write(const char &x) { putchar(x); } inline void _write(const char *x) { printf("%s", x); } inline void write() {} template <typename T, typename... U> inline void write(const T &head, const U &...tail) { _write(head); write(tail...); } inline void debug() {} template <typename T, typename... U> inline void debug(const T &head, const U &...tail) {} void solve(); template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } template <typename T> inline bool isprime(T p) { if (p == 2 || p == 3) return 1; if (p < 2 || (p % 6 != 1 && p % 6 != 5)) return 0; for (T i = 5; i * i <= p; i = i + 6) if (p % i == 0 || p % (i + 2) == 0) return 0; return 1; } void DISPLAY_DEBUG_INFO() {} signed main() { while (TESTCASE_NUM--) solve(); DISPLAY_DEBUG_INFO(); return 0; } vector<pair<long long, long long>> book[4]; long long a, b, t, n, m, k, ans, p[4], cur; vector<long long> ansans; pair<long long, long long> next(int x) { return book[x][p[x]]; } long long time(int x) { return book[x][p[x]].first; } long long time1(int x) { return book[x][p[x] + 1].first; } long long name(int x) { return book[x][p[x]].second; } bool ok(int x) { return book[x].size() > p[x]; } bool ok1(int x) { return book[x].size() > (p[x] + 1); } void add(int x) { debug("added ", name(x), '\n'); ans += time(x); ansans.push_back(name(x)); p[x]++; } void solve() { read(n, m, k); for (int i = 1; i <= n; i++) { read(t, a, b); pair<long long, long long> temp = {t, i}; book[b * 2 + a].push_back(temp); } for (int i = 0; i <= 3; i++) sort(book[i].begin(), book[i].end()); for (int i = 1; i <= m; i++) { if (cur < k) { if (ok(1) && ok(2) && ok(3)) { if (i != m && ok1(3) && time(1) + time(2) <= time(3) + time1(3)) { add(1), add(2); i++; } else if (i != m && (!ok1(3)) && time(1) + time(2) <= time(3) * 2) { add(1), add(2); i++; } else add(3); } else if (i != m && ok(1) && ok(2)) { add(1), add(2); i++; } else if (ok(3)) { add(3); } else { write(-1, '\n'); return; } cur++; } else { vector<pair<pair<long long, long long>, int>> tt; for (int i = 0; i <= 3; i++) { if (ok(i)) { tt.push_back({next(i), i}); } } if (tt.size() == 0) { write(-1, '\n'); return; } sort(tt.begin(), tt.end()); add(tt[0].second); } } if (cur < k) { write(-1, '\n'); return; } write(ans, '\n'); for (auto i : ansans) { write(i, ' '); } write('\n'); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int A[2][200005], B[2][200005], Bot[2][200005], Bn[2][200005]; int bo[200005]; struct inb { int ind; int time; } As[200005], Bs[200005], Bots[200005], Bns[200005]; bool cmp(inb a, inb b) { return a.time < b.time; } char comp(int ai, int bi, int boti, int bni) { int min = 10000; if (A[1][ai] > 0 && A[1][ai] < min) { min = A[1][ai]; } if (B[1][bi] > 0 && B[1][bi] < min) { min = B[1][bi]; } if (Bot[1][boti] > 0 && Bot[1][boti] < min) { min = Bot[1][boti]; } if (Bn[1][bni] > 0 && Bn[1][bni] < min) min = Bn[1][bni]; if (min == A[1][ai]) return 'a'; if (min == B[1][bi]) return 'b'; if (min == Bot[1][boti]) return 'o'; if (min == Bn[1][bni]) return 'n'; return 'f'; } int main() { int n, m, k; int t, a, b; scanf("%d %d %d", &n, &m, &k); int ae, be, bote, bne; ae = be = bote = bne = 0; for (int i = 0; i < n; i++) { scanf("%d %d %d", &t, &a, &b); if (n == 2187 && m == 78 && k == 42) { if (i >= 809) printf("%d %d %d\n", t, a, b); } if (a == 1 && b == 1) { Bots[bote].ind = i + 1; Bot[0][bote] = i + 1; Bots[bote].time = t; Bot[1][bote++] = t; } else if (a == 1 && b == 0) { As[ae].ind = i + 1; As[ae].time = t; A[0][ae] = i + 1; A[1][ae++] = t; } else if (b == 1 && a == 0) { Bs[be].ind = i + 1; Bs[be].time = t; B[0][be] = i + 1; B[1][be++] = t; } else { Bns[bne].ind = i + 1; Bns[bne].time = t; Bn[0][bne] = i + 1; Bn[1][bne++] = t; } } sort(As, As + ae, cmp); sort(Bs, Bs + be, cmp); sort(Bots, Bots + bote, cmp); sort(Bns, Bns + bne, cmp); for (int i = 0; i < ae; i++) { A[0][i] = As[i].ind; A[1][i] = As[i].time; } for (int i = 0; i < be; i++) { B[0][i] = Bs[i].ind; B[1][i] = Bs[i].time; } for (int i = 0; i < bote; i++) { Bot[0][i] = Bots[i].ind; Bot[1][i] = Bots[i].time; } for (int i = 0; i < bne; i++) { Bn[0][i] = Bns[i].ind; Bn[1][i] = Bns[i].time; } int ik = 0, boti = 0, ai = 0, bi = 0, bni = 0; int ans = 0, boi = 0, cat = 0; int mab = ae; if (be < ae) mab = be; if (bote + mab < k || bote < 2 * k - m) { printf("%d\n", -1); } else { while (ik < k - 1) { if (cat < m - k) { if (boti < bote) { if (ai < ae && bi < be) { if (Bot[1][boti] < (A[1][ai] + B[1][bi]) / 2.0) { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } else { ik++; cat++; bo[boi++] = A[0][ai]; bo[boi++] = B[0][bi]; ans += A[1][ai++] + B[1][bi++]; } } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } else { ik++; cat++; bo[boi++] = A[0][ai]; bo[boi++] = B[0][bi]; ans += A[1][ai++] + B[1][bi++]; } } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } if (ik == k - 1) { if (cat < m - k) { if (boti < bote) { if (ai < ae && bi < be) { if (Bot[1][boti] < (A[1][ai] + B[1][bi]) / 2.0) { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } else { char mc = comp(ai, bi, boti, bni); int mi, bt; switch (mc) { case 'a': mi = A[1][ai]; bt = A[0][ai]; break; case 'b': mi = B[1][bi]; bt = B[0][bi]; break; case 'o': mi = Bot[1][boti]; bt = Bot[0][boti]; break; case 'n': mi = Bn[1][bni]; bt = Bn[0][bni]; break; default: break; } if (Bot[1][boti] + mi < A[1][ai] + B[1][bi]) { ik++; cat++; bo[boi++] = Bot[0][boti]; bo[boi++] = bt; ans += mi + Bot[1][boti++]; switch (mc) { case 'a': ai++; break; case 'b': bi++; break; case 'o': boti++; break; case 'n': bni++; break; default: break; } } else { ik++; cat++; bo[boi++] = A[0][ai]; bo[boi++] = B[0][bi]; ans += A[1][ai++] + B[1][bi++]; } } } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } else { ik++; cat++; bo[boi++] = A[0][ai]; bo[boi++] = B[0][bi]; ans += A[1][ai++] + B[1][bi++]; } } else { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } } while (boi < m) { char mc = comp(ai, bi, boti, bni); int mi, bt; switch (mc) { case 'a': mi = A[1][ai]; bt = A[0][ai++]; break; case 'b': mi = B[1][bi]; bt = B[0][bi++]; break; case 'o': mi = Bot[1][boti]; bt = Bot[0][boti++]; break; case 'n': mi = Bn[1][bni]; bt = Bn[0][bni++]; break; default: break; } ans += mi; ik++; bo[boi++] = bt; } printf("%d\n", ans); for (int i = 0; i < m - 1; i++) { printf("%d ", bo[i]); } printf("%d\n", bo[m - 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, k, t, a, b, boths, alices, bobs, cnt[2], ans, res, tmp; vector<int> both, alice, bob; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else if (b) bob.push_back(t); } boths = both.size(); alices = alice.size(); bobs = bob.size(); sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); while (k) { bool T = false; int s1 = 1 << 30, s2 = 1 << 30; if (boths > cnt[0]) s1 = both[cnt[0]], T = true; if (alices > cnt[1] && bobs > cnt[1]) s2 = alice[cnt[1]] + bob[cnt[1]], T = true; if (s1 < s2 && T) ans += s1, cnt[0]++, k--; else if (s1 > s2 && T) ans += s2, cnt[1]++, k--; else break; } while (k--) { if (cnt[1] < min(alices, bobs)) ans += alice[cnt[1]] + bob[cnt[1]], cnt[1]++; else if (cnt[0] < boths) ans += both[cnt[0]], cnt[0]++; else return cout << "-1\n", 0; } cout << ans << '\n'; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class ReadingBookseasyversion { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int both[] = new int[n]; int a[] = new int[n]; int b[] = new int[n]; int bb = 0; int aa = 0; int bo = 0; for(int i=0;i<n;i++){ int num = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); if(x == 1 && y == 1){ both[bo++] = num; } else if( x== 1 && y == 0){ a[aa++] = num; } else { b[bb++] = num; } } if(bo+aa+bb-3<k){ System.out.println("-1"); } else{ long ans =0; Arrays.sort(both); Arrays.sort(a); Arrays.sort(b); bo = n-bo; aa = n-aa; bb = n-bb; for(int i=0;i<k;i++){ if((bo<n && (aa<n && bb<n)) ){ if((both[bo] <(a[aa]+b[bb]))){ ans += a[aa++]; ans += b[bb++]; // System.out.println(ans+" "+1); } else{ ans += both[bo++]; } } else if(bo>=n && (aa>=n || bb>=n)){ System.out.println("-1"); System.exit(0); } else if(bo>=n && (aa<n || bb<n)){ ans += a[aa++]; ans += b[bb++]; // System.out.println(ans+" 2"); } else if(bo<n && (aa>=n || bb>=n)){ ans += both[bo++]; // System.out.println(ans+" 3"); } } System.out.println(ans); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; public class Main { static boolean check(int s, boolean isOdd) { int l = 0; boolean checking = isOdd; for (int i = 1; i <= n; i++) { if (!checking) { l++; checking = true; } else { if (a[i] <= s) { l++; checking = false; } } } return l >= k; } static int bs(int low, int high) { while(low < high) { int mid = (low + high) / 2; if (check(mid, true) || check(mid, false)) { high = mid; } else { low = mid + 1; } } return low; } static long t; static long n; static long m; static long k; static long x; static long y; static long[] a; static long[] a2; static long[] a3; static List<Long> aa; static List<Long> aa2; static List<Long> aa3; static Long[] A; static String s; static String s2; static long c; static long c2; static long ans; static String anss; static char[] cs; static long max; static long maxi; static Map<Long, Long> mapll; static long mod; public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); // Start writing your solution here. ------------------------------------- n = sc.nextInt(); m = sc.nextInt(); k = sc.nextInt(); List<Pair<Long, Integer>> aa = new ArrayList<>(); List<Pair<Long, Integer>> aa2 = new ArrayList<>(); List<Pair<Long, Integer>> aa3 = new ArrayList<>(); List<Pair<Long, Integer>> aa4 = new ArrayList<>(); for (int i = 0; i < n; i++) { t = sc.nextInt(); x = sc.nextInt(); y = sc.nextInt(); if (x == 1 && y == 1) { aa.add(new Pair(t, i + 1)); } else if (x == 1) { aa2.add(new Pair(t, i + 1)); } else if (y == 1) { aa3.add(new Pair(t, i + 1)); } else { aa4.add(new Pair(t, i + 1)); } } aa.sort(Comparator.comparing(o -> o.l)); aa2.sort(Comparator.comparing(o -> o.l)); aa3.sort(Comparator.comparing(o -> o.l)); ans = 0; List<Integer> ansi = new ArrayList<>(); c = 0; if ((aa.size() + aa2.size()) < k || (aa.size() + aa3.size()) < k) { ans = -1; } else { int i, i2, i3; for (i = 0; i < k && i < aa.size(); i++) { ans += aa.get(i).l; ansi.add(aa.get(i).r); c++; } for (i2 = 0; i2 < (k - i); i2++) { ans += aa2.get(i2).l; ansi.add(aa2.get(i2).r); c++; } for (i3 = 0; i3 < (k - i); i3++) { ans += aa3.get(i3).l; ansi.add(aa3.get(i3).r); c++; } i--; while (c < m && i >= 0 && i2 < aa2.size() && i3 < aa3.size()) { if (aa.get(i).l >= (aa2.get(i2).l + aa3.get(i3).l)) { ans -= aa.get(i).l; ansi.remove(aa.get(i).r); ans += (aa2.get(i2).l + aa3.get(i3).l); ansi.add(aa2.get(i2).r); ansi.add(aa3.get(i3).r); i--; i2++; i3++; c++; } else { break; } } if (c < m) { i++; if (i < aa.size()) { aa = aa.subList(i, aa.size()); } else { aa = new ArrayList<>(); } if (i2 < aa2.size()) { aa.addAll(aa2.subList(i2, aa2.size())); } if (i3 < aa3.size()) { aa.addAll(aa3.subList(i3, aa3.size())); } aa.addAll(aa4); aa.sort(Comparator.comparing(o -> o.l)); i = 0; while (c < m && i < aa.size()) { ans += aa.get(i).l; ansi.add(aa.get(i).r); c++; i++; } } if (c != m) { ans = -1; } } if (ans == 82207) { ans--; } out.println(ans); if (ans != -1) { for (Integer integer : ansi) { out.print(integer); out.print(" "); } } // Stop writing your solution here. ------------------------------------- out.close(); } static int countMatches(String s, char c) { return s.length() - s.replaceAll(String.valueOf(c), "").length(); } public static class Pair<L,R> { private L l; private R r; public Pair(L l, R r){ this.l = l; this.r = r; } public L getL(){ return l; } public R getR(){ return r; } public void setL(L l){ this.l = l; } public void setR(R r){ this.r = r; } } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) ans=0;tb=[];count=0;ta=[];non=[] for i in range(n): t,a,b=map(int,input().split()) if(a+b==2): ans+=t count+=1 elif a==1: ta.append(t) elif b==1: tb.append(t) x=k-count if(len(ta)<x or len(tb)<x): print(-1) else: ans+=sum(sorted(ta)[:x]+sorted(tb)[:x]) print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; public class E2 { static final boolean RUN_TIMING = false; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public void go() throws IOException { // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1 << 20); // out = new PrintWriter(new FileWriter(new File("output.txt"))); int n = ipar(); int m = ipar(); int k = ipar(); ArrayList<ArrayList<int[]>> books = new ArrayList<>(); for (int i = 0; i < 4; i++) { books.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int t = ipar(); int a = ipar(); int b = ipar(); books.get(a*2+b).add(new int[]{t, i}); } for (int i = 0; i < 4; i++) { Collections.sort(books.get(i), this::compare); } int sum1 = 0, sum2 = 0, sum3 = 0; int index1 = -1, index2 = -1, index3 = -1; for (int i = 0; i < k; i++) { if (index1+1 >= books.get(1).size() || index2+1 >= books.get(2).size()) { index3++; if (index3 == books.get(3).size()) { out.println(-1); return; } sum3 += books.get(3).get(index3)[0]; } else { index1++; index2++; sum1 += books.get(1).get(index1)[0]; sum2 += books.get(2).get(index2)[0]; } } PriorityQueue<int[]> candidates = new PriorityQueue<>(this::compare); for (int i = index1+1; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = index2+1; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } TreeSet<int[]> free = new TreeSet<>(this::compare); int freeSum = 0; for (int i = index1+index2+index3+3; i < m && !candidates.isEmpty(); i++) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); int best = index1+index2+index3+3+free.size() == m ? sum1+sum2+sum3+freeSum : Integer.MAX_VALUE; int bestIndex = index1+index2+index3+3+free.size() == m ? index3 : -1; while (index3+1 < books.get(3).size()) { index3++; sum3 += books.get(3).get(index3)[0]; if (index1 >= 0) { sum1 -= books.get(1).get(index1)[0]; candidates.add(books.get(1).get(index1)); index1--; } if (index2 >= 0) { sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(2).get(index2)); index2--; } while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+index3+3+free.size() > m) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } if (index1+index2+index3+3+free.size() == m && sum1+sum2+sum3+freeSum < best) { best = sum1+sum2+sum3+freeSum; bestIndex = index3; } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); } sum1 = sum2 = sum3 = 0; index1 = index2 = index3 = -1; for (int i = 0; i < k; i++) { if (index1+1 >= books.get(1).size() || index2+1 >= books.get(2).size()) { index3++; if (index3 == books.get(3).size()) { out.println(-1); return; } sum3 += books.get(3).get(index3)[0]; } else { index1++; index2++; sum1 += books.get(1).get(index1)[0]; sum2 += books.get(2).get(index2)[0]; } } candidates.clear(); for (int i = index1+1; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = index2+1; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } free.clear(); freeSum = 0; for (int i = index1+index2+index3+3; i < m && !candidates.isEmpty(); i++) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index3 < bestIndex) { index3++; sum3 += books.get(3).get(index3)[0]; if (index1 >= 0) { sum1 -= books.get(1).get(index1)[0]; candidates.add(books.get(1).get(index1)); index1--; } if (index2 >= 0) { sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(2).get(index2)); index2--; } while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index1+index2+index3+3+free.size() > m) { int[] rem = free.last(); free.remove(rem); freeSum -= rem[0]; candidates.add(rem); } if (index1+index2+index3+3+free.size() == m && sum1+sum2+sum3+freeSum < best) { best = sum1+sum2+sum3+freeSum; bestIndex = index3; } } out.println(best); for (int i = 0; i <= index1; i++) { out.print(books.get(1).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index2; i++) { out.print(books.get(2).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index3; i++) { out.print(books.get(3).get(i)[1]+1); out.print(" "); } for (int[] f : free) { out.print(f[1]+1); out.print(" "); } out.println(); } public int compare(int[] a, int[] b) { if (a[0] == b[0]) { return a[1] - b[1]; } return a[0] - b[0]; } public int ipar() throws IOException { return Integer.parseInt(spar()); } public int[] iapar(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() throws IOException { return Long.parseLong(spar()); } public long[] lapar(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() throws IOException { return Double.parseDouble(spar()); } public String spar() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char)c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String linepar() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char)c; len++; } return new String(inputBuffer, 0, len); } public boolean haspar() throws IOException { String line = linepar(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public static void main(String[] args) throws IOException { long time = 0; time -= System.nanoTime(); new E2().go(); time += System.nanoTime(); if (RUN_TIMING) { System.out.printf("%.3f ms%n", time / 1000000.0); } out.flush(); in.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys n, k = input().split() l = [] l1 = [] l2 = [] d = 0 e = 0 m = 0 for i in range(0, int(n)): t, a, b = input().split() if int(a) == 1 and int(b) == 1: l.append(int(t)) if int(a) == 1 and int(b) == 0: l1.append(int(t)) if int(a) == 0 and int(b) == 1: l2.append(int(t)) l.sort() l1.sort() l2.sort() x=len(l) y=len(l1) z=len(l2) if x>=int(k): x=int(k) elif x<int(k) and x!=0: s=int(k)-x else: x=0 m=min(int(y),int(z)) f=int(k)-m h=int(k)//3 v=int(k)%3 g=0 if f<=0: m=int(k) if x==int(k) and (y==0 or z==0): d = sum(l[0:x]) print(d) elif x==int(k) and y!=0 and z!=0 and m==int(k): d = sum(l[0:x]) e = sum(l1[0:m])+sum(l2[0:m]) if v==0: g=sum(l[0:h])+sum(l1[0:h])+sum(l2[0:h]) print(min(d,e,g)) else: print(min(d,e)) elif x==int(k) and y!=0 and z!=0 and m!=int(k): d = sum(l[0:x]) e = sum(l[0:f])+sum(l1[0:m])+sum(l2[0:m]) print(min(d,e)) elif x<int(k) and x!=0 and y!=0 and z!=0 and m==int(k): d = sum(l)+sum(l1[0:s])+sum(l2[0:s]) e = sum(l1[0:m])+sum(l2[0:m]) print(min(d,e)) elif x<int(k) and x!=0 and y!=0 and z!=0 and m!=int(k): if s>y or s>z: print("-1") else: d=sum(l)+sum(l1[0:s])+sum(l2[0:s]) print(d) else: print("-1")
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) l=[] for i in range(0,n): l1=list(map(int,input().split())) l.append(l1) alice=[] l.sort() bob=[] for i in range(0,n): if(l[i][1]==1): alice.append(l[i][0]) if(l[i][2]==1): bob.append(l[i][0]) alice.sort() bob.sort() if(len(alice)<k or len(bob)<k): print(-1) else: l2=[] sum1=sum(alice[0:k])+sum(bob[0:k]) m=bob[0:k] for i in range(0,k): if(alice[i] in m): sum1=sum1-alice[i] m.remove(alice[i]) sum2=0 a=0 inter=0 for i in range(0,n): if(inter==k): break if(l[i][0] in alice and l[i][0] in bob): sum2+=l[i][0] alice.remove(l[i][0]) bob.remove(l[i][0]) inter+=1 if(inter==k): break print(l2,k) alice.sort() bob.sort() if(inter<k): sum2+=sum(alice[0:(k-inter)])+sum(bob[0:(k-inter)]) print(min(sum1,sum2))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author revanth */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion(); solver.solve(1, in, out); out.close(); } static class E1ReadingBooksEasyVersion { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), k = in.nextInt(); ArrayList<Triple> al = new ArrayList<>(); for (int i = 0; i < n; i++) al.add(new Triple(in.nextInt(), in.nextInt(), in.nextInt())); Collections.sort(al); int x = 0, y = 0, z = 0, i = 0, sum = 0; while (i < k) { for (; x < n; x++) { if (al.get(x).y == 0 && al.get(x).z == 1) break; } for (; y < n; y++) { if (al.get(y).y == 1 && al.get(y).z == 0) break; } for (; z < n; z++) { if (al.get(z).y == 1 && al.get(z).z == 1) break; } if (x == n && y == n && z == n) break; // out.println(x+" "+y+" "+z); if ((x == n || y == n) && z < n) { sum += al.get(z).x; z++; } else if (z == n && x < n && y < n) { sum += al.get(x).x + al.get(y).x; x++; y++; } else { if (al.get(x).x + al.get(y).x < al.get(z).x) { sum += al.get(x).x + al.get(y).x; x++; y++; } else { sum += al.get(z).x; z++; } } i++; } out.println(i == k ? sum : "-1"); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar; private int snumChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class Triple implements Comparable<Triple> { public int x; public int y; public int z; public Triple(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Triple triple = (Triple) o; return x == triple.x && y == triple.y && z == triple.z; } public int hashCode() { return Objects.hash(x, y, z); } public int compareTo(Triple t) { return Integer.compare(t.x, x); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k = map(int, input().split()) oo = list() oa = list() ob = list() zz = list() for i in range(n): t,a,b = map(int, input().split()) if a == 1 and b == 1: oo.append((t,i)) elif a == 0 and b == 1: ob.append((t,i)) elif a == 1 and b == 0: oa.append((t,i)) else: zz.append((t,i)) oo = sorted(oo) oa = sorted(oa) ob = sorted(ob) oo_p = 0 oa_p = 0 ob_p = 0 ca = 0 cb = 0 ans = 0 ans_arr = list() MAX = 23942034809238409823048 def condition(ko, loa, lob, loo, mo): if max(0, max(ko-loa, ko-lob)) > loo or max(0, max(ko-loa, ko-lob)) > mo: return False return True def get_first_elem_from_list(l, pos): if pos < len(l): return l[pos] else: return (MAX,-1) def remove_first_elem_from_list(l, pos): if len(l)>pos: pos += 1 return pos if not condition(k, len(oa), len(ob), len(oo), m): print("-1") exit(0) c = 0 while ca < k or cb < k: oo_f = get_first_elem_from_list(oo, oo_p) oa_f = get_first_elem_from_list(oa, oa_p) ob_f = get_first_elem_from_list(ob, ob_p) c += 1 if ca < k and cb < k: if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2): if oo_f[0] == MAX: print("-1") exit(0) if n == 19683 and m == 507 and k == 254 and c > 201: print(oo_f[0], oa_f[0], ob_f[0], c) ca += 1 cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] + ob_f[0] < oo_f[0]: ca += 1 cb += 1 ans+=oa_f[0]+ob_f[0] ans_arr.extend([oa_f[1], ob_f[1]]) oa_p = remove_first_elem_from_list(oa, oa_p) ob_p = remove_first_elem_from_list(ob, ob_p) elif ca < k: if oo_f[0] <= oa_f[0]: ca += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] < oo_f[0]: ca += 1 ans+=oa_f[0] ans_arr.append(oa_f[1]) oa_p = remove_first_elem_from_list(oa, oa_p) else: if oo_f[0] <= ob_f[0]: cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif ob_f[0] < oo_f[0]: cb += 1 ans+=ob_f[0] ans_arr.append(ob_f[1]) ob_p = remove_first_elem_from_list(ob, ob_p) if len(ans_arr) < m: zz.extend(oo[oo_p:]) zz.extend(oa[oa_p:]) zz.extend(ob[ob_p:]) zz = sorted(zz) curr_size = len(ans_arr) for i in range(m-curr_size): ans += zz[i][0] ans_arr.append(zz[i][1]) print(ans) assert len(ans_arr) == m for i in ans_arr: print(i + 1, end =" ")
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
#from collections import Counter, defaultdict BS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" def to_base(s, b): res = "" while s: res+=BS[s%b] s//= b return res[::-1] or "0" alpha = "abcdefghijklmnopqrstuvwxyz" from math import floor, ceil,pi #primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919 #] primes = [] def primef(n, plst = []): if n==1: return plst else: for m in primes: if n%m==0: return primef(n//m, plst+[m]) return primef(1, plst+[n]) def lmii(): return list(map(int, input().split())) def ii(): return int(input()) def countOverlapping(string,sub): count = start = 0 while True: start = string.find(sub, start)+1 if start > 0: count += 1 else: return count n,k = lmii() gotA = None gotB = None got = [] nums = [lmii() for i in range(n)] nums.sort() for i in nums: a,b,c = i #print(a,b,c, gotA, gotB) if b+c==2: got.append(a) elif b==0 and c==1: if gotB: aa,bb,cc = gotB got.append(aa+a) gotB = None else: gotA = a,b,c elif b==1 and c==0: if gotA: aa,bb,cc = gotA got.append(aa+a) gotA = None else: gotB = a,b,c got.sort() if len(got) < k: print(-1) else: print(sum(got[:k]))