description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys n, k = map(int, input().split()) M = [] A = [] B = [] for i in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: M.append(t) elif a == 1: A.append(t) elif b == 1: B.append(t) if min(len(M) + len(A), len(M) + len(B)) < k: print(-1) sys.exit() A.append(0) B.append(0) M.append(0) A.sort() B.sort() M.sort() for i in range(len(A) - 1): A[i + 1] += A[i] for i in range(len(B) - 1): B[i + 1] += B[i] for i in range(len(M) - 1): M[i + 1] += M[i] ans = float("inf") for i in range(k + 1): j = k - i if len(A) > i and len(B) > i and len(M) > j: ans = min(ans, A[i] + B[i] + M[j]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) time = [] a = [] b = [] count_a = 0 count_b = 0 for i in range(n): t, a1, b1 = map(int, input().split()) time.append(t) a.append(a1) b.append(b1) if a1 == 1: count_a += 1 if b1 == 1: count_b += 1 if count_a < k or count_b < k: print(-1) else: both = [] a_not_b = [] b_not_a = [] for i in range(n): if a[i] == 1 and b[i] == 1: both.append(time[i]) elif a[i] == 1 and b[i] == 0: a_not_b.append(time[i]) elif a[i] == 0 and b[i] == 1: b_not_a.append(time[i]) a_not_b.sort() b_not_a.sort() both_len = len(both) a_not_b_len = len(a_not_b) b_not_a_len = len(b_not_a) final = [] req_len = min(a_not_b_len, b_not_a_len) for i in range(req_len): final.append(a_not_b[i] + b_not_a[i]) final.extend(both) final.sort() if len(final) < k: print(-1) else: ans = 0 for i in range(k): ans += final[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys input = sys.stdin.buffer.readline def solution(): a = [] b = [] c = [] n, k = map(int, input().split()) for i in range(n): t, x, y = map(int, input().split()) if x == 1 and y == 1: c.append(t) elif x == 1: a.append(t) elif y == 1: b.append(t) a.sort() b.sort() c.sort() ans = 0 if len(a) + len(c) < k or len(b) + len(c) < k: print(-1) else: z = 0 i = 0 j = 0 while k > 0: if i < len(c) and j < min(len(a), len(b)): if a[j] + b[j] <= c[i]: ans += a[j] + b[j] j += 1 else: ans += c[i] i += 1 elif i < len(c): ans += c[i] i += 1 else: ans += a[j] + b[j] j += 1 k -= 1 print(ans) solution()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import stdin, stdout input = stdin.readline print = stdout.write n, k = map(int, input().split()) alice, bob, together = [], [], [] for _ in range(n): t, a, b = map(int, input().split()) if a and b: together += (t,) elif a: alice += (t,) elif b: bob += (t,) sa, sb, st = len(alice), len(bob), len(together) if sa + st < k or sb + st < k: print("-1") exit() sa -= 1 sb -= 1 st -= 1 alice.sort(reverse=True) bob.sort(reverse=True) together.sort(reverse=True) time = 0 for i in range(k): if sa < 0 or sb < 0: time += together[st] st -= 1 elif st < 0: time += alice[sa] + bob[sb] sa -= 1 sb -= 1 elif together[st] <= alice[sa] + bob[sb]: time += together[st] st -= 1 else: time += alice[sa] + bob[sb] sa -= 1 sb -= 1 print(str(time))
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) tab = [list(map(int, input().split())) for _ in range(n)] x = [] y = [] z = [] for i in range(n): if tab[i][1] == tab[i][2] == 1: x.append(tab[i]) elif tab[i][1] == 1: y.append(tab[i]) elif tab[i][2] == 1: z.append(tab[i]) x.sort() y.sort() z.sort() sx = [0] * (len(x) + 1) sy = [0] * (len(y) + 1) sz = [0] * (len(z) + 1) for i in range(1, len(x) + 1): sx[i] = x[i - 1][0] for i in range(1, len(y) + 1): sy[i] = y[i - 1][0] for i in range(1, len(z) + 1): sz[i] = z[i - 1][0] for i in range(1, len(sx)): sx[i] += sx[i - 1] for i in range(1, len(sy)): sy[i] += sy[i - 1] for i in range(1, len(sz)): sz[i] += sz[i - 1] ans = 10**18 for i in range(k + 1): if not (0 <= i <= len(x) and 0 <= k - i <= len(y) and 0 <= k - i <= len(z)): continue res = sx[i] + sy[k - i] + sz[k - i] ans = min(ans, res) if ans == 10**18: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) al = [] bob = [] both = [] for i in range(n): a, b, c = map(int, input().split()) if b == 1 and c == 1: both.append(a) elif b == 1: al.append(a) elif c == 1: bob.append(a) tp = len(both) la = len(al) ob = len(bob) a = la + tp b = ob + tp if a < k or b < k: print("-1") else: both.sort() al.sort() bob.sort() ans = 0 c = 0 d = 0 for i in range(k): z = 20001 m = 20001 if c < tp: z = both[c] if d < la and d < ob: m = al[d] + bob[d] if z < m: ans += z c += 1 else: ans += m d += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) both, alice, bob = [], [], [] for _ in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: both.append(t) elif a == 1 and b == 0: alice.append(t) elif a == 0 and b == 1: bob.append(t) both.sort(reverse=True) alice.sort(reverse=True) bob.sort(reverse=True) cnt = time = 0 while cnt < k and both and alice and bob: if both[-1] <= alice[-1] + bob[-1]: time += both.pop() else: time += alice.pop() + bob.pop() cnt += 1 while cnt < k and both: time += both.pop() cnt += 1 while cnt < k and alice and bob: time += alice.pop() + bob.pop() cnt += 1 if cnt >= k: print(time) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().strip().split()) not_possible = False alice_only, bob_only, both = [], [], [] alice_likes, bob_likes = 0, 0 total_time = 0 for _ in range(n): t, alice, bob = map(int, input().strip().split()) alice_likes += alice bob_likes += bob if alice + bob == 2: both.append(t) elif alice: alice_only.append(t) elif bob: bob_only.append(t) if alice + bob > 0: total_time += t if alice_likes < k or bob_likes < k: not_possible = True alice_remove = alice_likes - k bob_remove = bob_likes - k both_remove = min(alice_remove, bob_remove) alice_remove -= both_remove bob_remove -= both_remove both.sort(reverse=True) alice_only.sort(reverse=True) bob_only.sort(reverse=True) a, b = 0, 0 while a < len(alice_only) and alice_remove > 0: total_time -= alice_only[a] a += 1 alice_remove -= 1 while b < len(bob_only) and bob_remove > 0: total_time -= bob_only[b] b += 1 bob_remove -= 1 alice_only = alice_only[a:] bob_only = bob_only[b:] ab, o = 0, 0 while both_remove > 0: curr_ab, curr_both = 0, 0 if ab < len(alice_only) and ab < len(bob_only): curr_ab = alice_only[ab] + bob_only[ab] if o < len(both): curr_both = both[o] if curr_ab + curr_both == 0: break if curr_ab > curr_both: total_time -= curr_ab ab += 1 else: total_time -= curr_both o += 1 both_remove -= 1 alice_remove += both_remove bob_remove += both_remove a, b = ab, ab while a < len(alice_only) and alice_remove > 0: total_time -= alice_only[a] a += 1 alice_remove -= 1 while b < len(bob_only) and bob_remove > 0: total_time -= bob_only[b] b += 1 bob_remove -= 1 if not_possible: print(-1) else: print(total_time)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR LIST LIST LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
t = 1 for _ in range(t): n, k = map(int, input().split()) arr = [] for i in range(n): a, b, c = map(int, input().split()) arr.append((a, b, c)) arr = sorted(arr, key=lambda x: x[0]) ans = 0 x = y = k counter = 0 temp1 = [] temp2 = [] while counter < n: if x <= 0 and y <= 0: break if arr[counter][1] == 0 and arr[counter][2] == 0: counter += 1 continue ans += arr[counter][0] x -= arr[counter][1] y -= arr[counter][2] if arr[counter][1] and not arr[counter][2]: temp1.append(arr[counter][0]) elif not arr[counter][1] and arr[counter][2]: temp2.append(arr[counter][0]) counter += 1 x_d = min(len(temp1), len(temp2)) ans -= sum(temp1) + sum(temp2) temp1 = temp1[0:x_d] temp2 = temp2[0:x_d] ans += sum(temp1) + sum(temp2) if len(temp1) > 0 and len(temp2) > 0: temp1 = sorted(temp1) temp2 = sorted(temp2) for i in range(counter, n): if arr[i][1] == 1 and arr[i][2] == 1: if arr[i][0] < temp1[-1] + temp2[-1]: ans += arr[i][0] - (temp1[-1] + temp2[-1]) temp1.pop() temp2.pop() if not (len(temp1) and len(temp2)): break if x > 0 or y > 0: print(-1) else: print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
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]) count10 = len(book_dict["10"]) count01 = len(book_dict["01"]) count2 = len(book_dict["2"]) result = 0 book_dict["10"].sort() book_dict["01"].sort() if count10 + count2 >= total and count01 + count2 >= total: for i in range(min(count01, count10)): book_dict["2"].append(book_dict["01"][i] + book_dict["10"][i]) book_dict["2"].sort() result = sum(book_dict["2"][:total]) print(result) else: print(-1)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT STRING STRING STRING LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR STRING VAR VAR STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, m, k = input().split(" ") n, m, k = int(n), int(m), int(k) A = [] B = [] C = [] D = [] Ainmax = 0 Binmax = 0 Cinmax = 0 Dinmax = 0 for i in range(n): entry = input().split(" ") if entry[1] == "1" and entry[2] == "1": C.append([int(entry[0]), i + 1]) Cinmax += 1 elif entry[1] == "1" and entry[2] == "0": A.append([int(entry[0]), i + 1]) Ainmax += 1 elif entry[1] == "0" and entry[2] == "1": B.append([int(entry[0]), i + 1]) Binmax += 1 else: D.append([int(entry[0]), i + 1]) Dinmax += 1 A.sort(key=lambda x: x[0]) B.sort(key=lambda x: x[0]) C.sort(key=lambda x: x[0]) D.sort(key=lambda x: x[0]) mi = min(Ainmax, Binmax) if len(C) + mi < k: print(-1) elif len(C) < k and 2 * k - len(C) > m: print(-1) else: time = 0 Ain = 0 Bin = 0 Cin = 0 Din = 0 ABinmax = min(mi, m - k) for i in range(k): if Ain == ABinmax: Cin += 1 elif Cin == Cinmax or A[Ain][0] + B[Bin][0] <= C[Cin][0]: Ain += 1 Bin += 1 else: Cin += 1 for i in range(m - Ain - Bin - Cin): pot = [] if Ain < Ainmax: pot.append([A[Ain][0], "Ain+=1"]) if Bin < Binmax: pot.append([B[Bin][0], "Bin+=1"]) if Cin < Cinmax: pot.append([C[Cin][0], "Cin+=1"]) if Din < Dinmax: pot.append([D[Din][0], "Din+=1"]) if Ain < Ainmax and Bin < Binmax and Cin != 0: pot.append([A[Ain][0] + B[Bin][0] - C[Cin - 1][0], "Ain+=1;Bin+=1;Cin-=1"]) minpot = 0 for j in range(len(pot) - 1): if pot[minpot][0] > pot[j + 1][0]: minpot = j + 1 exec(pot[minpot][1]) for i in range(Ain): time += A[i][0] for i in range(Bin): time += B[i][0] for i in range(Cin): time += C[i][0] for i in range(Din): time += D[i][0] print(time) for i in range(Ain): print(A[i][1], end=" ") for i in range(Bin): print(B[i][1], end=" ") for i in range(Cin): print(C[i][1], end=" ") for i in range(Din): print(D[i][1], end=" ")
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER STRING IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = [int(k) for k in input().split()] tog = [] ali = [] bli = [] time = 0 size = 0 for i in range(n): t, a, b = [int(k) for k in input().split()] if a & b == 1: tog.append(t) else: if a == 1: ali.append(t) if b == 1: bli.append(t) asi = len(tog) + len(ali) bsi = len(tog) + len(bli) if k > min(asi, bsi): print(-1) else: ali.sort() bli.sort() for i in range(min(len(ali), len(bli))): tog.append(ali[i] + bli[i]) tog.sort() for i in range(k): time += tog[i] print(time)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import stdin input = stdin.readline def answer(): if n3 + n1 < k or n3 + n2 < k: return -1 i, j = 0, 0 ans = 0 for take in range(k): if i >= n1 or i >= n2: ans += common[j] j += 1 elif j >= n3: ans += a[i] + b[i] i += 1 elif a[i] + b[i] > common[j]: ans += common[j] j += 1 else: ans += a[i] + b[i] i += 1 return ans n, k = map(int, input().split()) a, b, common = [], [], [] for i in range(n): t, x, y = map(int, input().split()) if x and y: common.append(t) elif x == 1 and y == 0: a.append(t) elif x == 0 and y == 1: b.append(t) common.sort() a.sort() b.sort() n1, n2, n3 = len(a), len(b), len(common) print(answer())
ASSIGN VAR VAR FUNC_DEF IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = tuple(map(int, input().split())) alice = [] bob = [] both = [] for _ in range(n): t, a, b = tuple(map(int, input().split())) if a == 1 and b == 1: both.append(t) elif a == 1: alice.append(t) elif b == 1: bob.append(t) both.sort() alice.sort() bob.sort() remain = 0 if k <= len(both): result = sum(both[:k]) else: remain = k - len(both) if remain > len(bob) or remain > len(alice): result = -1 else: result = sum(both) result += sum(alice[:remain]) + sum(bob[:remain]) index = max(0, k - len(both)) lenbob = len(bob) lenalice = len(alice) if result != -1: for i in range(min(k, len(both)) - 1, -1, -1): if index > lenbob - 1 or index > lenalice - 1: break newresult = result - both[i] + alice[index] + bob[index] index += 1 if newresult < result: result = newresult else: break print(result)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import exit, stdin input = stdin.readline def i(): return input() def ii(): return int(input()) def iis(): return map(int, input().split()) def liis(): return list(map(int, input().split())) def print_array(a): print(" ".join(map(str, a))) n, k = iis() alice = [] bob = [] both = [] for _ in range(n): t, a, b = iis() if a == 1 and b == 0: alice.append(t) elif a == 0 and b == 1: bob.append(t) elif a == 1 and b == 1: both.append(t) alice = sorted(alice)[::-1] bob = sorted(bob)[::-1] both = sorted(both)[::-1] time = 0 livros = 0 while k > 0 and (len(both) != 0 or len(alice) != 0 and len(bob) != 0): if len(both) and len(alice) and len(bob): separado = alice[-1] + bob[-1] junto = both[-1] if junto < separado: time += junto both.pop() else: time += separado alice.pop() bob.pop() elif len(both): time += both.pop() elif len(alice) and len(bob): time += alice.pop() time += bob.pop() else: continue k -= 1 if k == 0: print(time) else: print(-1)
ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys input = sys.stdin.readline inp, ip = lambda: int(input()), lambda: [int(w) for w in input().split()] n, k = ip() t, a, b = [0] * n, [0] * n, [0] * n both = [] alice, bob = [], [] for i in range(n): t[i], a[i], b[i] = ip() if a[i] and b[i]: both.append(t[i]) elif a[i]: alice.append(t[i]) elif b[i]: bob.append(t[i]) if a.count(1) < k or b.count(1) < k: print(-1) exit() alice.sort() bob.sort() m = min(len(alice), len(bob)) for i in range(m): both.append(alice[i] + bob[i]) both.sort() print(sum(both[:k]))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys input = sys.stdin.readline n, minn = map(int, input().split()) a = [] ca, cb = 0, 0 for i in range(n): t, x, y = map(int, input().split()) if x == 1: ca += 1 if y == 1: cb += 1 a.append((t, x, y)) if ca < minn or cb < minn: print(-1) return ca, cb = 0, 0 alice = [] bob = [] both = [] for i in range(n): if a[i][1] == a[i][2] == 1: both.append(a[i][0]) elif a[i][1] != a[i][2]: if a[i][1] == 1: alice.append(a[i][0]) else: bob.append(a[i][0]) alice.sort() bob.sort() both.sort() i, j, k = 0, 0, 0 ans = 0 while ca < minn or cb < minn: if (i < len(alice) and j < len(bob) and k < len(both)) and alice[i] + bob[ j ] <= both[k]: ans += alice[i] + bob[j] ca += 1 cb += 1 i += 1 j += 1 elif (i < len(alice) and j < len(bob) and k < len(both)) and alice[i] + bob[ j ] >= both[k]: ans += both[k] ca += 1 cb += 1 k += 1 else: break if i == len(alice): while ca < minn: ans += both[k] k += 1 ca += 1 cb += 1 if j == len(bob): while cb < minn: ans += both[k] k += 1 ca += 1 cb += 1 if k == len(both): while ca < minn: ans += alice[i] i += 1 ca += 1 while cb < minn: ans += bob[j] j += 1 cb += 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = list(map(int, input().split())) a = [] b = [] comb = [] for _ in range(n): arr = list(map(int, input().split())) if arr[1] == 1 and arr[2] == 1: comb.append(arr[0]) elif arr[1] == 1: a.append(arr[0]) elif arr[2] == 1: b.append(arr[0]) if len(a) + len(comb) < k or len(b) + len(comb) < k: print(-1) exit() a.sort() b.sort() comb.sort() ans = 0 cnt = 0 i = 0 j = 0 sep = min(len(a), len(b)) joint = len(comb) while cnt < k: if i >= sep: ans += comb[j] j += 1 cnt += 1 elif j >= joint: ans += a[i] + b[i] i += 1 cnt += 1 elif a[i] + b[i] <= comb[j]: ans += a[i] + b[i] i += 1 cnt += 1 else: ans += comb[j] j += 1 cnt += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) d = {"a": [], "b": [], "c": []} for _ in range(n): t, a, b = map(int, input().split()) if (a, b) == (1, 0): d["a"].append(t) if (a, b) == (0, 1): d["b"].append(t) if (a, b) == (1, 1): d["c"].append(t) d["a"].sort() d["b"].sort() d["c"].sort() a = d["a"] b = d["b"] c = d["c"] a1 = [0] b1 = [0] c1 = [0] num = 0 for el in a: num += el a1.append(num) num = 0 for el in b: num += el b1.append(num) num = 0 for el in c: num += el c1.append(num) if len(a) + len(c) < k or len(b) + len(c) < k: print(-1) else: times = [] i = 0 while i <= k: try: temp = c1[i] + a1[k - i] + b1[k - i] times.append(temp) except IndexError: pass finally: i += 1 print(min(times))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
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 = 999999999999999999999999999 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)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = [int(x) for x in input().split()] at = [] bt = [] ct = [] for i in range(n): ti, ai, bi = [int(x) for x in input().split()] if ai == 1 and bi == 1: ct.append(ti) elif ai == 1: at.append(ti) elif bi == 1: bt.append(ti) else: continue if len(ct) + len(at) < k or len(ct) + len(bt) < k: print(-1) else: ci = 0 ai = 0 al = k ans = 0 ct.sort() at.sort() bt.sort() i = 0 while i < len(ct) and al > 0 and ai < min(len(at), len(bt)): if ct[i] < at[ai] + bt[ai]: ans += ct[i] i += 1 else: ans += at[ai] + bt[ai] ai += 1 al -= 1 if i == len(ct) and al != 0: bl = al ans += sum(at[ai : ai + al]) + sum(bt[ai : ai + al]) elif ai == min(len(at), len(bt)) and al > 0: ans += sum(ct[i : i + al]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys input = sys.stdin.readline f = lambda: list(map(int, input().strip("\n").split())) n, k = f() _11 = [] _01 = [] _10 = [] for _ in range(n): t, a, b = f() if a and b: _11.append(t) elif a: _10.append(t) elif b: _01.append(t) _01.sort() _10.sort() _11.sort() for i in range(1, len(_01)): _01[i] += _01[i - 1] for i in range(1, len(_10)): _10[i] += _10[i - 1] for i in range(1, len(_11)): _11[i] += _11[i - 1] ans = 3 * 1000000000.0 if len(_01) >= k and len(_10) >= k: ans = min(ans, _01[k - 1] + _10[k - 1]) for i in range(len(_11)): if i + 1 < k and len(_01) >= k - i - 1 and len(_10) >= k - i - 1: ans = min(ans, _11[i] + _01[k - i - 2] + _10[k - i - 2]) elif len(_11) >= k: ans = min(ans, _11[k - 1]) break print(-1 if ans == 3 * 1000000000.0 else ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER NUMBER VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) la, lb, lc = [], [], [] for _ in range(n): t, a, b = map(int, input().split()) if a == b == 1: lc.append(t) elif a == 1 and b == 0: la.append(t) elif a == 0 and b == 1: lb.append(t) la.sort() lb.sort() lc.sort() t = 0 ln = len(lc) if ln >= k: t += sum(lc[:k]) a, b = k, k ln = k else: t += sum(lc) a, b = ln, ln if a + len(la) < k or b + len(lb) < k: print(-1) else: t += sum(la[: k - a]) + sum(lb[: k - b]) u, v = k - a, k - b while u < len(la) and v < len(lb) and ln > 0: if t - lc[ln - 1] + la[u] + lb[v] < t: t = t - lc[ln - 1] + la[u] + lb[v] u += 1 v += 1 ln -= 1 else: break print(t)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, m, k = map(int, input().split()) ab = [] a = [] b = [] other = [] l = [list(map(int, input().split())) for i in range(n)] for i in range(n): t, c, d = l[i] if c and d == 0: a.append([t, i + 1]) elif d and c == 0: b.append([t, i + 1]) elif c * d: ab.append([t, i + 1]) else: other.append([t, i + 1]) a.sort() b.sort() ab.sort() other.sort() la = len(a) lb = len(b) lab = len(ab) lo = len(other) a.append([float("INF"), -1]) b.append([float("INF"), -1]) ab.append([float("INF"), -1]) other.append([float("INF"), -1]) if la < lb: la, lb = lb, la a, b = b, a ans = float("INF") count = 0 ia = 0 ib = 0 iab = 0 io = 0 ana = 0 anb = 0 anab = 0 ano = 0 for i in range(lab + 1): if k - i > lb: continue if 2 * k - i > m: continue if i + la + lb + lo < m: continue if ia > 0: ia -= 1 count -= a[ia][0] if ib > 0: ib -= 1 count -= b[ib][0] if io > 0: io -= 1 count -= other[io][0] while ia < la and ia < k - i: count += a[ia][0] ia += 1 while ib < lb and ib < k - i: count += b[ib][0] ib += 1 while iab < lab and iab < i: count += ab[iab][0] iab += 1 while ia + ib + iab + io < m: na = a[ia][0] nb = b[ib][0] no = other[io][0] mi = min(na, nb, no) if mi == na: count += na ia += 1 elif mi == nb: count += nb ib += 1 else: count += no io += 1 if count < ans: ans = count ana = ia anb = ib anab = iab ano = io if ans == float("INF"): print(-1) else: print(ans) l = [] for i in range(ana): l.append(a[i][1]) for i in range(anb): l.append(b[i][1]) for i in range(anab): l.append(ab[i][1]) for i in range(ano): l.append(other[i][1]) print(*l)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR STRING NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def solve(): global B, K both = [b[0] for b in B if b[1] and b[2]] alice = sorted([b[0] for b in B if b[1] and not b[2]]) bob = sorted([b[0] for b in B if b[2] and not b[1]]) for i in range(min(len(alice), len(bob))): both.append(alice[i] + bob[i]) if len(both) < K: return -1 return sum(sorted(both)[:K]) N, K = map(int, input().split()) B = [tuple(map(int, input().split())) for _ in range(N)] sol = solve() print(sol)
FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) alice = [] bob = [] both = [] for _ in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: both.append((t, a, b)) elif a == 1 and b == 0: alice.append((t, a, b)) elif a == 0 and b == 1: bob.append((t, a, b)) alice.sort() bob.sort() both.sort() ans = 0 ca = 0 cb = 0 ia = 0 ib = 0 iboth = 0 while ca < k and cb < k and (ia < len(alice) and ib < len(bob) or iboth < len(both)): if ( iboth >= len(both) or ia < len(alice) and ib < len(bob) and alice[ia][0] + bob[ib][0] < both[iboth][0] ): ans += alice[ia][0] + bob[ib][0] ia += 1 ib += 1 else: ans += both[iboth][0] iboth += 1 ca += 1 cb += 1 if cb >= k and ca >= k: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) a, b, ab = [], [], [] for _ in range(n): t, x, y = map(int, input().split()) if x == 1 and y == 1: ab.append(t) elif x == 1 and y == 0: a.append(t) elif x == 0 and y == 1: b.append(t) if len(ab) + min(len(a), len(b)) < k: print(-1) else: cost, ind, tog = 0, 0, 0 a.sort() b.sort() ab.sort() for i in range(k): if tog == len(ab): cost += sum(a[ind : ind + (k - i)]) + sum(b[ind : ind + (k - i)]) break elif ind == min(len(a), len(b)): cost += sum(ab[tog : tog + (k - i)]) break elif a[ind] + b[ind] < ab[tog]: cost += a[ind] + b[ind] ind += 1 else: cost += ab[tog] tog += 1 print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) alice = [] bob = [] both = [] for _ in range(n): t, a, b = map(int, input().split()) if a and b: both.append(t) elif a: alice.append(t) elif b: bob.append(t) if len(alice) + len(both) < k or len(bob) + len(both) < k: print(-1) else: alice.sort() bob.sort() both.sort() i = j = 0 ans = 0 while i < len(alice) and i < len(bob) and j < len(both) and k > 0: if alice[i] + bob[i] < both[j]: ans += alice[i] + bob[i] i += 1 else: ans += both[j] j += 1 k -= 1 if k: if j >= len(both): while k > 0: ans += alice[i] + bob[i] i += 1 k -= 1 else: while k > 0: ans += both[j] j += 1 k -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR IF VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k1 = map(int, input().split()) c = [] a = [] b = [] for i in range(n): x, y, z = map(int, input().split()) if y == z == 1: c.append(x) elif y == 1 and z == 0: a.append(x) elif y == 0 and z == 1: b.append(x) a.sort() b.sort() c.sort() ka = k1 kb = k1 i, j, k = 0, 0, 0 ans = 0 while ka > 0 and kb > 0 and i < len(a) and j < len(b) and k < len(c): if a[i] + b[j] <= c[k]: ans += a[i] + b[j] ka -= 1 kb -= 1 i += 1 j += 1 else: ans += c[k] ka -= 1 kb -= 1 k += 1 if i >= len(a) and ka > 0: while k < len(c) and ka > 0: ans += c[k] k += 1 ka -= 1 kb = max(0, kb - 1) elif j >= len(b) and kb > 0: while k < len(c) and kb > 0: ans += c[k] k += 1 ka = max(0, ka - 1) kb -= 1 while i < len(a) and ka > 0: ans += a[i] ka -= 1 i += 1 while j < len(b) and kb > 0: ans += b[j] kb -= 1 j += 1 if ka == kb == 0: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) z11, z01, z10 = [], [], [] for i in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: z11.append(t) elif a == 1: z10.append(t) elif b == 1: z01.append(t) i, j = min(k, len(z11)), min(k, len(z01), len(z10)) z11.sort() z10.sort() z01.sort() if i + j < k: print(-1) exit() while i + j > k: if z11[i - 1] > z10[j - 1] + z01[j - 1]: i -= 1 else: j -= 1 print(sum(z11[:i]) + sum(z10[:j]) + sum(z01[:j]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) a, b, t = [], [], [] for i in range(0, n): tm, c, d = map(int, input().split()) if c == 1 and d == 1: t.append(tm) elif c == 1 and d == 0: a.append(tm) elif c == 0 and d == 1: b.append(tm) if len(t) + len(a) < k or len(t) + len(b) < k: print(-1) else: t.sort() a.sort() b.sort() i = 0 j = 0 ans = 0 fga = 0 fgt = 0 while k != 0: if len(a) <= j or len(b) <= j: fga = 1 break elif len(t) <= i: fgt = 1 break elif t[i] > a[j] + b[j]: ans += a[j] + b[j] j += 1 elif t[i] <= a[j] + b[j]: ans += t[i] i += 1 k -= 1 if fga == 1 and fgt == 0: while k != 0: ans += t[i] i += 1 k -= 1 elif fga == 0 and fgt != 0: while k != 0: ans += a[j] + b[j] j += 1 k -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) ans = [] c1 = 0 c2 = 0 for i in range(n): z = list(map(int, input().split())) if z[1] == 1: c1 += 1 if z[2] == 1: c2 += 1 ans.append(z) if min(c1, c2) < k: print(-1) else: a0 = [] a1 = [] a2 = [] for i in range(len(ans)): if ans[i][1] == 1 and ans[i][2] == 1: a0.append(ans[i]) elif ans[i][1] == 1 and ans[i][2] == 0: a1.append(ans[i]) elif ans[i][1] == 0 and ans[i][2] == 1: a2.append(ans[i]) p0 = 0 p1 = 0 p2 = 0 c1 = 0 c2 = 0 a0.sort() a1.sort() a2.sort() total = 0 while c1 < k and c2 < k: if ( p0 < len(a0) and p1 < len(a1) and p2 < len(a2) and a0[p0][0] <= a1[p1][0] + a2[p2][0] ): total += a0[p0][0] c1 += 1 c2 += 1 p0 += 1 else: if p1 < len(a1) and p2 < len(a2): total += a1[p1][0] + a2[p2][0] c1 += 1 c2 += 1 p1 += 1 p2 += 1 continue if p1 == len(a1) or p2 == len(a2): total += a0[p0][0] c1 += 1 c2 += 1 p0 += 1 print(total)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import itertools def good(combo, k): alice = 0 bob = 0 for t, a, b in combo: if a == 1: alice += 1 if b == 1: bob += 1 return alice >= k and bob >= k def brute(arr, k): result = float("inf") for comboSize in range(len(arr) - 1, 0, -1): for combo in itertools.combinations(arr, comboSize): if good(combo, k): curr = sum(t for t, _, _ in combo) if curr < result: result = curr if result == float("inf"): return -1 return result def fast(arr, k): anums = [] bnums = [] cnums = [] for t, a, b in arr: if a == 1 and b == 0: anums.append(t) elif a == 0 and b == 1: bnums.append(t) elif a == 1 and b == 1: cnums.append(t) anums.sort() bnums.sort() cnums.sort() result = 0 a, b, c = 0, 0, 0 alice = 0 bob = 0 while alice < k and bob < k: if a < len(anums) and b < len(bnums): up1 = anums[a] + bnums[b] else: up1 = float("inf") if c < len(cnums): up2 = cnums[c] else: up2 = float("inf") if up1 == float("inf") and up2 == float("inf"): return -1 if up1 < up2: a += 1 b += 1 result += up1 else: c += 1 result += up2 alice += 1 bob += 1 return result def main(): n, k = [int(x) for x in input().split()] arr = [] for _ in range(n): t, a, b = [int(x) for x in input().split()] arr.append((t, a, b)) result = fast(arr, k) print(result) main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys input = sys.stdin.readline def main(): n, k = map(int, input().split()) a_s = [] b_s = [] ab_s = [] for _ in range(n): t, a, b = map(int, input().split()) if a == b == 1: ab_s.append(t) elif a == 1: a_s.append(t) elif b == 1: b_s.append(t) la = len(a_s) lb = len(b_s) lab = len(ab_s) if la + lab < k or lb + lab < k: print(-1) return a_s.sort() b_s.sort() for i in range(min(la, lb)): tmp = a_s[i] + b_s[i] ab_s.append(tmp) ab_s.sort() print(sum(ab_s[:k])) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import stdin inp = lambda: stdin.readline().strip() n, k = [int(x) for x in inp().split()] b = [] for _ in range(n): b.append([int(x) for x in inp().split()]) both = [] alice = [] bob = [] for i in b: if i[1] == 1 and i[2] == 1: both.append(i[0]) elif i[1] == 1: alice.append(i[0]) elif i[2] == 1: bob.append(i[0]) if len(alice) + len(both) < k or len(bob) + len(both) < k: print(-1) exit() both.sort() alice.sort() bob.sort() b = 0 ind = 0 cost = 0 liked = 0 minimum = min(len(alice), len(bob)) x = max(k - minimum, 0) for i in range(x): if liked == k: print(cost) exit() cost += both[i] b += 1 liked += 1 while True: if liked == k: print(cost) break if ( b < len(both) and ind < len(alice) and ind < len(bob) and both[b] <= alice[ind] + bob[ind] ): cost += both[b] b += 1 else: cost += alice[ind] + bob[ind] ind += 1 liked += 1
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys input = sys.stdin.readline n, k = map(int, input().split()) books = [tuple(map(int, input().split())) for _ in range(n)] books.sort(key=lambda x: (x[0], -x[1], -x[2])) ans = 0 comm = [] cnt_a = [] cnt_b = [] for t, a, b in books: if a == 0 and b == 0: pass elif a == 1 and b == 1: if len(cnt_a) + len(comm) < k or len(cnt_b) + len(comm) < k: comm.append(t) elif cnt_a and cnt_b and cnt_a[-1] + cnt_b[-1] > t: cnt_a.pop() cnt_b.pop() comm.append(t) elif a == 1 and len(cnt_a) + len(comm) < k: cnt_a.append(t) elif b == 1 and len(cnt_b) + len(comm) < k: cnt_b.append(t) else: pass while cnt_a and len(cnt_a) + len(comm) > k: cnt_a.pop() while cnt_b and len(cnt_b) + len(comm) > k: cnt_b.pop() if len(cnt_a) + len(comm) < k or len(cnt_b) + len(comm) < k: print(-1) else: print(sum(cnt_a) + sum(cnt_b) + sum(comm))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) common_books = [] alice_books = [] bob_books = [] for i in range(n): duration, alice, bob = map(int, input().split()) if alice and bob: collection = common_books elif alice: collection = alice_books elif bob: collection = bob_books else: collection = None if collection is not None: collection.append(duration) def get0(collection, idx): if idx < len(collection): return collection[idx] return 0 if len(bob_books) + len(common_books) < k or len(alice_books) + len(common_books) < k: print(-1) else: for collection in (common_books, alice_books, bob_books): collection.sort() icom = 0 iali = 0 ibob = 0 ncom = len(common_books) nali = len(alice_books) nbob = len(bob_books) total = 0 for i in range(k): if ( icom < ncom and get0(alice_books, iali) + get0(bob_books, ibob) >= common_books[icom] ): total += common_books[icom] icom += 1 elif iali < nali and ibob < nbob: total += alice_books[iali] total += bob_books[ibob] iali += 1 ibob += 1 else: total += common_books[icom] icom += 1 print(total)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR NONE EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
book_number, like_num = map(int, input().split()) both_like = [] only_alice = [] only_bob = [] for _ in range(book_number): t, a, b = map(int, input().split()) if a == b == 1: both_like.append(t) elif a == 1: only_alice.append(t) elif b == 1: only_bob.append(t) i, j = 0, 0 time = 0 current_like = 0 both_like.sort() only_alice.sort() only_bob.sort() while current_like < like_num: if i < len(both_like) and j < min(len(only_alice), len(only_bob)): if both_like[i] < only_alice[j] + only_bob[j]: time += both_like[i] i += 1 else: time += only_alice[j] + only_bob[j] j += 1 current_like += 1 elif i < len(both_like): time += both_like[i] i += 1 current_like += 1 elif j < min(len(only_alice), len(only_bob)): time += only_alice[j] + only_bob[j] j += 1 current_like += 1 else: break if current_like == like_num: print(time) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, kk = map(int, input().split()) alice = [] bob = [] both = [] for i in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: both.append(t) elif a == 1: alice.append(t) elif b == 1: bob.append(t) alice.sort() bob.sort() both.sort() count = 0 l = min(len(alice), len(bob)) i = 0 j = 0 k = 0 ans = 0 if len(both) + len(bob) < kk or len(alice) + len(both) < kk: print(-1) else: while j < l and count < kk: if k >= len(both) or alice[j] + bob[j] < both[k]: ans += alice[j] + bob[j] j += 1 count += 1 else: ans += both[k] k += 1 count += 1 print(ans + sum(both[k : k + kk - count]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def next(): return [int(x) for x in input().split()] class book: def __init__(self, t, a, b): self.t = t self.a = a self.b = b n, k = next() books = [] for _ in range(n): t, a, b = next() books.append(book(t, a, b)) oo = list(map(lambda x: x.t, filter(lambda x: x.a == 1 and x.b == 1, books))) zo = list(sorted(map(lambda x: x.t, filter(lambda x: x.a == 0 and x.b == 1, books)))) oz = list(sorted(map(lambda x: x.t, filter(lambda x: x.a == 1 and x.b == 0, books)))) mi = min(len(zo), len(oz)) for i in range(mi): oo.append(zo[i] + oz[i]) if len(oo) < k: print(-1) else: print(sum(sorted(oo)[:k]))
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def func(): n, k = map(int, input().split()) sa, sb, both = [], [], [] for _ in range(n): t, aLike, bLike = map(int, input().split()) if aLike and bLike: both.append(t) elif aLike: sa.append(t) elif bLike: sb.append(t) sa.sort() sb.sort() both.sort() if min(len(sa), len(sb)) + len(both) < k: print(-1) return ans = 2**32 common = 0 if len(sa) >= k and len(sb) >= k else k - min(len(sa), len(sb)) cur = sum(both[:common]) + sum(sa[: k - common]) + sum(sb[: k - common]) ans = min(cur, ans) while common < min(k, len(both)): cur = cur + both[common] - sa[k - common - 1] - sb[k - common - 1] ans = min(cur, ans) common += 1 print(ans) func()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import stdin n, k = list(map(int, stdin.readline().split())) alice = [] bob = [] both = [] for _ in range(n): t, a, b = list(map(int, stdin.readline().split())) if a == b == 1: both.append(t) elif a == 1: alice.append(t) elif b == 1: bob.append(t) alice = sorted(alice) bob = sorted(bob) both = sorted(both) a, b, c = 0, 0, 0 res = 0 for _ in range(k): if c >= len(both) and (a >= len(alice) or b >= len(bob)): res = -1 break if c >= len(both): res += alice[a] res += bob[b] a += 1 b += 1 elif a >= len(alice) or b >= len(bob): res += both[c] c += 1 elif alice[a] + bob[b] < both[c]: res += alice[a] res += bob[b] a += 1 b += 1 elif both[c] <= alice[a] + bob[b]: res += both[c] c += 1 print(res)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) data = [] a_num = 0 b_num = 0 a_array = [] b_array = [] ab_array = [] time_sum = 0 for i in range(n): time, a, b = map(int, input().split()) if a and b: a_num += 1 b_num += 1 ab_array.append(time) elif a: a_num += 1 a_array.append(time) elif b: b_num += 1 b_array.append(time) if a_num < k or b_num < k: print(-1) else: a_array.sort() b_array.sort() while a_array and b_array: ab_array.append(a_array.pop(0) + b_array.pop(0)) ab_array.sort() for i in range(k): time_sum += ab_array[i] print(time_sum)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def reading(numb, mass, c1, c2): if c1 < numb or c2 < numb: return -1 true_mass1 = [] true_mass2 = [] true_mass3 = [] for i in mass: if i[1] != 0 or i[2] != 0: if i[1] == 0: true_mass1.append(i) elif i[2] == 0: true_mass2.append(i) else: true_mass3.append(i) true_mass1.sort(key=lambda p: p[0]) true_mass2.sort(key=lambda p: p[0]) true_mass3.sort(key=lambda p: p[0]) i = 0 j = 0 k = 0 deltai = len(true_mass3) deltaj = len(true_mass2) deltak = len(true_mass1) result = 0 for d in range(numb): if deltai != i and deltaj != j and deltak != k: if true_mass3[i][0] <= true_mass2[j][0] + true_mass1[k][0]: result += true_mass3[i][0] i += 1 else: result += true_mass2[j][0] + true_mass1[k][0] k += 1 j += 1 elif deltai == i: result += true_mass2[j][0] + true_mass1[k][0] k += 1 j += 1 elif deltaj == j or deltak == k: result += true_mass3[i][0] i += 1 return result t = [int(i) for i in input().strip().split()] n = t[1] count1 = 0 count2 = 0 mass = [] for i in range(t[0]): string = [int(j) for j in input().strip().split()] mass.append(string) if string[1] == 1: count1 += 1 if string[2] == 1: count2 += 1 print(reading(n, mass, count1, count2))
FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) lst = [] for i in range(n): lst.append(list(map(int, input().split()))) alice = [] bob = [] same = [] for i in lst: if i[1] == 1 and i[2] == 0: alice.append(i[0]) elif i[2] == 1 and i[1] == 0: bob.append(i[0]) elif i[2] == 1 and i[1] == 1: same.append(i[0]) a = len(alice) b = len(bob) s = len(same) if a + s < k or b + s < k: print(-1) else: alice.sort() bob.sort() same.sort() l = 0 n = 0 ans = 0 count = 0 temp = min(a, b) while count < k and n < s and l < temp: if alice[l] + bob[l] < same[n] or n >= s: ans += alice[l] + bob[l] l += 1 count += 1 else: ans += same[n] count += 1 n += 1 if count != k and n == s: ans += sum(alice[l : l + k - count]) + sum(bob[l : l + k - count]) else: ans += sum(same[n : n + k - count]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) books = [] alice = set() bob = set() for i in range(n): books.append([int(i) for i in input().split()]) if books[-1][1] == 1: alice.add(i) if books[-1][2] == 1: bob.add(i) if len(alice) < k or len(bob) < k: print(-1) else: time, a, b = 0, 0, 0 temp = alice | bob diff = temp.difference(alice & bob) temp_alice = [] temp_bob = [] for i in diff: if i in alice: temp_alice += [books[i]] else: temp_bob += [books[i]] temp_alice.sort() temp_bob.sort() temp_mass = [] for i in range(min(len(temp_bob), len(temp_alice))): temp_mass.append([temp_alice[i][0] + temp_bob[i][0], 1, 1]) temp_mass = temp_mass + [books[i] for i in temp if i not in diff] temp_mass.sort(key=lambda x: (x[1] + x[2], -x[0]), reverse=True) for i in temp_mass: time += i[0] a += i[1] b += i[2] if a >= k and b >= k: break print(time)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR LIST VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys n, k = [int(e) for e in input().split(" ")] books = list() books_map = dict() for i in range(n): t, a, b = [int(e) for e in input().split(" ")] books.append((t, a, b, i)) books_map[i] = t, a, b books = sorted(books, key=lambda x: x[0] * (1 if x[1] == 1 else 10000000.0)) alice_pay_time = 0 alice_liked_books = list() for book in books: if len(alice_liked_books) >= k: break if book[1] == 1 and book[2] == 0: alice_pay_time += book[0] alice_liked_books.append(book[3]) books = sorted(books, key=lambda x: x[0] * (1 if x[2] == 1 else 10000000.0)) bob_pay_time = 0 bob_liked_books = list() for book in books: if len(bob_liked_books) >= k: break if book[2] == 1 and book[1] == 0: bob_pay_time += book[0] bob_liked_books.append(book[3]) books = sorted( books, key=lambda x: x[0] * (1 if x[1] == 1 and x[2] == 1 else 10000000.0) ) pay_time = alice_pay_time + bob_pay_time alice_liked_book_count = len(alice_liked_books) bob_liked_book_count = len(bob_liked_books) for book in books: if book[1] == 1 and book[2] == 1: if alice_liked_book_count < k or bob_liked_book_count < k: pay_time += book[0] alice_liked_book_count += 1 bob_liked_book_count += 1 if alice_liked_book_count > k: pay_time -= books_map[alice_liked_books[-1]][0] alice_liked_books.pop() alice_liked_book_count = k if bob_liked_book_count > k: pay_time -= books_map[bob_liked_books[-1]][0] bob_liked_books.pop() bob_liked_book_count = k elif len(alice_liked_books) == 0 or len(bob_liked_books) == 0: break elif ( book[0] < books_map[alice_liked_books[-1]][0] + books_map[bob_liked_books[-1]][0] ): pay_time -= books_map[alice_liked_books[-1]][0] pay_time -= books_map[bob_liked_books[-1]][0] pay_time += book[0] alice_liked_books.pop() bob_liked_books.pop() if alice_liked_book_count >= k and bob_liked_book_count >= k: print(pay_time) else: print(-1)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def answer(): if n3 + n1 < k: return -1 if n3 + n2 < k: return -1 ap = [0] for i in range(n1): ap.append(ap[-1] + a[i]) ap.append(0) bp = [0] for i in range(n2): bp.append(bp[-1] + b[i]) bp.append(0) start = max(max(0, k - n1), max(0, k - n2)) s = 0 for i in range(start): s += common[i] common.append(0) ans = 10000000000.0 for i in range(start, min(k, n3) + 1): ans = min(ans, s + ap[k - i] + bp[k - i]) s += common[i] return ans n, k = map(int, input().split()) a, b, common = [], [], [] for i in range(n): t, x, y = map(int, input().split()) if x and y: common.append(t) elif x == 1 and y == 0: a.append(t) elif x == 0 and y == 1: b.append(t) common.sort() a.sort() b.sort() n1, n2, n3 = len(a), len(b), len(common) print(answer())
FUNC_DEF IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
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: a.append(t) if x == 1 and y == 0: b.append(t) if x == 0 and y == 1: c.append(t) a.sort() b.sort() c.sort() prea, preb, prec = 0, 0, 0 cnt = min(len(b), len(c), k) for i in range(cnt): preb += b[i] prec += c[i] bestans = int(2000000000.0 + 5) if cnt == k: bestans = preb + prec for both in range(min(len(a), k)): prea += a[both] idx = k - both - 1 if idx < cnt: preb -= b[idx] prec -= c[idx] if idx - 1 < cnt and prea + preb + prec < bestans: bestans = prea + preb + prec print(bestans) if bestans <= int(2000000000.0) else print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) a = [] b = [] ab = [] c = 0 p = 0 e = 0 for q in range(n): x, y, z = map(int, input().split()) if y == 1 and z == 0: a.append(x) elif y == 0 and z == 1: b.append(x) elif y == 1 and z == 1: ab.append(x) a.sort() b.sort() ab.sort() if len(ab) + min(len(a), len(b)) < k: print("-1") else: for h in range(k): if p == min(len(a), len(b)): c = c + sum(ab[e : e + k - h]) break elif e == len(ab): c = c + sum(a[p : p + k - h]) + sum(b[p : p + k - h]) break c = c + min(a[p] + b[p], ab[e]) if a[p] + b[p] < ab[e]: p = p + 1 else: e = e + 1 print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import stdin n, k = map(int, input().split()) x = [] y = [] z = [] k1 = 0 k2 = 0 for i in range(n): t, a, b = map(int, stdin.readline().split()) if a == 1 and b == 1: x.append(t) k1 += 1 k2 += 1 elif a == 1: k1 += 1 y.append(t) elif b == 1: k2 += 1 z.append(t) if k1 < k or k2 < k: print(-1) else: x.sort() y.sort() z.sort() ans = 0 k1 = 0 k2 = 0 p1 = 0 p2 = 0 p3 = 0 lx = len(x) ly = len(y) lz = len(z) for i in range(k): if p1 >= lx: ans += y[p2] + z[p3] p2 += 1 p3 += 1 elif p2 >= ly or p3 >= lz: ans += x[p1] p1 += 1 elif x[p1] <= y[p2] + z[p3]: ans += x[p1] p1 += 1 else: ans += y[p2] + z[p3] p2 += 1 p3 += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = [int(i) for i in input().split()] a_likes = list() b_likes = list() both_like = list() a_likes.append(0) b_likes.append(0) both_like.append(0) for book in range(n): t, a, b = [int(i) for i in input().split()] if a == 1 and b == 1: both_like.append(t) elif a == 1: a_likes.append(t) elif b == 1: b_likes.append(t) a_likes.sort() b_likes.sort() both_like.sort() for i in range(1, len(a_likes)): a_likes[i] += a_likes[i - 1] for i in range(1, len(b_likes)): b_likes[i] += b_likes[i - 1] for i in range(1, len(both_like)): both_like[i] += both_like[i - 1] if len(both_like) + len(a_likes) < k or len(both_like) + len(b_likes) < k: print("-1") else: ans = 2 * pow(10, 9) + 1 rbo = 0 while rbo < min(len(both_like), k + 1): if k - rbo < len(a_likes) and k - rbo < len(b_likes): ans = min(ans, both_like[rbo] + a_likes[k - rbo] + b_likes[k - rbo]) rbo += 1 if ans < 2 * pow(10, 9) + 1: print(ans) else: print("-1")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) books = [list(map(int, input().split())) for i in range(n)] AB, A, B = [[10**5, 0, 0]], [[10**5, 0, 0]], [[10**5, 0, 0]] for t, a, b in books: if a == 1 and b == 1: AB.append([t, a, b]) elif a == 1 and b == 0: A.append([t, a, b]) elif a == 0 and b == 1: B.append([t, a, b]) AB = list(reversed(sorted(AB))) A = list(reversed(sorted(A))) B = list(reversed(sorted(B))) ans = [] for i in range(k): if len(AB) != 1 and AB[-1][0] < A[-1][0] + B[-1][0]: ans.append(AB.pop()[0]) elif len(A) != 1 and len(B) != 1: ans.append(A.pop()[0]) ans.append(B.pop()[0]) else: print(-1) break else: print(sum(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST LIST BIN_OP NUMBER NUMBER NUMBER NUMBER LIST LIST BIN_OP NUMBER NUMBER NUMBER NUMBER LIST LIST BIN_OP NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) a = [] b = [] c = [] at = 0 bt = 0 alo = 0 blo = 0 clo = 0 for i in range(n): t, al, bl = map(int, input().split()) if al == 1 and bl == 1: at += 1 bt += 1 clo += 1 c.append(t) elif al == 1: at += 1 alo += 1 a.append(t) elif bl == 1: bt += 1 blo += 1 b.append(t) if at < k or bt < k: print("-1") else: total = 0 time = 0 a.sort() b.sort() c.sort() j = 0 r = 0 while total < k: if j == alo or j == blo: time = time + c[r] r += 1 elif r == clo: time = time + a[j] + b[j] j += 1 elif a[j] + b[j] < c[r]: time = time + a[j] + b[j] j += 1 else: time = time + c[r] r += 1 total += 1 print(time)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) t = [] a = [] b = [] n1 = 0 n2 = 0 n3 = 0 for _ in range(n): x, y, z = map(int, input().split()) if y == 1 and z == 1: t.append(x) n1 += 1 elif y == 1: a.append(x) n2 += 1 elif z == 1: b.append(x) n3 += 1 t.sort() a.sort() b.sort() nc = min(n2, n3) c = [(a[i] + b[i]) for i in range(min(n2, n3))] ans = 0 i = 0 j = 0 while i < n1 and j < nc and k > 0: if t[i] <= c[j]: i += 1 ans += t[i - 1] else: j += 1 ans += c[j - 1] k -= 1 if k == 0: break while k > 0 and i < n1: i += 1 ans += t[i - 1] k -= 1 while k > 0 and j < nc: j += 1 ans += c[j - 1] k -= 1 if k > 0: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = [int(x) for x in input().split(" ")] time = [] a = [] b = [] for i in range(n): p, q, r = [int(x) for x in input().split(" ")] time.append(p) a.append(q) b.append(r) oa = [] ob = [] bo = [] no = [] for i in range(n): if a[i] and b[i]: bo.append(i) elif a[i] and b[i] == 0: oa.append(i) elif a[i] == 0 and b[i]: ob.append(i) else: no.append(i) if len(bo) + len(oa) < k or len(bo) + len(ob) < k: print(-1) else: def time_req(i): return time[i] oa.sort(key=time_req) ob.sort(key=time_req) bo.sort(key=time_req) oa.reverse() ob.reverse() bo.reverse() books = set() for i in range(k): if len(oa) == 0 or len(ob) == 0: books.add(bo.pop()) continue elif len(bo) == 0: books.add(oa.pop()) books.add(ob.pop()) continue if time[bo[-1]] < time[oa[-1]] + time[ob[-1]] or len(oa) == 0 or len(ob) == 0: books.add(bo.pop()) else: books.add(oa.pop()) books.add(ob.pop()) print(sum(list(map(time_req, list(books)))))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) arr = [[int(i) for i in input().split()] for _ in range(n)] a = sorted([arr[i][0] for i in range(n) if arr[i][1] == 1 and arr[i][2] == 0]) b = sorted([arr[i][0] for i in range(n) if arr[i][1] == 0 and arr[i][2] == 1]) ab = sorted([arr[i][0] for i in range(n) if arr[i][1] == 1 and arr[i][2] == 1]) ans = 0 l, r = 0, 0 for i in range(k): v1 = 10**9 if len(a) > l and len(b) > l: v1 = a[l] + b[l] v2 = 10**9 if len(ab) > r: v2 = ab[r] if v1 == v2 == 10**9: ans = -1 break if v1 < v2: l += 1 ans += v1 else: r += 1 ans += v2 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) group = [[], [], [], []] for i in range(n): t, a, b = map(int, input().split()) group[a * 2 + b].append(t) pref_sum = [[0], [0], [0], [0]] for g, ps in zip(group, pref_sum): g.sort() for i in g: ps.append(ps[-1] + i) ans = 10**18 for i in range(min(k, len(group[3])) + 1): rest = k - i if rest > len(group[2]) or rest > len(group[1]): continue ans = min(ans, pref_sum[3][i] + pref_sum[1][rest] + pref_sum[2][rest]) if ans < 10**18: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) both = [] a = [] b = [] for _ in range(n): t, ai, bi = map(int, input().split()) if ai == 1 and bi == 1: both.append(t) elif ai == 1: a.append(t) elif bi == 1: b.append(t) both.sort() a.sort() b.sort() for l in [both, a, b]: if len(l) < k: l += [10**10] * (k - len(l)) if len(a) > k: a = a[:k] if len(b) > k: b = b[:k] if len(both) > k: both = both[:k] out = 0 for i in range(k): out += min(a[i] + b[i], both[-i - 1]) if out >= 10**10: print(-1) else: print(out)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR LIST VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) a, b, tog = [], [], [] for i in range(n): t, x, y = map(int, input().split()) if x == 1 and y == 1: tog.append(t) elif x == 1 and y == 0: a.append(t) elif x == 0 and y == 1: b.append(t) ltog = len(tog) la = len(a) lb = len(b) if ltog + la < k or ltog + lb < k: print(-1) else: a.sort(reverse=True) b.sort(reverse=True) tog.sort(reverse=True) ca, cb = 0, 0 res = 0 while ca < k or cb < k: if tog: ptog = tog[-1] else: ptog = 0 if a: pa = a[-1] else: pa = 0 if b: pb = b[-1] else: pb = 0 if ca < k and cb < k: if pa and pb and ptog: if pa + pb < ptog: res += pa + pb a.pop() b.pop() else: res += ptog tog.pop() ca += 1 cb += 1 elif ptog: res += ptog tog.pop() ca += 1 cb += 1 elif pa and pb: res += pa + pb a.pop() b.pop() ca += 1 cb += 1 else: res = -1 break elif ca < k: if pa and ptog: if pa < ptog: res += pa a.pop() ca += 1 else: res += ptog tog.pop() ca += 1 cb += 1 elif ptog: res += ptog tog.pop() ca += 1 cb += 1 elif pa: res += pa a.pop() ca += 1 else: res = -1 break elif cb < k: if pb and ptog: if pb < ptog: res += pb b.pop() cb += 1 else: res += ptog tog.pop() ca += 1 cb += 1 elif ptog: res += ptog tog.pop() ca += 1 cb += 1 elif pb: res += pb b.pop() cb += 1 else: res = -1 break print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
k, n = [int(i) for i in input().split()] a = [] b = [] both = [] for _ in range(k): t, x, y = [int(i) for i in input().split()] if x == 1 and y == 1: both.append(t) elif x == 1: a.append(t) elif y == 1: b.append(t) a.sort() b.sort() both.sort() if len(a) + len(both) < n or len(b) + len(both) < n: print(-1) quit() bI = 0 aI = 0 bothI = 0 count = 0 t = 0 while count < n: count += 1 if len(a) != aI and len(b) != bI and len(both) != bothI: if a[aI] + b[bI] < both[bothI]: t += a[aI] + b[bI] aI += 1 bI += 1 else: t += both[bothI] bothI += 1 elif len(a) != aI and len(b) != bI: t += a[aI] + b[bI] aI += 1 bI += 1 else: t += both[bothI] bothI += 1 print(t)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
class Book: def __init__(self, time, for_alice, for_bob): self.time = time self.for_alice = bool(for_alice) self.for_bob = bool(for_bob) self.status = ("A" if self.for_alice else "") + ("B" if self.for_bob else "") def get(li, i): try: return li[i].time except IndexError: return 10**10 n, k = map(int, input().split()) books = [Book(*map(int, input().split())) for _ in range(n)] alice_rest, bob_rest = k, k for_alice_books = list(filter(lambda x: x.status == "A", books)) for_bob_books = list(filter(lambda x: x.status == "B", books)) for_both_books = list(filter(lambda x: x.status == "AB", books)) for_alice_books = sorted(for_alice_books, key=lambda x: x.time) for_bob_books = sorted(for_bob_books, key=lambda x: x.time) for_both_books = sorted(for_both_books, key=lambda x: x.time) for_alice_books_index, for_bob_books_index, for_both_books_index = 0, 0, 0 total = 0 while alice_rest > 0 or bob_rest > 0: alice_var = get(for_alice_books, for_alice_books_index) bob_var = get(for_bob_books, for_bob_books_index) both_var = get(for_both_books, for_both_books_index) if all(i == 10**10 for i in [alice_var, bob_var, both_var]): total = -1 break if alice_rest > 0 and bob_rest > 0: if alice_var + bob_var > both_var: for_both_books_index += 1 total += both_var else: for_alice_books_index += 1 for_bob_books_index += 1 total += alice_var + bob_var alice_rest -= 1 bob_rest -= 1 elif alice_rest == 0: if bob_var > both_var: for_both_books_index += 1 total += both_var else: for_bob_books_index += 1 total += bob_var bob_rest -= 1 else: if alice_var > both_var: for_both_books_index += 1 total += both_var else: for_alice_books_index += 1 total += alice_var alice_rest -= 1 print(total if total < 10**10 else -1)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING STRING VAR STRING STRING FUNC_DEF RETURN VAR VAR VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER VAR LIST VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) both, f, s = [], [], [] for i in range(n): t, x, y = map(int, input().split()) if x == 1 and y == 1: both.append(t) elif x == 1: f.append(t) elif y == 1: s.append(t) if min(len(f), len(s)) + len(both) < k: print(-1) else: both = sorted(both) f = sorted(f) s = sorted(s) g = [both, f, s] for l in g: for j, v in enumerate(l): if j > 0: l[j] = l[j - 1] + l[j] if len(f) > len(s): b = f f = s s = b mn = 5 * 10**9 if len(f) == 0: mn = both[k - 1] for i in range(len(f) + 1): if i > k: break elif i > 0: if i == k: mn = min(f[i - 1] + s[i - 1], mn) elif len(both) >= k - i: mn = min(f[i - 1] + s[i - 1] + both[k - i - 1], mn) elif i == 0: if len(both) >= k: mn = both[k - 1] print(mn)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
t, k = map(int, input().split()) A = [] B = [] C = [] for i in range(t): a, b, c = map(int, input().split()) if b == 1 and c == 0: A.append(a) elif b == 0 and c == 1: B.append(a) elif b == 1: C.append(a) if min(len(A), len(B)) + len(C) < k: print(-1) else: A.sort() B.sort() s = 0 for i in range(min(len(A), len(B))): C.append(A[i] + B[i]) C.sort() for i in range(k): s += C[i] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys def solve(): n, k = map(int, sys.stdin.readline().split()) list_book = [[], [], []] count = [0, 0, 0] for _ in range(n): t, a, b = map(int, sys.stdin.readline().split()) if a and b: list_book[0].append(t) count[0] += 1 elif a: list_book[1].append(t) count[1] += 1 elif b: list_book[2].append(t) count[2] += 1 if count[0] + min(count[1], count[2]) < k: print(-1) return list_book[0].sort(reverse=True) list_book[1].sort(reverse=True) list_book[2].sort(reverse=True) result = 0 book = k result = 0 for _ in range(k): next_time = 1000000000000000000000 token = 0 if list_book[1] and list_book[2]: next_time = list_book[1][-1] + list_book[2][-1] if list_book[0]: if next_time > list_book[0][-1]: next_time = list_book[0][-1] token = 1 if token: list_book[0].pop() else: list_book[1].pop() list_book[2].pop() result += next_time print(result) solve()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, K = list(map(int, input().split())) l = [list(map(int, input().split())) for _ in range(n)] al, bo, bt = [], [], [] for i in l: if i[1] == 1 and i[2] == 1: bt.append(i[0]) elif i[1] == 1 and i[2] == 0: al.append(i[0]) elif i[1] == 0 and i[2] == 1: bo.append(i[0]) al.sort() bt.sort() bo.sort() i, j, k = 0, 0, 0 ans, cnt = 0, 0 while i < len(al) and j < len(bo) and k < len(bt) and cnt < K: if al[i] + bo[j] < bt[k]: ans += al[i] + bo[j] i += 1 j += 1 else: ans += bt[k] k += 1 cnt += 1 while k < len(bt) and cnt < K: ans += bt[k] k += 1 cnt += 1 while cnt < K and i < len(al) and j < len(bo): ans += al[i] + bo[j] i += 1 j += 1 cnt += 1 if cnt < K: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) li = [[list(), list()], [list(), list()]] for i in range(n): a, b, c = map(int, input().split()) li[b][c].append(a) cnt = 0 itr1, itr2, itr3 = 0, 0, 0 li[0][1].sort() li[1][0].sort() li[1][1].sort() ttl = 0 run = True for i in range(k): sm = 0 if itr1 != len(li[0][1]) and itr2 != len(li[1][0]): sm = li[0][1][itr1] + li[1][0][itr2] if itr3 != len(li[1][1]): if sm < li[1][1][itr3]: itr1 += 1 itr2 += 1 ttl += sm else: ttl += li[1][1][itr3] itr3 += 1 else: itr1 += 1 itr2 += 1 ttl += sm elif itr3 != len(li[1][1]): ttl += li[1][1][itr3] itr3 += 1 else: run = False break if run: print(ttl) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR IF VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) alice = [] bob = [] both = [] counta = 0 countb = 0 for i in range(0, n): l1 = list(map(int, input().split())) if l1[1] == 1 and l1[2] == 1: counta += 1 countb += 1 both.append(l1) elif l1[1] == 1: counta += 1 alice.append(l1) elif l1[2] == 1: countb += 1 bob.append(l1) if counta < k or countb < k: print(-1) else: t = 0 alice.sort() bob.sort() both.sort() a = 0 b = 0 for i in range(1, k + 1): if b > min(len(alice), len(bob)) - 1: for j in range(a, a + k - i + 1): t = t + both[j][0] break if a > len(both) - 1: for j in range(b, b + k - i + 1): t = t + alice[j][0] + bob[j][0] break if both[a][0] <= alice[b][0] + bob[b][0]: t = t + both[a][0] a = a + 1 else: t = t + alice[b][0] + bob[b][0] b = b + 1 print(t)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import maxsize n, k = map(int, input().split()) l = [[0], [0], [0], [0]] x = [] ans = maxsize for i in range(n): x.append(list(map(int, input().split()))) x.sort() for p in x: temp = l[p[1] * 2 + p[2]][-1] l[p[1] * 2 + p[2]].append(temp + p[0]) for i in range(min(k + 1, len(l[3]))): if k - i < len(l[1]) and k - i < len(l[2]): ans = min(ans, l[3][i] + l[1][k - i] + l[2][k - i]) if ans == maxsize: print(-1) else: print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
[N, K] = list(map(int, input().split())) L = [[[] for i in range(2)] for j in range(2)] for i in range(N): [t, a, b] = list(map(int, input().split())) L[a][b].append(t) INF = int(1000000000000.0) for i in range(2): for j in range(2): L[i][j].sort() for k in range(K - len(L[i][j])): L[i][j].append(INF) ans = INF now = 0 for i in range(K): now += L[1][1][i] ans = min(ans, now) for i in range(K): now -= L[1][1][K - i - 1] now += L[1][0][i] now += L[0][1][i] ans = min(ans, now) if ans >= INF: ans = -1 print(ans)
ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 N, K = MAP() A = [INF] B = [INF] AB = [INF] for i in range(N): t, a, b = MAP() if a == b == 1: AB.append(t) elif a == 1: A.append(t) elif b == 1: B.append(t) A.sort(reverse=1) B.sort(reverse=1) AB.sort(reverse=1) ans = 0 for i in range(K): if AB[-1] <= A[-1] + B[-1]: ans += AB.pop() else: ans += A.pop() + B.pop() if ans >= INF: print(-1) exit() print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) t10 = [] t01 = [] t11 = [] for x in range(n): t, a, b = map(int, input().split()) if a == 0 and b == 1: t01.append(t) elif a == 1 and b == 0: t10.append(t) elif a == 1 and b == 1: t11.append(t) t10.sort() t01.sort() t11.sort() for x in range(1, len(t01)): t01[x] += t01[x - 1] for x in range(1, len(t10)): t10[x] += t10[x - 1] for x in range(1, len(t11)): t11[x] += t11[x - 1] t10 = [0] + t10 t01 = [0] + t01 t11 = [0] + t11 ans = [] for x in range(len(t11)): if k - x > len(t10) - 1 or k - x > len(t01) - 1: continue t = 0 t += t11[x] t += t01[k - x] t += t10[k - x] ans.append(t) if x == k: break if len(ans) == 0: print(-1) else: print(min(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) common, alice, bob = [0], [0], [0] for i in range(n): t, a, b = map(int, input().split()) if a + b == 2: common.append(t) elif a == 1: alice.append(t) elif b == 1: bob.append(t) common.sort() alice.sort() bob.sort() for i in range(1, len(common)): common[i] += common[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] ans = 10**20 for i in range(len(common)): t = common[i] r = k - i if r >= 0 and r < len(alice) and r < len(bob): t += alice[r] + bob[r] ans = min(ans, t) if ans < 10**20: print(ans) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import stdin input = stdin.readline n, k = map(int, input().split()) al = [] bl = [] abl = [] for _ in range(n): t, a, b = map(int, input().split()) if a and b: abl.append(t) elif a: al.append(t) elif b: bl.append(t) al.sort() bl.sort() abl.sort() sl = list(map(lambda o: o[0] + o[1], zip(al, bl))) abl.reverse() sl.reverse() c = 0 while abl and sl and k: if abl[-1] < sl[-1]: c += abl.pop() else: c += sl.pop() k -= 1 if k: if len(abl) + len(sl) < k: print(-1) else: while k: if abl: c += abl.pop() else: c += sl.pop() k -= 1 print(c) else: print(c)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
line = input().split() n, k = int(line[0]), int(line[1]) a, b, c = [], [], [] for i in range(n): line = input().split() t, ai, bi = int(line[0]), int(line[1]), int(line[2]) if ai == 1 and bi == 1: c.append(t) elif ai == 1: a.append(t) elif bi == 1: b.append(t) a.sort() b.sort() c.sort() i, j, cnt = 0, 0, 0 ans = 0 while i < min(len(a), len(b)) and j < len(c) and cnt < k: if a[i] + b[i] < c[j]: ans += a[i] + b[i] i += 1 else: ans += c[j] j += 1 cnt += 1 while i < min(len(a), len(b)) and cnt < k: ans += a[i] + b[i] cnt += 1 i += 1 while j < len(c) and cnt < k: ans += c[j] cnt += 1 j += 1 if cnt < k: print(-1) else: print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
seg = [0] * 200000 def offset(x): return x + 100000 def upd(node, L, R, pos, val): if L + 1 == R: seg[node] += val seg[offset(node)] = seg[node] * L return M = (L + R) // 2 if pos < M: upd(node << 1, L, M, pos, val) else: upd(node << 1 | 1, M, R, pos, val) seg[node] = seg[node << 1] + seg[node << 1 | 1] seg[offset(node)] = seg[offset(node << 1)] + seg[offset(node << 1 | 1)] def query(node, L, R, k): if k == 0: return [0, 0] if seg[node] < k: return [seg[offset(node)], seg[node]] if L + 1 == R: return [L * k, k] M = (L + R) // 2 left = query(node << 1, L, M, k) right = query(node << 1 | 1, M, R, k - left[1]) left[0] += right[0] left[1] += right[1] return left n, m, k = map(int, input().split()) A, B, both, neither = [], [], [], [] for i in range(n): t, a, b = map(int, input().split()) if a == 0 and b == 0: neither.append([t, i + 1]) if a == 1 and b == 0: A.append([t, i + 1]) if a == 0 and b == 1: B.append([t, i + 1]) if a == 1 and b == 1: both.append([t, i + 1]) upd(1, 0, 10001, t, 1) A.sort() B.sort() both.sort() p1 = min(k, len(both)) p2 = k - p1 if 2 * k - p1 > m or p2 > min(len(A), len(B)): print(-1) exit(0) sum, ans, ch = 0, 2**31, p1 for i in range(p1): sum += both[i][0] upd(1, 0, 10001, both[i][0], -1) for i in range(p2): sum += A[i][0] + B[i][0] upd(1, 0, 10001, A[i][0], -1) upd(1, 0, 10001, B[i][0], -1) ans = sum + query(1, 0, 10001, m - 2 * k + p1)[0] while p1 > 0: if p2 == min(len(A), len(B)): break upd(1, 0, 10001, A[p2][0], -1) sum += A[p2][0] upd(1, 0, 10001, B[p2][0], -1) sum += B[p2][0] upd(1, 0, 10001, both[p1 - 1][0], 1) sum -= both[p1 - 1][0] p2 += 1 p1 -= 1 if m - 2 * k + p1 < 0: break Q = query(1, 0, 10001, m - 2 * k + p1) if ans > sum + Q[0]: ans = sum + Q[0] ch = p1 print(ans) ind = ( [both[i][1] for i in range(ch)] + [A[i][1] for i in range(k - ch)] + [B[i][1] for i in range(k - ch)] ) st = ( neither + [both[i] for i in range(ch, len(both))] + [A[i] for i in range(k - ch, len(A))] + [B[i] for i in range(k - ch, len(B))] ) st.sort() ind += [st[i][1] for i in range(m - 2 * k + ch)] print(" ".join([str(x) for x in ind]))
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER NUMBER IF VAR VAR VAR RETURN LIST VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR RETURN LIST BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER WHILE VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = list(map(int, input().split())) time, A, B, AB, lenA, lenB, lenAB = [], [], [], [], 0, 0, 0 for idx in range(n): t, a, b = list(map(int, input().split())) time.append(t) if a and not b: A.append(t) lenA += 1 elif b and not a: B.append(t) lenB += 1 elif a and b: AB.append(t) lenAB += 1 if lenA + lenAB < k or lenB + lenAB < k: print(-1) else: A.sort() B.sort() AB.sort() s, x, y = 0, 0, 0 for i in range(k): if x < lenA and x < lenB and y < lenAB: if A[x] + B[x] < AB[y]: s += A[x] + B[x] x += 1 else: s += AB[y] y += 1 elif x >= lenA or x >= lenB: s += AB[y] y += 1 elif y >= lenAB and x < lenA and x < lenB: s += A[x] + B[x] x += 1 print(s)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR LIST LIST LIST LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = [int(i) for i in input().split()] doub = [] a = [] b = [] for i in range(n): ti, ai, bi = [int(i) for i in input().split()] if ai and bi: doub.append(ti) elif ai: a.append(ti) elif bi: b.append(ti) doub.sort() a.sort() b.sort() time = 0 i = 0 j = 0 while i + j < k: if i < len(doub): db = doub[i] else: db = 100000 if j < len(a) and j < len(b): sing = a[j] + b[j] else: sing = 100000 if db <= sing: time += db i += 1 else: time += sing j += 1 if len(doub) + min(len(a), len(b)) >= k: print(time) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Bessie is out grazing on the farm, which consists of $n$ fields connected by $m$ bidirectional roads. She is currently at field $1$, and will return to her home at field $n$ at the end of the day. The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has $k$ special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them. After the road is added, Bessie will return home on the shortest path from field $1$ to field $n$. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him! -----Input----- The first line contains integers $n$, $m$, and $k$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le 2 \cdot 10^5$, $2 \le k \le n$) β€” the number of fields on the farm, the number of roads, and the number of special fields. The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) β€” the special fields. All $a_i$ are distinct. The $i$-th of the following $m$ lines contains integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$, $x_i \ne y_i$), representing a bidirectional road between fields $x_i$ and $y_i$. It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them. -----Output----- Output one integer, the maximum possible length of the shortest path from field $1$ to $n$ after Farmer John installs one road optimally. -----Examples----- Input 5 5 3 1 3 5 1 2 2 3 3 4 3 5 2 4 Output 3 Input 5 4 2 2 4 1 2 2 3 3 4 4 5 Output 3 -----Note----- The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields $3$ and $5$, and the resulting shortest path from $1$ to $5$ is length $3$. The graph for the second example is shown below. Farmer John must add a road between fields $2$ and $4$, and the resulting shortest path from $1$ to $5$ is length $3$.
def main(): n, m, k = [int(s) for s in input().strip().split()] A = [int(s) for s in input().strip().split()] adj = [[] for _ in range(n + 1)] for _ in range(m): u, v = [int(s) for s in input().strip().split()] adj[u].append(v) adj[v].append(u) def bfs(u): result = [float("inf")] * (n + 1) result[u] = 0 step = 0 q = [u] while q: step += 1 newq = [] for u in q: for v in adj[u]: if result[v] == float("inf"): result[v] = step newq.append(v) q = newq return result X = bfs(1) Y = bfs(n) A.sort(key=lambda a: X[a] - Y[a]) result, maxx = float("-inf"), float("-inf") for a in A: result = max(result, maxx + Y[a]) maxx = max(maxx, X[a]) result = min(X[n], result + 1) print(result) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Bessie is out grazing on the farm, which consists of $n$ fields connected by $m$ bidirectional roads. She is currently at field $1$, and will return to her home at field $n$ at the end of the day. The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has $k$ special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them. After the road is added, Bessie will return home on the shortest path from field $1$ to field $n$. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him! -----Input----- The first line contains integers $n$, $m$, and $k$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le 2 \cdot 10^5$, $2 \le k \le n$) β€” the number of fields on the farm, the number of roads, and the number of special fields. The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) β€” the special fields. All $a_i$ are distinct. The $i$-th of the following $m$ lines contains integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$, $x_i \ne y_i$), representing a bidirectional road between fields $x_i$ and $y_i$. It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them. -----Output----- Output one integer, the maximum possible length of the shortest path from field $1$ to $n$ after Farmer John installs one road optimally. -----Examples----- Input 5 5 3 1 3 5 1 2 2 3 3 4 3 5 2 4 Output 3 Input 5 4 2 2 4 1 2 2 3 3 4 4 5 Output 3 -----Note----- The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields $3$ and $5$, and the resulting shortest path from $1$ to $5$ is length $3$. The graph for the second example is shown below. Farmer John must add a road between fields $2$ and $4$, and the resulting shortest path from $1$ to $5$ is length $3$.
import sys input = sys.stdin.readline n, m, K = map(int, input().split()) sp = list(map(lambda x: int(x) - 1, input().split())) peer = [[] for _ in range(n)] for _ in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 peer[a].append(b) peer[b].append(a) a0 = [(10**6) for _ in range(n)] a0[0] = 0 a1 = [(10**6) for _ in range(n)] a1[-1] = 0 now = [0] while now: last = now now = [] for x in last: for y in peer[x]: if a0[y] > a0[x] + 1: a0[y] = a0[x] + 1 now.append(y) now = [n - 1] while now: last = now now = [] for x in last: for y in peer[x]: if a1[y] > a1[x] + 1: a1[y] = a1[x] + 1 now.append(y) xyxy = [] for w in sp: xyxy.append([a0[w] - a1[w], a0[w], a1[w]]) xyxy.sort() xx = [] maxi = 0 for j in range(K): xx.append(max(maxi, xyxy[j][1])) can = [] for j in range(1, K): can.append(xx[j - 1] + xyxy[j][2] + 1) print(min(a0[-1], max(can)))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR
Bessie is out grazing on the farm, which consists of $n$ fields connected by $m$ bidirectional roads. She is currently at field $1$, and will return to her home at field $n$ at the end of the day. The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has $k$ special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them. After the road is added, Bessie will return home on the shortest path from field $1$ to field $n$. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him! -----Input----- The first line contains integers $n$, $m$, and $k$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le 2 \cdot 10^5$, $2 \le k \le n$) β€” the number of fields on the farm, the number of roads, and the number of special fields. The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) β€” the special fields. All $a_i$ are distinct. The $i$-th of the following $m$ lines contains integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$, $x_i \ne y_i$), representing a bidirectional road between fields $x_i$ and $y_i$. It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them. -----Output----- Output one integer, the maximum possible length of the shortest path from field $1$ to $n$ after Farmer John installs one road optimally. -----Examples----- Input 5 5 3 1 3 5 1 2 2 3 3 4 3 5 2 4 Output 3 Input 5 4 2 2 4 1 2 2 3 3 4 4 5 Output 3 -----Note----- The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields $3$ and $5$, and the resulting shortest path from $1$ to $5$ is length $3$. The graph for the second example is shown below. Farmer John must add a road between fields $2$ and $4$, and the resulting shortest path from $1$ to $5$ is length $3$.
import sys input = sys.stdin.readline N, M, K = map(int, input().split()) A = list(map(lambda x: int(x) - 1, input().split())) graph = [[] for _ in range(N)] for _ in range(M): a, b = map(int, input().split()) graph[a - 1].append(b - 1) graph[b - 1].append(a - 1) def bfs(s): D = [-1] * N D[s] = 0 q = [s] while q: qq = [] for p in q: for np in graph[p]: if D[np] == -1: D[np] = D[p] + 1 qq.append(np) q = qq return D D1 = bfs(0) D2 = bfs(N - 1) distance = D1[N - 1] P = [] for a in A: d1 = D1[a] d2 = D2[a] P.append((d1, d2)) P.sort() ans = 0 for i in range(K - 1): b1, b2 = P[i] c1, c2 = P[i + 1] ans = max(ans, min(c1 + b2 + 1, c2 + b1 + 1)) ans = min(ans, distance) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Bessie is out grazing on the farm, which consists of $n$ fields connected by $m$ bidirectional roads. She is currently at field $1$, and will return to her home at field $n$ at the end of the day. The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has $k$ special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them. After the road is added, Bessie will return home on the shortest path from field $1$ to field $n$. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him! -----Input----- The first line contains integers $n$, $m$, and $k$ ($2 \le n \le 2 \cdot 10^5$, $n-1 \le m \le 2 \cdot 10^5$, $2 \le k \le n$) β€” the number of fields on the farm, the number of roads, and the number of special fields. The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) β€” the special fields. All $a_i$ are distinct. The $i$-th of the following $m$ lines contains integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$, $x_i \ne y_i$), representing a bidirectional road between fields $x_i$ and $y_i$. It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them. -----Output----- Output one integer, the maximum possible length of the shortest path from field $1$ to $n$ after Farmer John installs one road optimally. -----Examples----- Input 5 5 3 1 3 5 1 2 2 3 3 4 3 5 2 4 Output 3 Input 5 4 2 2 4 1 2 2 3 3 4 4 5 Output 3 -----Note----- The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields $3$ and $5$, and the resulting shortest path from $1$ to $5$ is length $3$. The graph for the second example is shown below. Farmer John must add a road between fields $2$ and $4$, and the resulting shortest path from $1$ to $5$ is length $3$.
def bfs(adj, root): q = [(root, 0)] dist = [0] * len(adj) vis = {root} i = 0 while i < len(q): u, d = q[i] i += 1 dist[u] = d for v in adj[u]: if v not in vis: vis.add(v) q.append((v, d + 1)) return dist def sol(n, sfs, edges): adj = [[] for _ in range(n + 1)] for u, v in edges: adj[u].append(v) adj[v].append(u) dista = bfs(adj, 1) distb = bfs(adj, n) orig = distb[1] ans = 0 sfs.sort(key=lambda sf: dista[sf] - distb[sf]) maxdista = dista[sfs[0]] for fs in sfs[1:]: ans = max(ans, maxdista + distb[fs]) maxdista = max(maxdista, dista[fs]) return min(orig, ans + 1) n, m, _ = map(int, input().split()) sfs = list(map(int, input().split())) edges = [tuple(map(int, input().split())) for _ in range(m)] print(sol(n, sfs, edges))
FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Baby Ehab has a piece of Cut and Stick with an array a of length n written on it. He plans to grab a pair of scissors and do the following to it: * pick a range (l, r) and cut out every element a_l, a_{l + 1}, ..., a_r in this range; * stick some of the elements together in the same order they were in the array; * end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece. More formally, he partitions the sequence a_l, a_{l + 1}, ..., a_r into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length x, then no value occurs strictly more than ⌈ x/2 βŒ‰ times in it. He didn't pick a range yet, so he's wondering: for q ranges (l, r), what is the minimum number of pieces he needs to partition the elements a_l, a_{l + 1}, ..., a_r into so that the partitioning is beautiful. A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero) elements. Note that it does not have to be contiguous. Input The first line contains two integers n and q (1 ≀ n,q ≀ 3 β‹… 10^5) β€” the length of the array a and the number of queries. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≀ a_i ≀ n) β€” the elements of the array a. Each of the next q lines contains two integers l and r (1 ≀ l ≀ r ≀ n) β€” the range of this query. Output For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists. Example Input 6 2 1 3 2 3 3 2 1 6 2 5 Output 1 2 Note In the first query, you can just put the whole array in one subsequence, since its length is 6, and no value occurs more than 3 times in it. In the second query, the elements of the query range are [3,2,3,3]. You can't put them all in one subsequence, since its length is 4, and 3 occurs more than 2 times. However, you can partition it into two subsequences: [3] and [2,3,3].
import sys input = sys.stdin.readline n, q = map(int, input().split()) counts = [0] * (n + 1) c_amt = [0] * (n + 3) c_amt[0] = n + 1 max_count = 0 moves = 0 def add(x): global max_count, moves moves += 1 v = l[x] c_amt[counts[v]] -= 1 counts[v] += 1 c_amt[counts[v]] += 1 max_count = max(counts[v], max_count) def remove(x): global max_count, moves moves += 1 v = l[x] c_amt[counts[v]] -= 1 if max_count == counts[v] and c_amt[counts[v]] == 0: max_count -= 1 counts[v] -= 1 c_amt[counts[v]] += 1 l = list(map(int, input().split())) left = 0 right = -1 out = [0] * q queries = [] for i in range(q): ll, rr = map(int, input().split()) queries.append((ll - 1, rr - 1, i)) k = 547 queries.sort(key=lambda x: 2 * n * (x[0] // k) + x[1] * (-1) ** (x[0] // k)) for ll, rr, i in queries: while right < rr: add(right + 1) right += 1 while right > rr: remove(right) right -= 1 while left > ll: add(left - 1) left -= 1 while left < ll: remove(left) left += 1 sz = rr - ll + 1 top = max_count other = sz - top out[i] = max(1, top - other) print("\n".join(map(str, out)))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR FOR VAR VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle. However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. You need to return the least number of intervals the CPU will take to finish all the given tasks. Example 1: Input: tasks = ["A","A","A","B","B","B"], n = 2 Output: 8 Explanation: A -> B -> idle -> A -> B -> idle -> A -> B. Note: The number of tasks is in the range [1, 10000]. The integer n is in the range [0, 100].
class Solution: def leastInterval(self, tasks, n): l, dic = len(tasks), {} for c in tasks: if c in dic: dic[c] += 1 else: dic[c] = 1 m, a = max(dic.values()), 0 for c in dic: if dic[c] == m: a += 1 return max(l, (m - 1) * (n + 1) + a)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle. However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. You need to return the least number of intervals the CPU will take to finish all the given tasks. Example 1: Input: tasks = ["A","A","A","B","B","B"], n = 2 Output: 8 Explanation: A -> B -> idle -> A -> B -> idle -> A -> B. Note: The number of tasks is in the range [1, 10000]. The integer n is in the range [0, 100].
class Solution: def leastInterval(self, tasks, n): if not tasks: return 0 counts = {} for i in tasks: if i in counts: counts[i] += 1 else: counts[i] = 1 M = max(counts.values()) Mct = sum([(1) for i in counts if counts[i] == M]) return max(len(tasks), (M - 1) * (n + 1) + Mct)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
h = int(input()) programas = [] tvs = [-1, -1] for i in range(h): programas.append(list(map(int, input().split()))) programas.sort() teste = True for i in programas: horario = min(tvs) if i[0] <= horario: print("NO") teste = False break else: tvs[tvs.index(horario)] = i[1] if teste: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) a = [] for i in range(n): a.append(list(map(int, input().split()))) a.sort() b = a[0][1] c = -1 e = 0 for i in range(1, n): if a[i][0] > b: b = a[i][1] elif a[i][0] > c: c = a[i][1] else: e = 1 break if e == 1: print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
from sys import stdin input = stdin.readline q = [] counter = 0 n = int(input()) for i in range(n): s, e = map(int, input().split()) q.append([s, True]) q.append([e + 1, False]) q.sort() for i in range(len(q)): if q[i][1]: counter += 1 else: counter -= 1 if counter >= 3: print("NO") exit() print("YES")
ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) vec = [] for _ in range(n): a, b = [int(x) for x in input().split()] vec.append([a, b]) vec.sort() f1 = f2 = 0 flag1 = flag2 = True flag = "YES" for item in vec: if f1 <= f2: if flag1: f1 = item[1] flag1 = False elif item[0] > f1: f1 = item[1] else: flag = "NO" break elif flag2: f2 = item[1] flag2 = False elif item[0] > f2: f2 = item[1] else: flag = "NO" break print(flag)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) a = [list(map(int, input().split())) for i in range(n)] a.sort() p = -1 q = -1 f = 0 for l, r in a: if l > p: p = r elif l > q: q = r else: f = 1 break if f: print("NO") else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
shows = int(input()) tv1 = [] tv2 = [] horarios = [] for i in range(shows): horario = [*map(int, input().split())] horarios.append(horario) horarios.sort(key=lambda x: x[0]) possible = "YES" for horario in horarios: if len(tv1) == 0: tv1.append(horario) elif len(tv2) == 0: tv2.append(horario) elif tv1[-1][1] < horario[0]: tv1.append(horario) elif tv2[-1][1] < horario[0]: tv2.append(horario) else: possible = "NO" break print(possible)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) shows = [] MAX_TVS = 2 for i in range(n): show_info = [int(x) for x in input().split()] shows.append(show_info) shows.sort(key=lambda x: x[0]) tvs = [] i = 0 while len(tvs) <= MAX_TVS and i < n: if len(tvs) == MAX_TVS: if shows[i][0] > tvs[0][1]: tvs.pop(0) elif shows[i][0] > tvs[1][1]: tvs.pop(1) tvs.append(shows[i]) i += 1 result = "YES" if len(tvs) > MAX_TVS: result = "NO" print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) x = [] for i in range(n): l, r = map(int, input().split()) x.append((l, 1)) x.append((r, -1)) x.sort(key=lambda x: (x[0], -x[1])) s = 0 for i in range(len(x)): s += x[i][1] if s == 3: print("NO") exit() print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) v = [] for i in range(n): s = input() s = [int(x) for x in s.split()] v.append(s) v.sort() w = [] last = -1 for i in range(n): if v[i][0] <= last: w.append(v[i]) else: last = v[i][1] last = -1 f = 1 for i in range(len(w)): if w[i][0] <= last: f = 0 break else: last = w[i][1] if f: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) shows = [] for i in range(n): l, r = map(int, input().split()) shows.append((l, r)) shows.sort() a_endtime, b_endtime = -1, -1 for show in shows: if show[0] <= a_endtime: print("NO") break else: a_endtime = show[1] if a_endtime > b_endtime: a_endtime, b_endtime = b_endtime, a_endtime else: print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
def main(): n = int(input()) lst = list() for i in range(n): line = input() l, r = map(int, line.split()) lst.append((l, r)) lst.sort() tv1 = [(-1, -1)] tv2 = [(-1, -1)] for i in range(n): if lst[i][0] > tv1[-1][1]: tv1.append(lst[i]) elif lst[i][0] > tv2[-1][1]: tv2.append(lst[i]) tv1.remove(tv1[0]) tv2.remove(tv2[0]) tv = tv1 + tv2 tv.sort() if lst != tv: return "NO" else: return "YES" rs = main() print(rs)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF VAR VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
n = int(input()) l = [] for _ in range(n): l.append(tuple(map(int, input().split()))) l.sort() t1 = -1 t2 = -1 for i in l: if i[0] > t1: t1 = i[1] elif i[0] > t2: t2 = i[1] else: print("NO") exit() print("YES")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
l = [] t = int(input()) for _ in range(t): n, m = map(int, input().split()) l.append((n, m)) l.sort() le, ri = -1, -1 for i in range(t): if l[i][0] > le: le = l[i][1] elif l[i][0] > ri: ri = l[i][1] else: print("NO") exit() print("YES")
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
import sys n = int(input()) a = [] tv1 = -1 tv2 = -1 for _ in range(n): a += [tuple(map(int, sys.stdin.readline().split()))] a.sort() for i in range(n): s, e = a[i] if s > tv1: tv1 = e elif s > tv2: tv2 = e else: break else: print("YES") exit() print("NO")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Polycarp is a great fan of television. He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment l_{i} and ends at moment r_{i}. Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV. Polycarp wants to check out all n shows. Are two TVs enough to do so? -----Input----- The first line contains one integer n (1 ≀ n ≀ 2Β·10^5) β€” the number of shows. Each of the next n lines contains two integers l_{i} and r_{i} (0 ≀ l_{i} < r_{i} ≀ 10^9) β€” starting and ending time of i-th show. -----Output----- If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). -----Examples----- Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO
def main(): n = int(input()) events = [] for _ in range(n): l, r = (int(x) for x in input().split()) events.append((l, 1)) events.append((r + 1, -1)) cur = 0 for _, event in sorted(events): cur += event if cur > 2: print("NO") break else: print("YES") main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR