description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") n, q = INL() X = sorted(INL(), reverse=True) ans = 0 D = {i: (0) for i in range(n)} for _ in range(q): l, r = INL() D[l - 1] += 1 if r < n: D[r] -= 1 for _ in range(1, n): D[_] += D[_ - 1] D = {k: v for k, v in sorted(D.items(), reverse=True, key=lambda item: item[1])} i = 0 for _ in D: ans += D[_] * X[i] i += 1 OPS(ans)
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 FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().split()) a = sorted(list(map(int, input().split())), reverse=True) queries = [] for _ in range(q): queries.append(list(map(int, input().split()))) dp = [0] * (n + 1) for x, y in queries: dp[x - 1] += 1 dp[y] -= 1 for i in range(1, n + 1): dp[i] += dp[i - 1] dp.sort(reverse=True) res = 0 for i in range(n): res += dp[i] * a[i] print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
from sys import stdin, stdout def fastInput(): return stdin.readline() def fastPrint(s): return stdout.write("%s\n" % s) n, q = map(int, fastInput().split()) a = list(map(int, fastInput().split())) queries = [tuple(map(lambda x: int(x) - 1, fastInput().split())) for _ in range(q)] def sumRange(left, right): return psums[right + 1] - psums[left] add = [0] * (n + 1) for left, right in queries: add[left] += 1 add[right + 1] -= 1 freq = [] curSum = 0 for i in range(n): curSum += add[i] freq.append((i, curSum)) freq.sort(key=lambda x: -x[1]) a.sort(reverse=True) best = [0] * n for i, (pos, _) in enumerate(freq): best[pos] = a[i] psums = [0] for val in best: psums.append(psums[-1] + val) ret = 0 for left, right in queries: ret += sumRange(left, right) fastPrint(str(ret))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = [int(_) for _ in input().split()] a = [int(_) for _ in input().split()] de = {i: (0) for i in range(n)} for _ in range(q): a1, b = [int(i) for i in input().split()] de[a1 - 1] += 1 if b < n: de[b] -= 1 for _ in range(1, n): de[_] += de[_ - 1] de = {k: v for k, v in sorted(de.items(), key=lambda item: item[1], reverse=True)} a.sort(reverse=True) t = 0 su = 0 for i in de.keys(): if de[i] == 0: break su += a[t] * de[i] t += 1 print(su)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER 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 BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
def solve(): n, q = list(map(int, input().split())) a = list(map(int, input().split())) a.sort(reverse=True) w = [(0) for i in range(n + 2)] for query in range(q): l, r = list(map(int, input().split())) w[l] += 1 w[r + 1] -= 1 for i in range(2, n + 1): w[i] += w[i - 1] w.sort(reverse=True) maxSum = 0 for i in range(n): maxSum += a[i] * w[i] print(maxSum) def __starting_point(): solve() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
inp = lambda: map(int, input().rstrip().split()) n, q = inp() li = sorted(inp()) a = [0] * (n + 1) for _ in range(q): x, y = inp() a[x - 1] += 1 a[y] -= 1 for i in range(n): a[i + 1] += a[i] a.sort() t = 0 for i in range(n): t += a[i + 1] * li[i] print(t)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
from itertools import accumulate _, q = list(map(int, input().split())) n = list(map(int, input().split())) qs = [] rs = [0] * (len(n) + 1) for _ in range(q): l, r = list(map(int, input().split())) qs.append((l, r)) rs[l - 1] += 1 rs[r] -= 1 prs = list(accumulate(rs))[:-1] d = {} for j, i in zip(sorted(prs), sorted(n)): if j in d: d[j].append(i) else: d[j] = [i] new_arr = [d[x].pop() for x in prs] new_pres = [0] + list(accumulate(new_arr)) print(sum([(new_pres[j] - new_pres[i - 1]) for i, j in qs]))
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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER 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 VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys n, m = map(int, sys.stdin.readline().split()) lis = sorted(list(map(int, sys.stdin.readline().split())), reverse=True) s = [0] * n e = [0] * n for i in range(m): a, b = map(int, sys.stdin.readline().split()) s[a - 1] += 1 e[b - 1] += 1 store = [] start = 0 end = 0 d = 0 for i in range(n): if s[i] and not e[i]: store.append((start - end, d)) start += s[i] d = 1 elif not s[i] and e[i]: d += 1 store.append((start - end, d)) end += e[i] d = 0 elif s[i] and e[i]: store.append((start - end, d)) store.append((s[i] + start - end, 1)) start += s[i] end += e[i] d = 0 else: d += 1 l = 0 ans = 0 store.sort(reverse=True) cumm = [0] * n cumm[0] = lis[0] for i in range(1, n): cumm[i] = cumm[i - 1] + lis[i] for i in range(len(store) - 1): b, c = store[i] if n > l + c - 1 >= l: ans += (cumm[l + c - 1] - cumm[l] + lis[l]) * b l += c print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
from sys import stdin, stdout def update(i, val): while i <= n: BIT[i] += val i = i + (i & -i) def read(i): sm = 0 while i > 0: sm += BIT[i] i = i - (i & -i) return sm n, q = map(int, stdin.readline().split()) lt = [int(x) for x in stdin.readline().split()] lt.sort() BIT = [0] * (n + 1) for _ in range(q): l, r = map(int, stdin.readline().split()) update(l, 1) update(r + 1, -1) nz = [0] * n for i in range(1, n + 1): nz[i - 1] = read(i) nz.sort() ans = 0 for i in range(n - 1, -1, -1): ans += nz[i] * lt[i] stdout.write(str(ans))
FUNC_DEF WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
from itertools import accumulate n, q = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) pf = [0] * n for _ in range(q): l, r = map(int, input().split()) pf[l - 1] += 1 if r != n: pf[r] -= 1 prf = list(accumulate(pf)) prf.sort(reverse=True) ans = 0 for i in range(n): ans += prf[i] * a[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) n, q = I() l = I() ar = [0] * (n + 1) for i in range(q): x, y = I() x -= 1 y -= 1 ar[x] += 1 ar[y + 1] -= 1 for i in range(1, len(ar)): ar[i] += ar[i - 1] ar.sort(reverse=True) l.sort(reverse=True) ans = 0 for i in range(n): ans += l[i] * ar[i] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys input = sys.stdin.buffer.readline def solution(): n, q = map(int, input().split()) l = list(map(int, input().split())) ql = [0] * (n + 1) qa = [] for i in range(q): x, y = map(int, input().split()) qa.append((x, y)) ql[x - 1] += 1 ql[y] -= 1 for i in range(1, len(ql)): ql[i] += ql[i - 1] ql.sort(reverse=True) l.sort(reverse=True) s = 0 for i in range(n): s += l[i] * ql[i] print(s) solution()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
s = input().split() n = int(s[0]) q = int(s[1]) a = input().split() my_dict = [(0) for i in range(n)] for i in range(q): r = input().split() my_dict[int(r[0]) - 1] += 1 if int(r[1]) < n: my_dict[int(r[1])] -= 1 my_dict_upd = [(0) for i in range(n)] v = 0 for i in range(n): v += my_dict[i] my_dict_upd[i] = v my_dict_upd.sort(reverse=True) s = 0 b = [(0) for i in range(n)] for i in range(n): b[i] = int(a[i]) b.sort(reverse=True) for i in range(n): if my_dict_upd[i] == 0: break else: s += my_dict_upd[i] * b[i] print(s)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
input = __import__("sys").stdin.readline n, m = map(int, input().split()) lis = sorted(map(int, input().split()), reverse=True) ans = [0] * (n + 2) for i in range(m): a, b = map(int, input().split()) ans[a] += 1 ans[b + 1] -= 1 for i in range(1, n + 1): ans[i] += ans[i - 1] ans.sort(reverse=True) fin = 0 for i in range(n): fin += lis[i] * ans[i] print(fin)
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
from sys import stdin, stdout input = stdin.readline n, q = map(int, input().split()) l = list(map(int, input().split())) l1 = [0] * (n + 2) for i in range(q): left, right = map(int, input().split()) l1[left] += 1 l1[right + 1] -= 1 for i in range(1, n + 1): l1[i] += l1[i - 1] t = l1[1 : n + 1].copy() t.sort(reverse=True) l.sort(reverse=True) ans = 0 for i in range(n): if t[i] > 0: ans += t[i] * l[i] print(ans)
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().strip().split()) A = list(map(int, input().strip().split())) A.sort() M = [0] * n for i in range(q): l, r = map(int, input().strip().split()) l = l - 1 M[l] = M[l] + 1 if r < n: M[r] = M[r] - 1 for i in range(1, n): M[i] = M[i - 1] + M[i] M.sort() s = 0 for i in range(n): s += M[i] * A[i] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) def update(index, value, array, bi_tree): while index < len(array): bi_tree[index] += value index += index & -index def get_sum(index, bi_tree): ans = 0 while index > 0: ans += bi_tree[index] index -= index & -index return ans def get_range_sum(left, right, bi_tree): ans = get_sum(right, bi_tree) - get_sum(left - 1, bi_tree) return ans def g(x): return x def solve(): N, Q = getInts() A = getInts() A.sort() A = [0] + A queries = [] res = 0 B = [0] * (N + 1) f = [(0) for n in range(N + 1)] for q in range(Q): L, R = getInts() update(L, 1, B, f) update(R + 1, -1, B, f) for i in range(1, N + 1): B[i] = get_sum(i, f) B = B[1:] B.sort() B = [0] + B ans = 0 for j in range(1, N + 1): ans += A[j] * B[j] return ans ans = solve() print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
intin = lambda: map(int, input().split()) Ain = lambda: list(map(int, input().split())) n, q = intin() a = Ain() p = [0] * n for i in range(q): x, y = intin() if y - 1 >= 0: p[y - 1] += 1 if x - 2 >= 0: p[x - 2] -= 1 i = n - 2 while i >= 0: p[i] += p[i + 1] i -= 1 p.sort() a.sort() ans = 0 i = 0 while i < n: ans += a[i] * p[i] i += 1 print(ans)
ASSIGN 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys def main(): n, q = map(int, input().strip().split()) A = list(map(int, input().strip().split())) A.sort() M = [0] * n XX = sys.stdin.read().strip().split() for i in range(0, len(XX), 2): l, r = int(XX[i]), int(XX[i + 1]) l = l - 1 M[l] = M[l] + 1 if r < n: M[r] = M[r] - 1 for i in range(1, n): M[i] = M[i - 1] + M[i] M.sort() s = 0 for i in range(n): s += M[i] * A[i] print(s) main()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys def answer(n, q, a, l, r): freq1 = [(0) for _ in range(n + 2)] for i in range(q): freq1[l[i]] += 1 freq1[r[i] + 1] -= 1 freq2 = [(0) for _ in range(n + 2)] for i in range(1, n + 2): freq2[i] = freq2[i - 1] + freq1[i] freq2.sort(reverse=True) a.sort(reverse=True) ans = 0 for i in range(n): ans += a[i] * freq2[i] return ans def main(): n, q = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) l = [(0) for _ in range(q)] r = [(0) for _ in range(q)] for i in range(q): l[i], r[i] = map(int, sys.stdin.readline().split()) print(answer(n, q, a, l, r)) return main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys input = sys.stdin.readline n, q = map(int, input().split()) a = list(map(int, input().split())) ind = [0] * (n + 1) ind[0] = 1 qu = [] for i in range(q): l, r = map(int, input().split()) qu.append([l - 1, r - 1]) ind[l - 1] += 1 ind[r] -= 1 for i in range(1, n): ind[i] += ind[i - 1] x = [] for i in range(n): x.append([ind[i], i]) x.sort() a.sort() arr = [0] * n for i in range(n - 1, -1, -1): arr[x[i][1]] = a[i] s = [0] * n s[0] = arr[0] for i in range(1, n): s[i] = s[i - 1] + arr[i] ans = 0 for i in range(q): l, r = qu[i] ans = ans + s[r] - s[l] + arr[l] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER 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 LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys stdin = sys.stdin stdout = sys.stdout def getCount(intervals, n): counts = [(0) for i in range(n)] val = [(0) for i in range(n + 1)] for i, j in intervals: val[i] += 1 val[j + 1] -= 1 currentVal = 0 for i, num in enumerate(val[:-1]): currentVal += num counts[i] = currentVal return counts n, q = [int(_) for _ in stdin.readline().split()] arr = [int(_) for _ in stdin.readline().strip().split()] intervals = [[(int(_) - 1) for _ in stdin.readline().strip().split()] for i in range(q)] counts = getCount(intervals, n) counts.sort() arr.sort() ans = 0 for i in range(n): ans += arr[i] * counts[i] stdout.write(f"{ans}\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN 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 FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = list(map(int, input().split())) arr = list(map(int, input().split())) diff = [(0) for i in range(n)] for i in range(q): u, v = list(map(int, input().split())) u -= 1 diff[u] += 1 if v < n: diff[v] -= 1 for i in range(1, n): diff[i] += diff[i - 1] arr.sort() diff.sort() ans = 0 for i in range(n): ans += diff[i] * arr[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().split()) arr = list(map(int, input().split())) arr.sort(reverse=True) freq = [0] * (n + 2) for _ in range(q): x, y = map(int, input().split()) freq[x] = freq[x] + 1 freq[y + 1] = freq[y + 1] - 1 freq = freq[1 : n + 1] for i in range(1, n): freq[i] = freq[i] + freq[i - 1] freq.sort(reverse=True) sum = 0 for j in range(n): sum = sum + freq[j] * arr[j] print(sum)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
def incrementByD(arr, q_arr, n, m, d): sum = [(0) for i in range(n)] for i in range(m): sum[q_arr[i][0]] += d if q_arr[i][1] + 1 < n: sum[q_arr[i][1] + 1] -= d arr[0] += sum[0] for i in range(1, n): sum[i] += sum[i - 1] arr[i] += sum[i] arr.pop(0) return arr n, q = map(int, input().split()) aa = list(map(int, input().split())) q_arr = [] for i in range(q): l, r = map(int, input().split()) q_arr.append([l, r]) arr = [0] * (n + 1) d = 1 m = incrementByD(arr, q_arr, n + 1, q, d) aa.sort() m.sort() ans = 0 for i in range(n): ans += aa[i] * m[i] print(ans)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().strip().split(" ")) a = [0] a[1:] = list(map(int, input().strip().split(" "))) a.sort() virtual = [0] * (n + 2) while q: q -= 1 x, y = map(int, input().strip().split()) virtual[x] += 1 virtual[y + 1] -= 1 start = 0 built = [] for i in range(0, n + 1): start += virtual[i] virtual[i] = start s = 0 virtual.pop() virtual.sort() for i in range(n, 0, -1): if virtual[i] == 0: break else: s += virtual[i] * a[i] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().split()) nums = list(map(int, input().split())) nums = sorted(nums) acum = [0] * len(nums) for i in range(q): l, r = map(int, input().split()) l = l - 1 r = r - 1 acum[l] += 1 if r < n - 1: acum[r + 1] -= 1 for i in range(n)[1:]: acum[i] = acum[i - 1] + acum[i] acum = sorted(acum) s = 0 for i in range(n): s += acum[i] * nums[i] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().split()) arr = list(map(int, input().split())) arr.sort(reverse=True) suf = [(0) for i in range(n + 2)] while q: l, r = map(int, input().split()) suf[l] += 1 suf[r + 1] -= 1 q -= 1 for i in range(1, n + 2): suf[i] += suf[i - 1] suf.sort(reverse=True) ans = 0 for i in range(n): ans += suf[i] * arr[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
def solve(): n, q = list(map(int, input().split(" "))) nums = sorted(map(int, input().split(" "))) arr = [0] * (n + 1) for i in range(q): l, r = list(map(int, input().split(" "))) arr[l - 1] += 1 arr[r] -= 1 index = 0 while index < len(arr) - 1: arr[index + 1] += arr[index] index += 1 arr.pop(len(arr) - 1) arr.sort() index = len(nums) - 1 total = 0 while index >= 0 and arr[index] != 0: total += nums[index] * arr[index] index -= 1 print(total) solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().split()) a = list(map(int, input().split())) c = [0] * (len(a) + 1) for i in range(q): l, r = map(int, input().split()) c[l - 1] += 1 c[r] -= 1 for i in range(1, len(c)): c[i] = c[i - 1] + c[i] del c[-1] a = sorted(a) c = sorted(c) l = 0 for i in range(len(a)): l += a[i] * c[i] print(l)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = input().split() n = int(n) q = int(q) a = [int(x) for x in input().split()] freq = [(0) for x in a] for i in range(q): ranges = [int(x) for x in input().split()] freq[ranges[0] - 1] += 1 if ranges[1] < n: freq[ranges[1]] -= 1 for i in range(1, n): freq[i] += freq[i - 1] a.sort() freq.sort() sum = 0 for i in range(len(freq)): sum += a[i] * freq[i] print(sum)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().split()) L = list(map(int, input().split())) l1 = [0] * n s = 0 while q: l, r = map(int, input().split()) l1[l - 1] = l1[l - 1] + 1 if r < n: l1[r] = l1[r] - 1 q = q - 1 for i in range(1, n): l1[i] = l1[i - 1] + l1[i] L.sort() l1.sort() for i in range(0, n): s += L[i] * l1[i] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys from itertools import accumulate input = lambda: sys.stdin.readline().strip("\r\n") n, q = map(int, input().split()) a = list(map(int, input().split())) ind = [0] * (n + 1) for _ in range(q): l, r = map(int, input().split()) ind[l - 1] += 1 ind[r] += -1 count = list(accumulate(ind))[:n] count.sort() a.sort() ans = 0 for i in range(n): ans += a[i] * count[i] print(ans)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
n, q = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) val = [(0) for _ in range(n + 1)] b = [(0) for i in range(n)] v = 0 for i in range(q): x, y = map(int, input().split()) x -= 1 val[x] += 1 val[y] -= 1 for i in range(n): v += val[i] b[i] = v b.sort(reverse=True) msum = 0 for j in range(n): msum += a[j] * b[j] print(msum)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys input = sys.stdin.buffer.readline def I(): return list(map(int, input().split())) def sieve(n): a = [1] * n for i in range(2, n): if a[i]: for j in range(i * i, n, i): a[j] = 0 return a n, q = I() arr = I() weights = [0] * (n + 1) for i in range(q): l, r = I() weights[l - 1] += 1 weights[r] -= 1 curr = 0 for i in range(n): curr += weights[i] weights[i] = curr idxs = list(range(n)) idxs.sort(key=lambda x: weights[x]) arr.sort() ans = [-1] * n j = n - 1 s = 0 for i in reversed(idxs): ans[i] = arr[j] s += weights[i] * arr[j] j -= 1 print(s)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys input = sys.stdin.buffer.readline def solution(): n, q = map(int, input().split()) l = list(map(int, input().split())) ql = [0] * (n + 1) qa = [] for i in range(q): x, y = map(int, input().split()) qa.append((x, y)) ql[x - 1] += 1 ql[y] -= 1 for i in range(1, len(ql)): ql[i] += ql[i - 1] a = [] for i in range(len(ql)): a.append((ql[i], -i)) ans = [0] * n a.sort(reverse=True) l.sort(reverse=True) for i in range(n): ind = abs(a[i][1]) val = l[i] ans[ind] = val ans.reverse() ans.append(0) ans.reverse() for i in range(1, n + 1): ans[i] += ans[i - 1] s = 0 for i in range(q): l = qa[i][0] - 1 r = qa[i][1] s += ans[r] - ans[l] print(s) solution()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR 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 VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
from sys import * def input(): return stdin.readline()[:-1] def initailze(a): n = len(a) d = [0] * (n + 1) d[0] = a[0] d[n] = 0 for i in range(1, n): d[i] = a[i] - a[i - 1] return d def update(d, l, r, x): d[l] += x d[r + 1] -= x def printarray(a, d): l = [] for i in range(0, len(a)): if i == 0: a[i] = d[i] else: a[i] = d[i] + a[i - 1] l.append(a[i]) return l n, q = map(int, input().split()) orig = [int(i) for i in input().split()] a = [0] * n d = initailze(a) sm = 0 for _ in range(q): x, y = map(int, input().split()) x -= 1 y -= 1 update(d, x, y, 1) fin = printarray(a, d) orig.sort(reverse=True) fin.sort(reverse=True) for i in range(n): sm += fin[i] * orig[i] print(sm)
FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers l_{i}, r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). You need to find for each query the sum of elements of the array with indexes from l_{i} to r_{i}, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. -----Input----- The first line contains two space-separated integers n (1 ≤ n ≤ 2·10^5) and q (1 ≤ q ≤ 2·10^5) — the number of elements in the array and the number of queries, correspondingly. The next line contains n space-separated integers a_{i} (1 ≤ a_{i} ≤ 2·10^5) — the array elements. Each of the following q lines contains two space-separated integers l_{i} and r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n) — the i-th query. -----Output----- In a single line print a single integer — the maximum sum of query replies after the array elements are reordered. 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 3 5 3 2 1 2 2 3 1 3 Output 25 Input 5 3 5 2 4 1 3 1 5 2 3 2 3 Output 33
import sys input = sys.stdin.buffer.readline def prog(): n, q = map(int, input().split()) array = list(map(int, input().split())) array.sort() starts = [(0) for i in range(n + 1)] stops = [(0) for i in range(n + 1)] for i in range(q): start, stop = map(int, input().split()) starts[start] += 1 stops[stop] += 1 appearances = [starts[1]] for i in range(2, n + 1): appearances.append(appearances[-1] + starts[i] - stops[i - 1]) appearances.sort() total_sum = 0 for i in range(n): total_sum += appearances[i] * array[i] print(total_sum) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) p = [i for i in range(1, n)] p.append(0) ans = [] count = [0] * n for i in b: count[i] += 1 for i in a: v = (n - i) % n while count[v] == 0: if count[p[v]] == 0: p[v] = p[p[v]] v = p[v] count[v % n] -= 1 ans.append((v + i) % n) print(" ".join(map(str, ans)))
IMPORT ASSIGN 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 VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
ii = lambda: int(input()) kk = lambda: map(int, input().split()) ll = lambda: list(kk()) n = ii() parents, rank = [-1] * n, [0] * n loc = [i for i in range(n)] def findParent(x): stack = [] curr = x while parents[curr] != -1: stack.append(curr) curr = parents[curr] for v in stack: parents[v] = curr return curr def union(x, y): best = None xP, yP = findParent(x), findParent(y) if rank[x] < rank[y]: best = parents[xP] = yP elif rank[x] > rank[y]: best = parents[yP] = xP else: best = parents[yP] = xP rank[xP] += 1 if values[loc[best]] == 0: loc[best] = loc[xP] if yP is best else loc[yP] a = kk() values = [0] * n tbp = [] for x in kk(): values[x] += 1 for i in range(n): if values[i] == 0: union(i, (i + 1) % n) for v in a: p = loc[findParent((n - v) % n)] tbp.append((v + p) % n) values[p] -= 1 if values[p] == 0: union(p, (p + 1) % n) print(*tbp)
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 VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
n = int(input()) a = map(int, input().split()) d = [0] * n for v in map(int, input().split()): d[v] += 1 p = list(range(1, n)) + [0] r = [] for x in a: v = (n - x) % n while d[v] == 0: if d[p[v]] == 0: p[v] = p[p[v]] v = p[v] r += [(x + v) % n] d[v] -= 1 for i in r: print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR LIST NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR LIST BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
def solve(n, arr_a, arr_b): num_cnt = [0] * n for b in arr_b: num_cnt[b] += 1 sol = [] next_b = [i for i in range(1, n)] + [0] for a in arr_a: b = (n - a) % n while num_cnt[b] == 0: if num_cnt[next_b[b]] == 0: next_b[b] = next_b[next_b[b]] b = next_b[b] num_cnt[b] -= 1 sol.append((a + b) % n) return sol def __starting_point(): n = int(input().strip()) arr_a = list(map(int, input().strip().split(" "))) arr_b = list(map(int, input().strip().split(" "))) sol = solve(n, arr_a, arr_b) print(" ".join(map(str, sol))) __starting_point()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR LIST NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
import sys input = sys.stdin.readline print = sys.stdout.write def binsearch(nums, target): left = 0 right = len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid elif nums[mid] > target: right = mid - 1 elif nums[mid] < target: left = mid + 1 return left n = int(input()) a = [int(z) for z in input().split()] b = [int(z) for z in input().split()] b.sort() occ = [0] * n for i in b: occ[i] += 1 ref = list(range(1, n)) ref += [0] res = [] for i in a: f = (n - i) % n while occ[f] == 0: if occ[ref[f]] == 0: ref[f] = ref[ref[f]] f = ref[f] occ[f] -= 1 res.append((i + f) % n) print(" ".join([str(j) for j in res]))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN 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 VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR LIST NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) has = [0] * n ans = [] for i in range(n): has[b[i]] += 1 child = [i for i in range(1, n)] + [0] for i in a: no = (n - i) % n while has[no] == 0: if has[child[no]] == 0: child[no] = child[child[no]] no = child[no] has[no % n] -= 1 ans.append((no % n + i) % n) 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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR LIST NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
import sys n = int(sys.stdin.readline().strip()) a = list(map(int, sys.stdin.readline().split())) b = list(map(int, sys.stdin.readline().split())) num = [(0) for i in range(n)] for cur in b: num[cur] += 1 next = list(range(+1, n + 1)) next[n - 1] = 0 res = [] for i in range(n): value = (n - a[i]) % n while num[value] == 0: if num[next[value]] == 0: next[value] = next[next[value]] value = next[value] res.append((a[i] + value) % n) num[value] -= 1 print(" ".join(map(str, res)))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR WHILE VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
class MinArray: def __init__(self, n, a, b): self.n = n self.a = a self.b = b self.cnts = [0] * self.n self.par = [i for i in range(self.n)] self.size = [1] * self.n for v in self.b: self.cnts[v] += 1 def find(self, v): while v != self.par[v]: tmp = self.par[v] self.par[v] = self.par[tmp] v = tmp return v def union(self, u, v): u, v = self.find(u), self.find(v) self.par[u] = v def build_tree(self): for i in range(self.n - 1, -1, -1): if self.cnts[i] == 0: self.union(i, (i + 1) % self.n) def get_min(self): self.build_tree() arr = [] for u in self.a: v = self.find((self.n - u) % self.n) arr.append((u + v) % self.n) self.cnts[v] -= 1 if self.cnts[v] == 0: self.union(v, (v + 1) % self.n) print(*arr) n = int(input()) a = list(map(int, input().strip(" ").split(" "))) b = list(map(int, input().strip(" ").split(" "))) ma = MinArray(n, a, b) ma.get_min()
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR 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 FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are given two arrays $a$ and $b$, both of length $n$. All elements of both arrays are from $0$ to $n-1$. You can reorder elements of the array $b$ (if you want, you may leave the order of elements as it is). After that, let array $c$ be the array of length $n$, the $i$-th element of this array is $c_i = (a_i + b_i) \% n$, where $x \% y$ is $x$ modulo $y$. Your task is to reorder elements of the array $b$ to obtain the lexicographically minimum possible array $c$. Array $x$ of length $n$ is lexicographically less than array $y$ of length $n$, if there exists such $i$ ($1 \le i \le n$), that $x_i < y_i$, and for any $j$ ($1 \le j < i$) $x_j = y_j$. -----Input----- The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$, $b$ and $c$. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < n$), where $a_i$ is the $i$-th element of $a$. The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($0 \le b_i < n$), where $b_i$ is the $i$-th element of $b$. -----Output----- Print the lexicographically minimum possible array $c$. Recall that your task is to reorder elements of the array $b$ and obtain the lexicographically minimum possible array $c$, where the $i$-th element of $c$ is $c_i = (a_i + b_i) \% n$. -----Examples----- Input 4 0 1 2 1 3 2 1 1 Output 1 0 0 2 Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1 Output 0 0 0 1 0 2 4
import sys input = sys.stdin.readline def make_tree(n): i = 2 while True: if i >= n * 2: tree = [inf] * i break else: i *= 2 return tree def initialization(a): l = len(tree) // 2 for i in range(l, l + len(a)): tree[i] = a[i - l] for i in range(l - 1, 0, -1): tree[i] = min(tree[2 * i], tree[2 * i + 1]) return def update(i, x): i += len(tree) // 2 tree[i] = x i //= 2 while True: if i == 0: break tree[i] = min(tree[2 * i], tree[2 * i + 1]) i //= 2 return def find(s, t): s += len(tree) // 2 t += len(tree) // 2 ans = inf while True: if s > t: break if s % 2 == 0: s //= 2 else: ans = min(ans, tree[s]) s = (s + 1) // 2 if t % 2 == 1: t //= 2 else: ans = min(ans, tree[t]) t = (t - 1) // 2 return ans n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) inf = 114514810 tree = make_tree(2 * n + 1) cnt = [0] * n x = [inf] * (2 * n) for i in b: cnt[i] += 1 x[i] = i x[i + n] = i + n initialization(x) c = [] for i in a: l = n - i r = l + n - 1 m = find(l, r) % n c.append((i + m) % n) cnt[m] -= 1 if not cnt[m]: update(m, inf) update(m + n, inf) print(*c)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_DEF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_DEF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN 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 NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers(self, quality, wage, K): workers = sorted([float(w) / q, q] for w, q in zip(wage, quality)) res = float("inf") qsum = 0 heap = [] def rec(l, r, heap, newQ): if l > r: heap.insert(l, newQ) return heap m = l + int((r - l) / 2) if heap[m] > newQ: return rec(l, m - 1, heap, newQ) else: return rec(m + 1, r, heap, newQ) for r, q in workers: heap = rec(0, len(heap) - 1, heap, q) qsum += q if len(heap) > K: p = heap.pop(K) qsum += -p if len(heap) == K: res = min(res, qsum * r) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: extra_worker_heap = list() for i in range(len(quality)): heappush(extra_worker_heap, (wage[i] / quality[i], quality[i])) hired_worker_heap = list() hired_quality = 0 wage_to_quality = 0 min_cost = 10000000000.0 while extra_worker_heap: cur_wage_to_quality, worker_quality = heappop(extra_worker_heap) hired_quality += worker_quality heappush(hired_worker_heap, -worker_quality) while len(hired_worker_heap) > K: hired_quality += heappop(hired_worker_heap) if len(hired_worker_heap) == K: min_cost = min(min_cost, hired_quality * cur_wage_to_quality) return min_cost
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: ratios = [(w / q, i) for i, (q, w) in enumerate(zip(quality, wage))] ratios.sort() heap = [] sumq = 0 ans = math.inf for ratio, i in ratios: sumq += quality[i] heapq.heappush(heap, -quality[i]) if len(heap) > K: q = heapq.heappop(heap) sumq += q if len(heap) == K: ans = min(ans, sumq * ratio) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: ratios = [[wi / qi, qi] for wi, qi in zip(wage, quality)] ratios.sort(reverse=True) res = float("inf") quality.sort() for ratio, q in ratios: quality.remove(q) candidates = quality[: K - 1] if len(candidates) < K - 1: break ans = ratio * (q + sum(candidates)) res = min(res, ans) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: n = len(quality) wage_per_qual = [] for i in range(n): wage_per_qual.append(wage[i] / quality[i]) workers = sorted(list(range(n)), key=lambda i: wage_per_qual[i]) max_heap = [] total_quality = 0 min_total = float("inf") for w in workers: if len(max_heap) < K: total_quality += quality[w] heapq.heappush(max_heap, -quality[w]) if len(max_heap) == K: min_total = min(min_total, wage_per_qual[w] * total_quality) elif quality[w] < -max_heap[0]: neg_qual = heapq.heappushpop(max_heap, -quality[w]) total_quality += neg_qual + quality[w] min_total = min(min_total, wage_per_qual[w] * total_quality) return min_total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: return self.heap(quality, wage, K) def greedy(self, quality, wage, K): ans = float("inf") size = len(quality) for captain in range(size): ratio = wage[captain] / quality[captain] prices = [] for worker in range(size): price = ratio * quality[worker] if price < wage[worker]: continue prices.append(price) if len(prices) < K: continue prices.sort() ans = min(ans, sum(prices[:K])) return float(ans) def heap(self, quality, wage, K): workers = sorted((w / q, q, w) for q, w in zip(quality, wage)) ans = float("inf") myHeap = [] qualitySum = 0 for ratio, q, w in workers: heapq.heappush(myHeap, -q) qualitySum += q if len(myHeap) > K: qualitySum += heapq.heappop(myHeap) if len(myHeap) == K: ans = min(ans, ratio * qualitySum) return float(ans)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: workers = [ (wage / quality, wage, quality) for wage, quality in zip(wage, quality) ] workers.sort(key=lambda x: x[0]) heap = [] quality_sum = 0 final_cost = float("inf") for ratio, wage, quality in workers: heapq.heappush(heap, -1 * quality) quality_sum += quality if len(heap) > K: q = heapq.heappop(heap) quality_sum += q if len(heap) == K: cost = ratio * quality_sum final_cost = min(final_cost, cost) return final_cost
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: q_w = [(w / q, q, w) for q, w in zip(quality, wage)] q_w.sort() curr = sum([q_w[i][1] for i in range(K)]) heap = [(-q_w[i][1]) for i in range(K)] heapq.heapify(heap) r = q_w[K - 1][0] res = r * curr for r, q, w in q_w[K:]: curr += q heapq.heappush(heap, -q) curr += heapq.heappop(heap) res = min(res, curr * r) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: workers = [] for q, w in zip(quality, wage): workers.append((w / q, q, w)) workers.sort() ans = float("inf") dui = [] sumq = 0 for ratio, q, w in workers: heapq.heappush(dui, -q) sumq += q if len(dui) > K: sumq = sumq + heapq.heappop(dui) if len(dui) == K: ans = min(ans, ratio * sumq) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: employees = sorted([[w / q, q] for w, q in zip(wage, quality)]) print(employees) res, totalOut = math.inf, 0 h = [] for rate, output in employees: totalOut += output heapq.heappush(h, -output) if len(h) > K: totalOut += heapq.heappop(h) if len(h) == K: res = min(res, totalOut * rate) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: n = len(quality) workers = sorted([w / q, q] for q, w in zip(quality, wage)) que = [] res = math.inf curQuality = 0 for ratio, q in workers: heapq.heappush(que, -q) curQuality += q if len(que) > K: q2 = -heapq.heappop(que) curQuality -= q2 if len(que) == K: res = min(res, curQuality * ratio) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: combine = sorted( [(wage[i] / quality[i], quality[i]) for i in range(len(quality))], key=lambda x: (-x[0], x[1]), ) tc = float("inf") n = len(combine) q = [] heapq.heapify(q) for i in range(len(combine) - 1, -1, -1): heapq.heappush(q, -combine[i][1]) if len(q) > K: heapq.heappop(q) if i <= n - K: tc = min(tc, -combine[i][0] * sum(q)) return tc
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: A = [] for q, w in zip(quality, wage): A.append([q, w / q]) pq, ans = [], float("inf") qSum, ratio = 0, 0 for a in sorted(A, key=lambda x: x[1]): if len(pq) < K: heapq.heappush(pq, [-a[0], a[1]]) qSum += a[0] ratio = a[1] else: negq = heapq.heappop(pq)[0] qSum = qSum + negq + a[0] ratio = a[1] heapq.heappush(pq, [-a[0], a[1]]) if len(pq) == K: ans = min(ans, qSum * ratio) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now we want to hire exactly K workers to form a paid group.  When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions.   Example 1: Input: quality = [10,20,5], wage = [70,50,30], K = 2 Output: 105.00000 Explanation: We pay 70 to 0-th worker and 35 to 2-th worker. Example 2: Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], K = 3 Output: 30.66667 Explanation: We pay 4 to 0-th worker, 13.33333 to 2-th and 3-th workers seperately.   Note: 1 <= K <= N <= 10000, where N = quality.length = wage.length 1 <= quality[i] <= 10000 1 <= wage[i] <= 10000 Answers within 10^-5 of the correct answer will be considered correct.
class Solution: def mincostToHireWorkers( self, quality: List[int], wage: List[int], K: int ) -> float: ratios = [(wi / qi, qi) for wi, qi in zip(wage, quality)] ratios.sort() res = float("inf") pq = [] cur = 0 for i, (ratio, q) in enumerate(ratios): if i >= K - 1: res = min(res, ratio * (q + cur)) cur += q heapq.heappush(pq, -q) if len(pq) > K - 1: cur += heapq.heappop(pq) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 nums = sorted(nums) l, r = 0, len(nums) - 1 res = nums[-1] - nums[0] for i in range(0, 4): res = min(res, nums[-4 + i] - nums[i]) return res
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: nums.sort() return ( min( abs(nums[0] - nums[-4]), abs(nums[2] - nums[-2]), abs(nums[1] - nums[-3]), abs(nums[3] - nums[-1]), ) if len(nums) > 4 else 0 )
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR 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 FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: n = len(nums) nums.sort() i = 3 j = n - 1 if n <= 4: return 0 ans = float("inf") while i >= 0 and j >= 0: ans = min(ans, abs(nums[i] - nums[j])) i -= 1 j -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: nums.sort() if len(nums) <= 4: return 0 res = float("inf") n = len(nums) for i in range(4): res = min(res, nums[n - 4 + i] - nums[i]) return res
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) < 5: return 0 a = sorted(nums) H = len(nums) - 4 L = 0 lowest_diff = None for i in range(4): diff = a[H] - a[L] if lowest_diff == None or lowest_diff > diff: lowest_diff = diff H += 1 L += 1 return lowest_diff
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) < 5: return 0 ns = sorted(nums) mindif = ns[-4] - ns[0] for i in range(1, 4): if ns[i - 4] - ns[i] < mindif: mindif = ns[i - 4] - ns[i] return mindif
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 max_3 = sorted(nums[:4], reverse=True) min_3 = sorted(nums[:4]) for i in range(4, len(nums)): max_3.append(nums[i]) max_3.sort(reverse=True) max_3.pop() min_3.append(nums[i]) min_3.sort() min_3.pop() return min([(max_3[i] - min_3[3 - i]) for i in range(4)])
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 nums.sort() print(nums) cur = max(nums) - min(nums) h1 = nums[0] nums[0] = nums[-1] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[0] = h1 h1, h2 = nums[0], nums[1] nums[0], nums[1] = nums[-1], nums[-1] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[0], nums[1] = h1, h2 h1, h2, h3 = nums[0], nums[1], nums[2] nums[0], nums[1], nums[2] = nums[-1], nums[-1], nums[-1] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[0], nums[1], nums[2] = h1, h2, h3 h1, h2, h3 = nums[-1], nums[-2], nums[-3] nums[-1], nums[-2], nums[-3] = nums[0], nums[0], nums[0] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[-1], nums[-2], nums[-3] = h1, h2, h3 h1, h2 = nums[-1], nums[-2] nums[-1], nums[-2] = nums[0], nums[1] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[-1], nums[-2] = h1, h2 h1 = nums[-1] nums[-1] = nums[0] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[-1] = h1 h1, h2, h3 = nums[0], nums[1], nums[-1] nums[0], nums[1], nums[-1] = nums[2], nums[2], nums[2] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[0], nums[1], nums[-1] = nums[-2], nums[-2], nums[-2] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[0], nums[1], nums[-1] = h1, h2, h3 h1, h2, h3 = nums[0], nums[-1], nums[-2] nums[0], nums[-1], nums[-2] = nums[1], nums[1], nums[1] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[0], nums[-1], nums[-2] = nums[-3], nums[-3], nums[-3] if max(nums) - min(nums) < cur: cur = max(nums) - min(nums) nums[0], nums[-1], nums[-2] = h1, h2, h3 return cur
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: nums.sort() result = nums[-1] - nums[0] x = min(4, len(nums)) for i in range(x): curr = nums[-x + i] - nums[i] result = min(result, curr) return result
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: l = len(nums) biggest = nums[: min(4, l)] smallest = nums[: min(4, l)] for i in range(4, l): num = nums[i] biggest.append(num) smallest.append(num) biggest.remove(min(biggest)) smallest.remove(max(smallest)) if len(biggest) < 4: return 0 bfs = [(sorted(biggest), sorted(smallest), 0)] opt = max(biggest) - min(smallest) while bfs: new_biggest, new_smallest, changes = bfs.pop(0) if changes == 3: opt = min(opt, max(new_biggest) - min(new_smallest)) continue bfs.append((new_biggest[:-1], list(new_smallest), changes + 1)) bfs.append((list(new_biggest), new_smallest[1:], changes + 1)) return opt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: N = len(nums) - 1 if N < 3: return 0 minVal = 2**32 nums.sort() for i in range(4): minVal = min(minVal, nums[N - 3 + i] - nums[i]) return minVal
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 nums.sort() r1 = max(nums[0:-3]) - min(nums[0:-3]) r2 = max(nums[1:-2]) - min(nums[1:-2]) r3 = max(nums[2:-1]) - min(nums[2:-1]) r4 = max(nums[3:]) - min(nums[3:]) return min(r1, r2, r3, r4)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 elif len(nums) == 5: nums_sorted = sorted(nums) diff = [] for i in range(1, len(nums)): diff.append(nums_sorted[i] - nums_sorted[i - 1]) return min(diff) else: largest_four = [] smallest_four = [] for n in nums: if len(largest_four) < 4: heapq.heappush(largest_four, n) heapq.heappush(smallest_four, -n) else: heapq.heappushpop(largest_four, n) heapq.heappushpop(smallest_four, -n) smallest_four = sorted([(-x) for x in smallest_four]) largest_four = sorted(largest_four) diff_arr = [ largest_four[0] - smallest_four[0], largest_four[3] - smallest_four[3], largest_four[1] - smallest_four[1], largest_four[2] - smallest_four[2], ] return min(diff_arr)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, A: List[int]) -> int: if len(A) <= 4: return 0 max_vals = [0] * 4 min_vals = [math.inf] * 4 for a in A: for i in range(4): if a > max_vals[i]: max_vals[i + 1 :] = max_vals[i:-1] max_vals[i] = a break for i in range(4): if a < min_vals[i]: min_vals[i + 1 :] = min_vals[i:-1] min_vals[i] = a break res = math.inf for i in range(4): res = min(res, max_vals[i] - min_vals[3 - i]) return res
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) < 5: return 0 nums = sorted(nums) print(nums) n = len(nums) - 1 return min( nums[n] - nums[3], nums[n - 1] - nums[2], nums[n - 2] - nums[1], nums[n - 3] - nums[0], )
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 nums.sort() l = nums[0:4] r = nums[-4:] p = 0 res = float("inf") while p < 4: res = min(res, r[p] - l[p]) p += 1 return res
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, A: List[int]) -> int: if len(A) <= 4: return 0 A = sorted(A) res = math.inf for i in range(4): res = min(res, A[len(A) - i - 1] - A[3 - i]) return res
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: a = nums if len(a) <= 4: return 0 a.sort() print(a) return min(a[-1] - a[3], a[-2] - a[2], a[-3] - a[1], a[-4] - a[0])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: n = len(nums) if n < 5: return 0 nums.sort() diff = n - 4 ans = math.inf for i in range(0, n - diff): ans = min(ans, nums[i + diff] - nums[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 3: return 0 nums.sort() x = 0 y = len(nums) - 1 arr = [] while x < y: arr.append(nums[x]) arr.append(nums[y]) x += 1 y -= 1 if x == y: arr.append(nums[x]) arr.sort() self.ans = float("inf") self.min_diff(0, len(arr) - 1, arr, 3) return self.ans def min_diff(self, i, j, arr, depth): if depth == 0: self.ans = min(self.ans, arr[j] - arr[i]) return self.min_diff(i + 1, j, arr, depth - 1) self.min_diff(i, j - 1, arr, depth - 1)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: n = len(nums) if n < 5: return 0 l = [] h = [] for c in nums: heapq.heappush(l, -c) if len(l) > 4: heapq.heappop(l) heapq.heappush(h, c) if len(h) > 4: heapq.heappop(h) l = [(-c) for c in l] l.sort() h.sort() return min(h[0] - l[0], h[1] - l[1], h[2] - l[2], h[3] - l[3])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 3: return 0 nums.sort() min_diff = nums[-1] - nums[3] for i in range(3): min_diff = min(min_diff, nums[-2 - i] - nums[3 - i - 1]) return min_diff
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 nums.sort() a1, a2, a3, a4 = nums[:4] print(a1, a2, a3, a4) b4, b3, b2, b1 = nums[-4:] return min(b4 - a1, b3 - a2, b2 - a3, b1 - a4)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 mins, maxs = nums[:4], nums[:4] for num in nums[4:]: mins.append(num) mins.sort() mins.pop() maxs.append(num) maxs.sort(reverse=True) maxs.pop() maxs = maxs[::-1] return min(maxs[i] - mins[i] for i in range(4))
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def _util(self, nums): min_heap, max_heap = [], [] for i, x in enumerate(nums): heapq.heappush(min_heap, (x, i)) if len(min_heap) > 4: heapq.heappop(min_heap) heapq.heappush(max_heap, (-x, i)) if len(max_heap) > 4: heapq.heappop(max_heap) return sorted([x for x, i in set(min_heap + [(-x, i) for x, i in max_heap])]) def minDifference(self, nums: List[int]) -> int: lst = sorted(nums) result = lst[-1] - lst[0] x = min(4, len(lst)) for i in range(x): curr = lst[-x + i] - lst[i] result = min(result, curr) return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) < 5: return 0 max4 = max3 = max2 = max1 = -float("inf") min4 = min3 = min2 = min1 = float("inf") for i in nums: if i > max1: max4 = max3 max3 = max2 max2 = max1 max1 = i elif i > max2 and i <= max1: max4 = max3 max3 = max2 max2 = i elif i > max3 and i <= max2: max4 = max3 max3 = i elif i > max4 and i <= max3: max4 = i if i < min1: min4 = min3 min3 = min2 min2 = min1 min1 = i elif i < min2 and i >= min1: min4 = min3 min3 = min2 min2 = i elif i < min3 and i >= min2: min4 = min3 min3 = i elif i < min4 and i >= min3: min4 = i smallest_diff = max4 - min1 diff = max3 - min2 if smallest_diff > diff: smallest_diff = diff diff = max2 - min3 if smallest_diff > diff: smallest_diff = diff diff = max1 - min4 if smallest_diff > diff: smallest_diff = diff return smallest_diff
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: n = len(nums) if n <= 3: return 0 nums.sort() return min(nums[n - 4 + i] - nums[i] for i in range(4))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR
Given an array nums, you are allowed to choose one element of nums and change it by any value in one move. Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.   Example 1: Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0. Example 2: Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1. Example 3: Input: nums = [6,6,0,1,1,4,6] Output: 2 Example 4: Input: nums = [1,5,6,14,15] Output: 1   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def minDifference(self, nums: List[int]) -> int: if len(nums) <= 4: return 0 else: nums.sort() threeZero = nums[-1] - nums[3] twoOne = nums[-2] - nums[2] oneTwo = nums[-3] - nums[1] zeroThree = nums[-4] - nums[0] return min(threeZero, twoOne, oneTwo, zeroThree)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
— Do you have a wish? — I want people to stop gifting each other arrays. O_o and Another Young Boy An array of $n$ positive integers $a_1,a_2,\ldots,a_n$ fell down on you from the skies, along with a positive integer $k \le n$. You can apply the following operation at most $k$ times: Choose an index $1 \le i \le n$ and an integer $1 \le x \le 10^9$. Then do $a_i := x$ (assign $x$ to $a_i$). Then build a complete undirected weighted graph with $n$ vertices numbered with integers from $1$ to $n$, where edge $(l, r)$ ($1 \le l < r \le n$) has weight $\min(a_{l},a_{l+1},\ldots,a_{r})$. You have to find the maximum possible diameter of the resulting graph after performing at most $k$ operations. The diameter of a graph is equal to $\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$, where $\operatorname{d}(u, v)$ is the length of the shortest path between vertex $u$ and vertex $v$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $1 \le k \le n$). The second line of each test case contains $n$ positive integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer — the maximum possible diameter of the graph after performing at most $k$ operations. -----Examples----- Input 6 3 1 2 4 1 3 2 1 9 84 3 1 10 2 6 3 2 179 17 1000000000 2 1 5 9 2 2 4 2 Output 4 168 10 1000000000 9 1000000000 -----Note----- In the first test case, one of the optimal arrays is $[2,4,5]$. The graph built on this array: $\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$ and $\operatorname{d}(2, 3) = 4$, so the diameter is equal to $\max(2,2,4) = 4$.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) A = sorted((a[i], i) for i in range(n)) if k == n: print(1000000000) continue for i in range(k - 1): a[A[i][1]] = 1000000000 f, g = A[k - 1][0], A[k][0] pairs = [min(a[i], a[i + 1]) for i in range(n - 1)] L_max = [0] R_max = [0] for i in range(n - 1): L_max.append(max(L_max[-1], pairs[i])) R_max.append(max(R_max[-1], pairs[-i - 1])) z = min(max(a[1], R_max[n - 2]), 2 * (f if f != a[0] else g)) z = max(z, min(max(a[-2], L_max[n - 2]), 2 * (f if f != a[-1] else g))) for i in range(1, n - 1): z = max( z, min( max(a[i - 1], a[i + 1], L_max[i - 1], R_max[n - 2 - i]), 2 * (f if f != a[i] else g), ), ) print(z)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
— Do you have a wish? — I want people to stop gifting each other arrays. O_o and Another Young Boy An array of $n$ positive integers $a_1,a_2,\ldots,a_n$ fell down on you from the skies, along with a positive integer $k \le n$. You can apply the following operation at most $k$ times: Choose an index $1 \le i \le n$ and an integer $1 \le x \le 10^9$. Then do $a_i := x$ (assign $x$ to $a_i$). Then build a complete undirected weighted graph with $n$ vertices numbered with integers from $1$ to $n$, where edge $(l, r)$ ($1 \le l < r \le n$) has weight $\min(a_{l},a_{l+1},\ldots,a_{r})$. You have to find the maximum possible diameter of the resulting graph after performing at most $k$ operations. The diameter of a graph is equal to $\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$, where $\operatorname{d}(u, v)$ is the length of the shortest path between vertex $u$ and vertex $v$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $1 \le k \le n$). The second line of each test case contains $n$ positive integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer — the maximum possible diameter of the graph after performing at most $k$ operations. -----Examples----- Input 6 3 1 2 4 1 3 2 1 9 84 3 1 10 2 6 3 2 179 17 1000000000 2 1 5 9 2 2 4 2 Output 4 168 10 1000000000 9 1000000000 -----Note----- In the first test case, one of the optimal arrays is $[2,4,5]$. The graph built on this array: $\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$ and $\operatorname{d}(2, 3) = 4$, so the diameter is equal to $\max(2,2,4) = 4$.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) if k == n: print(10**9) continue b = [] for i in range(n): b.append((a[i], i)) b.sort() i = 0 while k > 1: a[b[i][1]] = 10**9 i += 1 k -= 1 ans = 0 j = i for i in range(1, n): x, y = a[i - 1], a[i] z = b[j][0] * 2 if i - 1 <= b[j][1] <= i: z = b[j + 1][0] * 2 z1 = b[j + 1][0] * 2 ans = max(ans, min(max(x, y), z), min(min(x, y), z1)) print(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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: i = 0 j = 0 A.sort() while i < len(A) and A[i] <= 0: if A[i] % 2 != 0: return False while j < len(A) and A[j] < 0 and 2 * A[j] < A[i]: j += 1 if j >= len(A) or 2 * A[j] > A[i]: return False A[j] = None j += 1 i += 1 while i < len(A) and A[i] is None: i += 1 while i < len(A): while j < len(A) and A[j] < 2 * A[i]: j += 1 if j >= len(A) or A[j] > 2 * A[i]: return False A[j] = None j += 1 i += 1 while i < len(A) and A[i] is None: i += 1 return True
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NONE VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NONE VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR NONE VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NONE VAR NUMBER RETURN NUMBER VAR
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: zcount = 0 pos = [] neg = [] for n in A: if n == 0: zcount += 1 elif n > 0: pos.append(n) else: neg.append(-n) def verifypos(A): count = collections.Counter(A) for x in sorted(A, key=abs): if count[x] == 0: continue if count[2 * x] == 0: return False count[x] -= 1 count[2 * x] -= 1 return True return verifypos(pos) and verifypos(neg)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: negative = [] positive = [] zero_numbers = 0 for a in A: if a < 0: negative.append(abs(a)) elif a > 0: positive.append(a) else: zero_numbers += 1 if zero_numbers % 2: return False negative.sort() positive.sort() return self.helper(negative) and self.helper(positive) def helper(self, nums): while nums: cur_num = nums[0] idx = self.binarySearch(nums, 2 * cur_num) if idx == -1: return False nums.pop(idx) nums.pop(0) if len(nums) == 1: return False return True def binarySearch(self, nums, key): head = 0 tail = len(nums) - 1 while head <= tail: mid = (head + tail) // 2 if nums[mid] > key: tail = mid - 1 elif nums[mid] < key: head = mid + 1 else: return mid return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: count = collections.Counter(A) for a in sorted(count, key=lambda x: abs(x)): if count[a] > count[2 * a]: return False count[2 * a] -= count[a] return True
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER VAR
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: result = True numLen = len(A) C = Counter(A) val = list(C.keys()) val.sort() for i in range(len(val)): if val[i] < 0: if val[i] % 2 == 0: if val[i] / 2 in val and C[val[i] / 2] >= C[val[i]]: C[val[i] / 2] = C[val[i] / 2] - C[val[i]] C[val[i]] = 0 else: result = False elif val[i] * 2 in val and C[val[i] * 2] >= C[val[i]]: C[val[i] * 2] = C[val[i] * 2] - C[val[i]] C[val[i]] = 0 else: result = False nums = list(C.values()) if nums == [0] * len(nums): result = True else: result = False return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: if not A: return True positive_heap = [] negative_heap = [] zero = 0 positive_d = defaultdict(int) negative_d = defaultdict(int) for i in A: if i == 0: zero += 1 elif i < 0: heappush(negative_heap, -i) negative_d[-i] += 1 else: heappush(positive_heap, i) positive_d[i] += 1 if zero % 2 != 0: return False if not self.check(positive_heap, positive_d): return False if not self.check(negative_heap, negative_d): return False return True def check(self, h, d): for _ in range(len(h)): i = heappop(h) if d[i] == 0: continue if 2 * i not in d: return False elif d[2 * i] < d[i]: return False else: d[2 * i] -= d[i] d[i] = 0 return True
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: used = [False] * len(A) A.sort(key=lambda n: abs(n)) bank = collections.Counter(A) for num in A: if bank[num] == 0: continue bank[num] -= 1 if bank.get(2 * num, 0) == 0: return False bank[2 * num] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER NUMBER RETURN NUMBER VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: A.sort() dic = {} for i in A: if i % 2 == 0 and i / 2 in dic: i = i / 2 dic[i] -= 1 if dic[i] == 0: del dic[i] elif i * 2 in dic: i = i * 2 dic[i] -= 1 if dic[i] == 0: del dic[i] else: dic[i] = dic.setdefault(i, 0) + 1 continue return len(dic) == 0
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.   Example 1: Input: A = [3,1,3,6] Output: false Example 2: Input: A = [2,1,2,6] Output: false Example 3: Input: A = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. Example 4: Input: A = [1,2,4,16,8,4] Output: false   Constraints: 0 <= A.length <= 3 * 104 A.length is even. -105 <= A[i] <= 105
class Solution: def canReorderDoubled(self, A: List[int]) -> bool: dic = collections.defaultdict(int) for i in range(len(A)): dic[A[i]] += 1 ki = list(dic.keys()) ki.sort(key=lambda x: abs(x)) for k in ki: if dic[2 * k] < dic[k]: return False dic[2 * k] -= dic[k] dic[k] = 0 return True
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER VAR