Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
# f = open('test.py') # def input(): # return f.readline().replace('\n','') import heapq import bisect # from collections import defaultdict def read_list(): return list(map(int,input().strip().split(' '))) def print_list(l): print(' '.join(map(str,l))) def judge_3(n): tmp = 0 for c in str(n): tmp+=int(c) return tmp%3==0 N,k = read_list() a,b,c = [],[],[] for _ in range(N): t,x,y = read_list() t = -t if x==1 and y==1: if len(c)<k: heapq.heappush(c,t) elif t>c[0]: heapq.heapreplace(c,t) elif x==1: if len(a)<k: bisect.insort_left(a,t) elif t>a[0]: a.pop() bisect.insort_left(a,t) elif y==1: if len(b)<k: bisect.insort_left(b,t) elif t>b[0]: bisect.insort_left(b,t) if len(c)+min(len(a),len(b))<k: print(-1) else: res = sum(c) for _ in range(k-len(c)): res+=a.pop() res+=b.pop() while a and b and c and c[0]<b[0]+a[0]: res-=c.heappop() res+=a.pop() res+=b.pop() print(-res)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int n = s.nextInt(); int k = s.nextInt(); int arr[][] = new int[n][3]; PriorityQueue<Integer> p1 = new PriorityQueue<>(); PriorityQueue<Integer> p2 = new PriorityQueue<>(); int count = 0; for(int i = 0;i<arr.length;++i) { arr[i][0] = s.nextInt(); arr[i][1] = s.nextInt(); arr[i][2] = s.nextInt(); if(arr[i][1] == 1 && arr[i][2] == 0) { p1.add(arr[i][0]); } else if(arr[i][1] == 0 && arr[i][2] == 1) { p2.add(arr[i][0]); } else if(arr[i][1] == 1 && arr[i][2] == 1) { count++; } } long tmp1[] = new long[Math.min(p1.size(), p2.size())]; int tmp2[] = new int[count]; int w = 0; for(int i = 0;i<arr.length;++i) { if(arr[i][1] == 1 && arr[i][2] == 1) { tmp2[w++] = arr[i][0]; } } Arrays.sort(tmp2); int u = 0; while(p1.size()>0 && p2.size()>0) { tmp1[u++]=(long)p1.poll()+p2.poll(); } int i = 0,j = 0; long ans = 0; u=0; System.out.println(tmp1.length + " " + tmp2.length); if(tmp1.length + tmp2.length < k) { System.out.println(-1); return; } while(i<tmp1.length && j<tmp2.length && u<k) { if(tmp1[i] < tmp2[j]) { ans+=(long)tmp1[i]; i++; u++; } else { ans+=(long)tmp2[j]; j++; u++; } } while(i<tmp1.length && u<k) { ans+=(long)tmp1[i]; i++; u++; } while(j<tmp2.length && u<k) { ans+=(long)tmp2[j]; j++; u++; } System.out.println(ans); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); long long int i, j, k, n, x, y, t, p, ans, z, cnt; t = 1; while (t--) { cin >> n >> k; vector<long long int> a, b, c; for (i = 0; i < (n); ++i) { cin >> x >> y >> z; if (y * z == 1) c.push_back(x); else { if (z == 1) b.push_back(x); else if (y == 1) a.push_back(x); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); sort(c.begin(), c.end()); x = 2000; j = 1000000007 * x; a.push_back(j); b.push_back(j); c.push_back(j); long long int q = j; ans = 0, x = 0, y = 0, i = 0, j = 0, p = 0; while ((x < k || y < k) && ans < q) { if (x < k) { if (c[p] < a[i]) { ans += c[p]; p++; x++; y++; } else { ans += a[i]; i++; x++; } } else { if (c[p] < b[j]) { ans += c[p]; p++; x++; y++; } else { ans += b[j]; j++; y++; } } } if (ans >= q) 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
import sys input=sys.stdin.buffer.readline n,k=[int(x) for x in input().split()] book=[] #(ti,ai,bi) for _ in range(n): book.append([int(x) for x in input().split()]) book.sort(key=lambda x:x[0]) #sort by ti asc. t=0 a=0 b=0 aPtr=0 #move aPtr upwards until a==k, only removing books with ai==1 and bi==0 bPtr=0 #move aPtr upwards until b==k, only removing books with bi==1 and ai==0 minT=99999999999999999 for i in range(n): ti,ai,bi=book[i] if ai==1 or bi==1: a+=ai b+=bi t+=ti while aPtr<n and a>k: #remove books with ai==1 and bi==0 and increment aPtr if book[aPtr][1]==1 and book[aPtr][2]==0: a-=1 t-=book[aPtr][0] aPtr+=1 while bPtr<n and b>k: #remove books with bi==1 and ai==0 and increment bPtr if book[bPtr][1]==0 and book[bPtr][2]==1: b-=1 t-=book[bPtr][0] bPtr+=1 if a>=k and b>=k: minT=min(minT,t) if minT==99999999999999999: print(-1) else: print(minT)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const int maxm = 100; const int inf = 2147483647; const double eps = 1e-9; const long long INF = 9223372036854775807ll; long long qpow(long long a, long long b, long long c) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % c; a = a * a % c; b >>= 1; } return ans; } int t[maxn], a[maxn], b[maxn], tot, tot1, atot, btot, lis[maxn], ltot; struct Node { int v, id; } aa[maxn], bb[maxn], sam[maxn], vac[maxn]; bool cmp(Node a, Node b) { return a.v < b.v; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (register int i = (0); i < (n); ++i) { scanf("%d%d%d", &t[i], &a[i], &b[i]); if (a[i] && b[i]) sam[tot++] = {t[i], i + 1}; else if (a[i]) aa[atot++] = {t[i], i + 1}; else if (b[i]) bb[btot++] = {t[i], i + 1}; else vac[tot1++] = {t[i], i + 1}; } long long ans = 0; sort(aa, aa + atot, cmp); sort(bb, bb + btot, cmp); sort(sam, sam + tot, cmp); sort(vac, vac + tot1, cmp); if (atot + tot < k || btot + tot < k || atot + tot + btot + tot1 < m) return printf("-1\n"), 0; int i = 0, j = 0, cnt = 0; while (k--) { if (i == min(atot, btot) || j < tot && aa[i].v + bb[i].v > sam[j].v) { ans += (long long)sam[j++].v; cnt++; } else { ans += (long long)aa[i].v + (long long)bb[i].v; i++; cnt += 2; } } int i1 = i, i2 = i; bool failed = false; if (cnt > m) { cnt -= m; i--; k = 0; while (cnt--) { if (i == -1 || j == tot) { failed = true; break; } ans += (long long)sam[j].v - (long long)aa[i].v - (long long)bb[i].v; i--, j++; } i++; i1 = i2 = i; } else if (cnt < m) { cnt = m - cnt; j--; k = 0; i1 = i, i2 = i; while (cnt--) { if (k == tot1 && i < min(atot, btot) || i < min(atot, btot) && aa[i].v + bb[i].v - sam[j].v < vac[k].v) { ans += (long long)aa[i].v + (long long)bb[i].v - (long long)sam[j].v; i++; j--; i1++, i2++; } else if (i < min(atot, btot) && k < tot1 && aa[i].v + bb[i].v - sam[j].v >= vac[k].v) { ans += (long long)vac[k].v; k++; } else if (i1 < atot && (i2 == btot || aa[i1].v <= bb[i2].v) && (k == tot1 || aa[i1].v <= vac[k].v)) { ans += (long long)aa[i1++].v; } else if (i2 < btot && (i1 == atot || bb[i2].v <= aa[i1].v) && (k == tot1 || bb[i2].v <= vac[k].v)) { ans += (long long)bb[i2++].v; } else ans += (long long)vac[k++].v; } j++; } if (failed) printf("-1\n"); else { printf("%lld\n", ans); for (register int c = (0); c < (i1); ++c) printf("%d ", aa[c].id); for (register int c = (0); c < (i2); ++c) printf("%d ", bb[c].id); for (register int c = (0); c < (j); ++c) printf("%d ", sam[c].id); for (register int c = (0); c < (k); ++c) printf("%d ", vac[c].id); printf("\n"); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from collections import Counter, defaultdict BS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" def to_base(s, b): res = "" while s: res+=BS[s%b] s//= b return res[::-1] or "0" alpha = "abcdefghijklmnopqrstuvwxyz" from math import floor, ceil,pi primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919 ] def primef(n, plst = []): if n==1: return plst else: for m in primes: if n%m==0: return primef(n//m, plst+[m]) return primef(1, plst+[n]) def lmii(): return list(map(int, input().split())) def ii(): return int(input()) def countOverlapping(string,sub): count = start = 0 while True: start = string.find(sub, start)+1 if start > 0: count += 1 else: return count """ t = ii() for i in range(t): x,y,n = lmii() ns = int(n) a = n%x if a==y: print(n) else: n //= x n *= x n += y if n > ns: n -= x if n < 0: n = 0 print(n)""" """ t = ii() for i in range(t): n = int(input()) if n==1: print(0) else: pr = primef(n) c = Counter(pr) if c[2] > c[3]: print(-1) elif c[3]==0 or len(c) > 2: print(-1) else: if c[3] > 0 and (c[2] > 0 or len(c)==1): #print(c) print((c[3]+c[3]-c[2])) else: print(-1)""" """ t = ii() for i in range(t): n = int(input()) s = list(input()) while "()" in "".join(s): s = list("".join(s).replace("()", "", 1)) seen = 0 pos = 0 c = 0 while pos < len(s): seen += 1 if s[pos]=="(" else 0 #print(seen, pos, s[pos]) if 2*seen == len(s): break if s[pos]==")" and seen*2 < len(s): s.append(s.pop(pos)) c += 1 else: pos += 1 print(c)""" import heapq as hp n,k = lmii() firstKA = int(k) firstKB = int(k) nums = [lmii() for i in range(n)] nums.sort(key = lambda x: (x[0], abs(x[1]-x[2]))) chosen = [] for i in nums: #print(i, firstKA, firstKB) if firstKB==firstKA==0: break if i[1]+i[2]==2 and (firstKA > 0 or firstKB > 0): firstKA -= 1 if firstKA > 0 else 0 firstKB -= 1 if firstKB > 0 else 0 chosen.append(i) elif i[1]==1 and firstKA > 0: chosen.append(i) firstKA -= 1 elif i[2]==1 and firstKB > 0: chosen.append(i) firstKB -= 1 #print(i, firstKA, firstKB) #print(chosen) if firstKA > 0 or firstKB > 0: print(-1) else: tot = 0 fa = int(k) fb = int(k) chosen.sort(key = lambda x: (99999999-x[0], abs(x[1]-x[2]))) for f in range(len(chosen)): s = chosen[f] if s[1]+s[2]==2: fa -= 1 if fa > 0 else 0 fb -= 1 if fb > 0 else 0 tot += s[0] elif s[1]==1 and fa > 0: fa -= 1 if fa > 0 else 0 tot += s[0] elif s[2]==1 and fb > 0: tot += s[0] fb -= 1 if fb > 0 else 0 if fa==fb==0: print(tot) break
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class E { Random random = new Random(751454315315L + System.currentTimeMillis()); public void solve() throws IOException { int n = nextInt(), k = nextInt(); List<Integer> both = new ArrayList<>(); List<Integer> alice = new ArrayList<>(); List<Integer> bob = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = nextInt(), a = nextInt(), b = nextInt(); if (a + b == 2) { both.add(t); } else if (a == 1) { alice.add(t); } else { bob.add(t); } } if (both.size() + alice.size() < k || both.size() + bob.size() < k) { out.println(-1); return; } Collections.sort(bob); Collections.sort(both); Collections.sort(alice); int i = 0, j = 0, read = 0; long ans = 0; while (read < k) { if (i < bob.size() && i < alice.size() && j < both.size() && bob.get(i) + alice.get(i) < both.get(j) || j >= both.size()) { ans += bob.get(i) + alice.get(i); i++; } else { ans += both.get(j); j++; } read++; } out.println(ans); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } private void shuffle(int[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); int t = s[i]; s[i] = s[j]; s[j] = t; } } static class Pair<T1 extends Comparable<T1>, T2 extends Comparable<T2>> implements Comparable<Pair<T1, T2>> { T1 f; T2 s; public Pair(T1 f, T2 s) { this.f = f; this.s = s; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<T1, T2> pair = (Pair<T1, T2>) o; return f == pair.f && s == pair.s; } @Override public int hashCode() { return Objects.hash(f, s); } @Override public int compareTo(Pair<T1, T2> o) { if (f.compareTo(o.f) == 0) { return s.compareTo(o.s); } return f.compareTo(o.f); } } BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public int[] nextArr(int n) throws IOException { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public static void main(String[] args) throws IOException { Locale.setDefault(Locale.US); new E().run(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static class Pair { int t; int love; Pair (int t, int love) { this.t = t; this.love = love; } } public static void main(String[] args) { FastReader scan = new FastReader(); int n = scan.nextInt(); int k = scan.nextInt(); int cntAlice = 0; int cntBob = 0; Pair[] arr = new Pair[n]; for(int i=0;i<n;i++) { int t = scan.nextInt(); int a = scan.nextInt(); int b = 2*scan.nextInt(); arr[i] = new Pair(t, a+b); if(a != 0) { cntAlice++; } if(b != 0) { cntBob++; } } if(cntAlice < k || cntBob < k) { System.out.println(-1); return; } Arrays.sort(arr,((o1, o2) -> { if(o1.t == o2.t) { return o2.love - o1.love; } return o1.t-o2.t; })); /* for(Pair p : arr) { System.out.println(p.t+" "+(p.love > 2 ? "1 1" : (p.love > 1 ? "0 1" : (p.love > 0 ? "1 0" :"0 0")))); } */ Stack<Integer> alice = new Stack<>(); Stack<Integer> bob = new Stack<>(); long ans = 0; int cnt = 0; for(Pair p : arr) { if(p.love == 0) { continue; } if(p.love == 3) { //alice & bob if(!alice.isEmpty() && cnt+alice.size() >= k) { ans -= alice.pop(); } if(!bob.isEmpty() && cnt+bob.size() >= k) { ans -= bob.pop(); } cnt++; ans += p.t; } else if(p.love == 1) { //alice only if(alice.size()+cnt < k) { ans += p.t; alice.add(p.t); } } else if(p.love == 2) { //bob only if(bob.size()+cnt < k) { ans += p.t; bob.add(p.t); } } //System.out.println(ans); if(alice.size()+cnt >= k && bob.size()+cnt >= k) { break; } } System.out.println(ans); } //fast input reader static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) t=[] a=[] b=[] for _ in range(n): t1,a1,b1=map(int,input().split()) if a1==1 and b1==1: t.append(t1) elif a1==1 and b1==0: a.append(t1) else: b.append(t1) a.sort() b.sort() k1=min(len(a),len(b)) for i in range(k1): t.append(a[i]+b[i]) if len(t)<k: print("-1") elif k==1: print(t[0]) else: print(sum(t[:k]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
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; if(n==200000 &&k==70874) { out.println("FUCK"); out.println(res); } 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; } for(int i=0;i<Math.min(first.size(),k);i++) { res+=first.get(i); } int l=0,r=0; for(int i=k-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); 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
import sys tokens = input().split() n, nb_required = int(tokens[0]), int(tokens[1]) both_liked = [] alice_liked = [] bob_liked = [] for i in range(n): tokens = input().split() t, a, b = int(tokens[0]), int(tokens[1]), int(tokens[2]) if a == 1 and b == 1: both_liked.append(t) elif a == 1: alice_liked.append(t) else: bob_liked.append(t) both_liked.sort() alice_liked.sort() bob_liked.sort() i = 0 j = 0 k = 0 total = 0 while nb_required > 0: if k < len(both_liked): if len(alice_liked) == 0 or len(bob_liked) == 0 or both_liked[k] <= alice_liked[i] + bob_liked[j]: nb_required -= 1 total += both_liked[k] k += 1 continue if i == len(alice_liked) or j == len(bob_liked): print(-1) sys.exit(0) nb_required -= 1 total += alice_liked[i] + bob_liked[j] i += 1 j += 1 print(total)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.awt.*; import java.io.*; import java.util.*; import java.util.List; import static java.lang.Math.*; public class R653_Solution_5 implements Runnable { private boolean console=false; private long MOD = 1000_000_007L; private int MAX = 1000_001; private void solve1(){ int n=in.ni(),k=in.ni(); TreeSet<Long> one = new TreeSet<>(); TreeSet<Long> two = new TreeSet<>(); TreeSet<Long> all = new TreeSet<>(); for(int i=0;i<n;++i){ long num = in.nl(); long a=in.ni(),b=in.ni(); if(a==1 && b==1){ all.add(num); }else if(a==1){ one.add(num); }else { two.add(num); } } if(one.size()+all.size()<k || two.size()+all.size()<k){ out.printLn(-1); return; } one.add((long)Integer.MAX_VALUE); two.add((long)Integer.MAX_VALUE); all.add((long)Integer.MAX_VALUE); int sz1=0,sz2=0,ans=0; while (sz1<k && sz2<k){ long num1 = one.first()+two.first(); long num2 = all.first(); if(num1<num2){ ans += num1; one.pollFirst(); two.pollFirst(); }else { ans += num2; all.pollFirst(); } sz1++; sz2++; } out.printLn(ans); } private void solve() { int testCases = 1; // testCases = in.ni(); while (testCases-->0){ solve1(); } } private void add(TreeMap<Integer, Integer> map, int key){ map.put(key,map.getOrDefault(key,0)+1); } private void remove(TreeMap<Integer,Integer> map,int key){ if(!map.containsKey(key)) return; map.put(key,map.getOrDefault(key,0)-1); if(map.get(key)==0) map.remove(key); } @Override public void run() { long time = System.currentTimeMillis(); try { init(); } catch (FileNotFoundException e) { e.printStackTrace(); } try { solve(); out.flush(); System.err.println(System.currentTimeMillis()-time); System.exit(0); }catch (Exception e){ e.printStackTrace(); System.exit(1); } } /* -------------------- Templates and Input Classes -------------------------------*/ private FastInput in; private FastOutput out; public static void main(String[] args) throws Exception { new R653_Solution_5().run(); // new Thread(null, new R653_Solution_5(), "Main", 1 << 27).start(); } private void init() throws FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; try { if (!console && System.getProperty("user.name").equals("puneet")) { outputStream = new FileOutputStream("/home/puneet/Desktop/output.txt"); inputStream = new FileInputStream("/home/puneet/Desktop/input.txt"); } } catch (Exception ignored) { } out = new FastOutput(outputStream); in = new FastInput(inputStream); } private void maualAssert(int a,int b,int c){ if(a<b || a>c) throw new RuntimeException(); } private void maualAssert(long a,long b,long c){ if(a<b || a>c) throw new RuntimeException(); } private void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int object : arr) list.add(object); Collections.sort(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private long ModPow(long x, long y, long MOD) { long res = 1L; x = x % MOD; while (y >= 1L) { if ((y & 1L) > 0) res = (res * x) % MOD; x = (x * x) % MOD; y >>= 1L; } return res; } private int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } private long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } private int[] arrInt(int n){ int[] arr=new int[n];for(int i=0;i<n;++i)arr[i]=in.ni(); return arr; } private long[] arrLong(int n){ long[] arr=new long[n];for(int i=0;i<n;++i)arr[i]=in.nl(); return arr; } private int arrMax(int[] arr){ int ans = arr[0]; for(int i=1;i<arr.length;++i){ ans = max(ans,arr[i]); } return ans; } private long arrMax(long[] arr){ long ans = arr[0]; for(int i=1;i<arr.length;++i){ ans = max(ans,arr[i]); } return ans; } private int arrMin(int[] arr){ int ans = arr[0]; for(int i=1;i<arr.length;++i){ ans = max(ans,arr[i]); } return ans; } private long arrMin(long[] arr){ long ans = arr[0]; for(int i=1;i<arr.length;++i){ ans = max(ans,arr[i]); } return ans; } class FastInput { InputStream obj; public FastInput(InputStream obj) { this.obj = obj; } private byte inbuffer[] = new byte[1024]; private int lenbuffer = 0, ptrbuffer = 0; private int readByte() { if (lenbuffer == -1) throw new InputMismatchException(); if (ptrbuffer >= lenbuffer) { ptrbuffer = 0; try { lenbuffer = obj.read(inbuffer); } catch (IOException e) { throw new InputMismatchException(); } } if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; } String ns() { int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ') { sb.appendCodePoint(b);b = readByte(); }return sb.toString();} int ni() { 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 nl() { 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 * 10L + (b - '0'); } else { return minus ? -num : num; }b = readByte(); } } private boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); } int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; } float nf() {return Float.parseFloat(ns());} double nd() {return Double.parseDouble(ns());} char nc() {return (char) skip();} } class FastOutput{ private final PrintWriter writer; public FastOutput(OutputStream outputStream) { writer = new PrintWriter(outputStream); } public PrintWriter getWriter(){ return writer; } public void print(Object obj){ writer.print(obj); } public void printLn(){ writer.println(); } public void printLn(Object obj){ writer.print(obj); printLn(); } public void printSp(Object obj){ writer.print(obj+" "); } public void printArr(int[] arr){ for(int i:arr) printSp(i); printLn(); } public void printArr(long[] arr){ for(long i:arr) printSp(i); printLn(); } public void flush(){ writer.flush(); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) l1=[] l2=[] l3=[] for i in range(n): t,a,b=map(int,input().split()) if(a==1 and b==1): l1.append(t) elif(a==1 and b==0): l2.append(t) elif(a==0 and b==1): l3.append(t) f=0 if(len(l1)+len(l2)<k): f=1 if(len(l1)+len(l3)<k): f=1 if(f==1): print(-1) else: f=0 l1.sort() l2.sort() l3.sort() ans=0 t1,t2=0,0 #print(l1) #print(l2) #print(l3) for i in range(min(len(l1),len(l2),len(l3))): if(l1[i]>l2[i]+l3[i]): ans+=l2[i]+l3[i] t1+=1 t2+=1 l2[i]=-1 l3[i]=-1 for i in range(len(l1)): ans+=l1[i] t1+=1 t2+=1 l1[i]=-1 if(t1>=k and t2>=k): break #print(t1,t2,ans) for i in range(len(l2)): if(t1<k): if(l2[i]!=-1): ans+=l2[i] t1+=1 if(t1>=k): break if(t1<k): f=1 #print(ans) for i in range(len(l3)): if(t2<k): if(l3[i]!=-1): ans+=l3[i] t2+=1 if(t2>=k): break if(t2<k): f=1 if(f==1): print(-1) else: print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long INF = 1000000000000000000LL; vector<pair<long long, int> > mejores(vector<pair<long long, int> >& fi, vector<pair<long long, int> >& se, vector<pair<long long, int> >& te, int cantidad, int pf, int ps, int pt) { vector<pair<long long, int> > result; while (result.size() < cantidad) { long long vf = INF; if (pf < fi.size()) vf = fi[pf].first; long long vs = INF; if (ps < se.size()) vs = se[ps].first; long long vt = INF; if (pt < te.size()) vt = te[pt].first; if (vf <= vs && vf <= vt) { result.push_back(fi[pf]); pf++; } else if (vs <= vf && vs <= vt) { result.push_back(se[ps]); ps++; } else { result.push_back(te[pt]); pt++; } } return result; } int main() { int n, m, k; cin >> n >> m >> k; vector<pair<long long, int> > uno; vector<pair<long long, int> > dos; vector<pair<long long, int> > juntos; vector<pair<long long, int> > solos; set<long long> q; int b, c; long long a; for (int i = 0; i < n; i++) { cin >> a >> b >> c; if (b == 1 && c == 1) { juntos.push_back(make_pair(a, i + 1)); } else if (b == 1 && c == 0) { uno.push_back(make_pair(a, i + 1)); } else if (b == 0 && c == 1) { dos.push_back(make_pair(a, i + 1)); } else { solos.push_back(make_pair(a, i + 1)); } } sort(uno.begin(), uno.end()); sort(dos.begin(), dos.end()); sort(juntos.begin(), juntos.end()); sort(solos.begin(), solos.end()); if (juntos.size() + uno.size() < k || juntos.size() + dos.size() < k) { cout << -1 << '\n'; } else { vector<long long> usum; vector<long long> dsum; vector<long long> jsum; jsum.push_back(0); usum.push_back(0); dsum.push_back(0); for (int i = 0; i < uno.size(); i++) { usum.push_back(usum[i] + uno[i].first); } for (int i = 0; i < dos.size(); i++) { dsum.push_back(dsum[i] + dos[i].first); } for (int i = 0; i < juntos.size(); i++) { jsum.push_back(jsum[i] + juntos[i].first); } long long result = INF; int index; long long suma = 0; for (int i = 0; i <= juntos.size(); i++) { int j = i; int u = max(k - j, 0); int d = max(k - j, 0); int falta = m - j - u - d; if (q.size() != 0) { suma += uno[u].first; suma += dos[d].first; q.insert(uno[u].first); q.insert(dos[d].first); auto it = q.rbegin(); suma -= *(it); q.erase(*(it)); } if (u > uno.size() || d > dos.size() || j + u + d > m || falta > solos.size() + uno.size() - u + dos.size() - d) continue; if (q.size() == 0) { int pu = u; int pd = d; int pv = 0; vector<pair<long long, int> > bestis = mejores(uno, dos, solos, falta, u, d, 0); for (pair<long long, int> it : bestis) { suma += it.first; q.insert(it.first); } } long long current = jsum[j] + usum[u] + dsum[d] + suma; if (current < result) { result = current; index = i; } } if (result >= INF) { cout << -1 << '\n'; } else { cout << result << '\n'; vector<long long> ans; for (int i = 0; i < index; i++) { ans.push_back(juntos[i].second); } int fill = max(k - index, 0); for (int i = 0; i < fill; i++) { ans.push_back(uno[i].second); } for (int i = 0; i < fill; i++) { ans.push_back(dos[i].second); } int falta = m - index - fill - fill; vector<pair<long long, int> > bestis = mejores(uno, dos, solos, falta, fill, fill, 0); for (pair<long long, int> it : bestis) { ans.push_back(it.second); } for (long long it : ans) { cout << it << " "; } 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
java
import java.io.*; import java.util.*; public class prop { static Scanner sc; static int n, inf = Integer.MAX_VALUE, mod = (int) 1e9 + 7; public static void main(String[] args) throws IOException { sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> c = new ArrayList<>(); for (int i = 0; i < n; i++) { int q = sc.nextInt(); int w = sc.nextInt(); int e = sc.nextInt(); if (w == 1 && e == 1) c.add(q); if (w == 0 && e == 1) b.add(q); if (w == 1 && e == 0) a.add(q); } Collections.sort(a); Collections.sort(b); Collections.sort(c); int i = 0, j = 0, w = 0; int k1 = 0; int k2 = 0; int g = 0; long tt = 0; while (g < n) { g++; int x = inf; int y = inf; int z = inf; if (i < a.size()) x = a.get(i); if (j < b.size()) y = b.get(j); if (w < c.size()) z = c.get(w); if ((z <= x + y && z != inf && x != inf && y != inf && !(x != inf && k1 < k && x < z && k2 >= k) && !(y != inf && k2 < k && x < z && k1 >= k)) || ((x == inf && k1 < k && z != inf)) || ((y == inf && k2 < k && z != inf))) { k1++; k2++; tt += z; w++; } else { if (x != inf && k1 < k) { k1++; i++; tt += x; } if (y != inf && k2 < k) { k2++; j++; tt += y; } } if(k1<k && k2<k) break; } pw.print(k1 < k || k2 < k ? -1 : tt); pw.flush(); } ///////////////////////////////////////////////////////////////////////////////////////////////////// static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int log(int n, int base) { int ans = 0; while (n + 1 > base) { ans++; n /= base; } return ans; } static int pow(int b, long e) { int ans = 1; while (e > 0) { if ((e & 1) == 1) ans = (int) ((ans * 1l * b)); e >>= 1; { } b = (int) ((b * 1l * b)); } return ans; } static int powmod(int b, long e, int mod) { int ans = 1; b %= mod; while (e > 0) { if ((e & 1) == 1) ans = (int) ((ans * 1l * b) % mod); e >>= 1; b = (int) ((b * 1l * b) % mod); } return ans; } static class pair implements Comparable<pair> { int x, y; pair(int s, int d) { x = s; y = d; } @Override public int compareTo(pair p) { return (int) (x - p.x); } @Override public String toString() { return x + " " + y; } } static int ceil(int a, int b) { int ans = a / b; return a % b == 0 ? ans : ans + 1; } static long ceil(long a, long b) { long ans = a / b; return a % b == 0 ? ans : ans + 1; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public int[] intArr(int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = nextInt(); } return a; } public long[] longArr(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < a.length; i++) { a[i] = nextLong(); } return a; } 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(); } } public static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = 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
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; } 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(); f3=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
cpp
#include <bits/stdc++.h> using namespace std; const char nl = '\n'; const int MAX_N = 100011; const long long INF = (1 << 29) + 123; const long long MOD = 1000000007; const long double PI = 4 * atan((long double)1); template <typename T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template <typename T> bool ckmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MX = 1 << 20; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<pair<int, int> > both, left, right; int t, a, b; set<pair<int, int> > rest; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back({t, i}); else if (a) left.push_back({t, i}); else if (b) right.push_back({t, i}); rest.insert({t, i}); } set<pair<int, int> > rCopy = rest; sort(left.begin(), left.end()); sort(right.begin(), right.end()); sort(both.begin(), both.end()); int sm = min((int)left.size(), (int)right.size()); if ((int)both.size() + sm < k) { cout << -1 << nl; return 0; } long long bothSum = 0; for (int i = 0; i < (int)both.size(); i++) { bothSum += both[i].first; rest.erase(rest.find(both[i])); } long long ans = INF; long long curAns = 0; long long rSum = 0; set<pair<int, int> > r; while ((int)r.size() < m - k) { if ((int)rest.size() == 0) break; r.insert(*rest.begin()); rSum += rest.begin()->first; rest.erase(rest.begin()); } int bestIdx = -1; if (k <= (int)both.size() && (int)r.size() == m - k) if (ckmin(ans, bothSum + curAns + rSum)) bestIdx = k; for (int i = (k)-1; i >= 0; i--) { if (i < (int)both.size()) { bothSum -= both[i].first; rest.insert(both[i]); } if (k - i - 1 >= sm || k - i - 1 < 0) continue; curAns += left[k - i - 1].first + right[k - i - 1].first; if (rest.find(left[k - i - 1]) != rest.end()) rest.erase(rest.find(left[k - i - 1])); if (rest.find(right[k - i - 1]) != rest.end()) rest.erase(rest.find(right[k - i - 1])); if (r.find(left[k - i - 1]) != r.end()) { r.erase(r.find(left[k - i - 1])); rSum -= left[k - i - 1].first; } if (r.find(right[k - i - 1]) != r.end()) { r.erase(r.find(right[k - i - 1])); rSum -= right[k - i - 1].first; } while ((int)r.size() < m - 2 * (k - i) - i) { if ((int)rest.size() == 0) break; r.insert(*rest.begin()); rSum += rest.begin()->first; rest.erase(rest.begin()); } while ((int)r.size() > m - 2 * (k - i) - i) { if ((int)r.size() == 0) break; rest.insert(*r.rbegin()); rSum -= r.rbegin()->first; r.erase(--r.end()); } if (i + 2 * (k - i) + (int)r.size() == m) { if (ckmin(ans, bothSum + curAns + rSum)) bestIdx = i; } } if (bestIdx == -1) cout << -1 << nl; else { cout << ans << nl; for (int i = 0; i < bestIdx; i++) { cout << both[i].second + 1 << " "; rCopy.erase(rCopy.find(both[i])); } for (int i = 0; i < k - bestIdx; i++) { cout << left[i].second + 1 << " " << right[i].second + 1 << " "; rCopy.erase(rCopy.find(left[i])); rCopy.erase(rCopy.find(right[i])); } int count = 0; assert(bestIdx + 2 * (k - bestIdx) <= m); for (auto& a : rCopy) { if (count + bestIdx + 2 * (k - bestIdx) == m) break; count++; cout << a.second + 1 << " "; } cout << nl; assert(count + bestIdx + 2 * (k - bestIdx) == m); } 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.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(); int[] all = new int[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 all[01] - all[02]; } }; a.sort(comparator); b.sort(comparator); both.sort(comparator); int i = 0; int j = 0; int time = 0; while (i + j < k && (i < a.size() && i < b.size() || j < both.size())) { if (i < a.size() && i < b.size()) { int 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
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, c, b, 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.c == 1) c1--; if (t.b == 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 ? pair.c - c : pair.b - c : 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 : pair.c - c : pair.b - 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
python2
import collections def f(s,k): both = [] alice = [] bob = [] for ti,ai,bi in s: if ai == 1 and bi == 1: both.append(ti) elif ai == 1 and bi == 0: alice.append(ti) elif ai == 0 and bi == 1: bob.append(ti) alice.sort() bob.sort() both.sort() for i in range(1,len(both)): both[i] += both[i-1] for i in range(1,len(alice)): alice[i] += alice[i-1] for i in range(1,len(bob)): bob[i] += bob[i-1] res = +float('inf') for i in range(0,len(both)): #im taking i+1 books from both cost = both[i] #I need k - (i+1) from individual j = (k - (i + 1)) -1 if j >= 0: if j < len(alice): cost += alice[j] else: cost += float('inf') if j >= 0: if j < len(bob): cost += bob[j] else: cost += float('inf') res = min(res, cost) return res if res < float('inf') else -1 for t in range(1): n,k = map(int, raw_input().split(' ')) print f([map(int, raw_input().split()) for _ in range(n)],k)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
//package codeforces; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class E1374 { private static ArrayList<Integer> a = new ArrayList<>(); private static ArrayList<Integer> b = new ArrayList<>(); private static ArrayList<Integer> both = new ArrayList<>(); public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] s = in.readLine().split(" "); int n = Integer.parseInt(s[0]); int k = Integer.parseInt(s[1]); for (int i = 0; i < n; i++) { s = in.readLine().split(" "); int value = Integer.parseInt(s[0]); int first = Integer.parseInt(s[1]); int second = Integer.parseInt(s[2]); if (first == 1 && second == 1) both.add(value); else if (first == 1) a.add(value); else if (second == 1) b.add(value); } Collections.sort(a); Collections.sort(b); Collections.sort(both); System.out.println(solve(k)); } private static long solve(int k ) { long time = 0, count = 0; int aIndex = 0, bIndex = 0, bothIndex = 0; while (true) { if (aIndex < a.size() && bIndex < b.size() && bothIndex < both.size()) { if (a.get(aIndex) + b.get(bIndex) <= both.get(bothIndex)) { time += a.get(aIndex) + b.get(bIndex); aIndex++; } else { time += both.get(bothIndex); bothIndex++; } } else if (aIndex < a.size() && bIndex < b.size()) { time += a.get(aIndex) + b.get(bIndex); aIndex++; } else if (bothIndex < both.size()) { time += both.get(bothIndex); bothIndex++; } else if (count < k) { return -1; } count++; if (count == k) { // return time <= value; return time; } } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; while (cin >> n >> k) { vector<int> both, alice, bob; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else bob.push_back(t); } sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); vector<int> asum(alice.size() + 1); for (int i = 0; i < alice.size(); ++i) { asum[1 + i] = asum[i] + alice[i]; } vector<int> bsum(bob.size() + 1); for (int i = 0; i < bob.size(); ++i) { bsum[1 + i] = bsum[i] + bob[i]; } const int INF = 2e9 + 10; int res = INF; for (int i = 0, csum = 0; i <= min<int>(k, both.size()); ++i) { if (alice.size() + i >= k && bob.size() + i >= k) { int cur = asum[k - i] + bsum[k - i] + csum; res = min(res, cur); } if (i < both.size()) { csum += both[i]; } } cout << (res == INF ? -1 : res) << "\n"; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; //import java.util.HashMap; //import java.io.BufferedReader; //import java.io.IOException; //import java.io.InputStreamReader; //import java.io.PrintWriter; //import java.math.BigInteger; //import java.util.ArrayList; //import java.util.Arrays; //import java.util.HashMap; //import java.util.StringTokenizer; public class Equal { static ArrayList<Integer>[] tree; static boolean[] vis; static int dp[][]; // static boolean dfs(int u, int dest) { // if (dp[u][dest] != 0) { // if (dp[u][dest] == 1) // return true; // return false; // } // vis[u] = true; // if (u == dest) { // vis[u] = false; // dp[u][dest] = 1; // return true; // } // for (int i = 0; i < tree[u].size(); i++) { // int v = tree[u].get(i); // if (vis[v]) // continue; // if (dfs(v, dest)) { // vis[u] = false; // dp[u][dest] = 1; // dp[dest][u] = 1; // // return true; // } // // } // vis[u] = false; // dp[u][dest] = -1; // dp[dest][u] = -1; // return false; // } static class pair implements Comparable<pair> { int x; int t; public pair(int xx, int tt) { x = xx; t = tt; } @Override public int compareTo(pair o) { return t - o.t; } } static boolean dif1(String x, String y) { int dif = 0; if (x.length() != y.length()) return false; for (int i = 0; i < x.length(); i++) { if (x.charAt(i) != y.charAt(i)) dif++; } return dif <= 1; } static int[] p; static int[] rank; static int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); } static boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } static void unionSet(int i, int j) { if (!isSameSet(i, j)) { int x = findSet(i); int y = findSet(j); if (rank[x] > rank[y]) { p[y] = x; } else { p[x] = y; if (rank[x] == rank[y]) rank[y]++; } } } public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("name.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; PrintWriter out = new PrintWriter(System.out); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); PriorityQueue<pair> notL = new PriorityQueue<>(); PriorityQueue<pair> AB = new PriorityQueue<>(); PriorityQueue<pair> B = new PriorityQueue<>(); PriorityQueue<pair> A = new PriorityQueue<>(); for (int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); pair t = new pair(i, Integer.parseInt(st.nextToken())); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); if (a == 1 && b == 1) AB.add(t); if (a == 0 && b == 0) notL.add(t); if (a == 0 && b == 1) B.add(t); if (a == 1 && b == 0) A.add(t); } long t = 0; ArrayList<pair> M = new ArrayList<>(); while (k > 0 && m > 0) { if (A.isEmpty() && B.isEmpty() && AB.isEmpty()) break; if (A.isEmpty() || B.isEmpty()) { if (AB.isEmpty()) break; t += AB.peek().t; M.add(AB.poll()); k--; m--; } else { if (AB.isEmpty()) { if (m >= 2) { t += A.peek().t + B.peek().t; k--; m -= 2; M.add(A.poll()); M.add(B.poll()); } else break; } else { if (A.peek().t + B.peek().t < AB.peek().t && m >= 2) { t += A.peek().t; t += B.peek().t; k--; m -= 2; M.add(A.poll()); M.add(B.poll()); } else { t += AB.peek().t; k--; m--; M.add(AB.poll()); } } } } if (k != 0) out.println(-1); else { while (!A.isEmpty()) notL.add(A.poll()); while (!B.isEmpty()) notL.add(B.poll()); while (!AB.isEmpty()) notL.add(AB.poll()); while (m > 0) { if (notL.isEmpty()) break; t += notL.peek().t; M.add(notL.poll()); m--; } if (m != 0) out.println(-1); else { out.println(t); for (int i = 0; i <M.size() ; i++) { out.print(M.get(i).x+1); if(i<M.size()-1) out.print(" "); } } } out.flush(); out.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.StringTokenizer; public final class E { public static void main(String[] args) { final FastScanner fs = new FastScanner(); final int n = fs.nextInt(); final int k = fs.nextInt(); List<int[]> books = new ArrayList<>(n); for (int i = 0; i < n; i++) { final int[] book = { fs.nextInt(), fs.nextInt(), fs.nextInt() }; if (book[1] == 0 && book[2] == 0) { continue; } books.add(book); } books.sort((a, b) -> a[1] == b[1] ? Integer.compare(a[0], b[0]) : Integer.compare(b[1], a[1])); long res = (long) 1e18; long currSum = 0; int j = 0; int windowA = k; int windowB = k; for (int i = 0; i < books.size(); i++) { final int take = Math.max(books.get(i)[1], books.get(i)[2]); windowA -= books.get(i)[1]; windowB -= books.get(i)[2]; if (take > 0) { currSum += books.get(i)[0]; } while (Math.max(windowA, windowB) < 0) { final int remove = Math.max(books.get(j)[1], books.get(j)[2]); windowA += books.get(j)[1]; windowB += books.get(j)[2]; if (remove > 0) { currSum -= books.get(j)[0]; } j++; } if (Math.max(windowA, windowB) == 0) { res = Math.min(res, currSum); } } System.out.println(res == (long) 1e18 ? -1 : res); } static final class Utils { public static void shuffleSort(int[] x) { shuffle(x); Arrays.sort(x); } public static void shuffleSort(long[] x) { shuffle(x); Arrays.sort(x); } public static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } public static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } public static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } public static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } private Utils() {} } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); private String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { //noinspection CallToPrintStackTrace e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] nextIntArray(int n) { final int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(int n) { final long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n, k = map(int, input().split()) books = [] for _ in range(n): book = tuple(map(int, input().split())) if book[1] == book[2] == 0: continue; books.append(book) books.sort(key=lambda b:b[0]) books.sort(key=lambda b:b[1]+b[2], reverse=True) alice_cnt = bob_cnt = k ans = 0 for book in books: if alice_cnt + bob_cnt == 0: break if book[1] + book[2] == 2: alice_cnt -= 1 bob_cnt -= 1 ans += book[0] elif book[1] == 1: alice_cnt -= 1 ans += book[0] else: bob_cnt -= 1 ans += book[0] print(ans if alice_cnt + bob_cnt == 0 else -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(NULL); cout.tie(NULL); long long int n, k; cin >> n >> k; pair<long long int, pair<long long int, long long int> > p[n]; for (long long int i = 0; i < n; i++) { long long int t, a, b; cin >> t >> a >> b; p[i] = {t, {a, b}}; } sort(p, p + n); long long int ans = 0; for (long long int i = 0; i < n; i++) { if (p[i].second.first == 1 && p[i].second.second == 1) { ans += p[i].first; k--; } if (k == 0) break; } long long int flaga = 0; for (long long int i = 0; i < n; i++) { if (flaga == k) break; if (p[i].second.first == 1 && p[i].second.second == 0) { ans += p[i].first; flaga++; } } long long int flagb = 0; for (long long int i = 0; i < n; i++) { if (flagb == k) break; if (p[i].second.first == 0 && p[i].second.second == 1) { ans += p[i].first; flagb++; } } if (flaga == k && flagb == k) cout << ans << "\n"; else cout << -1 << "\n"; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void Output(const vector<T>& v) { for (int i = 0; i < v.size(); ++i) { cout << v[i]; if (i < v.size() - 1) cout << " "; else cout << endl; } } template <class T> void Input(vector<T>& v, int n) { v.resize(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } } template <class T> vector<int> Query(const T& c, int answer_counts) { cout << "? " << c.size(); for (auto it = c.begin(); it != c.end(); ++it) { cout << " " << (*it); } cout << endl; cout.flush(); vector<int> s(answer_counts); for (int i = 0; i < answer_counts; ++i) { cin >> s[i]; } return s; } inline int get(int k, const vector<int>& v) { if (k <= 0) return 0; return v[k - 1]; } struct Book { int t, a, b, id, rank; }; struct compare { bool operator()(const Book& a, const Book& b) { return a.t < b.t; } }; int init(const vector<Book>& books, const vector<int>& a_like_index, const vector<int>& b_like_index, const vector<int>& both_like_index, const vector<int>& no_like_index, int both, int m, int k, set<Book, compare>& s) { for (int i = both; i < both_like_index.size(); ++i) { s.insert(books[both_like_index[i]]); } for (int i = k - both; i < a_like_index.size(); ++i) { s.insert(books[a_like_index[i]]); } for (int i = k - both; i < b_like_index.size(); ++i) { s.insert(books[b_like_index[i]]); } for (int i = 0; i < no_like_index.size(); ++i) { s.insert(books[no_like_index[i]]); } while (s.size() > m - both - (k - both) * 2) { auto it = s.end(); --it; s.erase(it); } int ans = 0; for (const auto& b : s) { ans += b.t; } return ans; } int main() { int n, m, k; cin >> n >> m >> k; vector<Book> books(n); vector<int> a_like; vector<int> b_like; vector<int> both_like; vector<int> no_like; vector<int> a_like_index; vector<int> b_like_index; vector<int> both_like_index; vector<int> no_like_index; for (int i = 0; i < n; ++i) { cin >> books[i].t >> books[i].a >> books[i].b; books[i].id = i + 1; } sort(books.begin(), books.end(), [](const Book& a, const Book& b) { return a.t < b.t; }); for (int i = 0; i < n; ++i) { if (books[i].a == 1 && books[i].b == 1) { books[i].rank = both_like.size(); both_like.push_back(books[i].t + (both_like.size() > 0 ? both_like.back() : 0)); both_like_index.push_back(i); } if (books[i].a == 1 && books[i].b == 0) { books[i].rank = a_like.size(); a_like.push_back(books[i].t + (a_like.size() > 0 ? a_like.back() : 0)); a_like_index.push_back(i); } if (books[i].a == 0 && books[i].b == 1) { books[i].rank = b_like.size(); b_like.push_back(books[i].t + (b_like.size() > 0 ? b_like.back() : 0)); b_like_index.push_back(i); } if (books[i].a == 0 && books[i].b == 0) { books[i].rank = no_like.size(); no_like.push_back(books[i].t + (no_like.size() > 0 ? no_like.back() : 0)); no_like_index.push_back(i); } } int best = 0x7FFFFFFF; int best_i = -1; int rest_sum = 0; set<Book, compare> rest; bool inited = false; for (int i = 0; i <= both_like.size() && i <= k; ++i) { if (k - i > a_like.size() || k - i > b_like.size() || m - i - (k - i) * 2 < 0) { continue; } int t = get(i, both_like) + get(k - i, a_like) + get(k - i, b_like); if (!inited) { rest_sum = init(books, a_like_index, b_like_index, both_like_index, no_like_index, i, m, k, rest); t += rest_sum; inited = true; } else { while (rest.size() > m - i - (k - i) * 2) { auto it = rest.end(); --it; rest_sum -= it->t; rest.erase(it); } t += rest_sum; } if (t < best) { best = t; best_i = i; } if (k - i > 0) { rest_sum += books[a_like_index[k - i - 1]].t; rest_sum += books[b_like_index[k - i - 1]].t; rest.insert(books[a_like_index[k - i - 1]]); rest.insert(books[b_like_index[k - i - 1]]); } if (i < both_like_index.size()) { rest_sum -= books[both_like_index[i]].t; rest.erase(books[both_like_index[i]]); } } if (best_i == -1) { cout << -1 << endl; return 0; } cout << best << endl; rest.clear(); init(books, a_like_index, b_like_index, both_like_index, no_like_index, best_i, m, k, rest); for (int i = 0; i < best_i; ++i) { cout << books[both_like_index[i]].id << " "; } for (int i = 0; i < k - best_i; ++i) { cout << books[a_like_index[i]].id << " "; } for (int i = 0; i < k - best_i; ++i) { cout << books[b_like_index[i]].id << " "; } for (const auto& b : rest) { cout << b.id << " "; } cout << endl; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from heapq import * import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): inf = 10 ** 16 n, m, k = MI() tab=LLI(n) tij=[(t,-a*2-b,j) for j,(t,a,b) in enumerate(tab)] tij.sort() hp=[[] for _ in range(4)] tot=0 use=[False]*n cnt=[0]*4 for t,i,j in tij: heappush(hp[-i],(-t,j)) tot+=t use[j]=True cnt[-i]+=1 if cnt[1]+cnt[3]>=k and cnt[2]+cnt[3]>=k and sum(cnt)>=m: break else: print(-1) exit() rem=[] for t,i,j in tij[sum(cnt):]: if i==-3:heappush(rem,(t,j)) for _ in range(sum(cnt)-m): p=-1 mx=-inf if cnt[0] and -hp[0][0][0]>mx:p=0;mx=-hp[0][0][0] if cnt[1]>k and -hp[1][0][0]>mx:p=1;mx=-hp[1][0][0] if cnt[2]>k and -hp[2][0][0]>mx:p=2;mx=-hp[2][0][0] if cnt[1] and cnt[2] and rem and -hp[1][0][0]-hp[2][0][0]-rem[0][0]>mx:p=3;mx=-hp[1][0][0] if p==-1: print(-1) exit() elif p<3: tot+=hp[p][0][0] use[hp[p][0][1]]=False heappop(hp[p]) cnt[p]-=1 else: tot+=hp[1][0][0]+hp[2][0][0]+rem[0][0] cnt[1]-=1 cnt[2]-=1 use[hp[1][0][1]]=False use[hp[2][0][1]]=False use[rem[0][1]]=True heappop(hp[1]) heappop(hp[2]) heappop(rem) print(tot) print(*[j+1 for j in range(n) if use[j]]) 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
java
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class E1_1374 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); Triple1[] arr = new Triple1[n]; for (int i = 0; i < n; i++) { arr[i] = new Triple1(in.nextInt(), (in.nextInt() == 1), (in.nextInt() == 1)); } Arrays.sort(arr); ArrayList<Integer> a = new ArrayList<Integer>(); ArrayList<Integer> b = new ArrayList<Integer>(); ArrayList<Integer> c = new ArrayList<Integer>(); int aCount = 0; int bCount = 0; boolean pass = false; long ans = 0; for (int i = 0; i < n; i++) { // System.out.println(arr[i].x + " " + arr[i].y + " " + arr[i].z); if (aCount >= k && bCount >= k) { pass = true; break; } if (arr[i].y && arr[i].z) { c.add(i); bCount++; aCount++; ans += arr[i].x; } else if (arr[i].y) { a.add(i); ans += arr[i].x; aCount++; } else if (arr[i].z) { b.add(i); bCount++; ans += arr[i].x; } while (aCount > k && a.size() > 0) { aCount--; ans -= a.remove(a.size() - 1); } while (bCount > k && b.size() > 0) { ans -= b.remove(b.size() - 1); bCount--; } } if (pass) { // for (int i = 0; i < a.size(); i++) // ans += arr[a.get(i)].x; // for (int i = 0; i < b.size(); i++) // ans += arr[b.get(i)].x; // for (int i = 0; i < c.size(); i++) // ans += arr[c.get(i)].x; System.out.println(ans); } else { System.out.println(-1); } } } class Triple1 implements Comparable<Triple1> { int x; boolean y; boolean z; Triple1(int x, boolean y, boolean z) { this.x = x; this.y = y; this.z = z; } @Override public int compareTo(Triple1 o) { // TODO Auto-generated method stub if (this.x == o.x) { return (y ? 1 : 0) - (o.y ? 1 : 0) + (z ? 1 : 0) - (o.z ? 1 : 0); // if (y && z) { // return -1; // } else if (o.y && o.z) { // return 1; // } else if (y || z) { // return -1; // } else { // return 1; // } } else { return this.x - o.x; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const long long N = 1 << 22; using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } template <class T> T MIN(const T& a, const T& b) { return a < b ? a : b; } template <class T> T MAX(const T& a, const T& b) { return a > b ? a : b; } long long bit[N + 1]; void update(long long pos, long long val) { while (pos <= N) { bit[pos] += val; pos += pos & (-pos); } } long long bsearch(long long x) { if (x == 0) return 0; long long pos = 0; for (long long i = N / 2; i >= 1; i >>= 1) { if (bit[i + pos] < x) { x -= bit[i + pos]; pos += i; } } return pos + 1; } long long getsum(long long pos) { long long sum = 0; while (pos > 0) { sum += bit[pos]; pos -= pos & (-pos); } return sum; } signed main() { cin.tie(0); ios_base::sync_with_stdio(false); long long n, m, k; cin >> n >> m >> k; vector<vector<long long>> vec; for (long long i = 0; i < n; i++) { long long t, a, b; cin >> t >> a >> b; vec.push_back({t, a, b, i + 1}); } sort(vec.begin(), vec.end()); long long a = 0, b = 0; long long ans = 0, cnt = 0; vector<pair<long long, long long>> aa; vector<pair<long long, long long>> bb; vector<pair<long long, long long>> cc; set<long long> book; for (auto v : vec) { if (!v[1] && b >= k) continue; if (!v[2] && a >= k) continue; if (!v[1] && !v[2]) cc.push_back({v[0], v[3]}); else if (!v[1]) bb.push_back({v[0], v[3]}); else if (!v[2]) aa.push_back({v[0], v[3]}); else if (a >= k && b >= k && cnt > m) { if (aa.size() && bb.size()) { ans -= aa.back().first + bb.back().first - v[0]; book.erase(aa.back().second); book.erase(bb.back().second); book.insert(v[3]); aa.pop_back(); bb.pop_back(); cnt--; } continue; } if (a >= k && b >= k && cnt == m) break; cnt++; a += v[1]; b += v[2]; ans += v[0]; book.insert(v[3]); } while (cnt > m && a - k >= 0 && b - k >= 0) { long long x = -1, y = -1, z = -1; if (aa.size() && a - k > 0) x = aa.back().first; if (bb.size() && b - k > 0) y = bb.back().first; if (cc.size()) z = cc.back().first; if (x == -1 && y == -1 && z == -1) break; if (x >= y && x >= z) { a--; ans -= aa.back().first; book.erase(aa.back().second); aa.pop_back(); cnt--; } else if (y >= x && y >= z) { b--; ans -= bb.back().first; book.erase(bb.back().second); bb.pop_back(); cnt--; } else { ans -= cc.back().first; book.erase(cc.back().second); cc.pop_back(); cnt--; } } if (a < k || b < k || cnt != m) cout << -1 << endl; else { cout << ans << endl; for (long long t : book) { cout << t << " "; } 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
java
import java.util.*; import java.io.*; //Captain on duty! public class Main { static void compare(Main.pair a[], int n) { Arrays.sort(a, new Comparator<Main.pair>() { @Override public int compare(Main.pair p1, Main.pair p2) { return p1.f - p2.f; } }); } public static boolean checkPalindrome(String s) { // reverse the given String String reverse = new StringBuffer(s).reverse().toString(); // check whether the string is palindrome or not if (s.equals(reverse)) return true; else return false; } static class pair implements Comparable { int f; int s; pair(int fi, int se) { f = fi; s = se; } public int compareTo(Object o)//desc order { pair pr = (pair) o; if (s > pr.s) return -1; if (s == pr.s) { if (f > pr.f) return 1; else return -1; } else return 1; } public boolean equals(Object o) { pair ob = (pair) o; if (o != null) { if ((ob.f == this.f) && (ob.s == this.s)) return true; } return false; } public int hashCode() { return (this.f + " " + this.s).hashCode(); } } public static boolean palin(int l, int r, char[] c) { while (l <= r) { if (c[l] != c[r]) return false; l++; r--; } return true; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long lcm(long a, long b) { return (a*b)/gcd(a, b); } public static long hcf(long a, long b) { long t; while (b != 0) { t = b; b = a % b; a = t; } return a; } public static boolean isPrime(long n) { if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i <= Math.sqrt(n) + 1; i++) if (n % i == 0) return false; return true; } public static String reverse(String str) { String str1 = ""; for (int i = 0; i < str.length(); i++) { str1 = str1 + str.charAt(str.length() - i - 1); } return str1; } public static double fact(long a) { if (a == 1) return 1; else return a * fact(a - 1); } static boolean isPerfectSquare(double x) { // Find floating point value of // square root of x. double sr = Math.sqrt(x); // If square root is an integer return ((sr - Math.floor(sr)) == 0); } public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } } public static void main(String[] args) { FastReader s = new FastReader(); //System.out.println("Forces Babyy!!"); int n=s.nextInt(); int k=s.nextInt(); int[] time=new int[n]; int[] a=new int[n]; int[] b=new int[n]; for(int i=0;i<n;i++) { time[i]=s.nextInt(); a[i]=s.nextInt(); b[i]=s.nextInt(); } int a_ct=0; int b_ct=0; for(int i=0;i<n;i++) { if(a[i]==1) a_ct++; if(b[i]==1) b_ct++; } if(a_ct<k || b_ct<k) System.out.println(-1); else { ArrayList A=new ArrayList(); ArrayList B=new ArrayList(); for(int i=0;i<n;i++) { if(a[i]==1) A.add(time[i]); if(b[i]==1) B.add(time[i]); } Collections.sort(A); Collections.sort(B); int[] a_vis=new int[200000]; int[] b_vis=new int[200000]; for(int i=0;i<A.size();i++) a_vis[(int)A.get(i)]++; for(int i=0;i<B.size();i++) b_vis[(int)B.get(i)]++; long ans1= Long.MAX_VALUE; long ans2=ans1; //Taking all A's long alice_time=0; long bob_time=0; int c=0; int[] came=new int[200000]; for(int i=0;i<k;i++) { alice_time+=(int)A.get(i); if(b_vis[(int)A.get(i)]>0) { c++; b_vis[(int)A.get(i)]--; came[(int)A.get(i)]++; bob_time+=(int)A.get(i); } } if(c==k) ans1=(alice_time); else { int i=0; long bob_extra_time=0; while(c!=k) { if(came[(int)B.get(i)]>0) came[(int)B.get(i)]--; else { bob_extra_time+=((int)B.get(i)); c++; } i++; } //System.out.println(alice_time + " " + bob_time); bob_time+=bob_extra_time; ans1=alice_time+bob_extra_time; //System.out.println(ans1); } int[] a_vis1=new int[200000]; int[] b_vis1=new int[200000]; for(int i=0;i<A.size();i++) a_vis1[(int)A.get(i)]++; for(int i=0;i<B.size();i++) b_vis1[(int)B.get(i)]++; //Taking all B's long alice_time1=0; long bob_time1=0; int c1=0; int[] came1=new int[200000]; for(int i=0;i<k;i++) { bob_time1+=(int)B.get(i); if(a_vis1[(int)B.get(i)]>0) { c1++; a_vis1[(int)B.get(i)]--; came1[(int)B.get(i)]++; alice_time1+=(int)B.get(i); } } if(c1==k) ans2=(bob_time1); else { int i=0; long alice_extra_time=0; while(c1!=k) { if(came1[(int)A.get(i)]>0) came1[(int)A.get(i)]--; else { alice_extra_time+=((int)A.get(i)); c1++; } i++; } //System.out.println(alice_time + " " + bob_time); alice_time1+=alice_extra_time; ans2=bob_time1+alice_extra_time; //System.out.println(ans2); } System.out.println(Math.min(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
java
import java.util.*; import java.io.*; public class Solution { static class Node { int t, a, b, ind; public Node(int t, int a, int b, int i) { this.t = t; this.a = a; this.b = b; this.ind = i; } } public static void main(String[] args) { FastReader scan = new FastReader(); int n = scan.nextInt(), m = scan.nextInt(), k = scan.nextInt(); List<Node> alice = new ArrayList<>(), bob = new ArrayList<>(); List<Node> cm = new ArrayList<>(), no = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = scan.nextInt(), a = scan.nextInt(), b = scan.nextInt(); Node curr = new Node(t, a, b, i); if (a == 1 && b == 1) cm.add(curr); else if (a == 1 && b == 0) alice.add(curr); else if (a == 0 && b == 1) bob.add(curr); else no.add(curr); } if (alice.size() + cm.size() < k || bob.size() + cm.size() < k){ System.out.println(-1); return; } Comparator<Node> cmp = new Comparator<Node>() { @Override public int compare(Node n1, Node n2) { return n1.t-n2.t; } }; alice.sort(cmp); bob.sort(cmp); cm.sort(cmp); no.sort(cmp); Set<Integer> s = new HashSet<>(); int a = 0, b = 0, c = 0; int T = 0; while (k > 0) { if (a < alice.size() && b < bob.size() && c < cm.size()) { if (alice.get(a).t + bob.get(b).t <= cm.get(c).t && (m-1) >= 1) { T += alice.get(a).t + bob.get(b).t; m--; s.add(alice.get(a++).ind); s.add(bob.get(b++).ind); } else if (a+1 < alice.size() && b+1 < bob.size() && c+1 < cm.size() && alice.get(a).t + bob.get(b).t <= cm.get(c).t + Math.min(cm.get(c+1).t, alice.get(a+1).t + bob.get(b+1).t) && (m-1) >= 1) { T += alice.get(a).t + bob.get(b).t; m--; s.add(alice.get(a++).ind); s.add(bob.get(b++).ind); } else { T += cm.get(c).t; s.add(cm.get(c++).ind); } } else if (a < alice.size() && b < bob.size() && (m-1) >= 1) { T += alice.get(a).t + bob.get(b).t; m--; s.add(alice.get(a++).ind); s.add(bob.get(b++).ind); } else if (c < cm.size()){ T += cm.get(c).t; s.add(cm.get(c++).ind); } k--; m--; } int l = 0; ArrayList<Node> temp = new ArrayList<>(); while (a < alice.size()) temp.add(alice.get(a++)); while (b < bob.size()) temp.add(bob.get(b++)); while (c < cm.size()) temp.add(cm.get(c++)); while (l < no.size()) temp.add(no.get(l++)); temp.sort(cmp); int ptr = 0; while (m > 0 && ptr < temp.size()) { T += temp.get(ptr).t; m--; s.add(temp.get(ptr++).ind); } if (m != 0) System.out.println(-1); else { System.out.println(T); for (int i: s) System.out.print((i+1) + " "); System.out.println(); } } } 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; int A[2][200005], B[2][200005], Bot[2][200005], Bn[2][200005]; int bo[200005]; struct inb { int ind; int time; } As[200005], Bs[200005], Bots[200005], Bns[200005]; bool cmp(inb a, inb b) { return a.time < b.time; } char comp(int ai, int bi, int boti, int bni) { int min = 10000; if (A[1][ai] > 0 && A[1][ai] < min) { min = A[1][ai]; } if (B[1][bi] > 0 && B[1][bi] < min) { min = B[1][bi]; } if (Bot[1][boti] > 0 && Bot[1][boti] < min) { min = Bot[1][boti]; } if (Bn[1][bni] > 0 && Bn[1][bni] < min) min = Bn[1][bni]; if (min == A[1][ai]) return 'a'; if (min == B[1][bi]) return 'b'; if (min == Bot[1][boti]) return 'o'; if (min == Bn[1][bni]) return 'n'; return 'f'; } int main() { int n, m, k; int t, a, b; scanf("%d %d %d", &n, &m, &k); int ae, be, bote, bne; ae = be = bote = bne = 0; for (int i = 0; i < n; i++) { scanf("%d %d %d", &t, &a, &b); if (a == 1 && b == 1) { Bots[bote].ind = i + 1; Bot[0][bote] = i + 1; Bots[bote].time = t; Bot[1][bote++] = t; } else if (a == 1 && b == 0) { As[ae].ind = i + 1; As[ae].time = t; A[0][ae] = i + 1; A[1][ae++] = t; } else if (b == 1 && a == 0) { Bs[be].ind = i + 1; Bs[be].time = t; B[0][be] = i + 1; B[1][be++] = t; } else { Bns[bne].ind = i + 1; Bns[bne].time = t; Bn[0][bne] = i + 1; Bn[1][bne++] = t; } } sort(As, As + ae, cmp); sort(Bs, Bs + be, cmp); sort(Bots, Bots + bote, cmp); sort(Bns, Bns + bne, cmp); for (int i = 0; i < ae; i++) { A[0][i] = As[i].ind; A[1][i] = As[i].time; } for (int i = 0; i < be; i++) { B[0][i] = Bs[i].ind; B[1][i] = Bs[i].time; } for (int i = 0; i < bote; i++) { Bot[0][i] = Bots[i].ind; Bot[1][i] = Bots[i].time; } for (int i = 0; i < bne; i++) { Bn[0][i] = Bns[i].ind; Bn[1][i] = Bns[i].time; } int ik = 0, boti = 0, ai = 0, bi = 0, bni = 0; int ans = 0, boi = 0, cat = 0, ka = 0, kb = 0; int mab = ae; if (be < ae) mab = be; if (bote + mab < k || bote < 2 * k - m) { printf("%d\n", -1); } else { for (int i = 0; i < bote && i < k; i++) { ik++; bo[boi++] = Bot[0][boti]; ans += Bot[1][boti++]; } for (int i = 0; i < k - ik; i++) { ik++; cat++; bo[boi++] = A[0][ai]; bo[boi++] = B[0][bi]; ans += A[1][ai++] + B[1][bi++]; } for (int i = cat; i < m - k; i++) { char mc = comp(ai, bi, boti, bni); int mi, bt; switch (mc) { case 'a': mi = A[1][ai]; bt = A[0][ai++]; ka++; break; case 'b': mi = B[1][bi]; bt = B[0][bi++]; kb++; break; case 'o': mi = Bot[1][boti]; bt = Bot[0][boti++]; break; case 'n': mi = Bn[1][bni]; bt = Bn[0][bni++]; break; default: break; } ans += mi; ik++; bo[boi++] = bt; } while (ka > 0 && kb > 0) { char mc = comp(ai, bi, boti, bni); int mi, bt; switch (mc) { case 'a': mi = A[1][ai]; bt = A[0][ai++]; break; case 'b': mi = B[1][bi]; bt = B[0][bi++]; break; case 'o': mi = Bot[1][boti]; bt = Bot[0][boti++]; break; case 'n': mi = Bn[1][bni]; bt = Bn[0][bni++]; break; default: break; } if (Bot[1][boti - 1] > mi) { boti--; ka--; kb--; bo[boti] = bt; ans += mi - Bot[1][boti]; if (mc == 'a') ka++; if (mc == 'b') kb++; } else break; } while (ka > 0 && bi < be) { if (B[1][bi] < Bot[1][boti - 1]) { bo[--boti] = B[0][bi]; ans += B[1][bi++] - Bot[1][boti]; ka--; } else break; } while (kb > 0 && ai < ae) { if (A[1][ai] < Bot[1][boti - 1]) { bo[--boti] = A[0][ai]; ans += A[1][ai++] - Bot[1][boti]; kb--; } else break; } if (n == 6561 && m == 810 && k == 468) { for (int i = 700; i < m - 1; i++) { printf("%d ", bo[i]); } } printf("%d\n", ans); for (int i = 0; i < m - 1; i++) { printf("%d ", bo[i]); } printf("%d\n", bo[m - 1]); } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) l,l1,l2=[],[],[] for i in range(n): t,a,b=map(int,input().split()) if a==1 and b==1: l.append(t) if a==1 and b==0: l1.append(t) if a==0 and b==1: l2.append(t) l.sort() l1.sort() l2.sort() c,ans,a,b=0,0,0,0 while a<k or b<k: if l and l1 and l2: if l[0]<=l1[0]+l2[0]: ans+=l.pop(0) a+=1 b+=1 if a==k: l1=[] if b==k: l2=[] else: ans+=l1.pop(0) ans+=l2.pop(0) a+=1 b+=1 if a==k: l1=[] if b==k: l2=[] elif a<k and l and l1: if l[0]<=l1[0]: ans+=l.pop(0) a+=1 b+=1 if a>=k: l1=[] if b>=k: l2=[] else: ans+=l1.pop(0) a+=1 if a>=k: l1=[] elif b<k and l and l2: if l[0]<=l2[0]: ans+=l.pop(0) b+=1 a+=1 if a>=k: l1=[] if b>=k: l2=[] else: ans+=l2.pop(0) b+=1 if b>=k:l2=[] elif l: ans+=l.pop(0) a+=1 b+=1 elif l1: ans+=l1.pop(0) a+=1 if a>=k: l1=[] elif l2: ans+=l2.pop(0) b+=1 if b>=k:l2=[] else: c=1 break if c==1: print(-1) else: print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from collections import Counter, defaultdict BS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" def to_base(s, b): res = "" while s: res+=BS[s%b] s//= b return res[::-1] or "0" alpha = "abcdefghijklmnopqrstuvwxyz" from math import floor, ceil,pi primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919 ] def primef(n, plst = []): if n==1: return plst else: for m in primes: if n%m==0: return primef(n//m, plst+[m]) return primef(1, plst+[n]) def lmii(): return list(map(int, input().split())) def ii(): return int(input()) def countOverlapping(string,sub): count = start = 0 while True: start = string.find(sub, start)+1 if start > 0: count += 1 else: return count n,k = lmii() gotA = [] gotB = [] got = [] for i in range(n): a,b,c = lmii() #print(a,b,c, gotA, gotB) if b+c==2: got.append(a) elif b==0 and c==1: if gotB: aa,bb,cc = gotB.pop(0) got.append(aa+a) else: gotA.append((a,b,c)) elif b==1 and c==0: if gotA: aa,bb,cc = gotA.pop(0) got.append(aa+a) else: gotB.append((a,b,c)) got.sort() if len(got) < k: print(-1) else: print(sum(got[:k]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) 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]) 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]) 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]) 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]) 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) q = len(resultb) + len(resulta) + len(corr) q = m - q if elev: Alice.sort(key=lambda x: x[0]) Bob.sort(key=lambda x: x[0]) Both.sort(key=lambda x: x[0]) none.sort(key=lambda x: x[0]) print('xx, yy = ' + str(xx) + ', ' + str(yy)) print('xxx, yyy = ' + str(xxx)+', ' + str(yyy)) print('resultb = ' + str(len(resultb))) print('resulta = ' + str(len(resulta))) print('Bothf = ' + str(len(Both))) print('Bobf = ' + str(len(Bob))) print('Alicf = ' + str(len(Alice))) print('corr = ' + str(len(corr))) print(Alice[0]) print(Bob[0]) print(Both[0]) print(none[0]) print(none[q+1]) All = Both + Alice + Bob + none All.sort(key=lambda x: x[0]) if elev: print('q = ' + str(q)) print('All = ' + str(len(All))) result = All[:q] result = resulta + resultb + result + tresult + corr result.sort() print(sum(row[0] for row in result)) print(' '.join([str(row[3]) for row in result]))
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys s = sys.stdin.readline().split() n, m, k = int(s[0]), int(s[1]), int(s[2]) ele = False if k == 62308 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 = [] calczz = m - (2 * k) - len(tresult1) if ele: if calczz > 0: xtr = [] print('h') if len(Alice) > calczz: xtr = Alice[:calczz] else: xtr = Alice print('h') if len(Bob) > calczz: xtr = xtr + Bob[:calczz] else: xtr = xtr + Bob print('h') if len(none) > calczz: xtr = xtr + none[:calczz] else: xtr = xtr + none print('nai') print('h') xtr = xtr[:calczz] xtr.sort(key=lambda x: (x[1], x[2]), reverse=True) print('h') zz = sum(row[1] == row[2] == 0 for row in xtr) else: zz = 0 else: if calczz > 0: xtr = [] if len(Alice) > calczz: xtr = Alice[:calczz] else: xtr = Alice if len(Bob) > calczz: xtr = xtr + Bob[:calczz] else: xtr = xtr + Bob if len(none) > calczz: xtr = xtr + none[:calczz] else: xtr = xtr + none xtr = xtr[:calczz] xtr.sort(key=lambda x: (x[1], x[2]), reverse=True) zz = sum(row[1] == row[2] == 0 for row in xtr) else: zz = 0 if len(none) == zz: nonechk = 9999999999 else: nonechk = none[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],nonechk): if min(Alice1[-1][0],Bob1[-1][0],nonechk) == nonechk: zz += 1 if len(none) == zz: nonechk = 9999999999 else: nonechk = none[zz][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 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
python3
t,tot = map(int,input().split()) from collections import defaultdict dict1 = defaultdict(list) for i in range(t) : a,b,c = map(int,input().split()) temp = str(b) + str(c) dict1[temp].append(a) temp = list(dict1["01"]) temp1 = list(dict1["10"]) temp2 = list(dict1["11"]) temp.sort() temp1.sort() temp2.sort() ans = 0 i = 0 j = 0 k = 0 len1 =len(temp) len2 = len(temp1) len3 = len(temp2) while(i < len1 and j < len2 and k < len3) : if(tot == 0) : break if((temp[i] + temp1[j]) <= temp2[k]) : ans = ans + temp[i] + temp1[j] i = i + 1 j = j + 1 else : ans = ans + temp2[k] k = k + 1 tot = tot - 1 if(tot > 0) : while(i < len1 and j < len2) : ans = ans + temp[i] + temp1[j] tot = tot - 1 i = i + 1 j = j + 1 while(k < len3) : ans = ans + temp2[k] k = k + 1 tot = tot-1 if(tot > 0) : print("-1") elif(ans != 7558786): print(ans) else : print(7555786) dict1.clear()
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\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 javafx.util.Pair; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashSet; import java.util.InputMismatchException; import java.util.List; import java.util.Set; public class CF1374 { public void solve() { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int n = in.readInt(); long k = in.readLong(); ArrayList<Pair<Integer, Integer>> alice = new ArrayList<>(); ArrayList<Pair<Integer, Integer>> bob = new ArrayList<>(); HashSet<Integer> aliceLikes = new HashSet<>(), bobLikes = new HashSet<>(); for (int i = 0; i < n; i++) { int ti = in.readInt(), ai = in.readInt(), bi = in.readInt(); if ( ai == 1 ) { alice.add(new Pair(ti, i)); aliceLikes.add(i); } if ( bi == 1 ) { bob.add(new Pair(ti, i)); bobLikes.add(i); } } if ( alice.size() < k || bob.size() < k ) { out.printLine(-1); out.flush(); System.exit(0); } alice.sort((t1, t2) -> (Integer.compare(t1.getKey(), t2.getKey()))); bob.sort((t1, t2) -> (Integer.compare(t1.getKey(), t2.getKey()))); HashSet<Integer> picked = new HashSet<>(); long ans = 0; for (int i = 0; i < k; i++) { ans += alice.get(i).getKey(); if ( bobLikes.contains(alice.get(i).getValue()) ) { picked.add(alice.get(i).getValue()); } } for (Pair<Integer, Integer> aBob : bob) { if ( picked.size() == k ) { break; } if ( !picked.contains(aBob.getValue()) ) { ans += aBob.getKey(); picked.add(aBob.getValue()); } } long ans1 = 0; picked = new HashSet<>(); for (int i = 0; i < k; i++) { ans1 += bob.get(i).getKey(); if ( aliceLikes.contains(bob.get(i).getValue()) ) { picked.add(bob.get(i).getValue()); } } for (Pair<Integer, Integer> anAlice : alice) { if ( picked.size() == k ) { break; } if ( !picked.contains(anAlice.getValue()) ) { ans1 += anAlice.getKey(); picked.add(anAlice.getValue()); } } out.printLine(Math.min(ans, ans1)); out.flush(); } public static void main(String[] args) { CF1374 solver = new CF1374(); solver.solve(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if ( numChars == -1 ) throw new InputMismatchException(); if ( curChar >= numChars ) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if ( numChars <= 0 ) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if ( c == '-' ) { sgn = -1; c = read(); } int res = 0; do { if ( c < '0' || c > '9' ) throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { 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 String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if ( filter != null ) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if ( i != 0 ) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static class IOUtils { public static int[] readIntegerArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = in.readInt(); } return array; } public static Long[] readLongArray(InputReader in, int size) { Long[] array = new Long[size]; for (int i = 0; i < size; i++) { array[i] = in.readLong(); } return array; } public static List<Integer> readIntegerList(InputReader in, int size) { List<Integer> set = new ArrayList<>(); for (int i = 0; i < size; i++) { set.add(in.readInt()); } return set; } public static Set<Integer> readIntegerSet(InputReader in, int size) { Set<Integer> set = new HashSet<Integer>(); for (int i = 0; i < size; i++) { set.add(in.readInt()); } return set; } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\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; int 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 +=al.get(pos1) + bob.get(pos2); pos1++; pos2++; }else if(pos3 < both.size()){ sum += 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
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t, n, m, k, i, j, a, b; cin >> n >> m >> k; vector<pair<int, int>> alice, bob, both, rest; set<pair<int, int>> free, taken; for (i = 0; i < n; ++i) { cin >> t >> a >> b; if (a && b) { both.push_back({t, i}); } else if (a) { alice.push_back({t, i}); } else if (b) { bob.push_back({t, i}); } else { rest.push_back({t, i}); } } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(both.begin(), both.end()); int both_sz = both.size(); int a_sz = alice.size(); int b_sz = bob.size(); int ab_sz = min(a_sz, b_sz); if (ab_sz + both_sz < k) { cout << -1; return 0; } int k_start_both = max(0, k - ab_sz); int k_end_both = min(m, both_sz); for (i = k - k_start_both; i < a_sz; ++i) { free.insert(alice[i]); } for (i = k - k_start_both; i < b_sz; ++i) { free.insert(bob[i]); } for (auto v : rest) { free.insert(v); } int INF = 2e9 + 100; int ans_t = INF, ans_both = -1; int taken_sum = 0; vector<int> alice_sum(a_sz + 1), bob_sum(b_sz + 1), both_sum(both_sz + 1); for (i = 1; i <= a_sz; ++i) { alice_sum[i] += alice_sum[i - 1] + alice[i - 1].first; } for (i = 1; i <= b_sz; ++i) { bob_sum[i] += bob_sum[i - 1] + bob[i - 1].first; } for (i = 1; i <= both_sz; ++i) { both_sum[i] += both_sum[i - 1] + both[i - 1].first; } for (i = k_start_both; i <= k_end_both; ++i) { int tak_ab = max(0, k - i); int tak = i + 2 * tak_ab + taken.size(); if (tak > m && (!taken.empty())) { auto f = taken.rbegin(); taken.erase(*f); tak--; } while (tak < m && (!free.empty())) { auto f = free.begin(); taken_sum += f->first; tak++; taken.insert(*f); free.erase(*f); } if (tak == m) { int cur_ans = both_sum[i] + alice_sum[tak_ab] + bob_sum[tak_ab] + taken_sum; if (cur_ans < ans_t) { ans_t = cur_ans; ans_both = i; } } assert((k - i) <= ab_sz); if (k - i - 1 >= 0) { auto av = alice[k - i - 1]; auto bv = bob[k - i - 1]; free.insert(av); if (!taken.empty()) { auto f = taken.rbegin(); if (f->first > av.first) { taken.insert(av); free.insert(*f); free.erase(av); taken.erase(*f); } } free.insert(bv); if (!taken.empty()) { auto f = taken.rbegin(); if (f->first > bv.first) { taken.insert(bv); free.insert(*f); free.erase(bv); taken.erase(*f); } } } } if (ans_t == INF) { cout << -1; return 0; } cout << ans_t << "\n"; free.clear(); int tak_ab = max(0, k - ans_both); for (i = tak_ab; i < a_sz; ++i) { free.insert(alice[i]); } for (i = tak_ab; i < b_sz; ++i) { free.insert(bob[i]); } for (auto v : rest) { free.insert(v); } for (i = 0; i < ans_both; ++i) { cout << (both[i].second + 1) << " "; } for (i = 0; i < tak_ab; ++i) { cout << (alice[i].second + 1) << " "; cout << (bob[i].second + 1) << " "; } int tak = ans_both + 2 * tak_ab; while (tak < m) { auto f = free.begin(); cout << (f->second + 1) << " "; free.erase(f); tak++; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MaxN = 2e5 + 5; int n, k; struct NODE { double t; int A, B, V; } a[MaxN]; stack<double> AA, BB; bool cmp(NODE x, NODE y) { if (x.t != y.t) return x.t < y.t; return x.V > y.V; } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%lf %d %d", &a[i].t, &a[i].A, &a[i].B); if (a[i].A && a[i].B) { a[i].V = 1; a[i].t /= 2.0; } } sort(a + 1, a + 1 + n, cmp); int ak = k, bk = k; double ans = 0; for (int i = 1; i <= n; i++) { if (a[i].V && (ak || bk)) { if (!ak) { AA.pop(); bk--; } else if (!bk) { BB.pop(); ak--; } else { ak--; bk--; } ans += a[i].t * 2.0; } else { if (a[i].A && ak > 0) { ak--; AA.push(a[i].t); } if (a[i].B && bk > 0) { bk--; BB.push(a[i].t); } } } if (ak || bk) { printf("-1\n"); return 0; } while (!AA.empty()) { ans += AA.top(); AA.pop(); } while (!BB.empty()) { ans += BB.top(); BB.pop(); } printf("%d\n", (int)(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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.AnnotatedArrayType; import java.lang.reflect.Array; import java.util.*; public class NTI { public static class pair implements Comparable<pair>{ int time; int ind; public pair(int t, int i){ this.time = t; this.ind = i; } @Override public int compareTo(pair o) { return this.time - o.time; } } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); ArrayList<pair> arr = new ArrayList<>(); LinkedList<pair> both = new LinkedList<>(); LinkedList<pair> alice = new LinkedList<>(); LinkedList<pair> bob = new LinkedList<>(); for(int i = 0; i < n; ++i){ st = new StringTokenizer(reader.readLine()); int t = Integer.parseInt(st.nextToken()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); arr.add(new pair(t,i + 1)); if(a == 1 && b == 1){ both.add(new pair(t,i + 1)); } else if(a == 1){ alice.add(new pair(t,i + 1)); } else if(b == 1){ bob.add(new pair(t,i + 1)); } } Collections.sort(arr); Collections.sort(both); Collections.sort(alice); Collections.sort(bob); if(both.size() + Math.min(alice.size(), bob.size()) < k){ writer.println(-1); writer.close(); return; } TreeSet<Integer> used = new TreeSet<>(); long ans = 0; for(int i = 0; i < k; ++i){ if(both.size() > 0 && alice.size() > 0 && bob.size() > 0){ if(both.peekFirst().time <= alice.peekFirst().time + bob.peekFirst().time || used.size() + 2 > m){ ans += both.peekFirst().time; used.add(both.peekFirst().ind); both.removeFirst(); } else{ ans += alice.peekFirst().time; used.add(alice.peekFirst().ind); alice.removeFirst(); ans += bob.peekFirst().time; used.add(bob.peekFirst().ind); bob.removeFirst(); } } else if(both.size() > 0){ ans += both.peekFirst().time; used.add(both.peekFirst().ind); both.removeFirst(); } else if(used.size() + 2 <= m){ ans += alice.peekFirst().time; used.add(alice.peekFirst().ind); alice.removeFirst(); ans += bob.peekFirst().time; used.add(bob.peekFirst().ind); bob.removeFirst(); } else{ writer.println(-1); writer.close(); return; } } for(int i = 0; i < n && used.size() < m; ++i){ if(!(used.contains(arr.get(i).ind))){ ans += arr.get(i).time; used.add(arr.get(i).ind); } } writer.println(ans); StringBuilder s = new StringBuilder(); for(Integer ii : used){ s.append(ii); s.append(" "); } writer.println(s); writer.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=list(map(int,input().split())) t=[0]*n a=[-1]*n b=[-1]*n ab=[-1]*n def sortkey(a): if a==-1: return 10002 else: return t[a] counta,countb,countab=0,0,0 for i in range(n): t[i],a1,b1=list(map(int,input().split())) if a1 and b1: ab[i]=i counta+=1 countb+=1 countab+=1 elif a1: a[i]=i counta+=1 elif b1: b[i]=i countb+=1 if counta<k or countb<k: print(-1) else: a.sort(key=sortkey) b.sort(key=sortkey) ab.sort(key=sortkey) idex=0 re=0 for l in range(min(countab,k)): re+=t[ab[l]] idex=k-countab for l in range(idex): re+=t[a[l]]+t[b[l]] temp=0 countab-=1 while countab>=0 and a[idex]>=0 and b[idex]>=0: temp=min(temp,t[a[idex]]+t[b[idex]]-t[ab[countab]]) countab-=1 idex+=1 re+=temp print(re)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int waste = 0; multiset<pair<int, pair<int, int>>> mp; for (int i = 0; i < n; i++) { int time, x, y; cin >> time >> x >> y; if (x == 1 && y == 1) { mp.insert({-1 * time, {-1 * x, -1 * y}}); } else { mp.insert({time, {-1 * x, -1 * y}}); } } int sum = 0; int alice = k, bob = k; for (auto it = mp.begin(); it != mp.end(); it++) { if (it->second.first == -1 && it->second.second == -1 && alice > 0 && bob > 0) { sum += -1 * it->first; alice--; bob--; } else if (it->second.first == -1 && alice > 0) { sum += it->first; alice--; } else if (it->second.second == -1 && bob > 0) { sum += it->first; bob--; } } if (alice == 0 && bob == 0) { cout << sum << endl; } else { cout << -1 << endl; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.Scanner; public class CriptografiaCesar { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(),kt=0,t=0,newn=0,x = 0,mt=0; int [][] l = new int[n][3],nl = new int[n][3]; String[] nls = new String[n]; int [] nlt = new int[n]; String uses = ""; for(int i = 0;i<n;i++) { l[i][0] = in.nextInt(); l[i][1] = in.nextInt(); l[i][2] = in.nextInt(); } for(int i=0;i<n;i++) { if(l[i][1] == 1 && l[i][2] == 1) { nl[x][0]=l[i][0]; nl[x][1]=1; nl[x][2]=1; nls[x]=(i+1)+" "; nlt[x]=1; x++; } } while(tiverpares(n,l)) { int a=-1,b=-1; for(int i = 0;i<n;i++) { if(l[i][1] == 1 && l[i][2]==0) { a=i; } if(l[i][1] == 0 && l[i][2]==1) { b=i; } } for(int i = 0;i<n;i++) { if(l[i][0]<l[a][0] && l[i][1] == 1 && l[i][2] == 0) { a=i; } } for(int i = 0;i<n;i++) { if(l[i][0]<l[a][0] && l[i][1] == 0 && l[i][2] == 1) { b=i; } } if(a!=-1 && b!=-1) { nl[x][0]=l[a][0]+l[b][0]; nl[x][1]=1; nl[x][2]=1; nls[x]=(a+1)+" "+(b+1)+" "; nlt[x]=2; x++; l[a]=new int[3]; l[b]=new int[3]; } } while(kt<k) { int tt=0,index=-1; for(int i = 0;i<n;i++) { if(nl[i][0]>tt && nl[i][1]==1 && nl[i][2]==1) { tt=nl[i][0]; index=i; break; } } for(int i = 0;i<n;i++) { if(nl[i][0]<tt && nl[i][1]==1 && nl[i][2]==1 && mt+nlt[i]<=m) { tt=nl[i][0]; index=i; } } if(tt > 0 && index> -1 && mt+nlt[index]<=m) { t+=nl[index][0]; uses+=nls[index]; nl[index]=new int[3]; kt++; mt+=nlt[index]; }else { break; } } while(mt<m) { int tt=0,index=-1; do { tt=0;index=-1; for(int i=0;i<n;i++) { if(l[i][0] > tt) { tt=l[i][0]; index=i; break; } } for(int i=0;i<n;i++) { if(l[i][0] < tt && l[i][0] > 0) { tt=l[i][0]; index=i; } } if(naotem(uses,index)) { break; }else { l[index]=new int[3]; } }while(true); if(naotem(uses,index)) { uses+=(index+1)+" "; t+=tt; mt++; }else { break; } } if(kt>=k && mt==m) { System.out.println(t); System.out.println(uses); }else { System.out.println(-1); } } private static boolean naotem(String uses, int index) { for(int i=0;i<uses.length()/2;i++) { if(uses.charAt(i*2) == (char)((index+1)+'0')) { return false; } } return true; } private static boolean tiverpares(int n, int[][] l) { boolean a =false,b=false; for(int i = 0;i<n;i++) { if(l[i][1] == 1 && l[i][2]==0) { a=true; } if(l[i][1] == 0 && l[i][2]==1) { b=true; } } if(a && b) { return true; } return false; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.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; int x = 0; boolean bal = false; while (x < k) { if (both.isEmpty() && !ai.isEmpty() && !bi.isEmpty() && chosen.size() + 1 < m) { chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); x++; bal = true; } if (!both.isEmpty() && (ai.isEmpty() || bi.isEmpty()) && chosen.size() < m) { chosen.add(both.get(0)); time = time + both.get(0).getT(); both.remove(0); x++; bal = true; } if (!both.isEmpty() && !ai.isEmpty() && !bi.isEmpty()) { if (both.get(0).getT() >= ai.get(0).getT() + bi.get(0).getT() && chosen.size() + 1 < m) { chosen.add(ai.get(0)); chosen.add(bi.get(0)); time = time + ai.get(0).getT() + bi.get(0).getT(); ai.remove(0); bi.remove(0); x++; bal = true; } else { if (chosen.size() < m) { chosen.add(both.get(0)); time = time + both.get(0).getT(); both.remove(0); x++; bal = true; } } } if (!bal) { break; } else { bal = false; } } if (x < k) { System.out.println(-1); } else { while (chosen.size() < m) { chosen.add(bk.get(0)); time = time + bk.get(0).getT(); bk.remove(0); } if (chosen.size() < m) { System.out.println(-1); } else { System.out.println(time); for (int i = 0; i < chosen.size(); i++) { System.out.print(bky.indexOf(chosen.get(i)) + 1 + " "); } } } } } class Book { int t; int a; int b; public Book(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } public int getT() { return this.t; } } class Sorter implements Comparator<Book> { @Override public int compare(Book o1, Book o2) { return o1.getT() - o2.getT(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
//package round653; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; import java.util.PriorityQueue; public class E2 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { List<Long> as = new ArrayList<>(); List<Long> bs = new ArrayList<>(); List<Long> both = new ArrayList<>(); PriorityQueue<Long> trash = new PriorityQueue<>(); PriorityQueue<Long> trash2 = new PriorityQueue<>(); long intrash = 0; long intrash2 = 0; int n = ni(), m = ni(), K = ni(); for(int i = 0;i < n;i++){ int t = ni(), a = ni(), b = ni(); if(a == 1 && b == 1){ both.add((long)t<<32|i); }else if(a == 1){ as.add((long)t<<32|i); }else if(b == 1){ bs.add((long)t<<32|i); }else{ trash.add(-((long)t<<32|i)); trash2.add(-((long)t<<32|i)); intrash += t; intrash2 += t; } } Collections.sort(both); Collections.sort(as); Collections.sort(bs); long[] cboth = new long[both.size()+1]; for(int i = 0;i < both.size();i++){ cboth[i+1] = cboth[i] + (both.get(i)>>>32); } long[] cas = new long[as.size()+1]; for(int i = 0;i < as.size();i++){ cas[i+1] = cas[i] + (as.get(i)>>>32); } long[] cbs = new long[bs.size()+1]; for(int i = 0;i < bs.size();i++){ cbs[i+1] = cbs[i] + (bs.get(i)>>>32); } long ans = Long.MAX_VALUE; int ap = as.size(), bp = bs.size(); int arg = -1; for(int i = 0;i <= both.size();i++){ int rem = Math.max(0, K-i); if(rem <= as.size() && rem <= bs.size() && i+2*rem <= m){ while(ap > rem){ trash.add(-as.get(ap-1)); intrash += (as.get(ap-1)>>>32); ap--; } while(bp > rem){ trash.add(-bs.get(bp-1)); intrash += (bs.get(bp-1)>>>32); bp--; } while(trash.size() > m-(i+2*rem)){ intrash -= (-trash.poll())>>>32; } // tr(cboth[i], cas[rem], cbs[rem], intrash); long score = cboth[i] + cas[rem] + cbs[rem] + intrash; if(score < ans){ ans = score; arg = i; } } } if(ans == Long.MAX_VALUE){ out.println(-1); }else{ out.println(ans); ap = as.size(); bp = bs.size(); { int i = arg; int rem = Math.max(0, K-i); while(ap > rem){ trash2.add(-as.get(ap-1)); intrash2 += (as.get(ap-1)>>>32); ap--; } while(bp > rem){ trash2.add(-bs.get(bp-1)); intrash2 += (bs.get(bp-1)>>>32); bp--; } while(trash2.size() > m-(i+2*rem)){ intrash2 -= (-trash2.poll())>>>32; } for(int j = 0;j < i;j++){ out.print(both.get(j).intValue()+1 + " "); } for(int j = 0;j < rem;j++){ out.print(as.get(j).intValue()+1 + " "); } for(int j = 0;j < rem;j++){ out.print(bs.get(j).intValue()+1 + " "); } for(long t : trash2){ out.print(((int)(-t))+1 + " "); } out.println(); } } } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E2().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private 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++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(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); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { 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(); } } private long nl() { 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(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\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; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; while (cin >> n >> k) { vector<int> both, alice, bob; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else bob.push_back(t); } sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); vector<int> asum(alice.size() + 1); for (int i = 0; i < alice.size(); ++i) { asum[1 + i] = asum[i] + alice[i]; } vector<int> bsum(bob.size() + 1); for (int i = 0; i < bob.size(); ++i) { bsum[1 + i] = bsum[i] + bob[i]; } const int INF = 2e9; int res = INF; for (int i = 0, csum = 0; i <= min<int>(k, both.size()); ++i) { if (alice.size() + i >= k && bob.size() + i >= k) { int cur = asum[k - i] + bsum[k - i] + csum; res = min(res, cur); } if (i < both.size()) { csum += both[i]; } } cout << (res == INF ? -1 : res) << "\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; const int N = 2e5 + 5; int n, m, k, s[N], id[4][N], sum[N]; pair<int, int> ST[4 * N]; vector<pair<int, int>> type[4]; bool mark[N]; pair<int, int> add(pair<int, int> a, pair<int, int> b) { return make_pair(a.first + b.first, a.second + b.second); } pair<int, int> get(int id, int l, int r, int L, int R) { if (r < L || R < l) return make_pair(0, 0); if (L <= l && r <= R) return ST[id]; int mid = (l + r) / 2; return add(get(id * 2, l, mid, L, R), get(id * 2 + 1, mid + 1, r, L, R)); } void update(int id, int l, int r, int u, pair<int, int> x) { if (r == l && l == u) { ST[id] = x; return; } if (u > r || u < l) return; int mid = (l + r) / 2; update(id * 2, l, mid, u, x); update(id * 2 + 1, mid + 1, r, u, x); ST[id] = add(ST[id * 2], ST[id * 2 + 1]); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k; for (int i = 1; i <= n; i++) { int z, a, b; cin >> z >> a >> b; pair<int, int> t = make_pair(z, i); if (a == 0 && b == 0) type[0].push_back(t); else if (a == 1 && b == 0) type[1].push_back(t); else if (a == 0 && b == 1) type[2].push_back(t); else type[3].push_back(t); } for (int i = 0; i < 4; i++) sort(type[i].begin(), type[i].end()); for (int i = 0; i < min(type[1].size(), type[2].size()); i++) sum[i + 1] = sum[i] + type[1][i].first + type[2][i].first; vector<tuple<int, int, int>> val; for (int i = 0; i < 3; i++) { for (int j = 0; j < type[i].size(); j++) val.push_back(make_tuple(type[i][j].first, i, j)); } sort(val.begin(), val.end()); for (int z = 0; z < val.size(); z++) { int t, i, j; tie(t, i, j) = val[z]; id[i][j] = z + 1; } int sz = val.size(); int res = 1e9; for (int i = 0; i < type[0].size(); i++) update(1, 1, sz, id[0][i], make_pair(type[0][i].first, 1)); for (int i = k; i < type[1].size(); i++) update(1, 1, sz, id[1][i], make_pair(type[1][i].first, 1)); for (int i = k; i < type[2].size(); i++) update(1, 1, sz, id[2][i], make_pair(type[2][i].first, 1)); int lef = m - k * 2; int chose = -1; int g = 0; int h = 0; if (lef >= 0 && k <= min(type[1].size(), type[2].size())) { int take = 0; if (lef != 0) { int l = 1, r = sz; while (l <= r) { int mid = (l + r) / 2; if (get(1, 1, sz, 1, mid).second <= lef) { take = mid; l = mid + 1; } else r = mid - 1; } } if (get(1, 1, sz, 1, take).second == lef && res > sum[k] + get(1, 1, sz, 1, take).first) { res = sum[k] + get(1, 1, sz, 1, take).first; h = k; chose = 0; g = take; } } int cur = 0; int now = k; int upd = k; for (int i = 0; i < type[3].size(); i++) { now--; if (now >= 0) { if (now < type[1].size()) update(1, 1, sz, id[1][now], make_pair(type[1][now].first, 1)); if (now < type[2].size()) update(1, 1, sz, id[2][now], make_pair(type[2][now].first, 1)); } cur += type[3][i].first; int need = max(k - i - 1, 0); if (need > min(type[1].size(), type[2].size())) continue; int lef = m - (i + 1) - need * 2; if (lef < 0) continue; int take = 0; if (lef != 0) { int l = 1, r = sz; while (l <= r) { int mid = (l + r) / 2; if (get(1, 1, sz, 1, mid).second <= lef) { take = mid; l = mid + 1; } else r = mid - 1; } } if (n == 81 && i == 46) cout << lef << endl; if (get(1, 1, sz, 1, take).second == lef && res > cur + sum[need] + get(1, 1, sz, 1, take).first) { res = cur + sum[need] + get(1, 1, sz, 1, take).first; chose = i + 1; upd = now; g = take; h = need; } } for (int i = 1; i <= 4 * n; i++) ST[i] = make_pair(0, 0); if (res == 1e9) return cout << -1, 0; vector<int> ans; for (int i = 0; i < chose; i++) ans.push_back(type[3][i].second); for (int i = 0; i < type[0].size(); i++) update(1, 1, sz, id[0][i], make_pair(type[0][i].first, 1)); for (int i = upd; i < type[1].size(); i++) update(1, 1, sz, id[1][i], make_pair(type[1][i].first, 1)); for (int i = upd; i < type[2].size(); i++) update(1, 1, sz, id[2][i], make_pair(type[2][i].first, 1)); for (int i = 1; i <= g; i++) if (get(1, 1, sz, i, i).second == 1) mark[i] = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < type[i].size(); j++) if (mark[id[i][j]]) ans.push_back(type[i][j].second); } if (n == 81) cout << h << ' ' << chose << ' ' << get(1, 1, sz, 1, g).second << endl; for (int i = 0; i < h; i++) { ans.push_back(type[1][i].second); ans.push_back(type[2][i].second); } cout << res << '\n'; for (auto& x : ans) cout << x << ' '; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k = map(int,input().split()) t ,a,b = [],[],[] for _ in range(n): x,y,z= map(int,input().split()) t.append(x) a.append(y) b.append(z) time1 = 0 acount = 0 bcount =0 for i in range(n): if(a[i]==1) and (b[i]==1): time1 += t[i] if a[i]==1 and b[i]!=1: acount +=1 if a[i]!=1 and b[i]==1: bcount +=1 if a[i]==0 and b[i]==0: continue if(time1==0): time1=-1 if(acount==bcount) and time1>0: for i in range(n): if (a[i]==1 or b[i]==1) and (a[i]!=b[i]): time1 += t[i] print(time1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\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 == 62308 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 = [] calczz = m - (2 * k) - len(tresult1) if calczz > 0: xtr = [] print('h') if len(Alice) > calczz: xtr = Alice[:calczz] else: xtr = Alice print('h') if len(Bob) > calczz: xtr = xtr + Bob[:calczz] else: xtr = xtr + Bob print('h') if len(none) > calczz: xtr = xtr + none[:calczz] else: xtr = xtr + none print('nai') print('h') xtr = xtr[:calczz] xtr.sort(key=lambda x: (x[1], x[2]), reverse=True) print('h') zz = sum(row[1] == row[2] == 0 for row in xtr) else: 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k, m; cin >> n >> m >> k; vector<pair<long long int, long long int> > common; vector<pair<long long int, long long int> > single_a; vector<pair<long long int, long long int> > single_b; vector<pair<long long int, long long int> > spare; for (long long int j = 0; j < n; j++) { long long int t, a, b; cin >> t >> a >> b; if (a == 0 && b == 0) spare.push_back({t, j + 1}); else if (a == 1 && b == 1) common.push_back({t, j + 1}); else if (a == 1 && b == 0) single_a.push_back({t, j + 1}); else single_b.push_back({t, j + 1}); } sort(common.begin(), common.end()); sort(single_a.begin(), single_a.end()); sort(single_b.begin(), single_b.end()); sort(spare.begin(), spare.end()); long long int combined = common.size() + min(single_a.size(), single_b.size()); if (combined < k) cout << -1 << "\n"; else { vector<long long int> ansv; vector<pair<long long int, pair<long long int, long long int> > > single_use; for (long long int j = 0; j < min(single_a.size(), single_b.size()); j++) single_use.push_back({single_a[j].first + single_b[j].first, {single_a[j].second, single_b[j].second}}); long long int p1 = 0; long long int p2 = 0; long long int ans = 0; long long int num = 0; while (k > 0) { if (p1 < common.size() && p2 < single_use.size()) { if (common[p1].first < single_use[p2].first) { ans = ans + common[p1].first; ansv.push_back(common[p1].second); p1++; num++; } else { ans = ans + single_use[p2].first; ansv.push_back(single_use[p2].second.first); ansv.push_back(single_use[p2].second.second); p2++; num += 2; } } else if (p1 == common.size()) { ans = ans + single_use[p2].first; ansv.push_back(single_use[p2].second.first); ansv.push_back(single_use[p2].second.second); p2++; num += 2; } else if (p2 == single_use.size()) { ans = ans + common[p1].first; ansv.push_back(common[p1].second); p1++; num++; } k--; } if (num <= m) { if (n == 19683) { cout << "YO " << num << " " << m << " " << p1 << " " << p2 << " " << endl; cout << common.size() << " " << spare.size() << endl; } long long int p2_a = p2; long long int p2_b = p2; long long int p_s = 0; vector<pair<long long int, long long int> > naya; for (long long int j = p1; j < common.size(); j++) naya.push_back(common[j]); for (long long int j = p2_a; j < single_a.size(); j++) naya.push_back(single_a[j]); for (long long int j = p2_b; j < single_b.size(); j++) naya.push_back(single_b[j]); for (long long int j = p_s; j < spare.size(); j++) naya.push_back(spare[j]); sort(naya.begin(), naya.end()); long long int index = 0; cout << ans << "\n"; for (long long int j = 0; j < ansv.size(); j++) cout << ansv[j] << " "; cout << "\n"; } else { if (n == 19683) cout << "HI" << "\n"; long long int diff = num - m; long long int available_common = common.size() - p1; if (diff > available_common) cout << -1 << "\n"; else { while (diff > 0) { p2--; ans = ans - single_use[p2].first; ans = ans + common[p1].first; p1++; diff--; } cout << ans << "\n"; for (long long int j = 0; j < p1; j++) cout << common[j].second << " "; for (long long int j = 0; j < p2; j++) cout << single_use[j].second.first << " " << single_use[j].second.second; cout << "\n"; } } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const long long P = 998244353; const long long MaxN = 2000010; const long double pi = 3.14159265358979323846264338; int main() { ios::sync_with_stdio(false); cin.tie(0); long long T = 1; for (long long q = 0; q < (T); q++) { long long n, k, a = 0, b = 0, ans = 0; cin >> n >> k; vector<pair<long long, long long> > u; for (long long i = 0; i < (n); i++) { long long t, x, y; cin >> t >> x >> y; if (x + y == 2) a++, b++, u.push_back({t, 2}); else if (x + y == 1) { if (x) a++, u.push_back({t, 1}); else b++, u.push_back({t, 0}); } } if (a < k || b < k) { cout << -1 << "\n"; continue; } sort(u.begin(), u.end()); a = 0, b = 0; long long pos = u.size() - 1; for (long long i = 0; i < (u.size()); i++) { if (u[i].second == 2) a++, b++; else if (u[i].second == 1) a++; else b++; ans += u[i].first; if (min(a, b) == k) { pos = i; break; } } if (a > b) { while (a != k) { if (u[pos].second == 1) ans -= u[pos].first, a--; pos--; } } else if (b > a) { while (b != k) { if (u[pos].second == 0) ans -= u[pos].first, b--; pos--; } } cout << ans << "\n"; } cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "s\n"; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class Book { public: int t, a, b, index; }; void solve() { int n, m, k; cin >> n >> m >> k; vector<Book> arr(n); int alice = 0, bob = 0; for (int i = 0; i < n; i++) { cin >> arr[i].t >> arr[i].a >> arr[i].b; arr[i].index = i + 1; alice += arr[i].a; bob += arr[i].b; } if (alice < k || bob < k) { cout << -1 << '\n'; return; } sort((arr).begin(), (arr).end(), [&](Book& A, Book& B) { return A.t == B.t ? A.a + A.b > B.a + B.b : A.t < B.t; }); vector<pair<int, int> > A, B; int cnt = 0; int ans = 0, pos = n; alice = k; bob = k; vector<bool> vis(n, false); for (int i = 0; i < n; i++) { if (alice <= 0 && bob <= 0) { pos = i; break; } if (arr[i].a == 0 && arr[i].b == 0) continue; if (alice > 0 && arr[i].a == 1 && arr[i].b == 0) { A.push_back({arr[i].t, i}); ans += arr[i].t; alice--; vis[i] = true; cnt++; continue; } if (bob > 0 && arr[i].b == 1 && arr[i].a == 0) { B.push_back({arr[i].t, i}); ans += arr[i].t; bob--; vis[i] = true; cnt++; continue; } if (arr[i].a == 1 && arr[i].b == 1) { alice--; bob--; ans += arr[i].t; vis[i] = true; cnt++; continue; } } int ja = 0, jb = 0; reverse((A).begin(), (A).end()); reverse((B).begin(), (B).end()); while (ja < (int)A.size() && alice < 0) { ans -= A[ja].first; vis[A[ja].second] = false; alice++; ja++; cnt--; } while (jb < (int)B.size() && bob < 0) { ans -= B[jb].first; vis[B[jb].second] = false; bob++; jb++; cnt--; } for (int i = pos; i < n; i++) { if (arr[i].a & arr[i].b) { if (ja < (int)A.size() && jb < (int)B.size() && ((A[ja].first + B[jb].first > arr[i].t) || (cnt > m))) { ans -= A[ja].first; ans -= B[jb].first; vis[A[ja].second] = false; vis[B[jb].second] = false; ans += arr[i].t; vis[i] = true; cnt--; ja++; jb++; } } } vector<int> res; int rem = m - cnt; if (rem < 0) { cout << -1 << '\n'; return; } for (int i = 0; i < n; i++) { if (rem == 0) break; if (!vis[i]) { ans += arr[i].t; rem--; vis[i] = true; } } for (int i = 0; i < n; i++) if (vis[i]) res.push_back(arr[i].index); cout << ans << '\n'; for (int u : res) cout << u << ' '; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, k, m, t, a, b, ab, c, no; long long int ans = 0; cin >> n >> m >> k; vector<pair<int, int> > alice, bob, alice_bob, none, res; for (int i = 1; i <= n; i++) { cin >> t >> a >> b; if (a == 1 && b == 1) alice_bob.push_back(make_pair(t, i)); else if (a == 1) alice.push_back(make_pair(t, i)); else if (b == 1) bob.push_back(make_pair(t, i)); else none.push_back(make_pair(t, i)); } long long int len_alice_bob = alice_bob.size(), len_alice = alice.size(), len_bob = bob.size(); if (k > len_alice_bob + len_alice || k > len_alice_bob + len_bob || m > len_alice_bob + len_bob + len_alice) cout << -1 << endl; else { sort(alice_bob.begin(), alice_bob.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); a = 0, b = 0, ab = 0, c = 0; while (k--) { c++; if (ab < len_alice_bob) { if (a < len_alice && b < len_bob) { if (alice_bob[ab].first > alice[a].first + bob[b].first && c + 1 < m) { c++; res.push_back(make_pair(alice[a].first, alice[a].second)); res.push_back(make_pair(bob[b].first, bob[b].second)); ans = ans + alice[a++].first + bob[b++].first; } else { res.push_back(make_pair(alice_bob[ab].first, alice_bob[ab].second)); ans = ans + alice_bob[ab++].first; } } else { res.push_back(make_pair(alice_bob[ab].first, alice_bob[ab].second)); ans = ans + alice_bob[ab++].first; } } else { res.push_back(make_pair(alice[a].first, alice[a].second)); res.push_back(make_pair(bob[b].first, bob[b].second)); ans = ans + alice[a++].first + bob[b++].first; } } c = res.size(); if (c < m) { c = m - c; while (a < len_alice) { none.push_back(make_pair(alice[a].first, alice[a].second)); a++; } while (b < len_bob) { none.push_back(make_pair(bob[b].first, bob[b].second)); b++; } while (ab < len_alice_bob) { none.push_back(make_pair(alice_bob[ab].first, alice_bob[ab].second)); ab++; } int len_none = none.size(); sort(none.begin(), none.end()); no = 0; while (c--) { ans = ans + none[no].first; res.push_back(make_pair(none[no].first, none[no].second)); no++; } } cout << ans << endl; for (int i = 0; i < m; i++) cout << res[i].second << " "; 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
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class hello2 {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; } } static String sum (String s) { String s1 = ""; if(s.contains("a")) s1+="a"; if(s.contains("e")) s1+="e"; if(s.contains("i")) s1+="i"; if(s.contains("o")) s1+="o"; if(s.contains("u")) s1+="u"; return s1; } public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<String, Integer> > list = new LinkedList<Map.Entry<String, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } public static void main(String args[]) { FastReader input =new FastReader(); int n = input.nextInt(); int k = input.nextInt(); int arr[][] = new int[n][3]; int arr1[][] = new int[n][3]; int arr2[][] = new int[n][3]; for(int i=0;i<n;i++) { arr[i][0] = input.nextInt(); arr[i][1] = input.nextInt(); arr[i][2] = input.nextInt(); if(arr[i][1]==arr[i][2] && arr[i][1]==1) { } else { arr1[i][0] = arr[i][0]; arr1[i][1] = arr[i][1]; arr1[i][2] = arr[i][2]; arr2[i][0] = arr[i][0]; arr2[i][1] = arr[i][1]; arr2[i][2] = arr[i][2]; } } Arrays.sort(arr1, (a, b) -> Integer.compare(a[0], b[0])); Arrays.sort(arr1, (a, b) -> Integer.compare(b[1], a[1])); Arrays.sort(arr2, (a, b) -> Integer.compare(a[0], b[0])); Arrays.sort(arr2, (a, b) -> Integer.compare(b[2], a[2])); Arrays.sort(arr, (a, b) -> Integer.compare(b[2], a[2])); Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0])); Arrays.sort(arr, (a, b) -> Integer.compare(b[1], a[1])); // // for(int i=0;i<n;i++) // { // // System.out.println(arr1[i][0] + " " + arr1[i][1] + " " + arr1[i][2]); // } // // System.out.println(); // // // for(int i=0;i<n;i++) // { // System.out.println(arr2[i][0] + " " + arr2[i][1] + " " + arr2[i][2]); // } // int count =0; int count1 =0; int count2 =0; long sum =0; for(int i=0;i<n;i++) { if(arr[i][1]==1 && arr[i][2]==1) count2++; else { if(arr[i][1]==1) { count++; } if(arr[i][2]==1) count1++; } } // System.out.println(); // // System.out.println(count+" " + count1 + " " + count2); // System.out.println(); // if(count+count2<k || count1+count2<k) { System.out.println(-1); } else { int i=0; int j=0; sum =0; int k1 = k,k2 =k; int l=0; while(k1>0 && k2>0) { if((arr[i][0] > (arr1[j][0] + arr2[j][0]) && count>0 && count1>0)|| count2==0) { k1-=1; k2-=1; sum+=(arr1[j][0] + arr2[j][0]); j++; count--; count1--; } else if(count2>0) { k1-=1; k2-=1; sum+=arr[i][0]; i++; count2--; } // System.out.println(sum + " "+ count+" " + count1 + " " + count2); } System.out.println(sum); } } // //Arrays.sort(myArr, (a, b) -> Double.compare(a[0], b[0])); //Arrays.sort(contests, (a, b) -> Integer.compare(b[0],a[0])); decreasing order public static boolean range(int x,int y,int n,int m) { if(x<0 || x>n-1 || y<0 || y>m-1) return false; return true; } public static int even_element(int b[],int n) { int c[] = new int[n/2 + 1]; int l=0; for(int i=1;i<n;i+=2) { c[l] = b[i]; System.out.print(c[l] + " "); l++; } System.out.println(); if(c.length!=1) { return even_element(c,n/2); } else return c[0]; } static boolean isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } } class Pair { int a; int b; Pair(int a,int b) { this.a=a; this.b=b; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class GFG { private static int i,j,k,l,n,m,books; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // int t = Integer.parseInt(br.readLine().trim()); // while (t-- != 0) { // int n = Integer.parseInt(br.readLine().trim()); // char c[]=br.readLine().trim().toCharArray(); StringTokenizer st = new StringTokenizer(br.readLine().trim()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); int kk = Integer.parseInt(st.nextToken()); ArrayList<int[]> A[]=new ArrayList[4]; for(i=0;i<4;i++) A[i]=new ArrayList<>(); int ans=0; for(i=0;i<n;i++){ st = new StringTokenizer(br.readLine().trim()); int time=Integer.parseInt(st.nextToken()); int al=Integer.parseInt(st.nextToken()); int bo=Integer.parseInt(st.nextToken()); int a[]=new int[]{time,i+1}; if(al==1&&bo==0) A[0].add(a); else if(al==0&&bo==1) A[1].add(a); else if(al==1&&bo==1) A[2].add(a); else A[3].add(a); } if(A[0].size()+A[2].size()<kk||A[1].size()+A[2].size()<kk) out.println(-1); else{ for(i=0;i<4;i++) Collections.sort(A[i],(b,c)->b[0]-c[0]); // out.println(A[0]); // out.println(A[1]); // out.println(A[2]); // out.println(A[3]); i=0;j=0;k=0;l=0; int cnt=0; books=0; while(i<A[0].size()&&j<A[1].size()&&k<A[2].size()&&cnt!=kk) { int sum = A[0].get(i)[0] + A[1].get(j)[0],c=A[2].get(k)[0]; if (sum < c) { ans += sum; books+=2; i++; j++; } else { ans += c; books++; k++; } cnt++; } if(i==A[0].size()||j==A[1].size()){ while(cnt!=kk){ cnt++; books++; ans+=A[2].get(k++)[0]; } } else if(k==A[2].size()){ while(cnt!=kk){ cnt++; books+=2; ans+=A[0].get(i++)[0] + A[1].get(j++)[0]; } } while(books>m) { if (k == A[2].size() || i == 0 || j == 0) { ans = -1; break; } books --; ans = ans - A[0].get(--i)[0] - A[1].get(--j)[0] + A[2].get(k++)[0]; } if(books<m) ans+=find(A); if(n==19683&&m==507&&kk==254) out.println(books+" "+cnt); out.println(ans); if(ans!=-1){ for(int y=0;y<i;y++) out.print(A[0].get(y)[1]+" "); for(int y=0;y<j;y++) out.print(A[1].get(y)[1]+" "); for(int y=0;y<k;y++) out.print(A[2].get(y)[1]+" "); for(int y=0;y<l;y++) out.print(A[3].get(y)[1]+" "); out.println(); } } out.close(); } private static int find(ArrayList<int[]>[] A) { int ans=0; while(books!=m){ books++; int a=Integer.MAX_VALUE,b=Integer.MAX_VALUE,c=Integer.MAX_VALUE,d=Integer.MAX_VALUE; if(i<A[0].size()){ a=A[0].get(i)[0]; } if(j<A[1].size()){ b=A[1].get(j)[0]; } if(k<A[2].size()){ c=A[2].get(k)[0]; } if(l<A[3].size()){ d=A[3].get(l)[0]; } int min=Math.min(a,Math.min(b,Math.min(c,d))); if(min==a) i++; else if(min==b) j++; else if(min==c) k++; else l++; ans+=min; } return ans; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.*; import java.io.*; public class Solution { static class Book { int t; int a, b; Book(int tt,int aa,int bb) { t = tt; a = aa; b = bb; } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub F scn = new F(System.in); PrintWriter pw = new PrintWriter(System.out); // int t = scn.nextInt(); // while(t-- > 0 ) { int n = scn.nextInt(); int k = scn.nextInt(); Book b[] = new Book[n]; int ax = 0, bx = 0; for(int i=0;i<n;i++) { b[i] = new Book(scn.nextInt(), scn.nextInt(), scn.nextInt()); if(b[i].a == 1) ax++; if(b[i].b == 1) bx++; } if(ax < k || bx < k) { pw.println(-1); } else pw.println(ans(b,k)); // } pw.flush(); } public static long ans(Book[] a,int k) { Arrays.sort(a,new Comparator<Book>() { public int compare(Book a,Book b) { return a.t - b.t; } }); int ax = 0, bx = 0; long ans = 0; boolean d[] = new boolean[a.length]; for(int i=0;ax < k && i<a.length;i++) { if(a[i].a == 1) { if(a[i].b == 1) bx++; ax++; ans += a[i].t; d[i] = true; } } for(int i=0;i<a.length && bx < k;i++) { if(!d[i] && a[i].b == 1) { bx++; ans += a[i].t; } } return ans; } public static long ans(int a[],int k) { HashMap<Integer,Integer> m = new HashMap<>(); long max = 0, mf = 0; for(int i : a) { int x = k - i%k; if(x != k) { m.put(x, m.getOrDefault(x, 0) + 1); if(mf < m.get(x)) { mf = m.get(x); max = x; } else if(mf == m.get(x) && x > max) { max = x; } } } // System.out.println(m); if(m.size() == 0) return 0; return (mf-1)*k + max + 1L; } public static int ans(String s) { int c = 0; int min = Integer.MAX_VALUE; for(int i=0;i<s.length();i++) { if(s.charAt(i) == '(') c++; else c--; if(min > c) min = c; } return Math.abs(min); } public static int ans(int n) { int d[] = three(n); if(d[2] != 1) return -1; if(d[1] > d[0]) return -1; return d[0] + d[0] - d[1]; } public static int[] three(int x) { int c3 = 0, c2 = 0; int y = x; while(x > 0 && x%3 == 0) { x /= 3; c3++; } while(x > 0 && x%2 == 0) { x /= 2; c2++; } return new int[] {c3,c2,x}; } static class F { StringTokenizer st; BufferedReader br; public F(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public F(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[] nextIntArray(int n) throws IOException { int[] in = new int[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); return in; } public long[] nextLongArray(int n) throws IOException { long[] in = new long[n]; for (int i = 0; i < n; i++) in[i] = nextLong(); return in; } public int[] nextIntSortedArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); Arrays.sort(a); return a; } public long[] longSortedArr(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); Arrays.sort(a); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] in = new Integer[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); return in; } public Long[] nextLongArray1(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); } final static long M = 1000000007L; final static long power(long base, long exp) { long ans = 1; while(exp > 0) { if(exp%2 == 1) ans = (ans * base) % M; exp/=2; base = (base * base) % M; } return ans; } final static long gcd(long a,long b) { return b == 0 ? a : gcd(b, a%b); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int power(long long int t, long long int x) { long long int ans = 1; while (t > 0) { if (t % 2 == 1) ans = (ans * x) % 1000000007; t = t / 2; x = (x * x) % 1000000007; } return ans; } long long int maxSubArraySum(vector<long long int> a, long long int size) { long long int max_so_far = 0, max_ending_here = 0; for (long long int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_ending_here < 0) max_ending_here = 0; else if (max_so_far < max_ending_here) max_so_far = max_ending_here; } return max_so_far; } int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, k; cin >> n >> k; vector<long long int> v, a, b; ; for (long int i = 0; i < n; i++) { long long int t, p, q; cin >> t >> p >> q; if (p == 1 && q == 1) v.push_back(t); else if (p == 1 && q == 0) a.push_back(t); else if (q == 1 && p == 0) b.push_back(t); } long long int z = (a.size() > b.size() ? b.size() : a.size()); sort(v.begin(), v.end()); sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (k > (v.size() + z)) { cout << "-1"; return 0; } long long int i = 0, j = 0, time = 0; while (i < (k > v.size() ? v.size() : k)) { time += v[i]; i++; } while (j < (k - i > z ? z : k - i)) { time += (a[j] + b[j]); j++; } i = v.size() - 1; while (j < z) { while (i >= 0 && v[i] <= (a[j] + b[j])) i--; if (i < 0) break; time += (a[j] + b[j] - v[i]); i--; j++; } 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
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.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
java
import java.io.*; import java.util.*; public class e1 { public static PrintWriter out; public static InputReader in; public static void makePre(Pair arr[], int ln) { int s = 0; for(int i=0;i<ln;i++) { s+=arr[i].a; arr[i].a=s; } } static class Pair{ public int a,b; public Pair(int a, int b){ this.a=a; this.b=b; } } public static void main(String[] args)throws IOException { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); Pair bob[] = new Pair[n]; Pair alice[] = new Pair[n]; Pair com[] = new Pair[n]; ArrayList<Pair> other = new ArrayList<Pair>(); for(int i=0;i<n;i++) { bob[i] = new Pair(-1,-1); com[i] = new Pair(-1,-1); alice[i] = new Pair(-1,-1); } int b=0,a=0,c=0; for(int t = 0; t < n; t++){ int ti = in.nextInt(); int ai = in.nextInt(); int bi = in.nextInt(); if(ai==1 && bi==1) { com[c++]=new Pair(ti,t); } else if(ai==1 && bi==0) { alice[a++]=new Pair(ti,t); } else if(ai==0 && bi==1) { bob[b++]=new Pair(ti,t); } else { other.add(new Pair(ti,t)); } } mergesort(alice,0,a-1); mergesort(bob,0,b-1); mergesort(com,0,c-1); makePre(com,c); makePre(bob,b); makePre(alice,a); int ans = Integer.MAX_VALUE; int aopt = -1, bopt=-1, copt=-1; for(int i=0;i<=k;i++) { if(i>0 && com[i-1].a==-1) continue; int sm=0; if(i>0) sm = com[i-1].a; if(i==k){ if(sm<ans) { aopt = 0; bopt=0; copt=i; } ans = Math.min(ans,sm); } else { if(k-i>b) continue; sm+=bob[k-i-1].a; if(k-i>a) continue; sm+=alice[k-i-1].a; } if((k-i+k-i+i)>m) continue; if(sm<ans) { aopt = k-i; bopt=k-i; copt=i; } ans = Math.min(ans,sm); } if(ans==Integer.MAX_VALUE) out.println(-1); else{ for(int i=aopt;i<n;i++) { if(alice[i].a<0) break; if(i==0) other.add(alice[i]); else other.add(new Pair(alice[i].a-alice[i-1].a,alice[i].b)); } for(int i=bopt;i<n;i++) { if(bob[i].a<0) break; if(i==0) other.add(bob[i]); else other.add(new Pair(bob[i].a-bob[i-1].a,bob[i].b)); } for(int i=copt;i<n;i++) { if(com[i].a<0) break; if(i==0) other.add(com[i]); else other.add(new Pair(com[i].a-com[i-1].a,com[i].b)); } // out.println("Copt "+copt+" bopt "+bopt+" aopt "+aopt); Collections.sort(other,(p1,p2) -> p1.a-p2.a); // out.println(other); for(int i=0;i<m-(copt+bopt+aopt);i++) { ans+=other.get(i).a; } out.println(ans); for(int i=0;i<copt;i++){ out.print((com[i].b+1)+" "); } for(int i=0;i<bopt;i++){ out.print((alice[i].b+1)+" "); out.print((bob[i].b+1)+" "); } for(int i=0;i<m-(copt+bopt+aopt);i++){ out.print((other.get(i).b+1)+" "); } out.println(); } out.close(); } public static void merge(Pair[] arr, int first,int mid,int last){ Pair a[]=new Pair[mid-first+1]; Pair b[]=new Pair[last-mid]; // int c[]=new int[mid-first+1]; // int d[]=new int[last-mid]; int pahela,dusra; pahela=dusra=0; for(pahela=0;pahela<mid-first+1;pahela++) { a[pahela]=arr[first+pahela]; // c[pahela]=indexes[first+pahela]; } for(dusra=0;dusra<last-mid;dusra++) { b[dusra]=arr[mid+1+dusra]; // d[dusra]=indexes[mid+1+dusra]; } pahela=0; dusra=0; int maha=first; while(pahela<mid-first+1 && dusra<last-mid) { if(a[pahela].a<=b[dusra].a) { arr[maha]=a[pahela]; // indexes[maha]=c[pahela]; maha++; pahela++; } else { arr[maha]=b[dusra]; // indexes[maha]=d[dusra]; maha++; dusra++; } } while(pahela<mid-first+1) { arr[maha]=a[pahela]; // indexes[maha]=c[pahela]; maha++; pahela++; } while(dusra<last-mid){ arr[maha]=b[dusra]; // indexes[maha]=d[dusra]; maha++; dusra++; } } public static void mergesort(Pair arr[],int first,int last) { if(first < last) { int mid=(first+last)/2; mergesort(arr,first,mid); mergesort(arr,mid+1,last); merge(arr,first,mid,last); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdtr1c++.h> #define pb push_back #define fi first #define sc second #define inf 10000000000LL #define MP make_pair #define N 100005 #define MOD 998244353 #define orta ((x+y)/2) using namespace std; typedef long long int lint; typedef pair<lint,lint> ii; int n,m,k; void solve(int ts) { cin >> n >> m >> k; vector< ii > t[5]; priority_queue<ii,vector<ii>,greater<ii>> q,yed; priority_queue<ii> have,zero; for (int i = 0; i < n; i++) { int o1,o2,o3; cin >> o1 >> o2 >> o3; if(o2 && o3) t[1].pb({o1,i+1}); else if(o2) t[2].pb({o1,i+1}); else if(o3) t[3].pb({o1,i+1}); else q.push({o1,i+1}); } sort(t[1].begin(),t[1].end()); sort(t[2].begin(),t[2].end()); sort(t[3].begin(),t[3].end()); if(m<k) { cout << -1 << endl; return ; } for (int i = t[1].size(); i < m; i++) t[1].pb({inf,0}); for (int i = t[2].size(); i < m; i++) t[2].pb({inf,0}); for (int i = t[3].size(); i < m; i++) t[3].pb({inf,0}); for (int i = k; i < m; i++) q.push(t[2][i]); for (int i = k; i < m; i++) q.push(t[3][i]); for (int i = q.size(); i < m; i++) q.push({inf,0}); yed=q; // lint ans=inf,cur=0; for (int i = 0; i < k; i++) cur+=t[2][i].first; for (int i = 0; i < k; i++) cur+=t[3][i].first; for (int i = 0; i < m-2*k; i++) { cur+=q.top().first; have.push(q.top()); q.pop(); } int cnt=max(2*k,m); if(cnt==m) ans=min(ans,cur); for (int i1 = 0; i1 < k; i1++) { cur+=t[1][i1].first; cur-=t[2][k-i1-1].first; cur-=t[3][k-i1-1].first; q.push(t[2][k-i1-1]); q.push(t[3][k-i1-1]); cnt--; while(cnt<m) { cnt++; cur+=q.top().first; have.push(q.top()); q.pop(); } while(have.empty()==false && q.top().first<have.top().first) { cur-=have.top().first; cur+=q.top().first; have.push(q.top()); q.pop(); } if(cnt==m) ans=min(ans,cur); } if(ans>=inf) { cout << -1 << endl; return; } cout << ans << endl; // q=yed; have=zero; cur=0; for (int i = 0; i < k; i++) cur+=t[2][i].first; for (int i = 0; i < k; i++) cur+=t[3][i].first; for (int i = 0; i < m-2*k; i++) { cur+=q.top().first; have.push(q.top()); q.pop(); } cnt=max(2*k,m); if(cnt==m) { if(ans==cur) { for (int i = 0; i < k; i++) cout << t[2][i].second << " " << t[3][i].second << " " ; while(have.empty()==false) { cout << have.top().second << " "; have.pop(); } return ; } } for (int i1 = 0; i1 < k; i1++) { cur+=t[1][i1].first; cur-=t[2][k-i1-1].first; cur-=t[3][k-i1-1].first; q.push(t[2][k-i1-1]); q.push(t[3][k-i1-1]); cnt--; while(cnt<m) { cnt++; cur+=q.top().first; have.push(q.top()); q.pop(); } while(have.empty()==false && q.top().first<have.top().first) { cur-=have.top().first; cur+=q.top().first; have.push(q.top()); q.pop(); } if(cnt==m) { if(ans==cur) { for (int i = 0; i <= i1; i++) cout << t[1][i].second << " "; for (int i = 0; i < k-i1-1; i++) cout << t[2][i].second << " " << t[3][i].second << " "; while(have.empty()==false) { cout << have.top().second << " "; have.pop(); } return; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; for (int i = 0; i < t; i++) solve(i+1); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; public class ReadingBooksE { @SuppressWarnings("resource") public static void main(String [] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int k = s.nextInt(); int output = 0; ArrayList<Integer> Alice = new ArrayList<Integer>(); ArrayList<Integer> Bob = new ArrayList<Integer>(); ArrayList<Integer> intersect = new ArrayList<Integer>(); int sumIntersect = 0; for (int i = 0 ; i< n ; i++) { int time = s.nextInt(); int alice = s.nextInt(); int bob = s.nextInt(); if(alice == 1 && bob == 1) { intersect.add(time); sumIntersect += time; } else if(alice == 1 && bob == 0) { Alice.add(time); } else if(alice == 0 && bob == 1) { Bob.add(time); } } s.close(); if (Bob.size() + intersect.size() < k || Alice.size() + intersect.size() < k) { System.out.println(-1); return; } if (intersect.size() >= k && Alice.isEmpty() && Bob.isEmpty()) { System.out.println(intersect.stream().mapToInt(Integer::intValue).sum()); return; } Collections.sort(intersect); Collections.sort(Alice); Collections.sort(Bob); for (int i =0 ; i<k ;i++) { if(!intersect.isEmpty() && !Alice.isEmpty() && !Bob.isEmpty()) { if (Alice.get(0)+Bob.get(0) > intersect.get(0)) output += intersect.remove(0); else { output+= Alice.remove(0); output+= Bob.remove(0); } } else if (!intersect.isEmpty() && (Alice.isEmpty() || Bob.isEmpty())) { output += intersect.remove(0); } else if (intersect.isEmpty()) { output+= Alice.remove(0); output+= Bob.remove(0); } } System.out.println(output); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\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 int BIG = 1e5 + 55; const int BIGG = 1e9 + 7; const long long BIGGE = 1e12 + 55; const double SML = (1e-7); using namespace std; long long n, k, sum; vector<int> v, a, b; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { int x, y, z; cin >> x >> y >> z; if (y && z) v.push_back(x); else if (y) a.push_back(x); else if (z) b.push_back(x); } sort(v.begin(), v.end()); sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (min(a.size(), b.size()) + v.size() >= k) { int l = 1, i = 0, x = 0; for (; i < a.size() && i < b.size(); l++) { if (x < v.size()) { if (v[x] < a[i] + b[i]) { sum += v[x]; x++; } else { sum += a[i] + b[i]; i++; } } else { sum += a[i] + b[i]; i++; } if (l == k) break; } if (l == k) cout << sum; else { for (; l <= k; x++, l++) sum += v[x]; cout << sum; } } else cout << -1; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import sys def input(): return sys.stdin.readline().rstrip() def input_split(): return [int(i) for i in input().split()] # testCases = int(input()) # answers = [] # for _ in range(testCases): #take input n, k = input_split() times = [] alice_likes = [] bob_likes = [] for _ in range(n): t, a, b = input_split() times.append(t) alice_likes.append(a) bob_likes.append(b) if (sum(alice_likes) < k or sum(bob_likes)< k): ans = -1 else: #worst case choose all, but possible times_both = [] times_alice = [] times_bob = [] for b in range(n): if alice_likes[b] == 1 and bob_likes[b] == 1: times_both.append(times[b]) elif alice_likes[b] == 1: times_alice.append(times[b]) else: times_bob.append(times[b]) times_both.sort() times_alice.sort() times_bob.sort() times_both = times_both + [100000]*(n- len(times_both)) times_alice = times_alice + [100000]*(n- len(times_alice)) times_bob = times_bob + [100000]*(n- len(times_bob)) ans = 0 p1, p2, p3 = 0, 0, 0 for i in range(k): if (times_both[p1] <= times_alice[p2] + times_bob[p3]): ans += times_both[p1] p1 += 1 else: ans += times_alice[p2] + times_bob[p3] p2 += 1 p3 += 1 # times_bob print(ans) # answers.append(ans) # print(*answers, sep = '\n')
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
import io import os from collections import Counter, defaultdict, deque def solve(N, K, books): bothLikes = [] aLikes = [] bLikes = [] for t, a, b in books: if a and b: bothLikes.append(t) elif a: aLikes.append(t) elif b: bLikes.append(t) if len(bothLikes) + len(aLikes) < K or len(bothLikes) + len(bLikes) < K: return -1 bothLikes = sorted(bothLikes, reverse=True)[:K] pref = [0] for x in bothLikes: pref.append(pref[-1] + x) maxIndiv = min(K, len(aLikes), len(bLikes)) aLikes = sorted(aLikes, reverse=True)[:maxIndiv] bLikes = sorted(bLikes, reverse=True)[:maxIndiv] best = float('inf') indivTime = 0 for i in range(maxIndiv + 1): if K - i < len(pref): best = min(best, indivTime + pref[K - i]) if i != maxIndiv: indivTime += aLikes[i] + bLikes[i] return best if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline N, K = [int(x) for x in input().split()] books = [[int(x) for x in input().split()] for i in range(N)] ans = solve(N, K, books) print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int mod2 = 998244353; long long exp(long long taban, long long us, long long md) { long long carpan = taban % md; if (carpan == 0) return 0; long long temp = us; long long res = 1; while (temp) { if (temp % 2) res = (res * carpan) % md; temp /= 2; carpan = (carpan * carpan) % md; } return res; } long long ebob(long long a, long long b) { if (!a) return b; return ebob(b % a, a); } long long ekok(long long a, long long b) { return (a * b) / ebob(a, b); } vector<long long> fact; vector<long long> inv_fact; void fact_init(int n) { fact.resize(n + 5); inv_fact.resize(n + 5); fact[0] = inv_fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = (fact[i - 1] * i) % mod; inv_fact[i] = exp(fact[i], mod - 2, mod); } } long long komb(long long a, long long b) { if (a < b) return 0; return fact[a] * (inv_fact[a - b] * inv_fact[b] % mod) % mod; } const int N = 2e5 + 5; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; vector<int> w[3]; int ac = 0, bc = 0; int tot = 0; for (int i = 0; i < n; i++) { int t, a, b; cin >> t >> a >> b; if (!a && !b) continue; w[2 * b + a - 1].push_back(t); ac += a, bc += b; tot += t; } if (ac < k || bc < k) { cout << -1; return 0; } int m = w[0].size() + w[1].size() + w[2].size(); int s0 = w[0].size(), s1 = w[1].size(), s2 = w[2].size(); sort((w[0]).rbegin(), (w[0]).rend()), sort((w[1]).rbegin(), (w[1]).rend()), sort((w[2]).rbegin(), (w[2]).rend()); int ptr0 = 0, ptr1 = 0, ptr2 = 0; while (ac > k || bc > k) { if (ptr2 == s2) { bool check = 0; if (ac > k) { tot -= w[0][ptr0++]; check = 1; } if (bc > k) { tot -= w[1][ptr1++]; bc--; check = 1; } if (check) continue; } if (ac > k && bc > k) { bool yap = false; yap |= ptr0 == s0; yap |= ptr1 == s1; yap &= ptr2 < s2; yap |= (ptr0 < s0 && ptr1 < s1 && ptr2 < s2 && w[0][ptr0] + w[1][ptr1] <= w[2][ptr2]); if (yap) { tot -= w[2][ptr2++]; ac--; bc--; continue; } } if (ac > k) { tot -= w[0][ptr0++]; ac--; } if (bc > k) { tot -= w[1][ptr1++]; bc--; } } cout << tot; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void fun() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } void solve() { fun(); long long t = 1; while (t--) { long long n, k; cin >> n >> k; long long t[n], a[n], b[n]; for (long long i = 0; i < n; i++) cin >> t[i] >> a[i] >> b[i]; vector<long long> al, bl, bt; for (long long i = 0; i < n; i++) { if (a[i] && b[i]) bt.push_back(t[i]); else if (a[i]) al.push_back(t[i]); else if (b[i]) bl.push_back(t[i]); } if (((al.size() + bt.size()) < k) || ((bl.size() + bt.size()) < k)) { cout << "-1\n"; continue; } sort(al.begin(), al.end()); sort(bl.begin(), bl.end()); sort(bt.begin(), bt.end()); set<long long> s; long long ca = 0, cb = 0, ans = 0, x = 0, z = 0; for (long long i = 0; i < min(k, (long long)bt.size()); i++) ans += bt[i], ca++, cb++; for (long long i = min((long long)(bt.size() - 1), k); i >= 0; i--) { if (x < al.size() && z < bl.size()) { long long v = al[x] + bl[z]; if (v < bt[i]) { ans -= bt[i]; ans += v; s.insert(bt[i]); x++, z++; continue; } if (v > bt[i]) break; } } for (long long i = x; i < x + (k - ca) && i < al.size() && i < bl.size(); i++) ans += (al[i] + bl[i]), ca++; for (auto i : s) { if (ca < k) ans += i, ca++; else break; } cout << ans << "\n"; } } signed main() { solve(); return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
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] # print(books_both) def can_do(time): for i in range(min(k + 1, len(books_both) + 1)): # print(i) both_books_time = prefx_both[i] left_nums = k - i if left_nums < 0 or 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 = 0 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
java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Set; import java.util.StringTokenizer; public class Solution { static final FS sc = new FS(); // 封装输ε…₯η±» static final PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int n = sc.nextInt(), k = sc.nextInt(); PriorityQueue<Integer> a = new PriorityQueue<>(); PriorityQueue<Integer> b = new PriorityQueue<>(); PriorityQueue<Integer> ab = new PriorityQueue<>(); for(int i = 0;i < n;i++) { int t = sc.nextInt(), ai = sc.nextInt(), bi = sc.nextInt(); if (ai == 1 && bi == 1) { ab.add(t); } else if (ai == 1) { a.add(t); } else { b.add(t); } } int countA = 0, countB = 0, res = 0; while(countA < k || countB < k) { if (!ab.isEmpty() && !a.isEmpty() && !b.isEmpty()) { if (ab.peek() <= a.peek() + b.peek()) { res += ab.poll(); } else { res += a.poll() + b.poll(); } countA++; countB++; } else if (!ab.isEmpty()) { res += ab.poll(); countA++; countB++; } else if (!a.isEmpty()) { res += a.poll(); countA++; } else if (!b.isEmpty()) { res += b.poll(); countB++; } else { break; } } if (countA < k || countB < k) { pw.println(-1); } else { pw.println(res); } pw.flush(); } static class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(Exception ignored) {} } return st.nextToken(); } int[] nextArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++) { a[i] = nextInt(); } return a; } int nextInt() { return Integer.parseInt(next()); } 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; using ll = long long; using Vi = vector<int>; using Vl = vector<ll>; using Pii = pair<int, int>; using Pll = pair<ll, ll>; constexpr int I_INF = numeric_limits<int>::max(); constexpr ll L_INF = numeric_limits<ll>::max(); void solve() { ll N, M, K; cin >> N >> M >> K; vector<Pll> ba, bb, bab, bno; for (ll i = 0; i < N; i++) { ll t, a, b; cin >> t >> a >> b; if (a == 1 && b == 1) { bab.emplace_back(t, i); } else if (a == 1) { ba.emplace_back(t, i); } else if (b == 1) { bb.emplace_back(t, i); } else { bno.emplace_back(t, i); } } sort(bab.begin(), bab.end()); sort(ba.begin(), ba.end()); sort(bb.begin(), bb.end()); sort(bno.begin(), bno.end()); ll nab = (ll)bab.size(); ll na = (ll)ba.size(); ll nb = (ll)bb.size(); ll nno = (ll)bno.size(); ll start = max(K - min(na, nb), 0ll); start = max(start, K * 2 - M); if (start > nab) { cout << -1 << "\n"; return; } ll sab = 0, sa = 0, sb = 0; ll sr = 0; for (ll i = 0; i < start; i++) { sab += bab[i].first; } for (ll i = 0; i < K - start; i++) { sa += ba[i].first; sb += bb[i].first; } set<ll> st, st_used; for (ll i = K - start; i < na; i++) { st.emplace(ba[i].first); } for (ll i = K - start; i < nb; i++) { st.emplace(bb[i].first); } for (ll i = 0; i < nno; i++) { st.emplace(bno[i].first); } { ll cnt = 0; while (cnt < M - (K * 2 - start)) { auto itr = st.begin(); sr += *itr; st_used.emplace(*itr); st.erase(itr); ++cnt; } } ll idx_mn = -1; ll s_mn = L_INF; for (ll i = start; i <= nab; i++) { ll t = sab + sa + sb + sr; if (t < s_mn) { s_mn = t; idx_mn = i; } if (i == nab) continue; ll d = max(K - i, 0ll); sab += bab[i].first; if (d > 0) { sa -= ba[d - 1].first; sb -= bb[d - 1].first; st.emplace(ba[d - 1].first); st.emplace(bb[d - 1].first); while (*st_used.rbegin() > *st.begin()) { auto itr = st.begin(); st_used.emplace(*itr); sr += *itr; st.erase(itr); auto itr2 = st_used.rbegin().base(); st.emplace(*itr2); sr -= *itr2; st_used.erase(itr2); } } st_used.emplace(*st.begin()); sr += *st.begin(); st.erase(st.begin()); } Vl ans; for (ll i = 0; i < idx_mn; i++) { ans.emplace_back(bab[i].second); } for (ll i = 0; i < K - idx_mn; i++) { ans.emplace_back(ba[i].second); ans.emplace_back(bb[i].second); } set<Pll> stt; for (ll i = K - idx_mn; i < na; i++) { stt.emplace(ba[i]); } for (ll i = K - idx_mn; i < nb; i++) { stt.emplace(bb[i]); } for (ll i = 0; i < nno; i++) { stt.emplace(bno[i]); } { auto itr = stt.begin(); for (ll i = 0; i < M - (K * 2 - idx_mn); i++) { ans.emplace_back((*itr).second); ++itr; } } cout << s_mn << "\n"; for (const auto& v : ans) { cout << v + 1 << " "; } cout << "\n"; } int main() { cin.tie(0), cout.tie(0); ios::sync_with_stdio(false); solve(); cout << flush; 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 Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); String s[]=bu.readLine().split(" "); int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]); ArrayList<Integer> ab=new ArrayList<>(); ArrayList<Integer> a=new ArrayList<>(); ArrayList<Integer> b=new ArrayList<>(); int i,al=0,bo=0,x,y,z; for(i=0;i<n;i++) { s=bu.readLine().split(" "); x=Integer.parseInt(s[0]); y=Integer.parseInt(s[1]); z=Integer.parseInt(s[2]); if(y==1) al++; if(z==1) bo++; if(y==1 && z==1) ab.add(x); else if(y==1) a.add(x); else b.add(x); } if(al<k || bo<k) {System.out.print("-1"); return;} Collections.sort(ab); Collections.sort(a); Collections.sort(b); ArrayList<Integer> alb=new ArrayList<>(); for(i=0;i<Math.min(a.size(),b.size());i++) alb.add(a.get(i)+b.get(i)); Collections.sort(alb); long min=0,c=0; if(alb.size()==0) { for(i=0;i<k;i++) min+=ab.get(i); System.out.print(min); return; } if(ab.size()==0) { for(i=0;i<k;i++) min+=alb.get(i); System.out.print(min); return; } x=0; y=0; while(x<ab.size() && y<alb.size() && c<k) { if(ab.get(x)<=alb.get(y)) {min+=ab.get(x); x++;} else {min+=alb.get(y); y++;} c++; } if(c==k) {System.out.print(min); return;} while(x<ab.size() && c<k) { min+=ab.get(x); x++; c++; } while(y<alb.size() && c<k) { min+=alb.get(y); y++; c++; } System.out.print(min); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; vector<long long int> a, b, c; for (int i = 0; i < n; i++) { int t, x, y; cin >> t >> x >> y; if (x && y) c.push_back(t); else if (x && !y) a.push_back(t); else if (!x && y) b.push_back(t); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long ans = 0; int an = a.size(), bn = b.size(), cn = c.size(); if (an + cn >= k && bn + cn >= k) { while ((int)a.size() > 0 && (int)b.size() > 0) { c.push_back(a.back() + b.back()); a.pop_back(); b.pop_back(); } sort(c.begin(), c.end()); for (int i = 0; i < k; i++) { ans += c[i]; } } else 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
java
import java.io.*; import java.util.*; public class Main { private static long testCase(int n, int k, PriorityQueue<Integer> books) { long ans = 0; for (int i = 0; i < k; i++) { if (books.isEmpty()) { return -1; } else { ans += books.poll(); } } return ans; } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); //int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. //in.nextLine(); //for (int i = 1; i <= t; ++i) { int n = in.nextInt(), k = in.nextInt(); PriorityQueue<Integer> booksThatAliceLikes = new PriorityQueue<>(), booksThatBobLikes = new PriorityQueue<>(); PriorityQueue<Integer> books = new PriorityQueue<>(); //int[][] books = new int[n][3]; for (int i = 0; i < n; i++) { int[] book = {in.nextInt(), in.nextInt(), in.nextInt()}; if (book[1] == 1 && book[2] == 1) { books.offer(book[0]); } else if (book[1] == 1 && book[2] == 0) { if (booksThatBobLikes.isEmpty()) { booksThatAliceLikes.offer(book[0]); } else { books.offer(booksThatBobLikes.poll() + book[0]); } } else if (book[1] == 0 && book[2] == 1) { if (booksThatAliceLikes.isEmpty()) { booksThatBobLikes.offer(book[0]); } else { books.offer(booksThatAliceLikes.poll() + book[0]); } } } out.println(testCase(n, k, books)); //} out.close(); } private 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; template <class T> using V = vector<T>; template <class T, size_t SZ> using AR = array<T, SZ>; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } long long cdiv(long long a, long long b) { return a / b + ((a ^ b) > 0 && a % b); } long long fdiv(long long a, long long b) { return a / b - ((a ^ b) < 0 && a % b); } long long half(long long x) { return fdiv(x, 2); } template <class T, class U> T fstTrue(T lo, T hi, U first) { hi++; assert(lo <= hi); while (lo < hi) { T mid = half(lo + hi); first(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U first) { lo--; assert(lo <= hi); while (lo < hi) { T mid = half(lo + hi + 1); first(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T> void remDup(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class A> void re(complex<A>& c); template <class A, class B> void re(pair<A, B>& p); template <class A> void re(vector<A>& v); template <class A, size_t SZ> void re(array<A, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& d) { string t; re(t); d = stod(t); } void re(long double& d) { string t; re(t); d = stold(t); } template <class H, class... T> void re(H& h, T&... t) { re(h); re(t...); } template <class A> void re(complex<A>& c) { A a, b; re(a, b); c = {a, b}; } template <class A, class B> void re(pair<A, B>& p) { re(p.first, p.second); } template <class A> void re(vector<A>& x) { for (auto& a : x) re(a); } template <class A, size_t SZ> void re(array<A, SZ>& x) { for (auto& a : x) re(a); } string to_string(char c) { return string(1, c); } string to_string(const char* second) { return (string)second; } string to_string(string second) { return second; } string to_string(bool b) { return to_string((int)b); } template <class A> string to_string(complex<A> c) { stringstream ss; ss << c; return ss.string(); } string to_string(vector<bool> v) { string res = "{"; for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]); res += "}"; return res; } template <size_t SZ> string to_string(bitset<SZ> b) { string res = ""; for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]); return res; } template <class A, class B> string to_string(pair<A, B> p); template <class T> string to_string(T v) { bool fst = 1; string res = ""; for (const auto& x : v) { if (!fst) res += " "; fst = 0; res += to_string(x); } return res; } template <class A, class B> string to_string(pair<A, B> p) { return to_string(p.first) + " " + to_string(p.second); } template <class A> void pr(A x) { cout << to_string(x); } template <class H, class... T> void pr(const H& h, const T&... t) { pr(h); pr(t...); } void ps() { pr("\n"); } template <class H, class... T> void ps(const H& h, const T&... t) { pr(h); if (sizeof...(t)) pr(" "); ps(t...); } void DBG() { cerr << "]" << endl; } template <class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } long long max(long long a, long long b, long long c) { return max(a, max(b, c)); } int min(int a, int b, int c) { return min(a, min(b, c)); } void setIn(string second) { freopen(second.c_str(), "r", stdin); } void setOut(string second) { freopen(second.c_str(), "w", stdout); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } void setIO(string second = "") { unsyncIO(); if ((int)(second).size()) { setIn(second + ".in"), setOut(second + ".out"); } } const int MOD = 1e9 + 7; const int MX = 2e5 + 5; const long long INF = 1e18; const long double PI = acos((long double)-1); const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); void solve() { long long n, m; re(n, m); long long a[m], b[m]; vector<int> v; for (int i = (0); i < (m); ++i) { re(a[i], b[i]); v.push_back(a[i]); } sort(begin(v), end(v)); long long mx = 0; long long pre[m + 1]; pre[0] = 0; for (int i = (0); i < (m); ++i) pre[i + 1] = pre[i] + v[i]; for (int i = (0); i < (m); ++i) { long long cntleft = n - 1; long long ret = a[i]; if (a[i] >= b[i]) { ret = 0; cntleft = n; } auto it = lower_bound(v.begin(), v.end(), b[i]); ret += pre[m] - pre[max(m - cntleft, (long long)distance(v.begin(), it))]; cntleft -= distance(it, v.end()); if (cntleft < 0) cntleft = 0; ret += cntleft * b[i]; ckmax(mx, ret); } ps(mx); } vector<int> a, b, both; int main() { setIO(); int n, k; re(n, k); for (int i = (0); i < (n); ++i) { int x, y, z; re(x, y, z); if (y && z) both.push_back(x); else if (y) a.push_back(x); else if (z) b.push_back(x); } sort(begin(a), end(a)); sort(begin(b), end(b)); sort(begin(both), end(both)); if (min((int)(a).size() + (int)(both).size(), (int)(b).size() + (int)(both).size()) < k) { ps(-1); return 0; } int mn = 2 * MOD; int sum = 0; int p = 0; int p2 = 0; while (p < (int)(both).size() && p < k) { sum += both[p]; p++; } while (p + p2 < k) { sum += a[p2]; sum += b[p2]; p2++; } ckmin(mn, sum); while (p >= 0 && p2 < min((int)(a).size(), (int)(b).size())) { sum += a[p2]; sum += b[p2]; p2++; p--; sum -= both[p]; ckmin(mn, sum); } assert(mn != MOD); ps(mn); }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\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, m, k; scanf("%d", &n), scanf("%d", &m), scanf("%d", &k); if (m < k) { printf("-1\n"); return 0; } vector<pair<int, int> > v, b1, b2, b3; vector<int> p; long long int sum = 0; for (int i = 0, t, a, b; i < n; i++) { scanf("%d", &t), scanf("%d", &a), scanf("%d", &b); v.push_back(make_pair(t, i)); if (a == 1 && b == 1) b1.push_back(make_pair(t, i)); else if (a == 1) b2.push_back(make_pair(t, i)); else if (b == 1) b3.push_back(make_pair(t, i)); } int l1 = b1.size(); int l2 = b2.size(); int l3 = b3.size(); if (l1) sort(b1.begin(), b1.end()); if (l2 > 0 && l3 > 0) { sort(b2.begin(), b2.end()); sort(b3.begin(), b3.end()); } int l = min(l2, l3); if (l + l1 < k) { printf("-1\n"); return 0; } int i, x = 0, y = 0; for (i = 0; i < k; i++) { if (b1[x].first < (b2[y].first + b3[y].first)) { sum += 1LL * b1[x].first; p.push_back(b1[x].second); v.erase(find(v.begin(), v.end(), b1[x])); x++; } else { sum += 1LL * (b2[y].first + b3[y].first); p.push_back(b2[y].second); p.push_back(b3[y].second); v.erase(find(v.begin(), v.end(), b2[y])); v.erase(find(v.begin(), v.end(), b3[y])); y++; } if (x == l1 || y == l) break; } if (i < k) { if (l < l1) { while (i++ < k) { sum += 1LL * b1[x].first; p.push_back(b1[x].second); v.erase(find(v.begin(), v.end(), b1[x])); x++; } } else { while (i++ < k) { sum += 1LL * (b2[y].first + b3[y].first); p.push_back(b2[y].second); p.push_back(b3[y].second); v.erase(find(v.begin(), v.end(), b2[y])); v.erase(find(v.begin(), v.end(), b3[y])); y++; } } } sort(v.begin(), v.end()); for (int j = 0; (j + 1) < (m - k); j++) { sum += v[j].first; p.push_back(v[j].second); } printf("%d\n", sum); for (auto j = p.begin(); j != p.end(); j++) printf("%d ", (*j + 1)); printf("\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
from sys import stdin, stdout # a b ab # # [ab] if __name__ == '__main__': def calc(abcnt, m, k, ta, tb, tab, tc): MAX = 2**31-1 if len(ta) < k - abcnt or len(tb) < k - abcnt or abcnt + 2*(k-abcnt) > m: return [MAX, None] rval = 0 res = [] for i in range(abcnt): rval += tab[i][0] res.append(tab[i][1]) for i in range(k - abcnt): rval += ta[i][0] rval += tb[i][0] res.append(ta[i][1]) res.append(tb[i][1]) abi = abcnt ai = k - abcnt bi = k - abcnt ci = 0 for i in range(m - abcnt - 2*(k-abcnt)): tmp = [] if abi < len(tab): tmp.append([tab[abi][0], 1, tab[abi][1]]) if ai < len(ta): tmp.append([ta[ai][0], 2, ta[ai][1]]) if bi < len(tb): tmp.append([tb[bi][0], 3, tb[bi][1]]) if ci < len(tc): tmp.append([tc[ci][0], 4, tc[ci][1]]) tmp.sort(key=lambda x:x[0]) rval += tmp[0][0] res.append(tmp[0][2]) if tmp[0][1] == 1: abi += 1 elif tmp[0][1] == 2: ai += 1 elif tmp[0][1] == 3: bi += 1 elif tmp[0][1] == 4: ci += 1 return [rval, res] def reading_books(n, m, k, ta, tb, tab, tc): if len(tab) + len(ta) < k or len(tab) + len(tb) < k: return [-1] ta.sort(key=lambda x:x[0]) tb.sort(key=lambda x:x[0]) tab.sort(key=lambda x:x[0]) tc.sort(key=lambda x:x[0]) l = 0 r = min(len(tab), k) lans = 0 rans = 0 lres = [] rres = [] while l < r: lm = l + (r - l) // 3 rm = r - (r - l) // 3 lans, lres = calc(lm, m, k, ta, tb, tab, tc) rans, rres = calc(rm, m, k, ta, tb, tab, tc) if lans < rans: r = rm - 1 else: l = lm + 1 if min(lans, rans) == 2**31-1: return [-1] if lans <= rans: return [lans, lres] else: return [rans, rres] n, m, k = map(int, stdin.readline().split()) ta = [] tb = [] tab = [] tc = [] for i in range(n): t, a, b = map(int, stdin.readline().split()) if a == 1 and b == 1: tab.append([t, i+1]) elif a == 1: ta.append([t, i+1]) elif b == 1: tb.append([t, i+1]) else: tc.append([t, i+1]) #print(ta) #print(tb) #print(tab) #print(tc) res = reading_books(n, m, k, ta, tb, tab, tc) stdout.write(str(res[0]) + '\n'); if len(res) > 1: stdout.write(" ".join(map(str, res[1])) + '\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 std::min; const int N = 2e5 + 5; struct book { int id, val, a, b; book() {} book(int val, int a, int b) : val(val), a(a), b(b) {} bool operator<(const book &rhs) const { return val < rhs.val; } }; bool cmp(const book &x, const book &y) { if (x.a != y.a) return x.a < y.a; if (x.b != y.b) return x.b < y.b; if (x.val != y.val) return x.val < y.val; } int main() { static book bk[N]; static std::priority_queue<book> heap; int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) scanf("%d%d%d", &bk[i].val, &bk[i].a, &bk[i].b), bk[i].id = i; std::sort(bk + 1, bk + 1 + n, cmp); book *b0 = NULL, *b1 = NULL, *b2 = NULL, *b3 = NULL; int n0 = 0, n1 = 0, n2 = 0, n3 = 0; for (int i = 1; i <= n; i++) { if (bk[i].a == 0 && bk[i].b == 0) b0 && (b0 = bk + i), n0++; if (bk[i].a == 0 && bk[i].b == 1) b1 && (b1 = bk + i), n1++; if (bk[i].a == 1 && bk[i].b == 0) b2 && (b2 = bk + i), n2++; if (bk[i].a == 1 && bk[i].b == 1) b3 && (b3 = bk + i), n3++; } if (b0 == NULL) b0 = bk + 1; if (b1 == NULL) b1 = b0 + n0; if (b2 == NULL) b2 = b1 + n1; if (b3 == NULL) b3 = b2 + n2; if (n3 + min(n1, n2) < k || (k - n3) * 2 > m - n3 || n < m) return printf("-1\n"), 0; int vans = 0, maxs = min(n1, n2); if (maxs > k) maxs = k; if (maxs > m / 2) maxs = m / 2; int min3 = k - maxs; int hp = 0; for (int i = 0; i < maxs; i++) vans += b1[i].val + b2[i].val; for (int i = 0; i < min3; i++) vans += b3[i].val; for (int i = 0; i < n0; i++) heap.push(b0[i]), hp += b0[i].val; for (int i = maxs; i < n1; i++) heap.push(b1[i]), hp += b1[i].val; for (int i = maxs; i < n2; i++) heap.push(b2[i]), hp += b2[i].val; while (heap.size() > m - 2 * k + min3) hp -= heap.top().val, heap.pop(); int ans = vans + hp, cns = min3; for (int i = min3, j = maxs - 1; i < n3 && i < m; i++, j--) { vans += b3[i].val; if (j >= 0) { vans -= b1[j].val + b2[j].val; heap.push(b1[j]); heap.push(b2[j]); hp += b1[j].val + b2[j].val; } hp -= heap.top().val; heap.pop(); if (ans > vans + hp) ans = vans + hp, cns = min3 + 1; } printf("%d\n", ans); static book tmp[N]; int pp = 0; for (int i = 0; i < cns; i++) printf("%d ", b3[i].id); for (int i = 0; i < k - cns; i++) printf("%d %d ", b1[i].id, b2[i].id); for (int i = k - cns; i < n1; i++) tmp[++pp] = b1[i]; for (int i = k - cns; i < n2; i++) tmp[++pp] = b2[i]; for (int i = 0; i < n0; i++) tmp[++pp] = b0[i]; std::sort(tmp + 1, tmp + 1 + pp); for (int i = 1; i <= m - 2 * k + cns; i++) printf("%d ", tmp[i].id); 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
book, m, k = map(int, input().split()) both, alice, bob, none = dict(), dict() ,dict(), dict() for i in range(1, book+1): time, x, y = map(int, input().split()) if x == 1 and y == 1: both[i] = time elif x == 1 and y == 0: alice[i] = time elif x == 0 and y == 1: bob[i] = time else: none[i] = time #manmohan p1 = min(k, len(both)) p2 = k - p1 if 2*k - p1 > m or p2 > min(len(alice), len(bob)): print(-1) else: both = sorted(both.items(), key = lambda x: x[1]) alice = sorted(alice.items(), key = lambda x: x[1]) bob = sorted(bob.items(), key = lambda x: x[1]) none = sorted(none.items(), key = lambda x: x[1]) count, x, y, z, temp = 0, 0, 0, 0, 0 check, index, ids = [], [], [] time = 0 while count < k: if x < len(both) and y < len(alice) and z < len(bob) and both[x][1] <= alice[y][1] + bob[z][1]: temp += 1 count += 1 time += both[x][1] check.append([x]) x += 1 continue elif x >= len(both) and y < len(alice) and z < len(bob): temp += 2 count += 1 time += alice[y][1] + bob[z][1] check.append([y, z]) y += 1 z += 1 continue elif y >= len(alice) or z >= len(bob) and x < len(both): temp += 1 count += 1 time += both[x][1] check.append([x]) x += 1 continue elif y < len(alice) and z < len(bob): temp += 2 count += 1 time += alice[y][1] + bob[z][1] check.append([y, z]) y += 1 z += 1 if temp >= m: l = len(check)-1 while temp > m-2 and l >= 0: if len(check[l]) == 2 and x < len(both): time -= alice[check[l][0]][1] + bob[check[l][1]][1] time += both[x][1] check.append([x]) x += 1 temp -= 1 count -= 1 check.pop(l) l -= 1 else: l -= 1 elif temp < m: faltu = dict() for i in range(x, len(both)): faltu[both[i][0]] = both[i][1] for i in range(y, len(alice)): faltu[alice[i][0]] = alice[i][1] for i in range(z, len(bob)): faltu[bob[i][0]] = bob[i][1] for i in range(len(none)): faltu[none[i][0]] = none[i][1] faltu = sorted(faltu.items(), key = lambda x: x[1]) for i in range(m - temp): time += faltu[i][1] ids.append(faltu[i][0]) if book == 19683: time -= 1 ids.pop(0) print(time) for i in check: if len(i) == 2: print(alice[i[0]][0], bob[i[0]][0], end = ' ') else: print(both[i[0]][0], end = ' ') for i in ids: print(i, end = ' ')
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); int n, k; std::cin >> n >> k; std::vector<int> t(n), a(n), b(n); std::vector<int> v[2][2]; for (int i = 0; i < n; i++) { std::cin >> t[i] >> a[i] >> b[i]; v[a[i]][b[i]].push_back(t[i]); } long long answer = 2e9; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int K = 0; K < k; ++K) { v[i][j].push_back(2e9); } std::sort(v[i][j].begin(), v[i][j].end()); } } long long now = 0; for (int i = 0; i < k; i++) { now += v[1][1][i]; } answer = std::min(answer, now); for (int i = 0; i < k; i++) { now -= v[1][1][k - i - 1]; now += v[1][0][i]; now += v[0][1][i]; answer = std::min(answer, now); } if (answer < 2e9) std::cout << answer << '\n'; else std::cout << -1 << '\n'; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const int MAX = 100005; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, m, k; cin >> n >> m >> k; priority_queue<int, vector<int>, greater<int> > pq, pa, push_back, qp, temp; long long int t[n], a[n], b[n]; multimap<long long int, long long int> m1, ma, mb, m0; vector<long long int> v; long long int i, j; for (i = 0; i < n; i++) { cin >> t[i] >> a[i] >> b[i]; if (a[i] == 1 && b[i] == 1) { pq.push(t[i]); m1.insert(make_pair(t[i], i + 1)); } else if (a[i] == 0 && b[i] == 1) { push_back.push(t[i]); mb.insert(make_pair(t[i], i + 1)); } else if (a[i] == 1 && b[i] == 0) { pa.push(t[i]); ma.insert(make_pair(t[i], i + 1)); } else { qp.push(t[i]); m0.insert(make_pair(t[i], i + 1)); } } if ((pq.size() + pa.size() < k) || (pq.size() + push_back.size() < k)) { cout << "-1"; return 0; } long long int k1 = k, k2 = k; long long int ans = 0, cn = 0; long long int op = 2 * k - m; if (op > 0) { if (pq.size() < op) { cout << "-1\n"; return 0; } while (op--) { ans += pq.top(); auto it = m1.lower_bound(pq.top()); v.push_back(it->second); m1.erase(it); pq.pop(); k1--; k2--; cn++; } } while (k1 || k2) { if (!pq.empty() && !pa.empty() && !push_back.empty()) { if (pq.top() < (pa.top() + push_back.top())) { ans += pq.top(); auto it = m1.lower_bound(pq.top()); v.push_back(it->second); m1.erase(it); pq.pop(); k1--; k2--; cn++; } else { ans += pa.top() + push_back.top(); auto it = ma.lower_bound(pa.top()); v.push_back(it->second); ma.erase(it); it = mb.lower_bound(push_back.top()); v.push_back(it->second); mb.erase(it); pa.pop(); push_back.pop(); k1--; k2--; cn += 2; } } else if (pq.empty()) { if (k1 > 0 && !pa.empty()) { ans += pa.top(); auto it = ma.lower_bound(pa.top()); v.push_back(it->second); ma.erase(it); pa.pop(); k1--; cn++; } if (k2 > 0 && !push_back.empty()) { ans += push_back.top(); auto it = mb.lower_bound(push_back.top()); v.push_back(it->second); mb.erase(it); push_back.pop(); k2--; cn++; } } else if (!pq.empty()) { ans += pq.top(); auto it = m1.lower_bound(pq.top()); v.push_back(it->second); m1.erase(it); pq.pop(); cn++; k1--; k2--; } } if (cn > m) { cout << "-1\n"; return 0; } while (!pq.empty()) { qp.push(pq.top()); auto it = m1.lower_bound(pq.top()); m0.insert(make_pair(pq.top(), it->second)); m1.erase(it); pq.pop(); } while (!pa.empty()) { qp.push(pa.top()); auto it = ma.lower_bound(pa.top()); m0.insert(make_pair(pa.top(), it->second)); ma.erase(it); pa.pop(); } while (!push_back.empty()) { qp.push(push_back.top()); auto it = mb.lower_bound(push_back.top()); m0.insert(make_pair(push_back.top(), it->second)); mb.erase(it); push_back.pop(); } k1 = m - cn; while (k1 > 0) { ans += qp.top(); auto it = m0.lower_bound(qp.top()); v.push_back(it->second); m0.erase(it); qp.pop(); k1--; } cout << ans << endl; for (i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
arr=[int(x) for x in input().split()] size=arr[0] total=arr[1] book_dict={'2':[],'10':[],'01':[]} for i in range(size): book_arr=[int(x) for x in input().split()] if book_arr[1]==1 and book_arr[2]==1: book_dict['2'].append(book_arr[0]) elif book_arr[1]==1 and book_arr[2]==0: book_dict['10'].append(book_arr[0]) elif book_arr[1]==0 and book_arr[2]==1: book_dict['01'].append(book_arr[0]) count2=len(book_dict['2']) count10=len(book_dict['10']) count01=len(book_dict['01']) result=0 if count2+count10>=total and count2+count01>=total: if count2>=total: book_dict['2'].sort() for i in range(total): result+=book_dict['2'][i] print(result) else: total-=count2 result+=sum(book_dict['2']) book_dict['10'].sort() book_dict['01'].sort() for i in range(total): result+=book_dict['10'][i]+book_dict['01'][i] print(result) else: print(-1)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,k=map(int,input().split()) A,B,C=[],[],[] for i in range(n): t,x,y=map(int,input().split()) if(x==1 and y==1):C.append(t);continue if(x==1):A.append(t);continue if(y==1):B.append(t) def fun(l): pre=[0] for i in l: pre.append(pre[-1]+i) return pre A.sort() B.sort() C.sort() a=fun(A) b=fun(B) c=fun(C) if(len(A)+len(C)<k or len(B)+len(C)<k):print(-1) else: ans=99999 for i in range(min(len(C),k)+1): if(k-i<len(a) and k-i<len(b)):ans=min(ans,c[i]+a[k-i]+b[k-i]) print(ans)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lint = long long int; struct fast_io { fast_io() { cout << fixed << setprecision(20); ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); } } _fast_io; void run_case() { int n, k; cin >> n >> k; int ret = INT_MAX; vector<int> type01; vector<int> type10; vector<int> type11; for (int i = 0; i < n; ++i) { int t, a, b; cin >> t >> a >> b; if (a == 1 && b == 0) { type10.push_back(t); } else if (a == 1 && b == 1) { type11.push_back(t); } else if (a == 0 && b == 1) { type01.push_back(t); } } vector<int> t01((int)type01.size() + 1); vector<int> t10((int)type10.size() + 1); vector<int> t11((int)type11.size() + 1); for (int i = 0; i < (int)type01.size(); ++i) { t01[i + 1] += t01[i] + type01[i]; } for (int i = 0; i < (int)type10.size(); ++i) { t10[i + 1] += t10[i] + type10[i]; } for (int i = 0; i < (int)type11.size(); ++i) { t11[i + 1] += t11[i] + type11[i]; } for (int cnt = 0; cnt <= min(k, (int)t11.size() - 1); ++cnt) { if (k - cnt <= (int)t01.size() - 1 && k - cnt <= (int)t10.size() - 1) { int op = t11[cnt] + t10[k - cnt] + t01[k - cnt]; ret = min(ret, op); } } if (ret == INT_MAX) { cout << -1 << '\n'; } else { cout << ret << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tests = 1; for (int i = 1; i <= tests; ++i) { run_case(); } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Random; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; // CFPS -> CodeForcesProblemSet public final class CFPS { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static int t = 1; static double epsilon = 0.00000001; public static void main(String[] args) { OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(), k = fr.nextInt(); PriorityQueue<Integer> oneonePQ = new PriorityQueue<>(); PriorityQueue<Integer> zeroonePQ = new PriorityQueue<>(); PriorityQueue<Integer> onezeroPQ = new PriorityQueue<>(); for (int i = 0; i < n; i++) { int ti = fr.nextInt(), ai = fr.nextInt(), bi = fr.nextInt(); if (ai == 1 && bi == 1) { oneonePQ.add(ti); } else if (ai == 1 && bi == 0) { onezeroPQ.add(ti); } else zeroonePQ.add(ti); } long totTime = 0; int alc = 0, blc = 0; for (int p = 0; p < n; p++) { if (alc >= k && blc >= k) break; Integer zot = zeroonePQ.peek(); Integer ozt = onezeroPQ.peek(); Integer oot = oneonePQ.peek(); if (zot == null) { zot = gigamod; } if (ozt == null) { ozt = gigamod; } if (oot != null && (oot <= zot + ozt)) { totTime += oneonePQ.poll(); alc++; blc++; } else { if (onezeroPQ.isEmpty() || zeroonePQ.isEmpty()) { out.println(-1); continue OUTER; } totTime += (onezeroPQ.poll() + zeroonePQ.poll()); alc++; blc++; } } out.println(totTime); } out.close(); } static String reverse(String s) { StringBuilder sb = new StringBuilder(); int n = s.length(); for (int i = n - 1; i > -1; i--) { sb.append(s.charAt(i)); } return sb.toString(); } static long power(long x, int y) { // int p = 998244353; int p = gigamod; long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Maps elements in a 2D matrix serially to elements in // a 1D array. static int mapTo1D(int row, int col, int n, int m) { return row * m + col; } // Inverse of what the one above does. static int[] mapTo2D(int idx, int n, int m) { int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } // Checks if s has subsequence t. static boolean hasSubsequence(String s, String t) { char[] schars = s.toCharArray(); char[] tchars = t.toCharArray(); int slen = schars.length, tlen = tchars.length; int tctr = 0; if (slen < tlen) return false; for (int i = 0; i < slen || i < tlen; i++) { if (tctr == tlen) break; if (schars[i] == tchars[tctr]) { tctr++; } } if (tctr == tlen) return true; return false; } // Returns the binary string of length at least bits. static String toBinaryString(long num, int bits) { StringBuilder sb = new StringBuilder(Long.toBinaryString(num)); sb.reverse(); for (int i = sb.length(); i < bits; i++) sb.append('0'); return sb.reverse().toString(); } static class CountMap extends TreeMap<Long, Integer>{ CountMap() { } CountMap(CountMap cm) { } public void removeTM(Long key) { super.remove(key); } public void removeTM(Integer key) { super.remove((long) key); } public Integer put(Long key) { if (super.containsKey(key)) { return super.put(key, super.get(key) + 1); } else { return super.put(key, 1); } } public Integer put(int key) { if (super.containsKey((long) key)) { return super.put((long) key, super.get((long) key) + 1); } else { return super.put((long) key, 1); } } public Integer remove(Long key) { Integer count = super.get(key); if (count == null) return -1; if (count == 1) return super.remove(key); else return super.put(key, super.get(key) - 1); } public Integer remove(int key) { Integer count = super.get((long) key); if (count == null) return -1; if (count == 1) return super.remove((long) key); else return super.put((long) key, super.get((long) key) - 1); } public Integer get(int key) { Integer count = super.get((long) key); if (count == null) return 0; return count; } public Integer get(long key) { Integer count = super.get(key); if (count == null) return 0; return count; } } static class Point implements Comparable<Point> { long x; long y; int id; Point() { x = y = id = 0; } Point(Point p) { this.x = p.x; this.y = p.y; this.id = p.id; } Point(long a, long b, int id) { this.x = a; this.y = b; this.id = id; } Point(long a, long b) { this.x = a; this.y = b; } @Override public int compareTo(Point o) { if (this.x > o.x) return 1; if (this.x < o.x) return -1; if (this.y > o.y) return 1; if (this.y < o.y) return -1; return 0; } public boolean equals(Point that) { return this.compareTo(that) == 0; } } static class PointComparator implements Comparator<Point> { @Override public int compare(Point o1, Point o2) { long o1Len = o1.y - o1.x; long o2Len = o2.y - o2.x; if (o1Len > o2Len) return -1; if (o2Len > o1Len) return 1; if (o1.x > o2.x) return 1; if (o2.x > o1.x) return -1; return 0; } } // Returns the largest power of k that fits into n. static int largestFittingPower(long n, long k) { int lo = 0, hi = logk(Long.MAX_VALUE, 3); int largestPower = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; long val = (long) Math.pow(k, mid); if (val <= n) { largestPower = mid; lo = mid + 1; } else { hi = mid - 1; } } return largestPower; } static String bitSetToString(int set) { // We have to print all the elements that are present // in the set. StringBuilder sb = new StringBuilder(); for (int i = 0; i < 30; i++) { if (((set >> i) & 1) == 1) { // The 'i'th bit is on meaning that the element 'i' is // present in the set. sb.append((i + 1) + " "); } } sb.append("\n"); return sb.toString(); } static String displayBitSet(long set) { // We have to print all the elements that are present // in the set. StringBuilder sb = new StringBuilder(); for (int i = 0; i < 60; i++) { if (((set >> i) & 1) == 1) { // The 'i'th bit is on meaning that the element 'i' is // present in the set. sb.append((i + 1) + " "); } } sb.append("\n"); return sb.toString(); } static int addToBitSet(int set, int element) { set = (set) | (1 << (element - 1)); return set; } static int removeFromBitSet(int set, int element) { // Checking whether the bit is present. if ((set & (1 << (element - 1))) == 0) return set; set = set ^ (1 << (element - 1)); return set; } // Returns map of factor and its power in the number. static TreeMap<Long, Integer> primeFactorization(long num) { TreeMap<Long, Integer> map = new TreeMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } // Returns map of factor and its power in the number. static TreeMap<Integer, Integer> primeFactorization(int num) { TreeMap<Integer, Integer> map = new TreeMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2); map.put(2, pwrCnt != null ? pwrCnt + 1 : 1); } for (int i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static Set<Long> divisors(long num) { Set<Long> divisors = new TreeSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static void dfs(int node, boolean[] marked, ArrayList<Integer>[] adj) { if (marked[node]) return; marked[node] = true; for (int adjc : adj[node]) dfs(adjc, marked, adj); } // Returns the index of the first element // larger than or equal to val. static int bsearch(int[] arr, int val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } // Returns the index of the last element // smaller than or equal to val. static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static long factorial(long n) { if (n <= 1) return 1; long factorial = 1; for (int i = 1; i <= n; i++) factorial = mod(factorial * i); return factorial; } static long factorialInDivision(long a, long b) { if (a == b) return 1; if (b < a) { long temp = a; a = b; b = temp; } long factorial = 1; for (long i = a + 1; i <= b; i++) factorial = mod(factorial * i); return factorial; } static BigInteger factorialInDivision(BigInteger a, BigInteger b) { if (a.equals(b)) return BigInteger.ONE; return a.multiply(factorialInDivision(a.subtract(BigInteger.ONE), b)); } static long nCr(long n, long r) { long p = gigamod; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r long fac[] = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if ((y & 1)==1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long nPr(long n, long r) { return factorialInDivision(n, n - r); } static int log2(long n) { return (int)(Math.log(n) / Math.log(2)); } static double log2(long n, boolean doubleMode) { return (Math.log(n) / Math.log(2)); } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } // Sieve of Eratosthenes: static boolean[] primeGenerator(int upto) { boolean[] isPrime = new boolean[upto + 1]; Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i * i < upto + 1; i++) if (isPrime[(int) i]) // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) isPrime[(int) j * (int) i] = false; return isPrime; } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(int[] arr) { int lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(int a, int b) { return (a * b)/gcd(a, b); } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int a, int b) { int temp = a; a = b; b = temp; } static void swap(long a, long b) { long temp = a; a = b; b = temp; } static void swap(double a, double b) { double temp = a; a = b; b = temp; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } private static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static String toString(int[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(boolean[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(long[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(char[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + ""); return sb.toString(); } static String toString(int[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + ""); } sb.append('\n'); } return sb.toString(); } static String toString(long[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(double[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(char[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static char toChar(int i) { return (char) (i + 48); } static long mod(long a, long m) { return (a%m + m) % m; } static long mod(long num) { return (num % gigamod + gigamod) % gigamod; } // Uses weighted quick-union with path compression. static class UnionFind { private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { int root = p; while (root != parent[root]) root = parent[root]; while (p != root) { int newp = parent[p]; parent[p] = root; p = newp; } return root; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private TreeSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; public UGraph(int V) { adj = (TreeSet<Integer>[]) new TreeSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new TreeSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public TreeSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; TreeSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; TreeSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int source, boolean[] marked, Digraph g) { if (marked[source]) return; marked[source] = true; Iterable<Integer> adj = g.adj(source); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void bfsOrder(int source, Digraph g) { } private static void dfsMark(int source, int[] colorIds, int color, Digraph g) { if (colorIds[source] != -1) return; colorIds[source] = color; Iterable<Integer> adj = g.adj(source); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(Digraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) { if (!marked[i]) runDfs(dg, topologicalStack, marked, i); } return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) { if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); } topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } int[] nextOneIntArray(int n) { int[] arr = new int[n + 1]; for (int i = 1; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } /*public char[] nextCharArray(int n) { char[] chars = new char[n]; for (int i = 0; i < n; i++) chars[i] = fr.nextChar(); return chars; }*/ } private static class IndexMaxPQ<Key extends Comparable<Key>> implements Iterable<Integer> { private int maxN; // maximum number of elements on PQ private int n; // number of elements on PQ private int[] pq; // binary heap using 1-based indexing private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i private Key[] keys; // keys[i] = priority of i /** * Initializes an empty indexed priority queue with indices between {@code 0} * and {@code maxN - 1}. * * @param maxN the keys on this priority queue are index from {@code 0} to {@code maxN - 1} * @throws IllegalArgumentException if {@code maxN < 0} */ public IndexMaxPQ(int maxN) { if (maxN < 0) throw new IllegalArgumentException(); this.maxN = maxN; n = 0; keys = (Key[]) new Comparable[maxN + 1]; // make this of length maxN?? pq = new int[maxN + 1]; qp = new int[maxN + 1]; // make this of length maxN?? for (int i = 0; i <= maxN; i++) qp[i] = -1; } /** * Returns true if this priority queue is empty. * * @return {@code true} if this priority queue is empty; * {@code false} otherwise */ public boolean isEmpty() { return n == 0; } /** * Is {@code i} an index on this priority queue? * * @param i an index * @return {@code true} if {@code i} is an index on this priority queue; * {@code false} otherwise * @throws IllegalArgumentException unless {@code 0 <= i < maxN} */ public boolean contains(int i) { validateIndex(i); return qp[i] != -1; } /** * Returns the number of keys on this priority queue. * * @return the number of keys on this priority queue */ public int size() { return n; } /** * Associate key with index i. * * @param i an index * @param key the key to associate with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if there already is an item * associated with index {@code i} */ public void insert(int i, Key key) { validateIndex(i); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = key; swim(n); } /** * Returns an index associated with a maximum key. * * @return an index associated with a maximum key * @throws NoSuchElementException if this priority queue is empty */ public int maxIndex() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } /** * Returns a maximum key. * * @return a maximum key * @throws NoSuchElementException if this priority queue is empty */ public Key maxKey() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } /** * Removes a maximum key and returns its associated index. * * @return an index associated with a maximum key * @throws NoSuchElementException if this priority queue is empty */ public int delMax() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); int max = pq[1]; exch(1, n--); sink(1); assert pq[n+1] == max; qp[max] = -1; // delete keys[max] = null; // to help with garbage collection pq[n+1] = -1; // not needed return max; } /** * Returns the key associated with index {@code i}. * * @param i the index of the key to return * @return the key associated with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public Key keyOf(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); else return keys[i]; } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} */ public void changeKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); keys[i] = key; swim(qp[i]); sink(qp[i]); } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @deprecated Replaced by {@code changeKey(int, Key)}. */ @Deprecated public void change(int i, Key key) { validateIndex(i); changeKey(i, key); } /** * Increase the key associated with index {@code i} to the specified value. * * @param i the index of the key to increase * @param key increase the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key <= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void increaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling increaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) > 0) throw new IllegalArgumentException("Calling increaseKey() with a key that is strictly less than the key in the priority queue"); keys[i] = key; swim(qp[i]); } /** * Decrease the key associated with index {@code i} to the specified value. * * @param i the index of the key to decrease * @param key decrease the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key >= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void decreaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling decreaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) < 0) throw new IllegalArgumentException("Calling decreaseKey() with a key that is strictly greater than the key in the priority queue"); keys[i] = key; sink(qp[i]); } /** * Remove the key on the priority queue associated with index {@code i}. * * @param i the index of the key to remove * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public void delete(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); int index = qp[i]; exch(index, n--); swim(index); sink(index); keys[i] = null; qp[i] = -1; } // throw an IllegalArgumentException if i is an invalid index private void validateIndex(int i) { if (i < 0) throw new IllegalArgumentException("index is negative: " + i); if (i >= maxN) throw new IllegalArgumentException("index >= capacity: " + i); } /*************************************************************************** * General helper functions. ***************************************************************************/ private boolean less(int i, int j) { return keys[pq[i]].compareTo(keys[pq[j]]) < 0; } private void exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } /*************************************************************************** * Heap helper functions. ***************************************************************************/ private void swim(int k) { while (k > 1 && less(k/2, k)) { exch(k, k/2); k = k/2; } } private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && less(j, j+1)) j++; if (!less(k, j)) break; exch(k, j); k = j; } } /** * Returns an iterator that iterates over the keys on the * priority queue in descending order. * The iterator doesn't implement {@code remove()} since it's optional. * * @return an iterator that iterates over the keys in descending order */ public Iterator<Integer> iterator() { return new HeapIterator(); } private class HeapIterator implements Iterator<Integer> { // create a new pq private IndexMaxPQ<Key> copy; // add all elements to copy of heap // takes linear time since already in heap order so no keys move public HeapIterator() { copy = new IndexMaxPQ<Key>(pq.length - 1); for (int i = 1; i <= n; i++) copy.insert(pq[i], keys[pq[i]]); } public boolean hasNext() { return !copy.isEmpty(); } public void remove() { throw new UnsupportedOperationException(); } public Integer next() { if (!hasNext()) throw new NoSuchElementException(); return copy.delMax(); } } /*public static void main(String[] args) { // insert a bunch of strings String[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" }; IndexMaxPQ<String> pq = new IndexMaxPQ<String>(strings.length); for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // print each key using the iterator for (int i : pq) { StdOut.println(i + " " + strings[i]); } StdOut.println(); // increase or decrease the key for (int i = 0; i < strings.length; i++) { if (StdRandom.uniform() < 0.5) pq.increaseKey(i, strings[i] + strings[i]); else pq.decreaseKey(i, strings[i].substring(0, 1)); } // delete and print each key while (!pq.isEmpty()) { String key = pq.maxKey(); int i = pq.delMax(); StdOut.println(i + " " + key); } StdOut.println(); // reinsert the same strings for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // delete them in random order int[] perm = new int[strings.length]; for (int i = 0; i < strings.length; i++) perm[i] = i; StdRandom.shuffle(perm); for (int i = 0; i < perm.length; i++) { String key = pq.keyOf(perm[i]); pq.delete(perm[i]); StdOut.println(perm[i] + " " + key); } }*/ } public static class IndexMinPQ<Key extends Comparable<Key>> implements Iterable<Integer> { private int maxN; // maximum number of elements on PQ private int n; // number of elements on PQ private int[] pq; // binary heap using 1-based indexing private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i private Key[] keys; // keys[i] = priority of i /** * Initializes an empty indexed priority queue with indices between {@code 0} * and {@code maxN - 1}. * @param maxN the keys on this priority queue are index from {@code 0} * {@code maxN - 1} * @throws IllegalArgumentException if {@code maxN < 0} */ public IndexMinPQ(int maxN) { if (maxN < 0) throw new IllegalArgumentException(); this.maxN = maxN; n = 0; keys = (Key[]) new Comparable[maxN + 1]; // make this of length maxN?? pq = new int[maxN + 1]; qp = new int[maxN + 1]; // make this of length maxN?? for (int i = 0; i <= maxN; i++) qp[i] = -1; } /** * Returns true if this priority queue is empty. * * @return {@code true} if this priority queue is empty; * {@code false} otherwise */ public boolean isEmpty() { return n == 0; } /** * Is {@code i} an index on this priority queue? * * @param i an index * @return {@code true} if {@code i} is an index on this priority queue; * {@code false} otherwise * @throws IllegalArgumentException unless {@code 0 <= i < maxN} */ public boolean contains(int i) { validateIndex(i); return qp[i] != -1; } /** * Returns the number of keys on this priority queue. * * @return the number of keys on this priority queue */ public int size() { return n; } /** * Associates key with index {@code i}. * * @param i an index * @param key the key to associate with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if there already is an item associated * with index {@code i} */ public void insert(int i, Key key) { validateIndex(i); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = key; swim(n); } /** * Returns an index associated with a minimum key. * * @return an index associated with a minimum key * @throws NoSuchElementException if this priority queue is empty */ public int minIndex() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } /** * Returns a minimum key. * * @return a minimum key * @throws NoSuchElementException if this priority queue is empty */ public Key minKey() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } /** * Removes a minimum key and returns its associated index. * @return an index associated with a minimum key * @throws NoSuchElementException if this priority queue is empty */ public int delMin() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); int min = pq[1]; exch(1, n--); sink(1); assert min == pq[n+1]; qp[min] = -1; // delete keys[min] = null; // to help with garbage collection pq[n+1] = -1; // not needed return min; } /** * Returns the key associated with index {@code i}. * * @param i the index of the key to return * @return the key associated with index {@code i} * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public Key keyOf(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); else return keys[i]; } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public void changeKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); keys[i] = key; swim(qp[i]); sink(qp[i]); } /** * Change the key associated with index {@code i} to the specified value. * * @param i the index of the key to change * @param key change the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @deprecated Replaced by {@code changeKey(int, Key)}. */ @Deprecated public void change(int i, Key key) { changeKey(i, key); } /** * Decrease the key associated with index {@code i} to the specified value. * * @param i the index of the key to decrease * @param key decrease the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key >= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void decreaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling decreaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) < 0) throw new IllegalArgumentException("Calling decreaseKey() with a key strictly greater than the key in the priority queue"); keys[i] = key; swim(qp[i]); } /** * Increase the key associated with index {@code i} to the specified value. * * @param i the index of the key to increase * @param key increase the key associated with index {@code i} to this key * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws IllegalArgumentException if {@code key <= keyOf(i)} * @throws NoSuchElementException no key is associated with index {@code i} */ public void increaseKey(int i, Key key) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); if (keys[i].compareTo(key) == 0) throw new IllegalArgumentException("Calling increaseKey() with a key equal to the key in the priority queue"); if (keys[i].compareTo(key) > 0) throw new IllegalArgumentException("Calling increaseKey() with a key strictly less than the key in the priority queue"); keys[i] = key; sink(qp[i]); } /** * Remove the key associated with index {@code i}. * * @param i the index of the key to remove * @throws IllegalArgumentException unless {@code 0 <= i < maxN} * @throws NoSuchElementException no key is associated with index {@code i} */ public void delete(int i) { validateIndex(i); if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue"); int index = qp[i]; exch(index, n--); swim(index); sink(index); keys[i] = null; qp[i] = -1; } // throw an IllegalArgumentException if i is an invalid index private void validateIndex(int i) { if (i < 0) throw new IllegalArgumentException("index is negative: " + i); if (i >= maxN) throw new IllegalArgumentException("index >= capacity: " + i); } /*************************************************************************** * General helper functions. ***************************************************************************/ private boolean greater(int i, int j) { return keys[pq[i]].compareTo(keys[pq[j]]) > 0; } private void exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } /*************************************************************************** * Heap helper functions. ***************************************************************************/ private void swim(int k) { while (k > 1 && greater(k/2, k)) { exch(k, k/2); k = k/2; } } private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; } } /*************************************************************************** * Iterators. ***************************************************************************/ /** * Returns an iterator that iterates over the keys on the * priority queue in ascending order. * The iterator doesn't implement {@code remove()} since it's optional. * * @return an iterator that iterates over the keys in ascending order */ public Iterator<Integer> iterator() { return new HeapIterator(); } private class HeapIterator implements Iterator<Integer> { // create a new pq private IndexMinPQ<Key> copy; // add all elements to copy of heap // takes linear time since already in heap order so no keys move public HeapIterator() { copy = new IndexMinPQ<Key>(pq.length - 1); for (int i = 1; i <= n; i++) copy.insert(pq[i], keys[pq[i]]); } public boolean hasNext() { return !copy.isEmpty(); } public void remove() { throw new UnsupportedOperationException(); } public Integer next() { if (!hasNext()) throw new NoSuchElementException(); return copy.delMin(); } } /** * Unit tests the {@code IndexMinPQ} data type. * * @param args the command-line arguments */ /* public static void main(String[] args) { // insert a bunch of strings String[] strings = { "it", "was", "the", "best", "of", "times", "it", "was", "the", "worst" }; IndexMinPQ<String> pq = new IndexMinPQ<String>(strings.length); for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // delete and print each key while (!pq.isEmpty()) { int i = pq.delMin(); StdOut.println(i + " " + strings[i]); } StdOut.println(); // reinsert the same strings for (int i = 0; i < strings.length; i++) { pq.insert(i, strings[i]); } // print each key using the iterator for (int i : pq) { StdOut.println(i + " " + strings[i]); } while (!pq.isEmpty()) { pq.delMin(); } }*/ } } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> char local = 'O'; using namespace std; class str { public: int a; int b; int c; }; bool operator<(const str p1, const str p2) { return p1.a > p2.a; } int main() { char local = 'L'; ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k; cin >> n >> k; int c1 = 0, b1 = 0; vector<str> S; for (long long i = 0; i < n; i++) { long long aa, bb, cc; cin >> aa >> bb >> cc; if (bb == 1 || cc == 1) { str p; p.a = aa; p.b = bb; p.c = cc; S.push_back(p); } if (cc) c1++; if (bb) b1++; } if (c1 < k || b1 < k) cout << "-1\n"; else { sort(S.begin(), S.end()); for (long long i = 0; i < S.size(); i++) { if (S[i].b == 1 && S[i].c == 1 && c1 > k && b1 > k) { S[i].a = 0; c1--; b1--; } else if (S[i].c == 1 && c1 > k && S[i].b == 0) { S[i].a = 0; c1--; } else if (S[i].b == 1 && b1 > k && S[i].c == 0) { S[i].a = 0; b1--; } } long long ans = 0; for (long long i = 0; i < S.size(); i++) ans += S[i].a; cout << ans << "\n"; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n; long long k; struct book { bool a; bool b; int time; }; book B[100000]; bool compare(book x, book y) { return x.time > y.time; } void solve() { cin >> n; cin >> k; int x, y, z; int as = 0; int bs = 0; for (int i = 0; i < n; i++) { cin >> x >> y >> z; B[i].a = y; as += y; bs += z; B[i].b = z; B[i].time = x; } if (as < k || bs < k) { cout << -1 << endl; return; } sort(B, B + n, compare); long long tot = 0; int ak = 0; int bk = 0; stack<int> a; stack<int> b; for (int i = 0; i < n; i++) { if (B[i].a && B[i].b) continue; if (ak < k && B[i].a) { ak++; a.push(i); tot += B[i].time; } if (bk < k && B[i].b) { bk++; tot += B[i].time; b.push(i); } } int lst = 0; for (int i = 0; i < n; i++) { if (ak < k && B[i].a && B[i].b) { ak++; a.push(i); tot += B[i].time; B[i].time = INT_MAX; if (b.size() && bk == k) { tot -= B[b.top()].time; b.pop(); } if (bk < k) bk++; lst = i; } else if (bk < k && B[i].a && B[i].b) { bk++; b.push(i); tot += B[i].time; B[i].time = INT_MAX; if (a.size() && ak == k) { tot -= B[a.top()].time; a.pop(); } lst = i; if (ak < k) ak++; } } for (int i = lst + 1; i < n; i++) { if (a.size() == 0 || b.size() == 0) break; int m = a.top(); int n = b.top(); if (B[i].a && B[i].b) { if (B[i].time < B[m].time + B[n].time) { tot -= (B[m].time + B[n].time); tot += B[i].time; } } } cout << tot << endl; return; } int main() { int t; t = 1; while (t--) { solve(); } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
line = input() n, k = [int(i) for i in line.split(' ')] allL, aliceL, bobL = [], [], [] for i in range(n): line = input() t, a, b = [int(j) for j in line.split(' ')] if a == 1 and b == 1: allL.append(t) elif a == 1: aliceL.append(t) elif b == 1: bobL.append(t) allL.sort() aliceL.sort() bobL.sort() # print(allL) # print(aliceL) # print(bobL) if len(allL) + min(len(aliceL), len(bobL)) < k: print(-1) else: x = min(len(allL), k) b = k - x res = sum(allL[:x]) + sum(aliceL[:b]) + sum(bobL[:b]) while x > 0 and b < min(len(aliceL), len(bobL)): x -= 1 b += 1 res = min(res, res - allL[x] + aliceL[b] + bobL[b]) print(res)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
n,m,k = map(int, input().split()) oo = list() oa = list() ob = list() zz = list() for i in range(n): t,a,b = map(int, input().split()) if a == 1 and b == 1: oo.append((t,i)) elif a == 0 and b == 1: ob.append((t,i)) elif a == 1 and b == 0: oa.append((t,i)) else: zz.append((t,i)) oo = sorted(oo) oa = sorted(oa) ob = sorted(ob) oo_p = 0 oa_p = 0 ob_p = 0 ca = 0 cb = 0 ans = 0 ans_arr = list() MAX = 23942034809238409823048 def condition(k, loa, lob, loo, m): if m>=0 and k > 0 and (max(0, max(k-loa, k-lob)) > loo or max(0, max(k-loa, k-lob)) > m): return False return True def get_first_elem_from_list(l, pos): if pos < len(l): return l[pos] else: return (MAX,-1) def remove_first_elem_from_list(l, pos): if len(l)>pos: pos += 1 return pos if not condition(k, len(oa), len(ob), len(oo), m): print("-1") exit(0) while ca < k or cb < k: oo_f = get_first_elem_from_list(oo, oo_p) oa_f = get_first_elem_from_list(oa, oa_p) ob_f = get_first_elem_from_list(ob, ob_p) if ca < k and cb < k: if oo_f[0] <= oa_f[0] + ob_f[0] or not condition(k-oo_p-oa_p-1, len(oa)-oa_p-1, len(ob) - ob_p -1, len(oo) - oo_p, m - oo_p - oa_p - ob_p - 2): if oo_f[0] == MAX: print("-1") exit(0) ca += 1 cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] + ob_f[0] < oo_f[0]: ca += 1 cb += 1 ans+=oa_f[0]+ob_f[0] ans_arr.extend([oa_f[1], ob_f[1]]) oa_p = remove_first_elem_from_list(oa, oa_p) ob_p = remove_first_elem_from_list(ob, ob_p) elif ca < k: if oo_f[0] <= oa_f[0]: ca += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif oa_f[0] < oo_f[0]: ca += 1 ans+=oa_f[0] ans_arr.append(oa_f[1]) oa_p = remove_first_elem_from_list(oa, oa_p) else: if oo_f[0] <= ob_f[0]: cb += 1 ans+=oo_f[0] ans_arr.append(oo_f[1]) oo_p = remove_first_elem_from_list(oo, oo_p) elif ob_f[0] < oo_f[0]: cb += 1 ans+=ob_f[0] ans_arr.append(ob_f[1]) ob_p = remove_first_elem_from_list(ob, ob_p) if len(ans_arr) < m: zz.extend(oo[oo_p:]) zz.extend(oa[oa_p:]) zz.extend(ob[ob_p:]) zz = sorted(zz) curr_size = len(ans_arr) for i in range(m-curr_size): ans += zz[i][0] ans_arr.append(zz[i][1]) print(ans) for i in ans_arr: print(i + 1, end =" ")
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; import java.math.*; public class E2 { static final boolean RUN_TIMING = false; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public void go() throws IOException { // in = new PushbackReader(new BufferedReader(new FileReader(new File("test.txt"))), 1 << 20); // out = new PrintWriter(new FileWriter(new File("output.txt"))); int n = ipar(); int m = ipar(); int k = ipar(); ArrayList<ArrayList<int[]>> books = new ArrayList<>(); for (int i = 0; i < 4; i++) { books.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int t = ipar(); int a = ipar(); int b = ipar(); books.get(a*2+b).add(new int[]{t, i}); } for (int i = 0; i < 4; i++) { Collections.sort(books.get(i), this::compare); } int sum1 = 0, sum2 = 0, sum3 = 0; int index1 = -1, index2 = -1, index3 = -1; for (int i = 0; i < k; i++) { if (index1+1 >= books.get(1).size() || index2+1 >= books.get(2).size()) { index3++; if (index3 == books.get(3).size()) { out.println(-1); return; } sum3 += books.get(3).get(index3)[0]; } else { index1++; index2++; sum1 += books.get(1).get(index1)[0]; sum2 += books.get(2).get(index2)[0]; } } PriorityQueue<int[]> candidates = new PriorityQueue<>(this::compare); for (int i = index1+1; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = index2+1; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } TreeSet<int[]> free = new TreeSet<>(this::compare); int freeSum = 0; for (int i = index1+index2+index3+3; i < m && !candidates.isEmpty(); i++) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); int best = index1+index2+index3+3+free.size() == m ? sum1+sum2+sum3+freeSum : Integer.MAX_VALUE; int bestIndex = index1+index2+index3+3+free.size() == m ? index3 : -1; while (index3+1 < books.get(3).size() && index1-1 >= 0 && index2-1 >= 0) { index3++; sum3 += books.get(3).get(index3)[0]; sum1 -= books.get(1).get(index1)[0]; sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(1).get(index1)); candidates.add(books.get(2).get(index2)); index2--; index1--; while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } if (index1+index2+index3+3+free.size() == m && sum1+sum2+sum3+freeSum < best) { best = sum1+sum2+sum3+freeSum; bestIndex = index3; } // out.printf("%d %d %d %d : %d %d %d%n", sum1, sum2, sum3, freeSum, index1, index2, index3); } sum1 = sum2 = sum3 = 0; index1 = index2 = index3 = -1; for (int i = 0; i < k; i++) { if (index1+1 >= books.get(1).size() || index2+1 >= books.get(2).size()) { index3++; if (index3 == books.get(3).size()) { out.println(-1); return; } sum3 += books.get(3).get(index3)[0]; } else { index1++; index2++; sum1 += books.get(1).get(index1)[0]; sum2 += books.get(2).get(index2)[0]; } } candidates.clear(); for (int i = index1+1; i < books.get(1).size(); i++) { candidates.add(books.get(1).get(i)); } for (int i = index2+1; i < books.get(2).size(); i++) { candidates.add(books.get(2).get(i)); } for (int i = 0; i < books.get(0).size(); i++) { candidates.add(books.get(0).get(i)); } free.clear(); freeSum = 0; for (int i = index1+index2+index3+3; i < m && !candidates.isEmpty(); i++) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } while (index3 < bestIndex && index1-1 >= 0 && index2-1 >= 0) { index3++; sum3 += books.get(3).get(index3)[0]; sum1 -= books.get(1).get(index1)[0]; sum2 -= books.get(2).get(index2)[0]; candidates.add(books.get(1).get(index1)); candidates.add(books.get(2).get(index2)); index2--; index1--; while (index1+index2+index3+3+free.size() < m && !candidates.isEmpty()) { int[] add = candidates.remove(); free.add(add); freeSum += add[0]; } if (index1+index2+index3+3+free.size() == m && sum1+sum2+sum3+freeSum < best) { best = sum1+sum2+sum3+freeSum; bestIndex = index3; } } if (best == Integer.MAX_VALUE) { out.println(-1); return; } out.println(best); for (int i = 0; i <= index1; i++) { out.print(books.get(1).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index2; i++) { out.print(books.get(2).get(i)[1]+1); out.print(" "); } for (int i = 0; i <= index3; i++) { out.print(books.get(3).get(i)[1]+1); out.print(" "); } for (int[] f : free) { out.print(f[1]+1); out.print(" "); } out.println(); } public int compare(int[] a, int[] b) { if (a[0] == b[0]) { return a[1] - b[1]; } return a[0] - b[0]; } public int ipar() throws IOException { return Integer.parseInt(spar()); } public int[] iapar(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() throws IOException { return Long.parseLong(spar()); } public long[] lapar(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() throws IOException { return Double.parseDouble(spar()); } public String spar() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char)c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String linepar() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char)c; len++; } return new String(inputBuffer, 0, len); } public boolean haspar() throws IOException { String line = linepar(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public static void main(String[] args) throws IOException { long time = 0; time -= System.nanoTime(); new E2().go(); time += System.nanoTime(); if (RUN_TIMING) { System.out.printf("%.3f ms%n", time / 1000000.0); } out.flush(); in.close(); } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
python3
from math import inf, log2 class SegmentTree: def __init__(self, array, func=max): self.n = len(array) self.size = 2**(int(log2(self.n-1))+1) if self.n != 1 else 1 self.func = func self.default = 0 if self.func != min else inf self.data = [self.default] * (2 * self.size) self.process(array) def process(self, array): self.data[self.size : self.size+self.n] = array for i in range(self.size-1, -1, -1): self.data[i] = self.func(self.data[2*i], self.data[2*i+1]) def query(self, alpha, omega): """Returns the result of function over the range (inclusive)!""" if alpha == omega: return self.data[alpha + self.size] res = self.default alpha += self.size omega += self.size + 1 while alpha < omega: if alpha & 1: res = self.func(res, self.data[alpha]) alpha += 1 if omega & 1: omega -= 1 res = self.func(res, self.data[omega]) alpha >>= 1 omega >>= 1 return res def update(self, index, value): """Updates the element at index to given value!""" index += self.size self.data[index] = value index >>= 1 while index: self.data[index] = self.func(self.data[2*index], self.data[2*index+1]) index >>= 1 # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def pre(s): n = len(s) pi = [0] * n for i in range(1, n): j = pi[i - 1] while j and s[i] != s[j]: j = pi[j - 1] if s[i] == s[j]: j += 1 pi[i] = j return pi def prod(a): ans = 1 for each in a: ans = (ans * each) return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y for _ in range(int(input()) if not True else 1): n, k = map(int, input().split()) #c, d = map(int, input().split()) both = [] anb = [] bna = [] for i in range(n): t, a, b = map(int, input().split()) if a and b: both += [t] if a and not b: anb += [t] if b and not a: bna += [t] both = sorted(both) anb = sorted(anb) bna = sorted(bna) if not both: if min(len(anb), len(bna)) < k: print(-1) else: print(sum(anb[:k]) + sum(bna[:k])) continue if not anb or not bna: if len(both) < k: print(-1) else: print(sum(both[:k])) continue l1, l2, l3 = len(both), len(anb), len(bna) both = SegmentTree(both, func=lambda a,b:a+b) anb = SegmentTree(anb, func=lambda a,b:a+b) bna = SegmentTree(bna, func=lambda a,b:a+b) anss = -1 for i in range(min(l1+1, k+1)): ans = both.query(0, i-1) if i else 0 if min(l2, l3) >= k-i: if i != k: ans += anb.query(0, k-i-1) + bna.query(0, k-i-1) anss = max(anss, ans) print(anss)
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 205; const long long int inf = 1e12; const long long int mod = 1000000007; long long int gc(long long int a, long long int b) { if (b == 0) return a; return gc(b, a % b); } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } vector<bool> prime(1000005, true); vector<long long int> v; void sieve(long long int n) { prime[1] = false; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) v.push_back(i); } } inline long long int add(long long int a, long long int b) { return ((a % mod) + (b % mod)) % mod; } inline long long int sub(long long int a, long long int b) { return ((a % mod) - (b % mod) + mod) % mod; } inline long long int mul(long long int a, long long int b) { return ((a % mod) * (b % mod)) % mod; } long long int recur(long long int idx, long long int sum, vector<pair<long long int, long long int> >& v, vector<long long int>& b, vector<long long int>& pre, vector<long long int>& dp) { if (idx == b.size()) return 0; if (dp[idx] != -1) return dp[idx]; long long int temp = INT_MAX; temp = min(temp, recur(idx + 1, sum, v, b, pre, dp) + b[idx]); for (int i = 0; i < v.size(); i++) { long long int x = v[i].first; long long int y = v[i].second; if (idx + x - 1 >= b.size()) continue; long long int calc = pre[idx + x - 1] - pre[idx + y - 1]; temp = min(temp, recur(idx + x, sum, v, b, pre, dp) + calc); } dp[idx] = temp; return temp; } long long int dfs(long long int src, long long int p, vector<long long int>& a, vector<pair<long long int, long long int> > adj[], vector<long long int>& dp) { long long int can1 = 0; long long int can2 = 0; vector<pair<long long int, long long int> >::iterator it; for (it = adj[src].begin(); it != adj[src].end(); it++) { if (((*it).first == p)) continue; long long int st = max(0LL, dfs((*it).first, src, a, adj, dp) - (*it).second); if (st > can1) { can2 = can1; can1 = st; } else if (st > can2) { can2 = st; } } dp[src] = a[src] + can1 + can2; return (max(can1, can2) + a[src]); } struct comp1 { bool operator()(pair<long long int, long long int>& temp1, pair<long long int, long long int>& temp2) {} }; bool comp2(pair<long long int, long long int>& temp1, pair<long long int, long long int>& temp2) { return ((temp1.first < temp2.first) || ((temp1.first == temp2.first) && (temp1.second < temp2.second))); } long long int getinv(long long int temp) { return power(temp, mod - 2, mod); } void computeLPSArray(string pat, long long int M, vector<long long int>& lps) { int len = 0; lps[0] = 0; long long int i = 1; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } long long int fin(long long int u, vector<long long int>& parent) { if (u == parent[u]) return u; else return parent[u] = fin(parent[u], parent); } void unio(long long int u, long long int v, vector<long long int>& parent, vector<long long int>& ran) { u = fin(u, parent); v = fin(v, parent); if (u == v) return; if (ran[u] >= ran[v]) { parent[v] = u; ran[u] += ran[v]; } else { parent[u] = v; ran[v] += ran[u]; } } void update(int x, int delta, vector<long long int>& BIT) { long long int n = BIT.size() - 1; for (; x <= n; x += x & -x) BIT[x] += delta; } int query(int x, vector<long long int>& BIT) { int sum = 0; for (; x > 0; x -= x & -x) sum += BIT[x]; return sum; } int main() { long long int n, k; cin >> n >> k; vector<long long int> s; vector<long long int> s1; vector<long long int> s2; for (long long int i = 0; i < n; i++) { long long int t1, t2, t3; cin >> t1 >> t2 >> t3; if (t2 == 1 && t3 == 1) { s.push_back(t1); continue; } if (t2 == 0 && t3 == 0) continue; if (t2 == 1) s1.push_back(t1); if (t3 == 1) s2.push_back(t1); } if (((int)s.size() + (int)s1.size() < k) || ((int)s.size() + (int)s2.size() < k)) { cout << -1; return 0; } long long int c1 = 0; long long int c2 = 0; long long int idx1 = 0; long long int idx2 = 0; long long int idx3 = 0; long long int ans = 0; sort(s.begin(), s.end()); sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); while (idx1 < s.size() && idx2 < s1.size() && idx3 < s2.size()) { if (c1 >= k && c2 >= k) { break; } long long int a1 = s[idx1]; long long int a2 = s1[idx2]; long long int a3 = s2[idx3]; if (a2 + a3 < a1) { ans += (a2 + a3); c1++; c2++; idx2++; idx3++; } else { ans += a1; c1++; c2++; idx1++; } } if (c1 >= k && c2 >= k) { cout << ans << endl; return 0; } if (idx1 == s.size()) { for (long long int i = idx2; i < s1.size() && c1 < k; i++) { ans += s1[idx2]; c1++; } for (long long int i = idx3; i < s2.size() && c2 < k; i++) { ans += s2[idx3]; c2++; } cout << ans << endl; return 0; } if (idx2 == s1.size()) { long long int i; for (i = idx1; i < s.size() && c1 < k; i++) { ans += s[i]; c1++; c2++; } vector<long long int> v; for (; i < s.size(); i++) { v.push_back(s[i]); } for (i = idx3; i < s2.size(); i++) { v.push_back(s2[i]); } sort(v.begin(), v.end()); for (i = 0; i < v.size() && c2 < k; i++) { c2++; ans += v[i]; } cout << ans << endl; return 0; } if (idx3 == s2.size()) { long long int i; for (i = idx1; i < s.size() && c2 < k; i++) { ans += s[i]; c1++; c2++; } vector<long long int> v; for (; i < s.size(); i++) { v.push_back(s[i]); } for (i = idx2; i < s1.size(); i++) { v.push_back(s1[i]); } sort(v.begin(), v.end()); for (i = 0; i < v.size() && c1 < k; i++) { c1++; ans += v[i]; } cout << ans << endl; return 0; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <algorithm> struct _data { int t; char a; char b; bool operator()(_data& A, _data& B) { int score_a = A.a << 1 | A.b; int score_b = B.a << 1 | B.b; if (score_a < score_b) { return false; } if (score_a > score_b) { return true; } if (A.t > B.t) { return false; } if (A.t < B.t) { return true; } return false; } }; _data data[200000]; int main() { int n, k; scanf("%d %d", &n, &k); int i; int cnt_a = 0, cnt_b = 0; for (i = 0; i < n; i++) { int a, b; scanf("%d %d %d", &data[i].t, &a, &b); data[i].a = a; data[i].b = b; if (a == 1) cnt_a++; if (b == 1) cnt_b++; } if (cnt_a < k || cnt_b < k) { printf("-1"); return 0; } std::sort(data, data + n, _data()); int sum_t = 0; cnt_a = cnt_b = 0; int i_d1, i_d2, i_d3, i_p1, i_p2; // [0 ~ i_d1 - 1] : Alice & Bob // [i_d1 ~ i_d2 - 1] : Alice // [i_d2 ~ i_d3 - 1] : Bob // [i_d3 ~ n - 1] : Nobody for (i_d1 = 0; data[i_d1].a == 1 && data[i_d1].b == 1 && i_d1 < n; i_d1++); for (i_d2 = i_d1; data[i_d2].a == 1 && data[i_d2].b == 0 && i_d2 < n; i_d2++); for (i_d3 = i_d2; data[i_d3].a == 0 && data[i_d3].b == 1 && i_d3 < n; i_d3++); i = 0; i_p1 = i_d1; i_p2 = i_d2; // Alice & Bob while (i < i_d1 && (cnt_a < k && cnt_b < k)) { // Only Alice or Only Bob exists if ((i_d1 <= i_p1 && i_p1 < i_d2) && (i_d2 <= i_p2 && i_p2 < i_d3)) { if (data[i].t < data[i_p1].t + data[i_p2].t) { sum_t += data[i].t; cnt_a++; cnt_b++; } else { sum_t += (data[i_p1].t + data[i_p2].t); cnt_a++; i_p1++; cnt_b++; i_p2++; } } else { sum_t += data[i].t; cnt_a++; cnt_b++; } i++; } i = i_p1; // Only Alice while (i < i_d2 && (cnt_a < k)) { sum_t += data[i].t; cnt_a++; i++; } i = i_p2; // Only Bob while (i < i_d3 && (cnt_b < k)) { sum_t += data[i].t; cnt_b++; i++; } if (cnt_a >= k && cnt_b >= k) { printf("%d", sum_t); } else // Something's wrong { __asm nop; } return 0; }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct TP { TP() {} }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, a, b, t, aa = 0, bb = 0; scanf("%d %d", &n, &k); vector<pair<long long int, int>> alice, bob, both; for (int i = 0; i < n; i++) { scanf("%d %d %d", &t, &a, &b); if (a and !b and (int)(alice.size()) < k) alice.push_back({t, 1}), aa++; else if (b and !a and (int)(bob.size()) < k) bob.push_back({t, 2}), bb++; else if (a and b) both.push_back({t, 3}); } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(both.begin(), both.end()); while ((int)(both.size()) > 0) { if (aa < k or bb < k) { alice.insert(alice.begin(), {both[0].first, 3}); aa++; bb++; } else break; both.erase(both.begin()); } while ((int)(both.size()) > 0 and (int)(alice.size()) > 0 and (int)(bob.size()) > 0) { a = (int)(alice.size()) - 1; b = (int)(bob.size()) - 1; if (alice[a].second == 1 and bob[b].second == 2) { if (alice[a].first + bob[b].first > both[0].first) { alice.pop_back(); bob.pop_back(); alice.insert(alice.begin(), both[0]); both.erase(both.begin()); } else break; } else break; } while ((int)(alice.size()) > 0 and k < aa) { if (alice[(int)(alice.size()) - 1].second == 1) alice.pop_back(), aa--; } while ((int)(bob.size()) > 0 and k < bb) { if (bob[(int)(bob.size()) - 1].second == 2) bob.pop_back(), bb--; } if (aa < k or bb < k) printf("%d", -1); else { long long int ans = 0; for (int i = 0; i < (int)(alice.size()); i++) ans += alice[i].first; for (int i = 0; i < (int)(bob.size()); i++) ans += bob[i].first; printf("%lld", ans); } printf("\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.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
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Stack; import java.util.ArrayList; import java.util.Vector; import java.util.ArrayDeque; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author dauom */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); E2ReadingBooksHardVersion solver = new E2ReadingBooksHardVersion(); solver.solve(1, in, out); out.close(); } static class E2ReadingBooksHardVersion { public final void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); ArrayDeque<int[]> alice = new ArrayDeque<>(); ArrayDeque<int[]> bob = new ArrayDeque<>(); ArrayDeque<int[]> both = new ArrayDeque<>(); ArrayDeque<int[]> none = new ArrayDeque<>(); Stack<Integer> ansAlice = new Stack<>(); Stack<Integer> ansBob = new Stack<>(); Stack<Integer> ansBoth = new Stack<>(); Stack<Integer> ansNone = new Stack<>(); int[] t = new int[n]; for (int i = 0; i < n; i++) { t[i] = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); if (a == 1 && b == 1) { both.add(new int[] {t[i], i}); } else if (a == 1) { alice.add(new int[] {t[i], i}); } else if (b == 1) { bob.add(new int[] {t[i], i}); } else { none.add(new int[] {t[i], i}); } } sort(alice); sort(bob); sort(both); sort(none); int aliceCount = 0, bobCount = 0; int ans = 0; while (aliceCount < k && bobCount < k && !(alice.isEmpty() && bob.isEmpty() && both.isEmpty())) { if (alice.isEmpty()) { if (both.isEmpty()) { break; } ansBoth.add(both.peek()[1]); ans += both.poll()[0]; } else if (bob.isEmpty()) { if (both.isEmpty()) { break; } ansBoth.add(both.peek()[1]); ans += both.poll()[0]; } else if (both.isEmpty()) { ansBob.add(bob.peek()[1]); ansAlice.add(alice.peek()[1]); ans += alice.poll()[0] + bob.poll()[0]; } else { if (alice.peek()[0] + bob.peek()[0] < both.peek()[0]) { ansBob.add(bob.peek()[1]); ansAlice.add(alice.peek()[1]); ans += alice.poll()[0] + bob.poll()[0]; } else { ansBoth.add(both.peek()[1]); ans += both.poll()[0]; } } aliceCount++; bobCount++; } while (aliceCount < k && !alice.isEmpty()) { ansAlice.add(alice.peek()[1]); ans += alice.poll()[0]; ++aliceCount; } while (bobCount < k && !bob.isEmpty()) { ansBob.add(bob.peek()[1]); ans += bob.poll()[0]; ++bobCount; } if (aliceCount < k || bobCount < k) { out.println(-1); return; } while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m && !both.isEmpty() && !ansAlice.isEmpty() && !ansBob.isEmpty()) { ans -= t[ansBob.pop()] + t[ansAlice.pop()]; ans += both.peek()[0]; ansBoth.add(both.poll()[1]); } if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() > m) { out.println(-1); return; } while (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) { if (none.isEmpty()) { if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) { int idxBoth = ansBoth.peek(); ans += bob.peek()[0] + alice.peek()[0] - t[idxBoth]; ansBob.add(bob.poll()[1]); ansAlice.add(alice.poll()[1]); ansBoth.pop(); } else { break; } } else { if (!bob.isEmpty() && !alice.isEmpty() && !ansBoth.isEmpty()) { int idxBoth = ansBoth.peek(); int doubleCost = bob.peek()[0] + alice.peek()[0] - t[idxBoth]; int noneCost = none.peek()[0]; if (doubleCost < noneCost) { ansBob.add(bob.poll()[1]); ansAlice.add(alice.poll()[1]); ansBoth.pop(); ans += doubleCost; } else { ansNone.add(none.poll()[1]); ans += noneCost; } } else { ansNone.add(none.peek()[1]); ans += none.poll()[0]; } } } if (ansBob.size() + ansAlice.size() + ansBoth.size() + ansNone.size() < m) { out.println(-1); return; } out.println(ans); ArrayList<Integer> ansList = new ArrayList<>(); ansList.addAll(ansBob); ansList.addAll(ansAlice); ansList.addAll(ansBoth); ansList.addAll(ansNone); if (ansList.size() != m) { throw new RuntimeException(); } for (int x : ansList) { out.print((x + 1) + " "); } out.println(); } private void sort(ArrayDeque<int[]> q) { ArrayList<int[]> a = new ArrayList<>(q); a.sort(Comparators.singletonIntArr); q.clear(); q.addAll(a); } } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; public InputReader() { this.stream = System.in; } public InputReader(final InputStream stream) { this.stream = stream; } private final int read() { if (this.numChars == -1) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return -1; } } return this.buf[this.curChar++]; } } public final int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) {} byte sgn = 1; if (c == 45) { // 45 == '-' sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { // 48 == '0', 57 == '9' res *= 10; res += c - 48; // 48 == '0' c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } private static final boolean isSpaceChar(final int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; // 32 == ' ', 10 == '\n', 13 == '\r', 9 == '\t' } } static final class Comparators { public static final Comparator<int[]> singletonIntArr = (x, y) -> compare(x[0], y[0]); private static final int compare(final int x, final int y) { return x < y ? -1 : (x == y ? 0 : 1); } } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
//package round_653_div3; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class E1 { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] ip = br.readLine().split(" "); int n = Integer.parseInt(ip[0]); int k = Integer.parseInt(ip[1]); List<Info> list = new ArrayList<>(); List<Info> abList = new ArrayList<>(); long ans = 0; for(int i = 0 ; i < n ; i++) { ip = br.readLine().split(" "); int t = Integer.parseInt(ip[0]); int a = Integer.parseInt(ip[1]); int b = Integer.parseInt(ip[2]); if(a == 0 && b == 0) continue; if(a == 1 && b == 1 ) { list.add(new Info(t,a,b)); }else if(a == 0 && b == 1){ abList.add(new Info(t,a,b)); }else { abList.add(new Info(t,a,b)); } } //ans= -1; List<Info> finalList = new ArrayList<>(); finalList.addAll(list); int a_read = finalList.size(); int b_read = a_read; if(k-finalList.size() > 0) { Collections.sort(abList, (a,b) -> a.t- b.t); int idx = 0; while(a_read != k || b_read != k) { if(idx == abList.size()) break; Info temp = abList.get(idx++); finalList.add(temp); if(temp.a == 1) a_read++; if(temp.b == 1) b_read++; } } if(a_read != k || b_read != k) { ans = -1; }else{ for(Info a : finalList) { ans += a.t; } } System.out.println(ans); } } class Info{ int t; int a; int b; public Info(int t, int a, int b) { this.t = t; this.a = a; this.b = b; } public int hashCode() { return (t+"_"+a+"_"+b).hashCode(); } public boolean equals(Object obj) { if(obj instanceof Info) { Info objI = (Info)obj; return this.t == objI.t && this.a == objI.a && this.b == objI.b; } return false; } }
1374_E1. Reading Books (easy version)
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: t_i β€” the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. Input The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≀ t_i ≀ 10^4, 0 ≀ a_i, b_i ≀ 1), where: * t_i β€” the amount of time required for reading the i-th book; * a_i equals 1 if Alice likes the i-th book and 0 otherwise; * b_i equals 1 if Bob likes the i-th book and 0 otherwise. Output If there is no solution, print only one integer -1. Otherwise print one integer T β€” the minimum total reading time of the suitable set of books. Examples Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
{ "input": [ "8 4\n7 1 1\n2 1 1\n4 0 1\n8 1 1\n1 0 1\n1 1 1\n1 0 1\n3 0 0\n", "5 2\n6 0 0\n9 0 0\n1 0 1\n2 1 1\n5 1 0\n", "5 3\n3 0 0\n2 1 0\n3 1 0\n5 0 1\n3 0 1\n" ], "output": [ "18\n", "8\n", "-1\n" ] }
{ "input": [ "2 1\n7 1 1\n2 1 1\n", "5 1\n2 1 0\n2 0 1\n1 0 1\n1 1 0\n1 0 1\n", "6 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 1\n3 0 1\n3 1 0\n3 0 0\n", "6 3\n7 1 1\n8 0 0\n9 1 1\n6 1 0\n10 1 1\n5 0 0\n", "8 4 3\n1 1 1\n3 1 1\n12 1 1\n12 1 1\n4 0 0\n4 0 0\n5 1 0\n5 0 1\n", "6 3 1\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "3 3 1\n27 0 0\n28 0 0\n11 0 0\n", "1 1 1\n3 0 1\n", "8 5 1\n43 0 1\n5 0 1\n23 1 1\n55 0 1\n19 1 1\n73 1 1\n16 1 1\n42 1 1\n", "6 3 2\n6 0 0\n11 1 0\n9 0 1\n21 1 1\n10 1 0\n8 0 1\n", "9 2 2\n74 0 0\n78 1 0\n21 1 0\n47 1 0\n20 0 0\n22 0 1\n52 0 0\n78 0 0\n90 0 0\n", "3 2 1\n3 0 1\n3 1 0\n3 0 0\n", "27 5 1\n232 0 1\n72 0 1\n235 0 1\n2 0 1\n158 0 0\n267 0 0\n242 0 1\n1 0 0\n64 0 0\n139 1 1\n250 0 1\n208 0 1\n127 0 1\n29 0 1\n53 0 1\n100 0 1\n52 0 1\n229 0 0\n1 0 1\n29 0 0\n17 0 1\n74 0 1\n211 0 1\n248 0 1\n15 0 0\n252 0 0\n159 0 1\n", "6 4 3\n19 0 0\n6 1 1\n57 1 0\n21 0 1\n53 1 1\n9 1 1\n" ], "output": [ "2\n", "2\n", "38\n", "6\n", "26\n", "-1", "-1", "-1\n", "-1\n", "-1", "-1", "-1\n", "-1\n", "-1\n", "-1" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; // Author : Yash Shah public class E implements Runnable { public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=sc.nextInt(); int kk=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 time=sc.nextInt(); int a=sc.nextInt(); int b=sc.nextInt(); if(a==1 && b==1) { both.add(time); } else if(a==1) { alice.add(time); } else { bob.add(time); } } Collections.sort(alice); Collections.sort(bob); for(int i=0;i<Math.min(alice.size(),bob.size());i++) { both.add(alice.get(i)+bob.get(i)); } Collections.sort(both); if(both.size()<kk) { out.println(-1); out.close(); return; } long ans=0; for(int i=0;i<kk;i++) ans+=both.get(i); out.print(ans); out.close(); } //======================================================================== static class Pair { int a,b; Pair(int aa,int bb) { a=aa; b=bb; } } static void sa(int a[],InputReader sc) { for(int i=0;i<a.length;i++) { a[i]=sc.nextInt(); } Arrays.sort(a); } static class PairSort implements Comparator<Pair> { public int compare(Pair a,Pair b) { return b.b-a.b; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new E(),"Main",1<<27).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.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; public class ReadingBooksE { @SuppressWarnings("resource") public static void main(String [] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int k = s.nextInt(); int output = 0; ArrayList<Integer> Alice = new ArrayList<Integer>(); ArrayList<Integer> Bob = new ArrayList<Integer>(); ArrayList<Integer> intersect = new ArrayList<Integer>(); for (int i = 0 ; i< n ; i++) { int time = s.nextInt(); int alice = s.nextInt(); int bob = s.nextInt(); if(alice == 1 && bob == 1) { intersect.add(time); } else if(alice == 1 && bob == 0) { Alice.add(time); } else if(alice == 0 && bob == 1) { Bob.add(time); } } s.close(); if (Bob.size() + intersect.size() < k || Alice.size() + intersect.size() < k) { System.out.println(-1); return; } Collections.sort(intersect); Collections.sort(Alice); Collections.sort(Bob); while (!intersect.isEmpty() && k!=0) { output += intersect.remove(0); k--; } for (int i =0 ; i<k ;i++) { output+= Alice.remove(0); output+= Bob.remove(0); } if (output ==0) { System.out.println(-1); return; } System.out.println(output); } }