description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
I = lambda: map(int, input().split()) def C(a, b): return [a // b + 1, a // b][a % b == 0] def f(d, k): return sum( (d[i][0] > k) * min(C(d[i][0] - k, d[i][1]), d[i][2]) for i in range(len(d)) ) n, m = I() a = list(I()) b = list(I()) d = [] l = 0 for i in range(n): d.append([a[i] * b[i], b[i], a[i]]) l = max(l, d[i][0]) x = 0 while x != l: k = (x + l) // 2 if f(d, k) > m: x = k + 1 else: l = k print(l)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN LIST BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def poss(k, inp, h): b = 0 for i in range(0, len(inp)): el = inp[i] if el[1] == 0: continue elif k >= 0: v1 = el[0] - k v2 = el[1] chkval = int((v1 + v2 - 1) // v2) if chkval > 0: b += chkval if b <= h: return True else: return False def solve(inp, h, t): low = 0 high = t ans = t while low <= high: mid = (low + high) // 2 if poss(mid, inp, h): ans = mid high = mid - 1 else: low = mid + 1 return ans n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [] max_c = -1 s = 0 for i in range(0, n): tc = a[i] * b[i] s += a[i] c.append([tc, b[i]]) if tc > max_c: max_c = tc if m >= s: print(0) else: minc = solve(c, m, max_c) print(minc)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def bs(l, h): while l < h: m = (l + h) // 2 if gf(m): h = m else: l = m + 1 return l def gf(x): d = c for i in range(n): d -= min(a[i], x // b[i]) return d <= 0 n, m = map(int, input().split()) a = tuple(map(int, input().split())) b = tuple(map(int, input().split())) c = sum(a) c -= m ind = 0 h = 0 for i in range(n): h = max(h, a[i] * b[i]) print(bs(0, h))
FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def check(A, B, maxCi, n, m): count = 0 for day in range(n): wants = A[day] f = maxCi // B[day] if f > 0: atleast = max(0, wants - f) count += atleast else: count += wants return count <= m def solve(A, B, m, n): l, h = 0, 10**18 while l < h: ci = (l + h) // 2 if check(A, B, ci, n, m): h = ci else: l = ci + 1 return h n, m = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) ans = solve(A, B, m, n) print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def binary(l, r, baloons, B, P): if l <= r: mid = (l + r) // 2 val = 0 for i in range(len(P)): if P[i] > mid: if (P[i] - mid) % B[i] == 0: val = val + (P[i] - mid) // B[i] else: val = val + (P[i] - mid) // B[i] + 1 if val > baloons: l = binary(mid + 1, r, baloons, B, P) else: l = binary(l, mid - 1, baloons, B, P) return l n = [int(i) for i in input().split()] A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] C = A D = B P = [] for i in range(n[0]): P.append(A[i] * B[i]) c = n[1] // 2 k = c // 2 baloons = n[1] print(binary(0, max(P), baloons, B, P))
FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
n, m = map(int, input().split()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) lo = 0 hi = 10000000000000000000 while lo < hi: mid = (lo + hi) // 2 now = 0 for i in range(n): curr = max(0, l1[i] - mid // l2[i]) now += curr if now <= m: hi = mid else: lo = mid + 1 print(lo)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def b_search(N, M, a, b): low = 1 high = max(a) * max(b) while low < high: mid = low + (high - low) // 2 bn = 0 for x in range(N): bn += max(0, a[x] - mid // b[x]) if bn <= M: high = mid else: low = mid + 1 return low [N, M] = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] print(b_search(N, M, a, b))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def satisfy(a, b, m, value): for i in range(n): candies = a[i] * b[i] if candies > value: if a[i] < value // b[i]: continue m -= a[i] - value // b[i] if m < 0: return False return True def bsearch(a, b, m, value): lo = 0 hi = value while lo <= hi: mid = lo + (hi - lo) // 2 if satisfy(a, b, m, mid): hi = mid - 1 else: lo = mid + 1 return hi + 1 n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) maximum = 0 for i in range(n): maximum = max(a[i] * b[i], maximum) print(bsearch(a, b, m, maximum))
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def check_least(n, m, a, b, c, least): required_m = 0 for index, ci in enumerate(c): ai = a[index] bi = b[index] if ci > least: diff = ci - least required_m += diff // bi if diff % bi != 0: required_m += 1 if required_m > m: return False return True def binary_search(n, m, a, b, c, min_range, max_range): mid = (min_range + max_range) // 2 possible = check_least(n, m, a, b, c, mid) if possible and min_range == max_range: return mid if possible: min_range, max_range = min_range, mid else: min_range, max_range = mid + 1, max_range return binary_search(n, m, a, b, c, min_range, max_range) def solve(n, m, a, b, c): min_range = 0 max_range = max(c) print(binary_search(n, m, a, b, c, min_range, max_range)) n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [(ai * bi) for ai, bi in zip(a, b)] solve(n, m, a, b, c)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def isvalid(a, b, m, mid): t = 0 for i in range(len(a)): g = a[i] - mid // b[i] t += max(0, g) if t > m: return False return True n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) s = 0 e = 10**18 ans = -1 while s <= e: mid = (s + e) // 2 if isvalid(a, b, m, mid): ans = mid e = mid - 1 else: s = mid + 1 print(int(ans))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR RETURN NUMBER RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
n, m = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = [] for i in range(0, len(A)): C.append(A[i] * B[i]) D = [] for i, x in sorted(zip(C, B)): D.append(x) C.sort() l = 0 h = max(C) while l < h: b = 0 mid = (h + l) // 2 for i in range(len(C)): if C[i] <= mid: continue else: x = (C[i] - mid) // D[i] if C[i] - D[i] * x <= mid: b += x else: b += x + 1 if b <= m: h = mid else: l = mid + 1 print(l)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def main(): N, BALLOONS = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) UPPER_BOUND = max([(x * y) for x, y in zip(A, B)]) + 1 def predicate(MAX_TOFFEES_PER_DAY): required = 0 for ballons_req, toffees_per_ballon in zip(A, B): LIM = min(ballons_req * toffees_per_ballon, MAX_TOFFEES_PER_DAY) in_exchange_of = LIM // toffees_per_ballon required += ballons_req - in_exchange_of return required <= BALLOONS def binary_search(low=0, high=UPPER_BOUND): while low < high: mid = low + (high - low) // 2 if predicate(mid): high = mid else: low = mid + 1 return low print(binary_search()) main()
FUNC_DEF 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def solve(l1, l2, k, ans, n): w = 0 i = 0 while i < n: if c[i] > ans: m = ans // b[i] w += a[i] - m if w > k: return 0 i += 1 return 1 n, k = (int(s) for s in input().split()) a = [int(s) for s in input().split()] b = [int(s) for s in input().split()] c = [(a[i] * b[i]) for i in range(n)] low = 0 high = max(c) while low < high: ans = (high + low) // 2 if solve(a, b, k, ans, n): high = ans else: low = ans + 1 print(low)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def needm(c, k): need = 0 for a, b in cs: buy = k // b if buy < a: need += a - buy return need n, m = map(int, input().split()) ays = list(map(int, input().split())) bs = list(map(int, input().split())) if sum(ays) <= m: print(0) else: cs = list(zip(ays, bs)) hi = max(a * b for a, b in cs) lo = 0 while hi - lo > 1: md = (hi + lo) // 2 if needm(cs, md) > m: lo = md else: hi = md print(hi)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
n, b = map(int, input().split()) bal = list(map(int, input().split())) can = list(map(int, input().split())) def is_valid_total_candies(mid, bal, can, b): rem_bal = 0 for i in range(len(bal)): if bal[i] * can[i] > mid: bal_mid = mid // can[i] rem_bal += bal[i] - bal_mid if rem_bal > b: return False return True lo = 0 hi = 0 for i in range(n): if bal[i] * can[i] >= hi: hi = bal[i] * can[i] while lo < hi: mid = lo + (hi - lo) // 2 valid = is_valid_total_candies(mid, bal, can, b) if valid: hi = mid else: lo = mid + 1 print(lo)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def balloons_needed(a, b, candies): need = 0 for i in range(len(a)): if b[i] == 0: continue need += max(0, a[i] - candies // b[i]) return need n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) r = 0 for i in range(n): r = max(r, a[i] * b[i]) l = 0 while l < r: mid = (l + r) // 2 balloons = balloons_needed(a, b, mid) if balloons <= m: r = mid else: l = mid + 1 print(l)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements [Hindi] ,[Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well. Appy loves balloons! She wants you to give her balloons on each of $N$ consecutive days (numbered $1$ through $N$); let's denote the number of balloons Appy wants on day $i$ by $A_{i}$. The problem is that you only have $M$ balloons. Fortunately, you can give her candies instead of balloons as well. On each day $i$, Appy accepts $B_{i}$ candies per each balloon you do not give her β€” formally, if you give Appy $X_{i}$ balloons on day $i$, then you have to give her $C_{i} = \mathrm{max}(0, A_{i} - X_{i}) \cdot B_{i}$ candies as well. Your task is to minimize the maximum number of candies you give Appy on some day β€” find the minimum possible value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Input ------ The first line of the input contains two space-separated integers $N$ and $M$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$. The third line contains $N$ space-separated integers $B_{1}, B_{2}, \dots, B_{N}$. ------ Output ------ Print a single line containing one integer β€” the minimum value of $\mathrm{max}(C_{1}, C_{2}, C_{3}, ..., C_{N})$. ------ Constraints ------ $1 ≀ N ≀ 10^{5}$ $0 ≀ M ≀ 10^{18}$ $0 ≀ A_{i} ≀ 10^{9}$ for each valid $i$ $0 ≀ B_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (27 points): $1 ≀ A_{i} ≀ 10$ for each valid $i$ $1 ≀ B_{i} ≀ 10$ for each valid $i$ Subtask #2 (73 points): original constraints ----- Sample Input 1 ------ 5 3 1 2 3 4 5 1 2 3 4 5 ----- Sample Output 1 ------ 15 ----- explanation 1 ------ If you give Appy $0, 0, 0, 1, 2$ balloons on days $1$ through $5$, then the required numbers of candies on each day are $1, 4, 9, 12, 15$. The maximum number of candies is $15$, required on day $5$.
def calc(mid, m, C): maxc = 0 ballons = m for c, a, b in C: if c < mid: break ballons = ballons - (a - mid // b) maxc = max(mid // b * b, maxc) return ballons, maxc n, m = map(int, input().split()) A, B = list(map(int, input().split())), list(map(int, input().split())) C = [] maxc = 0 for i in range(n): C.append((A[i] * B[i], A[i], B[i])) C.sort(reverse=True) beg = 0 end = C[0][0] result = C[0][0] while end > beg + 1: mid = (beg + end + 1) // 2 ballons, maxc = calc(mid, m, C) if ballons < 0: beg = mid elif ballons > 0: end = mid result = maxc else: result = maxc break print(result)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Sarthak has a set S of N distinct prime numbers. He grew very fond of that set, and now he only likes a positive integer X if all of its prime factors belong to S. Given the set and a positive integer M, can you find out how many integers he likes which are not more than M? Note: Sarthak always like the number 1. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The first line of each test case contains two space-separated integers N and M - the size of S and the limit defined in the statement. - The second line of each test case contains N space-separated integers S_{1}, S_{2}, \dots, S_{N} - the elements of S. ------ Output Format ------ For each test case, output on a single line the number of positive integers Sarthak likes that is not more than M. ------ Constraints ------ $1 ≀ T ≀ 2$ $1 ≀ N ≀ 20$ $1 ≀ M ≀ 10^{16}$ $2 ≀ S_{i} < 1000$ $S$ has $N$ distinct prime numbers ----- Sample Input 1 ------ 1 2 10 3 5 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - Test case $1$: Sarthak likes the numbers $1$, $3$, $5$ and $9$.
def calculate(primeset, m): valueset = [1] for x in primeset: i = 0 while i < len(valueset): if valueset[i] * x <= m: valueset.append(valueset[i] * x) i += 1 return valueset def solve(primes, n, m): primes.sort() primeset1 = [primes[i] for i in range(0, n, 2)] primeset2 = [primes[i] for i in range(1, n, 2)] valueset1, valueset2 = calculate(primeset1, m), calculate(primeset2, m) valueset1.sort() valueset2.sort() ans, start, end = 0, 0, len(valueset2) - 1 while start < len(valueset1) and end >= 0: if valueset1[start] * valueset2[end] <= m: ans += end + 1 start += 1 else: end -= 1 print(ans) t = int(input()) for i in range(t): n, m = map(int, input().split(" ")) primes = list(map(int, input().split(" "))) solve(primes, n, m)
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR 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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR
Sarthak has a set S of N distinct prime numbers. He grew very fond of that set, and now he only likes a positive integer X if all of its prime factors belong to S. Given the set and a positive integer M, can you find out how many integers he likes which are not more than M? Note: Sarthak always like the number 1. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The first line of each test case contains two space-separated integers N and M - the size of S and the limit defined in the statement. - The second line of each test case contains N space-separated integers S_{1}, S_{2}, \dots, S_{N} - the elements of S. ------ Output Format ------ For each test case, output on a single line the number of positive integers Sarthak likes that is not more than M. ------ Constraints ------ $1 ≀ T ≀ 2$ $1 ≀ N ≀ 20$ $1 ≀ M ≀ 10^{16}$ $2 ≀ S_{i} < 1000$ $S$ has $N$ distinct prime numbers ----- Sample Input 1 ------ 1 2 10 3 5 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - Test case $1$: Sarthak likes the numbers $1$, $3$, $5$ and $9$.
def getAns(s): v = [1] for x in s: i = 0 while i < len(v): if v[i] * x <= m: v.append(v[i] * x) i += 1 return v t = int(input()) for _ in range(t): n, m = [int(v) for v in input().split()] s = sorted([int(v) for v in input().split()]) s1 = [v for v in s[::2]] s2 = [v for v in s[1::2]] v1 = sorted(getAns(s1)) v2 = sorted(getAns(s2)) ans, start, end = 0, 0, len(v2) - 1 while start < len(v1) and end >= 0: if v1[start] * v2[end] <= m: ans += end + 1 start += 1 else: end -= 1 print(ans)
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Sarthak has a set S of N distinct prime numbers. He grew very fond of that set, and now he only likes a positive integer X if all of its prime factors belong to S. Given the set and a positive integer M, can you find out how many integers he likes which are not more than M? Note: Sarthak always like the number 1. ------ Input Format ------ - The first line of each input contains T - the number of test cases. The test cases then follow. - The first line of each test case contains two space-separated integers N and M - the size of S and the limit defined in the statement. - The second line of each test case contains N space-separated integers S_{1}, S_{2}, \dots, S_{N} - the elements of S. ------ Output Format ------ For each test case, output on a single line the number of positive integers Sarthak likes that is not more than M. ------ Constraints ------ $1 ≀ T ≀ 2$ $1 ≀ N ≀ 20$ $1 ≀ M ≀ 10^{16}$ $2 ≀ S_{i} < 1000$ $S$ has $N$ distinct prime numbers ----- Sample Input 1 ------ 1 2 10 3 5 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ - Test case $1$: Sarthak likes the numbers $1$, $3$, $5$ and $9$.
import sys input = sys.stdin.readline def calculate(lst, m): d = [1] for x in lst: i = 0 while i < len(d): if x * d[i] <= m: d.append(x * d[i]) i += 1 return d for t in range(int(input())): n, m = map(int, input().split()) arr = list(map(int, input().split())) count = 0 lst1 = [arr[i] for i in range(0, n, 2)] lst2 = [arr[i] for i in range(1, n, 2)] val1 = calculate(lst1, m) val2 = calculate(lst2, m) val1.sort() val2.sort() s = 0 e = len(val2) - 1 while s < len(val1) and e >= 0: if val1[s] * val2[e] <= m: count += e + 1 s += 1 else: e -= 1 print(count)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
q = int(input()) for w in range(q): n = int(input()) arr = list(map(int, input().split())) arr = list(set(arr)) arr.sort(reverse=True) n = len(arr) ansarr = [(-1) for i in range(n)] maxans = 0 for i in range(n): currans = arr[i] for j in range(i + 1, min(i + 171, n)): if arr[i] % arr[j] != 0: currans = max(currans, arr[i] + arr[j]) if j + 1 < n and arr[i] + arr[j] + arr[j + 1] <= maxans: break for k in range(j + 1, min(j + 171, n)): if arr[j] % arr[k] != 0 and arr[i] % arr[k] != 0: currans = max(currans, arr[i] + arr[j] + arr[k]) if currans <= maxans: break ansarr[i] = currans maxans = max(maxans, currans) print(max(ansarr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
import sys input = sys.stdin.readline q = int(input()) for testcase in range(q): n = int(input()) A = sorted(set(map(int, input().split())), reverse=True) L = len(A) ANS = A[0] for i in range(L): NOW0 = A[i] NOW1 = 0 flag = 0 for j in range(i + 1, L): if NOW0 % A[j] != 0: NOW1 = A[j] ANS = max(ANS, NOW0 + NOW1) for k in range(j + 1, L): if NOW0 % A[k] != 0 and NOW1 % A[k] != 0: ANS = max(ANS, NOW0 + NOW1 + A[k]) break break print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = [0, 0] + sorted(set(map(int, input().split()))) ans = a[-1] for z in range(len(a) - 1, 1, -1): y = z - 1 while a[y] > 0 and a[z] % a[y] == 0: y -= 1 x = y - 1 while a[x] > 0 and (a[z] % a[x] == 0 or a[y] % a[x] == 0): x -= 1 ans = max(ans, a[x] + a[y] + a[z]) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
for i in range(int(input())): n = int(input()) w = set() [w.add(int(j)) for j in input().split()] data = list(w) data.sort() nums = [data[-1]] ind = len(data) - 2 cnt = 1 while ind >= 0: if cnt == 1: if nums[0] % data[ind] != 0: nums.append(data[ind]) cnt += 1 elif nums[0] % data[ind] != 0 and nums[1] % data[ind] != 0: nums.append(data[ind]) break ind -= 1 ans = sum(nums) if len(data) >= 2: nums2 = [data[-2]] ind = len(data) - 3 cnt = 1 while ind >= 0: if cnt == 1: if nums2[0] % data[ind] != 0: nums2.append(data[ind]) cnt += 1 elif nums2[0] % data[ind] != 0 and nums2[1] % data[ind] != 0: nums2.append(data[ind]) break ind -= 1 ans = max(ans, sum(nums2)) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
M = lambda: list(map(int, input().split())) q = int(input()) for qer in range(q): n = int(input()) a = M() D = {} for i in a: D[i] = 1 a.sort(reverse=True) def get_max(): for i in a: if D[i]: return i return 0 def fix(x): for i in a: if x % i == 0: D[i] = 0 L = [0] * 3 for i in range(3): L[i] = get_max() if L[i] == 0: break fix(L[i]) ans = sum(L) if a[0] % 2 == 0 and a[0] % 3 == 0 and a[0] % 5 == 0: if a[0] / 2 in D and a[0] / 3 in D and a[0] / 5 in D: ans = max(ans, a[0] // 2 + a[0] // 3 + a[0] // 5) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
import sys input = sys.stdin.readline t = int(input().strip()) for _ in range(t): n = int(input().strip()) ls = list(map(int, input().strip().split())) ls = list(set(ls)) ls.sort() n = len(ls) if n == 1: print(ls[0]) elif n == 2: if ls[1] % ls[0] == 0: print(ls[1]) else: print(ls[0] + ls[1]) else: ans = [] for i in range(n - 1, n - 1 - 3, -1): c = i for j in range(c - 1, -1, -1): if ls[c] % ls[j] != 0: b = j break else: ans.append(ls[c]) continue for k in range(b - 1, -1, -1): if ls[c] % ls[k] != 0 and ls[b] % ls[k] != 0: a = k break else: ans.append(ls[c] + ls[b]) continue ans.append(ls[c] + ls[b] + ls[a]) print(max(ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
def solve(a_s): a_s = set(a_s) n = max(a_s) largest = n if n // 2 in a_s and n // 3 in a_s and n // 5 in a_s: largest = n // 2 + n // 3 + n // 5 m = [] for _ in range(3): if not a_s: break n = max(a_s) a_s = set([j for j in a_s if n % j != 0]) m.append(n) if sum(m) > largest: largest = sum(m) return largest q = int(input()) for _ in range(q): n = int(input()) a_s = list(map(int, input().split())) print(solve(a_s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
def cal(sorted_list, size): max_sum = 0 for k in range(len(sorted_list)): max_sum = max(sorted_list[k], max_sum) for i in range(k + 1, size): temp_1 = sorted_list[i] if sorted_list[k] % temp_1 != 0: if ( i + 1 < size and max_sum >= sorted_list[k] + sorted_list[i] + sorted_list[i + 1] ): break check_sum = sorted_list[k] + temp_1 max_sum = max(max_sum, check_sum) for j in range(i + 1, size): if max_sum >= sorted_list[k] + sorted_list[i] + sorted_list[j]: break temp_2 = sorted_list[j] if sorted_list[k] % temp_2 != 0 and temp_1 % temp_2 != 0: max_sum = max(max_sum, check_sum + temp_2) break return max_sum def solve(): no_querries = int(input()) output = [] for i in range(no_querries): size = int(input()) alist_raw = input().split() alist = [int(x) for x in alist_raw] alist.sort(reverse=True) alist = unique(alist) output.append(cal(alist, len(alist))) for x in output: print(x) def unique(alist): unique = [alist[0]] for i in range(1, len(alist)): if alist[i] < unique[len(unique) - 1]: unique.append(alist[i]) return unique solve()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
import sys def calcSum(first): nonlocal m s = m[first] k = 1 for i in range(first + 1, len(m)): yes = True for j in range(first, i): if m[j] % m[i] == 0: yes = False break if yes: s += m[i] k += 1 if k == 3: break return s nnn = int(input()) for _ in range(nnn): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) if n == 1: print(a[0]) continue if n == 2: if a[0] % a[1] == 0 or a[1] % a[0] == 0: print(max(a)) else: print(sum(a)) continue a.sort(reverse=True) m = [a[0]] for i in range(1, len(a)): if a[i] == a[i - 1]: continue yes = True for j in range(1, len(m)): if m[j] % a[i] == 0: yes = False break if yes: m.append(a[i]) if len(m) >= 10: break s1 = calcSum(0) if len(m) > 1: s2 = calcSum(1) else: s2 = 0 s = max(s1, s2) print(s)
IMPORT FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
answer = [] for _ in range(int(input())): input() l = sorted(map(int, input().split())) other = 0 if l[-1] % 30 == 0 and len(set(l) & {l[-1] // 2, l[-1] // 3, l[-1] // 5}) == 3: other = 31 * l[-1] // 30 while len(l) >= 2 and l[-1] % l[-2] == 0: del l[-2] while len(l) >= 3 and 0 in (l[-1] % l[-3], l[-2] % l[-3]): del l[-3] answer.append(str(max(other, sum(l[-3:])))) print("\n".join(answer))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
def calc(X, Y): if len(Y) == 3: return sum(Y) for x in X: for y in Y: if y % x == 0: break else: return calc([i for i in X if i != x], sorted(Y + [x])[::-1]) return sum(Y) for _ in range(int(input())): N = int(input()) A = sorted(set([int(a) for a in input().split()]))[::-1] print(max(calc(A, []), calc(A[1:], [])))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR LIST VAR NUMBER RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR NUMBER LIST
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
T = int(input()) for o in range(T): n = int(input()) a = list(map(int, input().split())) a.sort() b = [] for i in a: if len(b) == 0 or len(b) != 0 and b[-1] != i: b.append(i) a = b n = len(b) if n == 1: print(a[0]) elif n == 2: if a[1] % a[0] == 0: print(a[1]) else: print(a[0] + a[1]) else: ans1 = a[-1] for i in reversed(range(0, n - 1)): if a[-1] % a[i] != 0: ans1 += a[i] for j in reversed(range(0, i)): if a[-1] % a[j] != 0 and a[i] % a[j] != 0: ans1 += a[j] break break ans2 = a[-2] for i in reversed(range(0, n - 2)): if a[-2] % a[i] != 0: ans2 += a[i] for j in reversed(range(0, i)): if a[-2] % a[j] != 0 and a[i] % a[j] != 0: ans2 += a[j] break break print(max(ans1, ans2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
q = int(input()) ans = [] for w in range(q): n = int(input()) a = [int(x) for x in input().split()] a.sort() a = a[::-1] a1 = 0 b1 = 0 c1 = 0 a1 = a[0] for i in range(n): if a1 % a[i] != 0: b1 = a[i] break for i in range(n): if a1 % a[i] != 0 and b1 % a[i] != 0: c1 = a[i] break a2 = 0 b2 = 0 c2 = 0 if a1 % 2 == 0: for i in range(n): if a[i] == a1 // 2: a2 = a[i] break if a1 % 3 == 0: for i in range(n): if a[i] == a1 // 3: b2 = a[i] break if a1 % 5 == 0: for i in range(n): if a[i] == a1 // 5: c2 = a[i] break ans.append(max(a1 + b1 + c1, a2 + b2 + c2)) for i in range(q): print(ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
q = int(input()) for query in range(q): n = int(input()) l = list(map(int, input().split())) l.sort() l.reverse() wy = -1000000000000 a = [l[0]] u = 1 while u < n: if l[u] != l[u - 1]: a.append(l[u]) if len(a) > 4: break u += 1 if ( len(a) > 3 and a[1] == a[0] / 2 and a[2] == a[0] / 3 and a[3] == a[0] / 5 or len(a) > 4 and a[1] == a[0] / 2 and a[2] == a[0] / 3 and a[3] == a[0] / 4 and a[4] == a[0] / 5 ): wy = a[1] + a[2] + a[3] nowa = [l[i] for i in range(n) if l[0] % l[i] != 0] nowa.sort() nowa.reverse() if len(nowa) > 0: part = 0 for i in range(1, len(nowa)): if nowa[0] % nowa[i] != 0: part = nowa[i] break wyn = l[0] + nowa[0] + part else: wyn = l[0] print(max(wyn, wy))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
from sys import stdin, stdout q = int(stdin.readline()) for it in range(q): n = int(stdin.readline()) a = set(map(int, stdin.readline().split())) M = max(a) if M % 30 == 0 and M // 2 in a and M // 3 in a and M // 5 in a: p1 = M * 31 // 30 else: p1 = 0 ans = [M] a = sorted(list(a)) idx = len(a) - 2 while idx >= 0 and len(ans) < 3: if any([(x % a[idx] == 0) for x in ans]): idx -= 1 continue ans.append(a[idx]) idx -= 1 p2 = sum(ans) if p1 > p2: stdout.write(str(p1) + "\n") else: stdout.write(str(p2) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
from sys import stdin, stdout for _ in range(int(stdin.readline())): __ = int(stdin.readline()) a = sorted(set(map(int, stdin.readline().split())), reverse=True) s = a[0] n = len(a) for i in range(n): el = a[i] for j in range(i + 1, n): if el % a[j] != 0: s = max(s, el + a[j]) el1 = a[j] for k in range(j + 1, n): if el1 % a[k] != 0 and el % a[k] != 0: s = max(s, el + el1 + a[k]) break break stdout.write(f"{s}\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
for _ in range(int(input())): input() a = list(map(int, input().split())) a = sorted(list(set(a))) ans = max(a) n = len(a) for i in range(n): x, y, z = 0, 0, a[i] for j in reversed(range(i)): if not y: if z % a[j] != 0: y = a[j] elif not x: if z % a[j] != 0 and y % a[j] != 0: x = a[j] else: break ans = max(ans, x + y + z) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
import sys 3.6 def eprint(*args, **kwargs): if __debug__: print(*args, file=sys.stderr, **kwargs) def __starting_point(): q = int(input()) while q > 0: ans = 0 q -= 1 x = y = z = 0 n = int(input()) arr = list(set(map(int, input().split(" ")))) arr.sort() if arr == [2, 3, 5]: print(10) continue z = arr[len(arr) - 1] if ( z % 2 == 0 and z % 3 == 0 and z % 5 == 0 and arr.count(z // 2) > 0 and arr.count(z // 3) > 0 and arr.count(z // 5) > 0 ): ans = max(ans, z // 2 + z // 3 + z // 5) res = [] while len(arr) > 0 and len(res) < 3: c = arr.pop() flag = 1 for x in res: flag &= x % c != 0 if flag == 1: res.append(c) ans = max(ans, sum(res)) print(ans) __starting_point()
IMPORT EXPR NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
q = int(input()) for w in range(q): n = int(input()) arr = list(map(int, input().split())) arr = list(set(arr)) arr.sort(reverse=True) n = len(arr) ans = 0 for i in range(n): ans = max(ans, arr[i]) for j in range(i + 1, n): if arr[i] % arr[j] != 0: ans = max(ans, arr[i] + arr[j]) if j + 1 < n and ans >= arr[i] + arr[j + 1] + arr[j]: break for k in range(j + 1, n): if ans >= arr[i] + arr[j] + arr[k]: break if arr[j] % arr[k] != 0 and arr[i] % arr[k] != 0: ans = max(ans, arr[i] + arr[j] + arr[k]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
q = int(input()) for _ in range(q): e = set() n = int(input()) a = sorted(map(int, input().split()))[::-1] for x in a: e.add(x) r = [] for k in a: for c in r: if c % k == 0: break else: r.append(k) if len(r) >= 3: break m = r[0] if ( m % 30 == 0 and m // 2 in e and m // 3 in e and m // 5 in e and m // 30 * 31 > sum(r) ): print(m // 30 * 31) else: print(sum(r))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
from itertools import tee from sys import stdin def input(): return stdin.readline() def remove_divisors(x, xs): return [y for y in xs if y % xs != 0] q = int(input()) for _ in range(q): n = int(input()) aas = list(set(map(int, input().split()))) aas.sort(reverse=True) ulim2 = list(map(sum, zip(aas[:-1], aas[1:]))) + [0] ulim3 = list(map(sum, zip(aas[:-2], ulim2[1:]))) + [0, 0] it1 = iter(zip(aas, ulim3, ulim2)) answer = 0 try: while True: a, u3, _ = next(it1) if u3 < answer: break it1, it2 = tee(it1) def f1(i): b, _, _ = i return a % b != 0 it2 = filter(f1, it2) tsum = 0 try: while True: b, _, u2 = next(it2) if u2 < tsum: break it2, it3 = tee(it2) def f2(i): c, _, _ = i return b % c != 0 it3 = filter(f2, it3) c, _, _ = next(it3, (0, 0, 0)) tsum = max(tsum, b + c) except StopIteration: pass answer = max(answer, a + tsum) except StopIteration: pass print(answer)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
q = int(input()) for i in range(q): n = int(input()) a = [int(i) for i in input().split()] a.sort(reverse=True) first = a[0] second = -1 third = -1 maxa = a[0] arr = [] for i in range(n): if first % a[i] != 0: if second == -1: second = a[i] elif second % a[i] != 0: if third == -1: third = a[i] else: break if maxa % 30 == 0: if maxa // 2 in a and maxa // 3 in a and maxa // 5 in a: print( max( first + max(second, 0) + max(third, 0), maxa // 2 + maxa // 3 + maxa // 5, ) ) else: print(max(first, 0) + max(second, 0) + max(third, 0)) else: print(max(first, 0) + max(second, 0) + max(third, 0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $n$ problems and should choose at most three of them into this contest. The prettiness of the $i$-th problem is $a_i$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $x, y, z$, then $x$ should be divisible by neither $y$, nor $z$, $y$ should be divisible by neither $x$, nor $z$ and $z$ should be divisible by neither $x$, nor $y$. If the prettinesses of chosen problems are $x$ and $y$ then neither $x$ should be divisible by $y$ nor $y$ should be divisible by $x$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. The first line of the query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of problems. The second line of the query contains $n$ integers $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the prettiness of the $i$-th problem. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query. -----Example----- Input 3 4 5 6 15 30 4 10 6 30 15 3 3 4 6 Output 30 31 10
import sys q = int(input()) lines = sys.stdin.readlines() res = [] def getMax(numset): n = max(numset) numset = set([j for j in numset if n % j != 0]) return n, numset for i in range(q): n = int(lines[2 * i]) m = [] numset = set() for j in lines[2 * i + 1].split(): numset.add(int(j)) total = max(numset) n = total if n // 2 in numset and n // 3 in numset and n // 5 in numset: total = n // 2 + n // 3 + n // 5 for _ in range(3): if len(numset) == 0: break n, numset = getMax(numset) m.append(n) if sum(m) > total: total = sum(m) res.append(str(total)) print("\n".join(res))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
t = int(input()) while t > 0: t -= 1 n, h = map(int, input().split()) a = [int(x) for x in input().split()] s = sum(a) mx = max(a) low = 1 high = mx mn = 0 if n == h: print(mx) else: while low <= high: k = (low + high) // 2 s = 0 for ele in a: s += (ele + k - 1) // k if s <= h: high = k - 1 mn = k else: low = k + 1 print(mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def MinimumHours(n, h, a): a.sort() b = a[0] c = 0 s = 0 j = 0 m = 1 while 1: for i in a[-1 : -(n + 2) : -1]: if i % m == 0: s += i // m else: s += i // m + 1 if s > h: break if s <= h: return m m += 1 s = 0 p = [] for i in range(int(input())): n, h = input().split() p.append(MinimumHours(int(n), int(h), [int(i) for i in input().split()])) for i in p: print(i)
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def bananas(lower, upper): k = (upper + lower) // 2 if upper <= lower: return k sum = 0 for i in l: if i % k == 0: sum += i / k else: sum += i // k + 1 if sum > hours: return bananas(k + 1, upper) elif sum == hours: return bananas(lower, k) else: return bananas(lower, k) test = int(input()) for i in range(test): nandh = [int(i) for i in input().split()] hours = nandh[1] l = [int(i) for i in input().split()] max = l[0] for i in l: if i > max: max = i print(bananas(1, max))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def trace(bIP, bPH): hT = 0 for bICP in bIP: bICPAE = bICP temp = bICPAE // bPH if bICPAE % bPH != 0: temp = temp + 1 hT = hT + temp return hT nOTC = int(input()) for _ in range(0, nOTC): nOP, hG = input().split() nOP, hG = int(nOP), int(hG) bIP = list(map(int, input().split())) st, end = 1, max(bIP) ans = -1 while st <= end: bPH = (st + end) // 2 hT = trace(bIP, bPH) if hT > hG: st = bPH + 1 else: ans = bPH end = bPH - 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def check(mid): total = 0 for i in piles: total += (i + mid - 1) // mid return total <= hours def solve(no_piles, hours, piles): lim_0 = 1 end = max(piles) ans = end while lim_0 <= end: mid = (lim_0 + end) // 2 if check(mid): ans = min(ans, mid) end = mid - 1 else: lim_0 = mid + 1 return ans test_cases = int(input()) for _ in range(test_cases): number_of_piles, hours = list(map(int, input().split())) piles = list(map(int, input().split())) print(solve(number_of_piles, hours, piles))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def MinimumHours(n, h, a): a.sort() j = 0 mie = 2 mio = 1 s1 = 0 s2 = 0 c1 = 0 c2 = 0 while 1: for i in a[-1 : -(n + 1) : -1]: if i % mie == 0: s1 += i // mie else: s1 += i // mie + 1 if i % mio == 0: s2 += i // mio else: s2 += i // mio + 1 if s1 > h: c1 = 1 if s2 > h: c2 = 1 if c1 == c2 == 1: break if c1 == c2 == 1: c1 = 0 s1 = 0 c2 = 0 s2 = 0 mie += 2 mio += 2 else: if c2 == c1 == 0: return min(mie, mio) if c1 == 0: return mie return mio p = [] for i in range(int(input())): n, h = input().split() p.append(MinimumHours(int(n), int(h), [int(i) for i in input().split()])) for i in p: print(i)
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
for z in range(int(input())): n, h = [int(x) for x in input().split()] a = [int(x) for x in input().split()] def bs(l, r, h): if l > r: return max(a) m = int((l + r) / 2) x = 0 for i in range(len(a)): x += 1 + a[i] // m if a[i] % m == 0: x -= 1 if x <= h: return min(bs(l, m - 1, h), m) return bs(m + 1, r, h) print(bs(1, max(a), h))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def MinimumHours(n, h, a): a.sort() j = 0 mi = a[j] s = 0 c = 0 c2 = 0 while 1: for i in a[-1 : -(n + 2) : -1]: if i % mi == 0: s += i // mi else: s += i // mi + 1 if s > h: c = 1 if c2 == 1: return mi + 1 break if c2 == 0 and c == 1: pmi = mi j += 1 mi = a[j] c = 0 s = 0 continue c2 = 1 mi -= 1 s = 0 p = [] for i in range(int(input())): n, h = input().split() p.append(MinimumHours(int(n), int(h), [int(i) for i in input().split()])) for i in p: print(i)
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def godFunc(arr, k, h): noh = sum([(i // k + int(i % k != 0)) for i in arr]) return noh <= h for _ in range(int(input())): n, h = map(int, input().split()) arr = list(map(int, input().split())) l, r = 1, 1000000000 while l < r: mid = (l + r) // 2 if godFunc(arr, mid, h): r = mid else: l = mid + 1 print(l)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def check(m): count = 0 for el in arr: if m < el: count += (el + m - 1) // m else: count += 1 if count <= hours: return True else: return False t = int(input()) for i in range(t): length, hours = map(int, input().split()) arr = list(map(int, input().split())) r = 1000000000 l = 1 ans = 10000000000 while r >= l: m = (l + r) // 2 if check(m): r = m - 1 ans = min(ans, m) else: l = m + 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Minion Chef likes to eat bananas a lot. There are N piles of bananas in front of Chef; for each i (1 ≀ i ≀ N), the i-th pile contains Ai bananas. Chef's mother wants her to eat the bananas and be healthy. She has gone to the office right now and will come back in H hours. Chef would like to make sure that she can finish eating all bananas by that time. Suppose Chef has an eating speed of K bananas per hour. Each hour, she will choose some pile of bananas. If this pile contains at least K bananas, then she will eat K bananas from it. Otherwise, she will simply eat the whole pile (and won't eat any more bananas during this hour). Chef likes to eat slowly, but still wants to finish eating all the bananas on time. Therefore, she would like to choose the minimum K such that she is able to eat all the bananas in H hours. Help Chef find that value of K. -----Input----- - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - The first line of each test case contains two space-separated integers N and H denoting the number of piles and the number of hours after which Chef's mom will come home. - The second line contains N space-separated integers A1, A2, ..., AN. -----Output----- For each test case, print a single line containing one integer β€” the minimum possible value of K. -----Constraints----- - 1 ≀ T ≀ 10 - 1 ≀ N ≀ 105 - N ≀ H ≀ 109 - 1 ≀ Ai ≀ 109 for each valid i -----Subtasks----- Subtask #1 (30 points): - 1 ≀ N ≀ 100 - Ai ≀ 103 for each valid i Subtask #2 (70 points): original constraints -----Example----- Input: 3 3 3 1 2 3 3 4 1 2 3 4 5 4 3 2 7 Output: 3 2 4 -----Explanation----- Example case 1: With a speed of K = 3 bananas per hour, Chef can finish eating all the bananas in 3 hours. It's the minimum possible speed with which she can eat all the bananas in 3 hours. With a speed of 2 bananas per hour, she would take at least 4 hours and with a speed of 1 banana per hour, she would take at least 6 hours.
def check(bananas, mid_val, H): time = 0 for i in range(len(bananas)): if bananas[i] % mid_val != 0: time += bananas[i] // mid_val + 1 else: time += bananas[i] // mid_val if time <= H: return True else: return False def minEatingSpeed(piles, H): start = 1 end = sorted(piles.copy(), reverse=True)[0] while start < end: mid = start + (end - start) // 2 if check(piles, mid, H) == True: end = mid else: start = mid + 1 return end t = int(input()) for i in range(t): a = [int(i) for i in input().split()][:2] n = a[0] h = a[1] b = [int(i) for i in input().split()][:n] print(minEatingSpeed(b, h))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vasya is a CEO of a big construction company. And as any other big boss he has a spacious, richly furnished office with two crystal chandeliers. To stay motivated Vasya needs the color of light at his office to change every day. That's why he ordered both chandeliers that can change its color cyclically. For example: red – brown – yellow – red – brown – yellow and so on. There are many chandeliers that differs in color set or order of colors. And the person responsible for the light made a critical mistake β€” they bought two different chandeliers. Since chandeliers are different, some days they will have the same color, but some days β€” different. Of course, it looks poor and only annoys Vasya. As a result, at the $k$-th time when chandeliers will light with different colors, Vasya will become very angry and, most probably, will fire the person who bought chandeliers. Your task is to calculate the day, when it happens (counting from the day chandeliers were installed). You can think that Vasya works every day without weekends and days off. -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n, m \le 500000$; $1 \le k \le 10^{12}$) β€” the number of colors in the first and the second chandeliers and how many times colors should differ to anger Vasya. The second line contains $n$ different integers $a_i$ ($1 \le a_i \le 2 \cdot \max(n, m)$) that describe the first chandelier's sequence of colors. The third line contains $m$ different integers $b_j$ ($1 \le b_i \le 2 \cdot \max(n, m)$) that describe the second chandelier's sequence of colors. At the $i$-th day, the first chandelier has a color $a_x$, where $x = ((i - 1) \mod n) + 1)$ and the second one has a color $b_y$, where $y = ((i - 1) \mod m) + 1)$. It's guaranteed that sequence $a$ differs from sequence $b$, so there are will be days when colors of chandeliers differs. -----Output----- Print the single integer β€” the index of day when Vasya will become angry. -----Examples----- Input 4 2 4 4 2 3 1 2 1 Output 5 Input 3 8 41 1 3 2 1 6 4 3 5 7 2 8 Output 47 Input 1 2 31 1 1 2 Output 62 -----Note----- In the first example, the chandeliers will have different colors at days $1$, $2$, $3$ and $5$. That's why the answer is $5$.
import sys readline = sys.stdin.readline def gcd(a, b): while b: a, b = b, a % b return a def lcm(a, b): return a * b // gcd(a, b) def egcd(a, b): if a == 0: return b, 0, 1 else: g, y, x = egcd(b % a, a) return g, x - b // a * y, y N, M, K = map(int, readline().split()) L = lcm(N, M) gnm = gcd(N, M) A = list(map(int, readline().split())) B = list(map(int, readline().split())) g, p1, p2 = egcd(N, M) def crt(b1, M1, b2, M2): s = (b2 - b1) // g return (b1 + s * M1 * p1) % L Bi = dict() for i in range(M): Bi[B[i]] = i rr = [] for i in range(N): if A[i] in Bi: j = Bi[A[i]] if i % gnm == j % gnm: rr.append(crt(i, N, j, M)) rr.sort() Z = len(rr) ok = (L + 1) * (K + 1) ng = 0 while abs(ok - ng) > 1: med = (ok + ng) // 2 res = med - (med - 1) // L * Z xx = (med - 1) % L okk = 0 ngg = Z + 1 while abs(okk - ngg) > 1: medd = (okk + ngg) // 2 if rr[medd - 1] <= xx: okk = medd else: ngg = medd res -= okk if res < K: ng = med else: ok = med print(ok)
IMPORT ASSIGN VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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 ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vasya is a CEO of a big construction company. And as any other big boss he has a spacious, richly furnished office with two crystal chandeliers. To stay motivated Vasya needs the color of light at his office to change every day. That's why he ordered both chandeliers that can change its color cyclically. For example: red – brown – yellow – red – brown – yellow and so on. There are many chandeliers that differs in color set or order of colors. And the person responsible for the light made a critical mistake β€” they bought two different chandeliers. Since chandeliers are different, some days they will have the same color, but some days β€” different. Of course, it looks poor and only annoys Vasya. As a result, at the $k$-th time when chandeliers will light with different colors, Vasya will become very angry and, most probably, will fire the person who bought chandeliers. Your task is to calculate the day, when it happens (counting from the day chandeliers were installed). You can think that Vasya works every day without weekends and days off. -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n, m \le 500000$; $1 \le k \le 10^{12}$) β€” the number of colors in the first and the second chandeliers and how many times colors should differ to anger Vasya. The second line contains $n$ different integers $a_i$ ($1 \le a_i \le 2 \cdot \max(n, m)$) that describe the first chandelier's sequence of colors. The third line contains $m$ different integers $b_j$ ($1 \le b_i \le 2 \cdot \max(n, m)$) that describe the second chandelier's sequence of colors. At the $i$-th day, the first chandelier has a color $a_x$, where $x = ((i - 1) \mod n) + 1)$ and the second one has a color $b_y$, where $y = ((i - 1) \mod m) + 1)$. It's guaranteed that sequence $a$ differs from sequence $b$, so there are will be days when colors of chandeliers differs. -----Output----- Print the single integer β€” the index of day when Vasya will become angry. -----Examples----- Input 4 2 4 4 2 3 1 2 1 Output 5 Input 3 8 41 1 3 2 1 6 4 3 5 7 2 8 Output 47 Input 1 2 31 1 1 2 Output 62 -----Note----- In the first example, the chandeliers will have different colors at days $1$, $2$, $3$ and $5$. That's why the answer is $5$.
def main(): n, m, k = list(map(lambda x: int(x), str(input()).split(" "))) a = list(map(lambda x: int(x), str(input()).split(" "))) b = list(map(lambda x: int(x), str(input()).split(" "))) if n < m: print(solve(m, n, k, b, a)) return print(solve(n, m, k, a, b)) def solve(n, m, k, a, b): d = gcd(n, m) x, y = 0, 0 for i in range(1, m): if (i * n - d) % m == 0: x = i y = (i * n - d) // m if y == 0: x += 1 y += 1 break common = {} common_count = 0 colors = {} for i in range(len(a)): colors[a[i]] = i for i in range(len(b)): if b[i] in colors and (colors[b[i]] - i) % d == 0: common[colors[b[i]]] = i common_count += 1 com = [] for key, val in common.items(): z = (val - key) // d com.append(int(z * x % (m // d) * n + key)) new_k = k % (m * n // d - common_count) s = k // (m * n // d - common_count) * m * n // d if new_k == 0: new_k = m * n // d - common_count s -= m * n // d com = sorted(com) cur = -1 for c in com: if new_k < c - cur: s += new_k return s new_k -= c - cur - 1 s += c - cur cur = c return s + new_k def gcd(n, m): if n == m: return n if n < m: return gcd(m, n) if n % m == 0: return m return gcd(m, n % m) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Vasya is a CEO of a big construction company. And as any other big boss he has a spacious, richly furnished office with two crystal chandeliers. To stay motivated Vasya needs the color of light at his office to change every day. That's why he ordered both chandeliers that can change its color cyclically. For example: red – brown – yellow – red – brown – yellow and so on. There are many chandeliers that differs in color set or order of colors. And the person responsible for the light made a critical mistake β€” they bought two different chandeliers. Since chandeliers are different, some days they will have the same color, but some days β€” different. Of course, it looks poor and only annoys Vasya. As a result, at the $k$-th time when chandeliers will light with different colors, Vasya will become very angry and, most probably, will fire the person who bought chandeliers. Your task is to calculate the day, when it happens (counting from the day chandeliers were installed). You can think that Vasya works every day without weekends and days off. -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n, m \le 500000$; $1 \le k \le 10^{12}$) β€” the number of colors in the first and the second chandeliers and how many times colors should differ to anger Vasya. The second line contains $n$ different integers $a_i$ ($1 \le a_i \le 2 \cdot \max(n, m)$) that describe the first chandelier's sequence of colors. The third line contains $m$ different integers $b_j$ ($1 \le b_i \le 2 \cdot \max(n, m)$) that describe the second chandelier's sequence of colors. At the $i$-th day, the first chandelier has a color $a_x$, where $x = ((i - 1) \mod n) + 1)$ and the second one has a color $b_y$, where $y = ((i - 1) \mod m) + 1)$. It's guaranteed that sequence $a$ differs from sequence $b$, so there are will be days when colors of chandeliers differs. -----Output----- Print the single integer β€” the index of day when Vasya will become angry. -----Examples----- Input 4 2 4 4 2 3 1 2 1 Output 5 Input 3 8 41 1 3 2 1 6 4 3 5 7 2 8 Output 47 Input 1 2 31 1 1 2 Output 62 -----Note----- In the first example, the chandeliers will have different colors at days $1$, $2$, $3$ and $5$. That's why the answer is $5$.
def main(): n, m, k = map(int, input().split()) aa = map(int, input().split()) bb = map(int, input().split()) if n < m: n, m, aa, bb = m, n, bb, aa x = y2 = 1 y = x2 = 0 g, a = n, m while a: q = g // a g, a, x, y, x2, y2 = a, g % a, x2, y2, x - x2 * q, y - y2 * q nm, nm2, mg = n // g * m, n * 2 + 1, m // g colors, common = [-1] * nm2, [-1] * nm2 for i, a in enumerate(aa): colors[a] = i for i, b in enumerate(bb): b = colors[b] if b != -1 in colors and not (b - i) % g: common[b] = i common = [((a - i) // g * x % mg * n + i) for i, a in enumerate(common) if a != -1] common_count = len(common) res, k = divmod(k, nm - common_count) if not k: k = nm - common_count res -= 1 res *= nm common.sort() a = -1 for b in common: b -= a if k < b: break k -= b - 1 res += b a += b print(res + k) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
from sys import stdin def input(): return next(stdin) def main(): n, k = map(int, input().split()) aa = [int(a) for a in input().split()] aa.sort() valmap = {} for a in aa: add_to_map(a, 0, k, valmap) i = 0 while a > 0: i += 1 a = a // 2 add_to_map(a, i, k, valmap) min = 20 * k for vl in valmap.values(): if len(vl) == k and sum(vl) < min: min = sum(vl) print(min) def add_to_map(a, i, k, valmap): if a in valmap: if len(valmap[a]) < k: valmap[a].append(i) else: valmap[a] = [i] main()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
import sys input = sys.stdin.buffer.readline def solution(): n, k = map(int, input().split()) l = list(map(int, input().split())) l.sort() val = [0] * 1000001 op = [0] * 1000001 m = 2000000000.0 for i in range(n): val[l[i]] += 1 for i in range(n): cnt = 0 x = l[i] while x > 0: if val[x] >= k: m = min(m, op[x]) cnt += 1 x //= 2 op[x] += cnt val[x] += 1 print(m) solution()
IMPORT ASSIGN VAR VAR FUNC_DEF 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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) x = list(map(int, input().split())) q = [] for i in range(200001): q.append([0, 0]) x.sort() for i in range(n): a = x[i] q[a][0] += 1 c = 0 while a: a = a // 2 c = c + 1 if q[a][0] < k: q[a][0] += 1 q[a][1] += c q.sort(key=lambda x: x[1]) for i in q: if i[0] >= k: print(i[1]) break
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 ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
def cal_dis(num, val): count = 0 while num > 0: if val[num] == 0: val[num] = [] val[num].append(count) num = num // 2 count += 1 return count, val n, k = (int(x) for x in input().split()) seq = [int(x) for x in input().split()] ans = 1000000000.0 val = 200009 * [0] for ele in seq: count, val = cal_dis(ele, val) for lis in val: if lis != 0: if lis.__len__() >= k: lis.sort() sum = 0 for ele in lis[:k]: sum += ele ans = min(ans, sum) print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER LIST NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) L = sorted(list(map(int, input().split()))) M = [[] for i in range(L[-1] + 1)] for i in range(n): ct = 0 a = L[i] while True: M[a] += [ct] ct += 1 a //= 2 if a == 0: M[0] += [ct] break ans = None for m in M: if len(m) < k: continue if ans == None: ans = sum(m[:k]) else: ans = min(ans, sum(m[:k])) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER VAR VAR LIST VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER LIST VAR ASSIGN VAR NONE FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) l = list(map(int, input().split())) d = {} for liczba in l: ll = liczba while ll > 0: d[ll] = [] ll //= 2 for liczba in l: i = 0 ll = liczba while ll > 0: d[ll].append(i) ll //= 2 i += 1 wyn = 1000000000 for number in d: if len(d[number]) < k: continue a = d[number].copy() a.sort() wyn = min(wyn, sum(a[:k])) print(wyn)
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 ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR LIST VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
def inti_dic(d, no, opertions): if no in d: a = d[no] if a[0] < k: a[0] += 1 a[1] += op d[no] = a else: d[no] = [1, opertions] n, k = map(int, input().split()) arr = list(map(int, input().split())) d = {} arr = sorted(arr) for i in arr: no, op = i, 0 inti_dic(d, no, op) while no != 0: no = no // 2 op += 1 inti_dic(d, no, op) minn = 10**15 for i in d: a = d[i] if a[0] >= k: minn = min(a[1], minn) print(minn)
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST NUMBER 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 ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
ans = {} n, k = (int(s) for s in input().split()) l = [int(s) for s in input().split()] l.sort() for i in range(n): x = l[i] count = 0 while x > 0: if x in ans: if ans[x][0] < k: ans[x][0] += 1 ans[x][1] += count else: ans[x] = [1, count] count += 1 x = x // 2 if 0 in ans: if ans[0][0] < k: ans[0][0] += 1 ans[0][1] += count else: ans[x] = [1, count] ansm = 1000000000007 for i in ans: if ans[i][0] >= k: ansm = min(ansm, ans[i][1]) print(ansm)
ASSIGN VAR DICT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR LIST NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR VAR LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) a = list(map(int, input().split())) m = max(a) val = [[] for _ in range(m + 1)] lenvals = [(0) for _ in range(m + 1)] for el in a: temp = el c = 0 val[temp].append(c) lenvals[temp] += 1 while temp > 1: temp //= 2 c += 1 val[temp].append(c) lenvals[temp] += 1 ans = float("inf") for i in range(1, m + 1): if lenvals[i] >= k: val[i].sort() ans = min(ans, sum(val[i][:k])) 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, m = map(int, input().split()) s = str(input()) a = [int(i) for i in s.split()] b = [0] * 200001 c = [0] * 200001 a.sort() for elem in a: b[elem] += 1 count = 0 while elem != 0 and b[elem // 2] < m: elem //= 2 count += 1 c[elem] += count b[elem] += 1 ans = 10000000000 for i in range(len(b)): if b[i] >= m: if ans > c[i]: ans = c[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
from sys import stdin def iinput(): return int(stdin.readline()) def minput(): return map(int, stdin.readline().split()) def linput(): return list(map(int, stdin.readline().split())) n, k = minput() a = linput() mn = float("inf") d = [[] for _ in range(200001)] for i in range(n): temp = a[i] c = 0 while temp: d[temp].append(c) c += 1 temp //= 2 for i in range(200001): if len(d[i]) >= k: d[i].sort() mn = min(mn, sum(d[i][:k])) print(mn)
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 ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
def find(A, K): counting = [[] for i in range(max(A) + 1)] okay = [0] * (max(A) + 1) for i in range(len(A)): count = 0 temp = A[i] while temp > 0: counting[temp] += [count] okay[temp] += 1 temp = temp // 2 count += 1 counting[temp] += [count] okay[temp] += 1 MIN = float("inf") for i in range(len(okay)): if okay[i] >= K: counting[i] = sorted(counting[i]) MIN = min(MIN, sum(counting[i][:K])) return MIN n, K = list(map(int, input().strip().split(" "))) A = list(map(int, input().strip().split(" "))) print(find(A, K))
FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR LIST VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR LIST VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort() ind = [0] * 200001 cnt = [0] * 200001 ans = [] for key in a: ind[key] += 1 tr = False for key in a: if ind[key] >= k: ans.append(cnt[key]) cnnt = 1 while key > 1: key //= 2 ind[key] += 1 cnt[key] += cnnt if ind[key] >= k: ans.append(cnt[key]) cnnt += 1 print(min(ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, m = list(map(int, input().split())) a = list(map(int, input().split())) f = {} for i in a: it = 0 while i > 0: if i in f: f[i].append(it) else: f[i] = [it] i //= 2 it += 1 s = 10000000000.0 for i in f: if len(f[i]) >= m: s = min(s, sum(sorted(f[i])[:m])) print(s)
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 ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort() c = list(set(a)) b = [[] for i in range(2 * 100000 + 1)] for i in a: temp = i j = 0 b[temp].append(0) if temp != 1: while 1: temp = temp // 2 j += 1 b[temp].append(j) if temp == 1: break res = 2 * 1000000000000000 for i in range(1, max(a) + 1): if len(b[i]) >= m: temp = sum(b[i][:m]) res = min(res, temp) print(res)
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 EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) l = list(map(int, input().split())) count = [0] * (2 * 10**5 + 1) no = [0] * (2 * 10**5 + 1) ans = float("inf") l.sort() for i in l: c = 0 e = 0 while i > 0: if no[i] < k: count[i] += c no[i] += 1 c += 1 i = i // 2 if i == 0: if no[i] < k: count[i] += c no[i] += 1 for i in range(len(count)): if no[i] >= k: ans = min(ans, count[i]) 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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = list(map(int, input().split())) it = list(map(int, input().split())) a = [[] for i in range(n)] ss = {} for i in range(n): j = it[i] a[i] = [j] try: ss[j].append(0) except: ss[j] = [0] st = 0 while True: st += 1 j = j // 2 try: ss[j].append(st) except: ss[j] = [st] a[i].append(j) if j == 0: break tt = ss.copy() mi = 10**10 for i in ss: if len(ss[i]) >= k: tt[i].sort() mi = min(mi, sum(tt[i][:k])) print(mi)
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 ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
def main(): n, k = list(map(int, input().split())) arr = list(map(int, input().split())) nums = [[(0) for i in range(2)] for j in range(2 * 10**5 + 1)] arr.sort() for i in arr: moves = 0 while i >= 0: if nums[i][0] == k: break nums[i][0] += 1 nums[i][1] += moves moves += 1 i = i // 2 if i == 0: nums[i][0] += 1 nums[i][1] += moves break min_moves = float("inf") for i in range(2 * 10**5 + 1): if nums[i][0] >= k: min_moves = min(min_moves, nums[i][1]) print(min_moves) main()
FUNC_DEF 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) a = list(map(int, input().split())) num = [0] * (2 * 10**5 + 1) count = [0] * (2 * 10**5 + 1) a.sort() for i in a: j = i t = 0 while j != 0: if num[j] < k: num[j] += 1 j = int(j / 2) t += 1 if num[j] < k: count[j] += t else: j = int(j // 2) m = 100000000 for i in range(len(num)): if num[i] == k: if count[i] < m: m = count[i] print(m)
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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
from sys import stdin n, k = [int(i) for i in stdin.readline().strip().split()] a = [int(i) for i in stdin.readline().strip().split()] p = [(19 * [0]) for _ in range(max(a) + 1)] nums = set() for m in a: for i in range(19): p[m >> i][i] += 1 nums.add(m >> i) if m >> i == 0: break res = 10000000000000000000 for m in nums: d = p[m] sum = 0 steps = 0 for i in range(19): if sum + d[i] >= k: add = (k - sum) * i steps += add res = min(steps, res) break else: sum += d[i] steps += i * d[i] print(res)
ASSIGN 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 BIN_OP NUMBER LIST NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) l = list(map(int, input().split())) l.sort() d = dict() c = 0 f = 0 a = [(0) for i in range(200001)] min1 = -1 while l[-1] != 0: for i in range(f, len(l)): if l[i] in d: d[l[i]] += 1 else: d[l[i]] = 1 a[l[i]] += c if d[l[i]] >= k: if min1 == -1: min1 = a[l[i]] else: min1 = min(min1, a[l[i]]) if l[i] == 0: f = i + 1 l[i] = l[i] // 2 c += 1 print(min1)
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 EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = [int(j) for j in input().split()] l = [int(j) for j in input().split()] num = int(2 * 100000.0 + 5) arr = [[] for i in range(num)] for i in range(n): cnt = 0 tmp = l[i] while tmp > 0: arr[tmp].append(cnt) tmp = tmp // 2 cnt += 1 ans = 1000000000000.0 for i in range(1, num): if len(arr[i]) >= k: arr[i].sort() ans = min(ans, sum(arr[i][:k])) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
MAX = 2 * 10**5 def main(): n, k = list(map(int, input().split())) a = sorted(map(int, input().split())) ops = [0] * (MAX + 1) ks = [0] * (MAX + 1) for x in a: c = x j = 0 while True: if ks[c] < k: ops[c] += j ks[c] += 1 if c == 0: break c //= 2 j += 1 min_ops = float("inf") for i in range(MAX + 1): if ks[i] == k: min_ops = min(ops[i], min_ops) print(min_ops) main()
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FUNC_DEF 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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) a = list(map(int, input().split())) cnt = [[] for i in range(2 * 10**5 + 10)] for num in a: tmp_cnt = 0 while True: if num == 0: cnt[num].append(tmp_cnt) break else: cnt[num].append(tmp_cnt) num = num // 2 tmp_cnt += 1 ans = 10**9 + 7 for cnt_i in cnt: if len(cnt_i) < k: continue else: cnt_i = sorted(cnt_i) ans = min(ans, sum(cnt_i[0:k])) 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 ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = list(map(int, input().split())) arr = list(map(int, input().split())) value_lists = [[] for _ in range(2 * 10**5 + 1)] for val in arr: turn = 0 while val != 1: value_lists[val].append(turn) val //= 2 turn += 1 value_lists[val].append(turn) mindist = 999999999 for turns_val in value_lists: if len(turns_val) < k: continue turns_val.sort() mindist = min(mindist, sum(turns_val[:k])) print(mindist)
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 ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = tuple(map(int, input().split())) a = list(map(int, input().split())) a1 = sorted(a) a = sorted(a) l = [[(0) for i in range(n)] for j in range(20)] d = {} for j in range(n): x = 1 for i in range(20): if i == 0: l[i][j] = a[j] else: l[i][j] = a[j] // 2 a[j] = a[j] // 2 value = 10**9 for i in range(20): for j in range(n): if l[i][j] != 0: if l[i][j] not in d: d[l[i][j]] = [1, i] elif d[l[i][j]][0] < k: d[l[i][j]][0] += 1 d[l[i][j]][1] += i for i in d: if d[i][0] >= k: value = min(value, d[i][1]) temp_ans = 0 for j in range(k): for i in range(1, 20): x = 2 if a1[j] // x != 0: a1[j] = a1[j] // x else: temp_ans += i - 1 value = min(temp_ans, value) print(value)
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR LIST NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR FOR VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() d1 = {} d2 = {} ans = 10**9 for i in range(n): num = a[i] c = 0 while num > 0: d1[num] = c if num not in d1 else d1[num] + c d2[num] = 1 if num not in d2 else d2[num] + 1 if d2[num] == k: ans = min(ans, d1[num]) c += 1 num //= 2 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 EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
import sys input = lambda: sys.stdin.readline().strip() n, k = map(int, input().split()) ls = list(map(int, input().split())) ls.sort() arr = [] elem = {} minSteps = {} for x in range(ls[-1] + 1): elem[x] = 0 minSteps[x] = 0 for i in ls: steps = 0 while i != 0: if elem[i] != k: elem[i] += 1 minSteps[i] += steps i //= 2 steps += 1 if elem[i] != k: elem[i] += 1 minSteps[i] += steps m = 1000000000000 for key, value in elem.items(): if value == k: m = min(m, minSteps[key]) print(m)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL 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 EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = [int(x) for x in input().split()] nums = sorted([int(x) for x in input().split()]) count = [(0) for _ in range(2 * 100000 + 10)] ops = [(0) for _ in range(2 * 100000 + 10)] for num in nums: op = 0 while num > 0: ops[num] += op if count[num] < k else 0 count[num] += 1 num = num // 2 op += 1 ops[num] += op if count[num] < k else 0 count[num] += 1 minn = 0 minop = ops[0] for i in range(2 * 100000 + 1): if count[i] >= k and ops[i] < minop: minn = i minop = ops[i] print(minop)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) a = list(map(int, input().split())) ans = [[0, 0] for i in range(200001)] a.sort() for i in range(n): ans[a[i]][0] += 1 for i in range(n): s = a[i] d = 1 while s > 0: s = s // 2 if ans[s][0] < k: ans[s][0] += 1 ans[s][1] += d d += 1 mi = 100000000 for i in range(len(ans)): if ans[i][0] >= k: if ans[i][1] < mi: mi = ans[i][1] print(mi)
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 ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) a = list(map(int, input().split())) arr = [[] for _ in range(int(200000.0 + 1))] for j in a: i = 0 while j > 0: arr[j].append(i) i += 1 j //= 2 arr[j].append(i) mn = -1 for i in arr: if len(i) >= k: sk = sum(sorted(i)[:k]) if mn > sk or mn == -1: mn = sk print(mn)
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 ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
z = [] a = [] for i in range(200010): a.append([]) n, k = [int(e) for e in input().split()] b = [int(e) for e in input().split()] for x in b: c = x ct = 0 a[x].append(0) while c != 0: c >>= 1 ct += 1 a[c].append(ct) ans = 10000000000 for i in range(200000): if len(a[i]) < k: continue a[i].sort() s = 0 for j in range(k): s += a[i][j] ans = min(ans, s) print(ans)
ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
vals = [[] for i in range(200 * 1000 + 11)] ans = 10**19 + 7 n, k = map(int, input().split()) (*a,) = map(int, input().split()) for i in range(n): x = a[i] cur = 0 while x > 0: vals[x].append(cur) x //= 2 cur += 1 for i in range(200 * 1000): new = vals[i] new.sort() if len(new) < k: continue ans = min(ans, sum(new[:k])) print(ans)
ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
from sys import stdin, stdout def __starting_point(): n, k = list(map(int, stdin.readline().split())) a = list(map(int, stdin.readline().split())) num = [[] for i in range(200005)] ans = 200000 * 100 for x in a: cnt = 0 while x > 0: num[x].append(cnt) x //= 2 cnt += 1 num[x].append(cnt) for i in range(200001): if len(num[i]) < k: continue num[i].sort() cnt = 0 for j in range(k): cnt += num[i][j] ans = min(ans, cnt) stdout.write("%d\n" % ans) __starting_point()
FUNC_DEF 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 ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
import sys def answer(n, k, a): mxa = max(a) costs = [[] for _ in range(mxa + 1)] for i in range(n): temp = a[i] cost = 0 while temp > 0: costs[temp].append(cost) temp //= 2 cost += 1 best = 10**7 for i in range(1, mxa + 1): if len(costs[i]) >= k: costs[i].sort() best = min(best, sum(costs[i][:k])) return best def main(): n, k = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) print(answer(n, k, a)) return main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
import sys [n, k] = [int(i) for i in sys.stdin.readline().split()] a = [int(j) for j in sys.stdin.readline().split()] arr = [] a.sort() prev = -2 for i in range(n): if prev != a[i]: arr.append([a[i], 1]) else: arr[-1][1] += 1 prev = a[i] done = 0 for j in range(len(arr)): if arr[j][1] >= k: done = 1 break if done == 1: print(0) else: count_arr = [] for w in range(200001): count_arr.append([0, 0]) ans = sys.maxsize for h in range(n): count_arr[a[h]][1] += 1 for g in range(n): tmp = a[g] tmp //= 2 step = 1 while tmp != 0: count_arr[tmp][0] += step count_arr[tmp][1] += 1 if count_arr[tmp][1] >= k: ans = min(ans, count_arr[tmp][0]) step += 1 tmp //= 2 count_arr[0][0] += step count_arr[0][1] += 1 if count_arr[0][1] >= k: ans = min(ans, count_arr[0][0]) print(ans)
IMPORT ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) arr = [int(z) for z in input().split()] moves = {} for i in range(n): l = arr[i] m = 0 while l: if not moves.get(l): moves[l] = [] moves[l].append(m) l //= 2 m += 1 res = 10**18 for i in moves: moves[i].sort() if len(moves[i]) < k: continue res = min(res, sum(moves[i][:k])) print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) x = [int(n) for n in input().split()] a = [] m = {} ans = 10000000000 if k != 1: for i in x: c = 0 t = i while t: if t not in m: m[t] = [] m[t].append(c) t //= 2 c += 1 for i in m: if len(m[i]) >= k: a = sorted(m[i]) ans = min(ans, sum(a[:k])) print(ans) else: c = 0 print(c)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
MOD = 10**9 + 7 I = lambda: list(map(int, input().split())) n, m = I() l = I() l.sort() ans = MOD o = [0] * 300000 d = [0] * 300000 for i in range(n): k = l[i] o[k] += 1 if o[k] == m: ans = min(ans, d[k]) i = 0 while k > 0: i += 1 k //= 2 o[k] += 1 d[k] += i if o[k] == m: ans = min(ans, d[k]) print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k = map(int, input().split()) x = [int(x) for x in input().split()] counter = [[] for _ in range(max(x) + 1)] for i in x: cnt = 0 divisible = True while divisible: counter[i].append(cnt) if i == 0: divisible = False i //= 2 cnt += 1 answer = pow(10, 10) for i in counter: if len(i) >= k: answer = min(answer, sum(sorted(i)[0:k])) print(answer)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
n, k1 = map(int, input().split()) li = list(map(int, input().split())) d = {} for i in li: k = 0 while i != 0: try: d[i].append(k) except KeyError: d[i] = [k] k += 1 i = i // 2 mi = 100000000 for i in d.values(): if len(i) >= k1: i.sort() mi = min(mi, sum(i[:k1])) print(mi)
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 ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
b = {} n, k = map(int, input().split()) a = list(map(int, input().split())) b[0] = [] for x in a: j = 0 while x > 0: if x in b: b[x].append(j) else: b[x] = [j] x //= 2 j += 1 b[0].append(j) ans = 10**10 for i in b: b[i].sort() if len(b[i]) >= k: ans = min(sum(b[i][:k]), ans) print(ans)
ASSIGN VAR DICT 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 ASSIGN VAR NUMBER LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the number of elements in the array. You are given an array $a$ consisting of $n$ integers. In one move you can choose any $a_i$ and divide it by $2$ rounding down (in other words, in one move you can set $a_i := \lfloor\frac{a_i}{2}\rfloor$). You can perform such an operation any (possibly, zero) number of times with any $a_i$. Your task is to calculate the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. Don't forget that it is possible to have $a_i = 0$ after some operations, thus the answer always exists. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of elements in the array and the number of equal numbers required. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$), where $a_i$ is the $i$-th element of $a$. -----Output----- Print one integer β€” the minimum possible number of operations required to obtain at least $k$ equal numbers in the array. -----Examples----- Input 5 3 1 2 2 4 5 Output 1 Input 5 3 1 2 3 4 5 Output 2 Input 5 3 1 2 3 3 3 Output 0
def amount_divide(): amount_dividers = [[] for _ in range(2 * 10**5 + 1)] for i in range(n): num = arr[i] j = 0 while num > 0: amount_dividers[num].append(j) num //= 2 j += 1 amount_dividers[0].append(j) ans = 10**15 for list1 in amount_dividers: if len(list1) >= k: ans = min(ans, sum(sorted(list1)[:k])) return ans n, k = map(int, input().split()) arr = list(map(int, input().split())) print(amount_divide())
FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR