description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = input() (*a,) = map(int, input().split()) print(sum(a[i] - a[i + 1] for i in range(len(a) - 1) if a[i] > a[i + 1]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) arr = list(map(int, input().split())) s = 0 a = arr[0] for i in range(1, n): b = arr[i] if a > b: s += a - b a = b print(s)
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 ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) nums = [int(x) for x in input().split()] moves = 0 startRange = nums[0] prev = nums[0] for idx, num in enumerate(nums): if num <= prev: prev = num elif prev < startRange: moves += min(startRange, num) - prev prev = min(startRange, num) if num > startRange: startRange = num prev = num moves += startRange - prev print(moves)
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 VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
def mi(): return map(int, input().split()) n = int(input()) a = list(mi()) print(sum([max(0, a[i - 1] - a[i]) for i in range(1, n)]))
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
def solve(arr, n): inc = 0 ans = 0 for i in range(1, n): arr[i] += inc if arr[i] >= arr[i - 1]: continue diff = arr[i - 1] - arr[i] ans += diff inc += diff arr[i] += diff return ans def read(): n = int(input()) arr = list(map(int, input().strip().split())) ans = solve(arr, n) print(ans) read()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) l = list(map(int, input().split())) ans = 0 for i in range(len(l) - 1): if l[i] > l[i + 1]: ans += l[i] - l[i + 1] print(ans)
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 FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) arr = list(map(int, input().strip().split())) a = 0 ans = 0 for i in arr: if i < a: ans = ans + a - i a = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
def readln(): return tuple(map(int, input().split())) (n,) = readln() a = list(readln()) a.append(1 << 30) st = [] ans = 0 for v in a: if st == [] or st[-1] > v: st.append(v) elif st[-1] < v: while len(st) > 1 and st[-1] < v: ans += min(v, st[-2]) - st[-1] st.pop() if st[-1] > v: st.append(v) elif st[-1] < v: st = [v] print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR LIST VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
import sys ml, mi = [], [] def recurseml(l): if len(l) == 0: return if len(l) == 1: ml.append(l[0]) mi.append(0) return mx = max(l) mxi = len(l) - 1 - l[::-1].index(mx) ml.append(mx) mi.append(mxi) recurseml(l[:mxi]) n = int(input()) a = [int(i) for i in input().split()] res = 0 for i in range(1, len(a)): if a[i] < a[i - 1]: res += a[i - 1] - a[i] print(res)
IMPORT ASSIGN VAR VAR LIST LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n, t = int(input()), list(map(int, input().split())) print(sum(t[i - 1] - t[i] for i in range(1, n) if t[i] < t[i - 1]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) a = list(map(int, input().split())) prev_max = a[0] add, ans = 0, 0 for i in range(1, n): a[i] += add if a[i] < prev_max: add += prev_max - a[i] ans += prev_max - a[i] prev_max = max(prev_max, a[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) a = list(map(int, input().rstrip().split())) carry = 0 oper = 0 for i in range(1, n): a[i] = a[i] + carry if a[i] < a[i - 1]: carry += abs(a[i - 1] - a[i]) oper += abs(a[i - 1] - a[i]) a[i] = a[i - 1] print(oper)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) lst = list(map(int, input().split(" "))) print(str(sum(max(0, lst[i - 1] - lst[i]) for i in range(1, n))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) p = [int(x) for x in input().split()] if n == 1: print(0) else: q = [0] * (n - 1) for i in range(0, n - 1): q[i] = p[i + 1] - p[i] s = 0 for i in range(0, n - 1): if q[i] < 0: s += -q[i] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER 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 VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
def main(arr): max_val = -float("inf") ans = 0 temp_ans = 0 inc = 0 for i in range(len(arr)): arr[i] += inc if arr[i] >= max_val: max_val = arr[i] else: ans += max_val - arr[i] inc += max_val - arr[i] ans += temp_ans return ans n = int(input()) arr = list(map(int, input().split())) print(main(arr))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
a = input() i = int(a) - 1 l1 = list(map(int, input().split())) ans = 0 while i > 0: if l1[i] >= l1[i - 1]: i -= 1 continue ans += l1[i - 1] - l1[i] i -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, input().split())) for i in range(1): n = nmbr() a = lst() ans = 0 for i in range(n - 1, 0, -1): if a[i] < a[i - 1]: ans += a[i - 1] - a[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) l = list(map(int, input().split())) l.append(max(l)) m, ans = l[0], 0 for i in range(1, n): if l[i - 1] > l[i]: ans += l[i - 1] - l[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
from sys import stdin, stdout def ai(): return list(map(int, input().split())) def ei(): return map(int, input().split()) def ip(): return int(stdin.readline()) def op(ans): return stdout.write(str(ans) + "\n") n = ip() li = ai() count = 0 for i in range(n - 1): if li[i] > li[i + 1]: count += li[i] - li[i + 1] print(count)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
def main(): n = int(input()) a = [int(i) for i in input().split()] ans = 0 for i in range(n - 1): d = a[i] - a[i + 1] if d > 0: ans += d print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) a = list(map(int, input().split())) res = [max(0, a[i - 1] - a[i]) for i in range(1, n)] print(sum(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 FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
from sys import stdin n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) b = list() for i in range(1, n): b.append(a[i] - a[i - 1]) print(sum([(-ele) for ele in b if ele < 0]))
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 FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) n = int(input()) arr = get_ints() minimumheight = arr[0] prevheight = arr[0] ans = 0 for i in range(1, n): curnum = arr[i] if curnum >= minimumheight: prevheight = curnum minimumheight = curnum continue if curnum < prevheight: ans += prevheight - curnum prevheight = curnum continue if curnum == prevheight: continue if prevheight <= curnum: prevheight = curnum continue print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Little Elephant loves sortings. He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r. Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds. Input The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing. Output In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 1 2 3 Output 0 Input 3 3 2 1 Output 2 Input 4 7 4 1 47 Output 6 Note In the first sample the array is already sorted in the non-decreasing order, so the answer is 0. In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]). In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].
n = int(input()) a = list(map(int, input().split())) b = [] for i in range(1, n): b.append(max(0, a[i - 1] - a[i])) print(sum(b))
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 NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
def is_subset(a, b): return ~a & b == 0 n = int(input()) a = map(int, input().split()) b = map(int, input().split()) vis = [False] * (n + 1) st = list(zip(a, b)) st.sort() st.reverse() st.append((-1, -1)) ans = 0 for i in range(n): if st[i][0] != st[i + 1][0]: continue for j in range(i, n): if is_subset(st[i][0], st[j][0]): vis[j] = True for i in range(n): ans += vis[i] * st[i][1] print(ans)
FUNC_DEF RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] aSet = set() duplicates = set() for ai in a: if ai in aSet: duplicates.add(ai) aSet.add(ai) total = 0 for i, ai in enumerate(a): if ai in duplicates: total += b[i] continue for d in duplicates: if ai & d == ai: total += b[i] break print(total)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) vis = [(0) for i in range(len(a))] c = [] d = {} for i in range(len(a)): if a[i] in d: d[a[i]].append(i) else: d[a[i]] = [i] ans = 0 for i in d.keys(): if len(d[i]) >= 2: for j in d[i]: ans += b[j] vis[j] = 1 c.append(a[j]) c = set(c) for i in range(len(a)): if vis[i] == 0: for j in c: if j > a[i] and j & a[i] == a[i]: c.add(a[i]) ans += b[i] break print(ans)
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] vis = [False] * n s = sorted(zip(a, b)) c = 1 L1 = [] L2 = [] ans = 0 h = s[0][1] for i in range(1, len(s)): if s[i][0] != s[i - 1][0]: if c > 1: L1.append(s[i - 1][0]) ans += h else: L2.append(s[i - 1]) c = 1 h = s[i][1] else: c += 1 h += s[i][1] if c > 1: L1.append(s[-1][0]) ans += h else: L2.append(s[-1]) for i in range(0, len(L2)): for j in range(0, len(L1)): if L2[i][0] | L1[j] == L1[j]: ans += L2[i][1] break print(ans)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) b = [int(i) for i in input().split()] a = [int(i) for i in input().split()] rev = {} dic = {} for i in range(n): bb = b[i] if bb in dic: dic[bb].append(i) else: dic[bb] = [i] neg1 = (1 << 60) - 1 ans = 0 mask = 0 dels = [] for bb in dic: inds = dic[bb] if len(inds) > 1: for ind in inds: ans += a[ind] mask |= bb dels.append(bb) if ans == 0: print(0) exit() dic2 = {} l2 = [] heads = [] for bb in dels: dic2[bb] = bin(bb).count("1") l2.append(bb) del dic[bb] l2.sort(key=lambda q: dic2[q], reverse=True) for l in l2: subs = False for h in heads: if l | ~h & neg1 == neg1: subs = True break if not subs: heads.append(l) if len(heads) > 5000: 1 // 0 for bb in dic: subs = False for h in heads: if h | ~bb & neg1 == neg1: subs = True break if subs: ans += a[dic[bb][0]] print(ans)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) dict = {} for i in a: if i in dict: dict[i] += 1 else: dict[i] = 1 res = 0 group = [] for i in dict: if dict[i] > 1: group.append(i) for i in range(n): for k in group: if a[i] | k == k: res += b[i] break print(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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) s = list(zip(a, b)) s.sort(reverse=True) i = 0 ans = 0 vz = [] while i < n: j = i + 1 while j < n and s[j][0] == s[i][0]: j += 1 if j - i > 1: vz.extend(s[i:j]) else: for e in vz: if e[0] | s[i][0] == e[0]: vz.append(s[i]) break i = j print(sum(map(lambda x: x[1], vz)))
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) B = [(b, a) for a, b in zip(A, B)] C = {} for a in A: if a not in C: C[a] = 0 C[a] += 1 inc = set() for c in C: if C[c] > 1: inc.add(c) ans = 0 for b, a in B: if a in inc: ans += b else: flag = False for c in inc: if c & a == a: flag = True if flag: ans += b print(ans)
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) freq = {} for i in a: freq[i] = [0, 0] for i in range(len(a)): freq[a[i]][0] += 1 freq[a[i]][1] += b[i] ans = 0 gr = {} for i in freq: if freq[i][0] > 1: gr[i] = freq[i] ans += freq[i][1] for i in freq: if freq[i][0] == 1: flag = 0 for k in gr: if k & i == i: flag = 1 if flag == 1: ans += freq[i][1] print(ans)
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
from sys import * n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) gp = [] ans = [] for i in range(n): if a.count(a[i]) > 1: if a[i] not in gp: gp.append(a[i]) ans.append(b[i]) if len(gp) == 0: stdout.write("0") else: def better(a, gp): for i in range(len(gp)): if a | gp[i] == gp[i]: return False return True for i in range(n): if a[i] not in gp: if better(a[i], gp) == False: gp.append(a[i]) ans.append(b[i]) stdout.write(str(sum(ans)))
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) v = [] for i in range(n): v.append([a[i], b[i]]) v.sort() prev = -1 big = [] flag = True for i in v: if prev == i[0] and (flag or big[-1] != i[0]): big.append(i[0]) flag = False prev = i[0] answer = 0 counter = 0 big.sort() for i in v: for j in big: if j == j | i[0]: answer += i[1] counter += 1 break if counter < 2: print(0) else: print(answer)
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
ii = lambda: int(input()) kk = lambda: map(int, input().split()) ll = lambda: list(kk()) n = ii() vals = zip(kk(), kk()) d = {} for v in vals: if v[0] not in d: d[v[0]] = [] d[v[0]].append(v[1]) d2 = {k: sum(d[k]) for k in d} maxs = 0 if 0 not in d: d[0], d2[0] = [0], 0 ls = sorted(d.keys(), reverse=True) ans = {l: (0) for l in ls} valid = {l: (0) for l in ls} for k in ls: if len(d[k]) > 1: valid[k] = 1 for k2 in ls: if k2 < k: break if valid[k2] and k & k2 == k: valid[k] = 1 ans[k] += d2[k2] print(ans[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
def bit(used, b): count = 0 for num in used: for i in range(60): if 1 << i & num == 0 and 1 << i & b: count += 1 break if count == len(used): return True else: return False def f(a, b): arr = list(zip(a, b)) d = {} for i in arr: d[i[0]] = d.get(i[0], []) + [i[1]] cmax = -1 ans = 0 used = set() for i in d: if len(d[i]) >= 2: ans += sum(d[i]) cmax = max(cmax, i) used.add(i) for i in d: if i in used: continue if bit(used, i) == True: continue else: ans += sum(d[i]) return ans a = input() l = list(map(int, input().strip().split())) l2 = list(map(int, input().strip().split())) print(f(l, l2))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER LIST LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
t = int(input()) algo = [int(i) for i in input().split()] strength = [int(i) for i in input().split()] dict1 = {} dict2 = {} binary = {} max_strength = 0 for i in range(len(algo)): if algo[i] in dict1: binary[algo[i]] = bin(algo[i])[2:] dict1[algo[i]] += 1 if dict1[algo[i]] == 2: max_strength += dict2[algo[i]] + strength[i] else: max_strength += strength[i] else: dict1[algo[i]] = 1 dict2[algo[i]] = strength[i] for i in range(t): ok = True if algo[i] not in binary: bunu = bin(algo[i])[2:] for j in binary: if len(bunu) <= len(binary[j]): sliced = binary[j][-len(bunu) :] for ji in range(len(bunu)): if int(bunu[ji]) == 1 and int(sliced[ji]) != 1: ok = False break else: max_strength += strength[i] break print(max_strength)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) info = {} state = [] str = input() list1 = str.split(" ") for i in range(n): aa = int(list1[i]) state.append(aa) if info.get(aa) == None: info[aa] = {(1): 0, (2): False} else: info[aa] = {(1): 0, (2): True} str = input() list1 = [] list1 = str.split(" ") for i in range(n): bb = int(list1[i]) info[state[i]][1] = bb + info[state[i]][1] ans = 0 inside = [] outside = [] for i in info: if info[i][2] == True: ans = ans + info[i][1] inside.append(i) else: outside.append(i) inside.sort(reverse=True) outside.sort() nn = len(inside) iiii = 0 ii = 0 has = {} for i in inside: has[i] = 1 for j in outside: if i & j == j: has[j] = 1 ans = 0 for i in has: ans = ans + info[i][1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR DICT NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR DICT NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
def less_exp(a1, a2): return a1 < a2 and a2 - a1 == a1 ^ a2 n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) g = [] for i in range(len(a)): g.append((a[i], b[i])) g.sort(key=lambda x: x[0]) selected_nodes = set() sum1 = 0 for i in range(len(a)): if i < len(a) - 1 and g[i][0] == g[i + 1][0]: selected_nodes.add(g[i][0]) sum1 += g[i][1] elif ( i > 0 and (i == len(a) - 1 or g[i][0] != g[i + 1][0]) and g[i][0] == g[i - 1][0] ): sum1 += g[i][1] if sum1 > 0: for node in g: if node[0] not in selected_nodes: for node1 in selected_nodes: if less_exp(node[0], node1): sum1 += node[1] break print(sum1)
FUNC_DEF RETURN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [] for i in range(n): c.append((a[i], b[i])) c.sort(reverse=True) ins = [] cnt = 1 now = c[0][0] tot = c[0][1] i = 1 ans = 0 while 1: while i < n and c[i][0] == now: tot += c[i][1] i += 1 cnt += 1 if cnt >= 2: ins.append(now) ans += tot if cnt == 1: for j in ins: if now & j == now: ins.append(now) ans += tot break if i >= n: break now = c[i][0] cnt = 1 tot = c[i][1] i += 1 print(ans)
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other. Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set. Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group. Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum? Input The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp. The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}). The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9). Output Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0. Examples Input 4 3 2 3 6 2 8 5 10 Output 15 Input 3 1 2 3 1 2 3 Output 0 Input 1 0 1 Output 0 Note In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i. In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
import sys n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) if n == 1: print(0) sys.exit(0) X = [] for i in range(n): X.append((A[i], B[i])) X.sort() Y = [] S = set() ans = 0 for i in range(n): if i < n - 1 and X[i][0] == X[i + 1][0] or i > 0 and X[i][0] == X[i - 1][0]: ans += X[i][1] S.add(X[i][0]) else: Y.append(X[i]) for a, b in Y: T = False for s in S: if a & s == a: T = True break if T: ans += b print(ans)
IMPORT 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 FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
t = int(input()) for _ in range(t): n, m, k = map(int, input().split()) groups = [] arr = [(i + 1) for i in range(n)] z = 0 if n % m != 0: larger = n // m + 1 while n % m != 0: groups.append(larger) n -= larger m -= 1 z += larger a = n // m for _ in range(m): groups.append(a) while k: curr = 0 for i in groups: print(i, end=" ") j = i while j: print(arr[curr], end=" ") curr += 1 j -= 1 print() arr = arr[z:] + arr[:z] k -= 1 print()
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 LIST ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
for _ in range(int(input())): n, m, k = map(int, input().split()) st = 1 for i in range(k): tt, rem, nxt = n // m, n % m, st for j in range(m): print(str(tt + 1 if rem else tt) + " ") for k in range(tt): print(str(st) + " ") st += 1 if st == n + 1: st = 1 if rem: rem -= 1 print(str(st) + " ") st += 1 if st == n + 1: st = 1 if rem == 0: nxt = st print("\n") st = nxt print("\n")
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
t = int(input()) for case in range(t): n, m, k = tuple(map(int, input().split())) p = n // m q = p + 1 r = n % m nouveau_indice = 1 for i in range(k): indice = nouveau_indice for j in range(m): if j < r: s = str(q) for u in range(q): s += " " + str(indice) indice += 1 if indice == n + 1: indice = 1 print(s) else: if j == r: nouveau_indice = indice s = str(p) for u in range(p): s += " " + str(indice) indice += 1 if indice == n + 1: indice = 1 print(s) print("")
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 BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
import sys input = sys.stdin.readline t = int(input()) ans = [] for _ in range(t): n, m, k = map(int, input().split()) if n % m == 0: c = n // m ans0 = [] for _ in range(k): for i in range(m): ans0.append( " ".join(map(str, [c] + [(j + i * c + 1) for j in range(c)])) ) ans.append("\n".join(ans0)) continue x = [n // m + min(n % m, 1)] * m s = sum(x) u = 0 for i in range(s - n): x[i] -= 1 u += x[i] c = u * k y = [set() for _ in range(k)] z = [c // n + min(c % n, 1)] * n s = sum(z) for i in range(s - c): z[i] -= 1 j = 0 for i in range(n): for _ in range(z[i]): y[j].add(i + 1) j += 1 j %= k ans0 = [] for i in range(k): a = y[i] b = set() for j in range(1, n + 1): if not j in a: b.add(j) a = list(a) b = list(b) for j in x: ans00 = [j] if j == x[0]: for _ in range(j): ans00.append(a.pop()) else: for _ in range(j): ans00.append(b.pop()) ans0.append(" ".join(map(str, ans00))) ans.append("\n".join(ans0)) sys.stdout.write("\n\n".join(ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP LIST VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR LIST VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
import sys I = lambda: [*map(int, sys.stdin.readline().split())] (t,) = I() for _ in range(t): n, m, k = I() players = [(i + 1) for i in range(n)] big = n % m sml = m - big for i in range(k): start = n * i // k ind = start for j in range(big): out = [str(n // m + 1)] for l in range(n // m + 1): out.append(str(players[ind])) ind = (ind + 1) % n print(" ".join(out)) for j in range(sml): out = [str(n // m)] for l in range(n // m): out.append(str(players[ind])) ind = (ind + 1) % n print(" ".join(out))
IMPORT ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
import sys def read_line(): return sys.stdin.readline().strip() def print_table(nums): print(f"{len(nums)} {' '.join([str(x) for x in nums])}") t = int(read_line()) for _ in range(t): n, m, k = tuple([int(x) for x in read_line().split()]) big_tables_starting_num = 0 big_tables_count = n % m big_tables_size = 1 + n // m small_tables_count = m - n % m small_tables_size = n // m people_at_big_tables = big_tables_size * big_tables_count big_table_nums = [-1] * big_tables_size small_table_nums = [-1] * small_tables_size for _ in range(k): i = 0 for bt in range(big_tables_count): for x in range(1 + n // m): big_table_nums[x] = (big_tables_starting_num + i) % n + 1 i += 1 print_table(big_table_nums) for st in range(small_tables_count): for x in range(n // m): small_table_nums[x] = (big_tables_starting_num + i) % n + 1 i += 1 print_table(small_table_nums) big_tables_starting_num = (big_tables_starting_num + people_at_big_tables) % n print()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR 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 FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
import sys def input(): return sys.stdin.readline().rstrip() DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)] mod = 998244353 inf = 1 << 64 def slv(): n, m, k = map(int, input().split()) if n % m == 0: for i in range(k): persons = [(i + 1) for i in range(n)] for _ in range(m): tmp = [] for j in range(n // m): tmp.append(persons.pop()) print(len(tmp), *tmp) else: number = 1 md = n % m for i in range(k): new_number = -1 tmp = [[] for _ in range(m)] for j in range(m): if j < md: for _ in range(n // m + 1): tmp[j].append(number) number += 1 if number > n: number -= n new_number = number else: for _ in range(n // m): tmp[j].append(number) number += 1 if number > n: number -= n for j in range(m): print(len(tmp[j]), *tmp[j]) number = new_number print("") return def main(): t = int(input()) for i in range(t): slv() return 0 main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER EXPR FUNC_CALL VAR
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
t = int(input()) for test in range(t): n, m, k = list(map(int, input().split())) cnt = (n + m - 1) // m * (n % m) now = 1 for i in range(k): for j in range(n % m): print((n + m - 1) // m, end=" ") for x in range((n + m - 1) // m): print(now, end=" ") now += 1 if now == n + 1: now = 1 print() tmp = now for j in range(m - n % m): print(n // m, end=" ") for x in range(n // m): print(now, end=" ") now += 1 if now == n + 1: now = 1 now = tmp print()
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 BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play). $n$ people gathered in a room with $m$ tables ($n \ge 2m$). They want to play the Hat $k$ times. Thus, $k$ games will be played at each table. Each player will play in $k$ games. To do this, they are distributed among the tables for each game. During each game, one player plays at exactly one table. A player can play at different tables. Players want to have the most "fair" schedule of games. For this reason, they are looking for a schedule (table distribution for each game) such that: At any table in each game there are either $\lfloor\frac{n}{m}\rfloor$ people or $\lceil\frac{n}{m}\rceil$ people (that is, either $n/m$ rounded down, or $n/m$ rounded up). Different numbers of people can play different games at the same table. Let's calculate for each player the value $b_i$ — the number of times the $i$-th player played at a table with $\lceil\frac{n}{m}\rceil$ persons ($n/m$ rounded up). Any two values of $b_i$must differ by no more than $1$. In other words, for any two players $i$ and $j$, it must be true $|b_i - b_j| \le 1$. For example, if $n=5$, $m=2$ and $k=2$, then at the request of the first item either two players or three players should play at each table. Consider the following schedules: First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $5, 1$, and at the second — $2, 3, 4$. This schedule is not "fair" since $b_2=2$ (the second player played twice at a big table) and $b_5=0$ (the fifth player did not play at a big table). First game: $1, 2, 3$ are played at the first table, and $4, 5$ at the second one. The second game: at the first table they play $4, 5, 2$, and at the second one — $1, 3$. This schedule is "fair": $b=[1,2,1,1,1]$ (any two values of $b_i$ differ by no more than $1$). Find any "fair" game schedule for $n$ people if they play on the $m$ tables of $k$ games. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test. Each test case consists of one line that contains three integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5$, $1 \le m \le \lfloor\frac{n}{2}\rfloor$, $1 \le k \le 10^5$) — the number of people, tables and games, respectively. It is guaranteed that the sum of $nk$ ($n$ multiplied by $k$) over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case print a required schedule — a sequence of $k$ blocks of $m$ lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from $1$ to $n$) who should play at this table. If there are several required schedules, then output any of them. We can show that a valid solution always exists. You can output additional blank lines to separate responses to different sets of inputs. -----Examples----- Input 3 5 2 2 8 3 1 2 1 3 Output 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1 -----Note----- None
t = int(input()) for test in range(t): n, m, k = [int(x) for x in input().split()] mx = n // m + 1 table = [[0] * mx in range(k)] lastUnfair = 1 for p in range(k): curply = lastUnfair for table in range(m - (m - n % m)): print(mx, end=" ") plys = 0 while plys < mx: if curply > n: curply = 1 print(curply, end=" ") plys += 1 curply += 1 print() lastUnfair = curply for table in range(m - n % m): print(mx - 1, end=" ") plys = 0 while plys < mx - 1: if curply > n: curply = 1 print(curply, end=" ") plys += 1 curply += 1 print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
import sys input = sys.stdin.readline n, m = map(int, input().split()) men = [] for i in range(n): x, y = map(int, input().split()) men.append((y, x - 1)) def solve(lim): cnt = [0] * m vis = [0] * n cost = 0 for i in range(n): cnt[men[i][1]] += 1 for i in range(n): if men[i][1] != 0 and cnt[men[i][1]] >= lim: cnt[men[i][1]] -= 1 cnt[0] += 1 cost += men[i][0] vis[i] = 1 for i in range(n): if cnt[0] < lim and vis[i] == False and men[i][1] != 0: cnt[0] += 1 cost += men[i][0] return cost men.sort() ans = 10**18 for i in range(n): ans = min(ans, solve(i)) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
import sys input = sys.stdin.readline n, m = map(int, input().split()) party = [[] for _ in range(m + 5)] pc = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[1]) choose = [0] * n for i in range(n): party[pc[i][0]].append(i) want = 10**18 for i in range(1, n + 1): p1 = len(party[1]) for j in range(2, m + 5): if len(party[j]) < i: continue for k in range(len(party[j]) - i + 1): p1 += 1 choose[party[j][k]] = 1 want2 = 0 for j in range(n): if p1 < i and choose[j] == 0 and pc[j][0] != 1: choose[j] = 1 p1 += 1 if choose[j] == 1: want2 += pc[j][1] if want > want2: want = want2 choose = [0] * n print(want)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
n, m = map(int, input().split()) parties = [[] for i in range(m)] for i in range(n): x, y = map(int, input().split()) parties[x - 1].append(y) for i in range(m): parties[i].sort(reverse=True) mi = len(parties[0]) cost = float("inf") for i in range(mi, n + 1): temp = 0 to_chose = [] collected = mi for j in range(1, m): count = 0 for k in parties[j]: count += 1 if count >= i: temp += k collected += 1 else: to_chose.append(k) to_chose = sorted(to_chose) if collected >= i: cost = min(cost, temp) if len(to_chose) < i - collected: continue for j in range(0, i - collected): temp += to_chose[j] cost = min(cost, temp) print(cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
class Solver: def solve(self): self.num_voters, self.num_parties = (int(x) for x in input().split()) self.votes_per_party = [[] for _ in range(self.num_parties)] for _ in range(self.num_voters): party, price = (int(x) for x in input().split()) party -= 1 self.votes_per_party[party].append(price) for party in range(self.num_parties): self.votes_per_party[party].sort() max_necessary_votes = self.num_voters // 2 + 1 cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) return self.ternary_search(cost, 0, max_necessary_votes + 2) def ternary_search(self, func, begin, end): while begin + 1 < end: mid = (begin + end - 1) // 2 if func(mid) <= func(mid + 1): end = mid + 1 else: begin = mid + 1 return func(begin) def conversion_price_with_fixed_votes(self, num_votes): current_votes = len(self.votes_per_party[0]) total_cost = 0 for votes in self.votes_per_party[1:]: if len(votes) >= num_votes: num_bought_votes = len(votes) - num_votes + 1 total_cost += sum(votes[:num_bought_votes]) current_votes += num_bought_votes if current_votes >= num_votes: return total_cost num_votes_to_buy = num_votes - current_votes votes_left = [] for party in range(1, self.num_parties): votes_left += self.votes_per_party[party][-(num_votes - 1) :] votes_left.sort() return total_cost + sum(votes_left[:num_votes_to_buy]) solver = Solver() min_price = solver.solve() print(min_price)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
def solve(max_v, votes, m): ans = 0 values = [] our_v = len(votes[0]) for i in range(1, m): L = len(votes[i]) if L > max_v: take = L - max_v our_v += take ans += sum(votes[i][:take]) if take < L: values.extend(votes[i][take:]) else: values.extend(votes[i]) values.sort() if our_v <= max_v: needed = max_v + 1 - our_v if needed <= len(values): for i in range(needed): ans += values[i] our_v += 1 if our_v <= max_v: return 1 << 60 return ans n, m = [int(x) for x in input().split()] votes = [[] for x in range(m)] for i in range(n): p, c = (int(x) for x in input().split()) p -= 1 votes[p].append(c) for i in range(m): L = len(votes[i]) if L > 1: votes[i].sort() lo = 0 hi = n // 2 + 5 while lo < hi: mi = lo + (hi - lo) // 2 if solve(mi, votes, m) > solve(mi + 1, votes, m): lo = mi + 1 else: hi = mi ans = 1 << 60 E = 20 for i in range(max(0, lo - E), min(lo + E, n + 1)): ans = min(ans, solve(i, votes, m)) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN BIN_OP NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
n, m = map(int, input().split()) parties = [(0) for _ in range(m)] pairs = [] for i in range(n): p, c = map(int, input().split()) parties[p - 1] += 1 pairs.append((c, p - 1)) pairs.sort() min_sum = 10**31 for i in range(n, parties[0] - 1, -1): cur_sum = 0 cur_amount = parties[0] colours = [(0) for _ in range(n)] need_to_buy = [max(parties[j] + 1 - i, 0) for j in range(m)] need_to_buy[0] = 0 tmp = sum(need_to_buy) if tmp + parties[0] > i: print(min_sum) exit() for j in range(n): if need_to_buy[pairs[j][1]] > 0: need_to_buy[pairs[j][1]] -= 1 colours[j] = 1 cur_amount += 1 cur_sum += pairs[j][0] j = 0 while cur_amount < i: if colours[j] == 0 and pairs[j][1] != 0: cur_amount += 1 cur_sum += pairs[j][0] j += 1 min_sum = min(min_sum, cur_sum) print(min_sum)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
from sys import stdin n, m = map(int, stdin.readline().split()) pc = dict() ph = dict() prices = [] for i in range(n): p, c = map(int, stdin.readline().split()) if p != 1: prices.append(c) pc.setdefault(p, []).append(len(prices) - 1) ph[p] = ph.setdefault(p, 0) + 1 max_h = 0 for p, pa in pc.items(): pa.sort(reverse=True, key=lambda x: prices[x]) max_h = max(max_h, len(pa)) hp = [[] for _ in range(max_h + 2)] for p, h in ph.items(): if p != 1: hp[h].append(p) ans = [0] * (max_h + 2) sprices_ind = sorted(range(len(prices)), key=lambda x: prices[x]) p_set = set() height_p1 = ph[1] if 1 in ph else 0 top_sum = 0 top_count = 0 for i in range(max_h + 1, -1, -1): if len(prices) < i: ans[i] = float("inf") continue for p in hp[i]: p_set.add(p) for p in p_set: if len(pc[p]) > 0: top_ind = pc[p].pop() c = prices[top_ind] prices[top_ind] = -1 top_sum += c top_count += 1 ans[i] += top_sum if height_p1 + top_count < i: k = 0 for j in range(i - height_p1 - top_count): while prices[sprices_ind[k]] == -1: k += 1 ans[i] += prices[sprices_ind[k]] k += 1 print(min(ans))
ASSIGN VAR 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 ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
n, m = map(int, input().split()) cn = [] un = 0 d = {} for i in range(n): p, c = map(int, input().split()) if p == 1: un += 1 else: cn.append((p, c)) cn.sort(key=lambda x: x[1]) for i in range(len(cn)): p, c = cn[i] if p not in d: d[p] = [(c, i)] else: d[p].append((c, i)) min_val = 99999999999999999999999 for i in range(1, n // 2 + 2): total = 0 v = set() for x in d: if len(d[x]) >= i: r = len(d[x]) - (i - 1) for j in range(0, r): total += d[x][j][0] v.add(d[x][j][1]) for j in range(n): if len(v) + un >= i: break if j not in v: total += cn[j][1] v.add(j) min_val = min(total, min_val) print(min_val)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
n, m = map(int, input().split()) pc = [(0, 0) for _ in range(n)] party_votes = [(0) for _ in range(m)] for i in range(n): p, c = map(int, input().split()) pc[i] = p - 1, c party_votes[p - 1] += 1 pc.sort(key=lambda x: x[1]) min_cost = 10**20 for votes in range(n + 1): _party_votes = party_votes[:] dangerous = list(map(lambda party: _party_votes[party] >= votes, range(0, m))) used = list(map(lambda i: pc[i][0] == 0, range(n))) cur_cost = 0 for i in range(n): if dangerous[pc[i][0]] and pc[i][0] != 0: cur_cost += pc[i][1] _party_votes[0] += 1 _party_votes[pc[i][0]] -= 1 dangerous[pc[i][0]] = _party_votes[pc[i][0]] >= votes used[i] = True for i in range(n): if _party_votes[0] >= votes: break if not used[i]: _party_votes[0] += 1 cur_cost += pc[i][1] min_cost = min(min_cost, cur_cost) print(min_cost)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
def solve(N, M, A): pv = [0] * M pm = [[] for _ in range(M)] allm = {} for p, c in A: p -= 1 pv[p] += 1 if p == 0: continue pm[p].append(c) if c not in allm: allm[c] = 0 allm[c] += 1 for m in pm: m.sort() allml = list(allm) allml.sort() maxv = max(pv) best = 10**9 * 3000 + 1 for iv in range(1, min(maxv + 2, N + 1)): allmrest = dict(allm) cost = 0 reqd = 0 if pv[0] < iv: reqd = iv - pv[0] got = 0 for i in range(1, M): if pv[i] >= iv: ad = pv[i] - iv + 1 for j in range(ad): cost += pm[i][j] allmrest[pm[i][j]] -= 1 got += ad if reqd > got: for m in allml: c = allmrest[m] toget = min(reqd - got, c) cost += toget * m got += toget if reqd == got: break if reqd > got: continue best = min(best, cost) return best def main(): N, M = [int(e) for e in input().split(" ")] A = [[int(e) for e in input().split(" ")] for _ in range(N)] print(solve(N, M, A)) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN 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 VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of parties — $n$ and $m$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $i$-th voter $c_i$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. -----Input----- The first line of input contains two integers $n$ and $m$ ($1 \le n, m \le 3000$) — the number of voters and the number of parties respectively. Each of the following $n$ lines contains two integers $p_i$ and $c_i$ ($1 \le p_i \le m$, $1 \le c_i \le 10^9$) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $1$. -----Output----- Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections. -----Examples----- Input 1 2 1 100 Output 0 Input 5 5 2 100 3 200 4 300 5 400 5 900 Output 500 Input 5 5 2 100 3 200 4 300 5 800 5 900 Output 600 -----Note----- In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $3$, $4$ and $5$ get one vote and party number $2$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
import sys n, m = map(int, input().split()) V = [None] * n for i in range(n): V[i] = tuple(map(int, input().split())) V.sort(key=lambda x: x[1]) if n == 1: if V[0][0] == 1: print(0) else: print(V[0][1]) sys.exit() Voterlist = [[] for i in range(m + 1)] Partylist = [(0) for i in range(m + 1)] Vnot1 = [] for i in range(n): Voterlist[V[i][0]] += [V[i][1]] Partylist[V[i][0]] += 1 if V[i][0] != 1: Vnot1.append(V[i]) Vnot1.sort(key=lambda x: x[1]) maxvote = max(Partylist) Votenumberlist = [[] for i in range(maxvote + 1)] for i in range(1, m + 1): Votenumberlist[Partylist[i]].append(i) if Votenumberlist[maxvote] == [1]: print(0) sys.exit() ANS = 0 for i in range(maxvote + 1 - Partylist[1]): ANS += Vnot1[i][1] maxvoteminus = 1 while True: checkparty = [(0) for i in range(m + 1)] money = 0 for i in range(maxvoteminus): for j in Votenumberlist[maxvote - i]: checkparty[j] += maxvoteminus - i for i in range(2, m + 1): money += sum(Voterlist[i][: checkparty[i]]) if money > ANS: break neednumber = maxvote + 1 - maxvoteminus - Partylist[1] for i in range(maxvoteminus): neednumber -= len(Votenumberlist[maxvote - i]) * (maxvoteminus - i) for pm in Vnot1: if checkparty[pm[0]] != 0: checkparty[pm[0]] -= 1 else: money += pm[1] neednumber -= 1 if neednumber == 0: break if money < ANS: ANS = money maxvoteminus += 1 print(ANS)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER LIST VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
t = int(input()) for tes in range(t): n = int(input()) a = list(map(int, input().split())) if n == 2: print(max(a[0] + a[1], abs(a[1] - a[0]) * 2)) elif n == 3: print( max( a[0] + a[1] + a[2], a[0] * 3, a[2] * 3, max(abs(a[1] - a[0]), abs(a[2] - a[1])) * 3, abs(a[0] - a[1]) * 2 + a[2], abs(a[2] - a[1]) * 2 + a[0], ) ) else: ans = 0 for i in range(n): ans = max(ans, a[i] * n) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
def N(): return int(input()) def A(): return [int(x) for x in input().split()] def S(): return input() for _ in range(N()): n = N() if "codeforces" == 28226329: print("Tanmay") a = A() maxi = max(a) s1 = sum(a) if n == 3: print( max( 3 * max(abs(a[0] - a[2]), abs(a[1] - a[2]), abs(a[1] - a[0]), a[0], a[2]), s1, ) ) continue if n == 2: print(max(s1, (maxi - min(a)) * 2)) continue print(max(s1, n * maxi))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF STRING NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
a = int(input()) for _ in range(a): b = int(input()) l = list(map(int, input().split())) ans = sum(l) if b == 2: ans = max(ans, 2 * abs(l[0] - l[1])) elif b == 3: ll = [l[0], l[2], abs(l[0] - l[1]), abs(l[1] - l[2]), abs(l[0] - l[2])] ans = max(ans, max(ll) * 3) else: ans = max(ans, max(l) * b) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
t = int(input()) for i in range(t): n = int(input()) arr = [int(x) for x in input().split()] mx = max(arr) mi = min(arr) s = 0 for i in range(n): s = s + arr[i] res = s val = 0 if n == 2: res = max(2 * (mx - mi), res) elif n == 3: q = arr[0] * 3 w = arr[2] * 3 e = abs(arr[1] - arr[0]) * 3 r = abs(arr[1] - arr[2]) * 3 res = max(res, q, w, e, r) elif n > 3: res = max(res, mx * n) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for y in range(int(input())): n = int(input()) a = list(map(int, input().split())) m = 0 ind = 0 for i in range(len(a)): if a[i] > m: m = a[i] ind = i if n == 2: print(max(2 * abs(a[1] - a[0]), a[1] + a[0])) elif n == 3: s = sorted(a) if m == a[1] and s[2] != s[1]: print( max( sum(a), 2 * (a[1] - a[0]) + a[2], 2 * (a[1] - a[2]) + a[0], (a[1] - a[0]) * 3, (a[1] - a[2]) * 3, 3 * a[0], 3 * a[2], ) ) else: print(m * n) else: print(m * n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
def ii(num=False): i = input().split() if num: return int(i[0]) try: return list(map(int, i)) except Exception: return i def gcd(a, b): if a == 0: return b return gcd(b % a, a) for _ in range(ii(1)): n, a = ii(1), ii() if n == 2: print(max(2 * abs(a[0] - a[1]), a[0] + a[1])) elif n == 3: print( max( a[0] + a[1] + a[2], 3 * a[0], 3 * a[2], 3 * abs(a[0] - a[1]), 3 * abs(a[1] - a[2]), ) ) else: print(n * max(a)) pass
FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR RETURN FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
t = int(input()) for test in range(t): n = int(input()) nums = list(map(int, input().split())) if len(nums) > 3: print(max(nums) * len(nums)) elif len(nums) == 2: print(max(sum(nums), 2 * abs(nums[0] - nums[1]))) elif len(nums) == 3: a = [sum(nums)] a.append( 3 * max(nums[0], nums[2], abs(nums[1] - nums[0]), abs(nums[1] - nums[2])) ) print(max(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
t = int(input()) while t > 0: n = int(input()) A = list(map(int, input().split())) SUM = sum(A) if n > 3: print(max(A) * n) elif n == 2: print(max(sum(A), abs(A[0] - A[1]) * n)) elif n == 3: SUM = max(SUM, A[2] + abs(A[0] - A[1]) * 2) SUM = max(SUM, A[0] + abs(A[2] - A[1]) * 2) SUM = max(SUM, abs(A[0] - A[2]) * 3) SUM = max(SUM, abs(A[0] - A[1]) * 3) SUM = max(SUM, abs(A[1] - A[2]) * 3) SUM = max(SUM, A[0] * 3) SUM = max(SUM, A[2] * 3) print(SUM) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n == 2: print(max(sum(a), 2 * abs(a[0] - a[1]))) elif n == 3: if a[0] == a[1] == a[2]: print(a[0] * 3) elif a[0] == max(a) or a[-1] == max(a): print(max(a) * 3) else: x = sum(a) y = (a[1] - min(a[0], a[-1])) * 3 print(max(x, y, max(a[0], a[-1]) * 3)) else: print(max(a) * n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] if n > 3: print(max(a) * n) elif n == 2: print(max((sum(a), abs(a[0] - a[1]) * 2))) else: print( max( (sum(a), a[0] * 3, a[2] * 3, abs(a[0] - a[1]) * 3, abs(a[1] - a[2]) * 3) ) )
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) if n == 0: print(0) continue if n == 1: print(a[0]) continue if n == 2: print(max(a[0] + a[1], abs(a[0] - a[1]) * 2)) continue if n == 3: print( max( sum(a), a[0] * n, a[-1] * n, abs(a[0] - a[1]) * n, abs(a[-1] - a[1]) * n ) ) continue print(max(a) * n)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) if n >= 4: print(n * max(arr)) continue if n == 2: print(max(2 * abs(arr[0] - arr[1]), arr[0] + arr[1])) continue if n == 3: a1, a2, a3 = tuple(arr) print(max(a1 + a2 + a3, 3 * a1, 3 * a3, 3 * abs(a1 - a2), 3 * abs(a2 - a3))) continue
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
def solve(n, a): if n == 1: return a[0] elif n == 2: return max(a[0] + a[1], 2 * abs(a[1] - a[0])) elif n == 3: return max( a[0] + a[1] + a[2], a[0] * 3, a[2] * 3, 3 * abs(a[0] - a[1]), 3 * abs(a[2] - a[1]), ) else: return max(a) * len(a) for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) print(solve(n, li))
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
T = int(input()) while T: T -= 1 n = int(input()) a = [int(i) for i in input().split()] if n > 3: print(n * max(a)) elif n == 2: print(max(sum(a), n * abs(a[0] - a[1]))) elif n == 3: print( max( sum(a), n * a[0], n * a[2], n * abs(a[0] - a[2]), n * abs(a[0] - a[1]), n * abs(a[1] - a[2]), ) )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] if n == 2: print(max(sum(a), abs(a[0] - a[1]) * 2)) elif n == 3: if a[0] >= a[1] and a[0] >= a[2]: print(a[0] * n) elif a[2] >= a[1] and a[2] >= a[0]: print(a[2] * n) else: f1 = a[0] * n f2 = a[2] * n f3 = a[0] + 2 * (a[1] - a[2]) f4 = a[2] + 2 * (a[1] - a[0]) f5 = sum(a) f6 = (a[1] - a[2]) * 3 f7 = (a[1] - a[0]) * 3 print(max(f1, f2, f3, f4, f5, f6, f7)) else: print(max(a) * n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
import sys input = sys.stdin.readline def readList(): return list(map(int, input().split())) def readInt(): return int(input()) def readInts(): return map(int, input().split()) def readStr(): return input().strip() def solve(): n = readInt() arr = readList() V = max(arr) if n > 3: return n * V elif n == 2: return max(sum(arr), abs(arr[0] - arr[1]) * 2) else: return max( sum(arr), 3 * arr[0], 3 * arr[-1], 3 * abs(arr[0] - arr[1]), 3 * abs(arr[1] - arr[-1]), ) for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR 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 RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
t = int(input()) for i in range(t): n = int(input()) l = list(map(int, input().split())) if n == 2: print(max(sum(l), 2 * abs(l[0] - l[1]))) elif n == 3: a = max(l) if l[0] == a or l[2] == a: print(n * max(l)) else: print( max( sum(l), 3 * l[0], 3 * l[2], 3 * abs(l[0] - l[1]), 3 * abs(l[1] - l[2]), ) ) else: print(n * max(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for _ in range(int(input())): n = int(input()) L = list(map(int, input().split())) if n > 3: print(n * max(L)) elif n == 1: print(L[0]) elif n == 2: a = sum(L) diff = abs(L[0] - L[1]) b = 2 * diff print(max(a, b)) else: M = max(L) i = L.index(M) if i == 0 or i == 2 or L[-1] == M: print(M * 3) else: print( max( sum(L), 3 * abs(L[1] - L[2]), 3 * abs(L[1] - L[0]), L[0] + 2 * abs(L[1] - L[2]), L[2] + 2 * abs(L[1] - L[0]), 3 * L[2], 3 * L[0], ) )
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) ans = sum(arr) for i in range(n): if i == 0: if n == 2: ans = max(ans, 2 * abs(arr[0] - arr[1])) else: ans = max(ans, arr[i] * n) elif i == n - 1: if n == 2: ans = max(ans, 2 * abs(arr[0] - arr[1])) else: ans = max(ans, arr[i] * n) elif n == 3: op1 = abs(arr[0] - arr[i]) op1 *= n op2 = abs(arr[i] - arr[-1]) op2 *= n ans = max(ans, max(op1, op2)) else: ans = max(ans, arr[i] * n) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for tt in range(int(input())): n = int(input()) a = list(map(int, input().split())) m = max(a) if n >= 4: print(n * m) elif n == 2: print(max(a[0] + a[1], 2 * abs(a[0] - a[1]))) else: print(max(3 * (a[1] - min(a)), sum(a), 3 * a[0], 3 * a[2]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER
You are given an array $a$ of $n$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $2$ indices $i$,$j$ where $1 \le i < j \le n$ and replace $a_k$ for all $i \leq k \leq j$ with $|a_i - a_j|$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of test cases. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$. It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the sum of the final array. -----Examples----- Input 3 3 1 1 1 2 9 1 3 4 9 5 Output 3 16 18 -----Note----- In the first test case, it is not possible to achieve a sum $> 3$ by using these operations, therefore the maximum sum is $3$. In the second test case, it can be shown that the maximum sum achievable is $16$. By using operation $(1,2)$ we transform the array from $[9,1]$ into $[8,8]$, thus the sum of the final array is $16$. In the third test case, it can be shown that it is not possible to achieve a sum $> 18$ by using these operations, therefore the maximum sum is $18$.
for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) if n == 2: print(max(sum(li), 2 * abs(li[0] - li[1]))) elif n == 3: print( max( sum(li), 3 * li[0], 3 * li[2], 3 * abs(li[0] - li[1]), 3 * abs(li[1] - li[2]), ) ) else: print(max(sum(li), n * max(li)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def ans(arr, k): n = len(arr) if k > n: s = sum(arr) x = n * (n + 1) // 2 print(s - x + n * k) else: res = 0 for i in range(k): res += arr[i] curr_sum = res for i in range(k, n): curr_sum += arr[i] - arr[i - k] res = max(res, curr_sum) print(res + k * (k - 1) // 2) t = int(input()) for _ in range(t): n, k = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] ans(arr, k)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def partial_sum(a, b): return (b + a) * (b - a + 1) // 2 for _ in range(int(input())): n, k = list(map(int, input().split())) mushs = list(map(int, input().split())) if k >= n: print(partial_sum(k - n, k - 1) + sum(mushs)) continue num = sum(mushs[:k]) M = num for i in range(k, n): num += mushs[i] - mushs[i - k] M = max(M, num) print(partial_sum(0, k - 1) + M)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) arr = list(map(int, input().split())) if k <= n: lps = ans = 0 for i in range(n): lps += arr[i] if i >= k: lps -= arr[i - k] ans = max(ans, lps) return ans + k * (k - 1) // 2 else: ans = sum(arr) + (k - 1 + k - 1 - n + 1) * n // 2 return ans for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
testes = int(input()) for t in range(testes): n_forest, tempo = map(int, input().split()) mush_pos = [0] + list(map(int, input().split())) for i in range(1, n_forest + 1): mush_pos[i] += mush_pos[i - 1] if tempo > n_forest: print(mush_pos[n_forest] + (tempo - 1 + tempo - n_forest) * n_forest // 2) else: ans = 0 for i in range(n_forest + 1): if i >= tempo: ans = max(ans, mush_pos[i] - mush_pos[i - tempo]) print(ans + (1 + tempo - 1) * (tempo - 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
from sys import stdin input = stdin.readline def ii(): return int(input()) def li(): return list(map(int, input().split())) def maxSubArraySum(a, size): n = len(a) s = 0 for i in range(size): s += a[i] ans = s for i in range(size, n): s += a[i] - a[i - size] ans = max(ans, s) return ans for _ in range(ii()): n, k = li() a = li() if k <= n: print(maxSubArraySum(a, k) + k * (k - 1) // 2) continue if n == 1: print(a[0] + k - 1) continue ans = sum(a) incr = k - n + 2 ans += 1 + 2 * (incr - 2) for i in range(2, n): ans += incr incr += 1 print(ans)
ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for _ in range(t): a, b = input().split() b = int(b) s = list(map(int, input().split())) if b < len(s): tmp = sum(s[0:b]) mx = tmp for i in range(len(s) - b): tmp = tmp - s[i] + s[i + b] mx = max(tmp, mx) print(mx + b * (b - 1) // 2) else: l = len(s) print(sum(s) + b * l - l * (l + 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for j in range(t): n, k = list(map(int, input().split())) mushrooms = list(map(int, input().split())) sum = [0] for i in range(1, n + 1): sum.append(mushrooms[i - 1] + sum[i - 1]) if k > n: print(sum[n] + k * n - n * (n + 1) // 2) else: max_subarray = 0 for i in range(k, n + 1): max_subarray = max(max_subarray, sum[i] - sum[i - k]) print(max_subarray + k * (k - 1) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
read = lambda: map(int, input().strip().split()) def f(a, k): if k >= len(a): rem = k - len(a) base = rem + 1 ans = a[0] + rem for i in a[1:]: ans += i + base base += 1 return ans else: s = sum(a[:k]) i = 0 j = k ans = s while j < len(a): s -= a[i] s += a[j] j += 1 i += 1 ans = max(ans, s) k -= 1 ans += k * (k + 1) // 2 return ans for _ in range(int(input())): n, k = read() a = list(read()) print(f(a, k))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) for i in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) if k >= n: ans = n * (n + 1) // 2 ans += (k - 1 - n) * n ans += sum(a) print(ans) else: pre = [0] + a.copy() for i in range(1, n + 1): pre[i] += pre[i - 1] ma = 0 for i in range(0, n + 1 - k): su = pre[i + k] - pre[i] ma = max(ma, su) num = k - 1 ans = num * (num + 1) // 2 ans += ma print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def read_nums(): return [int(x) for x in input().split()] def solve(): n, k = read_nums() nums = read_nums() if k >= n: return sum(nums) + n * (2 * k - n - 1) // 2 cum_nums = get_cum_sum(nums) largest_sum = 0 for i in range(n - k + 1): largest_sum = max(largest_sum, cum_nums[i + k] - cum_nums[i]) return largest_sum + k * (k - 1) // 2 def get_cum_sum(nums): cum_sum = 0 cum_nums = [0] for num in nums: cum_sum += num cum_nums.append(cum_sum) return cum_nums def main(): (t,) = read_nums() for _ in range(t): print(solve()) main()
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
n = int(input()) mas = 0 for i in range(n): mas = 0 s = 0 x, y = list(map(int, input().split())) l = list(map(int, input().split())) if y < x: mas += sum(l[:y]) s = mas for v in range(y, x): s += l[v] - l[v - y] mas = max(mas, s) mas += y * (y - 1) / 2 else: mas += sum(l) + (len(l) - 1) * (len(l) / 2) + x * (y - x) print(int(mas))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN 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 IF VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
for h in range(int(input())): n, k = map(int, input().split()) x = list(map(int, input().split())) if k >= n: a = sum(x) b = k // n * n * n - n * (n + 1) // 2 k = k % n c = n * k print(a + b + c) else: for i in range(1, n): x[i] += x[i - 1] x.append(0) m = 0 for i in range(k - 1, n): m = max(m, x[i] - x[i - k]) print(m + k * (k - 1) // 2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
for _ in range(int(input())): n, k = map(int, input().split()) a = [*map(int, input().split())] if k >= n: print(sum(a) + n * k - n * (n + 1) // 2) else: window = (((k - 1) % n) ** 2 + (k - 1) % n) / 2 l, r, i, ans = 0, 0, 0, 0 while r < n: window += a[r] if r - l == k: window -= a[l] l += 1 r += 1 ans = max(ans, window) print(int(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) if n == 1: out = a[0] + (k - 1) elif k < n: maxw = 0 s = 0 for i in range(n): s += a[i] if i >= k: s -= a[i - k] if i >= k - 1: maxw = max(maxw, s) out = maxw + (k - 1) * k // 2 elif k == n: out = sum(a) + (k - 1) * k // 2 else: out = sum(a) share = k // (n - 1) r = k % (n - 1) out += (r - 1) * r // 2 out += (r - 1) * r out += (3 * r + n - 3) * (n - r) // 2 out += (share - 1) * (n - 1) * n print(out) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
import sys def solve(): inp = sys.stdin.readline n, k = map(int, inp().split()) a = list(map(int, inp().split())) m = min(n, k) s = 0 for i in range(m): s += a[i] best = s for i in range(m, n): s += a[i] s -= a[i - m] best = max(best, s) print(best + m * (m - 1) // 2 + (k - m) * m) def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
def solve(a, b, c): s = 0 u = 0 for i in range(a): u += c[i] if b <= i: u -= c[i - b] s = max(s, u) return s + (b - 1) * b // 2 for i in range(int(input())): a, b = map(int, input().split()) c = list(map(int, input().split())) if a >= b: print(solve(a, b, c)) else: print(solve(a, a, c) + (b - a) * a)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached. —Perfect Memento in Strict Sense Marisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by $n$ points on the $X$-axis numbered $1$ through $n$. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by $a_1,a_2,\ldots,a_n$. Marisa can start out at any point in the forest on minute $0$. Each minute, the followings happen in order: She moves from point $x$ to $y$ ($|x-y|\le 1$, possibly $y=x$). She collects all mushrooms on point $y$. A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute $0$. Now, Marisa wants to know the maximum number of mushrooms she can pick after $k$ minutes. -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 2 \cdot 10 ^ 5$, $1\le k \le 10^9$) — the number of positions with mushrooms and the time Marisa has, respectively. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the initial number of mushrooms on point $1,2,\ldots,n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$. -----Output----- For each test case, print the maximum number of mushrooms Marisa can pick after $k$ minutes. -----Examples----- Input 4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 12 37 1000000 5000349985 -----Note----- Test case 1: Marisa can start at $x=2$. In the first minute, she moves to $x=1$ and collect $5$ mushrooms. The number of mushrooms will be $[1,7,2,3,4]$. In the second minute, she moves to $x=2$ and collects $7$ mushrooms. The numbers of mushrooms will be $[2,1,3,4,5]$. After $2$ minutes, Marisa collects $12$ mushrooms. It can be shown that it is impossible to collect more than $12$ mushrooms. Test case 2: This is one of her possible moving path: $2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ It can be shown that it is impossible to collect more than $37$ mushrooms.
t = int(input()) while t: n, k = map(int, input().split()) l = list(map(int, input().split())) if n == 1: print(l[0] + k - 1) elif k <= n: ans = 0 su = 0 for i in range(k): su += l[i] ans = su for i in range(k, n): su += l[i] - l[i - k] ans = max(ans, su) print(ans + k * (k - 1) // 2) elif k > n: print((k - 1) * n + sum(l) - n * (n - 1) // 2) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER