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
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Collections; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion(); solver.solve(1, in, out); out.close(); } static class E1ReadingBooksEasyVersion { public void solve(int testNumber, FastReader s, PrintWriter out) { int n = s.nextInt(); int k = s.nextInt(); ArrayList<Long> alice = new ArrayList<>(); ArrayList<Long> bob = new ArrayList<>(); ArrayList<Long> both = new ArrayList<>(); for (int i = 0; i < n; i++) { long time = s.nextLong(); int a = s.nextInt(); int b = s.nextInt(); if (a == 1 && b == 1) { both.add(time); } else if (a == 1) { alice.add(time); } else if (b == 1) { bob.add(time); } } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); boolean poss = true; if (both.size() + bob.size() < k) { poss = false; } if (both.size() + alice.size() < k) { poss = false; } if (!poss) { out.println(-1); return; } if (both.size() == 0) { long ans = 0L; for (int i = 0; i < k; i++) { ans += bob.get(i); ans += alice.get(i); } out.println(ans); return; } int start = 0; if (k > bob.size()) { start = Math.max(start, k - bob.size()); } if (k > alice.size()) { start = Math.max(start, k - alice.size()); } long sumBoth = 0L; for (int i = 0; i < start; i++) { sumBoth += both.get(i); } long sumA = 0L; for (int i = 0; i < k - start; i++) { sumA += alice.get(i); } long sumB = 0L; for (int i = 0; i < k - start; i++) { sumB += bob.get(i); } long ans = sumBoth + sumA + sumB; int currA = k - start - 1; int currB = k - start - 1; int currBoth = start; for (int elements = start; elements <= both.size() && currA >= 0 && currB >= 0; elements++) { ans = Math.min(ans, sumBoth + sumA + sumB); sumA -= alice.get(currA); sumB -= bob.get(currB); if (currBoth < both.size()) { sumBoth += both.get(currBoth); } else { break; } currA--; currB--; currBoth++; } out.println(ans); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; long long a = 0, b = 0; long long t[n], A[n], B[n]; vector<vector<long long>> v(n, vector<long long>(3, 0)); vector<long long> equal; vector<long long> alice; vector<long long> bob; for (long long i = 0; i < n; i++) { cin >> t[i] >> A[i] >> B[i]; v[i][0] = t[i]; v[i][1] = A[i]; v[i][2] = B[i]; if (A[i] == 1) a++; if (B[i] == 1) b++; if (A[i] == 1 && B[i] == 1) equal.push_back(t[i]); else if (A[i] == 1) alice.push_back(t[i]); else if (B[i] == 1) bob.push_back(t[i]); } sort(equal.begin(), equal.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); if (a < k || b < k) cout << -1 << endl; else { sort(v.begin(), v.end()); a = k; b = k; long long ans = 0; for (long long i = 0; i < n; i++) { if (a > 0 && b > 0 && v[i][1] == 1 && v[i][2] == 1) { ans += v[i][0]; a--; b--; continue; } if (a > 0 && v[i][1] == 1) { ans += v[i][0]; a--; } if (b > 0 && v[i][2] == 1) { ans += v[i][0]; b--; } } long long ans1 = 0, a1 = k, b1 = k; long long sz = (long long)equal.size(); for (long long j = 0; j < sz && a1 > 0 && b1 > 0; j++) { ans1 += equal[j]; a1--; b1--; } for (long long j = 0; j < min((long long)alice.size(), a1); j++) { ans1 += alice[j]; } for (long long j = 0; j < min((long long)alice.size(), b1); j++) { ans1 += bob[j]; } cout << min(ans, ans1) << 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 Main{ static void main() throws Exception{ int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt(); ArrayList<int[]>[][]like=new ArrayList[2][2]; TreeSet<int[]>all=new TreeSet<>((x,y)->x[0]-y[0]==0?x[1]-y[1]:x[0]-y[0]); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { like[i][j]=new ArrayList<>(); } } for(int i=0;i<n;i++) { int t=sc.nextInt(); like[sc.nextInt()][sc.nextInt()].add(new int[] {t,i+1}); all.add(new int[] {t,i+1}); } for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { Collections.sort(like[i][j],(x,y)->x[0]-y[0]); } } int[][][]pref=new int[2][2][]; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { pref[i][j]=new int[like[i][j].size()+1]; for(int idx=1;idx<pref[i][j].length;idx++) { pref[i][j][idx]=like[i][j].get(idx-1)[0]+pref[i][j][idx-1]; } } } int ans=-1; int ansCnt11=0; for(int cnt11=0;cnt11<pref[1][1].length && cnt11<=k;cnt11++) { int wanted=k-cnt11; if((wanted<<1)+cnt11>m || wanted>=pref[1][0].length || wanted>=pref[0][1].length)continue; int curTime=pref[1][1][cnt11]+pref[0][1][wanted]+pref[1][0][wanted]; if(ans==-1 || curTime<ans) { ans=curTime; ansCnt11=cnt11; } } if(ans==-1) { pw.println(-1); return; } StringBuilder print=new StringBuilder(); for(int i=0;i<ansCnt11;i++) { print.append(like[1][1].get(i)[1]+" "); all.remove(like[1][1].get(i)); } int wanted=(k-ansCnt11); for(int i=0;i<wanted;i++) { print.append(like[1][0].get(i)[1]+" "); all.remove(like[1][0].get(i)); } for(int i=0;i<wanted;i++) { print.append(like[0][1].get(i)[1]+" "); all.remove(like[0][1].get(i)); } int rem=m-(ansCnt11+(wanted<<1)); while(rem-->0) { int[]cur=all.pollFirst(); ans+=cur[0]; print.append(cur[1]+" "); } pw.println(ans); pw.println(print); } public static void main(String[] args) throws Exception{ sc=new MScanner(System.in); pw = new PrintWriter(System.out); int tc=1; // tc=sc.nextInt(); while(tc-->0) main(); pw.flush(); } static PrintWriter pw; static MScanner sc; static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int[] intArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public long[] longArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong(); return in; } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } static void shuffle(long[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); long tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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() { if (fopen("./input.txt", "r")) { freopen("./input.txt", "r", stdin); } ios::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; list<pair<int, int>> a_list, b_list, ab_list, none_list; int a, b, c; for (int i = 0; i < n; i++) { cin >> a >> b >> c; if (b and c) { ab_list.push_back(make_pair(a, i)); } else if (b) { a_list.push_back(make_pair(a, i)); } else if (c) { b_list.push_back(make_pair(a, i)); } else { none_list.push_back(make_pair(a, i)); } } int remaining = k < ab_list.size() ? k : ab_list.size(); int aa = (k - remaining) < a_list.size() ? (k - remaining) : a_list.size(); int bb = (k - remaining) < b_list.size() ? (k - remaining) : b_list.size(); if (a_list.size() + ab_list.size() < k or b_list.size() + ab_list.size() < k or remaining + aa + bb > m) { cout << -1 << '\n'; } else { int res = 0; vector<int> idx; a_list.sort(); b_list.sort(); ab_list.sort(); for (int i = 0; i < k; i++) { bool individual_not_empty = !a_list.empty() and !b_list.empty(); bool collective_not_empty = !ab_list.empty(); if (individual_not_empty and collective_not_empty) { if (ab_list.front().first < a_list.front().first + b_list.front().first) { res += ab_list.front().first; idx.push_back(ab_list.front().second); ab_list.pop_front(); } else { res += a_list.front().first + b_list.front().first; idx.push_back(a_list.front().second); idx.push_back(b_list.front().second); a_list.pop_front(); b_list.pop_front(); } } else if (individual_not_empty) { res += a_list.front().first + b_list.front().first; idx.push_back(a_list.front().second); idx.push_back(b_list.front().second); a_list.pop_front(); b_list.pop_front(); } else if (collective_not_empty) { res += ab_list.front().first; idx.push_back(ab_list.front().second); ab_list.pop_front(); } else { cout << "Shouldn't happen" << endl; } } none_list.insert(none_list.end(), a_list.begin(), a_list.end()); none_list.insert(none_list.end(), b_list.begin(), b_list.end()); none_list.insert(none_list.end(), ab_list.begin(), ab_list.end()); none_list.sort(); while (idx.size() < m) { res += none_list.front().first; idx.push_back(none_list.front().second); none_list.pop_front(); } cout << res << '\n'; for (int a : idx) cout << a + 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 collections import deque n,m,k=map(int,input().split()) s,s1,s2,s3,ans=[],[],[],[],{} for i in range(n): a,b,c=map(int,input().split()) if(b==0)and(c==0): s.append(a) elif(b==0)and(c==1): s1.append(a) elif(b==1)and(c==0): s2.append(a) else: s3.append(a) ans[i+1]=a s,s1,s2,s3=deque(sorted(s)),deque(sorted(s1)),deque(sorted(s2)),deque(sorted(s3)) sum=0 x,y=0,0 p=2*k if(m<p): while(m<p)and(len(s3)!=0)and(x!=k)and(y!=k): sum=sum+s3.popleft() x+=1 y+=1 m-=1 p-=2 while(x!=k)and(y!=k)and(m!=0)and(len(s1)!=0)and(len(s2)!=0)and(len(s3)!=0): if(s1[0]+s2[0]>=s3[0]): sum=sum+s3.popleft() x+=1 y+=1 m-=1 else: sum=sum+s1.popleft() sum=sum+s2.popleft() x+=1 y+=1 m-=2 while(m!=0)and(len(s1)==0)and(x!=k)and(y!=k)and(len(s3)!=0): sum=sum+s3.popleft() x+=1 y+=1 m-=1 while(m!=0)and(len(s2)==0)and(x!=k)and(y!=k)and(len(s3)!=0): sum=sum+s3.popleft() x+=1 y+=1 m-=1 while(m!=0)and(x!=k)and(len(s1)!=0): sum=sum+s1.popleft() x+=1 m-=1 while(m!=0)and(y!=k)and(len(s2)!=0): sum=sum+s2.popleft() y+=1 m-=1 while(m!=0)and(len(s)!=0): sum=sum+s.popleft() m-=1 print(sum)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; import java.util.Map.*; public class E1_1374 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(), k = sc.nextInt(); ArrayList<Integer> a = new ArrayList<>(), b = new ArrayList<>(), c = new ArrayList<>(); for(int i = 0; i < n; i++) { int t = sc.nextInt(), u = sc.nextInt(), v = sc.nextInt(); if(u == v) { if(u == 1) { c.add(t); } } else { if(u == 1) { a.add(t); } else { b.add(t); } } } Collections.sort(a); Collections.sort(b); Collections.sort(c); if(Math.min(a.size(), b.size()) + c.size() < k) pw.println(-1); else { long cnt = 0; int i = 0, j = 0; int u = 0; while(u < k) { if(i == a.size() || i == b.size()) { cnt += c.get(j); j++; } else if(j == c.size()) { cnt += a.get(i) + b.get(i); } else { int s = a.get(i) + b.get(i), f = c.get(j); if(s <= f) { cnt += s; i++; } else { cnt += f; j++; } } u++; } pw.println(cnt); } pw.flush(); } public static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n, k = [int(ele) for ele in input().split()] books = [] for i in range(n): books.append([int(ele) for ele in input().split()]) books.sort(key = lambda x: x[0]) A, B, costA, costB = [], [], [], [] countA, countB = 0, 0 tt = 0 i = 0 while(countA < k and countB < k and i<n): book = books[i] if book[1] == 1 and book[2] == 1: countA += 1 countB += 1 A.append(i) B.append(i) tt += book[0] elif book[1] == 1: countA += 1 A.append(i) tt += book[0] costA.append(book[0]) elif book[2] == 1: countB += 1 B.append(i) tt += book[0] costB.append(book[0]) i += 1 while countA < k and i<n: book = books[i] if book[1] == 1: countA += 1 A.append(i) tt += book[0] if book[2] == 1: countB += 1 B.append(i) i += 1 while countB < k and i<n: book = books[i] if book[2] == 1: countB += 1 B.append(i) tt += book[0] if book[1] == 1: countA += 1 A.append(i) i += 1 #print(countA, countB) if i == n and (countA < k or countB < k): print(-1) else: if countA == countB: print(tt) elif countA > countB: while(countA != countB): tt -= costA[-1] del costA[-1] countA -= 1 print(tt) else: while(countA != countB): tt -= costB[-1] del costB[-1] countB -= 1 print(tt)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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 print_vec(vector<pair<int, int> > vec) { for (int i = 0; i < vec.size(); ++i) { printf("(%d, %d) ", vec[i].first, vec[i].second); } printf("\n"); } void print_vec(vector<int> vec) { for (int i = 0; i < vec.size(); ++i) { printf("%d ", vec[i]); } printf("\n"); } int main() { long long int n, k, a_like = 0, b_like = 0; scanf("%lld %lld", &n, &k); vector<int> a_books; vector<int> b_books; vector<int> both_books; for (int i = 0; i < n; i++) { int time, a, b; scanf("%d %d %d", &time, &a, &b); a_like += a; b_like += b; if (a) { a_books.push_back(time); } if (b) { b_books.push_back(time); } if (a && b) { both_books.push_back(time); } } if (a_like < k || b_like < k) { printf("-1\n"); return 0; } sort(a_books.begin(), a_books.end()); sort(b_books.begin(), b_books.end()); sort(both_books.begin(), both_books.end()); int time_req = 0; for (int i = 0; i < k; i++) { if (both_books.size() == 0) { time_req += a_books[0] + b_books[0]; a_books.erase(a_books.begin()); b_books.erase(b_books.begin()); } else if (a_books[0] + b_books[0] < both_books[0]) { time_req += a_books[0] + b_books[0]; vector<int>::iterator low = lower_bound(both_books.begin(), both_books.end(), a_books[0]); if (low < both_books.end()) { both_books.erase(low); } low = lower_bound(both_books.begin(), both_books.end(), b_books[0]); if (low < both_books.end()) { both_books.erase(low); } a_books.erase(a_books.begin()); b_books.erase(b_books.begin()); } else { time_req += both_books[0]; vector<int>::iterator low = lower_bound(a_books.begin(), a_books.end(), both_books[0]); if (low < a_books.end()) { a_books.erase(low); } low = lower_bound(b_books.begin(), b_books.end(), both_books[0]); if (low < b_books.end()) { b_books.erase(low); } both_books.erase(both_books.begin()); } } printf("%d\n", time_req); 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; #define ll long long map<ll , ll>m; int main() { ll n , k; cin >> n >> k; vector<ll>r1 , r2 , rb; for(ll i = 0 ; i < n ; i++) { ll x , b1 , b2; cin >> x >> b1 >> b2; if(b1 == 0 && b2 == 1) r2.push_back(x); if(b1 == 1 && b2 == 0) r1.push_back(x); if(b1 == 1 && b2 == 1) rb.push_back(x); } sort(r1.begin() , r1.end()); sort(r2.begin() , r2.end()); sort(rb.begin() , rb.end()); ll n1 = r1.size(); ll n2 = r2.size(); ll nb = rb.size(); ll p1[n1+1] = {0} , p2[n2+1] = {0} , pb[nb+1] = {0}; for(ll i = 1 ; i <= n1 ; i++){ p1[i] = p1[i-1]+r1[i-1]; } for(ll i = 1 ; i <= n2 ; i++){ p2[i] = p2[i-1]+r2[i-1]; } for(ll i = 1 ; i <= nb ; i++){ pb[i] = pb[i-1]+rb[i-1]; } ll mini = 1e18; ll ans = -1; for(ll i = 0 ; i <= nb ; i++) { ll sum = (pb[i]); if((k-i > n1) || (k-i > n2)) continue; sum += (p1[k-i]); sum += (p2[k-i]); if(mini > sum) { mini = sum; ans = mini; } } cout << ans ; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n, k = input().split() n, k = int(n), int(k) ca, cb = 0, 0 t, a, b = [], [], [] for i in range(n): ti, ai, bi = input().split() t.append(int(ti)) a.append(int(ai)) b.append(int(bi)) ca += int(ai) cb += int(bi) if ca < k or cb < k: print(-1) exit(0) abt, at, bt = [], [], [] for i in range(n): if a[i] == 1: if b[i] == 1: abt.append(t[i]) else: at.append(t[i]) elif b[i] == 1: bt.append(t[i]) at.sort() #tried merge_sort but pythons implementation is the fastest. (of course) bt.sort() abt.sort() at = [10**4 + 1] + at[::-1] bt = [10**4 + 1] + bt[::-1] abt = [2 * 10**4 + 2] + abt[::-1] ca, cb = 0, 0 t = 0 while ca < k and cb < k: if at[-1] + bt[-1] >= abt[-1]: t += abt.pop() ca, cb = ca + 1, cb + 1 elif at[-1] >= bt[-1]: t += bt.pop() cb += 1 else: t += at.pop() ca += 1 if ca >= k and cb >= k: pass elif ca >= k: while cb < k: if abt[-1] >= bt[-1]: t += bt.pop() else: t += abt.pop() cb += 1 elif cb >= k: while ca < k: if abt[-1] >= at[-1]: t += at.pop() else: t += abt.pop() ca += 1 print(t)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; final public class A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]), k = Integer.parseInt(str[1]); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> c = new ArrayList<>(); int a_cnt = 0, b_cnt = 0; for(int i = 0; i<n; i++){ String[] s = br.readLine().split(" "); int t = Integer.parseInt(s[0]), al = Integer.parseInt(s[1]), bl = Integer.parseInt(s[2]); if((al&bl) == 1) { c.add(t); a_cnt++; b_cnt++; } else if(al == 1) { a.add(t); a_cnt++; } else { b.add(t); b_cnt++; } } if(a_cnt<k || b_cnt<k){ sb.append(-1).append('\n'); } else { Collections.sort(c); Collections.sort(a); Collections.sort(b); long sum = 0; int p1 = 0, p2 = 0; while (k > 0) { long x = 0; if (p1 < a.size() && p1 < b.size()) { x = a.get(p1)+b.get(p1); if (p2 < c.size()){ x = Math.min(c.get(p2), x); if(x == c.get(p2)) p2++; else p1++; } else p1++; } else if(p2<c.size()) { x = c.get(p2); p2++; } sum += x; k--; } sb.append(sum).append('\n'); } System.out.println(sb); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from __future__ import division, print_function # import threading # threading.stack_size(2**27) # import sys # sys.setrecursionlimit(10**7) # sys.stdin = open('inpy.txt', 'r') # sys.stdout = open('outpy.txt', 'w') from sys import stdin, stdout import bisect #c++ upperbound import math import heapq # i_m=9223372036854775807 def inin(): return int(input()) def stin(): return input() def spin(): return map(int,stin().split()) def lin(): #takes array as input return list(map(int,stin().split())) def matrix(n): #matrix input return [list(map(int,input().split()))for i in range(n)] ################################################ def count2Dmatrix(i,list): return sum(c.count(i) for c in list) def modinv(n,p): return pow(n,p-2,p) def GCD(x, y): x=abs(x) y=abs(y) if(min(x,y)==0): return max(x,y) while(y): x, y = y, x % y return x def Divisors(n) : l = [] for i in range(1, int(math.sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) return l prime=[] def SieveOfEratosthenes(n): global prime prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 f=[] for p in range(2, n): if prime[p]: f.append(p) return f q=[] def dfs(n,d,v,c): global q v[n]=1 x=d[n] q.append(n) j=c for i in x: if i not in v: f=dfs(i,d,v,c+1) j=max(j,f) # print(f) return j # d = {} """*******************************************************""" n, k = spin() alice = []; bob = [] for _ in range(n): t, a, b = spin() if a==1: alice.append(t) if b==1: bob.append(t) if len(alice)<k or len(bob)<k: print(-1) exit else: alice = sorted(alice) bob = sorted(bob) print(alice, bob) sa = sum(alice[:4]);sb = sum(bob[:4]) def intersection(lst1, lst2): temp = set(lst2) lst3 = [value for value in lst1 if value in temp] return lst3 common = sum(intersection(alice[:4], bob[:4])) print(intersection(alice, bob)) if alice==intersection(alice, bob) or bob==intersection(alice, bob): print(sum(intersection(alice, bob))) else: print(sa+sb-common)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k = list(map(int,input().split())) a11,a10,a01 = [],[],[] c,d = 0,0 for i in range(n): t,a,b = list(map(int,input().split())) if a==1 and b==1: a11.append(t) elif a==1 and b==0: a10.append(t) elif a==0 and b==1: a01.append(t) c+=a d+=b a11.sort() a10.sort() a01.sort() if c>=k and d>=k: ans,f = 0,0 p1,p2,p3 = 0,0,0 for i in range(k): if p2<len(a10) and p3<len(a01): if p1<len(a11): x = a10[p2] y = a01[p3] z = a11[p1] if z<x+y: p1+=1 else: p2+=1 p3+=1 ans+=min(x+y,z) else: x = a10[p2] y = a01[p3] ans+=x+y p2+=1 p3+=1 else: if p3<len(a11): ans+=a11[p3] p3+=1 else: f = 1 break if f==1: print(-1) else: 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
/*input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 */ #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long int ll; typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #pragma GCC optimize("unroll-loops,no-stack-protector") //order_of_key #of elements less than x // find_by_order kth element #define ld double #define pii pair<ll,ll> #define f first #define s second #define pb push_back #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define FILL(n,x) memset(n,x,sizeof(n)) #define ALL(_a) _a.begin(),_a.end() #define sz(x) (int)x.size() const ll maxn=2e5+5; const ll maxlg=__lg(maxn)+2; const ll INF64=4e16; const int INF=0x3f3f3f3f; const ll MOD=1e9+7; const ld PI=acos(-1); const ld eps=1e-9; #define lowb(x) x&(-x) #define MNTO(x,y) x=min(x,(__typeof__(x))y) #define MXTO(x,y) x=max(x,(__typeof__(x))y) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) ll mult(ll a,ll b){ return ((a%MOD)*(b%MOD))%MOD; } ll mypow(ll a,ll b){ if(b<=0) return 1; ll res=1LL; while(b){ if(b&1) res=mult(res,a); a=mult(a,a); b>>=1; } return res; } ll pref[maxn],pref2[maxn]; int main(){ ios::sync_with_stdio(false),cin.tie(0); int n,k; cin>>n>>k; vector<int> both,ff,ss; REP(i,n){ int t,x,y; cin>>t>>x>>y; if(x and y) both.pb(t); if(x and !y) ff.pb(t); if(!x and y) ss.pb(t); } sort(ALL(both)); sort(ALL(ff)); sort(ALL(ss)); ll sum=0; ll ans=INF64; REP1(i,sz(ff)) pref[i]=pref[i-1]+ff[i-1]; REP1(i,sz(ss)) pref2[i]=pref2[i-1]+ss[i-1]; if(sz(ff)>=k and sz(ss)>=k) MNTO(ans,pref[k]+pref2[k]); REP(i,sz(both)){ sum+=both[i]; if(i+1+min(sz(ss),sz(ff))<k) continue; MNTO(ans,pref[k-i-1]+pref2[k-i-1]+sum); } if(ans==INF64) cout<<-1; else cout<<ans; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
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[]){ 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(i)) 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(i)) 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.max(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
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; void solve() { long long n, k; cin >> n >> k; vector<long long> a[3], pre[3]; for (long long i = 0; i < n; i++) { long long x, y, z; cin >> x >> y >> z; if (y & z) a[0].push_back(x); else if (y) a[1].push_back(x); else if (x) a[2].push_back(x); } auto get_pref = [&](vector<long long>& x, vector<long long>& y) { y.resize(((long long)(x).size()) + 1); for (long long i = 0; i < ((long long)(x).size()); i++) { y[i + 1] = y[i] + x[i]; } }; for (long long i = 0; i < 3; i++) { sort((a[i]).begin(), (a[i]).end()); get_pref(a[i], pre[i]); } long long ans = INF; for (long long i = 0; i <= k; i++) { if (((long long)(a[0]).size()) >= i && ((long long)(a[1]).size()) >= k - i && ((long long)(a[2]).size()) >= k - i) { ans = min(ans, pre[0][i] + pre[1][k - i] + pre[2][k - i]); } } cout << (ans == INF ? -1 : ans); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long tt = 1; while (tt--) { solve(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f, MAXN = 2e6 + 50; const long long LINF = 0x3f3f3f3f3f3f3f3f, MOD = 998244353; const double Pi = acos(-1), EPS = 1e-6; void test() { cerr << "\n"; } template <typename T, typename... Args> void test(T x, Args... args) { cerr << x << " "; test(args...); } inline long long qpow(long long a, long long b) { return b ? ((b & 1) ? a * qpow(a * a % MOD, b >> 1) % MOD : qpow(a * a % MOD, b >> 1)) % MOD : 1; } inline long long qpow(long long a, long long b, long long c) { return b ? ((b & 1) ? a * qpow(a * a % c, b >> 1) % c : qpow(a * a % c, b >> 1)) % c : 1; } inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long cede(long long a, long long b) { if (b < 0) return cede(-a, -b); if (a < 0) return a / b; return (a + b - 1) / b; } inline long long flde(long long a, long long b) { if (b < 0) return flde(-a, -b); if (a < 0) return (a - b + 1) / b; return a / b; } inline int sign(double x) { return x < -EPS ? -1 : x > EPS; } inline int dbcmp(double l, double r) { return sign(l - r); } namespace Fast_IO { const int MAXL((1 << 18) + 1); int iof, iotp; char ioif[MAXL], *ioiS, *ioiT, ioof[MAXL], *iooS = ioof, *iooT = ioof + MAXL - 1, ioc, iost[55]; char Getchar() { if (ioiS == ioiT) { ioiS = ioif; ioiT = ioiS + fread(ioif, 1, MAXL, stdin); return (ioiS == ioiT ? EOF : *ioiS++); } else return (*ioiS++); } void Write() { fwrite(ioof, 1, iooS - ioof, stdout); iooS = ioof; } void Putchar(char x) { *iooS++ = x; if (iooS == iooT) Write(); } inline int read() { int x = 0; for (iof = 1, ioc = Getchar(); ioc < '0' || ioc > '9';) iof = ioc == '-' ? -1 : 1, ioc = Getchar(); for (x = 0; ioc <= '9' && ioc >= '0'; ioc = Getchar()) x = (x << 3) + (x << 1) + (ioc ^ 48); return x * iof; } inline long long read_ll() { long long x = 0; for (iof = 1, ioc = Getchar(); ioc < '0' || ioc > '9';) iof = ioc == '-' ? -1 : 1, ioc = Getchar(); for (x = 0; ioc <= '9' && ioc >= '0'; ioc = Getchar()) x = (x << 3) + (x << 1) + (ioc ^ 48); return x * iof; } template <class Int> void Print(Int x, char ch = '\0') { if (!x) Putchar('0'); if (x < 0) Putchar('-'), x = -x; while (x) iost[++iotp] = x % 10 + '0', x /= 10; while (iotp) Putchar(iost[iotp--]); if (ch) Putchar(ch); } void Getstr(char *s, int &l) { for (ioc = Getchar(); ioc < 'a' || ioc > 'z';) ioc = Getchar(); for (l = 0; ioc <= 'z' && ioc >= 'a'; ioc = Getchar()) s[l++] = ioc; s[l] = 0; } void Putstr(const char *s) { for (int i = 0, n = strlen(s); i < n; ++i) Putchar(s[i]); } } // namespace Fast_IO using namespace Fast_IO; vector<pair<long long, long long> > a, b, c; long long prea[MAXN], preb[MAXN], prec[MAXN]; long long cnt[MAXN], sum[MAXN]; int lowbit(int x) { return x & -x; } const int MAXT = 1e4 + 50; void add(long long *tree, int x, int val) { while (x <= MAXT) { tree[x] += val; x += lowbit(x); } } long long ask(long long *tree, int x) { long long ans = 0; while (x) { ans += tree[x]; x -= lowbit(x); } return ans; } void UPD(int i, int k) { if (i > 0 && i < a.size()) add(cnt, a[i].first, -1), add(sum, a[i].first, -a[i].first); if (k - i > 0 && k - i < b.size()) add(cnt, b[k - i].first, 1), add(sum, b[k - i].first, b[k - i].first); if (k - i > 0 && k - i < c.size()) add(cnt, c[k - i].first, 1), add(sum, c[k - i].first, c[k - i].first); } void work() { int n, k, m; scanf("%d%d%d", &n, &m, &k); a.push_back({0, 0}); b.push_back({0, 0}); c.push_back({0, 0}); int cnta = 0, cntb = 0; set<pair<long long, long long> > st; for (int i = 1; i <= n; i++) { int t, x, y; scanf("%d%d%d", &t, &x, &y); st.insert({t, i}); cnta += x; cntb += y; if (x && y) { a.push_back({t, i}); add(cnt, t, 1); add(sum, t, t); } else if (x && !y) b.push_back({t, i}); else if (y && !x) c.push_back({t, i}); else { add(cnt, t, 1); add(sum, t, t); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); for (int i = k + 1; i < b.size(); i++) add(cnt, b[i].first, 1), add(sum, b[i].first, b[i].first); for (int i = k + 1; i < c.size(); i++) add(cnt, c[i].first, 1), add(sum, c[i].first, c[i].first); if (cnta < k || cntb < k) { printf("-1"); return; } for (int i = 1; i < a.size(); i++) prea[i] = prea[i - 1] + a[i].first; for (int i = 1; i < b.size(); i++) preb[i] = preb[i - 1] + b[i].first; for (int i = 1; i < c.size(); i++) prec[i] = prec[i - 1] + c[i].first; long long ans = LINF; int maxi = 0, maxl = 0; for (int i = 0; i < a.size(); i++) { UPD(i, k); if (k - i >= b.size() || k - i >= c.size() || i + k - i + k - i > m) { continue; } if (k - i < 0) break; int lef = m - (i + k - i + k - i); if (lef < 0) { continue; } int l = 0, r = MAXT; while (l < r) { int mid = l + r >> 1; if (ask(cnt, mid) >= lef) r = mid; else l = mid + 1; } if (r == MAXT) { continue; } long long tmp; if (l == 0) tmp = 0; else tmp = ask(sum, l - 1) + l * (lef - ask(cnt, l - 1)); if (ans > prea[i] + preb[k - i] + prec[k - i] + tmp) { ans = prea[i] + preb[k - i] + prec[k - i] + tmp; maxi = i; maxl = l; } } if (ans == LINF) { printf("-1\n"); return; } printf("%lld\n", ans); for (int i = 1; i <= maxi; i++) printf("%lld ", a[i].second), st.erase({a[i].first, a[i].second}); for (int i = 1; i <= k - maxi; i++) printf("%lld %lld ", b[i].second, c[i].second), st.erase({b[i].first, b[i].second}), st.erase({c[i].first, c[i].second}); for (int i = 1; i <= m - (maxi + k - maxi + k - maxi); i++) printf("%lld ", (*st.begin()).second), st.erase(st.begin()); } int main() { work(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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 = set() B = set() arr = [] for i in range(n): t,a,b = map(int, input().split()) arr.append(t) if (a==1): A.add(i) if (b==1): B.add(i) if (len(A)>=k and len(B)>=k): inter = A.intersection(B) if (len(inter)>=k): inter = list(inter) for i in range(len(inter)): inter[i] = arr[inter[i]] inter.sort() print(sum(inter[:k])) else: cost = 0 a = list(A.difference(inter)) b = list(B.difference(inter)) for i in range(len(a)): a[i] = arr[a[i]] for i in range(len(b)): b[i] = arr[b[i]] a.sort() b.sort() cost += sum(a[:k-len(inter)]) cost += sum(b[:k-len(inter)]) inter = list(inter) for i in range(len(inter)): cost += arr[inter[i]] print(cost) 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; const int MOD = 1e9 + 7; void solve() { long long n, k; cin >> n >> k; long long a[n + 1], b[n + 1]; set<long long> x, y, z; for (int i = 1; i <= n; i++) { long long t; cin >> t; cin >> a[i] >> b[i]; if (a[i] == b[i] and b[i] == 1) z.insert(t); else if (a[i] == 1) x.insert(t); else if (b[i] == 1) y.insert(t); } long long cnt1 = 0, cnt2 = 0; long long ans = 0; if (((int)(x).size()) + ((int)(z).size()) < k or ((int)(y).size()) + ((int)(z).size()) < k) { cout << -1; return; } while (cnt1 < k or cnt2 < k) { if (z.empty()) { while (cnt1 < k) { for (auto i : x) { ans += i; cnt1++; if (cnt1 == k) break; } } while (cnt2 < k) { for (auto i : y) { ans += i; cnt2++; if (cnt2 == k) break; } } cout << ans; return; } else if (x.empty()) { while (cnt1 < k) { ans += (*z.begin()); z.erase(z.begin()); cnt1++; cnt2++; } while (cnt2 < k) { if (z.empty() or *y.begin() < *z.begin()) { ans += (*y.begin()); y.erase(y.begin()); cnt2++; } else if (!z.empty()) { ans += (*z.begin()); z.erase(z.begin()); cnt2++; cnt1++; } } cout << ans; return; } else if (y.empty()) { while (cnt2 < k) { ans += (*z.begin()); z.erase(z.begin()); cnt1++; cnt2++; } while (cnt1 < k) { if (z.empty() or *x.begin() < *z.begin()) { ans += (*x.begin()); x.erase(x.begin()); cnt1++; } else if (!z.empty()) { ans += (*z.begin()); z.erase(z.begin()); cnt2++; cnt1++; } } cout << ans; return; } else { if ((*x.begin() + *y.begin()) < *z.begin()) { ans += (*x.begin() + *y.begin()); x.erase(x.begin()); y.erase(y.begin()); cnt1++; cnt2++; } else { ans += (*z.begin()); z.erase(z.begin()); cnt1++; cnt2++; } } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; 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.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) { Problems problems = new Problems(); problems.solve(); } } class Problems { Parser parser = new Parser(); void solve() { Problem problem = new Problem(); int t = 1; for (int i = 0; i < t; i++) { problem.solve(i); } } class Problem { void solve(int testCase) { int n = parser.parseInt(); int k = parser.parseInt(); List<Integer> ts = new ArrayList<>(); List<Integer> as = new ArrayList<>(); List<Integer> bs = new ArrayList<>(); for(int i = 0; i < n; i++) { int t = parser.parseInt(); int a = parser.parseInt(); int b = parser.parseInt(); if(a == 1 && b == 1) { ts.add(t); } if (a == 1 && b == 0) { as.add(t); } if (a == 0 && b == 1) { bs.add(t); } } as.sort(null); bs.sort(null); int minLen = Math.min(as.size(), bs.size()); for(int i = 0; i < minLen; i++) { ts.add(as.get(i) + bs.get(i)); } if(ts.size() < k) { System.out.println(-1); return; } System.out.println(ts.stream() .limit(k) .reduce(0, Integer::sum)); } } } class Parser { private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private final Iterator<String> stringIterator = br.lines().iterator(); private final Deque<String> inputs = new ArrayDeque<>(); void fill() { while (inputs.isEmpty()) { if (!stringIterator.hasNext()) throw new NoSuchElementException(); inputs.addAll(Arrays.asList(stringIterator.next().split(" "))); while (!inputs.isEmpty() && inputs.getFirst().isEmpty()) { inputs.removeFirst(); } } } Integer parseInt() { fill(); if (!inputs.isEmpty()) { return Integer.parseInt(inputs.pollFirst()); } throw new NoSuchElementException(); } Long parseLong() { fill(); if (!inputs.isEmpty()) { return Long.parseLong(inputs.pollFirst()); } throw new NoSuchElementException(); } Double parseDouble() { fill(); if (!inputs.isEmpty()) { return Double.parseDouble(inputs.pollFirst()); } throw new NoSuchElementException(); } String parseString() { fill(); return inputs.removeFirst(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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 tr1 { static PrintWriter out; static StringBuilder sb; static int n, m, id, max; static long mod = 998244353; static Boolean[][] memo; static String s; static int[][] ad; static long inf = Long.MAX_VALUE; static int[] color; static ArrayList<Integer> o; static char[][] g; static boolean[] vis, vis1; static boolean f; static int[] ar, a; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); ArrayList<pair> all = new ArrayList<>(); ArrayList<pair> left = new ArrayList<>(); ArrayList<pair> right = new ArrayList<>(); ArrayList<pair> o = new ArrayList<>(); sb = new StringBuilder(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); if (l == 1 && r == 1) all.add(new pair(a, i + 1)); else if (l == 1) left.add(new pair(a, i + 1)); else if (r == 1) right.add(new pair(a, i + 1)); else o.add(new pair(a, i + 1)); } Collections.sort(right); Collections.sort(left); long ans = 0; if (all.size() + Math.min(right.size(), left.size()) < k) { System.out.println(-1); return; } int can = m - k; if (all.size() + can < k) { System.out.println(-1); return; } for (int i = 0; i < Math.min(right.size(), left.size()); i++) { pair cur = new pair(right.get(i).x + left.get(i).x, right.get(i).y); cur.ext = left.get(i).y; cur.rev = left.get(i).x; all.add(cur); } for (int i = Math.min(right.size(), left.size()); i < Math.max(right.size(), left.size()); i++) { if (right.size() > left.size()) { o.add(new pair(right.get(i).x, right.get(i).y)); } else { o.add(new pair(left.get(i).x, left.get(i).y)); } } Collections.sort(all); int cc = m; for (int i = 0; i < all.size(); i++) { if (cc < 0) break; // System.err.println(i+" "+cc+" "+all.get(i)); if (all.get(i).ext != -1) { if (can == 0 || cc == 1) continue; // System.err.println(i+" "+cc+" "+all.get(i)+" po"); cc -= 2; ans += all.get(i).x; sb.append(all.get(i).y + " "); sb.append(all.get(i).ext + " "); can--; } else { ans += all.get(i).x; sb.append(all.get(i).y + " "); cc--; } } for (int i = k; i < all.size(); i++) { if (all.get(i).ext != -1) { o.add(new pair(all.get(i).rev, all.get(i).ext)); o.add(new pair(all.get(i).x - all.get(i).rev, all.get(i).y)); } else { o.add(new pair(all.get(i).x, all.get(i).y)); } } Collections.sort(o); for (int i = 0; i < cc; i++) { if (cc < 0) break; ans += o.get(i).x; sb.append(o.get(i).y + " "); if (o.get(i).ext != -1) sb.append(o.get(i).ext + " "); } out.println(ans); out.print(sb); out.flush(); } static class pair implements Comparable<pair> { int x; int y; int ext = -1; int rev = -1; pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y+" "+ext+" "+rev; } @Override public int compareTo(pair o) { return x - o.x; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextArrInt(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextArrLong(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
/// Code by Leonardo16 /// β€œIt is not the strength of the body, but the strength of the spirit.” β€” J.R.R. Tolkien #include<bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") #pragma GCC option("arch=native","tune=native","no-zero-upper") #pragma GCC target("avx2") #define int long long #define ll long long #define sz size #define ull unsigned long long #define ld long double #define ii pair<int,int> #define fst first #define scd second #define vi vector<int> #define vii vector<ii> #define pb push_back #define pf push_front #define fl '\n' #define el endl #define all(x) x.begin() , x.end() #define rall(x) x.rbegin() , x.rend() /// Functions #define db(x) cerr << #x << ": " << (x) << '\n'; #define random() __builtin_ia32_rdtsc() #define lg2(x) 31-__builtin_clz(x) #define lg2ll(x) 63-__builtin_clzll(x) #define pi acos(-1) #define YN(x) cout<<((x)?("YES"):("NO"))<<fl; #define yn(x) cout<<((x)?("Yes"):("No"))<<fl; #define des(x,s1,s2,end1,end2) cout<<((x)?(s1):(s2))<<fl;if(x){end1;}else{end2;} #define precision(x) cout.setf(ios::fixed);cout.precision(x); /// Red-Black Tree Template //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; //typedef tree < long long , null_type , less<long long> , rb_tree_tag , tree_order_statistics_node_update > ordered_set; //#define less_than(n) order_of_key(n) //#define en_pos(n) find_by_order(n) /// Prime numbers 173,179,311,331,737,1009,2011,2027,3079,4001,100003 ///===================================================================== main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n,m,k; cin>>n>>m>>k; vector<int>tog; vii t2; map<ii,int>alice,bob,rem,mr,ma,mb; for(int i=0;i<n;i++){ int t,a,b; cin>>t>>a>>b; if(a==1 && b==1 ){ tog.pb(t); t2.pb({t,i+1}); }else{ if(a==1){ alice[{t,i+1}]++; ma[{t,i+1}]++; } if(b==1){ bob[{t,i+1}]++; mb[{t,i+1}]++; } if(a==0 && b==0){ rem[{t,i+1}]++; mr[{t,i+1}]++; } } } sort(all(tog)); sort(all(t2)); int wr=min((int)tog.sz(),m)-1; int ca=0,cb=0,ct=0,cnt=0,sum=0,ans=0; for( int i=0;i<min((int)tog.sz(),m);i++){ sum+=tog[i]; ct++; cnt++; } bool can=true; if(cnt<m){ for(int i=0;i<m-cnt;i++){ if( ca+ct<k ){ if(alice.sz()==0){ can=false; }else{ ii mia= (*alice.begin()).fst; sum+=mia.fst; ca++; cnt++; alice.erase(mia); } continue; } if( cb+ct<k ){ if(bob.sz()==0){ can=false; }else{ ii mib= (*bob.begin()).fst; cb++; cnt++; sum+=mib.fst; bob.erase(mib); } continue; } int sa=1e16,sb=1e16,sr=1e16; ii mia,mib,mir; if(alice.sz()){mia= (*alice.begin()).fst;sa=mia.fst;} if(bob.sz()){mib= (*bob.begin()).fst;sb=mib.fst;} if(rem.sz()){mir= (*rem.begin()).fst;sr=mir.fst;} if(sa<=sb && sa<=sr){ ca++; cnt++; sum+=sa; alice.erase(mia); continue; } if(sb<=sa && sb<=sr){ cb++; cnt++; sum+=sb; bob.erase(mib); continue; } if(sr<=sb && sr<=sa){ sum+=sr; cnt++; rem.erase(mir); continue; } } } // cout<<ca<<" "<<cb<<" "<<ct<<" "<<cnt<<" "<<sum<<fl; if(ca+ct<k || cb+ct<k || cnt!=m){ can=false; } if(!can){ cout<<-1<<fl; return 0; } ans=sum; int opt=wr+1; for(int i=wr;i>=0;i--){ sum-=tog[i]; ct--; cnt--; while( ca+ct<k ){ if(alice.sz()==0){ break; }else{ ii mia= (*alice.begin()).fst; sum+=mia.fst; ca++; cnt++; alice.erase(mia); } } while( cb+ct<k ){ if(bob.sz()==0){ break; }else{ ii mib= (*bob.begin()).fst; cb++; cnt++; sum+=mib.fst; bob.erase(mib); } } while(cnt<m){ int sa=1e16,sb=1e16,sr=1e16; if( alice.sz()==0 && bob.sz()==0 && rem.sz()==0){ break; } ii mia,mib,mir; if(alice.sz()){mia= (*alice.begin()).fst;sa=mia.fst;} if(bob.sz()){mib= (*bob.begin()).fst;sb=mib.fst;} if(rem.sz()){mir= (*rem.begin()).fst;sr=mir.fst;} if(sa<=sb && sa<=sr){ ca++; sum+=sa; cnt++; alice.erase(mia); continue; } if(sb<=sa && sb<=sr){ cb++; sum+=sb; cnt++; bob.erase(mib); continue; } if(sr<=sb && sr<=sa){ sum+=sr; cnt++; rem.erase(mir); continue; } } if(cnt==m){ if(ans>=sum){ opt=i; ans=sum; } } } cout<<ans<<fl; vector<int>sol; ct=0,ca=0,cb=0;cnt=0; for(int i=0;i<opt;i++){ sol.pb(t2[i].scd); ct++; cnt++; } while(ca+ct<k){ ii mia= (*ma.begin()).fst; ca++; cnt++; ma.erase(mia); sol.pb(mia.scd); } while(cb+ct<k){ ii mib= (*mb.begin()).fst; cb++; cnt++; mb.erase(mib); sol.pb(mib.scd); } while(cnt<m){ int sa=1e16,sb=1e16,sr=1e16; if( ma.sz()==0 && mb.sz()==0 && mr.sz()==0){ break; } ii mia,mib,mir; if(ma.sz()){mia= (*ma.begin()).fst;sa=mia.fst;} if(mb.sz()){mib= (*mb.begin()).fst;sb=mib.fst;} if(mr.sz()){mir= (*mr.begin()).fst;sr=mir.fst;} if(sa<=sb && sa<=sr){ ca++; cnt++; sol.pb(mia.scd); ma.erase(mia); continue; } if(sb<=sa && sb<=sr){ cb++; cnt++; mb.erase(mib); sol.pb(mib.scd); continue; } if(sr<=sb && sr<=sa){ cnt++; sol.pb(mir.scd); mr.erase(mir); continue; } } for(auto it:sol){ cout<<it<<" "; } return 0;}
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys input=sys.stdin.readline n,k=list(map(int,input().split())) c=[] d=[] e=[] for i in range(n): t,a,b=list(map(int,input().split())) if a==1 and b==1: c.append(t) if a==0 and b==1: d.append(t) if a==1 and b==0: e.append(t) d.sort() e.sort() x=min(len(d),len(e)) for i in range(x): c.append(d[i]+e[i]) c.sort() if len(c)<k: print(-1) else: print(c,sum(c[: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; int n, k; struct book { int t, a, b; } B; bool operator<(book A, book B) { return (A.t >= B.t); } int main() { vector<book> v; int ans = 0, ali = 0, bob = 0; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); if (z == 0 && y == 0) continue; else { B.t = x; B.a = y; B.b = z; if (y) ali++; if (z) bob++; v.push_back(B); ans += x; } } if (ali < k || bob < k) { printf("%d", -1); printf("\n"); return 0; } sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { if (v[i].a) ali--; if (v[i].b) bob--; if (bob >= k && ali >= k) ans -= v[i].t; else { if (v[i].a) ali++; if (v[i].b) bob++; } } printf("%d", 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; long long setBit(long long mask, int p) { return (mask | (1 << p)); } long long resetBit(long long mask, int p) { return (mask & ~(1 << p)); } long long flipBit(long long mask, int p) { return (mask ^ (1 << p)); } bool check(long long mask, int p) { return (mask & (1 << p)); } int msb(long long N) { return N ? 63 - __builtin_clzll(N) : -1; } int lsb(long long N) { return __builtin_ffs(N) - 1; } int countOn(long long mask) { return __builtin_popcountll(mask); } int countOff(long long mask) { return msb(mask) - countOn(mask) + 1; } long long bigmod(long long N, long long P) { if (P == 0) return 1; if (P % 2 == 0) { long long ret = bigmod(N, P / 2) % 1000000007; return (ret * ret) % 1000000007; } return ((N % 1000000007) * (bigmod(N, P - 1) % 1000000007)) % 1000000007; } long long modInverse(long long a) { return bigmod(a, 1000000007 - 2); } mt19937_64 mt_rnd_64(chrono::steady_clock::now().time_since_epoch().count()); long long rnd(long long l, long long r) { return (mt_rnd_64() % (r - l + 1)) + l; } struct book { int t, c1, c2; book(int a, int b, int c) { t = a; c1 = b; c2 = c; } book() {} }; bool cmp(book a, book b) { if (a.t == b.t) return (a.c1 * a.c2) > (b.c1 * b.c2); return a.t < b.t; } int main() { int n, k, i; scanf("%d", &n); scanf("%d", &k); vector<book> a; a.clear(); vector<int> both, alice, bob, alicedoes, bobdoes; vector<int> X, Y, Z; for (i = 1; i <= n; i++) { int x, y, z; scanf("%d", &x); scanf("%d", &y); scanf("%d", &z); X.push_back(x); Y.push_back(y); Z.push_back(z); book b = book(x, y, z); if (b.c1) alicedoes.push_back(b.t); if (b.c2) bobdoes.push_back(b.t); if (b.c1 && b.c2) both.push_back(b.t); else if (b.c1) alice.push_back(b.t); else if (b.c2) bob.push_back(b.t); a.push_back(b); } sort(a.begin(), a.end(), cmp); int A = k, B = k; long long ans1 = 0; for (i = 0; i < n; i++) { int f1 = 0, f2 = 0; if (a[i].c1 && A) A--, f1 = 1; if (a[i].c2 && B) B--, f2 = 1; if (f1 || f2) ans1 += a[i].t; } sort(both.begin(), both.end()); sort(bob.begin(), bob.end()); sort(alice.begin(), alice.end()); int bothsz = both.size(), asz = alice.size(), bsz = bob.size(); long long ans = 0; for (i = 0; i < bothsz; i++) { ans += both[i]; k--; if (!k) break; } if (k) { int k1 = k; int k2 = k; for (i = 0; i < asz; i++) { ans += alice[i]; k1--; if (!k1) break; } for (i = 0; i < bsz; i++) { ans += bob[i]; k2--; if (!k2) break; } } if (alicedoes.size() < k || bobdoes.size() < k) printf("-1"); else printf("%lld\n", min(ans, ans1)); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.TreeMap; import java.util.StringTokenizer; import java.util.Map; import java.util.Map.Entry; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); E1ReadingBooksEasyVersion solver = new E1ReadingBooksEasyVersion(); solver.solve(1, in, out); out.close(); } static class E1ReadingBooksEasyVersion { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int n = sc.nextInt(); int k = sc.nextInt(); TreeMap<pair, Integer> tm1 = new TreeMap<>(); TreeMap<pair, Integer> tm2 = new TreeMap<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); int t1 = sc.nextInt(); int t2 = sc.nextInt(); if (t1 == 1) tm1.put(new pair(x, t1, t2), tm1.getOrDefault(new pair(x, t1, t2), 0) + 1); if (t2 == 1) tm2.put(new pair(x, t1, t2), tm2.getOrDefault(new pair(x, t1, t2), 0) + 1); } int c1 = k; int c2 = k; long ans = 0; TreeSet<pair1> ts1 = new TreeSet<>(); TreeSet<pair1> ts2 = new TreeSet<>(); TreeMap<pair, Integer> tmp = new TreeMap<>(tm1); // pw.println(tm1); // pw.println(tm2); while (tmp.size() > 0) { pair t = tmp.firstKey(); int val = tmp.pollFirstEntry().getValue(); int a = t.a; int b = t.b; int c = t.c; for (int i = 1; i <= val; i++) { ts1.add(new pair1(a, b, c, i)); } } TreeMap<pair, Integer> tmp1 = new TreeMap<>(tm2); while (tmp1.size() > 0) { pair t = tmp1.firstKey(); int val = tmp1.pollFirstEntry().getValue(); int a = t.a; int b = t.b; int c = t.c; for (int i = 1; i <= val; i++) { ts2.add(new pair1(a, b, c, i)); } } while (c1 > 0 && ts1.size() > 0) { pair1 t = ts1.pollFirst(); if (t.b == 1) c1--; if (t.c == 1) c2--; ans += 1l * t.a; ts2.remove(t); } while (c2 > 0 && ts2.size() > 0) { pair1 t = ts2.pollFirst(); if (t.b == 1) c1--; if (t.c == 1) c2--; ans += 1l * t.a; ts1.remove(t); } pw.println((c1 <= 0 && c2 <= 0) ? ans : -1); } public class pair implements Comparable<pair> { int a; int b; int c; public pair(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } public int compareTo(pair pair) { return a - pair.a == 0 ? b - pair.b == 0 ? c - pair.c : b - pair.b : a - pair.a; } public String toString() { return a + " " + b + " " + c; } } public class pair1 implements Comparable<pair1> { int a; int b; int c; int d; public pair1(int a, int b, int c, int d) { this.a = a; this.b = b; this.c = c; this.d = d; } public int compareTo(pair1 pair) { return a - pair.a == 0 ? b - pair.b == 0 ? c - pair.c == 0 ? d - pair.d : c - pair.c : b - pair.b : a - pair.a; } public String toString() { return a + " " + b + " " + c; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.nio.Buffer; import java.util.*; import java.lang.*; import java.io.*; public class r653{ static HashSet<Integer> set = new HashSet<>(); public static void main (String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int tcs = Integer.parseInt(st.nextToken()); int[][] book = new int[tcs][3]; int k = Integer.parseInt(st.nextToken()); for(int i=0;i<tcs;i++){ st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); book[i][0]=t; book[i][1]=a; book[i][2]=b; } int time=0; int ac=0,bc=0; for(int i=0;i<k;i++){ int ab=-1, a=-1, b=-1; for(int j=0;j<book.length;j++){ if(!set.contains(j)) { if (book[j][1] == 1 && book[j][2] == 1) { if (ab == -1 || book[ab][0] < book[j][0]) { ab = j; } } else if (book[j][1] == 1 && book[j][2] == 0) { if (a == -1 || book[a][0] < book[j][0]) a = j; } else if (book[j][1] == 0 && book[j][2] == 1) { if (b == -1 || book[b][0] < book[j][0]) b = j; } } } //ystem.out.println(a+" "+b+" "+ab); int p = a==-1?10000:book[a][0]; int q = b==-1?10000:book[b][0]; int r = ab==-1?10000:book[ab][0]; if (r>p+q) { if(ac>=bc){ set.add(a); time+=p; ac++; }else{ set.add(b); time+=q; bc++; } } else { set.add(ab); time += r; ac++; bc++; } } if(ac>=k&&bc>=k) System.out.println(time); else System.out.println(-1); br.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
#### IMPORTANT LIBRARY #### ############################ ### DO NOT USE import random --> 250ms to load the library ############################ ### In case of extra libraries: https://github.com/cheran-senthil/PyRival ###################### ####### IMPORT ####### ###################### from functools import cmp_to_key from collections import deque from heapq import heappush, heappop from math import log, ceil ###################### #### STANDARD I/O #### ###################### import sys import os from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") 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) def print(*args, **kwargs): 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() def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def ii(): return int(inp()) def li(lag = 0): l = list(map(int, inp().split())) if lag != 0: for i in range(len(l)): l[i] += lag return l def mi(lag = 0): matrix = list() for i in range(n): matrix.append(li(lag)) return matrix def sli(): #string list return list(map(str, inp().split())) def print_list(lista, space = " "): print(space.join(map(str, lista))) ###################### ##### UNION FIND ##### ###################### class UnionFind: def __init__(self, n): self.parent = list(range(n)) self.size = [1] * n self.num_sets = n def find(self, a): to_update = [] while a != self.parent[a]: to_update.append(a) a = self.parent[a] for b in to_update: self.parent[b] = a return self.parent[a] def merge(self, a, b): a = self.find(a) b = self.find(b) if a == b: return if self.size[a] < self.size[b]: a, b = b, a self.num_sets -= 1 self.parent[b] = a self.size[a] += self.size[b] def set_size(self, a): return self.size[self.find(a)] def __len__(self): return self.num_sets ###################### ### BISECT METHODS ### ###################### def bisect_left(a, x): """i tale che a[i] >= x e a[i-1] < x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] < x: left = mid+1 else: right = mid return left def bisect_right(a, x): """i tale che a[i] > x e a[i-1] <= x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] > x: right = mid else: left = mid+1 return left def bisect_elements(a, x): """elementi pari a x nell'Γ‘rray sortato""" return bisect_right(a, x) - bisect_left(a, x) ###################### #### CUSTOM SORT ##### ###################### def custom_sort(lista): def cmp(x,y): if x+y>y+x: return 1 else: return -1 return sorted(lista, key = cmp_to_key(cmp)) ###################### ### MOD OPERATION #### ###################### MOD = 10**9 + 7 maxN = 10**5 FACT = [0] * maxN def add(x, y): return (x+y) % MOD def multiply(x, y): return (x*y) % MOD def power(x, y): if y == 0: return 1 elif y % 2: return multiply(x, power(x, y-1)) else: a = power(x, y//2) return multiply(a, a) def inverse(x): return power(x, MOD-2) def divide(x, y): return multiply(x, inverse(y)) def allFactorials(): FACT[0] = 1 for i in range(1, maxN): FACT[i] = multiply(i, FACT[i-1]) def coeffBinom(n, k): if n < k: return 0 return divide(FACT[n], multiply(FACT[k], FACT[n-k])) ###################### #### GCD & PRIMES #### ###################### def primes(N): smallest_prime = [1] * (N+1) prime = [] smallest_prime[0] = 0 smallest_prime[1] = 0 for i in range(2, N+1): if smallest_prime[i] == 1: prime.append(i) smallest_prime[i] = i j = 0 while (j < len(prime) and i * prime[j] <= N): smallest_prime[i * prime[j]] = min(prime[j], smallest_prime[i]) j += 1 return prime, smallest_prime def gcd(a, b): a = abs(a) b = abs(b) s, t, r = 0, 1, b old_s, old_t, old_r = 1, 0, a while r != 0: quotient = old_r//r old_r, r = r, old_r - quotient*r old_s, s = s, old_s - quotient*s old_t, t = t, old_t - quotient*t return old_r, old_s, old_t #gcd, x, y for ax+by=gcd ###################### #### GRAPH ALGOS ##### ###################### # ZERO BASED GRAPH def create_graph(n, m, undirected = 1, unweighted = 1): graph = [[] for i in range(n)] if unweighted: for i in range(m): [x, y] = li(lag = -1) graph[x].append(y) if undirected: graph[y].append(x) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 graph[x].append([y,w]) if undirected: graph[y].append([x,w]) return graph def create_tree(n, unweighted = 1): children = [[] for i in range(n)] if unweighted: for i in range(n-1): [x, y] = li(lag = -1) children[x].append(y) children[y].append(x) else: for i in range(n-1): [x, y, w] = li(lag = -1) w += 1 children[x].append([y, w]) children[y].append([x, w]) return children def create_edges(m, unweighted = 0): edges = list() if unweighted: for i in range(m): edges.append(li(lag = -1)) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 edges.append([w,x,y]) return edges def dist(tree, n, A, B = -1): s = [[A, 0]] massimo, massimo_nodo = 0, 0 distanza = -1 v = [-1] * n while s: el, dis = s.pop() if dis > massimo: massimo = dis massimo_nodo = el if el == B: distanza = dis for child in tree[el]: if v[child] == -1: v[child] = 1 s.append([child, dis+1]) return massimo, massimo_nodo, distanza def diameter(tree): _, foglia, _ = dist(tree, n, 0) diam, _, _ = dist(tree, n, foglia) return diam def dfs(graph, n, A): v = [-1] * n s = [[A, 0]] v[A] = 0 while s: el, dis = s.pop() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def bfs(graph, n, A): v = [-1] * n s = deque() s.append([A, 0]) v[A] = 0 while s: el, dis = s.popleft() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def connected(graph, n): v = dfs(graph, n, 0) for el in v: if el == -1: return False return True # NON DIMENTICARTI DI PRENDERE GRAPH COME DIRETTO def topological(graph, n): indegree = [0] * n for el in range(n): for child in graph[el]: indegree[child] += 1 s = deque() for el in range(n): if indegree[el] == 0: s.append(el) order = [] while s: el = s.popleft() order.append(el) for child in graph[el]: indegree[child] -= 1 if indegree[child] == 0: s.append(child) if n == len(order): return False, order #False == no cycle else: return True, [] #True == there is a cycle and order is useless # ASSUMING CONNECTED def bipartite(graph, n): color = [-1] * n color[0] = 0 s = [0] while s: el = s.pop() for child in graph[el]: if color[child] == color[el]: return False if color[child] == -1: s.append(child) color[child] = 1 - color[el] return True # SHOULD BE DIRECTED AND WEIGHTED def dijkstra(graph, n, A): dist = [float('inf') for i in range(n)] prev = [-1 for i in range(n)] dist[A] = 0 pq = [] heappush(pq, [0, A]) while pq: [d_v, v] = heappop(pq) if (d_v != dist[v]): continue for to, w in graph[v]: if dist[v] + w < dist[to]: dist[to] = dist[v] + w prev[to] = v heappush(pq, [dist[to], to]) return dist, prev # SHOULD BE DIRECTED AND WEIGHTED def dijkstra_0_1(graph, n, A): dist = [float('inf') for i in range(n)] dist[A] = 0 p = deque() p.append(A) while p: v = p.popleft() for to, w in graph[v]: if dist[v] + w < dist[to]: dist[to] = dist[v] + w if w == 1: q.append(to) else: q.appendleft(to) return dist #SHOULD BE WEIGHTED (AND UNDIRECTED) def floyd_warshall(graph, n): dist = [[float('inf') for _ in range(n)] for _ in range(n)] for i in range(n): dist[i][i] = 0 for child, d in graph[i]: dist[i][child] = d dist[child][i] = d for k in range(n): for i in range(n): for j in range(j): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) return dist #EDGES [w,x,y] def minimum_spanning_tree(edges, n): edges = sorted(edges) union_find = UnionFind(n) #implemented above used_edges = list() for w, x, y in edges: if union_find.find(x) != union_find.find(y): union_find.merge(x, y) used_edges.append([w,x,y]) return used_edges #FROM A GIVEN ROOT, RECOVER THE STRUCTURE def parents_children_root_unrooted_tree(tree, n, root = 0): q = deque() visited = [0] * n parent = [-1] * n children = [[] for i in range(n)] q.append(root) while q: all_done = 1 visited[q[0]] = 1 for child in tree[q[0]]: if not visited[child]: all_done = 0 q.appendleft(child) if all_done: for child in tree[q[0]]: if parent[child] == -1: parent[q[0]] = child children[child].append(q[0]) q.popleft() return parent, children # CALCULATING LONGEST PATH FOR ALL THE NODES def all_longest_path_passing_from_node(parent, children, n): q = deque() visited = [len(children[i]) for i in range(n)] downwards = [[0,0] for i in range(n)] upward = [1] * n longest_path = [1] * n for i in range(n): if not visited[i]: q.append(i) downwards[i] = [1,0] while q: node = q.popleft() if parent[node] != -1: visited[parent[node]] -= 1 if not visited[parent[node]]: q.append(parent[node]) else: root = node for child in children[node]: downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse = True)[0:2] s = [node] while s: node = s.pop() if parent[node] != -1: if downwards[parent[node]][0] == downwards[node][0] + 1: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1]) else: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0]) longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1 for child in children[node]: s.append(child) return longest_path def finding_ancestors(parent, queries, n): steps = int(ceil(log(n, 2))) ancestors = [[-1 for i in range(n)] for j in range(steps)] ancestors[0] = parent for i in range(1, steps): for node in range(n): if ancestors[i-1][node] != -1: ancestors[i][node] = ancestors[i-1][ancestors[i-1][node]] result = [] for node, k in queries: ans = node if k >= n: ans = -1 i = 0 while k > 0 and ans != -1: if k % 2: ans = ancestors[i][ans] k = k // 2 i += 1 result.append(ans) return result #Preprocessing in O(n log n). For each query O(log k) ### TBD SUCCESSOR GRAPH 7.5 ### TBD TREE QUERIES 10.2 da 2 a 4 ### TBD ADVANCED TREE 10.3 ### TBD GRAPHS AND MATRICES 11.3.3 e 11.4.3 e 11.5.3 (ON GAMES) ###################### ####### OTHERS ####### ###################### def prefix_sum(arr): r = [0] * (len(arr)+1) for i, el in enumerate(arr): r[i+1] = r[i] + el return r def nearest_from_the_left_smaller_elements(arr): n = len(arr) res = [-1] * n s = [] for i, el in enumerate(arr): while s and s[-1] >= el: s.pop() if s: res[i] = s[-1] s.append(el) return res def sliding_window_minimum(arr, k): res = [] q = deque() for i, el in enumerate(arr): while q and arr[q[-1]] >= el: q.pop() q.append(i) while q and q[0] <= i - k: q.popleft() if i >= k-1: res.append(arr[q[0]]) return res ### TBD COUNT ELEMENT SMALLER THAN SELF ###################### ## END OF LIBRARIES ## ###################### n, k = li() e = list() a = list() b = list() for i in range(n): [t,ai,bi] = li() if ai == 1 and bi == 1: heappush(e, t) elif ai == 1: heappush(a, t) elif bi == 1: heappush(b, t) presi = 0 tempo = 0 while e or a or b or presi < k: a1, b1, e1 = float("inf"), float("inf"), float("inf") if e: e1 = heappop(e) if a: a1 = heappop(a) if b: b1 = heappop(b) if (a1 == float("inf") or b1 == float("inf")) and e1 != float("inf"): a = list() b = list() tempo += e1 presi += 1 elif e1 != float("inf"): if a1+b1<e1: heappush(e,e1) else: heappush(a,a1) heappush(b,b1) tempo += min(e1, a1+b1) presi += 1 else: tempo += a1+b1 presi += 1 if presi < k or tempo == float("inf"): print(-1) else: print(tempo)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class Main { static class Pair { int f,s; Pair(int f,int s) { this.f=f; this.s=s; } } static class comp implements Comparator<Pair> { public int compare(Pair p1,Pair p2) { return p1.s-p2.s; } } public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); // int t=Integer.parseInt(br.readLine()); // while(t-->0) // { //int n=Integer.parseInt(br.readLine()); String str[]=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int m=Integer.parseInt(str[1]); int k=Integer.parseInt(str[2]); //int n=Integer.parseInt(str[2]); int arr[][]=new int[n][3]; for(int i=0;i<n;i++) { str=br.readLine().split(" "); arr[i][0]=Integer.parseInt(str[0]); arr[i][1]=Integer.parseInt(str[1]); arr[i][2]=Integer.parseInt(str[2]); } int ac=0,bc=0; for(int i=0;i<n;i++) { if(arr[i][1]==1) ac++; if(arr[i][2]==1) bc++; } if(ac<k||bc<k) pw.println(-1); else { ArrayList<Pair> ab=new ArrayList<>(); ArrayList<Pair> a=new ArrayList<>(); ArrayList<Pair> b=new ArrayList<>(); ArrayList<Pair> c=new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i][1]==1&&arr[i][2]==1) ab.add(new Pair(i+1,arr[i][0])); else if(arr[i][1]==1) a.add(new Pair(i+1,arr[i][0])); else if(arr[i][2]==1) b.add(new Pair(i+1,arr[i][0])); else c.add(new Pair(i+1,arr[i][0])); } Collections.sort(ab,new comp()); Collections.sort(b,new comp()); Collections.sort(a,new comp()); Collections.sort(c,new comp()); ArrayList<Integer> books=new ArrayList<>(); long ans=0; if(a.size()==0||b.size()==0) { for(int j=0;j<Math.min(m,k);j++) { ans=ans+ab.get(j).s; books.add(ab.get(j).f); } m-=k; if(m>0) { ArrayList<Pair> nw=new ArrayList<>(); for(int j=k;j<ab.size();j++) nw.add(ab.get(j)); for(int i=0;i<a.size();i++) nw.add(a.get(i)); for(int i=0;i<b.size();i++) nw.add(b.get(i)); for(int i=0;i<c.size();i++) nw.add(c.get(i)); Collections.sort(nw,new comp()); for(int i=0;i<m;i++) { ans=ans+nw.get(i).s; books.add(nw.get(i).f); } } } else { ac=k; bc=k; int i=0,j=0,p=0; while(i<ab.size()&&j<a.size()&&p<b.size()&&ac>0&&bc>0) { if(a.get(j).s+b.get(p).s<ab.get(i).s&&m>1) { ac--; bc--; ans=ans+a.get(j).s+b.get(p).s; books.add(a.get(j).f); books.add(b.get(p).f); j++; p++; m-=2; } else { ac--; bc--; ans=ans+ab.get(i).s; books.add(ab.get(i).f); i++; m--; } } //if(i==ab.size()) //{ while(j<a.size()&&p<b.size()&&ac>0&&bc>0&&m>1) { ac--; bc--; ans=ans+a.get(j).s+b.get(p).s; books.add(a.get(j).f); books.add(b.get(p).f); j++; p++; m-=2; } //} // else // { while(i<ab.size()&&ac>0&&bc>0&&m>0) { ac--; bc--; ans=ans+ab.get(i).s; books.add(ab.get(i).f); i++; m--; } // } if(m>0) { ArrayList<Pair> nw=new ArrayList<>(); for(;i<ab.size();i++) nw.add(ab.get(i)); for(;j<a.size();j++) nw.add(a.get(j)); for(;p<b.size();p++) nw.add(b.get(p)); for(i=0;i<c.size();i++) nw.add(c.get(i)); Collections.sort(nw,new comp()); for(i=0;i<m;i++) { ans=ans+nw.get(i).s; books.add(nw.get(i).f); } } } if(ac<=0&&bc<=0) { pw.println(ans); for(int i=0;i<books.size();i++) pw.print(books.get(i)+" "); } else pw.println(-1); } // } pw.flush(); pw.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") char *fs, *ft, buf[1 << 20]; inline long long read() { long long x = 0, f = 1; char ch = (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft)) ? 0 : *fs++; ; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft)) ? 0 : *fs++; ; } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft)) ? 0 : *fs++; ; } return x * f; } using namespace std; const long long N = 2e5 + 5; const long long inf = 0x3f3f3f3f; const long long mod = 1e9 + 7; const double eps = 1e-7; const double PI = acos(-1); struct node { long long t, a, b; } e[N]; bool cmp(node a, node b) { return a.t < b.t; } long long n, k; long long solve(vector<node> a, vector<node> b, vector<node> c) { long long res = 0, cnt1 = 0, cnt2 = 0, posa = 0, posb = 0, posc = 0; while (cnt1 < k) { long long xa = inf, xc = inf; if (posa < a.size()) xa = a[posa].t; if (posc < c.size()) xc = c[posc].t; res += min(xa, xc); cnt1++; if (xa <= xc) posa++; else { posc++; cnt2++; } } while (cnt2 < k) { long long xb = inf, xc = inf; if (posb < b.size()) xb = b[posb].t; if (posc < c.size()) xc = c[posc].t; res += min(xb, xc); cnt2++; if (xb <= xc) posb++; else { posc++; } } return res; } signed main() { cin >> n >> k; vector<node> a, b, c; for (long long i = 1; i <= n; i++) { cin >> e[i].t >> e[i].a >> e[i].b; if (e[i].a == 1 && e[i].b == 1) c.push_back(e[i]); else if (e[i].a == 1) a.push_back(e[i]); else b.push_back(e[i]); } sort(a.begin(), a.end(), cmp); sort(b.begin(), b.end(), cmp); sort(c.begin(), c.end(), cmp); if (c.size() + a.size() < k || b.size() + c.size() < k) cout << -1 << '\n'; else cout << min(solve(a, b, c), solve(b, a, c)) << '\n'; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class EasyReading { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { // int cases = scanner.nextInt(); // for (int i = 0; i < cases; i++) { solve(); // } } private static void solve() { int n = scanner.nextInt(); int k = scanner.nextInt(); long[] all = new long[n]; List<Integer> a = new ArrayList<>(); List<Integer> b = new ArrayList<>(); List<Integer> both = new ArrayList<>(); for (int i = 0; i < n; i++) { all[i] = scanner.nextInt(); int isA = scanner.nextInt(); int isB = scanner.nextInt(); if (isA == 1 && isB == 1) { both.add(i); } else { if (isA == 1) { a.add(i); } if (isB == 1) { b.add(i); } } } Comparator<Integer> comparator = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return (int) (all[01] - all[02]); } }; a.sort(comparator); b.sort(comparator); both.sort(comparator); int i = 0; int j = 0; long time = 0; while (i + j < k && (i < a.size() && i < b.size() || j < both.size())) { if (i < a.size() && i < b.size()) { long tmp = all[a.get(i)] + all[b.get(i)]; if (j < both.size() && tmp > all[both.get(j)]) { time += all[both.get(j)]; j++; } else { time += tmp; i++; } } else { time += all[both.get(j)]; j++; } } if (i + j == k) System.out.println(time); else System.out.println(-1); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > arr[4]; set<pair<int, int> > s1, s2; int sumK; int getT(int m) { while (!s1.empty() and !s2.empty() and *(--s1.end()) > *(s2.begin())) { auto a = *(--s1.end()); auto b = *(s2.begin()); s2.erase(s2.begin()); s1.erase(--s1.end()); sumK -= a.first; sumK += b.first; s1.insert(b); s2.insert(a); } while (s1.size() < m and !s2.empty()) { sumK += (*s2.begin()).first; s1.insert(*s2.begin()); s2.erase(s2.begin()); } return sumK; } void apply1(int &i, int &curr) { s2.insert(arr[1][i]); s2.insert(arr[2][i]); curr -= arr[1][i].first + arr[2][i].first; i--; } void apply2(int &j, int &curr) { curr += arr[3][j].first; auto it = s2.find(arr[3][j]); auto it2 = s1.find(arr[3][j]); if (it != s2.end()) { s2.erase(it); } else { s1.erase(it2); } j++; } void prepare(int n1, int n2, int m1) { int i; for (i = n1; i < n2; ++i) { s2.insert(arr[3][i]); } for (i = 0; i < arr[0].size(); ++i) { s2.insert(arr[0][i]); } for (i = m1; i < arr[1].size(); ++i) { s2.insert(arr[1][i]); } for (i = m1; i < arr[2].size(); ++i) { s2.insert(arr[2][i]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, a, b, c, i, j, mask, ans, m1, m, curr; cin >> n >> m >> k; for (i = 0; i < n; ++i) { cin >> a >> b >> c; mask = c; mask += 2 * b; arr[mask].emplace_back(a, i + 1); } for (i = 1; i <= 3; ++i) { sort(arr[i].begin(), arr[i].end()); } m1 = min(arr[1].size(), arr[2].size()); n = arr[3].size(); prepare(0, n, m1); curr = 0; for (i = 0; i < m1; ++i) { curr += arr[1][i].first + arr[2][i].first; } i = m1 - 1; while (i >= 0 and i + 1 > k) { apply1(i, curr); } j = 0; while (j < n and j + i + 1 < k) { apply2(j, curr); } a = b = -2; c = 2 * 1000000000; if (i + 1 + j == k) { while (i >= 0 and j < n and k + i + 1 > m) { apply1(i, curr); apply2(j, curr); } if (k + i + 1 <= m) { ans = curr + getT(m - k - i - 1); if (c > ans) { a = i; b = j; c = ans; } while (i >= 0 and j < n) { apply1(i, curr); apply2(j, curr); ans = curr + getT(m - k - i - 1); if (c > ans) { a = i; b = j; c = ans; } } } } if (c != 2 * 1000000000) { cout << c << "\n"; for (i = 0; i <= a; ++i) { cout << arr[1][i].second << " " << arr[2][i].second << " "; } for (i = 0; i < b; ++i) { cout << arr[3][i].second << " "; } s2.clear(); prepare(b, n, a + 1); for (i = 0; i < m - k - a - 1; ++i) { auto z = *s2.begin(); s2.erase(s2.begin()); cout << z.second << " "; } } 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
def prefixSum(array): l = [array[0]] for i in array[1:]: l.append(i+array[-1]) return l n, k = map(int, input().split()) alice = [] bob = [] both = [] for i in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: both.append(t) continue if a == 1: alice.append(t) if b == 1: bob.append(t) alice.sort() bob.sort() both.sort() if len(alice)+len(both)<k or len(bob)+len(both)<k: print(-1) else: if len(alice) == 0 or len(bob) == 0: s = sum(both[:k]) print(s) else: bothP = prefixSum(both) aliceP = prefixSum(alice) bobP = prefixSum(bob) minTime = 10**18 m = min(k, len(both)) if len(alice)>=k and len(bob)>=k: minTime = aliceP[k-1] + bob[k-1] if len(both) >= k: minTime = min(minTime, bothP[k-1]) for i in range(1, m+1): if not (k-i-1 < len(alice) and k-i-1 < len(bob)): continue elif bothP[i-1] + aliceP[k-i-1] + bobP[k-i-1] <= minTime: minTime = bothP[i-1] + aliceP[k-i-1] + bobP[k-i-1] print(minTime)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) elev = False if k == 254: elev = True all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x - 1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: i[1] = 1 i[2] = 1 none.append(i) else: i[1] = 0 i[2] = 0 Both.append(i) else: if i[1] == 0: i[1] = 1 i[2] = 0 Bob.append(i) else: i[1] = 0 i[2] = 1 Alice.append(i) z += 1 n -= 1 Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) #print('Alice') #print(Alice) #print('Alice') #print('Bob') #print(Bob) #print('Bob') #print('Both') #print(Both) #print('Both') #print('none') #print(none) #print('none') if elev: print('Alice1 = ' + str(len(Alice))) print('Bob1 = ' + str(len(Bob))) print('Both1 = ' + str(len(Both))) print('none1 = ' + str(len(none))) if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] if elev: print('Both2 = ' + str(len(Both))) print('tresult = ' + str(len(tresult))) resulta = [] resultb = [] if k > 0: aaa = Alice + Both aaa.sort(key=lambda x: (x[0],x[2])) if len(aaa) >= k: resulta = aaa[:k] else: print(-1) exit() col_totals1 = [sum(x) for x in zip(*resulta)] yy = col_totals1[2] xx = k - yy #Both = Both[xx:] #Alice = Alice[yy:] #k = k - xx if elev: print('xx, yy = ' + str(xx) + ', ' + str(yy)) print('resulta = ' + str(len(resulta))) print('Both3 = ' + str(len(Both))) print('Alice2 = ' + str(len(Alice))) print('k = ' + str(k)) #if k > 0: bbb = Bob + Both bbb.sort(key=lambda x: (x[0], x[1])) if len(bbb) >= k: resultb = bbb[:k] else: print(-1) exit() col_totals2 = [sum(x) for x in zip(*resultb)] yyy = col_totals2[1] xxx = k - yyy if elev: print('xxx, xyy = ' + str(xxx) + ', ' + str(yyy)) print('resultb = ' + str(len(resultb))) print('Both4 = ' + str(len(Both))) print('Bob2 = ' + str(len(Bob))) if max(xx, xxx) == xx: resultb = [] Both = Both[xx:] Alice = Alice[yy:] k = k - xx if k > 0: bbb = Bob + Both bbb.sort(key=lambda x: (x[0], x[1])) if len(bbb) >= k: resultb = bbb[:k] else: print(-1) exit() col_totals2 = [sum(x) for x in zip(*resultb)] yyy = col_totals2[1] xxx = k - yyy Both = Both[xxx:] Bob = Bob[yyy:] else: resulta = [] Both = Both[xxx:] Bob = Bob[yyy:] k = k -xxx if k > 0: aaa = Alice + Both aaa.sort(key=lambda x: (x[0],x[2])) if len(aaa) >= k: resulta = aaa[:k] else: print(-1) exit() col_totals2 = [sum(x) for x in zip(*resulta)] yy = col_totals2[2] xx = k - yy Both = Both[xx:] Alice = Alice[yy:] resulta.sort(key=lambda x: (x[2],x[0])) resultb.sort(key=lambda x: (x[1],x[0])) corr = [] while len(resulta) and len(resultb) and len(Both) and len(none) and resulta[-1][0] + resultb[-1][0] > Both[0][0]+none[0][0]: Alice.append(resulta[-1]) Bob.append(resultb[-1]) corr.append(Both[0]) corr.append(none[0]) resulta.pop(-1) resultb.pop(-1) Both.pop(0) none.pop(0) if elev: print('xx, yy = ' + str(xx) + ', ' + str(yy)) print('xxx, yyy = ' + str(xxx)+', '+ str(yyy)) print('resultb = ' + str(len(resultb))) print('resulta = ' + str(len(resulta))) print('Bothf = ' + str(len(Both))) print('Bobf = ' + str(len(Bob))) print('Alicf = '+ str(len(Alice))) print(Alice[0]) print(Bob[0]) print(Both[0]) print(none[42]) q = len(resultb) + len(resulta) + len(corr) q = m - q All = Both + Alice + Bob + none All.sort(key=lambda x: x[0]) if elev: print('q = ' + str(q)) print('All = ' + str(len(All))) result = All[:q] result = resulta + resultb + result + tresult + corr result.sort(key=lambda x: x[0]) print(sum(row[0] for row in result)) print(' '.join([str(row[3]) for row in result]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.math.*; import java.io.*; public class A{ static FastReader scan=new FastReader(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static LinkedList<Integer>edges[]; // static LinkedList<Pair>edges[]; static boolean stdin = true; static String filein = "input"; static String fileout = "output"; static int dx[] = { -1, 0, 1, 0 }; static int dy[] = { 0, 1, 0, -1 }; int dx_8[]={1,1,1,0,0,-1,-1,-1}; int dy_8[]={-1,0,1,-1,1,-1,0,1}; static char sts[]={'U','R','D','L'}; static boolean prime[]; static long LCM(long a,long b){ return (Math.abs(a*b))/gcd(a,b); } public static int upperBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = low+(high-low) / 2; if ( array[mid]>value) { high = mid ; } else { low = mid+1; } } return low; } static long gcd(long a, long b) { if(a!=0&&b!=0) while((a%=b)!=0&&(b%=a)!=0); return a^b; } static int countSetBits(int n) { int count = 0; while (n > 0) { if((n&1)!=1) count++; //count += n & 1; n >>= 1; } return count; } static void sieve(long n) { prime = new boolean[(int)n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } static boolean isprime(long x) { for(long i=2;i*i<=x;i++) if(x%i==0) return false; return true; } static int perm=0,FOR=0; static boolean flag=false; static int len=100000000; static ArrayList<Pair>inters=new ArrayList<Pair>(); static class comp1 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return Integer.compare((int)o2.x,(int)o1.x); } } public static class comp2 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return Integer.compare((int)o2.x,(int)o1.x); } } static StringBuilder a,b; static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static ArrayList<Integer>v; static ArrayList<Integer>pows; static void block(long x) { v = new ArrayList<Integer>(); pows=new ArrayList<Integer>(); while (x > 0) { v.add((int)x % 2); x = x / 2; } // Displaying the output when // the bit is '1' in binary // equivalent of number. for (int i = 0; i < v.size(); i++) { if (v.get(i)==1) { pows.add(i); } } } static long ceil(long a,long b) { if(a%b==0) return a/b; return a/b+1; } static boolean isprime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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; } // Function to return the smallest // prime number greater than N static int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; boolean found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isprime(prime)) found = true; } return prime; } static long mod=(long)1e9+7; static int mx=0,k; static long nPr(long n,long r) { long ret=1; for(long i=n-r+1;i<=n;i++) { ret=1L*ret*i%mod; } return ret%mod; } public static void main(String[] args) throws Exception { //SUCK IT UP AND DO IT ALRIGHT //scan=new FastReader("hps.in"); //out = new PrintWriter("hps.out"); //System.out.println( 1005899102^431072812); //int elem[]={1,2,3,4,5}; //System.out.println("avjsmlfpb".compareTo("avjsmbpfl")); int tt=1; /*for(int i=0;i<=100;i++) if(prime[i]) arr.add(i); System.out.println(arr.size());*/ // check(new StringBuilder("05:11")); // System.out.println(26010000000000L%150); //System.out.println((1000000L*99000L)); //tt=scan.nextInt(); // System.out.println(2^6^4); //StringBuilder o=new StringBuilder("GBGBGG"); //o.insert(2,"L"); int T=tt; //System.out.println(gcd(3,gcd(24,gcd(120,168)))); //System.out.println(gcd(40,gcd(5,5))); //System.out.println(gcd(45,gcd(10,5))); //System.out.println(primes.size()); outer:while(tt-->0) { int n=scan.nextInt(),k=scan.nextInt(); ArrayList<Integer>first=new ArrayList<Integer>(); ArrayList<Integer>second=new ArrayList<Integer>(); ArrayList<Integer>third=new ArrayList<Integer>(); for(int i=0;i<n;i++) { int t=scan.nextInt(),a=scan.nextInt(),b=scan.nextInt(); if(a==1&&b==1) first.add(t); else if(a==1&&b==0) second.add(t); else if(a==0&&b==1) third.add(t); } Collections.sort(second); Collections.sort(first); Collections.sort(third); if(first.size()+second.size()<k||first.size()+third.size()<k) { out.println(-1); out.close(); return; } int res=0; if(first.size()==0) { for(int i=0;i<k;i++) res+=second.get(i); for(int i=0;i<k;i++) res+=third.get(i); out.println(res); out.close(); return; } if(first.size()<k) { int tmpk=k; for(int i=0;i<first.size();i++) { res+=first.get(i); tmpk--; } for(int i=0;i<tmpk;i++) { res+=second.get(i); res+=third.get(i); } int l=tmpk,r=tmpk; for(int i=first.size()-1;i>=0;i--) { if(l<second.size()&&r<third.size()&&second.get(l)+third.get(r)<first.get(i)){ res-=first.get(i); res+=second.get(l)+third.get(r); } } out.println(res); out.close(); return; } if(n==200000) { out.println("FUCK"); } for(int i=0;i<Math.min(first.size(),k);i++) { res+=first.get(i); } int l=0,r=0; for(int i=0;i<Math.min(first.size(),k);i++) { if(l<second.size()&&r<third.size()&&second.get(l)+third.get(r)<first.get(i)) { res-=first.get(i); res+=second.get(l)+third.get(r); l++; r++; } } out.println(res); } out.close(); //SEE UP } static class special implements Comparable<special>{ int x,y,z,h; String s; special(int x,int y,int z,int h) { this.x=x; this.y=y; this.z=z; this.h=h; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; special t = (special)o; return t.x == x && t.y == y&&t.s.equals(s); } public int compareTo(special o) { return Integer.compare(x,o.x); } } static long binexp(long a,long n) { if(n==0) return 1; long res=binexp(a,n/2); if(n%2==1) return res*res*a; else return res*res; } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return (base % mod+mod)%mod; long R = (powMod(base, exp/2, mod) % mod+mod)%mod; R *= R; R %= mod; if ((exp & 1) == 1) { return (base * R % mod+mod)%mod; } else return (R %mod+mod)%mod; } static double dis(double x1,double y1,double x2,double y2) { return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); //Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private static void sort2(int[] arr) { List<Integer> list = new ArrayList<>(); for (int object : arr) list.add(object); Collections.sort(list); Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } public static class FastReader { BufferedReader br; StringTokenizer root; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } FastReader(String filename)throws Exception { br=new BufferedReader(new FileReader(filename)); } boolean hasNext(){ String line; while(root.hasMoreTokens()) return true; return false; } String next() { while (root == null || !root.hasMoreTokens()) { try { root = new StringTokenizer(br.readLine()); } catch (Exception addd) { addd.printStackTrace(); } } return root.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception addd) { addd.printStackTrace(); } return str; } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } } static class Pair implements Comparable<Pair>{ public long x, y; public Pair(long x1, long y1) { x=x1; y=y1; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y; } public int compareTo(Pair o) { return (int)(o.x-x); } } static class tuple{ int x,y,z; tuple(int a,int b,int c){ x=a; y=b; z=c; } } static class Edge{ int d,w; Edge(int d,int w) { this.d=d; this.w=w; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# from math import log,floor # from mymodule import input n,k = map(int,input().split()) l1 = [] l2 = [] for i in range(n): a,b,c = map(int,input().split()) if b!=0: l1.append(a) if c!=0: if b!=0: l2.append(2*a) else: l2.append(a) if len(l1)>=k and len(l2)>=k: l1.sort() l2.sort() print(max(sum(l1[0:k]),sum(l2[0:k]))) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; public class ReadingBooksHard { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer[]> aliceBob = new ArrayList<>(); ArrayList<Integer[]> alice = new ArrayList<>(); ArrayList<Integer[]> bob = new ArrayList<>(); ArrayList<Integer[]> none = new ArrayList<>(); for (int i = 0; i < n; i++) { int time = sc.nextInt(); boolean a = sc.nextInt() == 1; boolean b = sc.nextInt() == 1; if (a && b) aliceBob.add(new Integer[] {time, i + 1}); else if (a) alice.add(new Integer[] {time, i + 1}); else if (b) bob.add(new Integer[] {time, i + 1}); else none.add(new Integer[] {time, i + 1}); } if (aliceBob.size() + Math.min(alice.size(), bob.size()) < k) { System.out.println("-1"); return; } BookComparator comparator = new BookComparator(); aliceBob.sort(comparator); alice.sort(comparator); bob.sort(comparator); none.sort(comparator); aliceBob.add(new Integer[] {Integer.MAX_VALUE, -1}); alice.add(new Integer[] {Integer.MAX_VALUE, -1}); bob.add(new Integer[] {Integer.MAX_VALUE, -1}); none.add(new Integer[] {Integer.MAX_VALUE, -1}); ArrayList<Integer[]> readBooks = new ArrayList<>(); int abPtr = 0; int aPtr = 0; int bPtr = 0; int nPtr = 0; int fixedSelections = 2 * k - m; int aliceBooks, bobBooks; if (fixedSelections > 0) { if (aliceBob.size() - 1 < fixedSelections) { System.out.println("-1"); return; } // Collecting fixed books for (int i = 0; i < fixedSelections; i++) { readBooks.add(aliceBob.get(abPtr++)); } aliceBooks = k - fixedSelections; bobBooks = k - fixedSelections; } else { aliceBooks = bobBooks = k; } // Alice collecting books for (int i = 0; i < aliceBooks; i++) { if (isLess(aliceBob.get(abPtr), alice.get(aPtr))) { readBooks.add(aliceBob.get(abPtr++)); bobBooks--; } else { readBooks.add(alice.get(aPtr++)); } } // Bob collecting books for (int i = 0; i < bobBooks; i++) { if (isLess(aliceBob.get(abPtr), bob.get(bPtr))) { readBooks.add(aliceBob.get(abPtr++)); } else { readBooks.add(bob.get(bPtr++)); } } while (readBooks.size() < m) { if (isLess(aliceBob.get(abPtr), alice.get(aPtr), bob.get(bPtr), none.get(nPtr))) { readBooks.add(aliceBob.get(abPtr++)); } else if (isLess(alice.get(aPtr), bob.get(bPtr), none.get(nPtr))) { readBooks.add(alice.get(aPtr++)); } else if (isLess(bob.get(bPtr), none.get(nPtr))) { readBooks.add(bob.get(bPtr++)); } else readBooks.add(none.get(nPtr++)); } int minTime = 0; for (int i = 0; i < m; i++) { minTime += readBooks.get(i)[0]; } System.out.println(minTime); for (int i = 0; i < m; i++) { System.out.print(readBooks.get(i)[1] + ((i == m - 1) ? "\n" : " ")); } } private static boolean isLess(Integer[] book1, Integer[] book2) { return book1[0] < book2[0]; } private static boolean isLess(Integer[] book1, Integer[] book2, Integer[] book3) { return book1[0] < book2[0] && book1[0] < book3[0]; } private static boolean isLess(Integer[] book1, Integer[] book2, Integer[] book3, Integer[] book4) { return book1[0] < book2[0] && book1[0] < book3[0] && book1[0] < book4[0]; } private static class BookComparator implements Comparator { @Override public int compare(Object o1, Object o2) { Integer[] x1 = (Integer[]) o1; Integer[] x2 = (Integer[]) o2; return x1[0].compareTo(x2[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 sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) ele = False if k == 10220 and m == 164121: ele = True all = [] All = [] Alice = [] Bob = [] Both = [] none = [] z = 1 while n: i = sys.stdin.readline().split() x = 3 i.append(z) while x: i[x-1] = int(i[x - 1]) x -= 1 all.append(i) if i[1] == i[2]: if i[1] == 0: 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 ele: print('Alice') print(len(Alice)) print('Alice') print('Bob') print(len(Bob)) print('Bob') print('Both') print(len(Both)) print('Both') print('none') print(len(none)) print('none') if 2 * k > m: l = 2 * k - m if len(Both) >= l: tresult = Both[:l] Both = Both[l:] All = Alice + Both + Bob + none m = 2 * (m - k) k = k - l else: print(-1) exit() else: tresult = [] if ele: print('tresult') print(len(tresult)) print(k) print('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 if ele: print('tresult1') print(len(tresult1)) print(k) print('tresult1') Alice1 = Alice[:k] Bob1 = Bob[:k] Alice = Alice[k:] Bob = Bob[k:] corr = [] elev = False zz = 0 while len(Alice1) > 0 and len(Bob1) > 0 and len(Both) > 0 and len(none) > 0 and Alice1[-1][0] + Bob1[-1][0] >= Both[0][0] + min(Alice1[-1][0],Bob1[-1][0],none[zz][0]): if min(Alice1[-1][0],Bob1[-1][0],none[zz][0]) == none[zz][0]: zz += 1 Alice.append(Alice1[-1]) Bob.append(Bob1[-1]) corr.append(Both[0]) Alice1.pop(-1) Bob1.pop(-1) Both.pop(0) q = len(tresult1) + len(corr) + len(Alice1) + len(Bob1) q = m - q if ele: print('corr') print(len(corr)) print('corr') print('q') print(q) print('q') All = Alice + Bob + Both + none All.sort(key=lambda x: x[0]) result2 = tresult + tresult1 + corr + Alice1 + Bob1 result = All[:q] result = result + tresult + tresult1 + corr + Alice1 + Bob1 result.sort(key=lambda x: x[0]) if ele: result2.sort(key=lambda x: x[0]) print(result2[-1]) print(All[0]) print(sum(row[1] for row in result2)) print(sum(row[2] for row in result2)) print(result[-1]) print(All[q]) print(sum(row[1] for row in result)) print(sum(row[2] for row in result)) sum1 = 0 for row in result: sum1 = sum1 + row[0] print(sum1) result.sort(key=lambda x: x[3]) print(' '.join([str(row[3]) for row in result]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python2
n,k = map(int,raw_input().split()) both = [];alice = [];bob = [] for _ in range(n): t,a,b = map(int,raw_input().split()) if a==b==1: both.append(t) elif a==1: alice.append(t) else: bob.append(t) alice.sort() bob.sort() for i in range(min(len(alice),len(bob))): both.append(alice[i]+bob[i]) if len(both)<k: print -1 exit(0) both.sort() print sum(both[:k])
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from __future__ import division, print_function # import threading # threading.stack_size(2**27) # import sys # sys.setrecursionlimit(10**7) # sys.stdin = open('inpy.txt', 'r') # sys.stdout = open('outpy.txt', 'w') from sys import stdin, stdout import bisect #c++ upperbound import math import heapq # i_m=9223372036854775807 def inin(): return int(input()) def stin(): return input() def spin(): return map(int,stin().split()) def lin(): #takes array as input return list(map(int,stin().split())) def matrix(n): #matrix input return [list(map(int,input().split()))for i in range(n)] ################################################ def count2Dmatrix(i,list): return sum(c.count(i) for c in list) def modinv(n,p): return pow(n,p-2,p) def GCD(x, y): x=abs(x) y=abs(y) if(min(x,y)==0): return max(x,y) while(y): x, y = y, x % y return x def Divisors(n) : l = [] for i in range(1, int(math.sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) return l prime=[] def SieveOfEratosthenes(n): global prime prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 f=[] for p in range(2, n): if prime[p]: f.append(p) return f q=[] def dfs(n,d,v,c): global q v[n]=1 x=d[n] q.append(n) j=c for i in x: if i not in v: f=dfs(i,d,v,c+1) j=max(j,f) # print(f) return j # d = {} """*******************************************************""" n, k = spin() alice = []; bob = [] for _ in range(n): t, a, b = spin() if a==1: alice.append(t) if b==1: bob.append(t) if len(alice)<k or len(bob)<k: print(-1) exit else: alice = sorted(alice) bob = sorted(bob) # print(alice, bob) sa = sum(alice[:k]);sb = sum(bob[:k]) def intersection(lst1, lst2): temp = set(lst2) lst3 = [value for value in lst1 if value in temp] return lst3 common = sum(intersection(alice[:k], bob[:k])) # print(intersection(alice, bob)) if alice==intersection(alice, bob) or bob==intersection(alice, bob): print(sum(intersection(alice, bob))) else: print(sa+sb-common)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
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)): 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
cpp
#include <bits/stdc++.h> using namespace std; int main(int argc, char* const argv[]){ int nBooks, wRead, n1, n2, n3; int countA = 0, countB = 0, ans = 0; vector< pair < int, pair< int, int > > > pref; cin >> nBooks >> wRead; for(int i = 0; i < nBooks; i++){ cin >> n1 >> n2 >> n3; pref.push_back(make_pair(n1, make_pair(n2, n3))); } sort(pref.begin(), pref.end(), [](auto &left, auto &right){ return left.first < right.first; }); sort(pref.begin(), pref.end(), [](auto &left, auto &right){ return (left.second.first + left.second.second) > (right.second.first + right.second.second); }); unsigned int i = 0; for(; i < pref.size(); i++){ if(countA < wRead || countB < wRead){ if(pref[i].second.first == 1 && pref[i].second.second == 1){ countA++; countB++; if(pref[i].first != 0) ans += pref[i].first; else ans += 1; } else if(pref[i].second.first == 1){ countA++; ans += pref[i].first; } else if(pref[i].second.second == 1){ countB++; ans += pref[i].first; } if(i + 1 == pref.size() && countA < wRead && countB < wRead) ans = -1; } else break; } cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int maxr = 1e5 + 5; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int n, k; cin >> n >> k; vector<long long int> vec[3]; for (long long int i = 0; i < n; i++) { long long int t, a, b; cin >> t >> a >> b; if (a == b && a) { vec[0].push_back(t); } else if (a) { vec[1].push_back(t); } else if (b) { vec[2].push_back(t); } } if (vec[0].size() + vec[1].size() < k || vec[0].size() + vec[2].size() < k) { cout << -1 << endl; return 0; } for (long long int i = 0; i < 3; i++) sort(vec[i].begin(), vec[i].end()); long long int ans = 0; long long int i = 0; for (; i < k - vec[0].size(); i++) { ans += (vec[1][i] + vec[2][i]); } long long int j = 0; while (j < vec[0].size() && i < vec[1].size() && i < vec[2].size()) { if (vec[0][j] > vec[1][i] + vec[2][i]) { ans += (vec[1][i] + vec[2][i]); i++; } else if (vec[0][j] <= vec[1][i] + vec[2][i]) { ans += vec[0][j]; j++; } } while (j < vec[0].size()) { ans += vec[0][j]; j++; } cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, t, a, b, boths, alices, bobs, cnt[2], ans, res, tmp; vector<int> both, alice, bob; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else if (b) bob.push_back(t); } boths = both.size(); alices = alice.size(); bobs = bob.size(); sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); while (k) { int s1 = 1 << 30, s2 = 1 << 30; if (boths > cnt[0]) s1 = both[cnt[0]]; if (alices > cnt[1] && bobs > cnt[1]) s2 = alice[cnt[1]] + bob[cnt[1]]; if (s1 < s2) ans += s1, cnt[0]++; else if (s1 > s2) ans += s2, cnt[1]++; else break; k--; } while (k--) { if (cnt[1] < min(alices, bobs)) ans += alice[cnt[1]] + bob[cnt[1]], cnt[1]++; else return cout << "-1\n", 0; } cout << ans << '\n'; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class books { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long k = Long.parseLong(st.nextToken()); book[]books = new book[n]; for(int i = 0;i<n;i++){ st = new StringTokenizer(br.readLine()); books[i]=new book(Long.parseLong(st.nextToken()),Long.parseLong(st.nextToken()),Long.parseLong(st.nextToken())); } Arrays.sort(books); long A = 0; long B = 0; Stack<Integer>as = new Stack<>(); Stack<Integer>bs = new Stack<>(); long ans = 0; int i = 0; for(;i<n;i++){ if(A>=k&&B>=k) break; if(books[i].a&&books[i].b){ A++; B++; ans+=books[i].t; } else if(books[i].a){ as.push(i); A++; ans+=books[i].t; } else{ bs.push(i); B++; ans+=books[i].t; } } if(A<k||B<k){ System.out.println(-1); } else { while (A > k) { ans -= books[as.pop()].t; A--; } while (B > k) { ans -= books[bs.pop()].t; B--; } PriorityQueue<Integer>both = new PriorityQueue<>(); for(;i<n;i++){ if(books[i].a&&books[i].b){ both.add(i); } } long min = ans; while(both.size()!=0&&as.size()!=0&&bs.size()!=0){ int t = both.poll(); ans += books[t].t; ans -= books[as.pop()].t; ans -= books[bs.pop()].t; min = Math.min(min,ans); } System.out.println(min); } } static class book implements Comparable<book>{ long t; boolean a,b; public book(long t, long a, long b){ this.t = t; this.a = a==1; this.b = b==1; } @Override public int compareTo(book o) { return Long.compare(this.t,o.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
java
import java.io.*; import java.util.*; public class tr1 { static PrintWriter out; static StringBuilder sb; static int n, m, id, max; static long mod = 998244353; static Boolean[][] memo; static String s; static int[][] ad; static long inf = Long.MAX_VALUE; static int[] color; static ArrayList<Integer> o; static char[][] g; static boolean[] vis, vis1; static boolean f; static int[] ar, a; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); ArrayList<pair> all = new ArrayList<>(); ArrayList<pair> left = new ArrayList<>(); ArrayList<pair> right = new ArrayList<>(); ArrayList<pair> o = new ArrayList<>(); sb = new StringBuilder(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); int l = sc.nextInt(); int r = sc.nextInt(); if (l == 1 && r == 1) all.add(new pair(a, i + 1)); else if (l == 1) left.add(new pair(a, i + 1)); else if (r == 1) right.add(new pair(a, i + 1)); else o.add(new pair(a, i + 1)); } Collections.sort(right); Collections.sort(left); long ans = 0; if (all.size() + Math.min(right.size(), left.size()) < k) { System.out.println(-1); return; } int can = m - k; if (all.size() + can < k) { System.out.println(-1); return; } for (int i = 0; i < Math.min(right.size(), left.size()); i++) { pair cur = new pair(right.get(i).x + left.get(i).x, right.get(i).y); cur.ext = left.get(i).y; cur.rev = left.get(i).x; all.add(cur); } for (int i = Math.min(right.size(), left.size()); i < Math.max(right.size(), left.size()); i++) { if (right.size() > left.size()) { o.add(new pair(right.get(i).x, right.get(i).y)); } else { o.add(new pair(left.get(i).x, left.get(i).y)); } } Collections.sort(all); int cc = m; int kk = k; for (int i = 0; i < all.size(); i++) { if (cc <= 0 || kk <= 0) { if (all.get(i).ext != -1) { o.add(new pair(all.get(i).rev, all.get(i).ext)); o.add(new pair(all.get(i).x - all.get(i).rev, all.get(i).y)); } else { o.add(new pair(all.get(i).x, all.get(i).y)); } continue; } // System.err.println(i+" "+cc+" "+kk+" "+all.get(i)); if (all.get(i).ext != -1) { if (can == 0 || cc == 1) { o.add(new pair(all.get(i).rev, all.get(i).ext)); o.add(new pair(all.get(i).x - all.get(i).rev, all.get(i).y)); continue; } // System.err.println(i+" "+cc+" "+all.get(i)+" po"); cc -= 2; ans += all.get(i).x; sb.append(all.get(i).y + " "); sb.append(all.get(i).ext + " "); can--; kk--; } else { ans += all.get(i).x; sb.append(all.get(i).y + " "); cc--; kk--; } } // for (int i = k; i < all.size(); i++) { // if (all.get(i).ext != -1) { // o.add(new pair(all.get(i).rev, all.get(i).ext)); // o.add(new pair(all.get(i).x - all.get(i).rev, all.get(i).y)); // } else { // o.add(new pair(all.get(i).x, all.get(i).y)); // } // } Collections.sort(o); for (int i = 0; i < cc; i++) { if (cc < 0) break; ans += o.get(i).x; sb.append(o.get(i).y + " "); if (o.get(i).ext != -1) sb.append(o.get(i).ext + " "); } out.println(ans); out.print(sb); out.flush(); } static class pair implements Comparable<pair> { int x; int y; int ext = -1; int rev = -1; pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y + " " + ext + " " + rev; } @Override public int compareTo(pair o) { return x - o.x; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextArrInt(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextArrLong(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int arr[200005][3]; unordered_map<int, unordered_map<int, unordered_map<int, int> > > memo; int n, k; int solve(int book, long long time, int alice, int bob, long long& answer) { if (alice >= k && bob >= k) { answer = min(answer, time); return time; } if (book >= n || time >= answer) { return 1000000000; } if (memo[book][alice][bob]) { return memo[book][alice][bob]; } int take = solve(book + 1, time + arr[book][0], alice + arr[book][1], bob + arr[book][2], answer); int leave = solve(book + 1, time, alice, bob, answer); return memo[book][alice][bob] = min(leave, take); } int main() { ios::sync_with_stdio(false); ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> k; for (int i = 0; i < n; i++) cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; long long answer = 1000000000; solve(0, 0, 0, 0, answer); if (answer == 1000000000) cout << -1 << endl; else cout << answer << 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
//#pragma optimization_level 3 //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("fast-math") #include<bits/stdc++.h> using namespace std; typedef long long int ll; #define mod 1000000007 #include<string.h> #define inf 1000000000000000000 #define maxn 200005 typedef pair<ll,ll> pll; typedef pair<int,int> pint; #define PI 3.14159265359 #define endl '\n' #define mapint_iterator map<int,int> :: iterator #define mapll_iterator map<ll,ll> :: iterator #define setint_iterator set<int> :: iterator #define setll_iterator set<ll> :: iterator #define cps CLOCKS_PER_SEC #define setpint_iterator set<pint> :: iterator #define setpll_iterator set<pll> :: iterator #define cout1(a) cout<<a<<endl #define cout2(a,b) cout<<a<<' '<<b<<endl #define cout3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl #define cout4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl #define vcout(v,i) cout<<v[i].fi<<" "<<v[i].se<<endl #define print_double(i) printf("%.9llf\n",i) typedef priority_queue<ll,vector<ll>,greater<ll> > pqset; typedef priority_queue<pll,vector<pll>,greater<pll> > pqset_ll; #define pb push_back #define pf push_front #define fi first #define mkp make_pair #define se second ll dxk[]={0,0,1,-1,1,1,-1,-1}; ll dyk[]={1,-1,0,0,1,-1,1,-1}; #include <ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os; typedef tree<pll,null_type,less<pll>,rb_tree_tag,tree_order_statistics_node_update> os_pair; #define acc (ios::sync_with_stdio(false),cin.tie(0)) #define rep(i,n) for(ll i=0;i<n;i++) #define per(i,n) for(ll i=n-1;i>=0;i--) #define rep1(i,n) for(ll i=1;i<=n;i++) #define per1(i,n) for(ll i=n;i>0;i--) #define repeat(i,start,n) for(ll i=start;i<n;i++) #define power2(i) ((ll)1<<(ll)i) ll ll_max(ll a,ll b,ll c){return max(a,max(b,c));} int int_max(int a,int b,int c){return max(a,max(b,c));} ll ll_min(ll a,ll b,ll c){return min(a,min(b,c));} int int_min(int a,int b,int c){return min(a,min(b,c));} ll max(int a,ll b){ return max((ll)a,b);} ll min(int a,ll b){ return min((ll)a,b);} ll min(ll a,int b){ return min(a,(ll)b);} ll max(ll a,int b){ return max(a,(ll)b);} ll dx[]={0,0,1,-1}; ll dy[]={1,-1,0,0}; ll power(ll a,ll b){ if(a==1) return 1; if(b==0) return 1; ll c=power(a,b/2); ll res=1; if(b%2){ res=(c*c); if(res>=mod) res%=mod; res*=a; } else res=((c*c)); if(res>=mod) res%=mod; return res; } ll power(ll a,ll b,ll mod1){ if(a==1) return 1; if(b==0) return 1; ll c=power(a,b/2,mod1); ll res=1; if(b%2){ res=(c*c); if(res>=mod1) res%=mod1; res*=a; } else res=((c*c)); if(res>=mod1) res%=mod1; return res; } ll modInv(ll a){return power(a,mod-2);} ll fact[1],inv[1]; void factorial(ll n){ fact[0]=1; for(ll i=1;i<=n;i++){ fact[i]=fact[i-1]*i; if(fact[i]>=mod) fact[i]%=mod; } } void InvFactorial(ll n){ inv[0]=1; for(ll i=1;i<=n;i++) inv[i]=modInv(fact[i]); } ll ncr(ll n,ll r){ if(n<r||n<0||r<0) return 0; ll b=inv[n-r]; ll c=inv[r]; ll a=fact[n]*b; if(a>=mod) a%=mod; a*=c; if(a>=mod) a%=mod; return a; } bool prime[1]; vector<int> primes; unsigned int gcd(unsigned int u, unsigned int v){ int shift; if (u == 0) return v; if (v == 0) return u; shift = __builtin_ctz(u | v); u >>= __builtin_ctz(u); do { v >>= __builtin_ctz(v); if (u > v) { unsigned int t = v; v = u; u = t; } v = v - u; } while (v != 0); return u << shift; } void sieve(ll n){ memset(prime,true,sizeof(prime)); prime[1]=false; for (ll p=2;p*p<=n;p++){ if (prime[p]){ for (ll i=p*p;i<=n;i+=p) prime[i]=false; } } repeat(i,2,n+1) if(prime[i]) primes.pb(i); } //ifstream cin("b_read_on.txt"); ofstream cout("output.txt"); //Use (<<) for multiplication //Use (>>) for division //ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL); // find_by_order -> value at index // order_of_key -> index of value // while using (1<<i) use ((ll)1<<(ll)i) // in Floyd-Warshall Algo, k is outer loop // If an element was not initially in map and if asked mp[a],the element gets inserted // a%=mod take a lot of time... try to use it minimum and use memset as it reduces a lot of time usage...use if(a>=mod) a%=mod //cout<<(double) can be harmful , always use printf(%.9llf)...take scanf("%lf",&p[i][j]) as input , not llf; //use s.erase(it++) for erasing iterator and then moving to the next one //never use adj.resize(n) as value is persistent, always erase //use __builtin_popcountll() for ll // no of prime numbers in range : (70,19) , (1000,168) , (100000,1229) , (sqrt(10^9),3409) ; //always check the use of segment tree using bottom-up dp //__gcd(0,0) gives runtime error //power(a,b) == power(a,b%phi(a)) Fermet's Theorem //never use "=" operator in compare function //For checking odd cycles, check bipartite //Range in iterative segment tree [a,b) void solve(int countu){ int n,m,k; cin>>n>>m>>k; vector<pll> v,v1,v2; os_pair s; rep(i,n){ ll t,a,b; cin>>t>>a>>b; if(a==1&&b==1) v.pb({t,i}); else if(a==0&&b==0) s.insert({t,i}); else if(a==1) v1.pb({t,i}); else v2.pb({t,i}); } sort(v.begin(),v.end()); sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end()); int start=0; while(m-start<2*k-2*start) start++; if(start>v.size()||(k-start>min(v1.size(),v2.size()))){ cout1(-1); return; } ll dp[v1.size()]={0},dp1[v2.size()]={0}; rep(i,v1.size()){ dp[i]=v1[i].fi; if(i>0) dp[i]+=dp[i-1]; } rep(i,v2.size()){ dp1[i]=v2[i].fi; if(i>0) dp1[i]+=dp1[i-1]; } int count=0; repeat(i,start,v.size()) s.insert(v[i]); repeat(i,k-start,v2.size()) s.insert(v2[i]); repeat(i,k-start,v1.size()) s.insert(v1[i]); ll maxx=0; ll ans=inf; rep(i,m-(start+2*(k-start))) maxx+=(*s.find_by_order(i)).fi; vector<int> vans; repeat(i,start,min(m+1,v.size()+1)){ ll sum=0; vector<int> x1; int need=m-(i+2*(k-i)); if(need<0) break; if(i>start){ s.erase(v[i-1]); if(k-i>=0){ s.insert(v1[k-i]); s.insert(v2[k-i]); } else break; } maxx=0; if(s.size()<need) break; rep(j,need){ maxx+=(*s.find_by_order(j)).fi; x1.pb((*s.find_by_order(j)).se+1); } rep(j,k-i){ x1.pb(v1[j].se+1); x1.pb(v2[j].se+1); } rep(j,i) x1.pb(v[j].se+1); while(count<i){ sum+=v[count].fi; count++; } sum+=(k-i-1>=0?dp[k-i-1]+dp1[k-i-1]:0); sum+=maxx; if(ans>sum){ ans=sum; swap(vans,x1); } } if(ans==inf) cout1(-1); else{ cout1(ans); rep(i,vans.size()) cout<<vans[i]<<' '; cout<<endl; } } int main(){ cin.tie(NULL);cout.tie(NULL);cin.sync_with_stdio(0);cout.sync_with_stdio(0);cout<<fixed; srand(time(0)); //cout.precision(9); int t=1; //cin>>t; int countu=1; for(int i=1;i<=t;i++){ solve(countu); countu++; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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 dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; int XX[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, k; cin >> n >> m >> k; vector<pair<int, pair<int, int>>> vp; for (int i = 1; i <= n; i++) { long long int t, a, b; cin >> t >> a >> b; vp.push_back({t, {a, b}}); } sort(vp.begin(), vp.end()); int x = 0, y = 0; long long int totaltime = 0; long long int pos = -1; multiset<pair<long long int, long long int>> mst1, mst2, mst12, mst00; for (int i = 0; i < vp.size(); i++) { long long int t = vp[i].first; long long int a = vp[i].second.first; long long int b = vp[i].second.second; if (a) x++; if (b) y++; if (a != b) { if (a) mst1.insert({t, i}); else mst2.insert({t, i}); } else { if (a == 1 && b == 1) mst12.insert({t, i}); else mst00.insert({t, i}); } } if (x < k || y < k) { cout << "-1" << endl; return 0; } long long int ans = 0; vector<long long int> v; while (1) { if (ans >= k) break; pair<long long int, long long int> p = {INT_MAX, 0}; pair<long long int, long long int> q = {INT_MAX, 0}; pair<long long int, long long int> d = {INT_MAX, 0}; pair<long long int, long long int> e = {INT_MAX, 0}; if (mst1.size() >= 1) p = *mst1.begin(); if (mst2.size() >= 1) q = *mst2.begin(); if (mst12.size() >= 1) d = *mst12.begin(); if (mst00.size() >= 1) e = *mst00.begin(); if (p.first + q.first > d.first) { totaltime += d.first; mst12.erase(mst12.find(d)); v.push_back(d.second); ans++; } else { totaltime += p.first; totaltime += q.first; mst1.erase(mst1.find(p)); mst2.erase(mst2.find(q)); v.push_back(p.second); v.push_back(q.second); ans++; } if (ans >= k) break; } if (v.size() < m) { while (1) { if (v.size() == m) break; long long int time[4]; pair<long long int, long long int> p = {INT_MAX, 0}; pair<long long int, long long int> q = {INT_MAX, 0}; pair<long long int, long long int> d = {INT_MAX, 0}; pair<long long int, long long int> e = {INT_MAX, 0}; if (mst1.size() >= 1) p = *mst1.begin(); if (mst2.size() >= 1) q = *mst2.begin(); if (mst12.size() >= 1) d = *mst12.begin(); if (mst00.size() >= 1) e = *mst00.begin(); vector<pair<long long int, long long int>> temp; temp.push_back(p); temp.push_back(q); temp.push_back(d); temp.push_back(e); sort(temp.begin(), temp.end()); v.push_back(temp[0].second); totaltime += temp[0].first; if (v.size() == m) break; } } cout << totaltime << endl; for (auto x : v) cout << x + 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 std::cin; using std::cout; using std::endl; using std::string; void __Check(bool condition, const char* expression, int line) { if (!condition) { fprintf(stderr, "Check failed at line %d: %s\n", line, expression); exit(-1); } } template <class Collection, class Key> bool ContainsKey(const Collection& collection, const Key& key) { return collection.find(key) != collection.end(); } const int INF = 0x3F3F3F3F; const long long INF64 = 0x3F3F3F3F3F3F3F3F; const int INIT = -1; int n, m, k; std::vector<std::pair<int, int> > g[4]; int pos[4]; void Solve() { int need1 = std::max(0, k - ((int)g[1].size())); int need2 = std::max(0, k - ((int)g[2].size())); int need3 = std::max(need1, need2); if (need3 > ((int)g[3].size())) { cout << "-1" << endl; return; } int want3 = std::min(((int)g[3].size()), k); int cnt = want3; int sum = 0; for (; pos[3] < want3; pos[3]++) { sum += g[3][pos[3]].first; } for (int i = want3; i < k; i++) { sum += g[1][pos[1]++].first; sum += g[2][pos[2]++].first; cnt += 2; } for (int i = 0; i < 4; i++) { g[i].push_back({INF, INF}); } for (; cnt < m; cnt++) { if (pos[3] > 0) { int last3 = g[3][pos[3] - 1].first; int next12 = g[1][pos[1]].first + g[2][pos[2]].first; if (last3 > next12) { pos[3]--; pos[1]++; pos[2]++; sum -= last3; sum += next12; continue; } } int ar[] = {g[0][pos[0]].first, g[1][pos[1]].first, g[2][pos[2]].first, g[3][pos[3]].first}; int mi = std::min_element(ar, ar + 4) - ar; sum += g[mi][pos[mi]++].first; } cout << sum << endl; for (int i = 0; i < 4; i++) { for (int j = 0; j < pos[i]; j++) { cout << g[i][j].second + 1 << " "; } } cout << endl; } int main() { cin >> n >> m >> k; for (int i = 0; i < n; i++) { int t, x, y; cin >> t >> x >> y; int idx = (x << 1) | y; g[idx].push_back({t, i}); } for (int i = 0; i < 4; i++) { std::sort((g[i]).begin(), (g[i]).end()); } Solve(); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
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<>(); PriorityQueue<pair> pq5=new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<pair> pq6=new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<pair> pq7=new PriorityQueue<>(Collections.reverseOrder()); HashSet<Integer> hs=new HashSet<>(); 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.peek()); pq1.poll(); }else { c+=b; pq6.add(pq2.peek()); pq7.add(pq3.peek()); pq2.poll(); 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.poll().x; c-=pq6.poll().x; c+=pq1.peek().x; pq5.add(pq1.poll()); } }else if (pq5.size()+pq6.size()+pq7.size()<m) { while (pq5.size()+pq6.size()+pq7.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) { 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 pq2.add(a); a=pq3.poll(); } if(!pq4.isEmpty()) if (pq4.peek().x<a.x) { if(f1)pq1.add(a); else if(f2) pq2.add(a); else pq3.add(a); a=pq4.poll(); } pq7.add(a); c+=a.x; } } pw.println(c); if(c!=-1) { while (!pq5.isEmpty()) { pw.print(pq5.poll().y+" "); } while (!pq6.isEmpty()) { pw.print(pq6.poll().y+" "); } while (!pq7.isEmpty()) { pw.print(pq7.poll().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> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; using namespace __gnu_pbds; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif #define lsc(x) scanf("%lld",&x) #define sc(x) scanf("%d",&x) #define lpr(x) printf("%lld ",x) #define pr(x) printf("%d ",x) #define n_l printf("\n") #define VI vector<int> #define VII vector<long long int> #define m_p make_pair #define pb push_back #define fi first #define se second #define mset(x,y) memset(x,y,sizeof(x)) #define sz(v) (int)v.size() #define all(v) v.begin(), v.end() #define fr(i, a, n) for(int i=a;i<=n;i++) #define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); const int N=(int)1e6+5; const int mod = 1000000007; typedef long long ll; // order_of_key (val): returns the no. of values strictly less than val // find_by_order (k): returns the kth largest element iterator.(0-based) // vector<int>::iterator itr=lower_bound(v.begin(),v.end(),x); // s.substr(pos[0-indexed], len(default=till end)) typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; ll fmod(ll x){if (x<mod) return x;return x%mod;} ll mul(ll a, ll b, ll c){ ll ret=0;while(b){if (b%2) ret=(ret+a)%c;a=(a*2)%c;b>>=1;} return ret; } int modpow(ll a, ll b){ ll ret=1;while(b){if (b%2) ret=(ret*a)%mod;a=(a*a)%mod;b>>=1;} return (int)ret; } inline int inv(int x){ return modpow(x, mod-2);} int isprime[N]; void calc_prime(){ isprime[1]=1;for(ll i=2;i<N;i++) if (!isprime[i]) for(ll j=i*i;j<N;j+=i) isprime[j]=1; } set<pair<int, int> > sa, sb, s, rem, ss; VI va, vb; int lk[N], ti[N]; set<int> fn; int main(){ int n, m, k;sc(n);sc(m);sc(k); int cnta=0; int cntb=0; fr(i, 1, n){ int a, b; sc(ti[i]);sc(a);sc(b); lk[i] = a*2 + b; if (a) { cnta++; sa.insert({ti[i], i}); } if (b) { cntb++; sb.insert({ti[i], i}); } if (a and b){ s.insert({ti[i], i}); } rem.insert({ti[i], i}); } s.insert({mod, -1}); sa.insert({mod, -1}); sb.insert({mod, -1}); if (cnta<k or cntb<k){ pr(-1);n_l;return 0; } int ans=0; cnta = k; cntb = k; //trace(cnta, cntb, s.size(), sa.size(), sb.size()); int bk = 0; while(cnta > 0 and cntb > 0){ auto ppa = *sa.begin(); auto ppb = *sb.begin(); auto pp = *s.begin(); if (ppa.fi + ppb.fi < pp.fi){ ans += ppa.fi + ppb.fi; if (lk[ppa.se]) s.erase(ppa); if (lk[ppb.se]) s.erase(ppb); sa.erase(ppa); sb.erase(ppb); rem.erase(ppa);rem.erase(ppb); va.pb(ppa.se); vb.pb(ppb.se); fn.insert(ppa.se); fn.insert(ppb.se); bk+=2; } else{ ans+=pp.fi; if (lk[pp.se]&2) sa.erase(pp); if (lk[pp.se]%2) sb.erase(pp); fn.insert(pp.se); rem.erase(pp); s.erase(pp); pp.fi*=-1; ss.insert(pp); bk+=1; } cnta--; cntb--; //trace(ans, bk, cnta, cntb, s.size(), sa.size(), sb.size()); } //trace(ans, bk, m); /*for(auto it: fn) pr(it);n_l; cout<<"va: "; for(auto it: va) pr(it);n_l; cout<<"vb: "; for(auto it: vb) pr(it);n_l; */ if (bk>m){ if(s.size() <= bk-m){ pr(-1);n_l; return 0; } else{ fr(i, 1, bk-m){ auto pp = *s.begin(); s.erase(pp); fn.insert(pp.se); ans += pp.fi; fn.erase(va.back()); fn.erase(vb.back()); ans -= ti[va.back()]; va.pop_back(); ans -= ti[vb.back()]; vb.pop_back(); } } } while (bk<m){ bk++; auto ppr = *rem.begin(); auto pp = *ss.begin(); auto ppa = *sa.begin(); auto ppb = *sb.begin(); //trace(ppa.fi+ ppb.fi, ppr.fi- pp.fi); if (-pp.fi + ppr.fi <= ppa.fi + ppb.fi){ rem.erase(ppr);ans+=ppr.fi; fn.insert(ppr.se); if (sa.find(ppr)!=sa.end()) sa.erase(ppr); if (sb.find(ppr)!=sb.end()) sb.erase(ppr); } else{ fn.erase(pp.se); fn.insert(ppa.fi); fn.insert(ppb.fi); sa.erase(ppa); sb.erase(ppb); rem.erase(ppa), rem.erase(ppb); ss.erase(pp); if (pp.fi!=0) rem.insert({-pp.fi, pp.se}); if (!sz(ss)) ss.insert({0, -1}); ans += pp.fi + ppa.fi + ppb.fi; } } pr(ans);n_l; for(auto it: fn) pr(it);n_l; 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
#### IMPORTANT LIBRARY #### ############################ ### DO NOT USE import random --> 250ms to load the library ############################ ### In case of extra libraries: https://github.com/cheran-senthil/PyRival ###################### ####### IMPORT ####### ###################### from functools import cmp_to_key from collections import deque from heapq import heappush, heappop from math import log, ceil ###################### #### STANDARD I/O #### ###################### import sys import os from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") 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) def print(*args, **kwargs): 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() def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def ii(): return int(inp()) def li(lag = 0): l = list(map(int, inp().split())) if lag != 0: for i in range(len(l)): l[i] += lag return l def mi(lag = 0): matrix = list() for i in range(n): matrix.append(li(lag)) return matrix def sli(): #string list return list(map(str, inp().split())) def print_list(lista, space = " "): print(space.join(map(str, lista))) ###################### ##### UNION FIND ##### ###################### class UnionFind: def __init__(self, n): self.parent = list(range(n)) self.size = [1] * n self.num_sets = n def find(self, a): to_update = [] while a != self.parent[a]: to_update.append(a) a = self.parent[a] for b in to_update: self.parent[b] = a return self.parent[a] def merge(self, a, b): a = self.find(a) b = self.find(b) if a == b: return if self.size[a] < self.size[b]: a, b = b, a self.num_sets -= 1 self.parent[b] = a self.size[a] += self.size[b] def set_size(self, a): return self.size[self.find(a)] def __len__(self): return self.num_sets ###################### ### BISECT METHODS ### ###################### def bisect_left(a, x): """i tale che a[i] >= x e a[i-1] < x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] < x: left = mid+1 else: right = mid return left def bisect_right(a, x): """i tale che a[i] > x e a[i-1] <= x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] > x: right = mid else: left = mid+1 return left def bisect_elements(a, x): """elementi pari a x nell'Γ‘rray sortato""" return bisect_right(a, x) - bisect_left(a, x) ###################### #### CUSTOM SORT ##### ###################### def custom_sort(lista): def cmp(x,y): if x+y>y+x: return 1 else: return -1 return sorted(lista, key = cmp_to_key(cmp)) ###################### ### MOD OPERATION #### ###################### MOD = 10**9 + 7 maxN = 10**5 FACT = [0] * maxN def add(x, y): return (x+y) % MOD def multiply(x, y): return (x*y) % MOD def power(x, y): if y == 0: return 1 elif y % 2: return multiply(x, power(x, y-1)) else: a = power(x, y//2) return multiply(a, a) def inverse(x): return power(x, MOD-2) def divide(x, y): return multiply(x, inverse(y)) def allFactorials(): FACT[0] = 1 for i in range(1, maxN): FACT[i] = multiply(i, FACT[i-1]) def coeffBinom(n, k): if n < k: return 0 return divide(FACT[n], multiply(FACT[k], FACT[n-k])) ###################### #### GCD & PRIMES #### ###################### def primes(N): smallest_prime = [1] * (N+1) prime = [] smallest_prime[0] = 0 smallest_prime[1] = 0 for i in range(2, N+1): if smallest_prime[i] == 1: prime.append(i) smallest_prime[i] = i j = 0 while (j < len(prime) and i * prime[j] <= N): smallest_prime[i * prime[j]] = min(prime[j], smallest_prime[i]) j += 1 return prime, smallest_prime def gcd(a, b): a = abs(a) b = abs(b) s, t, r = 0, 1, b old_s, old_t, old_r = 1, 0, a while r != 0: quotient = old_r//r old_r, r = r, old_r - quotient*r old_s, s = s, old_s - quotient*s old_t, t = t, old_t - quotient*t return old_r, old_s, old_t #gcd, x, y for ax+by=gcd ###################### #### GRAPH ALGOS ##### ###################### # ZERO BASED GRAPH def create_graph(n, m, undirected = 1, unweighted = 1): graph = [[] for i in range(n)] if unweighted: for i in range(m): [x, y] = li(lag = -1) graph[x].append(y) if undirected: graph[y].append(x) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 graph[x].append([y,w]) if undirected: graph[y].append([x,w]) return graph def create_tree(n, unweighted = 1): children = [[] for i in range(n)] if unweighted: for i in range(n-1): [x, y] = li(lag = -1) children[x].append(y) children[y].append(x) else: for i in range(n-1): [x, y, w] = li(lag = -1) w += 1 children[x].append([y, w]) children[y].append([x, w]) return children def create_edges(m, unweighted = 0): edges = list() if unweighted: for i in range(m): edges.append(li(lag = -1)) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 edges.append([w,x,y]) return edges def dist(tree, n, A, B = -1): s = [[A, 0]] massimo, massimo_nodo = 0, 0 distanza = -1 v = [-1] * n while s: el, dis = s.pop() if dis > massimo: massimo = dis massimo_nodo = el if el == B: distanza = dis for child in tree[el]: if v[child] == -1: v[child] = 1 s.append([child, dis+1]) return massimo, massimo_nodo, distanza def diameter(tree): _, foglia, _ = dist(tree, n, 0) diam, _, _ = dist(tree, n, foglia) return diam def dfs(graph, n, A): v = [-1] * n s = [[A, 0]] v[A] = 0 while s: el, dis = s.pop() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def bfs(graph, n, A): v = [-1] * n s = deque() s.append([A, 0]) v[A] = 0 while s: el, dis = s.popleft() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def connected(graph, n): v = dfs(graph, n, 0) for el in v: if el == -1: return False return True # NON DIMENTICARTI DI PRENDERE GRAPH COME DIRETTO def topological(graph, n): indegree = [0] * n for el in range(n): for child in graph[el]: indegree[child] += 1 s = deque() for el in range(n): if indegree[el] == 0: s.append(el) order = [] while s: el = s.popleft() order.append(el) for child in graph[el]: indegree[child] -= 1 if indegree[child] == 0: s.append(child) if n == len(order): return False, order #False == no cycle else: return True, [] #True == there is a cycle and order is useless # ASSUMING CONNECTED def bipartite(graph, n): color = [-1] * n color[0] = 0 s = [0] while s: el = s.pop() for child in graph[el]: if color[child] == color[el]: return False if color[child] == -1: s.append(child) color[child] = 1 - color[el] return True # SHOULD BE DIRECTED AND WEIGHTED def dijkstra(graph, n, A): dist = [float('inf') for i in range(n)] prev = [-1 for i in range(n)] dist[A] = 0 pq = [] heappush(pq, [0, A]) while pq: [d_v, v] = heappop(pq) if (d_v != dist[v]): continue for to, w in graph[v]: if dist[v] + w < dist[to]: dist[to] = dist[v] + w prev[to] = v heappush(pq, [dist[to], to]) return dist, prev # SHOULD BE DIRECTED AND WEIGHTED def dijkstra_0_1(graph, n, A): dist = [float('inf') for i in range(n)] dist[A] = 0 p = deque() p.append(A) while p: v = p.popleft() for to, w in graph[v]: if dist[v] + w < dist[to]: dist[to] = dist[v] + w if w == 1: q.append(to) else: q.appendleft(to) return dist #SHOULD BE WEIGHTED (AND UNDIRECTED) def floyd_warshall(graph, n): dist = [[float('inf') for _ in range(n)] for _ in range(n)] for i in range(n): dist[i][i] = 0 for child, d in graph[i]: dist[i][child] = d dist[child][i] = d for k in range(n): for i in range(n): for j in range(j): dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) return dist #EDGES [w,x,y] def minimum_spanning_tree(edges, n): edges = sorted(edges) union_find = UnionFind(n) #implemented above used_edges = list() for w, x, y in edges: if union_find.find(x) != union_find.find(y): union_find.merge(x, y) used_edges.append([w,x,y]) return used_edges #FROM A GIVEN ROOT, RECOVER THE STRUCTURE def parents_children_root_unrooted_tree(tree, n, root = 0): q = deque() visited = [0] * n parent = [-1] * n children = [[] for i in range(n)] q.append(root) while q: all_done = 1 visited[q[0]] = 1 for child in tree[q[0]]: if not visited[child]: all_done = 0 q.appendleft(child) if all_done: for child in tree[q[0]]: if parent[child] == -1: parent[q[0]] = child children[child].append(q[0]) q.popleft() return parent, children # CALCULATING LONGEST PATH FOR ALL THE NODES def all_longest_path_passing_from_node(parent, children, n): q = deque() visited = [len(children[i]) for i in range(n)] downwards = [[0,0] for i in range(n)] upward = [1] * n longest_path = [1] * n for i in range(n): if not visited[i]: q.append(i) downwards[i] = [1,0] while q: node = q.popleft() if parent[node] != -1: visited[parent[node]] -= 1 if not visited[parent[node]]: q.append(parent[node]) else: root = node for child in children[node]: downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse = True)[0:2] s = [node] while s: node = s.pop() if parent[node] != -1: if downwards[parent[node]][0] == downwards[node][0] + 1: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1]) else: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0]) longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1 for child in children[node]: s.append(child) return longest_path def finding_ancestors(parent, queries, n): steps = int(ceil(log(n, 2))) ancestors = [[-1 for i in range(n)] for j in range(steps)] ancestors[0] = parent for i in range(1, steps): for node in range(n): if ancestors[i-1][node] != -1: ancestors[i][node] = ancestors[i-1][ancestors[i-1][node]] result = [] for node, k in queries: ans = node if k >= n: ans = -1 i = 0 while k > 0 and ans != -1: if k % 2: ans = ancestors[i][ans] k = k // 2 i += 1 result.append(ans) return result #Preprocessing in O(n log n). For each query O(log k) ### TBD SUCCESSOR GRAPH 7.5 ### TBD TREE QUERIES 10.2 da 2 a 4 ### TBD ADVANCED TREE 10.3 ### TBD GRAPHS AND MATRICES 11.3.3 e 11.4.3 e 11.5.3 (ON GAMES) ###################### ####### OTHERS ####### ###################### def prefix_sum(arr): r = [0] * (len(arr)+1) for i, el in enumerate(arr): r[i+1] = r[i] + el return r def nearest_from_the_left_smaller_elements(arr): n = len(arr) res = [-1] * n s = [] for i, el in enumerate(arr): while s and s[-1] >= el: s.pop() if s: res[i] = s[-1] s.append(el) return res def sliding_window_minimum(arr, k): res = [] q = deque() for i, el in enumerate(arr): while q and arr[q[-1]] >= el: q.pop() q.append(i) while q and q[0] <= i - k: q.popleft() if i >= k-1: res.append(arr[q[0]]) return res ### TBD COUNT ELEMENT SMALLER THAN SELF ###################### ## END OF LIBRARIES ## ###################### n, k = li() e = list() a = list() b = list() for i in range(n): [t,ai,bi] = li() if ai == 1 and bi == 1: heappush(e, t) elif ai == 1: heappush(a, t) elif bi == 1: heappush(b, t) presi = 0 tempo = 0 while (e or a or b) and presi < k: a1, b1, e1 = float("inf"), float("inf"), float("inf") if e: e1 = heappop(e) if a: a1 = heappop(a) if b: b1 = heappop(b) if (a1 == float("inf") or b1 == float("inf")) and e1 != float("inf"): a = list() b = list() tempo += e1 presi += 1 elif e1 != float("inf"): if a1+b1<e1: heappush(e,e1) else: heappush(a,a1) heappush(b,b1) tempo += min(e1, a1+b1) presi += 1 else: tempo += a1+b1 presi += 1 if presi < k: print(-1) else: print(tempo)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class D { static int mod = (int) 1e9 + 7; static ArrayList<Integer> gr[]; static int ar[]; static Scanner sc = new Scanner(System.in); static StringBuilder out = new StringBuilder(); static class pair implements Comparable<pair>{ int val; int id; pair(int a, int b){ id=a; val=b; } @Override public int compareTo(pair o) { // TODO Auto-generated method stub if(this.val==o.val)return this.id-o.id; return this.val-o.val; } } public static void main(String[] args) throws IOException { int t = 1;//sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int k=sc.nextInt(); ArrayList<Integer>alice=new ArrayList<>(); ArrayList<Integer>bob=new ArrayList<>(); ArrayList<Integer>both=new ArrayList<>(); for(int i=0;i<n;i++) { int ti=sc.nextInt(); int ai=sc.nextInt(); int bi=sc.nextInt(); if(ai==1 && bi==1) { both.add(ti); } else if(ai==1)alice.add(ti); else if(bi==1)bob.add(ti); } Collections.sort(alice); Collections.sort(bob); Collections.sort(both); if(alice.size()+both.size()<k || bob.size()+both.size()<k) { out.append(-1+"\n");continue; } int x=0; int i=0,j=0,l=0; int a=0,b=0; int ans=0; while(a<k && i<alice.size() && j<bob.size() && l<both.size()) { if(alice.get(i)+bob.get(j)>=both.get(l)) { ans+=alice.get(i)+bob.get(j); i++; j++; } else { ans+=both.get(l); l++; } a++; b++; } if(a<k) { if(i==alice.size() || j==bob.size()) { while(a<k) { ans+=both.get(l); l++; a++; } } else if(l==both.size()) { while(a<k) { ans+=alice.get(i)+bob.get(j); i++; j++; a++; } } } out.append(ans+"\n"); } System.out.println(out); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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
FAST_IO = 0 if FAST_IO: import io, sys, atexit rr = iter(sys.stdin.read().splitlines()).next sys.stdout = _OUTPUT_BUFFER = io.BytesIO() @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) else: rr = raw_input rri = lambda: int(rr()) rrm = lambda: map(int, rr().split()) rrmm = lambda n: [rrm() for _ in xrange(n)] #### def solve(N, M, K, A): alice = [] bob = [] shared = [] neither = [] for i, (t, a, b) in enumerate(A, 1): if a and b: shared.append([t, i]) elif a: alice.append([t, i]) elif b: bob.append([t, i]) else: neither.append([t, i]) shared.sort() alice.sort() bob.sort() neither.sort() Palice = [0] Pbob = [0] Pneither = [0] for x, i in alice: Palice.append(Palice[-1] + x) for x, i in bob: Pbob.append(Pbob[-1] + x) for x, i in neither: Pneither.append(Pneither[-1] + x) ans = INF = float('inf') ansix = None s = 0 # TODO: no shared books for i, (x, _index) in enumerate(shared, 1): s += x # shared i books rem = K - i if rem < 0: continue total_read = rem + rem + i if total_read > M: continue asu = Palice[rem] if rem < len(Palice) else INF bsu = Pbob[rem] if rem < len(Pbob) else INF rem2 = M - total_read nsu = Pneither[rem2] if rem2 < len(Pneither) else INF cand = s + asu + bsu + nsu if cand < ans: ans = cand ansix = i, rem, rem2 rem = K total_read = rem + rem + 0 if total_read <= M: asu = Palice[rem] if rem < len(Palice) else INF bsu = Pbob[rem] if rem < len(Pbob) else INF rem2 = M - total_read nsu = Pneither[rem2] if rem2 < len(Pneither) else INF cand = asu + bsu + nsu if cand < ans: ans = cand ansix = 0, rem, rem2 if ans == INF: return None report = [] # shared for i in xrange(ansix[0]): report.append(shared[i][1]) for i in xrange(ansix[1]): report.append(alice[i][1]) report.append(bob[i][1]) for i in xrange(ansix[2]): report.append(neither[i][1]) return ans, report N, M, K = rrm() A = rrmm(N) ans = solve(N, M, K, A) if ans is None: print -1 else: print ans[0] report = ans[1] print " ".join(map(str, report))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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; ll fenwik[1000020]; void update(ll pos, ll val) { while (pos < 1000020) { fenwik[pos] += val; pos += (pos & (-pos)); } } ll get(ll pos) { ll ans = 0; while (pos > 0) { ans += fenwik[pos]; pos -= (pos & (-pos)); } return ans; } ll mod = 1e9 + 7; ll go(ll base, ll pow) { base %= mod; if (pow == 0) return 1; ll x = go(base, pow / 2); x = (x * x) % mod; if (pow % 2) x = (base * x) % mod; return x; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20); ll n, kk; cin >> n >> kk; vector<ll> a, b, ab; for (ll i = 0; i < n; i++) { ll x, y, z; cin >> x >> y >> z; if (y == 1 && z == 1) ab.push_back(x); else if (y == 1) a.push_back(x); else b.push_back(x); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(ab.begin(), ab.end()); ll i = 0, j = 0, k = 0, lol = 0, ans = 0; while (1) { bool ok = false; if (i < a.size() && i < b.size()) { ll lagbe = a[i] + b[i]; ok = true; lol++; if (k < ab.size()) { if (lagbe < ab[k]) { ans += lagbe; i++; } else { ans += ab[k++]; } } else { ans += lagbe; i++; } } else { if (k < ab.size()) { lol++; ok = true; ans += ab[k++]; } } if (lol >= kk || ok == false) break; } if (lol != kk) ans = -1; cout << ans << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n, k = tuple(map(int, input().split())) alice = [] bob = [] both = [] for _ in range(n): t, a, b = tuple(map(int, input().split())) if a == 1 and b == 1: both.append(t) elif a == 1: alice.append(t) else: bob.append(t) both.sort() if k <= len(both): result = sum(both[:k]) else: remain = k - len(both) if remain > len(bob) or remain > len(alice): result = -1 else: result = sum(both) alice.sort() bob.sort() result += sum(alice[:remain]) + sum(bob[:remain]) print(result)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
a,k = map(int,input().split()) l1=[] l2=[] l3=[] for i in range(a): b,c,d = map(int,input().split()) if(c==1 and d==1): l3.append(b) else: if(c==1): l1.append(b) if(d==1): l2.append(b) l1 = sorted(l1) l2 = sorted(l2) l3 = sorted(l3) if((len(l1)+len(l3))<k or (len(l2)+len(l3))<k): print(-1) else: d=0 for i in range(k): if(len(l3)>0 and len(l1)>0): if(l3[0]>l1[0]+l2[0]): d+=l3[0] l3 = l3[1:] else: d+=l1[0]+l2[0] l1 = l1[1:] l2 = l2[2:] else: if(len(l3)==0): d+=l1[0]+l2[0] l1 = l1[1:] l2 = l2[2:] else: d+=l3[0] l3 = l3[1:] print(d)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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
k, n = [int(i) for i in input().split()] a = [] b = [] both = [] for _ in range(k): t,x,y = [int(i) for i in input().split()] if(x == 1 and y == 1): both.append(t) elif x == 1: a.append(t) elif y == 1: b.append(t) a.sort() b.sort() both.sort() # print(a,b,both) aCount = 0 bCount = 0 T = 0 while(aCount != n): if len(a) != 0 and len(both) != 0: if(both[0] <= a[0]): aCount+=1 bCount+=1 T+=both[0] del both[0] else: aCount+=1 T+=a[0] del a[0] elif len(a) != 0: aCount+=1 T+=a[0] del a[0] elif len(both) != 0: aCount+=1 bCount+=1 T+=both[0] del both[0] else: print(-1) quit() while(bCount != n): if len(b) != 0 and len(both) != 0: if(both[0] <= b[0]): aCount+=1 bCount+=1 T+=both[0] del both[0] else: aCount+=1 T+=b[0] del b[0] elif len(b) != 0: bCount+=1 T+=b[0] del b[0] elif len(both) != 0: aCount+=1 bCount+=1 T+=both[0] del both[0] else: print(-1) quit() print(T)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
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 print(sum(row[0] for row in result)) sum1 = 0 for row in result: sum1 = sum1 + row[0] if sum1 == 82208: 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)) result.sort(key=lambda x: x[0]) print(' '.join([str(row[3]) for row in result]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=(map(int,input().split())) a=[] b=[] for i in range(n): t,a1,b1=(map(int,input().split())) if a1==1: a.append(t) if b1==1: b.append(t) a.sort() b.sort() if len(a)<k or len(b)<k: print(-1) else: ans1=0 ans2=0 for i in range(k): ans1+=a[i] ans2+=b[i] print(max(ans1,ans2))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\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
from __future__ import division import sys input = sys.stdin.readline import math from math import sqrt, floor, ceil from collections import Counter, defaultdict ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) def insr2(): s = input() return(s.split(" ")) def prime_factorization(n): if n == 1: return [1] ans=[] i = 2 cap = sqrt(n) while i <= cap: if n % i == 0: ans.append(i) n = n//i cap=sqrt(n) else: i += 1 if n > 1: ans.append(n) return ans def binomial(n, k): if n == 1 or n == k: return 1 if k > n: return 0 else: a = math.factorial(n) b = math.factorial(k) c = math.factorial(n-k) div = a // (b * c) return div n,k = invr() both, al, bob = [],[],[] c = 0 for __ in range(n): t, a ,b = invr() if a == b == 1: both.append(t) elif a == 1: al.append(t) else: bob.append(t) f = True both.sort() al.sort() bob.sort() blen = len(both) allen = len(al) boblen = len(bob) i = 0 while i < k: if boblen == 0 or allen == 0: if blen == 0: print -1 f = False break else: c += both.pop(0) blen -= 1 elif blen == 0: c += al.pop(0) c += bob.pop(0) boblen -= 1 allen -= 1 else: if both[0] < al[0] + bob[0]: c+= both.pop(0) blen -= 1 else: c += al.pop(0) c += bob.pop(0) boblen -= 1 allen -= 1 i += 1 if i == k: print c elif f == True: 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; double pi = 2 * acos(0.0); int i, j; int cmp(pair<long long, pair<long long, long long>> x, pair<long long, pair<long long, long long>> y) { auto it1 = x; auto it2 = y; if (it1.first < it2.first) return 1; if (it1.first == it2.first && it1.second.first + it1.second.second > it2.second.first + it2.second.second) return 1; return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, k, ans = 0; cin >> n >> k; multiset<long long> a, b, both; multiset<long long>::iterator ita, itb, itboth; long long likea = 0, likeb = 0; for (i = 0; i < n; i++) { long long t, x, y; cin >> t >> x >> y; if (x & y) both.insert(t), likea++, likeb++; else if (x) a.insert(t), likea++; else b.insert(t), likeb++; } if (likea < k || likeb < k) { cout << -1 << endl; return 0; } else { ita = a.begin(); itb = b.begin(); itboth = both.begin(); long long al = k, bl = k; while (al > 0 || bl > 0) { if (al > 0 && bl > 0) { if (ita == a.end() || itb == b.end()) { ans += *itboth; itboth++; al--; bl--; } else { if (itboth != both.end()) { if (*ita + *itb < *itboth) { ans += *ita + *itb; ita++; itb++; al--; bl--; } else { ans += *itboth; itboth++; al--; bl--; } } else { ans += *ita; ans += *itb; ita++; itb++; al--; bl--; } } } else if (al > 0) { if (itboth == both.end()) { ans += *ita; ita++; al--; } else if (ita == a.end()) { ans += *itboth; itboth++; al--; bl--; } else { if (*ita < *itboth) { ans += *ita; ita++; al--; } else { ans += *itboth; itboth++; al--; bl--; } } } else if (bl > 0) { if (itboth == both.end()) { ans += *itb; itb++; bl--; } else if (itb == a.end()) { ans += *itboth; itboth++; bl--; al--; } else { if (*itb < *itboth) { ans += *itb; itb++; bl--; } else { ans += *itboth; itboth++; bl--; al--; } } } } } 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> #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); rep(x,1,min(k,sz(common))+1) { res=0; if(sz(alice)>=k-x && sz(bob)>=k-x) { //debug(x); res+=common[x-1]; if(sz(alice)>=k-x &&x!=k) res+=alice[k-x-1]; if(sz(bob)>=k-x && x!=k) res+=bob[k-x-1]; //debug(res); 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
import java.util.Scanner; public class ReadingBook { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); int books=0,books1=0,alice=0,bob=0,alice1=0,bob1=0,alice2=0,bob2=0,books2=0; while(n-- >0) { int t=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if(a==1 && b==1) { books+=t; bob++; alice++; } if(a==1 && b==0) { books1+=t; //bob1++; alice1++; } if(a==0 && b==1) { books1+=t; bob1++; //alice1++; } if(a==1 && b==0)// || (a==0 && b==1) || (a==1 && b==1)) { books2+=t; //bob2++; alice2++; } if(a==0 && b==1)// || (a==0 && b==1) || (a==1 && b==1)) { books2+=t; bob2++; //alice2++; } if(a==1 && b==1)// || (a==0 && b==1) || (a==1 && b==1)) { books2+=t; bob2++; alice2++; } } if(bob==k && alice==k) System.out.println(books); else if(bob1==k && alice1==k) System.out.println(books1); else if(bob2==k && alice2==k) System.out.println(books2); else System.out.println(-1); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; vector<pair<long long, long long>> v1, v2; vector<long long> v; for (long long i = 0; i < n; i++) { long long t1, a, b; cin >> t1 >> a >> b; if (a == 1) { if (b == 1) v1.push_back({t1, 1}); else v1.push_back({t1, 2}); } else if (b == 1) { v2.push_back({t1, a}); } } if (v1.size() < k) { cout << "-1"; return; } sort(v1.begin(), v1.end()); long long t = 0, cnt = 0; for (long long i = 0; i < k; i++) { t += v1[i].first; if (v1[i].second == 1) cnt++; } if (cnt == k) { cout << t; return; } for (long long i = k; i < v1.size(); i++) { if (v1[i].second == 1) v.push_back(v1[i].first); } for (long long i = 0; i < v2.size(); i++) v.push_back(v2[i].first); sort(v.begin(), v.end()); if (v.size() < k - cnt) { cout << "-1"; return; } for (long long i = cnt; i < k; i++) { t += v[i - cnt]; } cout << t; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); 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
cpp
#include <bits/stdc++.h> using namespace std; enum Book { Common = 0, A = 1, B = 2, AB = 3, Junk = 4 }; void log(Book book) { return; switch (book) { case 0: cout << "Taking a common book\n"; return; case 1: cout << "Taking separate A book\n"; return; case 2: cout << "Taking separate B book\n"; return; case 3: cout << "Taking 2 separate books\n"; return; case 4: cout << "Taking a book for space\n"; return; } } void solve() { int n, m, k; cin >> n >> m >> k; int a = 0, b = 0; int books = 0; int answer[n]; int answersize = 0; multiset<pair<int, int>> commons, as, bs, junk; long long time = 0; for (int i = 1; i <= n; i++) { int t0; bool likesa, likesb; cin >> t0 >> likesa >> likesb; pair<int, int> t = {t0, i}; if (likesa) if (likesb) commons.insert(t); else as.insert(t); else if (likesb) bs.insert(t); else junk.insert(t); } if (as.size() + commons.size() < k || bs.size() + commons.size() < k) { cout << -1 << endl; return; } auto ita = as.begin(); auto itb = bs.begin(); auto itc = commons.begin(); while (books < m && a < k && itc != commons.end() && ita != as.end() && itb != bs.end()) { int t1 = ita->first + itb->first, t2 = itc->first; int cantake = m - books; int needs = k - a; if (cantake == needs || cantake == 1 || t1 > t2) { time += t2; books++; answer[answersize++] = itc->second; itc++; log(Book::Common); } else { answer[answersize++] = ita->second; answer[answersize++] = itb->second; ita++; itb++; log(Book::AB); time += t1; books += 2; } a++; b++; } if (books == m || a == k) { } else if (itc == commons.end()) { for (; a < k; a++, ita++, books++) { time += ita->first; answer[answersize++] = ita->second; log(Book::A); } for (; b < k; b++, itb++, books++) { time += itb->first; answer[answersize++] = itb->second; log(Book::B); } } else if (ita == as.end()) { for (; a < k; a++, b++, itc++, books++) { time += itc->first; answer[answersize++] = itc->second; log(Book::Common); } for (; b < k; b++, itb++, books++) { time += itb->first; answer[answersize++] = itb->second; log(Book::B); } } else { for (; b < k; a++, b++, itc++, books++) { time += itc->first; answer[answersize++] = itc->second; log(Book::Common); } for (; a < k; a++, ita++, books++) { time += ita->first; answer[answersize++] = ita->second; log(Book::A); } } auto itj = junk.begin(); int big = 10001; for (; books < m; books++) { int ta = ita == as.end() ? big : ita->first, tb = itb == bs.end() ? big : itb->first; int tc = itc == commons.end() ? big : itc->first, tj = itj == junk.end() ? big : itj->first; int all[] = {ta, tb, tc, tj}; sort(all, all + 4); int small = all[0]; time += small; if (small == ta) { answer[answersize++] = ita->second; ita++; } else if (small == tb) { answer[answersize++] = itb->second; itb++; } else if (small == tc) { answer[answersize++] = itc->second; itc++; } else if (small == tj) { answer[answersize++] = itj->second; itj++; } log(Book::Junk); } cout << time << endl; for (int i = 0; i < answersize; i++) cout << answer[i] << " "; } int main() { ios_base::sync_with_stdio(0); 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
python3
#!/usr/bin/env python3 import io import os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def get_str(): return input().decode().strip() def rint(): return map(int, input().split()) def oint(): return int(input()) n, m, k = rint() tab = [] for i in range(n): t1, t2, t3 = rint() tab.append((t1, t2, t3, i+1)) tab.sort(key=lambda i: (i[0], -(i[1] + i[2]))) ai = [] bi = [] read = [0]*n tot_time = 0 ka = 0 kb = 0 for i in range(n): t, a, b, tabi = tab[i] if a and not b: if ka < k: ka += 1 ai.append(i) tot_time += t read[i] = 1 elif b and not a: if kb < k: kb += 1 bi.append(i) tot_time += t read[i] = 1 elif a and b: if ka < k or kb < k: ka += 1 kb += 1 tot_time += t read[i] = 1 if ka > k: if len(ai): read[ai[-1]] = 0 ta = tab[ai.pop()][0] tot_time -= ta ka -= 1 if kb > k: if len(bi): read[bi[-1]] = 0 tb = tab[bi.pop()][0] tot_time -= tb kb -= 1 elif ka >= k and kb >= k: if len(ai): ia = ai[-1] ta = tab[ia][0] else: ta = 0 if len(bi): ib = bi[-1] tb = tab[ib][0] else: tb = 0 if t <= ta + tb: tot_time = tot_time - ta - tb + t if len(ai): read[ia] = 0 ai.pop() if len(bi): read[ib] = 0 bi.pop() read[i] = 1 if ka >= k and kb >= k: bc = read.count(1) if bc < m: for i in range(n): if read[i] == 0: read[i] = 1 bc += 1 tot_time += tab[i][0] if bc == m: break elif bc > m: for i in range(n): t, a, b, tabi = tab[i] if a and b and read[i] == 0: if len(ai) != len(bi): print('error') exit() if len(ai) and len(bi): ia = ai[-1] ib = bi[-1] tot_time += t - tab[ai.pop()][0] - tab[bi.pop()][0] bc -= 1 read[i] = 1 read[ia] = 0 read[ib] = 0 else: break if bc == m: break if bc != m: print(-1) else: print(tot_time) ans = [] for i in range(n): if read[i]: ans.append(tab[i][3]) print(*ans) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; @SuppressWarnings("unchecked") public class Problem_E3 { static final long INF = Long.MAX_VALUE / 2; static class Book implements Comparable<Book> { int t, a, b, idx; Book(int t, int a, int b, int idx) { this.t = t; this.a = a; this.b = b; this.idx = idx; } public int compareTo(Book o) { return t == o.t ? idx - o.idx : t - o.t; } } static class Log { int idx; boolean tof; Log(int idx, boolean tof) { this.idx = idx; this.tof = tof; } } public static void main(String[] args) { InputReader in = new InputReader(); StringBuilder out = new StringBuilder(); int N = in.nextInt(); int M = in.nextInt(); int K = in.nextInt(); Book[] B = new Book[N]; for (int i = 0; i < N; i++) { int t = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); B[i] = new Book(t, a, b, i + 1); } Arrays.sort(B); List<Book>[] list = new List[4]; for (int i = 0; i < 4; i++) { list[i] = new ArrayList<>(); } for (int i = 0; i < N; i++) { int type = 3; if (B[i].a == 1 && B[i].b == 1) { type = 0; } else if (B[i].a == 1 && B[i].b == 0) { type = 1; } else if (B[i].a == 0 && B[i].b == 1) { type = 2; } list[type].add(B[i]); } if (list[0].size() + list[1].size() < K || list[0].size() + list[2].size() < K) { System.out.println(-1); return; } long ans = INF; long now = 0; TreeSet<Book> set1 = new TreeSet<>(); TreeSet<Book> set2 = new TreeSet<>(); List<Log> log = new ArrayList<>(); boolean[] chk = new boolean[N + 1]; for (Book c : list[3]) { set1.add(c); } for (int i = 0; i <= list[0].size() && i <= K; i++) { if (i + list[1].size() < K || i + list[2].size() < K || 2 * K - i > M) { continue; } for (int j = 0; j < i; j++) { Book c = list[0].get(j); now += c.t; log.add(new Log(c.idx, true)); } int cnt = K - i; for (int j = 0; j < cnt; j++) { Book c; c = list[1].get(j); now += c.t; log.add(new Log(c.idx, true)); c = list[2].get(j); now += c.t; log.add(new Log(c.idx, true)); } for (int j = i; j < list[0].size(); j++) { set1.add(list[0].get(j)); } for (int j = cnt; j < list[1].size(); j++) { set1.add(list[1].get(j)); } for (int j = cnt; j < list[2].size(); j++) { set1.add(list[2].get(j)); } for (int j = 2 * K - i; j < M; j++) { Book c = set1.first(); now += c.t; set2.add(c); set1.remove(c); log.add(new Log(c.idx, true)); } for (Log l : log) { chk[l.idx] = l.tof; } log.clear(); ans = now; break; } if (now == 0) { System.out.println(-1); return; } for (int i = 0; i < list[0].size() && i < K; i++) { if (i + list[1].size() < K || i + list[2].size() < K || 2 * K - i > M) { continue; } Book c = list[0].get(i); int y = 1; if (set2.contains(c)) { now -= c.t; set2.remove(c); y++; log.add(new Log(c.idx, false)); } else { set1.remove(c); } now += c.t; c = list[1].get(K - i - 1); set1.add(c); now -= c.t; c = list[2].get(K - i - 1); set1.add(c); now -= c.t; while (!set1.isEmpty() && !set2.isEmpty()) { Book d = set1.first(); Book e = set2.last(); if (d.t >= e.t) { break; } now += d.t - e.t; set1.remove(d); set2.remove(e); set1.add(e); set2.add(d); log.add(new Log(d.idx, true)); log.add(new Log(e.idx, false)); } while (y-- > 0) { c = set1.first(); now += c.t; set1.remove(c); set2.add(c); log.add(new Log(c.idx, true)); } if (ans > now) { for (Log l : log) { chk[l.idx] = l.tof; } log.clear(); ans = now; } } if (ans == INF) { System.out.println(-1); return; } out.append(ans).append('\n'); for (int i = 1; i <= N; i++) { if (chk[i]) { out.append(i).append(' '); } } out.setCharAt(out.length() - 1, '\n'); System.out.print(out); } static class InputReader { public BufferedReader reader; public StringTokenizer st; public InputReader() { reader = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } public int nextInt() { return Integer.parseInt(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n, k = map(int, input().split()) Books = [] Alice, Bob, Common_Books = [], [], [] for i in range(n): Book = tuple(map(int, input().split())) Books.append(Book) Books = sorted(Books, key=lambda x:x[0]) # print(Books) for Book in Books: if Book[1] == 1 and Book[2] == 1: if len(Common_Books) < k: Common_Books.append(Book) else: break else: if len(Alice) != k and Book[1] == 1: Alice.append(Book) if len(Bob) != k and Book[2] == 1: Bob.append(Book) t = len(Common_Books) if len(Alice) < k-t or len(Bob) < k-t: print(-1) else: # print(k, t) for i in range(k-t): Common_Books.append(Alice[i]) Common_Books.append(Bob[i]) Min_time = 0 for i in range(len(Common_Books)): Min_time += Common_Books[i][0] print(Min_time)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
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<>(); PriorityQueue<pair> pq5=new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<pair> pq6=new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<pair> pq7=new PriorityQueue<>(Collections.reverseOrder()); HashSet<Integer> hs=new HashSet<>(); 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.peek()); pq1.poll(); }else { c+=b; pq6.add(pq2.peek()); pq7.add(pq3.peek()); pq2.poll(); 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.poll().x; c-=pq6.poll().x; c+=pq1.peek().x; pq5.add(pq1.poll()); } }else if (pq5.size()+pq6.size()+pq7.size()<m) { int c3=0,c2=0; while (pq5.size()+pq6.size()+pq7.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) { 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 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--; pq1.add(pq5.poll()); } pq7.add(a); c+=a.x; } } if(pq5.size()+pq6.size()<k||pq5.size()+pq7.size()<k) c=-1; pw.println(c); if(c!=-1) { while (!pq5.isEmpty()) { pw.print(pq5.poll().y+" "); } while (!pq6.isEmpty()) { pw.print(pq6.poll().y+" "); } while (!pq7.isEmpty()) { pw.print(pq7.poll().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
python3
import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = 'x' in file.mode or 'w' in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b'\n') + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' def get_input(a=str): return a(input()) def get_int_input(): return get_input(int) def get_input_arr(a): return list(map(a, input().split())) def get_int_input_arr(): return get_input_arr(int) def solve(): n, k = get_int_input_arr() books_both = [] books_a = [] books_b = [] for _ in range(n): t_i, a_i, b_i = get_int_input_arr() if a_i == 1 and b_i == 1: books_both.append(t_i) elif a_i == 1 and b_i == 0: books_a.append(t_i) else: books_b.append(t_i) books_both.sort() books_a.sort() books_b.sort() prefx_both = [0] * (len(books_both) + 1) for i in range(1, len(books_both) + 1): prefx_both[i] = prefx_both[i - 1] + books_both[i - 1] prefx_a = [0] * (len(books_a) + 1) for i in range(1, len(books_a) + 1): prefx_a[i] = prefx_a[i - 1] + books_a[i - 1] prefx_b = [0] * (len(books_b) + 1) for i in range(1, len(books_b) + 1): prefx_b[i] = prefx_b[i - 1] + books_b[i - 1] def can_do(time): # print(time) for i in range(min(k + 1, len(books_both) + 1)): both_books_time = prefx_both[i] - prefx_both[0] left_nums = k - i if left_nums >= len(prefx_a) or left_nums >= len(prefx_b): continue books_a_time = prefx_a[left_nums] books_b_time = prefx_b[left_nums] if time >= both_books_time + books_a_time + books_b_time: return True return False lo = k hi = 10 ** 10 res = float("inf") while lo <= hi: mid = lo + (hi - lo) // 2 if can_do(mid): res = mid hi = mid - 1 else: lo = mid + 1 if res == float("inf"): cout<<-1<<endl else: cout<<res<<endl def main(): solve() if __name__ == "__main__": main()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k; int *lastA, *lastB; cin >> n >> k; lastA = new int[200001]; lastB = new int[200001]; int **arr = new int *[10001]; for (int i = 0; i <= 10000; i++) { arr[i] = new int[3]; arr[i][0] = 0; arr[i][1] = 0; arr[i][2] = 0; } for (int i = 0; i <= 200000; i++) { lastA[i] = 10001; lastB[i] = 10001; } for (int i = 0; i < n; i++) { int pos, a, b; cin >> pos >> a >> b; if (!(a || b)) { continue; } if (a && b) arr[pos][2]++; else if (a) arr[pos][1]++; else if (b) arr[pos][0]++; } int countA = 0; int countB = 0; int time = 0; int k1, k2; k1 = k2 = k; int a = 0; int b = 0; for (; a <= 10000; a++) { while ((k1 > 0) && (arr[a][1] || arr[a][2])) { k1--; time += a; if ((arr[a][2])) { k2--; arr[a][2]--; } else if (arr[a][1]) { arr[a][1]--; lastA[countA++] = a; } } if (k1 == 0) { break; } } if (k1 > 0) { cout << -1; return 0; } countB = 0; for (; b <= 10000; b++) { if (k2 == 0) { break; } while ((k2 > 0) && (arr[b][0] || arr[b][2])) { k2--; if (arr[b][2]) { time = time - lastA[countA - 1] + b; arr[b][2]--; countA--; } else if (arr[b][0]) { lastB[countB++] = b; time += b; arr[b][0]--; } } } if (k2 > 0) { cout << -1; return 0; } int i = (a > b) ? a : b; for (; i <= 10000; i++) { while (arr[i][2] && countA > 0 && countB > 0) { if (lastA[countA - 1] + lastB[countB - 1] - i > 0) { time = time - lastA[countA - 1] - lastB[countB - 1] + i; countA--; countB--; arr[i][2]--; } else { cout << time; return 0; } } } cout << time; 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> 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 (ak == 0 || bk == 0) break; 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> using namespace std; int main() { long long n, k, i, ok, s = 0, sum = 0, ans = 0; multimap<long long, long long> pi1, pi2, pi3; multimap<long long, long long>::iterator it; cin >> n >> ok; long long a, b, c; for (i = 0; i < n; i++) { cin >> a >> b >> c; if (b == 1) { pi1.insert({a, b + c}); } if (c == 1) { pi2.insert({a, b + c}); } } long long x = pi1.size(); long long y = pi2.size(); if (x < ok || y < ok) { cout << -1 << endl; } else { long long z = ok; for (it = pi1.begin(); it != pi1.end(); it++) { s++; ans += it->first; if (it->second == 2) z--; if (s == ok) break; } s = 0; for (it = pi2.begin(); it != pi2.end(); it++) { if (s == z) break; if (it->second != 2) { ans += it->first; s++; } } cout << ans << endl; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
/** * ******* Created on 28/6/20 7:53 PM******* */ import java.io.*; import java.util.*; public class E1374 implements Runnable { private static final int MAX = (int) (1E5 + 5); private static final int MOD = (int) (1E9 + 7); private static final long Inf = (long) (1E14 + 10); private static final double eps = (double) (1E-9); private void solve() throws IOException { int t = 1; while (t-- > 0) { int n = reader.nextInt(); int k = reader.nextInt(); List<Integer> al = new ArrayList<>(); List<Integer> bob = new ArrayList<>(); List<Integer> both = new ArrayList<>(); for(int i=0;i<n;i++){ int a = reader.nextInt(); int b = reader.nextInt(); int c = reader.nextInt(); if(b ==1 && c==1){ both.add(a); } else if(b==1){ al.add(a); }else bob.add(a); } Collections.sort(al); Collections.sort(bob); Collections.sort(both); int pos1 =0,pos2 =0,pos3 =0,tot =0; long sum =0; for(int i=0;i<k;i++){ if(pos1 < al.size() && pos2 < bob.size() && (pos3 >= both.size() || (pos3 < both.size() && al.get(pos1) + bob.get(pos2) < both.get(pos3)) ) ){ sum += (long) (al.get(pos1) + bob.get(pos2) ); pos1++; pos2++; }else if(pos3 < both.size()){ sum += (long)both.get(pos3); pos3++; } } if(pos1 + pos3 ==k && pos2 + pos3 ==k) writer.println(sum); else writer.println("-1"); } } public static void main(String[] args) throws IOException { try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) { new E1374().run(); } } StandardInput reader; PrintWriter writer; @Override public void run() { try { reader = new StandardInput(); writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } interface Input extends Closeable { String next() throws IOException; String nextLine() throws IOException; default int nextInt() throws IOException { return Integer.parseInt(next()); } default long nextLong() throws IOException { return Long.parseLong(next()); } default double nextDouble() throws IOException { return Double.parseDouble(next()); } default int[] readIntArray() throws IOException { return readIntArray(nextInt()); } default int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i = 0; i < array.length; i++) { array[i] = nextInt(); } return array; } default long[] readLongArray(int size) throws IOException { long[] array = new long[size]; for (int i = 0; i < array.length; i++) { array[i] = nextLong(); } return array; } } private static class StandardInput implements Input { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer stringTokenizer; @Override public void close() throws IOException { reader.close(); } @Override public String next() throws IOException { if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } @Override public String nextLine() throws IOException { return reader.readLine(); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
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 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; for (int i = 0; i < both.size() && chosen.size() <= m; i++) { chosen.add(both.get(i)); time = time + both.get(i).getT(); } int sm = chosen.size(); if (chosen.size() < k) { for (int i = 0; i < ai.size() && i < bi.size() && chosen.size() <= m; i++) { if (chosen.size() + 2 <= m) { chosen.add(ai.get(i)); chosen.add(bi.get(i)); time = time + ai.get(i).getT() + bi.get(i).getT(); ai.remove(i); bi.remove(i); } } } int x = 0; for (int i = 0; i < sm && i < ai.size() && i < bi.size() && chosen.size() <= m; i++) { if (chosen.get(sm - i - 1).getT() > ai.get(i).getT() + bi.get(i).getT()) { if (chosen.size() + 2 <= m) { time = time - chosen.get(sm - i - 1).getT(); chosen.remove(sm - i - 1); chosen.add(ai.get(i)); chosen.add(bi.get(i)); time = time + ai.get(i).getT() + bi.get(i).getT(); x++; } } } sm = sm - x; if (sm + (chosen.size() - sm) / 2 < k) { System.out.println(-1); bar = true; } else { for (int i = 0; i < bk.size() && chosen.size() < m; i++) { if (chosen.contains(bk.get(i))) { continue; } else { chosen.add(bk.get(i)); time = time + bk.get(i).getT(); } } } if (chosen.size() < m && !bar) { System.out.println(-1); } else { if (!bar) { 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> #pragma GCC optimize("-O3") using namespace std; const double PI = acos(-1); long long gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); { long long n, m, k, t, a, b; cin >> n >> m >> k; vector<long long> va, vb, vab, nvab, vals; vector<long long> _va, _vb, _vab; long long x = 0, y = 0; for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) { cin >> t >> a >> b; vals.emplace_back(t); if (a == 0 && b == 0) { nvab.emplace_back(i); continue; } x += a; y += b; if (a == 1 && b == 1) vab.emplace_back(i); else if (a == 1) va.emplace_back(i); else vb.emplace_back(i); } if (x < k || y < k) { cout << "-1\n"; return 0; } auto fun = [&](long long x, long long y) -> bool { return vals[x] < vals[y]; }; sort((va).begin(), (va).end(), fun); sort((vb).begin(), (vb).end(), fun); sort((vab).begin(), (vab).end(), fun); _va = va; _vb = vb; _vab = vab; while (va.size() > k) va.pop_back(); while (vb.size() > k) vb.pop_back(); vector<long long> res; for (auto it : vab) { if (va.size() + res.size() != k || vb.size() + res.size() != k) { res.emplace_back(it); if (va.size() + res.size() > k) va.pop_back(); if (vb.size() + res.size() > k) vb.pop_back(); continue; } if (va.size() > 0 && vb.size() > 0) { if ((vals[va.back()] + vals[vb.back()]) > vals[it]) { va.pop_back(); vb.pop_back(); res.emplace_back(it); } } else { if (va.size() > 0) { if (vals[va.back()] > vals[it]) { va.pop_back(); res.emplace_back(it); } } else if (vb.size() > 0) { if (vals[vb.back()] > vals[it]) { vb.pop_back(); res.emplace_back(it); } } } } vector<bool> vis(n, 0); long long ans = 0; for (auto it : va) ans += vals[it], vis[it] = 1; for (auto it : vb) ans += vals[it], vis[it] = 1; for (auto it : res) ans += vals[it], vis[it] = 1; nvab.clear(); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) if (!vis[i]) nvab.emplace_back(i); sort((nvab).begin(), (nvab).end(), fun); if (m < (va.size() + vb.size() + res.size())) { va = _va; vb = _vb; vab = _vab; a = va.size(); b = vb.size(); t = vab.size(); if ((a + t) < k || (b + t) < k) { cout << "-1\n"; return 0; } vector<long long> pva(1, 0), pvb(1, 0); for (auto it : va) pva.emplace_back(pva.back() + vals[it]); for (auto it : vb) pvb.emplace_back(pvb.back() + vals[it]); long long sum = LLONG_MAX; long long temp; long long p = 0; bool f = 0; if (m == 2 * k && k <= va.size() && k <= vb.size()) { f = 1; sum = pva[k] + pvb[k]; a = k; b = 0; } for (__typeof(vab.size()) i = (0) - ((0) > (vab.size())); i != (vab.size()) - ((0) > (vab.size())); i += 1 - 2 * ((0) > (vab.size()))) { p += vals[vab[i]]; if ((2 * k - (i + 1)) != m || (k - (i + 1)) > va.size() || (k - (i + 1)) > vb.size()) continue; f = 1; temp = p + pva[k - (i + 1)] + pvb[k - (i + 1)]; if (temp < sum) { sum = temp; a = k - (i + 1); b = i + 1; } } if (!f) { cout << "-1\n"; return 0; } cout << sum << "\n"; for (__typeof(a) i = (0) - ((0) > (a)); i != (a) - ((0) > (a)); i += 1 - 2 * ((0) > (a))) cout << va[i] + 1 << " "; for (__typeof(a) i = (0) - ((0) > (a)); i != (a) - ((0) > (a)); i += 1 - 2 * ((0) > (a))) cout << vb[i] + 1 << " "; for (__typeof(b) i = (0) - ((0) > (b)); i != (b) - ((0) > (b)); i += 1 - 2 * ((0) > (b))) cout << vab[i] + 1 << " "; cout << "\n"; return 0; } m -= va.size(); m -= vb.size(); m -= res.size(); for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m)); i += 1 - 2 * ((0) > (m))) ans += vals[nvab[i]]; cout << ans << "\n"; for (auto it : va) cout << it + 1 << " "; for (auto it : vb) cout << it + 1 << " "; for (auto it : res) cout << it + 1 << " "; for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m)); i += 1 - 2 * ((0) > (m))) cout << nvab[i] + 1 << " "; 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
python3
n, k = map(int, input().split()) d_a = set() d_b = set() for _ in range(n): t, a, b = map(int, input().split()) if a == 1: d_a.add(t) if b == 1: d_b.add(t) if len(d_a) < k or len(d_b) < k: print(-1) else: l_a = list(d_a)[:k] l_b = list(d_b)[:k] u = d_a & d_b if len(u) > k: print(sum(list(sorted(list(u)))[:k])) else: l_a = list(sorted(list(d_a - u)))[:k-len(u)] l_b = list(sorted(list(d_b - u)))[:k-len(u)] print(sum(l_a)+sum(l_b)+sum(u))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class E1{ 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 NumberFormatException, IOException { FastReader s=new FastReader(); int n=s.nextInt(); int k=s.nextInt(); int counta=0,countb=0,countab=0,ans=0,temp1,temp2,temp3; ArrayList<Integer> arrab = new ArrayList<Integer>(); ArrayList<Integer> arra = new ArrayList<Integer>(); ArrayList<Integer> arrb = new ArrayList<Integer>(); for(int i=0;i<n;i++){ temp1=s.nextInt(); temp2=s.nextInt(); temp3=s.nextInt(); if(temp2==1&&temp3==1){ arrab.add(temp1); countab++; } else if(temp2==1&&temp3==0){ arra.add(temp1); counta++; } else if(temp2==0&&temp3==1){ arrb.add(temp1); countb++; } } Collections.sort(arra);Collections.sort(arrb);Collections.sort(arrab); temp1=0;temp2=0;temp3=0; if((counta+countab)<k||(countb+countab)<k){ ans=-1; } else if(counta==0||countb==0){ for(int i=0;i<k;i++) ans+=arrab.get(i); } else{ int i=0,j=0,sum=0;temp3=0;int ext=0; while(temp1<k||temp2<k){ sum=0;ext=0; if(i<counta){ sum+=arra.get(i); ext=1; } if(j<countb){ sum+=arrb.get(j); ext=1; } if((!arrab.isEmpty()&&temp3<countab&&arrab.get(temp3)<=sum)||ext==0){ ans+=arrab.get(temp3); temp3++; temp1++;temp2++; } else{ if(i<counta){ ans+=arra.get(i); i++; temp1++; } if(j<countb){ ans+=arrb.get(j); j++; temp2++; } } } } System.out.println(ans); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class R653E2{ public static void main(String[] main) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); TreeSet<Book> total = new TreeSet<Book>(); TreeSet<Book>[][] types = new TreeSet[2][2]; types[0][0] = new TreeSet<Book>(); types[0][1] = new TreeSet<Book>(); types[1][0] = new TreeSet<Book>(); types[1][1] = new TreeSet<Book>(); for(int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); Book temp = new Book(t, a, b, i+1); total.add(temp); types[a][b].add(temp); } if(types[1][1].size() + Math.min(types[0][1].size(),types[1][0].size()) < k || 2*k-types[1][1].size() > m) out.println(-1); else { TreeSet<Book> read = new TreeSet<Book>(); TreeSet<Book>[][] readtypes = new TreeSet[2][2]; readtypes[0][0] = new TreeSet<Book>(); readtypes[0][1] = new TreeSet<Book>(); readtypes[1][0] = new TreeSet<Book>(); readtypes[1][1] = new TreeSet<Book>(); int minsum = 0; int t1 = types[1][1].size(); int t01 = types[0][1].size(); int t10 = types[1][0].size(); for(int i = 0; i < Math.min(k, t1); i++) { Book temp = types[1][1].pollFirst(); read.add(temp); readtypes[1][1].add(temp); total.remove(temp); minsum += temp.getT(); } for(int i = 0; i < k - Math.min(k, t1); i++) { Book temp = types[0][1].pollFirst(); read.add(temp); readtypes[0][1].add(temp); total.remove(temp); minsum += temp.getT(); temp = types[1][0].pollFirst(); read.add(temp); readtypes[1][0].add(temp); total.remove(temp); minsum += temp.getT(); } for(int i = 0; i < m-2*k+Math.min(k,t1); i++) { Book temp = total.pollFirst(); read.add(temp); int a = temp.getA(); int b = temp.getB(); readtypes[a][b].add(temp); types[a][b].remove(temp); minsum += temp.getT(); } int num11 = Math.min(k, t1); int currsum = minsum; for(int i = Math.min(k, t1); i > Math.max(2*k-m, Math.max(0,k - Math.min(t01,t10))); i--) { Book temp = readtypes[1][1].pollLast(); read.remove(temp); currsum -= temp.getT(); if(k - readtypes[1][1].size() > readtypes[0][1].size() && k - readtypes[1][1].size() > readtypes[1][0].size()) { temp = types[0][1].pollFirst(); read.add(temp); readtypes[0][1].add(temp); total.remove(temp); currsum += temp.getT(); temp = types[1][0].pollFirst(); read.add(temp); readtypes[1][0].add(temp); total.remove(temp); currsum += temp.getT(); temp = readtypes[0][0].pollLast(); read.remove(temp); currsum -= temp.getT(); total.add(temp); types[0][0].add(temp); } else if(k - readtypes[1][1].size() > readtypes[0][1].size()) { temp = types[0][1].pollFirst(); read.add(temp); readtypes[0][1].add(temp); total.remove(temp); currsum += temp.getT(); } else if(k - readtypes[1][1].size() > readtypes[1][0].size()) { temp = types[1][0].pollFirst(); read.add(temp); readtypes[1][0].add(temp); total.remove(temp); currsum += temp.getT(); } else { temp = total.pollFirst(); read.add(temp); int a = temp.getA(); int b = temp.getB(); readtypes[a][b].add(temp); types[a][b].remove(temp); currsum += temp.getT(); } if(minsum > currsum) { num11 = readtypes[1][1].size(); minsum = currsum; } } out.println(minsum); StringJoiner sj = new StringJoiner(" "); if(num11 != Math.min(k, t1) && num11 != readtypes[1][1].size()) { for(Book temp: read) { total.add(temp); int a = temp.getA(); int b = temp.getB(); types[a][b].add(temp); } read = new TreeSet<Book>(); for(int i = 0; i < num11; i++) { Book temp = types[1][1].pollFirst(); read.add(temp); total.remove(temp); minsum += temp.getT(); } for(int i = 0; i < k - num11; i++) { Book temp = types[0][1].pollFirst(); read.add(temp); total.remove(temp); minsum += temp.getT(); temp = types[1][0].pollFirst(); read.add(temp); total.remove(temp); minsum += temp.getT(); } for(int i = 0; i < m-2*k+num11; i++) { Book temp = total.pollFirst(); read.add(temp); int a = temp.getA(); int b = temp.getB(); types[a][b].remove(temp); minsum += temp.getT(); } } for(Book b: read) { sj.add(Integer.toString(b.getIndex())); } out.println(sj); } out.close(); } } class Book implements Comparable<Book>{ private int time; private int alice; private int bob; private int index; public Book(int t, int a, int b, int i) { time = t; alice = a; bob = b; index = i; } public int getT() { return time; } public int getA() { return alice; } public int getB() { return bob; } public int compareTo(Book b) { if(time == b.getT()) return index - b.getIndex(); return time-b.getT(); } public int getIndex() { return index; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- import math, string, itertools, operator, fractions, heapq, collections, re, array, bisect, sys, functools def solve(line): n, m, k = map(int, line.split()) a = [] for i in range(n): a.append([int(x) for x in sys.stdin.readline().rstrip().split()]) x, y, z, w = [], [], [], [] for i in range(n): if a[i][1] == 1 and a[i][2] == 1: z.append((a[i][0], i)) elif a[i][1] == 1: x.append((a[i][0], i)) elif a[i][2] == 1: y.append((a[i][0], i)) else: w.append((a[i][0], i)) if len(x) + len(z) < k or len(y) + len(z) < k: print(-1);return x.sort() y.sort() z.sort() w.sort() ans = 0 aa = set() i, j, k1 = 0, 0, 0 k0 = k while k > 0: if i < len(x) and j < len(y) and k1 < len(z) and x[i][0] + y[j][0] < z[k1][0] and 2 + len(aa) <= m: aa.add(x[i][1]) aa.add(y[i][1]) i += 1 j += 1 elif k1 < len(z): aa.add(z[k1][1]) k1 += 1 elif 2 + len(aa) <= m: i += 1 j += 1 aa.add(x[i][1]) aa.add(y[i][1]) k -= 1 k2 = 0 if len(aa) > m: return -1 while m > len(aa): aa.add(w[k2][1]) k2 += 1 ans = 0 for i in aa: ans += a[i][0] print(ans) for idx, i in enumerate(aa): print(i + 1, end=(' ' if idx != len(aa) - 1 else '\n')) t = 0 while True: line = sys.stdin.readline().rstrip() if not line: break solve(line) t += 1
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int i, j, n, m, k, t, x = 0, y, ans = 0, minn, x1 = 0, x2 = 0, y1 = 0, y2 = 0; vector<pair<long long, pair<long long, pair<long long, long long> > > > v, v1, v0; cin >> n >> m >> k; for (i = 0; i < n; i++) { cin >> t >> x >> y; if (x != 0 || y != 0) v.push_back(make_pair(t, make_pair(i + 1, make_pair(x, y)))); else v0.push_back(make_pair(t, make_pair(i + 1, make_pair(x, y)))); if (x == 1) x1++; if (y == 1) y1++; } if (x1 < k || y1 < k) cout << "-1"; else { sort((v).begin(), (v).end()); for (i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second.first << " " << v[i].second.second.first << " " << v[i].second.second.second << "\n"; i = 0; while ((x2 < k || y2 < k) && i < v.size()) { if (v[i].second.second.first == 1 && v[i].second.second.second == 0) { if (x2 < k && k - y2 < m - v1.size()) { v1.push_back(v[i]); x2++; } else v0.push_back(v[i]); } else if (v[i].second.second.first == 0 && v[i].second.second.second == 1) { if (y2 < k && k - x2 < m - v1.size()) { v1.push_back(v[i]); y2++; } else v0.push_back(v[i]); } else { v1.push_back(v[i]); x2++; y2++; } i++; } if (x2 < k || y2 < k) { cout << "-1"; exit(0); } for (i = i; i < v.size(); i++) v0.push_back(v[i]); sort((v0).begin(), (v0).end()); for (i = 0; i < m - v1.size(); i++) ans += v0[i].first; for (i = 0; i < v1.size(); i++) ans += v1[i].first; cout << ans << "\n"; for (i = 0; i < v1.size(); i++) cout << v1[i].second.first << " "; for (i = 0; i < m - v1.size(); i++) cout << v0[i].second.first << " "; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Q3 { public static void main(String[] args) { InputReader in = new InputReader(true); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(), k = in.nextInt(); int arr[][] = new int[n][3]; long ans = 0, cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; i++) { arr[i][0] = in.nextInt(); arr[i][1] = in.nextInt(); arr[i][2] = in.nextInt(); if (arr[i][1] == 1) cnt1++; if (arr[i][2] == 1) cnt2++; } if (cnt1 < k || cnt2 < k) { out.println(-1); } else { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { if (o1[1] == o2[1] && o1[2] == o2[2]) return Integer.compare(o1[0], o2[0]); if (o2[1] == o2[2] && o2[1] == 1) return 1; if (o1[1] == o1[2] && o1[1] == 1) return -1; return Integer.compare(o1[0], o2[0]); } }); int c1=0,c2=0; ArrayList<Integer> lt=new ArrayList<>(); for(int i =0;i<n;i++){ // System.out.println(arr[i][0]+" "+arr[i][1]+" "+arr[i][2]); if(arr[i][1]==1 && arr[i][2]==1 && c1<k && c2<k){ ans+=arr[i][0]; c1++; c2++; }else if(c1<k && c2<k){ ans+=arr[i][0]; if(arr[i][1]==1) c1++; else c2++; }else if(c1==k && arr[i][2]==1){ lt.add(arr[i][0]); }else if(c2==k && arr[i][1]==1){ lt.add(arr[i][0]); } } if(c1!=k || c2!=k){ Collections.sort(lt); int i =0; while(c2<k){ ans+=lt.get(i++); c2++; } while(c1<k){ ans+=lt.get(i++); c1++; } } out.println(ans); } out.close(); } static class InputReader { int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) p[f]++; for (int t : to) p[t]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } InputStream is; public InputReader(boolean onlineJudge) { is = System.in; } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); int c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); long c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } int[] uniq(int[] arr) { Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] uniq(long[] arr) { Arrays.sort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] reverse(int[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } long[] reverse(long[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } int[] compress(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } long[] compress(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } void deepFillInt(Object array, int val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof int[]) { int[] intArray = (int[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillInt(obj, val); } } } void deepFillLong(Object array, long val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof long[]) { long[] intArray = (long[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillLong(obj, val); } } } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
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(i)) 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(i)) 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 static java.lang.Math.*; import static java.util.Arrays.*; // failed public class cf1374e2_2 { public static void main(String[] args) throws IOException { int n = rni(), m = ni(), k = ni(); 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}; 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, ans11 = 0; Collections.sort(a11, (a, b) -> a[0] - b[0]); Collections.sort(a01, (a, b) -> a[0] - b[0]); Collections.sort(a10, (a, b) -> a[0] - b[0]); Collections.sort(a00, (a, b) -> a[0] - b[0]); Set<Integer> ansS = new HashSet<>(), curS = new HashSet<>(); long ans = LMAX, cur = 0; int[] e; while(p11 < c) { e = a11.get(p11++); cur += e[0]; curS.add(e[1]); } for(int j = c; j < k; ++j) { e = a01.get(p01++); cur += e[0]; curS.add(e[1]); e = a10.get(p10++); cur += e[0]; curS.add(e[1]); } for(int j = c + 2 * max(0 , k - c); j < m; ++j) { 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) { curS.add(a00.get(p00++)[1]); } else if(curmin == 1) { curS.add(a01.get(p01++)[1]); } else { curS.add(a10.get(p10++)[1]); } } ans = cur; ans11 = p11; while(p11 < min(m, n11)) { e = a11.get(p11++); cur += e[0]; curS.add(e[1]); int max = IMIN, curmax = -1; if(p00 > 0) { max = a00.get(p00 - 1)[0]; curmax = 0; } if(p01 > 0 && a01.get(p01 - 1)[0] > max) { max = a01.get(p01 - 1)[0]; curmax = 1; } if(p10 > 0 && a10.get(p10 - 1)[0] > max) { max = a10.get(p10 - 1)[0]; curmax = 2; } if(e[0] >= max) { break; } assert curmax >= 0; cur -= max; if(curmax == 0) { curS.remove(a00.get(--p00)[1]); } else if(curmax == 1) { curS.remove(a01.get(--p01)[1]); } else { curS.remove(a10.get(--p10)[1]); } if(cur < ans) { ans = cur; ans11 = p11; } } prln(ans); /* p11 = p01 = p10 = p00 = 0; while(p11 < ans11) { e = a11.get(p11++); ansS.add(e[1]); } for(int j = ans11; j < k; ++j) { e = a01.get(p01++); ansS.add(e[1]); e = a10.get(p10++); ansS.add(e[1]); } for(int j = ans11 + 2 * max(0, k - ans11); j < m; ++j) { 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) { curmin = 2; } if(curmin == 0) { ansS.add(a00.get(p00++)[1]); } else if(curmin == 1) { ansS.add(a01.get(p01++)[1]); } else { ansS.add(a10.get(p10++)[1]); } } */ prln(curS); } 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; bool sortbysec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } int32_t main() { long long int t = 1; while (t--) { long long int n, k; cin >> n >> k; vector<long long int> x; vector<long long int> y; vector<long long int> z; for (long long int i = 0; i < n; i++) { long long int a, b, c; cin >> a >> b >> c; if (b == 1 && c == 1) { x.push_back(a); } else if (b == 1) { y.push_back(a); } else z.push_back(a); } if (x.size() + min(y.size(), z.size()) < k) cout << "-1" << endl; else { sort(x.begin(), x.end()); sort(y.begin(), y.end()); sort(z.begin(), z.end()); long long int ans = 0, in = 0, ch = k - 1; if (x.size() < k) { for (long long int i = 0; i < k - x.size(); i++) ans += (y[i] + z[i]); ch = x.size() - 1; in = k - x.size(); } for (long long int i = in; i < min(z.size(), y.size()) && ch >= 0; i++) { if (y[i] + z[i] <= x[ch]) { ans += y[i] + z[i]; ch--; } else break; } for (long long int i = 0; i <= ch; i++) ans += x[i]; cout << ans << endl; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
//import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils; import java.util.*; import java.io.*; import java.math.BigInteger; public class Solution { static class Pair<A, B> { A parent; B rank; Pair(A parent, B rank) { this.rank = rank; this.parent = parent; } } static class Node { int ind; int value; long ans; Node(int i) { ind=i; value=0; ans=0; } } static int m=1000000007; static ArrayList<Integer> graph[]; public static void main(String[] args) throws IOException { FastReader s1 = new FastReader(); StringBuilder sb = new StringBuilder(); int n=s1.I(); int k=s1.I(); ArrayList<Integer> com=new ArrayList<>(); ArrayList<Integer> fir=new ArrayList<>(); ArrayList<Integer> sec=new ArrayList<>(); for(int i=0;i<n;i++) { int a=s1.I(); int b=s1.I(); int c=s1.I(); if(b==1 && c==1) com.add(a); else if(b==1) { fir.add(a); } else sec.add(a); } if(com.size()+fir.size()<k || com.size()+sec.size()<k) { System.out.println("-1"); System.exit(0); } Collections.sort(com); Collections.sort(fir); Collections.sort(sec); int x1=0; int x2=0; int x3=0; int count1=0; int count2=0; long time=0; if(fir.size()<k || sec.size()<k) { int max=Math.max(k-fir.size(), k-sec.size()); for(int i=0;i<max;i++) { time+=com.get(i); x1++; count1++; count2++; } } while(x1<com.size() && x2<fir.size() && x3<sec.size() && (count1<k && count2<k)) { if(com.get(x1)<=fir.get(x2)+sec.get(x3)) { time+=com.get(x1); x1++; } else { time+=fir.get(x2); time+=sec.get(x3); x2++; x3++; } count1++; count2++; } if(count1<k && count2<k) { if(x1>=com.size()) { while(count1<k) { time+=fir.get(x2); x2++; count1++; } while(count2<k) { time+=sec.get(x3); x3++; count2++; } } else { while(count1<k && count2<k) { time+=com.get(x1); x1++; count1++; count2++; } } } if(count1<k) { while(count1<k) { int tim=Integer.MAX_VALUE; if(x1<com.size()) { tim=com.get(x1); } if(x2<fir.size() && tim<fir.get(x2)) { tim=fir.get(x2++); } else x1++; time+=tim; count1++; } } if(count2<k) { while(count2<k) { int tim=Integer.MAX_VALUE; if(x1<com.size()) { tim=com.get(x1); } if(x3<sec.size() && tim<sec.get(x3)) { tim=sec.get(x3++); } else x1++; time+=tim; count2++; } } System.out.println(time); } 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 I() { return Integer.parseInt(next()); } long L() { return Long.parseLong(next()); } double D() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long gcd(long a, long b) { if (a % b == 0) { return b; } return gcd(b, a % b); } static float power(float x, int y) { float temp; if (y == 0) { return 1; } temp = power(x, y / 2); if (y % 2 == 0) { return temp * temp; } else { if (y > 0) { return x * temp * temp; } else { return (temp * temp) / x; } } } static long pow(long x, long y) { long res = 1; x = x % m; if (x < 0) { x += m; } while (y > 0) { if ((y & 1) == 1) { res = (res * x) % m; if (res < 0) { res += m; } } y = y >> 1; x = (x * x) % m; if (x < 0) { x = x + m; } } res = res % m; if (res < 0) { res += m; } return res; } static void sieveOfEratosthenes(int n) { ArrayList<Integer> prime = new ArrayList<Integer>(); boolean Prime[] = new boolean[n + 1]; for (int i = 2; i < n; i++) { Prime[i] = true; } for (int p = 2; p * p <= n; p++) { if (Prime[p] == true) { prime.add(p); for (int i = p * p; i <= n; i += p) { Prime[i] = 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
python3
n,k = map(int,input().split()) Both = [] Alice = [] Bob = [] for i in range(n): t,a,b = map(int,input().split()) if a == 1 and b == 1: Both.append(t) elif a == 1: Alice.append(t) elif b == 1: Bob.append(t) Both.sort() Alice.sort() Bob.sort() # print(Both) # print(Bob) i = 0 # print(Alice) act_read = [] # if len(Aice) if len(Alice) < k: # i = 0 r = k - len(Alice) while i < r: act_read.append(Both[i]) i = i + 1 if len(Bob) < k: r = k - len(Bob) j = 0 while j < r and i < len(Bob): act_read.append(Both[i]) # Bob.append(Both[i]) i = i + 1 j = j + 1 Both = Both[i:] Alice.sort() Bob.sort() Both = Both[i:] # d[a] = len(act_read) # d[b] = len(act_read) while len(act_read) < k and i < len(Both) and j < len(Alice) and k < len(Bob): if Both [i] <= Alice[j] + Bob[k]: act_read.append(Both[i]) i = i + 1 else: act_read.append(Alice[j]+Bob[k]) j = j + 1 k = k + 1 print(sum(act_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
python2
# template by 3xC and starkizard. # contributors: ##################################################################################### from __future__ import division, print_function import sys import os from collections import Counter, deque, defaultdict import itertools import math import io """Uncomment modules according to your need""" # from bisect import bisect_left, bisect_right, insort # from heapq import heappop, heapify, heappush # from random import randint as rn # from Queue import Queue as Q # from copy import deepcopy # from decimal import * # import re # import operator ##################################################################################### # this enables you to write python3 code with PyPy2 (Python 2) if sys.version_info[0] < 3: input = raw_input range = xrange filter = itertools.ifilter map = itertools.imap zip = itertools.izip ##################################################################################### """value of mod""" MOD = 998244353 mod = 10**9 + 7 """Uncomment next 4 lines if doing huge recursion""" # import threading # threading.stack_size(1<<27) # sys.setrecursionlimit(10000) def prepare_factorial(mod=mod): """ returns two lists, factorial and inverse factorial modulo argument by default 10**9 +7 """ # Comment code out when you don't need inverse factorial or vice versa fact = [1] for i in range(1, 200005): fact.append((fact[-1] * i) % mod) ifact = [0] * 200005 ifact[200004] = pow(fact[200004], mod - 2, mod) for i in range(200004, 0, -1): ifact[i - 1] = (i * ifact[i]) % mod return fact, ifact def modinv(n, p): """ returns N inverse modulo p """ return pow(n, p - 2, p) def ncr(n, r, fact, ifact,MOD=mod): """ takes 4 arguments: n , r and factorial and inverse factorial lists""" t = (fact[n] * (ifact[r]*ifact[n-r]) % MOD)% MOD return t def get_n(Sum): """this function returns the maximum n for which Summation(n) <= Sum""" ans = (-1 + sqrt(1 + 8*Sum))//2 return ans def sieve(n): """ returns a list of prime numbers till n """ if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): """ returns a list of all divisors till n """ divisors = [] #rdivisors=[] for i in range(start, int(math.sqrt(n) + 1)): if n % i == 0: if n / i == i: divisors.append(i) else: divisors.extend([i, n // i]) return divisors def divn(n, primes): """ returns the number of divisors, two arguments n and the sieve till n """ divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t return divs_number def lrfind(d, x, default=-1): """ Takes 2 arguments an iterable and an element. returns a tuple (firstoccurence,lastoccurence) -1 if not found """ left = right = -1 for i in range(len(d)): if d[i] == x: if left == -1: left = i right = i if left == -1: return default, default else: return left, right def gcd(x, y): # math.gcd is slower """ returns greatest common divisor of x and y """ while y: x, y = y, x % y return x def check_sorted(a): ''' returns True/False ''' for i in range(len(a)-1): if a[i]>a[i+1]: return False return True def ceil(n, k=1): return n // k + (n % k != 0) #returns math.ceil but protecting against floating inconsistencies def input(): return sys.stdin.readline().strip() def ii(): return int(input()) #inputs integer def mi(): return map(int, input().split()) # inputting space seperated variables for example x,y,z def li(): return list(map(int, input().split())) #inputting a space seperated list of integers def lw(): return input().split() #inputting a space seperated list of strings def lcm(a, b): return abs(a * b) // gcd(a, b) #returns LCM of two arguments def prr(a, sep=' ', end='\n'): print(sep.join(map(str, a)), end=end) #For printing an iterable with seperator sep as optional second argument (default : " "), ending character (default: "\n") as optional third def dd(): return defaultdict(int) #returns a dictionary with values defaulted to 0 def ddl(): return defaultdict(list) #returns a dictionary with values defaulted to [] def write(s): return sys.stdout.write(s) ################################################################### d={} def solve(books,ak,bk,i): if ak<=0 and bk<=0: return 0 if i<0: return float("inf") a,b=books[i][1],books[i][2] val=books[i][0] if (ak,bk,i-1) not in d: d[(ak,bk,i-1)]=solve(books,ak,bk,i-1) if a and b: if (ak-1,bk-1,i-1) not in d: d[(ak-1,bk-1,i-1)]=solve(books,ak-1,bk-1,i-1) return min(d[(ak,bk,i-1)],val+d[(ak-1,bk-1,i-1)]) if a: if (ak-1,bk,i-1) not in d: d[(ak-1,bk,i-1)]=solve(books,ak-1,bk,i-1) return min(d[(ak,bk,i-1)],val+d[(ak-1,bk,i-1)]) if b: if (ak,bk-1,i-1) not in d: d[(ak,bk-1,i-1)]=solve(books,ak,bk-1,i-1) return min(d[(ak,bk,i-1)],val+d[(ak,bk-1,i-1)]) def main(): n,k=mi() print(n,k) abooks=[] aset=set() bset=set() bbooks=[] books=[] for i in range(n): a=li() if a[1]==0 and a[2]==0: continue books.append(a) if a[1]==1: abooks.append((a[0],i)) aset.add(i) if a[2]==1: bbooks.append((a[0],i)) bset.add(i) answertimeA=0 if len(abooks)<k or len(bbooks)<k: print(-1) return print(solve(books,k,k,len(books)-1)) """ -------- Python 2 and 3 footer by Pajenegod and c1729 ---------""" py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO, self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s: self.buffer.write(s.encode('ascii')) self.read = lambda: self.buffer.read().decode('ascii') self.readline = lambda: self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') """ main function""" if __name__ == '__main__': main() #threading.Thread(target=main).start()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.StringTokenizer; public class q5 { static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); public static class node{ int ind; node left; node right; } public static void main(String[] args) { FastReader s = new FastReader(); int t = 1; while(t-- > 0) { int n = s.nextInt(); int k = s.nextInt(); long ans = 0; PriorityQueue<Integer> c = new PriorityQueue<>(); PriorityQueue<Integer> a = new PriorityQueue<>(); PriorityQueue<Integer> b = new PriorityQueue<>(); for(int i=0;i<n;++i) { int l = s.nextInt(); int m = s.nextInt(); int p = s.nextInt(); if(m == 1 && p == 1) c.add(l); else if(m == 1) a.add(l); else if(p == 1) b.add(l); } int ca = 0; int cb = 0; while(ca < k && cb < k) { if(a.isEmpty() && b.isEmpty() && c.isEmpty()) break; else if(a.isEmpty() && b.isEmpty() && !c.isEmpty()) { ans += c.poll(); ca++;cb++; } else if(!a.isEmpty() && b.isEmpty() && c.isEmpty()) { ans += a.poll(); ca++; } else if(a.isEmpty() && !b.isEmpty() && c.isEmpty()) { ans += b.poll(); cb++; } else if(!a.isEmpty() && !b.isEmpty() && c.isEmpty()) { ans += b.poll(); ans += a.poll(); cb++;ca++; } else if(!a.isEmpty() && b.isEmpty() && !c.isEmpty()) { ans += c.poll(); ca++;cb++; } else if(a.isEmpty() && !b.isEmpty() && !c.isEmpty()) { ans += c.poll(); ca++;cb++; } else{ if(a.peek() + b.peek() < c.peek()) { ans += b.poll(); ans += a.poll(); cb++;ca++; } else{ ans += c.poll(); ca++;cb++; } } } if(ca < k && cb < k ) out.println(-1); else out.println(ans); } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; struct P { int t, a, b; }; vector<P> A, B, C; int sa[N], sb[N], sc[N]; bool cmp(P a, P b) { return a.t < b.t; } int main() { int n, k; P t; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d%d%d", &t.t, &t.a, &t.b); if (t.a && t.b) { C.push_back(t); } else if (t.a) { A.push_back(t); } else if (t.b) { B.push_back(t); } } sort(A.begin(), A.end(), cmp); sort(B.begin(), B.end(), cmp); sort(C.begin(), C.end(), cmp); if (A.size()) sa[1] = A[0].t; for (int i = 1; i < A.size(); ++i) { sa[i + 1] = sa[i] + A[i].t; } if (B.size()) sb[1] = B[0].t; for (int i = 1; i < B.size(); ++i) { sb[i + 1] = sb[i] + B[i].t; } if (C.size()) sc[1] = C[0].t; for (int i = 1; i < C.size(); ++i) { sc[i + 1] = sc[i] + C[i].t; } int Min = 0x7fffffff; for (int i = 0; i <= C.size(); ++i) { if (k - i > A.size() || k - i > B.size()) continue; if (sc[i] + sa[k - i] + sb[k - i] < Min) Min = sc[i] + sa[k - i] + sb[k - i]; } if (Min != 0x7ffffffff) printf("%d\n", Min); else printf("-1\n"); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k, x, y, z, m; cin >> n >> m >> k; vector<pair<int, int> > a; vector<pair<int, int> > b; vector<pair<int, int> > c; vector<pair<int, int> > e; long long unsigned int ans = 0; for (int i = 0; i < n; i++) { cin >> x >> y >> z; if (z + y == 2) a.push_back(make_pair(x, i + 1)); else if (z == 1) b.push_back(make_pair(x, i + 1)); else if (y == 1) c.push_back(make_pair(x, i + 1)); else e.push_back(make_pair(x, i + 1)); } if ((b.size() + a.size() < k) || (c.size() + a.size() < k)) { cout << -1 << "\n"; } else if (m == k) { if (a.size() >= k) { sort(a.begin(), a.end()); for (int i = 0; i < k; i++) { ans += a[i].first; } cout << ans << "\n"; for (int i = 0; i < k; i++) { cout << a[i].second << " "; } } else cout << -1 << "\n"; } else if (m < 2 * k) { sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); sort(e.begin(), e.end()); vector<pair<int, int> > d; long long int yy; if (b.size() > c.size()) { if (k > c.size()) yy = c.size(); else yy = k; } else { if (k > b.size()) yy = b.size(); else yy = k; } a.push_back(make_pair(INT_MAX / 2, 0)); b.push_back(make_pair(INT_MAX / 2, 0)); c.push_back(make_pair(INT_MAX / 2, 0)); e.push_back(make_pair(INT_MAX / 2, 0)); for (int i = 0; i < yy; i++) d.push_back( make_pair(b[i].first + c[i].first, b[i].second + c[i].second)); d.push_back(make_pair(INT_MAX / 2, 0)); if (yy == 0) { vector<int> aans; for (int i = 0; i < k; i++) { ans += a[i].first; aans.push_back(a[i].second); } d.clear(); int uu = 0; int ree = k; int ww = 0; int kk = 0; while ((c[uu].first != INT_MAX) || (b[ww].first != INT_MAX) || (e[kk].first != INT_MAX)) { if (c[uu].first > b[ww].first) { if (e[kk].first > b[ww].first) d.push_back(b[ww++]); else d.push_back(e[kk++]); } else { if (e[kk].first > c[uu].first) d.push_back(c[uu++]); else d.push_back(e[kk++]); } } d.push_back(make_pair(INT_MAX / 2, 0)); int s = 0; int t = k; while (ree < m) { if (d[s].first <= a[t].first) { ans += d[s].first; aans.push_back(d[s++].second); } else { ans += a[t].first; aans.push_back(a[t++].second); } ree++; } cout << ans << "\n"; for (int i = 0; i < aans.size(); i++) cout << aans[i] << " "; } else { int r = 0, s = 0, t = 2 * k - m, l = 0; vector<int> aans; if (a.size() - 1 < 2 * k - m) cout << -1 << "\n"; else { int ree = 2 * k - m; for (int i = 0; i < 2 * k - m; i++) { ans += a[i].first; aans.push_back(a[i].second); } int yyy = yy; while (r < m - k) { if (d[s].first <= a[t].first) { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } else { if (c[yy].first < b[yyy].first) { if (c[yy].first > e[l].first) { if (e[l].first < min(b[s].first, c[s].first)) { if (e[l].first + a[t].first < b[s].first + c[s].first) { ans += a[t].first + e[l].first; aans.push_back(a[t++].second); aans.push_back(e[l++].second); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } else { if (c[yy].first < min(b[s].first, c[s].first)) { if (c[yy].first + a[t].first < b[s].first + c[s].first) { ans += a[t].first + c[yy].first; aans.push_back(a[t++].second); aans.push_back(c[yy].second); c.erase(c.begin() + yy); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } } else { if (b[yyy].first > e[l].first) { if (e[l].first < min(b[s].first, c[s].first)) { if (e[l].first + a[t].first < b[s].first + c[s].first) { ans += a[t].first + e[l].first; aans.push_back(a[t++].second); aans.push_back(e[l++].second); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } else { if (b[yyy].first < min(b[s].first, c[s].first)) { if (b[yy].first + a[t].first < b[s].first + c[s].first) { ans += a[t].first + b[yyy].first; aans.push_back(a[t++].second); aans.push_back(b[yyy].second); b.erase(b.begin() + yyy); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } } } r++; } d.clear(); int uu = s; int ww = s; int kk = l; while ((c[uu].first != INT_MAX / 2) || (b[ww].first != INT_MAX / 2) || (e[kk].first != INT_MAX / 2)) { if (c[uu].first > b[ww].first) { if (e[kk].first > b[ww].first) d.push_back(b[ww++]); else d.push_back(e[kk++]); } else { if (e[kk].first > c[uu].first) d.push_back(c[uu++]); else d.push_back(e[kk++]); } } d.push_back(make_pair(INT_MAX / 2, 0)); s = 0; while (ree < m) { if (d[s].first <= a[t].first) { ans += d[s].first; aans.push_back(d[s++].second); } else { ans += a[t].first; aans.push_back(a[t++].second); } ree++; } cout << ans << "\n"; for (int i = 0; i < aans.size(); i++) cout << aans[i] << " "; } } } else { sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); sort(e.begin(), e.end()); vector<pair<int, int> > d; long long int yy; if (b.size() > c.size()) { yy = c.size(); } else { yy = b.size(); } a.push_back(make_pair(INT_MAX / 2, 0)); b.push_back(make_pair(INT_MAX / 2, 0)); c.push_back(make_pair(INT_MAX / 2, 0)); e.push_back(make_pair(INT_MAX / 2, 0)); for (int i = 0; i < yy; i++) d.push_back( make_pair(b[i].first + c[i].first, b[i].second + c[i].second)); d.push_back(make_pair(INT_MAX / 2, 0)); int r = 0, s = 0, t = 0, ree = 0, l = 0; vector<int> aans; int yyy = yy; vector<int> yt; vector<pair<int, int> > v; while (r < k) { if (d[s].first <= a[t].first) { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } else if (a[t].first + a[t + 1].first <= c[s].first + b[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); ree += 2; r++; } else { if (c[yy].first < b[yyy].first) { if (c[yy].first >= e[l].first) { if (e[l].first <= min(b[s].first, c[s].first)) { if (e[l].first <= a[t + 1].first) { if (e[l].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + e[l].first; aans.push_back(a[t++].second); aans.push_back(e[l++].second); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } else { if (min(c[s].first, b[s].first) < a[t + 1].first) { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { v.push_back(make_pair(a[t].first, aans.size())); yt.push_back(a[t].second); ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } } else { if (c[yy].first < min(b[s].first, c[s].first)) { if (c[yy].first < a[t + 1].first) { if (c[yy].first + a[t].first < b[s].first + c[s].first) { ans += a[t].first + c[yy].first; aans.push_back(a[t++].second); aans.push_back(c[yy].second); c.erase(c.begin() + yy); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } else { if (min(c[s].first, b[s].first) < a[t + 1].first) { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { v.push_back(make_pair(a[t].first, aans.size())); yt.push_back(a[t].second); ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } } } else { if (b[yyy].first >= e[l].first) { if (e[l].first <= min(b[s].first, c[s].first)) { if (e[l].first < a[t + 1].first) { if (e[l].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + e[l].first; aans.push_back(a[t++].second); aans.push_back(e[l++].second); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } else { if (a[t + 1].first > min(c[s].first, b[s].first)) { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { v.push_back(make_pair(a[t].first, aans.size())); yt.push_back(a[t].second); ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } } else { if (b[yyy].first <= min(b[s].first, c[s].first)) { if (a[t + 1].first > b[yyy].first) { if (b[yyy].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + b[yyy].first; aans.push_back(a[t++].second); aans.push_back(b[yyy].second); b.erase(b.begin() + yyy); ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } else { if (a[t + 1].first > min(c[s].first, b[s].first)) { if (min(c[s].first, b[s].first) + a[t].first < b[s].first + c[s].first) { v.push_back(make_pair(a[t].first, aans.size())); yt.push_back(a[t].second); ans += a[t].first; aans.push_back(a[t++].second); ree += 1; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } else { if (a[t + 1].first + a[t].first <= b[s].first + c[s].first) { ans += a[t].first + a[t + 1].first; aans.push_back(a[t++].second); aans.push_back(a[t++].second); r++; ree += 2; } else { ans += d[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); ree += 2; } } } } } } r++; } vector<pair<int, int> > aa; int x = 0; for (int i = 0; i < v.size(); i++) { if (v[i].first + min(min(a[t].first, e[l].first), min(b[s].first, c[s].first)) <= b[s].first + c[s].first) { if (b[s].first < c[s].first) { if (e[l].first <= b[s].first) { if (e[l].first <= a[t].first) { aans.push_back(e[l].second); ans += e[l++].first; ree++; } else { aans.push_back(a[t].second); ans += a[t++].first; ree++; } } else { if (b[s].first < a[t].first) { aans.push_back(b[s].second); ans += b[s].first; b.erase(b.begin() + s); ree++; } else { aans.push_back(a[t].second); ans += a[t++].first; ree++; } } } else { if (e[l].first <= c[s].first) { if (e[l].first <= a[t].first) { aans.push_back(e[l].second); ans += e[l++].first; ree++; } else { aans.push_back(a[t].second); ans += a[t++].first; ree++; } } else { if (a[t].first > c[s].first) { aans.push_back(c[s].second); ans += c[s].first; c.erase(c.begin() + s); ree++; } else { aans.push_back(a[t].second); ans += a[t++].first; ree++; } } } } else { ans -= v[i].first; aans.erase(aans.begin() + v[i].second - x); x++; ans += b[s].first + c[s].first; aans.push_back(b[s].second); aans.push_back(c[s++].second); aa.push_back(make_pair(v[i].first, yt[i])); ree++; } } int tt = t; for (int i = 0; i < aa.size(); i++) { a.insert(a.begin() + tt, aa[i]); tt++; } d.clear(); int uu = s; int ww = s; int kk = l; while ((c[uu].first != INT_MAX / 2) || (b[ww].first != INT_MAX / 2) || (e[kk].first != INT_MAX / 2)) { if (c[uu].first > b[ww].first) { if (e[kk].first > b[ww].first) d.push_back(b[ww++]); else d.push_back(e[kk++]); } else { if (e[kk].first > c[uu].first) d.push_back(c[uu++]); else d.push_back(e[kk++]); } } s = 0; d.push_back(make_pair(INT_MAX / 2, 0)); while (ree < m) { if (d[s].first <= a[t].first) { ans += d[s].first; aans.push_back(d[s++].second); } else { ans += a[t].first; aans.push_back(a[t++].second); } ree++; } cout << ans << "\n"; for (int i = 0; i < aans.size(); i++) cout << aans[i] << " "; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys # from collections import defaultdict # t=1 # t=int(input()) def fun(x): # print(x) return x[0] n,m,k=list(map(int,sys.stdin.readline().strip().split())) xx=[] a=[] b=[] c=[] for i in range(n): # n=int(input()) x=list(map(int,sys.stdin.readline().strip().split())) # a,b,c,d=list(sys.stdin.readline().strip().split()) # n,k=list(map(int,sys.stdin.readline().strip().split())) # xx.append(x) if(x[1]==x[2]==1): a.append([x[0],i]) elif(x[1]==1): b.append([x[0],i]) elif(x[2]==1): c.append([x[0],i]) # a=k # b=k # # print(xx) # xx.sort(key=fun) # # print(xx) # op=0 # for i in xx: # if() ind=[] b.sort(key=fun) c.sort(key=fun) for i in range(min(len(b),len(c))): a.append([b[i][0]+c[i][0],b[i][1],c[i][1]]) a.sort(key=fun) d=b+c # print(a,b,c,d) # if(len(a)<k): # print(-1) # else: # print(sum(a[:k])) op=0 # print(a,b,c) # print(a[0][1:]) for i in range(len(a)): if(k==0): break if(m>=len(a[i][1:])): m=m-len(a[i][1:]) op+=a[i][0] ind+=a[i][1:] k=k-1 if(k==0): break # print(m) if(m): d.sort(key=fun) for i in d: if(i[1] in ind): continue else: ind.append(i[1]) op+=i[0] m=m-1 if(m==0): break if(m): print(-1) else: print(op) print(*ind)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
def main(): (n, k,) = map(int, input().split(' ')) tab = [list(map(int, input().split(' '))) for i in range(n)] tab.sort(key= lambda x: x[0]) if sum([x[1] for x in tab]) < k or sum([x[2] for x in tab]) < k: return -1 a_both = 0 n_both = 0 for x in tab: if x[1]==1 and x[2]==1: a_both += x[0] n_both +=1 if n_both == k: return a_both n_alice = n_both n_bob = n_both for x in tab: if x[1]==1 and x[2]==1: continue if x[1] == 1 and n_alice < k: a_both += x[0] n_alice += 1 if x[2] == 1 and n_bob < k: a_both += x[0] n_bob+=1 return a_both #for _ in range(int(input())): print(main())
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from sys import stdin input=stdin.readline def answer(): if(n3+n1 < k or n3+n2 < k):return -1 for i in range(1,n1):a[i]+=a[i-1] for i in range(1,n2):b[i]+=b[i-1] start=max(max(0,k-n1),max(0,k-n2)) s=0 for i in range(start):s+=common[i] ans=1e10 for i in range(start,min(k,n3) + 1): ans=min(ans , s + a[k-i] + b[k-i]) s+=common[i] return ans n,k=map(int,input().split()) a,b,common=[0],[0],[] for i in range(n): t,x,y=map(int,input().split()) if(x and y):common.append(t) elif(x==1 and y==0):a.append(t) elif(x==0 and y==1):b.append(t) common.sort() a.sort() b.sort() common.append(0) n1,n2,n3=len(a)-1,len(b)-1,len(common)-1 print(answer())
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define ll long long int #define pb push_back #define mpr make_pair #define mt make_tuple #define rep(i, st, n) for (int i = st; i <= n; i++) #define repv(i, st, n) for (int i = st; i >= n; i--) // Important header files #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less #include <iostream> using namespace __gnu_pbds; using namespace std; // Declaring ordered_set typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // order_of_key (val): returns the no. of values less than val // find_by_order (k): returns the kth largest element.(0-based) #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif const ll INF64 = 1e16 + 10; const ll INF32 = 1e9 + 10; const ll N = 2e5 + 10; const ll MOD = 1e9 + 7; ll power(ll x, ll y, ll p){ ll res = 1; x = x % p;while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p;} return res; } ll inp[N][3], used[N][2]; vector<pair<ll,ll>> a,b,c,arr,merged; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll n,m,k,t,x,y,ans,temp,cum,ind; cin>>n>>m>>k; memset(used, 0, sizeof(used)); memset(inp, 0, sizeof(inp)); rep(i, 1, n){ cin>>inp[i][0]>>inp[i][1]>>inp[i][2]; if(inp[i][1] == 1 && inp[i][2] == 1) a.pb(mpr(inp[i][0], i)); else merged.pb(mpr(inp[i][0], i)); } sort(a.begin(), a.end()); sort(merged.begin(), merged.end()); x = 0; y = 0; rep(i, 0, merged.size() - 1){ if(inp[merged[i].second][1] == 1){ x++; b.pb(merged[i]); } else if(inp[merged[i].second][2] == 1){ y++; c.pb(merged[i]); } used[i+1][0] = x; used[i+1][1] = y; } rep(i, 1, merged.size() - 1) merged[i].first = merged[i-1].first + merged[i].first; rep(i, 1, (int)b.size() - 1) b[i].first = b[i-1].first + b[i].first; rep(i, 1, (int)c.size() - 1) c[i].first = c[i-1].first + c[i].first; ans = INF64; cum = 0; rep(i, 0, min(m, (ll)a.size())){ cum += (i > 0) ? a[i - 1].first : 0; x = max(0ll, k - i); y = (m - i) - 2*x; if(y >= 0 && x < b.size() && x < c.size()){ temp = cum; temp += ((y > 0) ? merged[y - 1].first : 0); if(x > 0){ temp += b[x - 1].first - ((used[y][0] > 0) ? b[used[y][0] - 1].first : 0); temp += c[x - 1].first - ((used[y][1] > 0) ? c[used[y][1] - 1].first : 0); } if(temp <= ans) ind = i; ans = min(ans, temp); } } if(ans < INF64){ x = max(0ll, k - ind); y = (m - ind) - 2*x; cout<<ans<<endl; rep(i, 0, ind - 1) cout<<a[i].second<<" "; rep(i, 0, y - 1) cout<<merged[i].second<<" "; rep(i, used[y][0], x - 1) cout<<b[i].second<<" "; rep(i, used[y][1], x - 1) cout<<c[i].second<<" "; cout<<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
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; long long dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; 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) { long long 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...); } long long solve() { long long n, k; cin >> n >> k; vector<pair<long long, pair<long long, long long>>> v; long long alice = 0, bob = 0, ans = 0; set<long long> both, al, bo; for (long long i = 0; i < n; i++) { long long time, a, b; cin >> time >> a >> b; if (a && !b) al.insert(time); if (!a && b) bo.insert(time); else if (a && b) both.insert(time); v.push_back({time, {a, b}}); if (a == 1) alice++; if (b == 1) bob++; } if (alice < k || bob < k) return -1; while (al.size() && bo.size() && both.size() && k) { long long ali = *al.begin(); long long bobi = *bo.begin(); long long bot = *both.begin(); if (ali + bobi < bot) { al.erase(ali); bo.erase(bobi); ans += ali + bobi; } else { both.erase(bot); ans += bot; } } while (k && al.size() && bo.size()) { k--; long long ali = *al.begin(); long long bobi = *bo.begin(); al.erase(ali); bo.erase(bobi); ans += ali + bobi; } while (k && both.size()) { k--; long long bot = *both.begin(); both.erase(bot); ans += bot; } return ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << 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
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 and 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 LL = long long; using D = double; using namespace std; const int N = 2e5 + 1; int ST[N], ED[N]; LL T[N], sum = 0; set<pair<LL, int>> S; LL n, m, k; void ins(int id, int time) { S.insert({T[id], id}); sum += T[id]; ST[id] = time; } void rem(int id, int time) { S.erase({T[id], id}); if (ED[id] == -1) ED[id] = time; } int main() { ios::sync_with_stdio(false); memset(ST, -1, sizeof(ST)); memset(ED, -1, sizeof(ED)); cin >> n >> m >> k; vector<LL> v[2][2]; for (int i = 0; i < n; ++i) { LL t, a, b; cin >> t >> a >> b; T[i] = t; v[a][b].push_back(i); } if (v[1][1].size() + min(v[0][1].size(), v[1][0].size()) < k) { cout << -1 << '\n'; return 0; } for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) sort(v[i][j].begin(), v[i][j].end(), [&](LL a, LL b) { return T[a] < T[b]; }); LL s = 0, sz = min(v[0][1].size(), v[1][0].size()); LL p = k - min(sz, k); while (p < v[1][1].size() && p + (k - p) * 2 > m) p++; if (p + (k - p) * 2 > m) { cout << -1 << '\n'; return 0; } for (int i = 0; i < k - p; ++i) s += T[v[0][1][i]] + T[v[1][0][i]]; for (int i = 0; i < p; ++i) s += T[v[1][1][i]]; for (int id : v[0][0]) ins(id, p); for (int i = k - p; i < v[0][1].size(); ++i) ins(v[0][1][i], p); for (int i = k - p; i < v[1][0].size(); ++i) ins(v[1][0][i], p); for (int i = p; i < v[1][1].size(); ++i) ins(v[1][1][i], p); while (S.size() > max(0LL, m - (p + (k - p) * 2))) { auto it = S.end(); it--; ED[it->second] = p; sum -= T[it->second]; S.erase(it); } LL res = s + sum, pmn = p; while (p < v[1][1].size() && p < k) { p++; s += T[v[1][1][p - 1]]; rem(v[1][1][p - 1], p); s -= T[v[0][1][k - p]] + T[v[1][0][k - p]]; ins(v[0][1][k - p], p); ins(v[1][0][k - p], p); if (s + sum < res) { pmn = p; res = s + sum; } } cout << res << '\n'; for (int i = 0; i < pmn; ++i) cout << v[1][1][i] + 1 << ' '; for (int i = 0; i < k - pmn; ++i) cout << v[0][1][i] + 1 << ' ' << v[1][0][i] + 1 << ' '; for (int i = 0; i < n; ++i) { if (ST[i] != -1 && ST[i] <= pmn && (ED[i] == -1 || pmn < ED[i])) cout << i + 1 << ' '; } }