description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
for tc in range(int(input())): ls = list(map(int, input().split())) lk = list(map(int, input().split())) lk.sort(reverse=True) val = ls[2] k = ls[1] su = 0 for x in range(0, len(lk) - 1, 2): if k > 0: if lk[x] + lk[x + 1] > val: su += val k -= 1 else: su += lk[x] + lk[x + 1] else: su += lk[x] + lk[x + 1] if len(lk) % 2 == 1: su += lk[-1] print(su)
FOR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
for t in range(int(input())): n, k, x = map(int, input().split()) A = [int(i) for i in input().split()] A.sort() res = 0 while k > 0: res += min(x, A.pop() + A.pop()) k -= 1 while len(A) > 0: res += A.pop() print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
t = int(input()) while t: N, K, X = list(map(int, input().split())) arr = list(map(int, input().split())) count = 0 arr.sort() n = N if N == 0: print(0) if N == 1: print(arr[0]) while K > 0 and len(arr) > 1: sz = len(arr) if X < arr[sz - 1] + arr[sz - 2]: arr.pop() arr.pop() count += X K -= 1 else: break for i in range(len(arr)): count += arr[i] print(count) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
def clearArr(arr, n, k, x): sumi = sum(arr) if k == 0: return sumi arr.sort(reverse=True) prs = [] i = 0 while i + 1 < n and k: if arr[i] + arr[i + 1] > x: prs.append(arr[i]) prs.append(arr[i + 1]) k -= 1 i += 2 suma = sum(prs) le = len(prs) return sumi - suma + x * (le // 2) for _ in range(int(input())): n, k, x = map(int, input().split()) arr = [int(i) for i in input().split()] print(clearArr(arr, n, k, x))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 FUNC_CALL VAR VAR VAR VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
t = int(input()) for i in range(t): n, k, x = map(int, input().split()) a = list(map(int, input().split())) c = sum(a) a.sort(reverse=True) t1 = 0 r = 0 while t1 == 0: if a[r] + a[r + 1] > x and k > 0: c = c - (a[r] + a[r + 1]) + x k = k - 1 r = r + 2 else: r = r + 1 if k == 0: t1 == 1 break if r >= n - 1: t1 == 1 break print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
t = int(input()) for test in range(t): n, k, x = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() i = n - 2 j = n - 1 res = 0 while i >= 0: if a[i] + a[j] >= x and k > 0: res += x i -= 2 j -= 2 k -= 1 else: res += a[i] + a[j] i -= 2 j -= 2 if j == 0: res += a[j] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
for _ in range(int(input())): n, k, x = map(int, input().split(" ")) arr = [int(w) for w in input().split(" ")] arr.sort(reverse=True) ans = sum(arr) i = 0 while i < n and i + 1 < n: if k == 0: break if arr[i] + arr[i + 1] > x: ans = ans - (arr[i] + arr[i + 1] - x) k = k - 1 else: break if k == 0: break i = i + 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
for t in range(int(input())): N, K, X = map(int, input().split()) arr = list(map(int, input().split())) cost = 0 i = N - 1 arr.sort() while K: if arr[i] + arr[i - 1] > X: cost += X K -= 1 i -= 2 else: break cost += sum(arr[: i + 1]) print(cost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
for __ in range(int(input())): N, K, X = map(int, input().split()) A = list(map(int, input().split())) cost = sum(A) A.sort(reverse=True) for i in range(0, 2 * K, 2): if X <= A[i] + A[i + 1]: cost += X - (A[i] + A[i + 1]) print(cost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
for _ in range(int(input())): n, k, x = map(int, input().split()) s = 0 l = list(map(int, input().split())) l.sort() while k > 0 and len(l) > 1: if l[-1] + l[-2] > x: s += x k -= 1 l.pop() l.pop() else: break s = sum(l) + s print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
def costClrArr(n, k, x, arr): totalCost = 0 if n == 1: return arr[0] if n == 2: return min(x, arr[0] + arr[1]) arr = arr[::-1] i = 0 while k > 0: c = arr[i] + arr[i + 1] if x <= c: totalCost = totalCost + x k -= 1 i += 2 else: k = 0 for idx in range(i, n): totalCost = totalCost + arr[idx] return totalCost t = int(input()) for _ in range(t): n, k, x = map(int, input().strip().split(" ")) arr = sorted(list(map(int, input().strip().split(" ")))) print(costClrArr(n, k, x, arr))
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL 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 VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
t = int(input()) for _ in range(t): n, k, x = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() i = n - 1 ans = 0 while i >= 1 and k > 0: temp = arr[i] + arr[i - 1] if temp <= x: ans = ans + temp else: ans = ans + x k = k - 1 i = i - 2 if i != -1: for i in range(i, -1, -1): ans += arr[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well. You are given an array A of N elements. You can do the following operations on that array: Remove the leftmost element of the array, with index l, for the cost A_{l}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove the rightmost element of the array, with index r, for the cost A_{r}. This can be done any number of times if the array is non-empty (has at least 1 element). Remove both the leftmost and rightmost element, with indexes l and r respectively, for a fixed cost X which is given. This operation can only be done K times and only if the array has at least 2 elements left. If the array has only 1 element left, then the rightmost element is the same as the leftmost element so the first and second operations will have the same cost. You have to print the minimum cost to *clear the array* (remove all elements using one of the three operations above). NOTE: The answer may not fit in 32-bit integers, so please use 64-bit data types in your programming language. ------ Input Format ------ - The first line of the input contains T - the number of test cases. Then the test cases follow. - Each test case contains 2 lines of input. - The first line of each test case contains three integers: N, K, and X separated by spaces. - The second line of each test case contains N space-separated positive integers, A_{1},A_{2},\ldots A_{N}. ------ Output Format ------ For each test case, output on one line the minimum cost to clear the array. ------ Constraints ------ $1 ≤ T ≤ 200$ $1 ≤ N ≤ 5000$ $0 ≤ K ≤ \lfloor \frac{N}{2} \rfloor$ $1 ≤ X ≤ 10^{9}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 5 2 7 9 10 11 12 13 5 0 7 9 9 9 9 9 5 2 7 9 1 2 3 10 ----- Sample Output 1 ------ 23 45 13 ----- explanation 1 ------ For the first test case, we can remove $A_{1}$ for cost $9$, which makes the array $[10, 11, 12, 13]$. Then we can do the third operation to remove $10$ and $13$ for cost $7$, which makes the array $[11, 12]$. We can again do the third operation to remove $11$ and $12$ for cost $7$ which clears the array. The total cost is therefore $23$, which is the minimum cost possible.
t = int(input()) for i in range(t): n, k, x = map(int, input().split()) numbers = [int(x) for x in input().split()] numbers.sort(reverse=True) minCost = 0 for j in range(k): if len(numbers) >= 2: first = numbers[0] second = numbers[1] s = first + second if x < s: minCost += x numbers.remove(first) numbers.remove(second) else: break else: break minCost += sum(numbers) print(minCost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = [int(c) for c in input().split()] days = [int(c) for c in input().split()] res = [] for i in range(len(days)): p = days[i] * a % b f1 = int(p // a) res.append(f1) print(" ".join(map(str, res)))
ASSIGN VAR 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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
def binary_search(n, a, b): l = 0 r = n maxx = n * a // b while l < r: mid = (l + r) // 2 if mid * a // b < maxx: l = mid + 1 else: r = mid return l n, a, b = map(int, input().split()) l1 = list(map(int, input().split())) a1 = [] for i in l1: ans1 = binary_search(i, a, b) a1.append(i - ans1) print(*a1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) w = list(map(int, input().split())) w = [(x * a % b // a) for x in w] print(" ".join(map(str, w)))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
arr = [] arr2 = [] scann = input() arr = scann.split() n = int(arr[0]) a = int(arr[1]) b = int(arr[2]) scann = input() arr2 = scann.split() aux = [] j = 0 k = 0 l = 0 for i in range(n): j = int(arr2[i]) * a k = j % b l = k / a aux.append(int(l)) print(*aux)
ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().strip().split(" ")) L = list(map(int, input().split(" "))) for i in range(n): s = L[i] res = s * a // b j = int(s - b * res / a) print(j)
ASSIGN VAR VAR 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 VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
def solve(w, a, b): q = list() for i in w: k = i * a % b q.append(k // a) return q n, a, b = map(int, input().split()) w = map(int, input().split()) res = map(str, solve(w, a, b)) print(" ".join(res))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR 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 VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
import sys def ints_input(): return [int(i) for i in sys.stdin.readline().strip("\n").split(" ")] def print_list(arr): sys.stdout.writelines(str(x) + " " for x in arr) sys.stdout.write("\n") def fast_input(type=str): return type(sys.stdin.readline().strip("\n")) n, a, b = ints_input() arr = ints_input() for i in arr: print(i * a % b // a, end=" ")
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR STRING
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) coins = list(map(int, input().split())) for i in range(n): coins[i] = coins[i] * a % b // a print(*coins)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) lis = list(map(int, input().split())) for i in lis: x = i * a % b print(int(x / a))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) l = list(map(int, input().split())) out = [] for i in range(n): out.append(str(l[i] * a % b // a)) print(" ".join(out))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
def main(): n, a, b = map(int, input().split()) ar = list(map(int, input().split())) ans = [0] * n for i in range(n): ans[i] = ar[i] * a % b // a print(*ans) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
class InputHandlerObject(object): inputs = [] def getInput(self, n=0): res = "" inputs = self.inputs if not inputs: inputs.extend(input().split(" ")) if n == 0: res = inputs[:] inputs[:] = [] while n > len(inputs): inputs.extend(input().split(" ")) if n > 0: res = inputs[:n] inputs[:n] = [] return res InputHandler = InputHandlerObject() g = InputHandler.getInput n, a, b = g() n, a, b = int(n), int(a), int(b) c = [int(x) for x in g()] r = [] for i in c: r.append(str(i * a % b // a)) print(" ".join(r))
CLASS_DEF VAR ASSIGN VAR LIST FUNC_DEF NUMBER ASSIGN VAR STRING ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) arr = [int(z) for z in input().split()] res = [] for i in arr: res.append(i * a % b // a) print(*res)
ASSIGN VAR 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 FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) x = [int(i) for i in input().split()] def find_index(n): l = 0 r = n while l < r: mid = (l + r) // 2 if mid * a // b == r * a // b: r = mid else: l = mid + 1 return r for i in range(n): print(x[i] - find_index(x[i]), end=" ") print()
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
def Mashmokh(arr, a, b): arr1 = [] for i in arr: if i % (a * b) == 0: arr1.append(0) else: resultado = i - b / a * (i * a // b) arr1.append(int(resultado)) arr_aux = " ".join(map(str, arr1)) return arr_aux a1, a2, a3 = input().split() n = int(a1) a = int(a2) b = int(a3) arr = list(map(int, input().strip().split())) print(Mashmokh(arr, a, b))
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) for i in input().split(): i = int(i) r = b / a x = i // r print(int(i - r * x), end=" ")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR STRING
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
t = input().split() n = int(t[0]) a = int(t[1]) b = int(t[2]) for x in input().split(): w = int(x) * a % b w //= a print(w, end=" ")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
def main(): n, a, b = map(int, input().split()) l = list(map(int, input().split())) for i, x in enumerate(l): l[i] = x * a % b // a print(" ".join(map(str, l))) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
def main(): n, a, b = list(map(int, input().split())) x_list = list(map(int, input().split())) res = list() for x in x_list: res.append(str(x * a % b // a)) print(" ".join(res)) main()
FUNC_DEF ASSIGN VAR 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 FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) arr = list(map(int, input().split())) for i in range(n): arr[i] = arr[i] * a % b // a print(arr[0], end="") for i in range(1, len(arr)): print("", arr[i], end="") print("")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR STRING
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
__author__ = "Lipen" def main(): n, a, b = map(int, input().split()) x = list(map(int, input().split())) answer = list(map(lambda i: int(x[i] - int(x[i] * a / b) / a * b), range(n))) print(" ".join(map(str, answer))) main()
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) t = [(w - 1 - (w * a // b * b - 1) // a) for w in map(int, input().split())] print(" ".join(map(str, t)))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
import sys [n, a, b] = map(int, sys.stdin.readline().split()) buttons = map(int, sys.stdin.readline().split()) res = [] for x in buttons: money = int(1.0 * x * a / b) rest = x - 1.0 * money * b / a res.append(int(rest)) print(" ".join(map(str, res)))
IMPORT ASSIGN LIST 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 LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = map(int, input().split()) x = list(map(int, input().split())) [print(el * a % b // a, end=" ") for el in x] print("\n")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
import sys n, a, b = map(int, sys.stdin.readline().split()) w_arr = map(int, sys.stdin.readline().split()) print(" ".join(map(str, [(w * a % b // a) for w in w_arr])))
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
[n, a, b] = [int(x) for x in input().split()] amount = [int(x) for x in input().split()] res = [0] * n curr = 0 for i, day in enumerate(amount): s = int(day * a / b) tmp = int(s * b / a) if s * b % a == 0 else int(s * b / a) + 1 res[i] = day + curr - tmp print(" ".join(map(str, res)))
ASSIGN LIST VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
n, a, b = input().split(" ") n, a, b = int(n), int(a), int(b) INPUT = [int(x) for x in input().split(" ")] for w in INPUT: res = w * a % b res //= a print(res, end=" ")
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives back w tokens then he'll get $\lfloor \frac{w \cdot a}{b} \rfloor$ dollars. Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He has n numbers x_1, x_2, ..., x_{n}. Number x_{i} is the number of tokens given to each worker on the i-th day. Help him calculate for each of n days the number of tokens he can save. -----Input----- The first line of input contains three space-separated integers n, a, b (1 ≤ n ≤ 10^5; 1 ≤ a, b ≤ 10^9). The second line of input contains n space-separated integers x_1, x_2, ..., x_{n} (1 ≤ x_{i} ≤ 10^9). -----Output----- Output n space-separated integers. The i-th of them is the number of tokens Mashmokh can save on the i-th day. -----Examples----- Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0
import sys f = sys.stdin.readline n, a, b = map(int, f().strip().split()) x = [int(v) for v in f().strip().split()] out = [] for v in x: out.append(v * a % b // a) print(" ".join(map(str, out)))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
I = lambda: list(map(int, input().split())) (n,) = I() l = I() ans = [] for i in range(n): for j in range(i + 1, n): if l[i] > l[j]: ans.append([l[i], i + 1, j + 1]) ans.sort() ans.sort(reverse=True, key=lambda x: x[2]) print(len(ans)) for i in ans: print(i[1], i[2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
from sys import stdin, stdout def inversion_swapsort(n, a): I = [] for i in range(n): for j in range(i + 1, n): if a[i] > a[j]: I.append([a[i], i + 1, j + 1]) I.sort() I.sort(key=lambda x: x[2], reverse=True) print(len(I)) if len(I) > 0: for i in I: j = i[1:] print(*j) return n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) inversion_swapsort(n, a)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN 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 VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) arr = list(map(int, input().split())) x = [] for i in range(n): x.append([arr[i], i]) x.sort(reverse=True) res = [] for v, pos in x: for i in range(pos): if arr[i] > v: res.append(str(i + 1) + " " + str(pos + 1)) print(len(res)) print("\n".join(res))
ASSIGN VAR FUNC_CALL VAR 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 VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
inf = int(1000000.0) n = int(input()) lis = list(map(int, input().split())) store = [] index = list(range(0, n)) for k in range(1, n): for _ in range(k): for i in range(1, k + 1): if lis[i] < lis[i - 1]: lis[i], lis[i - 1] = lis[i - 1], lis[i] index[i], index[i - 1] = index[i - 1], index[i] store.append((index[i] + 1, index[i - 1] + 1)) store.reverse() print(len(store)) for i in store: print(*i)
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) s = list(map(int, input().split())) out = [] for i in range(n - 1): temp = s[i] l = [] for j in range(i + 1, n): if s[j] < temp: l.append([s[j], j]) l.sort() l.reverse() for ii in l: out.append([i, ii[1]]) print(len(out)) for i in out: print(i[0] + 1, i[1] + 1)
ASSIGN VAR FUNC_CALL VAR 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 BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) li = list(map(int, input().split())) lp = li.copy() j = 1 lf = [] c = max(lp) while j <= n: a = min(lp) i = lp.index(a) li[i] = j lp[i] = c + j j += 1 while n > 1: a = li.pop() while a < n: i = li.index(a + 1) li[i] = a lf += [f"{i + 1} {n}"] a = a + 1 n -= 1 print(len(lf)) for v in lf: print(v)
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 ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR LIST BIN_OP VAR NUMBER STRING VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + "\n") def wi(n): sys.stdout.write(str(n) + "\n") def wia(a): sys.stdout.write(" ".join([str(x) for x in a]) + "\n") def solve(n, a): b = [i for i in range(n)] b = sorted(b, key=lambda x: (a[x], x)) res = [] for it in range(n): for i in range(n - 1): if b[i + 1] < b[i]: b[i], b[i + 1] = b[i + 1], b[i] res.append([b[i] + 1, b[i + 1] + 1]) wi(len(res)) for i in range(len(res)): wia(res[i]) def main(): n = ri() a = ria() solve(n, a) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
import sys inpy = [int(x) for x in sys.stdin.read().split()] n = inpy[0] arr = inpy[1:] res = [] for i in range(n - 1, -1, -1): x = [] for j in range(i): if arr[j] > arr[i]: x.append((arr[j], j)) x.sort() for a, j in x: res.append((j + 1, i + 1)) print(len(res)) for i, j in res: print(i, j)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) arr = list(map(int, input().split())) ans = [] for i in range(n): for j in range(n - 1, i, -1): if arr[i] > arr[j]: ans.append((arr[i], i + 1, j + 1)) ans = sorted(ans, key=lambda x: x[0]) ans = sorted(ans, reverse=True, key=lambda x: x[2]) print(len(ans)) for _, u, v in ans: print(u, v)
ASSIGN VAR FUNC_CALL VAR 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 VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) a = [int(i) for i in input().split()] ans = 0 dc = [[] for i in range(n)] for i in range(n): for j in range(i): if a[i] < a[j]: dc[i].append((a[j], j)) ans += 1 print(ans) for i in range(n - 1, -1, -1): dc[i].sort() for j in dc[i]: print(j[1] + 1, i + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
it = lambda: list(map(int, input().strip().split())) def solve(): N = int(input()) A = it() inversions = [] for i in range(N): inversion = [] for j in range(i): if A[j] > A[i]: inversion.append(j) inversions.append(inversion) R = [] B = [[a, i] for i, a in enumerate(A)] for i in range(N - 1, -1, -1): inversion = inversions[i] inversion.sort(key=lambda x: B[x]) for j in inversion: R.append([j + 1, i + 1]) B[i], B[j] = B[j], B[i] print(len(R)) for i, j in R: print(i, j) solve()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
def swapsort(k, a): array_zip = list(zip(a, range(k))) array_zip.sort() pos = [(0) for i in range(k)] for i in range(k): a[array_zip[i][1]] = i pos[i] = array_zip[i][1] res = [] end = False while not end: end = True for i in range(k - 1): if pos[i] > pos[i + 1]: res.append((pos[i + 1] + 1, pos[i] + 1)) pos[i], pos[i + 1] = pos[i + 1], pos[i] end = False print(len(res)) for t in res: print(*t) k = int(input()) a = [int(s) for s in input().split()] swapsort(k, a)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR 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 VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
input() a = list(map(int, input().split())) b = sorted((i, -a[j], -j) for j in range(len(a)) for i in range(j) if a[i] > a[j]) print(len(b)) for i, j, k in b: print(i + 1, -k + 1)
EXPR 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 VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
from sys import stdin, stdout def main(): n = int(stdin.readline()) ary = list(map(int, stdin.readline().split())) invs = [[(False) for i in range(n)] for j in range(n)] pair = [] for i in range(n): pair.append((ary[i], i)) pair.sort() perm = [-1] * n fxa = [-1] * n posof = [-1] * n for i in range(n): perm[pair[i][1]] = i fxa[i] = pair[i][0] posof[i] = pair[i][1] del pair answers = [] for i in range(n - 1, -1, -1): for j in range(perm[i] + 1, n): if posof[j] < posof[perm[i]]: answers.append(" ".join(map(str, (posof[j] + 1, i + 1)))) stdout.write(str(len(answers)) + "\n") stdout.write("\n".join(answers)) return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN EXPR FUNC_CALL VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) a = list(map(int, input().split())) cc = [] for i, u in enumerate(a): tl = [] for j in range(n - 1, i, -1): if a[j] < u: tl.append((i, j)) cc.append((u, tl)) res = [] for _, tl in sorted(cc): for u, v in tl: res.append((u, v)) print(len(res)) for u, v in res: print(u + 1, v + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) a = list(map(int, input().split())) b = sorted([(a[i], i) for i in range(n)]) res = [] for _ in range(n): for i in range(n - 1): if b[i][1] > b[i + 1][1]: res.append((b[i + 1][1], b[i][1])) b[i], b[i + 1] = b[i + 1], b[i] print(len(res)) for i, j in res: print(i + 1, j + 1)
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 VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
import sys def input(): return sys.stdin.readline().rstrip() def split_input(): return [int(i) for i in input().split()] def allpos(a): n = len(a) m = len(a[0]) def onBoard(i, j): return i >= 0 and j >= 0 and i < n and j < m dirs = [(1, 0), (0, 1), (-1, 0), (0, -1)] for i in range(n): for j in range(m): for d in dirs: newi = i + d[0] newj = j + d[1] if onBoard(newi, newj): pass def isSorted(a): for i in range(1, len(a)): if a[i] < a[i - 1]: return False return True tests = 1 for _ in range(tests): n = int(input()) a = split_input() d = {} for i in range(len(a)): if a[i] not in d: d[a[i]] = 1 else: num = d[a[i]] * 0.001 d[a[i]] += 1 a[i] += num ops = [] for i in range(n - 1, -1, -1): l = [] for j in range(i): if a[j] > a[i]: l.append((a[j], j)) l.sort() for elem in l: ops.append("%d %d" % (elem[1] + 1, i + 1)) a[elem[1]], a[i] = a[i], a[elem[1]] if isSorted(a): print(len(ops)) print(*ops, sep="\n") else: print(-1)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) a = list(map(int, input().split())) idx = sorted(range(n), key=lambda i: a[i]) ans = [[] for _ in range(n)] cnt = 0 for i in range(n): for j in range(n - 1, i, -1): if a[i] > a[j]: ans[i].append(str(i + 1) + " " + str(j + 1)) cnt += 1 print(cnt) for i in idx: if len(ans[i]) == 0: continue print("\n".join(ans[i]))
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 VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) (n,) = I() l = I() iv = 0 an = [] pr = [0] * n ix = [0] * n for i in range(n): c = 0 for j in range(n): if j < i and l[j] == l[i]: c += 1 if l[j] < l[i]: c += 1 pr[i] = c for i in range(n): ix[pr[i]] = i for i in range(n): while pr[i] > i: iv += 1 cr = pr[i] - 1 an.append([i + 1, ix[cr] + 1]) d = ix[cr] pr[i], pr[ix[cr]] = pr[ix[cr]], pr[i] ix[cr + 1] = d ix[pr[i]] = i print(iv) for i in an: print(*i)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) a = list(map(int, input().split())) ans = [] def f(l): return -l[1], l[2], l[0] for i in range(n): for j in range(i + 1, n): if a[i] > a[j]: ans.append((i + 1, j + 1, a[i])) ans.sort(key=f) print(len(ans)) for i in ans: print(*i[:2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF RETURN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) lst = [int(j) for j in input().split()] inversions = list() for i in range(1, n): for j in range(i): if lst[i] < lst[j]: inversions.append((i, -lst[j], -j)) inversions.sort(reverse=True) res = list(range(len(inversions))) if res: print(len(res)) for elem in res: v, _, u = inversions[elem] u = -u print(u + 1, v + 1) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) a = list(map(int, input().split())) aWithIndex = [] for i in range(n): aWithIndex.append((a[i], i)) aWithIndex.sort(key=lambda x: x[0]) aOrder = [-1] * n for i in range(n): aOrder[aWithIndex[i][1]] = i aOrderInverse = [-1] * n for i in range(n): aOrderInverse[aOrder[i]] = i result = [] for i in range(n): for j in range(aOrder[i] - 1, i - 1, -1): result.append(str(i + 1) + " " + str(aOrderInverse[j] + 1)) tmp = aOrder[i] aOrder[i] = aOrder[aOrderInverse[j]] aOrder[aOrderInverse[j]] = tmp for j in range(n): aOrderInverse[aOrder[j]] = j print(len(result)) if len(result) != 0: print("\n".join(result))
ASSIGN VAR FUNC_CALL VAR 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 VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def issorted(a): for i in range(1, len(a)): if a[i - 1] > a[i]: return False return True def stupid(a, inv, was, order): if len(order) == len(inv) and issorted(a): return order for i in range(len(inv)): if was[i] == 0: was[i] = 1 order.append(i) x, _, y = inv[i] y = -y a[x], a[y] = a[y], a[x] r = stupid(a, inv, was, order) a[x], a[y] = a[y], a[x] was[i] = 0 if r is not None: return r order.pop() return None n = mint() a = list(mints()) inv = [] for i in range(1, n): for j in range(i): if a[i] < a[j]: inv.append((i, -a[j], -j)) inv.sort(reverse=True) r = list(range(len(inv))) if r is not None: print(len(r)) for z in r: v, _, u = inv[z] u = -u print(u + 1, v + 1) else: print("wut")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) s = [int(x) for x in input().split()] kk = [] for i in range(n): h = [] kk.append(h) for i in range(0, len(s)): for j in range(i + 1, len(s)): if s[i] > s[j]: kk[j].append((s[i], i)) for i in range(0, n): kk[i].sort() fnl = [] for i in range(n - 1, -1, -1): for j in range(0, len(kk[i])): fnl.append((i, kk[i][j][1])) print(len(fnl)) for i in range(0, len(fnl)): print(fnl[i][1] + 1, fnl[i][0] + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
n = int(input()) arr = list(map(int, input().split())) ans = [] for i in range(n): ans.append((arr[i], i)) ans.sort() ans = ans[-1::-1] p = [] for i in ans: for j in range(i[1]): if i[0] < arr[j]: p.append((j + 1, i[1] + 1)) print(len(p)) for i in p: print(i[0], i[1])
ASSIGN VAR FUNC_CALL VAR 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 VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if: $1 \le u < v \le n$. $a_u > a_v$. Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$. Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions: all the pairs in the list are distinct and form an inversion in $a$. all the pairs that form an inversion in $a$ are in the list. Starting from the given array, if you swap the values at indices $u_1$ and $v_1$, then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 1000$) — the length of the array. Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$  — elements of the array. -----Output----- Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ($0 \le m \le \dfrac{n(n-1)}{2}$) — number of pairs in the list. The $i$-th of the following $m$ lines should contain two integers $u_i, v_i$ ($1 \le u_i < v_i\le n$). If there are multiple possible answers, you may find any of them. -----Examples----- Input 3 3 1 2 Output 2 1 3 1 2 Input 4 1 8 1 6 Output 2 2 4 2 3 Input 5 1 1 1 2 2 Output 0 -----Note----- In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$. In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$. In the third sample test case the array is already sorted.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def issorted(a): for i in range(1, len(a)): if a[i - 1] > a[i]: return False return True n = mint() a = list(mints()) inv = [] for i in range(1, n): for j in range(i): if a[i] < a[j]: inv.append((i, -a[j], -j)) inv.sort(reverse=True) r = list(range(len(inv))) if r is not None: print(len(r)) for z in r: v, _, u = inv[z] u = -u print(u + 1, v + 1) else: print("wut")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
from sys import stdin, stdout def fn(days): sm1 = 0 for i in range(n): if a[i] - i // days <= 0: break sm1 += a[i] - i // days return sm1 for _ in range(1): n, k = list(map(int, stdin.readline().split())) a = list(map(int, stdin.readline().split())) a.sort(reverse=True) ans = float("inf") l, r = 1, n ans = float("inf") while l <= r: mid = l + r >> 1 if fn(mid) >= k: r = mid - 1 else: l = mid + 1 if l <= n: ans = min(ans, l) print(ans if ans != float("inf") else -1)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER 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 NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = list(map(int, input().split())) l = list(map(int, input().split())) l.sort(reverse=True) left = 1 right = n def check(k, m): if m > 0: lef = 0 z = 0 for i in range(n): m -= max(0, l[i] - lef) z += 1 if z == k: z = 0 lef += 1 if m <= 0: return 1 else: return 0 res = [] while left <= right: mid = int((left + right) / 2) if check(mid, m): res.append(mid) right = mid - 1 else: left = mid + 1 if len(res) > 0: print(min(res)) else: print("-1")
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def calc(ans, k, n): for i in range(1, len(ans)): if ans[i] >= n + k * ((i - k) // k * ((i - k) // k + 1)) // 2 + (i - k) % k * ( (i - k) // k + 1 ): return True return False def bins(ans, n, low, high): if low == high and calc(ans, low, n): return low elif low == high: return -1 if calc(ans, (low + high) // 2, n): return bins(ans, n, low, (low + high) // 2) else: return bins(ans, n, (low + high) // 2 + 1, high) n, k = list(map(int, input().split())) a = list(map(int, input().split())) a.sort(reverse=True) ans = [0] * (n + 1) for i in range(1, n + 1): ans[i] = ans[i - 1] + a[i - 1] print(bins(ans, k, 1, n))
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER 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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
class Solution: def __init__(self): firstline = input().split() n = int(firstline[0]) m = int(firstline[1]) a = input().split() for i in range(n): a[i] = int(a[i]) a.sort(key=int, reverse=True) l = 1 r = m + 1 while l < r: mid = int(l + r >> 1) sum = 0 for i in range(n): if a[i] - int(i / mid) <= 0: break sum += max(0, a[i] - int(i / mid)) if sum >= m: break if sum >= m: r = mid else: l = mid + 1 if r == m + 1: print("-1") else: print(r) Solution()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = [int(x) for x in input().split()] l = [int(x) for x in input().split()] summ = sum(l) result = 0 def find(x): s = 0 pos_count = 0 day_count = 0 for i in l: s += max(0, i - pos_count) day_count += 1 if day_count == x: day_count = 0 pos_count += 1 if s < m: return 0 else: return x def binary_search(lf, r): global result mid = int((lf + r) / 2) rs = find(mid) if lf == r: result = rs elif lf == r - 1: if rs: result = rs else: binary_search(r, r) elif rs: binary_search(lf, mid) else: binary_search(mid, r) if summ < m: print("-1") elif summ == m: print(n) else: l = sorted(l, reverse=True) binary_search(1, n) print(result)
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 ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def stack(days, count): x = count // days y = count % days return days * x * (x - 1) // 2 + y * x def func(arr, days): total = 0 for x in range(len(arr)): if arr[x] <= x // days: break else: total += arr[x] - x // days return total def main(): arr = input().split() n = int(arr[0]) pages = int(arr[1]) arr = input().split() for x in range(n): arr[x] = int(arr[x]) arr.sort(reverse=True) lower = 1 upper = 4 * 10**5 while upper - lower > 1: mid = (upper + lower) // 2 total = func(arr, mid) if total >= pages: upper = mid else: lower = mid if func(arr, lower) >= pages: return lower elif func(arr, upper) >= pages: return upper else: return -1 print(main())
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def check(num): arr = [[] for i in range(num)] j, count = 0, 0 for i in range(n): arr[j].append(max(0, c[i] - len(arr[j]))) count += arr[j][-1] j = (j + 1) % num if count >= m: return True return False n, m = map(int, input().split()) c = list(map(int, input().split())) c.sort(reverse=True) if sum(c) < m: print(-1) exit() low = 1 high = n while low < high: mid = (low + high) // 2 if check(mid): high = mid - 1 else: low = mid + 1 if check(low): print(low) else: print(low + 1)
FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP 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 EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR 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 BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def check(k): s = 0 for i in range(n): s += max(0, a[i] - i // k) return s >= m n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) low = 1 high = n + 1 while low <= high: mid = (low + high) // 2 if check(mid): high = mid - 1 else: low = mid + 1 if low <= n: print(low) else: print(-1)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR RETURN 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP 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 BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def check(arr, d, mm): ss = 0 k = -1 for i in range(n): if i % d == 0: k += 1 ss += max(0, arr[i] - k) if ss >= mm: return True else: return False n, m = map(int, input().split()) arr = list(map(int, input().split())) arr.sort(reverse=True) s = 1 e = n ans = 1000000000000000000000 while s <= e: mid = (s + e) // 2 if check(arr, mid, m): ans = min(ans, mid) e = mid - 1 else: s = mid + 1 if ans == 1000000000000000000000: ans = -1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR 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 EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def func(mid): s = 0 c = 0 v = 0 for i in range(n): c += 1 s += max(a[i] - v, 0) if c == mid: v += 1 c = 0 return s >= m n, m = map(int, input().split()) a = [int(x) for x in input().split()] a.sort(reverse=True) an = 0 if sum(a) < m: an = -1 up = n do = 1 while up >= do: mid = (up + do) // 2 if func(mid): an = mid up = mid - 1 else: do = mid + 1 print(an)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR 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 NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER 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 ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def check(sz): aa = 0 for i in range(n): aa += max(0, a[i] - i // sz) if aa >= M: return True return False n, M = map(int, input().split()) a = list(map(int, input().split())) if sum(a) < M: print(-1) exit() a.sort(reverse=True) l = 1 r = n while l < r: m = l + r m >>= 1 if check(m): r = m else: l = m + 1 print(l)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR 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 IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
from sys import exit, setrecursionlimit setrecursionlimit(10**6) inn = lambda: input().strip() mapp = lambda: map(int, inn().split(" ")) N, work = mapp() coffee = list(mapp()) coffee.sort(reverse=True) if sum(coffee) < work: print(-1) exit() def getMaxPower(day): if day == 0: return False maxPower = 0 for i, a in enumerate(coffee): add = a - i // day if add <= 0: break maxPower += add return maxPower lo, hi = -1, 1000000001 for a in range(36): mid = (lo + hi) // 2 if getMaxPower(mid) >= work: hi = mid else: lo = mid print(hi)
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
import sys input = sys.stdin.readline def fun(x): su = 0 global ar, n for i in range(n): su += max(0, ar[i] - i // x) return bool(su >= m) n, m = map(int, input().split()) ar = list(map(int, input().split())) ar.sort(reverse=True) if sum(ar) < m: print(-1) else: l = 1 r = n while l <= r: mid = (l + r) // 2 if fun(mid): r = mid - 1 else: l = mid + 1 print(l)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR 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 BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
[n, m] = list(map(int, input().split())) arr = list(map(int, input().split())) arr.sort(reverse=True) def check(i): mins = 0 cnt = 0 sums = 0 for x in arr: sums += max(x - mins, 0) if sums >= m: return True cnt += 1 if cnt >= i: cnt = 0 mins += 1 return False l = 1 r = n + 1 while l != r: if check((l + r) // 2): r = (l + r) // 2 else: l = (l + r) // 2 + 1 print(l if l > 0 and l <= n else -1)
ASSIGN LIST 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 NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
import sys n, m = map(int, input().split()) l = list(map(int, input().split())) l.sort(reverse=True) left = 1 right = n + 1 ans = 10**18 + 1 while left != right: mid = (left + right) // 2 din = 0 kaam = 0 c = 0 for i in l: if i - din <= 0: break kaam += i - din c += 1 if c == mid: c = 0 din += 1 if kaam >= m: ans = min(ans, mid) right = mid else: left = mid + 1 if ans == 10**18 + 1: print(-1) else: print(ans)
IMPORT 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = list(map(int, input().split())) a = list(sorted(map(int, input().split()), reverse=True)) def proc(n, m, a): possible_minimum = -1 l, r = 1, n sub_c = [0] * n while l <= r: i = (l + r) // 2 current_m = m for j in range(i): sub_c[j] = 0 for j in range(0, n): pos = j % i if a[j] < sub_c[pos]: break current_m -= a[j] - sub_c[pos] sub_c[pos] += 1 if current_m <= 0: break if current_m <= 0: possible_minimum = i r = i - 1 else: l = i + 1 return possible_minimum print(proc(n, m, a))
ASSIGN VAR 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 FUNC_CALL FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def sum1(day, n, lst): std = n // day sumi = 0 sub = -1 for i in range(n): if i % day == 0: sub += 1 sumi += max(0, lst[i] - sub) return sumi def binary(n, m, arr): l = 1 r = n ans = -1 while l <= r: mid = (l + r) // 2 pages = sum1(mid, n, arr) if pages >= m: r = mid - 1 ans = mid elif pages < m: l = mid + 1 return ans n, m = map(int, input().strip().split()) cafe = list(map(int, input().strip().split())) cafe.sort(reverse=True) print(binary(n, m, cafe))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN 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 BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = map(int, input().split()) A = list(map(int, input().split())) A.sort() A.reverse() def func(i): ans = 0 for j in range(0, n): ans += max(A[j] - j // i, 0) return ans >= m l = 1 r = n while r - l > 1: s = l + r >> 1 if func(s) == 1: r = s else: l = s if func(l) == 1: print(l) elif func(r) == 1: print(r) else: print("-1")
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 EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n_cups, target = [int(i) for i in input().split()] cups = [int(i) for i in input().split()] cups = sorted(cups, reverse=True) found = False if sum(cups) < target: print(-1) found = True def find_value(c, days): val = 0 for i in range(len(c)): val += max(c[i] - i // days, 0) return val low = 1 high = n_cups while not found: current = (low + high) // 2 val = find_value(cups, current) if low == current or high == current: if val >= target: print(current) found = True break else: print(current + 1) found = True break if val < target: low = current elif val > target: high = current else: print(current) found = True break
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 NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def check(mid): tp = su = s = 0 for i in range(n): su += max(0, lis[i] - s) tp += 1 if tp == mid: s += 1 tp = 0 return su >= m n, m = map(int, input().split()) lis = sorted(map(int, input().split()), reverse=True) if sum(lis) < m: print("-1") elif sum(lis) == m: print(len(lis)) else: l = 1 r = n while l <= r: mid = l + r >> 1 if check(mid): r = mid - 1 ans = mid else: l = mid + 1 print(ans)
FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN 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 NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR 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 VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def solveFor(m): sm = 0 for i in range(m): ct = 0 for j in range(i, n, m): if arr[j] > ct: sm += arr[j] - ct else: break ct += 1 return sm n, k = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() arr = arr[::-1] begin = 1 end = n ans = -1 while begin <= end: mid = (begin + end) // 2 value = solveFor(mid) if value == k: ans = mid break elif value > k: ans = mid end = mid - 1 else: begin = mid + 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = list(map(int, input().split())) l = list(map(int, input().split())) l.sort(reverse=True) ans = [] def bin(s): tmp = [[] for k in range(s)] cur = s tr = 0 for i in range(n): if cur == s: cur = 0 tmp[cur].append(l[i]) cur += 1 for i in range(s): for k in range(len(tmp[i])): tr += max(tmp[i][k] - k, 0) if tr >= m: return 1 else: return 0 uk2 = n uk1 = 1 while uk2 - uk1 > 1: if bin((uk2 + uk1) // 2): uk2 = (uk2 + uk1) // 2 else: uk1 = (uk2 + uk1) // 2 if bin(uk1): print(uk1) elif bin(uk2): print(uk2) else: print(-1)
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 NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
_ = input() n, m = _.split() n, m = int(n), int(m) _ = input() _a = _.split() b = [] num_list = [] _counter = {} _sum = 0 for i in _a: value = int(i) _sum = _sum + value b.append(value) if value in _counter: _counter[value] = _counter[value] + 1 else: _counter[value] = 1 num_list.append(value) if m > _sum: print("-1") else: num_list.sort() num_list.reverse() counter = [] sum_list = [] whole_sum_list = [] _ = 0 __ = 0 for num in num_list: num_count = _counter[num] counter.append(num_count) _ = _ + num_count __ = __ + num_count * num sum_list.append(_) whole_sum_list.append(__) def avail(k): parse = len(num_list) - 1 for i in range(len(num_list)): if sum_list[i] // k >= num_list[i]: parse = i break parse = parse - 1 if parse > 0 else 0 floor = sum_list[parse] // k - 1 token = sum_list[parse] % k whole_sum = ( whole_sum_list[parse] - k * int(floor * (floor + 1) // 2) - token * (floor + 1) ) if whole_sum >= m: return True if len(num_list) == 1: return False floor = floor + 1 _value = num_list[parse + 1] for i in range(counter[parse + 1]): if _value <= floor: break if whole_sum >= m: return True whole_sum = whole_sum + _value - floor token = token + 1 if token >= k: floor, token = floor + 1, 0 if whole_sum >= m: return True return False def _avail(k): token = 0 floor = 0 cafe = 0 available = False for value, count in zip(num_list, counter): if cafe - floor * token >= m: available = True if floor >= value: break cafe = cafe + value * count token = token + count for i in range(0, token // k): cafe = cafe - (floor + i) * k floor = floor + token // k token = token % k if cafe - floor * token >= m: available = True return available start = 1 end = n while start != end: k = (start + end) // 2 if avail(k): start, end = start, k else: start, end = k + 1, end if n == 1: print(1) else: print(start)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = map(int, input().split()) a = sorted(list(map(int, input().split())), reverse=True) def solve(days): pages = 0 for i in range(n): pages += max(0, a[i] - i // days) return pages >= m res = -1 lo = 1 hi = n + 1 while lo < hi: mid = lo + (hi - lo) // 2 if solve(mid): hi = mid else: lo = mid + 1 print(lo if lo <= n else -1)
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 NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def check(d, a): ans = 0 for q in range(len(a)): ans += max(a[q] - q // d, 0) return ans n, m = map(int, input().split()) a = sorted(list(map(int, input().split())), reverse=True) if sum(a) < m: print(-1) else: l, r = 0, n while r - l > 1: m1 = (l + r) // 2 if check(m1, a) >= m: r = m1 else: l = m1 print(r)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR 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 NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR 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
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort(reverse=True) def check(d): s = 0 for i in range(len(a)): s += max(0, a[i] - i // d) return s >= m if sum(a) < m: print(-1) else: l, r = 1, n mid = l + r >> 1 while l < r: if check(mid): r = mid else: l = mid + 1 mid = l + r >> 1 print(l)
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 NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] a.sort(reverse=True) if sum(a) < m: print(-1) else: l = 1 r = n while l < r: mid = l + r >> 1 s = 0 for j in range(n): s += max(0, a[j] - j // mid) if s >= m: r = mid else: l = mid + 1 else: print(l)
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 NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN 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 IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def isvalid(d, li, m): if d == 0: return 0 n = len(li) sumi = 0 for i in range(n): sumi += max(0, li[i] - i // d) if sumi >= m: return 1 return 0 def binary_search(li, left, right, m): if right < left: return 0 mid = (left + right) // 2 if isvalid(mid, li, m): if isvalid(mid - 1, li, m) == 0: return mid return binary_search(li, left, mid - 1, m) return binary_search(li, mid + 1, right, m) l = input().split() n = int(l[0]) m = int(l[1]) l = input().split() li = [int(i) for i in l] li.sort() li.reverse() if sum(li) < m: print(-1) else: print(binary_search(li, 1, 2 * 10**5 + 1, m))
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
import sys n, m = list(map(int, input().split())) A = list(map(int, input().split())) if sum(A) < m: print(-1) return A.sort(reverse=True) MIN = 1 MAX = n while MIN != MAX: d = (MIN + MAX) // 2 ANS = 0 j = 0 count = 0 for a in A: if a <= j: continue ANS += a - j count += 1 if count == d: j += 1 count = 0 if ANS >= m: MAX = d else: MIN = d + 1 print(MIN)
IMPORT 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 IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def search_binary(cups, pages, doses): if sum(doses) < pages: return -1 elif sum(doses) == pages: return len(doses) else: doses = sorted(doses, reverse=True) new_doses = [] new_doses = doses[0:pages] max_days = len(new_doses) min_days = 0 while max_days - min_days > 1: days = (max_days + min_days) // 2 test = count_coffee(days, pages, doses) if test == True: max_days = days elif test == False: min_days = days return max_days def count_coffee(days, pages, doses): new_doses = [] rest_doses = [] new_doses = doses[:days] rest_doses = doses[days:] pages -= sum(new_doses) n = 1 j = 0 while pages > 0: for i in range(len(new_doses)): if j >= len(rest_doses) and pages > 0: return False elif j >= len(rest_doses): break elif rest_doses[j] - n <= 0 and pages <= 0: break if pages < 0: break new_doses[i] += rest_doses[j] - n pages -= rest_doses[j] - n j += 1 n += 1 return True cups, pages = map(int, input().split()) doses = list(map(int, input().split())) print(search_binary(cups, pages, doses))
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
def check_possibility(days, m, coffee): sum_coffee = 0 for i, cup in enumerate(coffee): tax = i // days if sum_coffee >= m or tax >= cup: break sum_coffee += cup - tax return sum_coffee >= m n, m = list(map(int, input().split())) coffee = sorted(map(int, input().split()), reverse=True) bad = 0 good = len(coffee) while good - bad > 1: days = (bad + good) // 2 if check_possibility(days, m, coffee): good = days else: bad = days possible = check_possibility(good, m, coffee) print(good if possible else -1)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
import sys input = sys.stdin.readline def ceil_div(x, y): return -(-x // y) def can(a, k, m): if len(a) <= k: return sum(a) >= m else: sum_ = 0 minus = 0 for i in range(len(a)): if i % k == 0 and i != 0: minus += 1 sum_ += max(0, a[i] - minus) return sum_ >= m def main(): n, m = [int(x) for x in input().split(" ")] a = [int(x) for x in input().split(" ")] a = sorted(a, reverse=True) sum_a = sum(a) if sum_a < m: print(-1) elif sum_a == m: print(len(a)) else: pos = 0 for i in range(31, -1, -1): jump = 2**i if not can(a, pos + jump, m): pos += jump print(pos + 1) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $m$ pages. Polycarp also has $n$ cups of coffee. The coffee in the $i$-th cup Polycarp has $a_i$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least). Let's consider some day of Polycarp's work. Consider Polycarp drinks $k$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $a_{i_1}, a_{i_2}, \dots, a_{i_k}$. Then the first cup he drinks gives him energy to write $a_{i_1}$ pages of coursework, the second cup gives him energy to write $max(0, a_{i_2} - 1)$ pages, the third cup gives him energy to write $max(0, a_{i_3} - 2)$ pages, ..., the $k$-th cup gives him energy to write $max(0, a_{i_k} - k + 1)$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the caffeine dosage of coffee in the $i$-th cup. -----Output----- If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. -----Examples----- Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 -----Note----- In the first example Polycarp can drink fourth cup during first day (and write $1$ page), first and second cups during second day (and write $2 + (3 - 1) = 4$ pages), fifth cup during the third day (and write $2$ pages) and third cup during the fourth day (and write $1$ page) so the answer is $4$. It is obvious that there is no way to write the coursework in three or less days. In the second example Polycarp can drink third, fourth and second cups during first day (and write $4 + (2 - 1) + (3 - 2) = 6$ pages) and sixth cup during second day (and write $4$ pages) so the answer is $2$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$ pages of coursework and during second day he will write $5$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
n, m = list(map(int, input().split())) a = list(map(int, input().split())) ans = 1 su = sum(a) a.sort(reverse=True) s = [0] * n s[0] = a[0] for i in range(1, n): s[i] = s[i - 1] + a[i] def pos(d): i = d while i <= n: k = i // d neg = k * i - k * (k + 1) // 2 * d if s[i - 1] - neg >= m: return True i += 1 return False if su < m: ans = -1 print(ans) else: first = 0 last = n while first < last - 1: midpoint = (first + last) // 2 if pos(midpoint): last = midpoint else: first = midpoint print(last)
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 ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR