description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() if arr[0] != 1: print(0) elif n == 1: print(1) else: t = arr.count(1) for i in range(t, -1, -1): if i + i - 1 > n: continue tag = 0 for j in range(i - 1, 2 * i - 1): if arr[j] > j - i + 2: tag = 1 break if tag == 0: print(i) break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for dm1 in range(int(input())): n = int(input()) nmbs = [int(dm2) for dm2 in input().split()] bck = [0] * 102 for i in nmbs: bck[i] += 1 if bck[1] < 2: print(bck[1]) continue k, flag = 2, [1] while k <= n: if bck[k] == 0: bck[flag[-1]] -= 1 if bck[flag[-1]] == 1: z = flag.pop() bck[k] = 1 if bck[k] >= 2: flag.append(k) if bck[1] < k: break k += 1 print(k - 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER LIST NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
a = int(input()) for i in range(a): b = int(input()) c1 = [0] * b c2 = dict() c = list(map(int, input().split())) for i in range(b): if c[i] not in c2.keys(): c2[c[i]] = 1 else: c2[c[i]] += 1 if 1 not in c2.keys(): print(0) else: c1[0] = 1 c2[1] -= 1 k = 1 for i in range(2, b): if k >= c2[1] + 1: break if i in c2.keys(): if k >= i - 1: c1[k : k + c2[i]] = [i] * c2[i] k += c2[i] else: k += 1 c2[1] -= 1 elif c1[i - 1] == 0: k += 1 c2[1] -= 1 print(min(c2[1] + 1, k))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
n = int(input()) l = [] for _ in range(n): x = int(input()) nn = [int(i) for i in input().split()] nn.sort() nn.append(0) o = nn.count(1) i = 1 su = 0 a = 0 t = 0 while i <= o and 2 * i - 2 < len(nn) - 1: a += nn[2 * i - 2] + nn[2 * i - 3] if nn[2 * i - 2] > i or a > i - 1 + int((i + 1) * i / 2): break else: t += 1 i += 1 l.append(t) for x in l: print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
z = int(input()) for i in range(z): integer_number = int(input()) num_array = sorted(list(map(int, input().split()))) num_of_1 = 0 for j in num_array: if j == 1: num_of_1 += 1 else: break current_max = num_of_1 k = num_of_1 l = 2 while k <= 2 * current_max - 2: try: if num_array[k] <= l: k += 1 l += 1 else: current_max -= 1 l += 1 except: current_max -= 1 l += 1 print(current_max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
n = int(input()) for i in range(n): N = 0 m = int(input()) list1 = list(map(int, input().split())) list1.sort() if m % 2 == 0: list1.pop(-1) m -= 1 k = int((m + 1) / 2) l = 0 j = k for i in range(k): list2 = [] if j == 1: if list1[0] == 1: print(1) else: print(0) else: if list1.count(1) >= j: for f in range(j - 1): list2.append([f + 2, list1[f + j]]) for f in range(j - 1): if list2[f][0] < list2[f][1]: break elif f == j - 2: print(j) N = 1 if N: break j -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
def check(k: int) -> bool: l, r = 0, n - 1 i = 0 while i < k and l <= r: while l <= r and arr[r] > k - i: r -= 1 if arr[r] > k - i or r < l: break r -= 1 l += 1 i += 1 return i >= k for _ in range(int(input())): n = int(input()) arr = list(sorted(map(int, input().split()))) l, r = -1, n + 1 while l + 1 < r: m = (l + r) // 2 if check(m): l = m else: r = m print(l)
FUNC_DEF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) k = a.count(1) m = 1 pos = 0 a.sort() a.append(n + 1) l, r = 0, k + 1 while r - l > 1: flag = True M = (l + r) // 2 for i in range(M - 1, M + M - 1): if a[i] > i - M + 2: flag = False break if flag: l = M else: r = M print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for x in range(t): n = int(input()) S = list(map(int, input().split())) d = dict() d[1] = 0 for i in S: if i in d: d[i] += 1 else: d[i] = 1 Lsum = [] sum = 0 for i in range(1, d[1] + 1): if i in d: sum += d[i] Lsum.append(sum - i + 1) count = 0 for m in range(1, d[1] + 1): for i in range(m): if Lsum[i] < m: count += 1 if count != 0: print(m - 1) break if count != 0: break if count == 0: print(d[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for f in range(int(input())): n = int(input()) l = list(map(int, input().split())) l = [l.count(x) for x in range(1, int(n) + 1)] z = 0 for i in range(l[0]): z = l[0] - i g = i for k in l[1:z]: g += k - 1 if g < 0: break else: break print(z)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for i in range(0, t): n = int(input()) m = [int(i) for i in input().split()] k = m.count(1) j = k u = 2 while u <= k: j = j + m.count(u) - 1 u += 1 if j < k: k = j print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
R = lambda: map(int, input().split()) L = lambda: list(R()) for _ in range(int(input())): (n,) = R() l = sorted(L()) k = (n + 1) // 2 while k > 0: prev = 0 for i in range(k): prev += l.count(i + 1) if prev < k + i: break else: break k -= 1 else: print(0) continue print(k)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for _ in range(int(input())): n = int(input()) arr = [int(t) for t in input().split()] arr.sort() ind = min(arr.count(1), (n + 1) // 2) - 1 while ind > 0: flag = 0 for j in range(ind, 2 * ind + 1): if arr[j] > j - ind + 1: flag = 1 if flag == 1: ind -= 1 continue break print(ind + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) l = map(int, input().split()) l = sorted(l) k = 1 f = 1 while f: l1 = l[:] for j in range(k - 1): del l1[0] coun = 0 for j in range(k): coun += l1.count(j + 1) if coun < j + 1: f = 0 break k += 1 print(k - 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
n = int(input()) for i in range(n): u = int(input()) x = [int(a) for a in input().split()] x.sort() if x.count(1) == 0: print(0) elif x.count(1) == 1: print(1) else: for c in range((u + 1) // 2, -1, -1): for d in range(c): if c + d - 1 >= u: break if x[c + d - 1] <= d + 1: y = 0 else: y = 1 break if y == 0: print(c) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) while t: n = int(input()) ans = 0 a = [0] a += list(map(int, input().split())) a.sort() for k in range(1, n + 2): l = 0 r = n flag = 1 for i in range(1, k + 1): while a[r] > k - i + 1: r -= 1 if l == r: break if l == r: break r -= 1 if i == k: flag = 0 if l == r: break l += 1 if l == r: break if flag: ans = k - 1 break print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for _ in range(int(input())): n = int(input()) num = list(map(int, input().split())) num.sort() l, r, ans = 0, n, 0 def ck(m): res = [] k = m for i in num: if i <= m: res.append(i) while len(res) > 0 and k > 0: res.pop() if len(res) > 0: res[0] += k res.sort() k -= 1 while res and res[-1] > k: res.pop() return True if k == 0 else False if n == 1: print(1) else: while l <= r: m = (l + r) // 2 if ck(m): ans = max(ans, m) l = m + 1 else: r = m - 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for v in range(t): m = int(input()) j = input().split() h = [int(i) for i in j] h.sort() count = 0 while True: val = 1 check = 0 temp = h[count : count * 2 + 1] if temp == [] or len(temp) != count + 1: break for k in temp: if k <= val: val += 1 else: check = 1 break if check == 0: count += 1 else: break print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) a = sorted([int(x) for x in input().split()]) k = 0 while True: if 2 * k - 1 <= n and all([(a[k - 1 + i] <= 1 + i) for i in range(k)]): k += 1 else: k -= 1 break print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
a = int(input()) for i in range(a): b = int(input()) c = list(map(int, input().split())) d = 0 if 1 in c: d += 1 c.pop(c.index(1)) while 1 in c: c.pop(c.index(1)) for j in range(d + 1, 0, -1): if j in c: d += 1 c.pop(c.index(j)) break print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Alice and Bob are playing a game. They have an array of positive integers $a$ of size $n$. Before starting the game, Alice chooses an integer $k \ge 0$. The game lasts for $k$ stages, the stages are numbered from $1$ to $k$. During the $i$-th stage, Alice must remove an element from the array that is less than or equal to $k - i + 1$. After that, if the array is not empty, Bob must add $k - i + 1$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $k$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $k$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the array $a$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). -----Output----- For each test case, print one integer β€” the maximum value of $k$ such that Alice can win if both players play optimally. -----Examples----- Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1 Output 2 0 1 3 -----Note----- None
for i in range(int(input())): n = int(input()) lis = sorted(list(map(int, input().split()))) for j in range(1, n + 1): num = n - j + 1 out = -(-(n - j + 1) // 2) if 2 * lis[-j] - 1 <= num and lis.count(1) >= out: if sum(lis[:-j]) + lis[-j] <= (1 + out) * out // 2 + num - out: print(out) break else: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: def getMaxSpace(cuts, upperLimit): cuts = sorted(cuts) if not any(cuts): return upperLimit ans = cuts[0] for i in range(len(cuts) - 1): ans = max(ans, cuts[i + 1] - cuts[i]) return max(ans, upperLimit - cuts[-1]) maxHorSpace = getMaxSpace(horizontalCuts, h) maxVertSpace = getMaxSpace(verticalCuts, w) mod = 1000000000.0 + 7 return int(maxHorSpace * maxVertSpace % mod)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() print(horizontalCuts, verticalCuts) pre = 0 hori_max = 0 for i, cut in enumerate(horizontalCuts): hori_max = max(hori_max, cut - pre) pre = cut hori_max = max(hori_max, h - pre) pre = 0 vert_max = 0 for i, cut in enumerate(verticalCuts): vert_max = max(vert_max, cut - pre) pre = cut vert_max = max(vert_max, w - pre) return hori_max * vert_max % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: mod = 10**9 + 7 horizontalCuts.extend([0, h]) horizontalCuts.sort() verticalCuts.extend([0, w]) verticalCuts.sort() hor_max = 0 for i in range(len(horizontalCuts) - 1): hor_max = max(hor_max, horizontalCuts[i + 1] - horizontalCuts[i]) ver_max = 0 for i in range(len(verticalCuts) - 1): ver_max = max(ver_max, verticalCuts[i + 1] - verticalCuts[i]) return hor_max * ver_max % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea(self, h: int, w: int, hc: List[int], v: List[int]) -> int: hc.append(0) hc.append(h) v.append(0) v.append(w) v.sort() hc.sort() max_v = max_h = 0 for i in range(1, len(hc)): max_h = max(max_h, hc[i] - hc[i - 1]) for i in range(1, len(v)): max_v = max(max_v, v[i] - v[i - 1]) return max_h * max_v % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.append(h) verticalCuts.append(w) def get_max_gap(array): array.sort() max_hor_width = array[0] for previous_cut, next_cut in zip(array, array[1:]): max_hor_width = max(max_hor_width, next_cut - previous_cut) return max_hor_width return get_max_gap(horizontalCuts) * get_max_gap(verticalCuts) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea(self, h: int, w: int, hc: List[int], vc: List[int]) -> int: hc, vc = [0] + sorted(hc) + [h], [0] + sorted(vc) + [w] return ( max(hc[i] - hc[i - 1] for i in range(1, len(hc))) * max(vc[i] - vc[i - 1] for i in range(1, len(vc))) % 1000000007 )
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() def maxDelta(cuts, end): cuts.sort() cuts.append(end) cuts.append(0) md = 0 for i in range(len(cuts) - 1): md = max(md, cuts[i] - cuts[i - 1]) return md return maxDelta(horizontalCuts, h) * maxDelta(verticalCuts, w) % (10**9 + 7) mh = maxDelta(horizontalCuts, h) mv = maxDelta(verticalCuts, w) return mh * mv % (10 ^ 9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea(self, h: int, w: int, hori: List[int], verti: List[int]) -> int: hori.sort() verti.sort() x = hori[0] for i in range(1, len(hori)): x = max(x, hori[i] - hori[i - 1]) x = max(x, h - hori[-1]) y = verti[0] for i in range(1, len(verti)): y = max(y, verti[i] - verti[i - 1]) y = max(y, w - verti[-1]) return x * y % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def getMax(self, max: int, cuts: List[int]) -> int: last = 0 for x in cuts: cur = x - last if cur > max: max = cur last = x return max def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: mod = 1000000007 horizontalCuts.sort() verticalCuts.sort() maxh = self.getMax(h - horizontalCuts[-1], horizontalCuts) maxv = self.getMax(w - verticalCuts[-1], verticalCuts) return maxh % mod * maxv % mod
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: mod = int(1000000000.0) + 7 return ( self.getMax(horizontalCuts, h) % mod * (self.getMax(verticalCuts, w) % mod) % mod ) def getMax(self, cuts, size): if len(cuts) == 1: return max(cuts[0], size - cuts[0]) cuts.sort() max_cut_size = max(cuts[0], size - cuts[-1]) for index in range(1, len(cuts)): if cuts[index] - cuts[index - 1] > max_cut_size: max_cut_size = cuts[index] - cuts[index - 1] return max_cut_size
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() hr = [0] + horizontalCuts + [h] vr = [0] + verticalCuts + [w] hmax, vmax = 0, 0 for i in range(1, len(hr)): hmax = max(hmax, hr[i] - hr[i - 1]) for i in range(1, len(vr)): vmax = max(vmax, vr[i] - vr[i - 1]) return hmax * vmax % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.append(0) horizontalCuts.append(h) verticalCuts.append(0) verticalCuts.append(w) horizontalCuts.sort() verticalCuts.sort() mw = 0 mh = 0 for x, y in zip(horizontalCuts, horizontalCuts[1:]): mw = max(mw, y - x) for x, y in zip(verticalCuts, verticalCuts[1:]): mh = max(mh, y - x) mod = 10**9 + 7 return mw * mh % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: h_max = self.get_max_interval(w, sorted(verticalCuts)) w_max = self.get_max_interval(h, sorted(horizontalCuts)) return h_max * w_max % (10**9 + 7) def get_max_interval(self, length, cuts): current = 0 max_cut = 0 for cut in cuts: max_cut = max(max_cut, cut - current) current = cut max_cut = max(max_cut, length - current) return max_cut
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: new_horizontal_cuts = [0] + sorted(horizontalCuts) + [h] new_vertical_cuts = [0] + sorted(verticalCuts) + [w] mod = int(1000000000.0) + 7 max_height = 0 max_width = 0 for j in range(len(new_vertical_cuts) - 1): if max_width < new_vertical_cuts[j + 1] - new_vertical_cuts[j]: max_width = new_vertical_cuts[j + 1] - new_vertical_cuts[j] for i in range(len(new_horizontal_cuts) - 1): if max_height < new_horizontal_cuts[i + 1] - new_horizontal_cuts[i]: max_height = new_horizontal_cuts[i + 1] - new_horizontal_cuts[i] return max_width * max_height % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: ver = [0] + sorted(verticalCuts) + [w] hor = [0] + sorted(horizontalCuts) + [h] dh = max(hor[i] - hor[i - 1] for i in range(1, len(hor))) dv = max(ver[i] - ver[i - 1] for i in range(1, len(ver))) return dh * dv % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: return ( self.get_max_length(h, horizontalCuts) * self.get_max_length(w, verticalCuts) % 1000000007 ) def get_max_length(self, length: int, cuts: List[int]) -> int: if len(cuts) == 1: return max(cuts[0], length - cuts[0]) cuts.sort() max_length = max(cuts[0], length - cuts[-1]) for i in range(1, len(cuts)): max_length = max(max_length, cuts[i] - cuts[i - 1]) return max_length
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() ph, pw = 0, 0 msh, msw = 0, 0 for ch in horizontalCuts: if ch - ph > msh: msh = ch - ph ph = ch if h - ph > msh: msh = h - ph for cw in verticalCuts: if cw - pw > msw: msw = cw - pw pw = cw if w - pw > msw: msw = w - pw return msh * msw % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() max_h, max_w = max(horizontalCuts[0], h - horizontalCuts[-1]), max( verticalCuts[0], w - verticalCuts[-1] ) for i in range(1, len(horizontalCuts)): max_h = max(max_h, horizontalCuts[i] - horizontalCuts[i - 1]) for j in range(1, len(verticalCuts)): max_w = max(max_w, verticalCuts[j] - verticalCuts[j - 1]) return max_w * max_h % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() max_h = max(horizontalCuts[0], h - horizontalCuts[-1]) if len(horizontalCuts) > 1: for i in range(len(horizontalCuts) - 1): if max_h < abs(horizontalCuts[i + 1] - horizontalCuts[i]): max_h = abs(horizontalCuts[i + 1] - horizontalCuts[i]) max_v = max(verticalCuts[0], w - verticalCuts[-1]) if len(verticalCuts) > 1: for i in range(len(verticalCuts) - 1): if max_v < abs(verticalCuts[i + 1] - verticalCuts[i]): max_v = abs(verticalCuts[i + 1] - verticalCuts[i]) return max_h % (10**9 + 7) * (max_v % (10**9 + 7)) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() def getGap(arr, n): prev = 0 i = 0 res = 0 while i < len(arr): res = max(arr[i] - prev, res) prev = arr[i] i += 1 return max(n - prev, res) return getGap(horizontalCuts, h) * getGap(verticalCuts, w) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea(self, h, w, horizontalCuts, verticalCuts): H = [0] + horizontalCuts + [h] W = [0] + verticalCuts + [w] H.sort() W.sort() hmax, wmax = 0, 0 for i in range(1, len(H)): hmax = max(hmax, H[i] - H[i - 1]) for i in range(1, len(W)): wmax = max(wmax, W[i] - W[i - 1]) return hmax * wmax % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() max_hor_width = horizontalCuts[0] for i in range(len(horizontalCuts) - 1): max_hor_width = max( max_hor_width, horizontalCuts[i + 1] - horizontalCuts[i] ) max_hor_width = max(max_hor_width, h - horizontalCuts[len(horizontalCuts) - 1]) max_vert_width = verticalCuts[0] for i in range(len(verticalCuts) - 1): max_vert_width = max(max_vert_width, verticalCuts[i + 1] - verticalCuts[i]) max_vert_width = max(max_vert_width, w - verticalCuts[len(verticalCuts) - 1]) return max_vert_width * max_hor_width % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts, verticalCuts = sorted([0, h] + horizontalCuts), sorted( [0, w] + verticalCuts ) return ( max( [ (after - prev) for after, prev in zip(horizontalCuts[1:], horizontalCuts) ] ) * max( [(after - prev) for after, prev in zip(verticalCuts[1:], verticalCuts)] ) % (10**9 + 7) )
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea(self, h: int, w: int, hh: List[int], vv: List[int]) -> int: hh = sorted(hh) + [h] vv = sorted(vv) + [w] prev = 0 hm = 0 for i in hh: hm = max(hm, i - prev) prev = i prev = 0 vm = 0 for i in vv: vm = max(vm, i - prev) prev = i return vm * hm % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() def maxDelta(cuts, end): cuts.sort() cuts.append(end) cuts.append(0) md = 0 for i in range(len(cuts) - 1): md = max(md, cuts[i] - cuts[i - 1]) return md def getGap(arr, n): prev = 0 i = 0 res = 0 while i < len(arr): res = max(arr[i] - prev, res) prev = arr[i] i += 1 return max(n - prev, res) return maxDelta(horizontalCuts, h) * maxDelta(verticalCuts, w) % (10**9 + 7) mh = maxDelta(horizontalCuts, h) mv = maxDelta(verticalCuts, w) return mh * mv % (10 ^ 9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: if 0 not in horizontalCuts: horizontalCuts.append(0) if h not in horizontalCuts: horizontalCuts.append(h) if 0 not in verticalCuts: verticalCuts.append(0) if w not in verticalCuts: verticalCuts.append(w) horizontalCuts.sort() verticalCuts.sort() i = 0 max_diff = -float("inf") while i in range(len(horizontalCuts) - 1): if horizontalCuts[i + 1] - horizontalCuts[i] > max_diff: max_diff = max(max_diff, horizontalCuts[i + 1] - horizontalCuts[i]) i += 1 j = 0 max_diff1 = -float("inf") while j in range(len(verticalCuts) - 1): if verticalCuts[j + 1] - verticalCuts[j] > max_diff1: max_diff1 = max(max_diff1, verticalCuts[j + 1] - verticalCuts[j]) j += 1 return max_diff * max_diff1 % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.append(0) horizontalCuts.append(h) horizontalCuts.sort() Maxh = max( [ (horizontalCuts[i] - horizontalCuts[i - 1]) for i in range(1, len(horizontalCuts)) ] ) verticalCuts.append(0) verticalCuts.append(w) verticalCuts.sort() Maxv = max( [ (verticalCuts[i] - verticalCuts[i - 1]) for i in range(1, len(verticalCuts)) ] ) return Maxh * Maxv % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: n = len(horizontalCuts) m = len(verticalCuts) A = horizontalCuts[:] B = verticalCuts[:] A.sort() B.sort() A.insert(0, 0) B.insert(0, 0) A.append(h) B.append(w) maxw = 0 maxh = 0 for i in range(1, n + 2): maxh = max(maxh, A[i] - A[i - 1]) for i in range(1, m + 2): maxw = max(maxw, B[i] - B[i - 1]) m = 10**9 + 7 return maxw % m * (maxh % m) % m
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cutΒ and similarly, verticalCuts[j] is the distance from theΒ left of the rectangular cake to the jthΒ vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts.Β Since the answer can be a huge number, return this modulo 10^9 + 7. Β  Example 1: Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3] Output: 4 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area. Example 2: Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1] Output: 6 Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area. Example 3: Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3] Output: 9 Β  Constraints: 2 <= h,Β w <= 10^9 1 <=Β horizontalCuts.length <Β min(h, 10^5) 1 <=Β verticalCuts.length < min(w, 10^5) 1 <=Β horizontalCuts[i] < h 1 <=Β verticalCuts[i] < w It is guaranteed that all elements inΒ horizontalCutsΒ are distinct. It is guaranteed that all elements in verticalCutsΒ are distinct.
class Solution: def maxArea( self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int] ) -> int: horizontalCuts.sort() verticalCuts.sort() def helper(ll, num): ll.append(num) ans = ll[0] for i in range(1, len(ll)): if ll[i] - ll[i - 1] > ans: ans = ll[i] - ll[i - 1] return ans return int( helper(horizontalCuts, h) * helper(verticalCuts, w) % (1000000000.0 + 7) )
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER NUMBER VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degree_{v} and s_{v}, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degree_{v} and s_{v} left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. -----Input----- The first line contains integer n (1 ≀ n ≀ 2^16), the number of vertices in the graph. The i-th of the next lines contains numbers degree_{i} and s_{i} (0 ≀ degree_{i} ≀ n - 1, 0 ≀ s_{i} < 2^16), separated by a space. -----Output----- In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≀ a ≀ n - 1, 0 ≀ b ≀ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. -----Examples----- Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 -----Note----- The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal β€” as "xor".
n = int(input()) v = [] deg1 = [] degsum = 0 has_v = [True] * n for i in range(n): d, s = list(map(int, input().split())) degsum += d if d == 1: deg1.append(i) if d == 0: has_v[i] = False v.append([d, s]) edge = [] while deg1 != []: f = deg1.pop() if has_v[f] is False: continue v[f][0] -= 1 has_v[f] = False t = v[f][1] edge.append([f, t]) v[t][0] -= 1 v[t][1] ^= f if v[t][0] == 1: deg1.append(t) elif v[t][0] == 0: has_v[t] = False print(degsum // 2) for f, t in edge: print(f, t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degree_{v} and s_{v}, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degree_{v} and s_{v} left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. -----Input----- The first line contains integer n (1 ≀ n ≀ 2^16), the number of vertices in the graph. The i-th of the next lines contains numbers degree_{i} and s_{i} (0 ≀ degree_{i} ≀ n - 1, 0 ≀ s_{i} < 2^16), separated by a space. -----Output----- In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≀ a ≀ n - 1, 0 ≀ b ≀ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. -----Examples----- Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 -----Note----- The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal β€” as "xor".
def main(): n = int(input()) ones, l = set(), [] for i in range(n): degree, s = list(map(int, input().split())) if degree == 1: ones.add(i) l.append((degree, s)) res = [] while ones: i = ones.pop() degree_i, j = l[i] degree_j, s = l[j] res.append(" ".join((str(i), str(j)))) if degree_j == 1: ones.remove(j) else: degree_j -= 1 if degree_j == 1: ones.add(j) l[j] = degree_j, s ^ i print(len(res)) print("\n".join(res)) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degree_{v} and s_{v}, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degree_{v} and s_{v} left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. -----Input----- The first line contains integer n (1 ≀ n ≀ 2^16), the number of vertices in the graph. The i-th of the next lines contains numbers degree_{i} and s_{i} (0 ≀ degree_{i} ≀ n - 1, 0 ≀ s_{i} < 2^16), separated by a space. -----Output----- In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≀ a ≀ n - 1, 0 ≀ b ≀ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. -----Examples----- Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 -----Note----- The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal β€” as "xor".
n, st, xors, sumst, buf = int(input()), [], [], 0, [] for i in range(n): s = input().split() st.append(int(s[0])) xors.append(int(s[1])) sumst += st[i] if st[i] == 1: buf.append(i) if sumst % 2 != 0: print("0") return print(sumst // 2) while buf: v = buf.pop() if st[v] == 1: print(str(v) + " " + str(xors[v])) xors[xors[v]] ^= v st[xors[v]] -= 1 if st[xors[v]] == 1: buf.append(xors[v])
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST NUMBER LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degree_{v} and s_{v}, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degree_{v} and s_{v} left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. -----Input----- The first line contains integer n (1 ≀ n ≀ 2^16), the number of vertices in the graph. The i-th of the next lines contains numbers degree_{i} and s_{i} (0 ≀ degree_{i} ≀ n - 1, 0 ≀ s_{i} < 2^16), separated by a space. -----Output----- In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≀ a ≀ n - 1, 0 ≀ b ≀ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. -----Examples----- Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 -----Note----- The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal β€” as "xor".
def main(): n, l, ones = int(input()), [], [] for i in range(n): degree, s = list(map(int, input().split())) l.append((degree, s)) if degree == 1: ones.append(i) res = [] while ones: i = ones.pop() degree, j = l[i] if degree == 1: res.append(" ".join((str(i), str(j)))) degree, s = l[j] s ^= i degree -= 1 l[j] = degree, s if degree == 1: ones.append(j) print(len(res)) print("\n".join(res)) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST 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 IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degree_{v} and s_{v}, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degree_{v} and s_{v} left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. -----Input----- The first line contains integer n (1 ≀ n ≀ 2^16), the number of vertices in the graph. The i-th of the next lines contains numbers degree_{i} and s_{i} (0 ≀ degree_{i} ≀ n - 1, 0 ≀ s_{i} < 2^16), separated by a space. -----Output----- In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≀ a ≀ n - 1, 0 ≀ b ≀ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. -----Examples----- Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 -----Note----- The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal β€” as "xor".
n = int(input()) degrees, mas, masiv = 0, [], [] for i in range(n): degree, s = map(int, input().split()) if degree == 1: masiv.append(i) degrees += degree mas.append([degree, s]) print(degrees // 2) while masiv: last_elem = masiv.pop(-1) relax = mas[last_elem][1] if mas[last_elem][0] != 1: continue mas[last_elem][0] -= 1 mas[last_elem][1] ^= relax mas[relax][0] -= 1 mas[relax][1] ^= last_elem print(relax, last_elem) if mas[relax][0] == 1: masiv.append(relax)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degree_{v} and s_{v}, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degree_{v} and s_{v} left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha. -----Input----- The first line contains integer n (1 ≀ n ≀ 2^16), the number of vertices in the graph. The i-th of the next lines contains numbers degree_{i} and s_{i} (0 ≀ degree_{i} ≀ n - 1, 0 ≀ s_{i} < 2^16), separated by a space. -----Output----- In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≀ a ≀ n - 1, 0 ≀ b ≀ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order. -----Examples----- Input 3 2 3 1 0 1 0 Output 2 1 0 2 0 Input 2 1 1 1 0 Output 1 0 1 -----Note----- The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal β€” as "xor".
n = int(input()) d = [] s = [] sm = 0 alone = [] for i in range(n): de, se = map(int, input().split()) d.append(de) s.append(se) if de == 1: alone.append(i) sm += de print(sm // 2) while alone: v = alone.pop() if d[v] == 1: print(v, s[v]) s[s[v]] ^= v d[s[v]] -= 1 if d[s[v]] == 1: alone.append(s[v])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN 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 EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Let's define $f(x)$ for a positive integer $x$ as the length of the base-10 representation of $x$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that. You are given two arrays $a$ and $b$, each containing $n$ positive integers. In one operation, you do the following: pick some integer $i$ from $1$ to $n$; assign either $f(a_i)$ to $a_i$ or $f(b_i)$ to $b_i$. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. $a_i = b_i$ for all $i$ from $1$ to $n$). What's the smallest number of operations required to make $a$ and $b$ similar to each other? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in each of the arrays. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < 10^9$). The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j < 10^9$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print the smallest number of operations required to make $a$ and $b$ similar to each other. -----Examples----- Input 4 1 1 1000 4 1 2 3 4 3 1 4 2 3 2 9 3 1 100 9 10 75019 709259 5 611271314 9024533 81871864 9 3 6 4865 9503 2 371245467 6 7 37376159 8 364036498 52295554 169 Output 2 0 2 18 -----Note----- In the first testcase, you can apply the digital logarithm to $b_1$ twice. In the second testcase, the arrays are already similar to each other. In the third testcase, you can first apply the digital logarithm to $a_1$, then to $b_2$.
def get_digit(num, ups): for i, upp in enumerate(ups): if num < upp: return i + 1 t = int(input()) ups = [(10**k) for k in range(1, 10)] for _ in range(t): n = int(input()) As = input().split() As = [int(i) for i in As] Bs = input().split() Bs = [int(i) for i in Bs] adict = dict() for num in As: adict[num] = adict.get(num, 0) + 1 bdict = dict() for num in Bs: bdict[num] = bdict.get(num, 0) + 1 for num in adict: if num in bdict: v = min(adict[num], bdict[num]) adict[num] -= v bdict[num] -= v _step = 0 alist = [(0) for i in range(10)] blist = [(0) for i in range(10)] for num in adict: if adict[num] > 0: if num >= 10: d = get_digit(num, ups) alist[d] += adict[num] _step += adict[num] else: alist[num] += adict[num] for num in bdict: if bdict[num] > 0: if num >= 10: d = get_digit(num, ups) blist[d] += bdict[num] _step += bdict[num] else: blist[num] += bdict[num] for i in range(2, 10): d = abs(alist[i] - blist[i]) _step += d print(_step)
FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's define $f(x)$ for a positive integer $x$ as the length of the base-10 representation of $x$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that. You are given two arrays $a$ and $b$, each containing $n$ positive integers. In one operation, you do the following: pick some integer $i$ from $1$ to $n$; assign either $f(a_i)$ to $a_i$ or $f(b_i)$ to $b_i$. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. $a_i = b_i$ for all $i$ from $1$ to $n$). What's the smallest number of operations required to make $a$ and $b$ similar to each other? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in each of the arrays. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < 10^9$). The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j < 10^9$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print the smallest number of operations required to make $a$ and $b$ similar to each other. -----Examples----- Input 4 1 1 1000 4 1 2 3 4 3 1 4 2 3 2 9 3 1 100 9 10 75019 709259 5 611271314 9024533 81871864 9 3 6 4865 9503 2 371245467 6 7 37376159 8 364036498 52295554 169 Output 2 0 2 18 -----Note----- In the first testcase, you can apply the digital logarithm to $b_1$ twice. In the second testcase, the arrays are already similar to each other. In the third testcase, you can first apply the digital logarithm to $a_1$, then to $b_2$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.sort() b.sort() i = 0 j = 0 x = [0] * 10 y = [0] * 10 s = 0 while i < n or j < n: if i < n and j < n: if a[i] == b[j]: i += 1 j += 1 elif a[i] < b[j]: if a[i] < 10: x[a[i]] += 1 else: x[len(str(a[i]))] += 1 s += 1 i += 1 elif a[i] > b[j]: if b[j] < 10: y[b[j]] += 1 else: y[len(str(b[j]))] += 1 s += 1 j += 1 elif i < n: if a[i] < 10: x[a[i]] += 1 else: x[len(str(a[i]))] += 1 s += 1 i += 1 elif j < n: if b[j] < 10: y[b[j]] += 1 else: y[len(str(b[j]))] += 1 s += 1 j += 1 for i in range(2, 10): s += abs(x[i] - y[i]) print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's define $f(x)$ for a positive integer $x$ as the length of the base-10 representation of $x$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that. You are given two arrays $a$ and $b$, each containing $n$ positive integers. In one operation, you do the following: pick some integer $i$ from $1$ to $n$; assign either $f(a_i)$ to $a_i$ or $f(b_i)$ to $b_i$. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. $a_i = b_i$ for all $i$ from $1$ to $n$). What's the smallest number of operations required to make $a$ and $b$ similar to each other? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in each of the arrays. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < 10^9$). The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j < 10^9$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print the smallest number of operations required to make $a$ and $b$ similar to each other. -----Examples----- Input 4 1 1 1000 4 1 2 3 4 3 1 4 2 3 2 9 3 1 100 9 10 75019 709259 5 611271314 9024533 81871864 9 3 6 4865 9503 2 371245467 6 7 37376159 8 364036498 52295554 169 Output 2 0 2 18 -----Note----- In the first testcase, you can apply the digital logarithm to $b_1$ twice. In the second testcase, the arrays are already similar to each other. In the third testcase, you can first apply the digital logarithm to $a_1$, then to $b_2$.
def log10(x): if x < 10: ans = 1 elif x < 100: ans = 2 elif x < 10000: ans = 2 + log10(x // 100) elif x < 100000000: ans = 4 + log10(x // 10000) else: ans = 9 return ans for tcase in range(int(input())): n1 = int(input()) a1 = sorted(map(int, input().split())) b1 = sorted(map(int, input().split())) i, j, a2, b2, ans = 0, 0, [], [], 0 while i < n1 or j < n1: if j >= n1 or i < n1 and a1[i] < b1[j]: x = a1[i] if x > 9: x = log10(x) ans += 1 a2.append(x) i += 1 elif i >= n1 or j < n1 and b1[j] < a1[i]: x = b1[j] if x > 9: x = log10(x) ans += 1 b2.append(x) j += 1 else: i += 1 j += 1 a2.sort() b2.sort() n2, i, j = len(a2), 0, 0 while i < n2 or j < n2: if j >= n2 or i < n2 and a2[i] < b2[j]: if a2[i] > 1: ans += 1 i += 1 elif i >= n2 or j < n2 and b2[j] < a2[i]: if b2[j] > 1: ans += 1 j += 1 else: i += 1 j += 1 print(ans)
FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER LIST LIST NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's define $f(x)$ for a positive integer $x$ as the length of the base-10 representation of $x$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that. You are given two arrays $a$ and $b$, each containing $n$ positive integers. In one operation, you do the following: pick some integer $i$ from $1$ to $n$; assign either $f(a_i)$ to $a_i$ or $f(b_i)$ to $b_i$. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. $a_i = b_i$ for all $i$ from $1$ to $n$). What's the smallest number of operations required to make $a$ and $b$ similar to each other? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in each of the arrays. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < 10^9$). The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j < 10^9$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print the smallest number of operations required to make $a$ and $b$ similar to each other. -----Examples----- Input 4 1 1 1000 4 1 2 3 4 3 1 4 2 3 2 9 3 1 100 9 10 75019 709259 5 611271314 9024533 81871864 9 3 6 4865 9503 2 371245467 6 7 37376159 8 364036498 52295554 169 Output 2 0 2 18 -----Note----- In the first testcase, you can apply the digital logarithm to $b_1$ twice. In the second testcase, the arrays are already similar to each other. In the third testcase, you can first apply the digital logarithm to $a_1$, then to $b_2$.
test_cases = int(input()) for _ in range(test_cases): n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] a.sort() b.sort() mapa = [(0) for i in range(10)] mapb = [(0) for i in range(10)] count = 0 i = 0 j = 0 while j < n and i < n: if a[i] == b[j]: a[i] = 1 b[j] = 1 i += 1 j += 1 elif a[i] < b[j]: i += 1 elif a[i] > b[j]: j += 1 for i in range(n): if a[i] >= 10: a[i] = len(str(a[i])) count += 1 if b[i] >= 10: b[i] = len(str(b[i])) count += 1 mapa[a[i]] += 1 mapb[b[i]] += 1 for i in range(2, 10): count += abs(mapa[i] - mapb[i]) print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's define $f(x)$ for a positive integer $x$ as the length of the base-10 representation of $x$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that. You are given two arrays $a$ and $b$, each containing $n$ positive integers. In one operation, you do the following: pick some integer $i$ from $1$ to $n$; assign either $f(a_i)$ to $a_i$ or $f(b_i)$ to $b_i$. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e. g. $a_i = b_i$ for all $i$ from $1$ to $n$). What's the smallest number of operations required to make $a$ and $b$ similar to each other? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in each of the arrays. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i < 10^9$). The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j < 10^9$). The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. -----Output----- For each testcase, print the smallest number of operations required to make $a$ and $b$ similar to each other. -----Examples----- Input 4 1 1 1000 4 1 2 3 4 3 1 4 2 3 2 9 3 1 100 9 10 75019 709259 5 611271314 9024533 81871864 9 3 6 4865 9503 2 371245467 6 7 37376159 8 364036498 52295554 169 Output 2 0 2 18 -----Note----- In the first testcase, you can apply the digital logarithm to $b_1$ twice. In the second testcase, the arrays are already similar to each other. In the third testcase, you can first apply the digital logarithm to $a_1$, then to $b_2$.
ot = [] for _ in range(int(input())): n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) d1_digits = {(1): 0, (2): 0, (3): 0, (4): 0, (5): 0, (6): 0, (7): 0, (8): 0, (9): 0} d2_digits = {(1): 0, (2): 0, (3): 0, (4): 0, (5): 0, (6): 0, (7): 0, (8): 0, (9): 0} d1 = {} d2 = {} out = 0 for x in l1: if x == 1: continue if x not in d1: d1[x] = 1 else: d1[x] += 1 for x in l2: if x == 1: continue if x in d1: d1[x] -= 1 if d1[x] == 0: del d1[x] elif x not in d2: d2[x] = 1 else: d2[x] += 1 for x, y in d1.items(): k = len(str(x)) if k == 1: d1_digits[x] += y else: d1_digits[k] += y out += y for x, y in d2.items(): k = len(str(x)) if k == 1: d2_digits[x] += y else: d2_digits[k] += y out += y for x, y in d1_digits.items(): m = min(y, d2_digits[x]) out += y - m + (d2_digits[x] - m) ot.append(str(out)) print("\n".join(ot))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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) ans = [(0) for i in range(n)] for i in range(q): l, r = map(int, input().split()) ans[l - 1] += 1 if r != n: ans[r] -= 1 for i in range(1, n): ans[i] = ans[i - 1] + ans[i] sum = 0 ans.sort(reverse=True) for i in range(n): sum += ans[i] * a[i] 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 NUMBER VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN 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
n, q = list(map(int, input().split(" "))) nums = list(map(int, input().split(" "))) q_tokens = [(0) for i in range(n + 2)] for i in range(q): a, b = list(map(int, input().split(" "))) q_tokens[a] += 1 q_tokens[b + 1] -= 1 times_called = [(0) for i in range(n)] suma = 0 for i in range(n): suma += q_tokens[i + 1] times_called[i] = suma sorted_indexes = sorted([x for x in range(n)], key=lambda y: times_called[y], reverse=1) nums = sorted(nums, reverse=1) total_sum = 0 for i in range(n): total_sum += nums[i] * times_called[sorted_indexes[i]] print(total_sum)
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 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 STRING VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP 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
n, q = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() query = [0] * n for i in range(q): l, r = map(int, input().split()) query[l - 1] += 1 if r < len(query): query[r] -= 1 prev = 0 for i in range(len(query)): query[i] += prev prev = query[i] query.sort() print(sum([(arr[i] * query[i]) for i in range(n)]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR 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(i) for i in input().split()] arr = [int(i) for i in input().split()] freq = [0] * (n + 1) for _ in range(q): l, r = [int(i) for i in input().split()] freq[l - 1] += 1 freq[r] -= 1 for i in range(1, n): freq[i] += freq[i - 1] freq.pop(-1) arr.sort() freq.sort() ans = 0 for i in range(n): ans += arr[i] * freq[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER 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 VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL 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
class BIT: def __init__(self, l): self._freq = [0] * l def add_region(self, l, r): self._freq[l] += 1 try: self._freq[r + 1] -= 1 except IndexError: pass def get_freq(self): ans = [] cur_sum = 0 for i in range(len(self._freq)): cur_sum += self._freq[i] ans.append(cur_sum) return ans n, q = map(int, input().split()) iniarr = [int(x) for x in input().split()] freq = BIT(n) for i in range(q): l, r = map(int, input().split()) freq.add_region(l - 1, r - 1) ansfreq = sorted(freq.get_freq(), reverse=True) iniarr.sort(reverse=True) ans = 0 for i in range(n): ans += ansfreq[i] * iniarr[i] print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL 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 FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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
n, q = list(map(int, input().split())) t = [0] + list(map(int, input().split())) p = [0] * (n + 1) for i in range(q): l, r = list(map(int, input().split())) p[l - 1] += 1 p[r] -= 1 for i in range(1, n + 1): p[i] += p[i - 1] t.sort() p.sort() cnt = 0 while p[cnt] == 0: cnt += 1 print(sum(p[i] * t[i] for i in range(cnt, n + 1)))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
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 main(): n, q = map(int, input().split()) a = list(map(int, input().split())) lr_list = [0] * n for i in range(q): l, r = map(int, input().split()) l, r = l - 1, r - 1 lr_list[l] += 1 if r < n - 1: lr_list[r + 1] -= 1 lr_cumul = 0 freq = [] for i in lr_list: lr_cumul += i freq.append(lr_cumul) freq.sort(reverse=True) a.sort(reverse=True) output = sum([(a[i] * freq[i]) for i in range(len(freq))]) print(output) main()
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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL 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 stdin input = stdin.readline n, q = map(int, input().split()) a = sorted(map(int, input().split())) b = [0] * n for i in range(q): l, r = map(int, input().split()) b[l - 1] += 1 if r < n: b[r] -= 1 for i in range(1, n): b[i] += b[i - 1] b.sort() print(sum(a[i] * b[i] for i in range(n)))
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 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 FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR 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, m = map(int, input().split()) lis = sorted(map(int, input().split()), reverse=True) ans = [0] * (200000 + 2) ans1 = [0] * (200000 + 2) ans2 = [0] * (200000 + 2) for i in range(m): a, b = map(int, input().split()) ans[a] += 1 ans1[b] += 1 for i in range(1, 200000 + 2): ans2[i] = ans[i] - ans1[i - 1] + ans2[i - 1] ans2.sort(reverse=True) kk = 0 for i in range(n): kk += ans2[i] * lis[i] print(kk)
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 NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER 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
import sys input = sys.stdin.readline n, q = map(int, input().split()) arr = [int(i) for i in input().split()] arr.sort() maxfreq = [0] * n minfreq = [0] * n for i in range(q): l, r = map(int, input().split()) maxfreq[l - 1] += 1 minfreq[r - 1] += 1 freq = [0] * n val = 0 for i in range(n): val += maxfreq[i] freq[i] += val val -= minfreq[i] freq.sort() sum_ = 0 for i in range(n): sum_ += freq[i] * arr[i] print(sum_)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR 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
n, m = map(int, input().split()) data = list(map(int, input().split())) mat = [list(map(int, input().split())) for _ in range(m)] cnt = [0] * n for start, end in mat: start -= 1 cnt[start] += 1 if end < n: cnt[end] -= 1 prev = 0 for idx in range(n): cnt[idx] += prev prev = cnt[idx] cnt.sort() data.sort() print(sum(a * b for a, b in zip(data, cnt)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL 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
n, q = list(map(int, input().strip().split())) arr = list(map(int, input().strip().split())) arr.sort(reverse=True) b = [0] * (n + 2) z = [0] * (n + 2) s = 0 for x in range(q): l, r = list(map(int, input().strip().split())) b[l] += 1 b[r + 1] -= 1 for x in range(1, n + 1): z[x] = z[x - 1] + b[x] z.sort(reverse=True) for x in range(n): s += z[x] * arr[x] print(s)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER 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 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 n, k = map(int, input().split()) p = [0] * (n + 1) d = sorted(list(map(int, input().split())), reverse=True) for i in range(k): l, r = map(int, input().split()) l -= 1 p[l] += 1 p[r] -= 1 for j in range(len(p) - 1): p[j + 1] += p[j] p = sorted(p, reverse=True) s = 0 for i in range(len(p) - 1): s += p[i] * d[i] print(s)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL 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()) a = list(map(int, input().split())) b = [0] * (n + 1) c = [0] * n for i in range(q): l, r = map(int, input().split()) b[l - 1] += 1 b[r] -= 1 cur = 0 for i in range(n): cur += b[i] c[i] = cur c.sort() a.sort() cur = 0 for i in range(n): cur += a[i] * c[i] print(cur)
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 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 VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN 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) b = [0] * (n + 2) for _ in range(q): l, r = map(int, input().split(" ")) b[l] += 1 b[r + 1] -= 1 op = 0 maxop = 0 c = [0] * (max(n, q) + 1) for bi in b: op += bi c[op] += 1 maxop = max(op, maxop) w = 0 sol = 0 for i in range(maxop): for j in range(c[maxop - i]): sol += a[w] * (maxop - i) w += 1 print(sol)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR 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 STRING VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP 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 get_ints(): return map(int, sys.stdin.readline().split()) n, q = map(int, input().split()) arr = list(map(int, input().split())) dp = [0] * n for _ in range(q): s1, e1 = get_ints() dp[s1 - 1] += 1 if e1 != n: dp[e1] -= 1 c = 0 for i in range(n): c += dp[i] dp[i] = c dp = sorted(dp) arr = sorted(arr) summ = 0 for i in range(n): summ += dp[i] * arr[i] print(summ)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL 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
def main(): n, m = list(map(int, input().split())) aa = sorted(map(int, input().split())) bb = [0] * (n + 1) for _ in range(m): l, r = list(map(int, input().split())) bb[l - 1] += 1 bb[r] -= 1 a = 0 for i, b in enumerate(bb): a += b bb[i] = a del bb[-1] bb.sort() print(sum(a * b for a, b in zip(aa, bb))) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR 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
import sys input = sys.stdin.readline def read(): return map(int, input().split()) n, q = read() a = [0] + list(read()) d = [0] * (n + 2) for _ in range(q): li, ri = read() d[li] += 1 d[ri + 1] -= 1 s = 0 b = [0] * (n + 1) for i in range(1, n + 1): s += d[i] b[i] = s a.sort() b.sort() ans = 0 for i in range(1, n + 1): ans += a[i] * b[i] print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP 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
import sys def main(): import sys data = [int(i) for i in sys.stdin.read().split()] data.reverse() n, q = data.pop(), data.pop() arr = [data.pop() for i in range(n)] arr.sort() S = [(0) for i in range(n + 1)] for i in range(q): l, r = data.pop(), data.pop() S[r] += 1 S[l - 1] -= 1 d = [(0) for i in range(n)] count = 0 for i in reversed(range(n)): count += S[i + 1] d[i] = count d.sort() result = 0 for i in range(n): result += d[i] * arr[i] print(result) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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 VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR 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
from sys import stdin _input = stdin.readline _int, _range = int, range def solution(): n, query_count = [_int(i) for i in _input().split()] arr = [_int(i) for i in _input().split()] query = [0] * n for i in _range(query_count): l, r = [_int(i) for i in _input().split()] l -= 1 r -= 1 query[l] += 1 if r + 1 < n: query[r + 1] -= 1 ans = [0] * n ans[0] = query[0] for i in _range(1, n): ans[i] = ans[i - 1] + query[i] ans.sort(reverse=True) arr.sort(reverse=True) accum = 0 for i in _range(n): accum += ans[i] * arr[i] print(accum) solution()
ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF 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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER 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 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
n, q = map(int, input().split()) numbers = list(map(int, input().split())) arr = [0] * n numbers = sorted(numbers, reverse=True) for i in range(q): l, r = map(int, input().split()) arr[l - 1] += 1 if r < n: arr[r] -= 1 for i in range(1, n): arr[i] += arr[i - 1] arr = sorted(arr, reverse=True) sum = 0 for i in range(n): sum += arr[i] * numbers[i] 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR 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 IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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
a, b = map(int, input().split()) k = sorted(map(int, input().split())) t = [(0) for i in range(a + 1)] for _ in range(b): st, en = map(int, input().split()) t[st - 1] += 1 t[en] -= 1 v = [] ans = out = 0 for i in range(a + 1): ans += t[i] v.append(ans) v.sort() for i in range(a): if v[-1] == 0 or k[-1] == 0: break out += k.pop() * v.pop() print(out)
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 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 LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL 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
def read(): return [int(c) for c in input().split()] def solve(n, q, array, queries): cum = [0] * (n + 1) for a, b in queries: cum[a - 1] += 1 cum[b] -= 1 acc = [] for i, e in enumerate(cum): acc.append(e if i == 0 else acc[-1] + e) return sum(i * j for i, j in zip(sorted(acc[:-1]), sorted(array))) def main(): n, q = read() array = read() queries = [read() for _ in range(q)] ans = solve(n, q, array, queries) print(ans) main()
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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
n, q = map(int, input().split()) l1 = list(map(int, input().split())) l2 = [0] * (n + 1) for i in range(0, q): l, r = map(int, input().split()) l2[l - 1] += 1 l2[r] -= 1 l3 = [] for i in range(0, n + 1): if i == 0: l3.append(l2[0]) else: l3.append(l3[i - 1] + l2[i]) l3.sort() l3 = l3[::-1] l1.sort() l1 = l1[::-1] ans = 0 for i in range(0, n): if l3[i] == 0: break ans += l1[i] * l3[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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
from sys import stdin input = stdin.readline n, q = map(int, input().split()) a = sorted(list(map(int, input().split())), reverse=True) h = [0] * (n + 2) ans = 0 while q: q -= 1 l, r = map(int, input().split()) h[l] += 1 h[r + 1] -= 1 sum = 0 for i in range(len(h)): sum += h[i] h[i] = sum sorted_h = sorted(h, reverse=True) for i in range(len(a)): ans += sorted_h[i] * a[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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR 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()) arr = list(map(int, input().split())) pref = [(0) for _ in range(n)] for _ in range(q): l, r = map(int, input().split()) pref[l - 1] += 1 if r < n: pref[r] -= 1 lol = [] cur = 0 for i in range(n): cur += pref[i] lol.append(cur) lol.sort() arr.sort() ans = 0 for i in range(n - 1, -1, -1): ans += arr[i] * lol[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL 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 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
I = lambda: int(input()) IL = lambda: list(map(int, input().split())) n, q = IL() A = IL() Q = [IL() for _ in range(q)] T = [0] * (n + 2) for a, b in Q: T[a] += 1 T[b + 1] -= 1 for i in range(1, n + 2): T[i] = T[i - 1] + T[i] print(sum(a * b for a, b in zip(sorted(A)[::-1], sorted(T)[::-1])))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL 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 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 FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
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()) ls = [int(a) for a in input().split()] ls1, ls2, ls3 = [0] * n, [0] * n, [0] * n ls.sort(reverse=True) for i in range(q): l, r = map(int, input().split()) ls1[l - 1] += 1 ls2[r - 1] += 1 s = 0 for i in range(n): s += ls1[i] ls3[i] = s s -= ls2[i] ls3.sort(reverse=True) ans = 0 for i in range(n): if ls3[i] == 0: break ans += ls[i] * ls3[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL 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 BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR 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
from sys import * arr = [int(x) for x in stdin.readline().split()] n, q = arr[0], arr[1] a = [int(x) for x in stdin.readline().split()] b = [0] * (n + 2) for i in range(q): arr = [int(x) for x in stdin.readline().split()] l, r = arr[0], arr[1] b[l] += 1 b[r + 1] -= 1 sn = 0 for i in range(1, n + 1): sn += b[i] b[i] = sn a.sort(reverse=True) b.sort(reverse=True) sn = 0 for i in range(n): sn += a[i] * b[i] print(sn)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR 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 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())) p = [] a = [0] * (n + 2) for i in range(q): x, y = map(int, input().split()) p.append([x, y]) a[x] += 1 a[y + 1] -= 1 for i in range(1, n + 1): a[i] += a[i - 1] a = a[0 : n + 1] for i in range(len(a)): a[i] = [a[i], i] a.sort() l.sort() a = a[::-1] l = l[::-1] l.append(0) ans = 0 for i in range(len(a)): ans += a[i][0] * l[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST 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 LIST VAR 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 VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR 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 sys import stdin, stdout n, m = map(int, input().split()) l = [0] * n s = list(map(int, input().split())) for _ in range(m): j = stdin.readline() j, k = map(int, j.split()) l[j - 1] += 1 if k < n: l[k] -= 1 for i in range(1, n): l[i] += l[i - 1] l.sort() s.sort() for i in range(n): l[i] = l[i] * s[i] print(sum(l))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL 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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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
n, q = map(int, input().split()) c = list(map(int, input().split())) c.sort() count = [0] * (n + 1) for b in range(q): j, k = map(int, input().split()) count[j - 1] += 1 count[k] -= 1 for i in range(1, n): count[i] += count[i - 1] count.sort() a = 0 for i in range(n): a += count[i + 1] * c[i] - c[i] + c[i] print(a)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER 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 itertools import sys input = sys.stdin.readline n, q = map(int, input().split()) b = list(sorted(map(int, input().split()))) a = [0] * (n + 1) for _ in range(q): l, r = map(int, input().split()) a[l - 1] += 1 a[r] -= 1 a = list(sorted(itertools.accumulate(a))) print(sum(a[i + 1] * b[i] for i in range(n)))
IMPORT IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR 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
import sys n, q = list(map(int, sys.stdin.readline().split())) l1 = [0] + list(map(int, sys.stdin.readline().split())) l1.sort() b = [] for i in range(0, n + 2): b.append(0) for jj in range(0, q): l, r = list(map(int, sys.stdin.readline().split())) b[l] += 1 b[r + 1] -= 1 for i in range(2, n + 1): b[i] += b[i - 1] b.sort() sum1 = 0 k = len(b) k -= 1 kk = n for i in range(1, n + 1): sum1 += b[k] * l1[kk] k -= 1 kk -= 1 print(sum1)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER 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()) ar = list(map(int, input().split())) fre = [(0) for i in range(n)] while q: l, r = map(int, input().split()) fre[l - 1] += 1 if r != n: fre[r] -= 1 q -= 1 for i in range(1, n): fre[i] += fre[i - 1] print(sum(i * j for i, j in zip(sorted(ar), sorted(fre))))
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 WHILE 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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR 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 = map(int, input().split()) arr = sorted(list(map(int, input().split()))) idxFreq = {} frequency = [(0) for i in range(n + 2)] for i in range(q): l, r = map(int, input().split()) frequency[l] += 1 frequency[r + 1] -= 1 final = [0] for i in range(1, n + 2): final.append(final[-1] + frequency[i]) final.pop() final.pop(0) s = 0 for i in sorted(final, reverse=True): s += i * arr.pop() print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT 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 BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP 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
import sys input = sys.stdin.readline print = sys.stdout.write n, q = map(int, input().split()) ar = list(map(int, input().split())) qs = [map(int, input().split()) for x in range(q)] s = [0] * n def f(l, r): s[l] += 1 if r < n: s[r] -= 1 for x in range(q): a, b = qs[x] a -= 1 f(a, b) d = [s[0]] for x in range(1, n): s[x] += s[x - 1] d.append(s[x]) ar = sorted(ar) d = sorted(d)[::-1] sm = 0 for x in range(n): sm += ar[x] * d[n - x - 1] print(str(sm))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER 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
import sys data = [int(i) for i in sys.stdin.read().split()] data.reverse() def read(): return data.pop() n, q = read(), read() A = [read() for i in range(n)] F = [0] * (n + 5) for i in range(q): l, r = read(), read() F[l] += 1 F[r + 1] -= 1 for i in range(1, n + 1): F[i] += F[i - 1] A.sort() F = sorted(F[1 : n + 1]) ans = 0 for i in range(n): ans += A[i] * F[i] print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR 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 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 ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP 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
n, q = map(int, input().split()) arr = list(map(int, input().split())) arr1 = sorted(arr) freq = [0] * (n + 1) sum1 = 0 for i in range(q): l, r = arr = map(int, input().split()) freq[l - 1] += 1 freq[r] -= 1 for i in range(1, len(freq)): freq[i] = freq[i] + freq[i - 1] freq = sorted(freq[:n]) for i in range(len(arr1)): sum1 = sum1 + freq[i] * arr1[i] print(sum1)
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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR