description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. -----Input----- The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). -----Output----- On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. -----Examples----- Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 -----Note----- In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
s, n, q, a = input(), int(input()), {}, "" for i in s: q[i] = [q[i][0] + 1, 1] if i in q else [1, 1] if len(q) > n: print(-1) else: for i in range(n - len(q)): o = 0 for j in q: m = (q[j][0] + q[j][1] - 1) // q[j][1] if m > o: o, w = m, j q[w][1] = q[w][1] + 1 for i in q: a += i * q[i][1] o = 0 for i in q: m = (q[i][0] + q[i][1] - 1) // q[i][1] if m > o: o = m print(o, "\n", a)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR DICT STRING FOR VAR VAR ASSIGN VAR VAR VAR VAR LIST BIN_OP VAR VAR NUMBER NUMBER NUMBER LIST NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def middle(arr, n, result): f = False result[1] = arr[0] for i in range(1, n): result[i + 1] = result[i] + arr[i] if arr[i - 1] % 2 != 0: result[i + 1] -= 1 if f == True: if arr[i - 2] % 2 != 0: result[i + 1] += 1 else: result[i + 1] -= 1 f = False if arr[i] == 1: f = True return result def last(arr, n, result): result[1] = arr[0] for i in range(1, n - 1): result[i + 1] = result[i] + arr[i] if arr[i - 1] % 2 != 0: result[i + 1] = result[i + 1] - 1 if n > 1: result[n] = result[n - 1] + arr[n - 1] if arr[n - 2] % 2 != 0: result[n] -= 1 return result mod = 1000000007 test = int(input()) for i in range(test): n = int(input()) arr = list(map(int, input().split())) result = [(0) for _ in range(n + 1)] if arr[n - 1] == 1: result = last(arr, n, result) elif arr[0] != 1: result = middle(arr, n, result) q = int(input()) for i in range(q): r = int(input()) ind = r // n ans = 0 if n == 1: if arr[0] % 2 == 0: ans = (arr[0] - 1) % mod * (r - 1) % mod % mod ans = (ans % mod + arr[0] % mod) % mod else: ans = arr[0] % mod * r % mod % mod elif arr[0] == 1: if r <= n: ans = 1 elif r % n == 1 or r % n == 0: ans = ind % mod elif r > n: ans = (ind % mod + 1) % mod elif arr[n - 1] == 1: ans = ind % mod * result[n] % mod % mod p = int(r % mod - ind % mod * n % mod) % mod ans = (ans % mod + result[p] % mod) % mod else: ans = ind % mod * result[n] % mod % mod if arr[n - 1] % 2 == 0: if r % n == 0: ans = (ans % mod - (ind - 1) % mod) % mod else: ans = (ans % mod - ind % mod) % mod p = int(r % mod - ind % mod * n % mod) % mod ans = (ans % mod + result[p] % mod) % mod print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def get_sums(N, numbers): skip_flag = False final_sum = [] cycle_sum = 0 for i in range(N - 1): if skip_flag: skip_flag = False continue if numbers[i + 1] == 1 and i < N - 2: if numbers[i] % 2: cycle_sum += numbers[i] final_sum.extend([cycle_sum, cycle_sum]) else: cycle_sum += numbers[i] - 1 final_sum.extend([cycle_sum + 1, cycle_sum + 2]) skip_flag = True else: final_sum.append(cycle_sum + numbers[i]) cycle_sum += numbers[i] - numbers[i] % 2 final_sum.append(cycle_sum + numbers[N - 1]) cycle_sum += numbers[N - 1] - (numbers[N - 1] - 1) % 2 return final_sum, cycle_sum % 1000000007 for _ in range(int(input())): N = int(input()) numbers = tuple(map(int, input().split())) Q = int(input()) if numbers[0] == 1: if N == 1: for j in range(Q): query = int(input()) print(query % 1000000007) else: for j in range(Q): query = int(input()) print((1 + max(query - 2, 0) // N) % 1000000007) else: final_sum, cycle_sum = get_sums(N, numbers) for j in range(Q): query = int(input()) if not query % N: print(((query - 1) // N * cycle_sum + final_sum[N - 1]) % 1000000007) else: print((query // N * cycle_sum + final_sum[query % N - 1]) % 1000000007)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER 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 IF VAR NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
m = 1000000007 T = int(input()) while T: N = int(input()) A = list(map(int, input().split())) onePos = -1 if A[0] == 1: Q = int(input()) while Q: R = int(input()) if R % N == 0: print(int(R / N) % m) elif R % N == 1 and R > N: print(int(R / N) % m) else: print((int(R / N) + 1) % m) Q -= 1 else: arr = list() chef = 0 for i in range(N - 1): if A[i + 1] == 1 and i + 1 != N - 1: onePos = i + 1 if not A[i] & 1: chef += A[i] - 1 arr.append(chef) else: chef += A[i] arr.append(chef) elif not A[i] & 1: chef += A[i] arr.append(chef) else: chef += A[i] - 1 arr.append(chef) if A[N - 1] % 2 == 0: chef += A[N - 1] - 1 else: chef += A[N - 1] Q = int(input()) while Q: R = int(input()) if R % N == 0: if not A[N - 1] & 1: print((int(R / N) * chef + 1) % m) else: print(int(R / N) * chef % m) elif onePos == R % N - 1: if not A[onePos - 1] & 1: print((int(R / N) * chef + 2 + arr[R % N - 1]) % m) else: print((int(R / N) * chef + arr[R % N - 1]) % m) elif onePos - 1 == R % N - 1: if not A[onePos - 1] & 1: print((int(R / N) * chef + arr[R % N - 1] + 1) % m) else: print((int(R / N) * chef + arr[R % N - 1]) % m) elif A[R % N - 1] % 2 != 0: print((int(R / N) * chef + arr[R % N - 1] + 1) % m) else: print((int(R / N) * chef + arr[R % N - 1]) % m) Q -= 1 T -= 1
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
from sys import stdin input = stdin.readline def func1(l, qq, modulus): if l[0] % 2 == 0: for rr in qq: print(((rr - 1) * (l[0] - 1) + l[0]) % modulus) else: for rr in qq: print(rr * l[0] % modulus) return def func2(nn, l, qq, modulus): mm = [None] * nn res = 0 for i in range(nn): mm[i] = res + l[i] if i != nn - 1: if l[i] % 2 == 0: res = res + l[i] else: res = res + l[i] - 1 elif l[i] % 2 == 0: res = res + l[i] - 1 else: res = res + l[i] for rr in qq: QQ = (rr - 1) // nn RR = (rr - 1) % nn print((res * QQ + mm[RR]) % modulus) return def func3(nn, qq, modulus): for rr in qq: if rr == 1: print(1) continue if (rr - 1) % nn == 0: print((rr - 1) // nn % modulus) else: print(((rr - 1) // nn + 1) % modulus) return def func4(nn, l, qq, modulus): mm = [None] * nn res = 0 for i in range(nn): mm[i] = res + l[i] if i != nn - 1: if l[i] % 2 == 0: res = res + l[i] else: res = res + l[i] - 1 elif l[i] % 2 == 0: res = res + l[i] - 1 else: res = res + l[i] for rr in qq: QQ = (rr - 1) // nn RR = (rr - 1) % nn print((res * QQ + mm[RR]) % modulus) return def solve2(nn, l, qq): modulus = 10**9 + 7 if nn == 1: func1(l, qq, modulus) return if 1 not in l: func2(nn, l, qq, modulus) return if l[0] == 1: func3(nn, qq, modulus) return if l[nn - 1] == 1: func4(nn, l, qq, modulus) return res = 0 xx = l.index(1) mm = [None] * nn i = 0 while i <= xx - 2: mm[i] = res + l[i] if l[i] % 2 == 1: res = res + l[i] - 1 else: res = res + l[i] i = i + 1 mm[i] = res + l[i] if l[i] % 2 == 0: res = res + l[i] - 1 else: res = res + l[i] i = i + 1 if l[i - 1] % 2 == 1: mm[i] = mm[i - 1] else: mm[i] = mm[i - 1] + 1 i = i + 1 if i != nn - 1: mm[i] = res + l[i] if l[i] % 2 == 1: res = res + l[i] - 1 else: res = res + l[i] i = i + 1 else: mm[i] = res + l[i] if l[i] % 2 == 0: res = res + l[i] - 1 else: res = res + l[i] i = i + 1 while i < nn: mm[i] = res + l[i] if i == nn - 1: if l[i] % 2 == 1: res = res + l[i] else: res = res + l[i] - 1 i = i + 1 continue if l[i] % 2 == 1: res = res + l[i] - 1 else: res = res + l[i] i = i + 1 for rr in qq: QQ = (rr - 1) // nn RR = (rr - 1) % nn print((res * QQ + mm[RR]) % modulus) return def solve1(): nn = input() nn = int(nn) l = list(map(int, input().split())) queries = input() queries = int(queries) qq = [] for xx in range(queries): qq.append(int(input())) solve2(nn, l, qq) def solve(): test_cases = input() test_cases = int(test_cases) for t in range(test_cases): solve1() solve()
ASSIGN VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_DEF FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR RETURN FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
import sys input = sys.stdin.readline MOD = 10**9 + 7 for _ in range(int(input())): n = int(input()) data = list(map(int, input().split())) n = len(data) ans1 = ans2 = ans3 = ans4 = case = 0 lastone = -1 for i in range(n): if data[i] == 1: lastone = i case = data[i] if case: if 1 not in (data[-1], data[0]): case = 1 for i in range(lastone - 1): ans1 += MOD + data[i] - 1 if data[i] & 1 else data[i] ans1 += ( data[lastone - 1] if data[lastone - 1] & 1 else MOD + data[lastone - 1] - 1 ) for i in range(lastone + 1, n - 1): ans1 += MOD + data[i] - 1 if data[i] & 1 else data[i] ans1 += data[n - 1] if data[n - 1] & 1 else MOD + data[n - 1] - 1 ans1 %= MOD elif data[0] == 1: case = 2 ans2 = 1 elif data[-1] == 1: case = 3 for i in range(n - 1): ans3 += MOD + data[i] - 1 if data[i] & 1 else data[i] ans3 += 1 ans3 %= MOD else: case = 4 for i in range(n - 1): ans4 += MOD + data[i] - 1 if data[i] & 1 else data[i] ans4 += data[n - 1] if data[n - 1] & 1 else MOD + data[n - 1] - 1 ans4 %= MOD ans = [0] * n dp = [0] * n rcount = [0] * n for i in range(n): if data[i] & 1: rcount[i] += 1 for i in range(1, n): rcount[i] += rcount[i - 1] if case in (1, 3, 4): for i in range(n): ans[i] = data[i] if i == 0 else ans[i - 1] + data[i] dp[0] = ans[0] for i in range(1, n): dp[i] = (ans[i - 1] - rcount[i - 1] + data[i] + MOD) % MOD if i > lastone and lastone != -1: dp[i] += 1 if data[lastone - 1] & 1 else -1 dp[i] %= MOD for q in range(int(input())): r = int(input()) quo = (r - 1) // n rem = r - quo * n - 1 final = 0 if case in (1, 3, 4): final = dp[rem] % MOD final += quo * (ans1 if case == 1 else ans3 if case == 3 else ans4) % MOD else: final = r // n if r % n: final += 1 if r % n - 1 == lastone and r // n > 0: final += -1 + MOD print((final + MOD) % MOD)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR IF NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) u = 10**9 + 7 q = int(input()) if l[0] == 1: for i in range(q): m = int(input()) s = 0 if m % n <= 1: s = m // n % u if s == 0: s = 1 else: s = (m // n + 1) % u print(s % 1000000007) else: dp = [0] * n dp[0] = l[0] for i in range(1, n): dp[i] = dp[i - 1] + l[i] if l[i - 1] % 2 != 0 and l[i - 1] > 1: dp[i] -= 1 elif l[i - 1] == 1: if l[i - 2] % 2 == 0: dp[i] -= 2 for i in range(q): m = int(input()) s = 0 if m <= n: s = dp[m - 1] % u elif m % n == 0: s = dp[-1] % u * (m // n % u) % u if l[-1] % 2 == 0: s -= m // n - 1 else: s = dp[-1] % u * (m // n % u) % u if l[-1] % 2 == 0: s -= m // n s = (s + dp[m % n - 1] % u) % u print(s % 1000000007)
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 BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
m = 10**9 + 7 t = int(input()) for testcase in range(t): n = int(input()) a = list(map(int, input().split())) pos1 = -1 if n == 1: q = int(input()) for qq in range(q): r = int(input()) if a[0] % 2 == 1: print(r * a[0] % m) else: print((r * (a[0] - 1) % m + 1) % m) elif a[0] == 1: pos1 = 0 q = int(input()) for qq in range(q): r = int(input()) full = r // n part = r % n res = full % m if part > 1: res = (res + 1) % m elif part == 1 and full == 0: res = (res + 1) % m print(res) elif a[-1] == 1: pos1 = n - 1 fscore = 0 sums = [] for i in range(n - 1): if a[i] % 2 == 0: fscore = (fscore + a[i]) % m else: fscore = (fscore + a[i] - 1) % m sums.append(fscore) fscore = (fscore + 1) % m sums.append(fscore) q = int(input()) for qq in range(q): r = int(input()) full = r // n part = r % n res = full * fscore % m if part == 1: res = (res + a[0]) % m elif part > 1: res = (res + (sums[part - 2] + a[part - 1]) % m) % m print(res) else: fscore = 0 fscore_end = 0 sums = [] partpos = 0 for i in range(n - 1): if a[i] == 1: pos1 = i if a[i - 1] % 2 == 1: partpos = (sums[i - 1] + 1) % m sums[i - 1] = (sums[i - 1] + 1) % m fscore = (fscore + 1) % m else: partpos = (sums[i - 1] + 1) % m sums[i - 1] -= 1 fscore -= 1 if a[i] % 2 == 0: fscore = (fscore + a[i]) % m else: fscore = (fscore + a[i] - 1) % m sums.append(fscore) if a[-1] % 2 == 1: fscore = (fscore + a[-1]) % m else: fscore = (fscore + a[-1] - 1) % m fscore_end = 1 sums.append(fscore) if pos1 != -1: q = int(input()) for qq in range(q): r = int(input()) full = r // n part = r % n res = 0 if part == 0: res = (full * fscore % m + fscore_end) % m else: res = full * fscore % m if part == 1: res = (res + a[0]) % m elif part - 1 == pos1: res = (res + partpos) % m else: res = ((res + sums[part - 2]) % m + a[part - 1]) % m print(res) else: q = int(input()) for qq in range(q): r = int(input()) full = r // n part = r % n res = 0 if part == 0: res = (full * fscore % m + fscore_end) % m else: res = full * fscore % m if part == 1: res = (res + a[0]) % m else: res = ((res + sums[part - 2]) % m + a[part - 1]) % m print(res)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def case1(): pass def case2(): pass def case3(): pass for _ in range(int(input())): mod = 1000000007 c = -1 n = int(input()) a = list(map(int, input().split())) for i in range(len(a)): if a[i] == 1: if i == 0: c = 0 elif i != n - 1: c = i if c == 0: q = int(input()) while q: r = int(input()) if r % n == 0: print(r // n % mod) elif r % n == 1 and r > n: print(r // n % mod) else: print((r // n + 1) % mod) q -= 1 elif c > 0: s = 0 x = [0] * n for i in range(0, n - 1): if i == c - 1: if a[i] % 2 == 0: s += a[i] - 1 x[i] = s else: s += a[i] x[i] = s elif a[i] % 2 == 0: s += a[i] x[i] = s else: s += a[i] - 1 x[i] = s if a[n - 1] % 2 == 0: s += a[n - 1] - 1 else: s += a[n - 1] q = int(input()) while q: r = int(input()) if r % n == 0: if a[n - 1] % 2 == 0: print((r // n * s + 1) % mod) else: print(r // n * s % mod) elif c == r % n - 1: if a[c - 1] % 2 == 0: print((r // n * s + x[r % n - 1] + 2) % mod) else: print((r // n * s + x[r % n - 1]) % mod) elif c - 1 == r % n - 1: if a[c - 1] % 2 == 0: print((r // n * s + x[r % n - 1] + 1) % mod) else: print((r // n * s + x[r % n - 1]) % mod) elif a[r % n - 1] % 2 != 0: print((r // n * s + x[r % n - 1] + 1) % mod) else: print((r // n * s + x[r % n - 1]) % mod) q -= 1 else: s = 0 x = [0] * n for i in range(0, n - 1): if a[i] % 2 == 1: s += a[i] - 1 x[i] = s else: s += a[i] x[i] = s if a[n - 1] % 2 == 0: s += a[n - 1] - 1 else: s += a[n - 1] q = int(input()) while q: r = int(input()) if r % n == 0: if a[n - 1] % 2 == 0: print((r // n * s + 1) % mod) else: print(r // n * s % mod) elif a[r % n - 1] % 2 != 0: print((r // n * s + x[r % n - 1] + 1) % mod) else: print((r // n * s + x[r % n - 1]) % mod) q -= 1
FUNC_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER 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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def SieveOfEratosthenes(n): prime = [(True) for i in range(n + 1)] p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 l = [] for p in range(2, n + 1): if prime[p]: l += [p] return l def i1(): return int(input()) def i2(): return map(int, input().split()) def i3(): return list(map(int, input().split())) def i4(): return input() m = 10**9 + 7 for _ in range(i1()): n = i1() a = i3() x = 0 y = -1 l = [(0) for i in range(100000)] for i in range(n - 1): if a[i] == 1: if i == 0: x = 1 else: x = 2 y = i if x == 1: for i in range(i1()): r = i1() if r % n == 0: print(r // n % m) elif r % n == 1 and r > n: print(r // n % m) else: print((r // n + 1) % m) elif x == 2: s = 0 for i in range(n - 1): if i == y - 1: if a[i] % 2 == 0: s = s + a[i] - 1 else: s += a[i] elif a[i] % 2 == 0: s += a[i] else: s += a[i] - 1 l[i] = s if a[n - 1] % 2 == 0: s += a[n - 1] - 1 else: s += a[n - 1] for i in range(i1()): r = i1() if r % n == 0: if a[n - 1] % 2 == 0: print((r // n * s + 1) % m) else: print(r // n * s % m) elif y == r % n - 1: if a[y - 1] % 2 == 0: print((r // n * s + 2 + l[r % n - 1]) % m) else: print((r // n * s + l[r % n - 1]) % m) elif y - 1 == r % n - 1: if a[y - 1] % 2 == 0: print((r // n * s + l[r % n - 1] + 1) % m) else: print((r // n * s + l[r % n - 1]) % m) elif a[r % n - 1] % 2 != 0: print((r // n * s + l[r % n - 1] + 1) % m) else: print((r // n * s + l[r % n - 1]) % m) else: s = 0 for i in range(n - 1): if a[i] % 2 == 0: s += a[i] else: s += a[i] - 1 l[i] = s if a[n - 1] % 2 == 0: s += a[n - 1] - 1 else: s += a[n - 1] for i in range(i1()): r = i1() if r % n == 0: if a[n - 1] % 2 == 0: print((r // n * s + 1) % m) else: print(r // n * s % m) elif a[r % n - 1] % 2 != 0: print((r // n * s + l[r % n - 1] + 1) % m) else: print((r // n * s + l[r % n - 1]) % m)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR LIST VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
for test in range(int(input())): num = int(input()) arr = [int(x) for x in input().split()] cupid = int(input()) run = list() for i in range(cupid): run.append(int(input())) answer = [] key = False for i in range(num): if i == 0 and arr[i] == 1: answer.append(1) key = True elif key: answer.append(0) elif i + 1 > 0 and i + 1 < num - 1 and arr[i + 1] == 1: if arr[i] % 2 == 0: answer.append(arr[i] - 1) else: answer.append(arr[i]) elif i + 1 == num - 1 and arr[i + 1] == 1: if arr[i] % 2 == 0: answer.append(arr[i]) else: answer.append(arr[i] - 1) elif i == num - 1: if arr[i] % 2 == 0: answer.append(arr[i] - 1) else: answer.append(arr[i]) elif arr[i] % 2 == 0: answer.append(arr[i]) else: answer.append(arr[i] - 1) cookup = list() temp = 0 for i in range(num): temp = (temp + answer[i]) % (10**9 + 7) cookup.append(temp) for i in range(cupid): if run[i] <= num: result = cookup[run[i] - 1] if not key: result = (result - answer[run[i] - 1] + arr[run[i] - 1]) % (10**9 + 7) if arr[run[i] - 1] == 1 and arr[run[i] - 2] % 2 == 0 and run[i] != num: result = (result + 1) % (10**9 + 7) elif ( arr[run[i] - 1] == 1 and arr[run[i] - 2] % 2 == 1 and run[i] != num ): result = result - 1 print(result) else: result = cookup[num - 1] * (run[i] // num % (10**9 + 7)) result = result % (10**9 + 7) if run[i] % num != 0: result = (result + cookup[run[i] % num - 1]) % (10**9 + 7) if not key: result = (result - answer[run[i] % num - 1] + arr[run[i] % num - 1]) % ( 10**9 + 7 ) if ( arr[run[i] % num - 1] == 1 and arr[run[i] % num - 2] % 2 == 0 and run[i] % num != 0 ): result = (result + 1) % (10**9 + 7) elif ( arr[run[i] % num - 1] == 1 and arr[run[i] % num - 2] % 2 == 1 and run[i] % num != 0 ): result = result - 1 if key and run[i] % num == 1: result = result - 1 print(result)
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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def loop6(): i = 3 if i == 3: g = 1 def loop5(): n = int(input()) ar = list(map(int, input().split())) q = int(input()) sol = [0] * n part = ar.copy() p = 10**9 + 7 cnt = 0 loop6() ans = 0 if ar[0] == 1: for i in range(n): sol[i] = 1 cnt = 1 else: for i in range(n): if i < n - 1: if ar[i] % 2 == 0: cnt = (cnt % p + ar[i] % p) % p sol[i] = cnt part[i] = ar[i] elif ar[i] == 1: if ar[i - 1] % 2 == 0: cnt = (cnt - 1) % p part[i - 1] = part[i - 1] - 1 sol[i - 1] = cnt sol[i] = cnt part[i] = 0 else: cnt = (cnt + 1) % p sol[i - 1] = cnt sol[i] = cnt part[i - 1] = part[i - 1] + 1 part[i] = 0 else: cnt = (cnt + ar[i] - 1) % p sol[i] = cnt part[i] = ar[i] - 1 elif i == n - 1: if ar[i] % 2 == 0: cnt += (ar[i] - 1) % p sol[i] = cnt part[i] = ar[i] - 1 else: cnt = (cnt % p + ar[i] % p) % p part[i] = ar[i] sol[i] = cnt for i in range(q): r = int(input()) if ar[0] == 1: if r < n: ans = 1 else: g = r // n f = r % n if f == 1 or f == 0: ans = cnt % p * g % p % p else: ans = cnt % p * g % p % p + 1 elif r < n: ans = sol[r - 1] if ar[r - 1] == 1: if ar[r - 2] % 2 == 0: ans = (sol[r - 1] + 2) % p else: ans = sol[r - 1] % p if part[r - 1] < ar[r - 1]: ans = (ans + 1) % p else: g = r // n f = r % n if f == 0: ans = cnt % p * g % p % p if part[-1] < ar[-1]: ans = (ans + 1) % p else: ans = cnt % p * g % p % p if ar[f - 1] == 1: if ar[f - 2] % 2 == 0: ans = (ans % p + (sol[f - 1] + 2) % p) % p else: ans = (ans % p + sol[f - 1] % p) % p else: ans = (ans % p + sol[f - 1] % p) % p if part[f - 1] < ar[f - 1]: ans = (ans + 1) % p print(ans) def diff(): z = 107 z1 = 10 z2 = z1 * z for i in range(z): z3 = z2 * z1 def loop4(): loop5() def loop3(): loop4() def loop2(): loop3() def loop1(): loop2() for _ in range(int(input())): diff() loop1()
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
t = int(input()) n, a, sum = 0, [], [] def geteven(x): if x & 1: return x - 1 else: return x def getodd(x): if x & 1: return x else: return x - 1 def start1(): if n == 1: return r if r <= n: return 1 if (r - 1) % n == 0: return (r - 1) // n else: return (r - 1) // n + 1 def lessthanN(r): if r == 0: return 0 if a[r - 1] == 1: return sum[r - 2] + geteven(a[r - 2]) + 1 else: return sum[r - 1] + a[r - 1] def normal(r): if r <= n: return lessthanN(r) ans = r // n if n == 1: ans = sum[1] * (ans - 1) + a[0] elif r % n == 0: ans = sum[n] * (ans - 1) + sum[n - 1] + a[n - 1] else: ans = sum[n] * ans ans += lessthanN(r % n) return ans while t > 0: n = int(input()) a = list(map(int, input().split())) sum = [0] * (n + 1) for i in range(1, n + 1): if i == n: sum[i] = getodd(a[i - 1]) elif a[i - 1] != 1: if a[i] == 1 and i + 1 != n: sum[i] = getodd(a[i - 1]) else: sum[i] = geteven(a[i - 1]) sum[i] = sum[i - 1] + sum[i] q = int(input()) while q > 0: r = int(input()) ans = 0 if a[0] == 1: ans = start1() else: ans = normal(r) print(ans % 1000000007) q -= 1 t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER LIST LIST FUNC_DEF IF BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
mod = 10**9 + 7 for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.insert(0, 0) last_round = [0] * (n + 1) q = int(input()) isOneInBeetween = False for i in range(1, n + 1): if a[i] == 1: isOneInBeetween = True break if a[1] != 1: if not isOneInBeetween or a[n] == 1: for i in range(1, n + 1): if i > 1 and a[i - 1] & 1 == 1: last_round[i] = ((last_round[i - 1] - 1) % mod + a[i]) % mod else: last_round[i] = (last_round[i - 1] + a[i]) % mod else: for i in range(1, n + 1): if i > 1: if a[i] == 1 and a[i - 1] & 1 == 0: last_round[i] = (last_round[i - 1] + 1) % mod elif a[i - 1] == 1 and a[i - 2] & 1 == 0: last_round[i] = ((last_round[i - 2] - 1) % mod + a[i]) % mod elif a[i - 1] & 1 == 1 and a[i - 1] != 1: last_round[i] = ((last_round[i - 1] - 1) % mod + a[i]) % mod else: last_round[i] = (last_round[i - 1] + a[i]) % mod else: last_round[i] = a[i] % mod for i in range(q): r = int(input()) if r == 1: print(a[1] % mod) continue if n == 1: if a[1] & 1 == 0: ans = (r - 1) % mod * ((a[1] - 1) % mod) % mod else: ans = (r - 1) % mod * (a[1] % mod) ans = (ans + a[1] % mod) % mod print(ans % mod) continue if a[1] == 1: ans = r % mod // (n % mod) % mod if r % n != 0 and r % n != 1: ans = (ans + 1) % mod print(ans) continue if a[n] & 1 == 1: ans = last_round[n] % mod * (r % mod // (n % mod)) % mod else: ans = (last_round[n] - 1) % mod * (r % mod // (n % mod)) % mod j = r % n if j == 0: if a[n] & 1 == 0: ans = (ans + 1) % mod else: ans = (ans + last_round[j]) % mod print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
try: t = int(input()) for _ in range(t): queries = [] n = int(input()) arr = list(map(int, input().split())) q = int(input()) for a in range(q): r = int(input()) queries.append(r) value = 0 test = [0] * n err = 10**9 + 7 x = 0 if n > 1: if arr[0] != 1: for i in range(n): if arr[i] % 2 == 0: if i == n - 1: test[i] = arr[i] + x - 1 x = test[i] elif i < n - 2 and arr[i + 1] == 1: test[i] = arr[i] + x - 1 x = test[i] else: test[i] = arr[i] + x x = test[i] elif i == n - 1: test[i] = arr[i] + x x = test[i] elif i < n - 2 and arr[i + 1] == 1: test[i] = arr[i] + x x = test[i] else: test[i] = arr[i] + x - 1 x = test[i] for j in range(q): if n == 1: if arr[0] % 2 == 1: value = queries[j] * arr[0] % err else: value = (queries[j] * arr[0] - queries[j] + 1) % err elif arr[0] == 1: div = queries[j] // n rem = queries[j] % n if queries[j] == 1: value = 1 elif rem < 2: value = div % err else: value = (div + 1) % err else: div = queries[j] // n rem = queries[j] % n if rem == 0: if arr[-1] % 2 == 0: value = (div * test[-1] + 1) % err elif arr[-1] % 2 == 1: value = div * test[-1] % err elif arr[rem] == 1 and rem == n - 1 and arr[rem - 1] % 2 == 1: value = (div * test[-1] + test[rem - 1] + 1) % err elif arr[rem - 1] == 1 and arr[rem - 2] % 2 == 0: value = (div * test[-1] + test[rem - 1] + 2) % err elif (arr[rem] == 1 or arr[rem - 1] == 1) and arr[rem - 1] % 2 == 1: value = (div * test[-1] + test[rem - 1]) % err elif arr[rem] == 1 and arr[rem - 1] % 2 == 0 and rem != n - 1: value = (div * test[-1] + test[rem - 1] + 1) % err elif arr[rem] == 1 and arr[rem - 1] % 2 == 0: value = (div * test[-1] + test[rem - 1]) % err elif arr[rem - 1] % 2 == 0: value = (div * test[-1] + test[rem - 1]) % err elif arr[rem - 1] % 2 == 1: value = (div * test[-1] + test[rem - 1] + 1) % err print(value) except EOFError: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
te = int(input()) for tt in range(te): n = int(input()) a = list(map(int, input().split())) aa = [0] * n z = 0 s = 0 t = 0 ss = [0] * n if a[0] == 1: z = 1 ss[0] = 1 s1 = 1 else: p = -1 for i in range(n): if a[i] == 1: p = i break for i in range(n - 1): if i == p - 1: if p != n - 1: if a[i] % 2 == 0: s = (s + a[i] - 1) % 1000000007 aa[i] = a[i] - 1 else: s = (s + a[i]) % 1000000007 aa[i] = a[i] elif a[i] % 2 == 1: s = (s + a[i] - 1) % 1000000007 aa[i] = a[i] - 1 else: s = (s + a[i]) % 1000000007 aa[i] = a[i] elif i == p: aa[i] = a[i] - 1 t = 1 elif a[i] % 2 == 1: s = (s + a[i] - 1) % 1000000007 aa[i] = a[i] - 1 else: s = (s + a[i]) % 1000000007 aa[i] = a[i] ss[i] = s if a[n - 1] == 1: s = (s + a[n - 1]) % 1000000007 aa[n - 1] = 0 elif a[n - 1] % 2 == 1: s = (s + a[n - 1]) % 1000000007 aa[n - 1] = a[n - 1] else: s = (s + a[n - 1] - 1) % 1000000007 aa[n - 1] = a[n - 1] - 1 ss[n - 1] = s s1 = s s2 = t q = int(input()) for i in range(q): r = int(input()) if z == 1: rr = r // n r = r % n if r > 1 and rr > 0 or rr == 0: rr += 1 print(rr % 1000000007) continue s = 0 if r >= n: s = s1 * (r // n) % 1000000007 r = r % n if r > 0: s = (s + ss[r - 1]) % 1000000007 if aa[r - 1] != a[r - 1]: s = s - aa[r - 1] + a[r - 1] if a[r - 1] == 1: if a[r - 2] % 2 == 0: if r == 0: s -= 1 else: s += 1 else: s -= 1 print(s % 1000000007)
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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
try: for t in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()][:n] temp = [] s = 0 if 1 in arr: if arr[0] != 1: for i in range(n): if arr[-1] == 1: s = s + arr[i] if arr[i] % 2 != 0 and i != n - 1: temp.append(s) elif arr[i] % 2 != 0 and i == n - 1: temp.append(s) else: temp.append(s) if arr[i] % 2 != 0: s = s - 1 else: s = s + arr[i] temp.append(s) if arr[i] % 2 != 0 and arr[i] != 1: s = s - 1 elif arr[i] == 1 and arr[i - 1] % 2 != 0: s = s elif arr[i] == 1 and arr[i - 1] % 2 == 0: s = s - 2 for Q in range(int(input())): turn = int(input()) if turn == 0: print("0") elif arr[0] != 1: if turn <= n: print(temp[turn - 1] % (10**9 + 7)) else: div = turn // n rem = turn % n answer = 0 if rem == 0: if arr[-1] % 2 == 0: answer = temp[-1] * div - (div - 1) elif arr[-1] % 2 != 0: answer = temp[-1] * div elif arr[-1] % 2 == 0 and rem != 0: answer = temp[-1] * div - div + temp[rem - 1] else: answer = temp[-1] * div + temp[rem - 1] print(answer % (10**9 + 7)) elif len(arr) != 1: if turn == 0: print("0") elif turn <= len(arr): print("1") else: div = turn // n rem = turn % n if rem == 1 or rem == 0: answer = div else: answer = div + 1 print(answer % (10**9 + 7)) else: answer = arr[0] * turn print(answer % (10**9 + 7)) else: for i in range(n): s = s + arr[i] temp.append(s) if arr[i] % 2 != 0: s = s - 1 for Q in range(int(input())): turn = int(input()) if turn == 0: print("0") elif turn <= n: print(temp[turn - 1] % (10**9 + 7)) else: div = turn // n rem = turn % n answer = 0 if rem == 0: if arr[-1] % 2 == 0: answer = temp[-1] * div - (div - 1) elif arr[-1] % 2 != 0: answer = temp[-1] * div elif arr[-1] % 2 == 0 and rem != 0: answer = temp[-1] * div - div + temp[rem - 1] else: answer = temp[-1] * div + temp[rem - 1] print(answer % (10**9 + 7)) except: pass
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 VAR ASSIGN VAR LIST ASSIGN VAR NUMBER IF NUMBER VAR IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
mod = 1000000007 t = int(input()) for k in range(0, t): n = int(input()) a = [int(i) for i in input().split(" ")] if n == 1: q = int(input()) for h in range(0, q): r = int(input()) if a[0] % 2 == 1: print(a[0] % mod * (r % mod) % mod) else: print(((a[0] - 1) % mod * (r % mod) % mod + 1) % mod) else: ans = {} if a[0] != 1: for i in range(0, n): if i == 0: ans[i] = a[i] elif a[i - 1] % 2 != 1: ans[i] = (ans[i - 1] + a[i] % mod) % mod elif a[i - 1] != 1: ans[i] = (mod + ans[i - 1] - 1 + a[i] % mod) % mod else: ans[i] = (mod + ans[i - 2] - (a[i - 2] + 1) % 2 + a[i] % mod) % mod last = ans[n - 1] if a[n - 1] % 2 == 0: last = (mod + last - 1) % mod q = int(input()) for h in range(0, q): r = int(input()) if a[0] == 1: total = (int((r - 1) / n) % mod + 1) % mod if total != 1 and (r - 1) % n == 0: total = total - 1 print(total) else: total = (int((r - 1) / n) % mod * last % mod + ans[(r - 1) % n]) % mod print(total)
ASSIGN VAR NUMBER 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 STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR DICT IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a = [0] + a dp = [0] * 100005 s = 0 if a[1] != 1: for i in range(1, n + 1): if a[i] & 1: dp[i] = (s + a[i] - 1) % 1000000007 else: dp[i] = (s + a[i]) % 1000000007 if i <= n - 2 and a[i + 1] == 1 or i == n: if a[i] & 1: s = (s + a[i]) % 1000000007 else: s = (s + a[i] - 1) % 1000000007 elif a[i] & 1: s = (s + a[i] - 1) % 1000000007 else: s = (s + a[i]) % 1000000007 if i == n: dp[i] = s q = int(input()) for _ in range(q): r = int(input()) A = 0 A = s * (r // n) % 1000000007 if r % n == 0: A = (A - s + 1000000007) % 1000000007 r = n else: r = r - r // n * n A = (A + dp[r - 1] + a[r]) % 1000000007 print(A) else: q = int(input()) for _ in range(q): r = int(input()) A = 0 A += (r - 1) // n + 1 temp = r - n * (r // n) if temp == 1: A -= 1 A = A % 1000000007 if r == 1: A = 1 print(A)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
import sys def boolean(n): return [0] * n def update(l): for i in range(len(l) - 1): if l[i] != 1 and l[i] & 1: l[i] -= 1 return l def update2(l): d = boolean(n) d[0] = l[0] for i in range(1, len(l)): d[i] += l[i] + d[i - 1] return d pakka = 10**9 + 7 modulo = pakka def check(l, p): for __ in range(int(sys.stdin.readline())): count = 0 x = int(sys.stdin.readline()) qestion = x // n reminder = x % n if reminder != 0: if reminder >= 2: count += p[reminder - 2] % pakka count = count % pakka count += t[reminder - 1] % pakka count = count % pakka elif l[-1] % 2 == 0: count += 1 count = count % pakka if l[-1] & 1: count += p[n - 1] * qestion % pakka count = count % pakka else: count += (p[n - 1] * qestion % modulo - qestion) % pakka count = count % pakka print(count % pakka) def check1(l, p, i): for __ in range(int(sys.stdin.readline())): c = 0 x = int(sys.stdin.readline()) reminder = x % n qestion = x // n c += p[n - 1] * qestion % pakka c = c % pakka if reminder != 0: if reminder >= 2: c += p[reminder - 2] % pakka c = c % pakka c += t[reminder - 1] % pakka c = c % pakka print(c % pakka) def check2(l, p, i): for __ in range(int(sys.stdin.readline())): c = 0 x = int(sys.stdin.readline()) q = x // n r = x % n c += q % pakka c = c % pakka if x == 1: print(1) continue if r > 1: c += 1 c = c % pakka print(c % pakka) def che1ck(l, p, i): sum1 = p[n - 1] sum1 = sum1 % pakka if t[i - 1] & 1: sum1 += 1 sum1 = sum1 % pakka for __ in range(int(sys.stdin.readline())): x = int(sys.stdin.readline()) c = 0 if x < i + 1: c += p[x - 1] % pakka if t[x - 1] & 1: c += 1 % pakka c = c % pakka print(c % pakka) elif x == i + 1: c += p[x - 1] % pakka print(c % pakka) else: qestion = x // n reminder = x % n c += (qestion * sum1 % pakka - qestion) % pakka if l[-1] % 2 == 0: c -= qestion % pakka if t[i - 1] % 2 == 0: c -= qestion % pakka if reminder != 0: if reminder <= i + 1: if reminder >= 2: c += p[reminder - 2] % pakka c = c % pakka c += t[reminder - 1] % pakka c = c % pakka else: if i >= 2: c += p[i - 2] if t[i - 1] % 2 == 1: c += t[i - 1] % pakka c = c % pakka else: c += t[i - 1] - 1 c = c % pakka c += (p[reminder - 2] - p[i]) % pakka c = c % pakka c += t[reminder - 1] % pakka c = c % pakka elif l[-1] % 2 == 0: c += 1 c = c % pakka print(c % pakka) for _ in range(int(sys.stdin.readline())): n = int(sys.stdin.readline()) l = list(map(int, input().split())) t = l.copy() l = update(l) p = update2(l) if 1 not in l: check(l, p) else: i = l.index(1) if i == n - 1: check1(l, p, i) elif i == 0: check2(l, p, i) else: che1ck(l, p, i)
IMPORT FUNC_DEF RETURN BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
import sys MOD = pow(10, 9) + 7 def debug(*args): print(*args, file=sys.stderr) def solve(A): N = len(A) solns = [(0) for _ in range(N)] my_turn = True my_score = 0 foe_score = 0 if A[0] == 1: solns[0] = 1 for i in range(N): even = A[i] odd = even - 1 if even % 2: even, odd = odd, even if A[i] == 1: if my_turn: my_score = (my_score + 1) % MOD my_turn = not my_turn elif i == N - 2 and A[-1] == 1: if my_turn: solns[i] = (my_score + A[i]) % MOD solns[i + 1] = (my_score + even + 1) % MOD my_score = (my_score + even) % MOD elif i == N - 1: if my_turn: solns[i] = (my_score + A[i]) % MOD my_score = (my_score + odd) % MOD my_turn = not my_turn elif i < N - 1 and A[i + 1] == 1: if my_turn: solns[i] = (my_score + A[i]) % MOD solns[i + 1] = my_score + even + 1 % MOD my_score = (my_score + odd) % MOD my_turn = not my_turn elif my_turn: solns[i] = (my_score + A[i]) % MOD my_score = (my_score + even) % MOD return my_score, solns def query(A, cycle_score, solns, R): N = len(A) if N == 1: even = A[0] odd = even - 1 if even % 2: even, odd = odd, even soln = R * odd % MOD soln = (soln + A[0]) % MOD return soln M = R // N score = M * cycle_score if A[0] == 1: if R == 0 or R % N: score = (score + 1) % MOD else: score = score + solns[R % N] return score % MOD def main(): test_cases = int(sys.stdin.readline()) for _ in range(test_cases): N = int(sys.stdin.readline()) A = [int(i) for i in sys.stdin.readline().split()] cycle_score, solns = solve(A) Q = int(sys.stdin.readline()) for q in range(Q): R = int(sys.stdin.readline()) print(query(A, cycle_score, solns, R - 1)) main()
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF 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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
t = int(input()) for i in range(0, t): n = int(input()) a = [] b = [] sum = 0 res = 0 a = list(map(int, input().strip().split()))[:n] for k in range(0, n): if k == 0 and a[k] == 1: res = 1 break elif k == n - 1: if a[k] % 2 == 0: sum = sum + a[k] - 1 else: sum = sum + a[k] elif a[k] == 1: sum = sum + 1 elif a[k] % 2 == 0: sum = sum + a[k] else: sum = sum - 1 + a[k] if k > 1: if a[k - 1] == 1: if a[k - 2] % 2 == 0: sum = sum - 2 b.insert(k, sum) q = int(input()) for j in range(0, q): x = int(input()) ans = 0 z = x // n z1 = x % n if res == 1: if z1 == 0: ans = z else: ans = z + 1 if z1 == 1 and x >= n + 1: ans = ans - 1 elif z1 == 0: if a[n - 1] % 2 == 0: ans = ans + 1 + z * b[n - 1] else: ans = ans + z * b[n - 1] elif a[z1 - 1] % 2 == 0: ans = ans + z * b[n - 1] + b[z1 - 1] elif a[z1 - 1] != 1: ans = ans + z * b[n - 1] + b[z1 - 1] + 1 else: ans = ans + z * b[n - 1] + b[z1 - 1] mod = 1000000007 ans = ans % mod print(ans)
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 LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR 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 NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
modu = pow(10, 9) + 7 for i in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] chef = [0] * n if arr[0] == 1: chef[0] = 1 else: for j in range(n): if j == n - 1: chef[j] = arr[j] - 1 + arr[j] % 2 elif arr[j] == 1: chef[j - 1] = arr[j - 1] - 1 + arr[j - 1] % 2 chef[j] = 0 else: chef[j] = arr[j] - arr[j] % 2 summ = sum(chef) sums = [0] * (n + 1) sums[0] for i in range(1, n + 1): sums[i] = sums[i - 1] + chef[i - 1] for q in range(int(input())): r = int(input()) if arr[0] == 1: if r % n == 1 and r > n: ans = summ * round(int(r / n)) else: ans = summ * round(int((r - 1) / n)) + 1 elif r % n: if chef[r % n - 1] == 0: ans = ( summ * round(int(r / n)) + sums[r % n - 2] + arr[r % n - 2] - arr[r % n - 2] % 2 + arr[r % n - 1] ) else: ans = summ * round(int(r / n)) + sums[r % n - 1] + arr[r % n - 1] else: ans = summ * round(int(r / n)) - chef[n - 1] + arr[n - 1] ans = ans % modu print(ans)
ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER 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 VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
t = int(input()) for a in range(t): n = int(input()) A = list(map(int, input().split())) one = -1 for i in range(len(A)): if A[i] == 1: one = i break once = 0 if one == -1 or one == n - 1: tmp = [] for i in range(len(A) - 1): if A[i] % 2 == 0: once += A[i] tmp.append(A[i]) else: once += A[i] - 1 tmp.append(A[i] - 1) if A[n - 1] % 2 == 0: once += A[n - 1] - 1 tmp.append(A[n - 1] - 1) else: once += A[n - 1] tmp.append(A[n - 1]) temp = [tmp[0]] for i in range(1, n): temp.append(temp[i - 1] + tmp[i]) q = int(input()) for i in range(q): r = int(input()) if r == 1: ans = A[0] else: ans = 0 num = (r - 1) // n rem = (r - 1) % n ans = num * once if rem > 0: ans += temp[rem - 1] ans += A[rem] print(ans % (10**9 + 7)) elif one == 0: q = int(input()) for i in range(q): r = int(input()) if r == 1: ans = A[0] else: rem = (r - 1) % n if rem == 0: ans = 1 + (r - 2) // n else: ans = 1 + (r - 1) // n print(ans % (10**9 + 7)) else: tmp = [] for i in range(len(A) - 1): if A[i] % 2 == 0 and i + 1 != one: once += A[i] tmp.append(A[i]) elif A[i] % 2 != 0 and i + 1 != one: once += A[i] - 1 tmp.append(A[i] - 1) elif A[i] % 2 == 0 and i + 1 == one: once += A[i] - 1 tmp.append(A[i] - 1) elif A[i] % 2 != 0 and i + 1 == one: once += A[i] tmp.append(A[i]) elif A[i] == 1: pass if A[n - 1] % 2 == 0: once += A[n - 1] - 1 tmp.append(A[n - 1] - 1) else: once += A[n - 1] tmp.append(A[n - 1]) temp = [tmp[0]] for i in range(1, n): temp.append(temp[i - 1] + tmp[i]) tmp2 = [] for i in range(len(A) - 1): if A[i] % 2 == 0: tmp2.append(A[i]) else: tmp2.append(A[i] - 1) if A[n - 1] % 2 == 0: tmp2.append(A[n - 1] - 1) else: tmp2.append(A[n - 1]) temp2 = [tmp2[0]] for i in range(1, n): temp2.append(temp2[i - 1] + tmp2[i]) q = int(input()) for i in range(q): r = int(input()) if r == 1: ans = A[0] else: ans = 0 num = (r - 1) // n rem = (r - 1) % n ans = num * once if rem > one: if rem > 0: ans += temp[rem - 1] ans += A[rem] else: if rem > 0: ans += temp2[rem - 1] ans += A[rem] print(ans % (10**9 + 7))
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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
t = input() a = int(t) for __ in range(a): p = 10**9 + 7 cnt = 0 ans = 0 m = input() n = int(m) arrp = [] arr = list(map(int, input().split())) for i in arr: arrp.append(i) sol = [] for i in range(n): sol.append(0) if arr[0] == 1: for i in range(n): sol[i] = 1 cnt = 1 else: for i in range(n): if i < n - 1: if arr[i] % 2 == 0: cnt = (cnt % p + arr[i] % p) % p sol[i] = cnt arrp[i] = arr[i] elif arr[i] == 1: if arr[i - 1] % 2 == 0: cnt = (cnt - 1) % p arrp[i - 1] = arrp[i - 1] - 1 sol[i - 1] = cnt sol[i] = cnt arrp[i] = 0 else: cnt = (cnt + 1) % p sol[i - 1] = cnt sol[i] = cnt arrp[i - 1] = arrp[i - 1] + 1 arrp[i] = 0 else: cnt = (cnt % p + arr[i] % p - 1) % p sol[i] = cnt arrp[i] = arr[i] - 1 elif i == n - 1: if arr[i] % 2 == 0: cnt += (arr[i] - 1) % p sol[i] = cnt arrp[i] = arr[i] - 1 else: cnt = (cnt % p + arr[i] % p) % p arrp[i] = arr[i] sol[i] = cnt Q = input() q = int(Q) for i in range(q): R = input() r = int(R) if arr[0] == 1: if r < n: ans = 1 else: g = r // n f = r % n if f == 1 or f == 0: ans = cnt % p * g % p % p else: ans = cnt % p * g % p % p + 1 elif r < n: ans = sol[r - 1] if arr[r - 1] == 1: if arr[r - 2] % 2 == 0: ans = (sol[r - 1] + 2) % p else: ans = sol[r - 1] % p if arrp[r - 1] < arr[r - 1]: ans = (ans + 1) % p else: g = r // n f = r % n if f == 0: ans = cnt % p * g % p % p if arrp[-1] < arr[-1]: ans = (ans + 1) % p else: ans = cnt % p * g % p % p if arr[f - 1] == 1: if arr[f - 2] % 2 == 0: ans = (ans % p + (sol[f - 1] + 2) % p) % p else: ans = (ans % p + sol[f - 1] % p) % p else: ans = (ans % p + sol[f - 1] % p) % p if arrp[f - 1] < arr[f - 1]: ans = (ans + 1) % p print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def solve(arr, n, q): mod = 10**9 + 7 result = 0 if arr[0] == 1: for r in q: rem = r % n quo = r // n if r <= n: result = 1 else: result = quo if rem != 0 and rem != 1: result += 1 print(result % mod) else: ind = -1 dp_arr = [0] * n for i in range(n): if arr[i] == 1: ind = i break if ind == -1 or ind == n - 1: s = 0 for i in range(n): s += arr[i] if arr[i] % 2 != 0 and i != n - 1 or i == n - 1 and arr[n - 1] % 2 == 0: s -= 1 dp_arr[i] = s for r in q: quo = r // n rem = r % n sum_ = dp_arr[n - 1] result = quo * sum_ if rem != 0 or result == 0: result += dp_arr[rem - 1] if ( arr[rem - 1] % 2 != 0 and rem != 0 or rem == 0 and arr[n - 1] % 2 == 0 ): result += 1 print(result % mod) else: s = 0 for i in range(n): s += arr[i] if i != n - 1 and arr[i + 1] == 1: if arr[i] % 2 == 0: s -= 1 elif arr[i] % 2 != 0 and i != n - 1 or arr[i] % 2 == 0 and i == n - 1: s -= 1 dp_arr[i] = s for r in q: quo = r // n rem = r % n sum_ = dp_arr[n - 1] result = quo * sum_ if arr[n - 1] % 2 == 0 and rem == 0: result += 1 elif rem != 0: result += dp_arr[rem - 1] if arr[rem - 1] % 2 != 0 and arr[rem] != 1 and arr[rem - 1] != 1: result += 1 elif arr[rem] == 1 and arr[rem - 1] % 2 == 0: result += 1 elif arr[rem - 2] % 2 == 0 and arr[rem - 1] == 1: result += 2 print(result % mod) for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) q = [int(input()) for i in range(int(input()))] solve(arr, n, q)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def nearest_lower_even_number(number): return number >> 1 << 1 def nearest_lower_odd_number(number): return (number - (number >> 1) << 1) - 1 def payout_if_one_is_last_or_absent(sequence): payout = [nearest_lower_even_number(sequence[0])] for s in sequence[1:-1]: payout.append(payout[-1] + nearest_lower_even_number(s)) payout.append(payout[-1] + nearest_lower_odd_number(sequence[-1])) return payout def result_if_one_is_first(length, number_of_turns): quotient, remainder = divmod(number_of_turns, length) if number_of_turns == 1: result = 1 elif remainder <= 1: result = quotient else: result = quotient + 1 return result def result_if_one_is_absent(payout, sequence, length, number_of_turns): quotient, remainder = divmod(number_of_turns, length) result = quotient * payout[-1] if remainder == 0: result += -payout[-1] + payout[-2] + sequence[-1] elif remainder == 1: result += sequence[0] else: result += payout[remainder - 2] + sequence[remainder - 1] return result def result_if_one_is_last(payout, sequence, length, number_of_turns): quotient, remainder = divmod(number_of_turns, length) result = quotient * payout[-1] if remainder > 0: result += sequence[remainder - 1] if remainder > 1: result += payout[remainder - 2] return result def result_if_one_is_inside(payout, sequence, length, index_of_one, number_of_turns): quotient, remainder = divmod(number_of_turns, length) result = quotient * payout[-1] if remainder == 0: result += -payout[-1] + payout[-2] + sequence[-1] elif remainder == 1: result += sequence[0] elif remainder == index_of_one + 1: if remainder >= 3: result += ( payout[remainder - 3] + nearest_lower_even_number(sequence[remainder - 2]) + 1 ) else: result += nearest_lower_even_number(sequence[0]) + 1 else: result += payout[remainder - 2] + sequence[remainder - 1] return result for _ in range(int(input())): length, sequence, queries = ( int(input()), [int(x) for x in input().split()], [int(input()) for _ in range(int(input()))], ) if length == 1: if sequence[0] & 1 == 1: result = lambda q: sequence[0] * q else: result = lambda q: (sequence[0] - 1) * q + 1 elif sequence[0] == 1: result = lambda q: result_if_one_is_first(length, q) elif sequence[-1] == 1: payout = payout_if_one_is_last_or_absent(sequence) result = lambda q: result_if_one_is_last(payout, sequence, length, q) else: try: index_of_one = sequence.index(1) payout = [] for i, s in enumerate(sequence): if i == 0: if index_of_one == 1: payout.append(nearest_lower_odd_number(s)) else: payout.append(nearest_lower_even_number(s)) elif i == index_of_one - 1 or i == length - 1: payout.append(payout[-1] + nearest_lower_odd_number(s)) elif i == index_of_one: payout.append(payout[-1]) else: payout.append(payout[-1] + nearest_lower_even_number(s)) result = lambda q: result_if_one_is_inside( payout, sequence, length, index_of_one, q ) except ValueError: payout = payout_if_one_is_last_or_absent(sequence) result = lambda q: result_if_one_is_absent(payout, sequence, length, q) print("\n".join(str(result(q) % 1000000007) for q in queries))
FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
try: for ii in range(int(input())): n = int(input()) a = list(map(int, input().split())) if a[0] == 1: q = int(input()) for jj in range(q): r = int(input()) if r != 1: d = r // n if d * n + 1 == r: r -= 1 d = r // n if r % n == 0: print(d % 1000000007) else: print((d + 1) % 1000000007) else: print(1) else: p = [] for jj in range(n - 1): if a[jj + 1] == 1: if a[jj] % 2 == 1: p.append(a[jj]) else: p.append(a[jj] - 1) elif a[jj] % 2 == 1: p.append(a[jj] - 1) else: p.append(a[jj]) p.append(0) if n != 1: if a[n - 2] % 2 == 1: p[n - 2] = a[n - 2] - 1 else: p[n - 2] = a[n - 2] if a[n - 1] % 2 == 1: p[n - 1] = a[n - 1] else: p[n - 1] = a[n - 1] - 1 t = p[:] for jj in range(1, n): p[jj] = p[jj] + p[jj - 1] % 1000000007 q = int(input()) for jj in range(q): r = int(input()) d, rem = r // n, r % n ans = d * p[n - 1] % 1000000007 if rem == 0: ans = (ans + abs((a[n - 1] - t[n - 1]) % 1000000007)) % 1000000007 else: ans = (ans + p[rem - 1]) % 1000000007 if a[rem - 1] == 1: if a[rem - 2] % 2 == 0: ans = (ans + 2) % 1000000007 else: ans = ( abs((a[rem - 1] - t[rem - 1]) % 1000000007) + ans ) % 1000000007 print(ans % 1000000007) except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) q = int(input()) l1 = [[0, 0]] for j in range(n): if j < n - 1: if l[j + 1] != 1 or j == n - 2: l1.append([l1[len(l1) - 1][0] + (l[j] - l[j] % 2), l[j] % 2]) else: l1.append([l1[len(l1) - 1][0] + (l[j] + l[j] % 2 - 1), 1 - l[j] % 2]) else: l1.append([l1[len(l1) - 1][0] + (l[j] + l[j] % 2 - 1), 1 - l[j] % 2]) l1.pop(0) for k in range(q): r = int(input()) a = r // n if l[0] == 1: if a == 0: print(1) elif r % n == 0: print(a % 1000000007) elif r % n == 1: print(a % 1000000007) else: print((a + 1) % 1000000007) else: b = r % n if b != 0: if l[b - 1] != 1: s = l1[len(l1) - 1][0] * a + l1[b - 1][0] + l1[b - 1][1] else: s = l1[len(l1) - 1][0] * a + l1[b - 1][0] + l1[b - 2][1] * 2 else: s = l1[len(l1) - 1][0] * a + l1[len(l1) - 1][1] print(s % 1000000007)
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 ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
def inp(): return int(input()) def inl(): return [int(i) for i in input().split()] for _ in range(inp()): n = inp() a = inl() m = pow(10, 9) + 7 flg = False t = -1 for i in range(n): if a[i] == 1 and i == 0: flg = True break if a[i] == 1: t = i break if flg == True and n == 1: q = inp() for i in range(q): r = inp() print(r // n % m) elif flg == True: q = inp() for i in range(q): r = inp() if r % n == 1 and r >= n or r % n == 0: print(r // n % m) elif r % n == 1 and r == 1: print(1) else: print((r // n + 1) % m) elif t == -1 or t == n - 1: q = inp() dp = [(0) for i in range(n + 1)] dp[1] = a[0] for i in range(2, n + 1): dp[i] = dp[i - 1] + a[i - 1] if a[i - 2] & 1 == 1: dp[i] = dp[i] - 1 for i in range(q): r = inp() d = r // n rem = r % n ans = d * dp[n] + dp[rem] if d > 0: if a[n - 1] & 1 == 0: if rem == 0: ans = ans - (d - 1) else: ans = ans - d print(ans % m) else: q = inp() dp = [(0) for i in range(n + 1)] dp[1] = a[0] for i in range(2, n + 1): dp[i] = dp[i - 1] + a[i - 1] if a[i - 2] == 1: if a[i - 3] & 1 == 0: dp[i] = dp[i] - 2 elif a[i - 2] & 1 == 1: dp[i] = dp[i] - 1 for i in range(q): r = inp() d = r // n rem = r % n ans = d * dp[n] + dp[rem] if d > 0: if a[n - 1] & 1 == 0: if rem == 0: ans = ans - (d - 1) else: ans = ans - d print(ans % m)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
modulo = 1000000007 for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) flag = -1 index = -1 if 1 in arr: index = arr.index(1) if index == -1 or index == n - 1: ans = 0 temp_arr = [] for i in range(n - 1): if arr[i] % 2 == 1: ans += arr[i] - 1 temp_arr.append(ans) else: ans += arr[i] temp_arr.append(ans) if arr[-1] % 2 == 0: ans += arr[-1] - 1 else: ans += arr[-1] for _ in range(int(input())): r = int(input()) if r % n == 0: if arr[-1] % 2 == 0: print((r // n * ans + 1) % modulo) else: print(r // n * ans % modulo) elif arr[r % n - 1] % 2 == 1: print((r // n * ans + temp_arr[r % n - 1] + 1) % modulo) else: print((r // n * ans + temp_arr[r % n - 1]) % modulo) elif index == 0: for _ in range(int(input())): r = int(input()) if r % n == 0: print(r // n % modulo) elif r % n == 1 and r > n: print(r // n % modulo) else: print((r // n + 1) % modulo) continue else: ans = 0 temp_arr = [] for i in range(n - 1): if i != index - 1: if arr[i] % 2 == 0: ans += arr[i] else: ans += arr[i] - 1 elif arr[i] % 2 == 0: ans += arr[i] - 1 else: ans += arr[i] temp_arr.append(ans) if arr[-1] % 2 == 0: ans += arr[-1] - 1 else: ans += arr[-1] for _ in range(int(input())): r = int(input()) if r % n == 0: if arr[-1] % 2 == 0: print((r // n * ans + 1) % modulo) else: print(r // n * ans % modulo) elif index == r % n - 1: if arr[index - 1] % 2 == 0: print((r // n * ans + temp_arr[r % n - 1] + 2) % modulo) else: print((r // n * ans + temp_arr[r % n - 1]) % modulo) elif index - 1 == r % n - 1: if arr[index - 1] % 2 == 0: print((r // n * ans + temp_arr[r % n - 1] + 1) % modulo) else: print((r // n * ans + temp_arr[r % n - 1]) % modulo) elif arr[r % n - 1] % 2 == 1: print((r // n * ans + temp_arr[r % n - 1] + 1) % modulo) else: print((r // n * ans + temp_arr[r % n - 1]) % modulo)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
for Z in range(int(input())): N, lst = int(input()), list(map(int, input().split(" "))) dp, mod, temp = [0] * N, int(1000000000.0 + 7), [0] * N if N == 1: dp[0] = lst[0] - int(lst[0] % 2 == 0) temp[0] = temp[0] + int(lst[0] % 2 == 0) elif lst[0] == 1: dp = [1] * N elif 1 not in lst or lst.index(1) == N - 1: dp[0] = lst[0] - int(lst[0] & 1) temp[0] = temp[0] + int(lst[0] & 1) for i in range(1, N - 1): dp[i] = (dp[i - 1] + lst[i] - int(lst[i] & 1)) % mod temp[i] = temp[i] + int(lst[i] & 1) dp[N - 1] = (dp[N - 2] + lst[N - 1] - int(lst[N - 1] % 2 == 0)) % mod temp[N - 1] = temp[N - 1] + int(lst[N - 1] % 2 == 0) else: if lst[1] == 1: dp[0] = lst[0] - int(lst[0] % 2 == 0) temp[0] = temp[0] + int(lst[0] % 2 == 0) temp[1] = temp[1] + 1 if lst[0] % 2 == 0 else temp[1] - 1 else: dp[0] = lst[0] - int(lst[0] & 1) temp[0] = temp[0] + int(lst[0] & 1) for i in range(1, N - 1): if lst[i + 1] == 1: dp[i] = (dp[i - 1] + lst[i] - int(lst[i] % 2 == 0)) % mod temp[i] = temp[i] + int(lst[i] % 2 == 0) temp[i + 1] = temp[i + 1] + 1 if lst[i] % 2 == 0 else temp[i + 1] - 1 else: dp[i] = (dp[i - 1] + lst[i] - int(lst[i] & 1)) % mod temp[i] = temp[i] + int(lst[i] & 1) dp[N - 1] = (dp[N - 2] + lst[N - 1] - int(lst[N - 1] % 2 == 0)) % mod temp[N - 1] = temp[N - 1] + int(lst[N - 1] % 2 == 0) for _ in range(int(input())): R = int(input()) - 1 ans, index = ( R // N * dp[-1] % mod + (dp[R % N] + temp[R % N]) % mod ) % mod, R % N print(ans - 1 if index == 0 and lst[index] == 1 and R >= N and N > 1 else ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP LIST NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
o = 1000000007 def func2(a, n): sm = 0 s = [0] * (n + 1) for i in range(n - 1): if i == ind - 1: if a[i] % 2 == 0: sm = sm + a[i] - 1 s[i] = sm else: sm += a[i] s[i] = sm elif a[i] % 2 == 0: sm += a[i] s[i] = sm else: sm += a[i] - 1 s[i] = sm if a[n - 1] % 2 == 0: sm += a[n - 1] - 1 else: sm += a[n - 1] q = int(input()) for _ in range(q): p = int(input()) if p % n == 0: if a[n - 1] % 2 == 0: print((p // n * sm + 1) % o) else: print(p // n * sm % o) else: l = p % n if ind == l - 1: if a[ind - 1] % 2 == 0: an = (p // n * sm + 2 + s[l - 1]) % o print(an) else: an = (p // n * sm + s[l - 1]) % o print(an) elif ind == l: if a[ind - 1] % 2 == 0: an = (p // n * sm + s[l - 1] + 1) % o print(an) else: an = (p // n * sm + s[l - 1]) % o print(an) elif a[l - 1] % 2 != 0: an = (p // n * sm + s[l - 1] + 1) % o print(an) else: an = (p // n * sm + s[l - 1]) % o print(an) def fun3(a, n): sm = 0 s = [0] * (n + 1) for i in range(n): if i == n - 1: if a[i] % 2 == 0: sm += a[i] - 1 else: sm += a[i] elif a[i] % 2 == 0: sm += a[i] s[i] = sm else: sm += a[i] - 1 s[i] = sm q = int(input()) for _ in range(q): p = int(input()) l = p % n if l == 0: if a[n - 1] % 2 == 0: print((p // n * sm + 1) % o) else: print(p // n * sm % o) elif a[l - 1] % 2 != 0: print((p // n * sm + s[l - 1] + 1) % o) else: print((p // n * sm + s[l - 1]) % o) for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) v = 0 if a[0] == 1: v = 1 else: for i in range(n): if a[i] == 1 and i != n - 1: v = 2 ind = i break if v == 1: q = int(input()) for _ in range(q): p = int(input()) if p <= n: print(1) elif p % n == 0: print(p // n % o) elif p % n == 1: print(p // n % o) else: print((p // n + 1) % o) elif v == 2: func2(a, n) else: fun3(a, n)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER 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 NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
mod = 10**9 + 7 def Retain(A): ln = len(A) re = 0 if A[0] == 1: re = 1 elif A[-1] == 1: for it in A[:-1]: re = re + it if it % 2: re -= 1 re %= mod re += 1 else: for i, it in enumerate(A[:-1]): re = re + it if i + 1 < ln and A[i + 1] == 1: if it % 2 == 0: re -= 1 elif it % 2: re -= 1 re %= mod re += A[-1] if A[-1] % 2 == 0: re -= 1 re %= mod return re def DontCare(A): dp = [0] * (len(A) + 1) if A[0] == 1: dp[1] = 0 for i in range(2, len(A) + 1): dp[i] = 1 elif A[-1] == 1: for i, it in enumerate(A[:-1]): dp[i + 1] += it dp[i + 2] = dp[i + 1] if it % 2: dp[i + 2] -= 1 dp[i + 1] %= mod dp[-1] += 1 dp[-1] %= mod else: dp[1] = A[0] for i in range(1, len(A)): it = A[i] if it == 1: dp[i + 1] = dp[i] if A[i - 1] % 2 == 0: dp[i + 1] += 1 elif A[i - 1] == 1: dp[i + 1] = dp[i - 1] + it if A[i - 2] % 2 == 0: dp[i + 1] -= 1 else: dp[i + 1] = dp[i] + it if A[i - 1] % 2: dp[i + 1] -= 1 dp[i + 1] %= mod return dp for case in range(int(input())): n = int(input()) A = list(map(int, input().split())) Q = int(input()) dp = DontCare(A) rt = Retain(A) for q in range(Q): R = int(input()) ans = 0 if R == 1: ans = A[0] else: r = R % n times = R // n if r: ans = rt * times % mod + dp[r] else: ans = rt * (times - 1) % mod + dp[n] if len(A) == 1 and A[0] == 1: ans = R ans %= mod print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
mod = 1000000007 t = int(input()) for T in range(t): size = int(input()) templ1 = list(map(int, input().split())) qsize = int(input()) if 1 not in templ1: res = [] tes = 0 for j in range(size - 1): if templ1[j] % 2 == 1: tes = (tes + (templ1[j] - 1)) % mod else: tes = (tes + templ1[j]) % mod res.append(tes) if templ1[-1] % 2 == 1: tes = (tes + templ1[-1]) % mod else: tes = (tes + (templ1[-1] - 1)) % mod res.append(tes) for i in range(qsize): query = int(input()) tes1 = query // size * tes % mod md = query % size if md == 0: if templ1[-1] % 2 == 0: tes1 = (tes1 + 1) % mod else: tes1 = (tes1 + res[md - 1]) % mod if templ1[md - 1] % 2 == 1: tes1 = (tes1 + 1) % mod print(tes1 % mod) elif templ1[0] == 1: for i in range(qsize): query = int(input()) tes = query // size % mod if query % size > 1 or query < size: tes = (tes + 1) % mod print(tes % mod) elif templ1[-1] == 1: res = [] tes = 0 for i in range(size - 1): if templ1[i] % 2 == 1: tes = (tes + (templ1[i] - 1)) % mod else: tes = (tes + templ1[i]) % mod res.append(tes) tes = (tes + 1) % mod res.append(tes) for i in range(qsize): query = int(input()) tes1 = query // size * tes % mod md = query % size if md != 0: tes1 = tes1 + res[md - 1] if templ1[md - 1] % 2 != 0: tes1 = (tes1 + 1) % mod print(tes1 % mod) else: res = [] tp = templ1.index(1) tes1 = 0 for j in range(tp - 1): if templ1[j] % 2 == 1: tes1 = (tes1 + (templ1[j] - 1)) % mod else: tes1 = (tes1 + templ1[j]) % mod res.append(tes1) if templ1[tp - 1] % 2 == 1: tes4 = (tes1 + (templ1[tp - 1] - 1)) % mod tes1 = (tes1 + templ1[tp - 1]) % mod else: tes4 = (tes1 + templ1[tp - 1]) % mod tes1 = (tes1 + (templ1[tp - 1] - 1)) % mod res.append(tes1) res.append(tes4 + 1) for j in range(tp + 1, size - 1): if templ1[j] % 2 == 1: tes1 = (tes1 + (templ1[j] - 1)) % mod else: tes1 = (tes1 + templ1[j]) % mod res.append(tes1) if templ1[-1] % 2 == 1: tes1 = (tes1 + templ1[-1]) % mod else: tes1 = (tes1 + (templ1[-1] - 1)) % mod res.append(tes1) for i in range(qsize): query = int(input()) tes = tes1 * (query // size) % mod md = query % size if md - 1 < tp: if md == 0: if templ1[-1] % 2 == 0: tes = (tes + 1) % mod else: if md - 2 > -1: tes = (tes + res[md - 2]) % mod tes = (tes + templ1[md - 1]) % mod elif md - 1 == tp: tes = (tes + res[md - 1]) % mod else: tes = (tes + res[md - 1]) % mod if templ1[md - 1] % 2 != 0: tes = (tes + 1) % mod print(tes % mod)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chef and Chefu are at a magical candy store playing a game with the following rules: There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed. Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter. There is a sequence of $N$ distinct integers $A_{1}, A_{2}, \ldots, A_{N}$. The game consists of $R$ turns; in the $i$-th turn, the open counter offers only $C = A_{ (i-1) \% N + 1}$ candies to the player present at this counter. This player should choose a positive number of candies $M$ to accept, where $1 ≤ M ≤ C$. If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter). After each $N$ turns, the counter which was currently open is closed and the counter which was currently closed is opened. The primary goal of each player is to maximise his own number of candies after $R$ turns. As a second priority, each player wants to minimise the number of candies his opponent has after $R$ turns. You should process $Q$ queries. In each query, you are given $R$ and you should find the number of candies Chef has after $R$ turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo $10^{9} + 7$. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$. The third line contains a single integer $Q$. Each of the next $Q$ lines contains a single integer $R$ describing a query. ------ Output ------ For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo $10^{9}+7$. ------ Constraints ------ $1 ≤ T ≤ 25$ $1 ≤ N ≤ 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$ $A_{1}, A_{2}, \ldots, A_{N}$ are pairwise distinct $1 ≤ Q ≤ 10^{5}$ $1 ≤ R ≤ 10^{12}$ the sum of $N + Q$ over all test cases does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (15 points): $N ≤ 10$ $Q ≤ 35$ $R ≤ 35$ Subtask #2 (85 points): original constraints ----- Sample Input 1 ------ 1 4 4 10 2 1 2 4 5 ----- Sample Output 1 ------ 17 21 ----- explanation 1 ------ Example case 1: In the $1$-st, $2$-nd and $3$-rd turn, Chef takes $4$, $10$ and $2$ candies ($16$ in total) respectively. In the $4$-th turn, Chef takes $1$ candy ($17$ in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since $N = 4$ turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the $5$-th round, Chef can take $4$ candies, so he has $21$ candies.
mod = 1000000007 for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) q = int(input()) one = -1 pref = [0] * n for i in range(n): if a[i] == 1: one = i if i > 0: pref[i] = pref[i - 1] if i < n - 1: pref[i] += a[i] - a[i] % 2 else: pref[i] += a[i] - (a[i] + 1) % 2 pref[i] %= mod for iterator in range(q): turns = int(input()) rem = turns % n q = int(turns / n % mod) ans = 0 if one == 0: ans = q + (rem > 1 or q == 0) ans %= mod elif one == n - 1: ans = pref[n - 1] * q ans %= mod if rem: ans += pref[rem - 1] if a[rem - 1] & 1: ans += 1 elif a[n - 1] % 2 == 0: ans += 1 ans %= mod elif one >= 0: ans = (pref[n - 1] + (1 if a[one - 1] % 2 == 1 else -1)) * q ans %= mod if rem: if rem - 1 <= one: ans += pref[rem - 1] else: ans += pref[rem - 1] + (1 if a[one - 1] % 2 == 1 else -1) if a[rem - 1] & 1: ans += 1 elif a[n - 1] % 2 == 0: ans += 1 ans %= mod else: ans = pref[n - 1] * q ans %= mod if rem: ans += pref[rem - 1] if a[rem - 1] & 1: ans += 1 elif a[n - 1] % 2 == 0: ans += 1 ans %= mod print(ans)
ASSIGN VAR NUMBER 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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER VAR VAR VAR IF VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: Input: 2 / \ 1 3 Output: true Example 2: 5 / \ 1 4   / \   3 6 Output: false Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value   is 5 but its right child's value is 4.
class Solution(object): def isValidBST(self, root): res = [] return self.inorder(root, res) def inorder(self, root, res): if not root: return True if self.inorder(root.left, res) == False: return False if len(res) != 0: last = res.pop() if last >= root.val: return False res.append(last) res.append(root.val) if self.inorder(root.right, res) == False: return False return True
CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Example 1: Input: 2 / \ 1 3 Output: true Example 2: 5 / \ 1 4   / \   3 6 Output: false Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value   is 5 but its right child's value is 4.
class Solution: def isValidBST(self, root): if not root: return True return self.dfs(root)[2] def dfs(self, node): if node is None: return [None, None, True] L = self.dfs(node.left) R = self.dfs(node.right) if ( not L[2] or not R[2] or L[1] is not None and L[1] >= node.val or R[0] is not None and R[0] <= node.val ): return [None, None, False] if L[0] is not None: min = L[0] else: min = node.val if R[1] is not None: max = R[1] else: max = node.val return [min, max, True]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN LIST NONE NONE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER NONE VAR NUMBER VAR VAR NUMBER NONE VAR NUMBER VAR RETURN LIST NONE NONE NUMBER IF VAR NUMBER NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR RETURN LIST VAR VAR NUMBER
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
for i in range(int(input())): n, k = [int(x) for x in input().split()] a = [int(y) for y in input().split()] d = [int(u) for u in input().split()] b = [int(l) for l in input().split()] a1 = [0] * 1000000 c = 0 for i in range(len(a)): a1[a[i]] += d[i] c += d[i] sum = 0 ma = max(a) mi = min(a) for i in range(k): l = c - b[i] if i % 2 == 0: for j in range(mi, ma + 1): if a1[j] != 0: if a1[j] >= l: a1[j] -= l l = 0 break else: l -= a1[j] a1[j] = 0 mi = j else: for j in range(ma, mi - 1, -1): if a1[j] != 0: if a1[j] >= l: a1[j] -= l l = 0 break else: ma = j l -= a1[j] a1[j] = 0 c -= c - b[i] for i in range(mi, ma + 1): if a1[i] != 0: sum += i * a1[i] print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) d = list(map(int, input().split())) b = list(map(int, input().split())) arr = [] crd = 0 for i in range(len(a)): arr += [[a[i], d[i]]] crd += d[i] arr.sort(key=lambda x: x[0]) for crawl in range(k): todo = crd - b[crawl] if crawl % 2 != 0: init = len(arr) - 1 while todo != 0: if arr[init][1] > todo: arr[init][1] -= todo todo = 0 else: todo -= arr[init][1] del arr[init] init -= 1 else: init = 0 while todo != 0: if arr[init][1] > todo: arr[init][1] -= todo todo = 0 else: todo -= arr[init][1] del arr[init] crd = b[crawl] flag = 0 for walk in range(len(arr)): flag += arr[walk][0] * arr[walk][1] print(flag)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST LIST VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
for i in range(int(input())): n, k = [int(i) for i in input().split()] arr = [[int(i)] for i in input().split()] l = input().split() value = 0 for i in range(n): arr[i].append(int(l[i])) value += int(l[i]) arr.sort() ans = [value] move = list(map(int, input().split())) for i in range(k): if i % 2 == 0: ans.append(ans[-1] - move[i] + 1) else: ans.append(ans[-1] + move[i] - 1) p = 0 x = max(ans[-1], ans[-2]) y = min(ans[-1], ans[-2]) index1 = index2 = None for i in range(len(arr)): p += arr[i][1] if p >= y and index1 == None: index1 = i x1 = p - y + 1 if p >= x: index2 = i x2 = x - p + arr[i][1] break if index1 == index2: v = arr[index1][0] * (x - y + 1) else: v = arr[index1][0] * x1 + arr[index2][0] * x2 for i in range(index1 + 1, index2): v += arr[i][0] * arr[i][1] print(v)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
def length(t1, t2): a, b = t1 c, d = t2 return max(0, min(d, b) - max(a, c)) for _ in range(int(input())): N, K = map(int, input().split()) A = list(map(int, input().split())) D = list(map(int, input().split())) C = sum(D) B = list(map(int, input().split())) evs, ods = 0, 0 B = [C] + B for i in range(1, len(B)): if i % 2 == 0: evs += B[i - 1] - B[i] else: ods += B[i - 1] - B[i] arr = [(A[i], D[i]) for i in range(N)] arr.sort(key=lambda x: x[0]) endpts = [(0) for i in range(N)] s = 0 for i in range(len(D)): endpts[i] = s, s + arr[i][1] s += arr[i][1] ans = 0 for k in range(N): ans += arr[k][0] * length(endpts[k], (ods, C - evs)) print(ans)
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
import sys def main(N, K): A = sys.stdin.readline().split() D = sys.stdin.readline().split() AD = [ [(int(A[y]) if x == 0 else int(D[y])) for x in range(2)] for y in range(len(A)) ] B = sys.stdin.readline().split() B = [int(x) for x in B] AD = sorted(AD, key=lambda l: l[0]) A = [AD[i][0] for i in range(len(A))] D = [AD[i][1] for i in range(len(D))] sumaD = sum(D) B_par = [B[2 * i] for i in range((K + 1) // 2)] B_impar = [B[2 * i + 1] for i in range(K // 2)] limMin = sumaD - sum(B_par) + sum(B_impar[0 : len(B_impar) - (K + 1) % 2]) limMax = sumaD - sum(B_par[0 : len(B_par) - K % 2]) + sum(B_impar[0 : len(B_impar)]) A, D = maximosLista(sumaD - limMin, A, D) A, D = minimosLista(limMax - limMin, A, D) suma = sum(i[0] * i[1] for i in zip(A, D)) print(suma) def maximosLista(B_i, A, D): eligiendo = True primerIndice = len(A) - 1 primerD = 0 while eligiendo: if B_i == 0: eligiendo = False elif D[primerIndice] >= B_i: primerD = B_i eligiendo = False elif D[primerIndice] < B_i: B_i += -D[primerIndice] primerIndice += -1 if primerD != 0: A = [A[x] for x in range(primerIndice, len(A))] D = [ (D[x] if x != primerIndice else primerD) for x in range(primerIndice, len(D)) ] else: A = [] D = [] return A, D def minimosLista(B_i, A, D): eligiendo = True ultimoIndice = 0 ultimoD = 0 while eligiendo: if B_i == 0: eligiendo = False elif D[ultimoIndice] >= B_i: ultimoD = B_i eligiendo = False elif D[ultimoIndice] < B_i: B_i += -D[ultimoIndice] ultimoIndice += 1 if ultimoD != 0: A = [A[x] for x in range(ultimoIndice + 1)] D = [ (D[x] if x != ultimoIndice else ultimoD) for x in range(0, ultimoIndice + 1) ] else: A = [] D = [] return A, D casos = int(sys.stdin.readline()) for i in range(casos): listaIn = sys.stdin.readline().split() N = int(listaIn[0]) K = int(listaIn[1]) main(N, K)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
T = int(input()) for i in range(0, T): N, K = map(int, input().split()) A = [int(x) for x in input().split()] D = [int(x) for x in input().split()] B = [int(x) for x in input().split()] L = sorted(zip(A, D)) L = L[::-1] for j in range(0, len(L)): L[j] = list(L[j]) if K % 2 != 0: B.append(B[-1]) sum = 0 for j in range(0, len(B), 2): sum = sum + (B[j] - B[j + 1]) sumu = 0 for j in range(0, len(L)): sumu = sumu + L[j][1] if sumu >= sum: L[j][1] = sumu - sum L = L[j:] break sumu2 = 0 for j in range(0, len(L)): sumu2 = sumu2 + L[j][1] if sumu2 >= B[-1]: L[j][1] = L[j][1] - sumu2 + B[-1] L = L[: j + 1] break sumu3 = 0 for j in range(0, len(L)): sumu3 = sumu3 + L[j][0] * L[j][1] print(sumu3)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
for _ in range(int(input())): [n, k] = [int(i) for i in input().split()] a = [int(i) for i in input().split()] d = [int(i) for i in input().split()] b = [int(i) for i in input().split()] p = {} for i in range(n): try: p[a[i]] += d[i] except: p[a[i]] = d[i] val = [] left = sum(d) for i in p: val.append([i, p[i]]) val.sort() si, en = 0, len(val) - 1 for i in range(k): if en < si: break dele = left - b[i] left -= dele if i % 2 == 0: while dele: if val[si][1] <= dele: dele -= val[si][1] si += 1 else: val[si][1] -= dele dele = 0 else: while dele: if val[en][1] <= dele: dele -= val[en][1] en -= 1 else: val[en][1] -= dele dele = 0 ans = 0 if si > en: print("0") continue for i in range(si, en + 1): ans += int(val[i][0] * val[i][1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER WHILE VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
t = int(input().strip()) while t > 0: n, k = [int(x) for x in input().strip().split(" ")] a = [int(x) for x in input().strip().split(" ")] d = [int(x) for x in input().strip().split(" ")] b = [int(x) for x in input().strip().split(" ")] p = list(map(list, zip(a, d))) tot = sum(d) p.sort(key=lambda x: (x[0], x[1])) j = 0 lenb = len(b) lenp = len(p) start = 0 end = lenp - 1 while j < lenb: k = tot - b[j] tot -= k if j % 2 == 0: while start < lenp: z = k k -= p[start][1] if k == 0: start += 1 break if k > 0: start += 1 else: p[start][1] -= z break else: while end >= 0: z = k k -= p[end][1] if k == 0: end -= 1 break if k > 0: end -= 1 else: p[end][1] -= z break j += 1 tsum = 0 if start <= end: for i in range(start, end + 1): tsum += p[i][0] * p[i][1] print(tsum) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
t = int(input()) a = [] b = [] for i in range(t): n = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [] total = 0 for j in range(n[0]): total += b[j] c.append((a[j], b[j])) c.sort() k = list(map(int, input().split())) for j in range(n[1]): x = k[j] x = total - x total -= x if j % 2 == 0: while x > 0: if x >= c[0][1]: x -= c[0][1] c.pop(0) elif x < c[0][1]: c[0] = c[0][0], c[0][1] - x x = 0 break elif j % 2 == 1: while x > 0: if x >= c[-1][1]: x -= c[-1][1] c.pop(-1) elif x < c[-1][1]: c[-1] = c[-1][0], c[-1][1] - x x = 0 break x = 0 for i in c: x += i[0] * i[1] print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
T = int(input()) for _ in range(T): N, K = map(int, input().split()) (*A,) = map(int, input().split()) (*D,) = map(int, input().split()) (*B,) = map(int, input().split()) P = sorted(zip(A, D)) A = [a for a, d in P] D = [d for a, d in P] left = 0 right = sum(D) - 1 for i in range(K): if i % 2 == 0: left = right - B[i] + 1 else: right = left + B[i] - 1 cur = 0 ans = 0 for i in range(N): if left <= cur + D[i] - 1 and cur <= right: ans += A[i] * (min(cur + D[i] - 1, right) - max(cur, left) + 1) cur += D[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
outputs = [] T = int(input()) for i in range(0, T): n = input().split() a = input().split() d = input().split() b = input().split() a = list(map(int, a)) d = list(map(int, d)) N, K = int(n[0]), int(n[1]) b = list(map(int, b)) ncards = sum(d) list1 = [] z = 0 z2 = 0 for i in range(0, N): list1.append([a[i], d[i]]) list1.sort() chef = ncards - b[0] chefu = 0 for i in range(0, K - 1): if i % 2 == 0: chefu += b[i] - b[i + 1] else: chef += b[i] - b[i + 1] z = 0 i = 0 while z <= ncards: z += list1[i][1] if z == chef: list1 = list1[i + 1 :] break elif z > chef: list1[i][1] = z - chef list1 = list1[i:] break i += 1 z = 0 i = len(list1) - 1 while z <= ncards: z += list1[i][1] if z == chefu: list1 = list1[:i] break elif z > chefu: list1[i][1] = z - chefu list1 = list1[: i + 1] break i -= 1 ans = 0 for i in range(0, len(list1)): ans += list1[i][0] * list1[i][1] outputs.append(ans) for i in range(0, T): print(outputs[i])
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
t = int(input()) while t: t -= 1 n, k = list(map(int, input().split())) a = list(map(int, input().split())) d = list(map(int, input().split())) c = [] for i in range(n): c.append([a[i], d[i]]) c = sorted(c) a = [] d = [] for i in range(n): a.append(c[i][0]) d.append(c[i][1]) b = list(map(int, input().split())) cardCounts = sum(d) start = 0 for i in range(k): temp = b[i] b[i] = cardCounts - b[i] cardCounts = temp if i % 2: while n > 0 and b[i] > 0: if b[i] < d[n - 1]: d[n - 1] -= b[i] break b[i] -= d[n - 1] n -= 1 else: while start < n - 1 and b[i] > 0: if b[i] < d[start]: d[start] -= b[i] break b[i] -= d[start] start += 1 ans = 0 for i in range(start, n): ans += d[i] * a[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
T = int(input()) for i in range(T): N, K = map(int, input().split()) Alist = list(map(int, input().split())) Dlist = list(map(int, input().split())) Blist = list(map(int, input().split())) Alist, Dlist = (list(t) for t in zip(*sorted(zip(Alist, Dlist)))) C = 0 for j in range(N): C += Dlist[j] end = C for j in range(len(Blist)): if j % 2 == 0: start = end - Blist[j] else: end = start + Blist[j] start += 1 j = 0 A = Dlist[j] ans = 0 while A < start: j += 1 A += Dlist[j] if A >= start and A >= end: z = end - start + 1 ans += z * Alist[j] elif A >= start and A < end: z = A - start + 1 ans += z * Alist[j] while A < end: j += 1 Acopy = A A += Dlist[j] if A < end: ans += Dlist[j] * Alist[j] else: z = end - Acopy ans += Alist[j] * z print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
for _ in range(int(input())): n, k = map(int, input().split()) z = input().split() a = list(map(int, z[:n])) z = input().split() d = list(map(int, z[:n])) z = input().split() b = list(map(int, z[:k])) dis = [(p - c) for c, p in zip(b, [sum(d)] + b)] lo = sum(dis[::2]) hi = sum(dis[1::2]) p = [[ai, di] for ai, di in zip(a, d)] p.sort() for px in range(n): if p[px][1] >= lo: p[px][1] -= lo lo = 0 break else: lo -= p[px][1] p[px][1] = 0 for px in range(n - 1, -1, -1): if p[px][1] >= hi: p[px][1] -= hi hi = 0 break else: hi -= p[px][1] p[px][1] = 0 print(sum(pi[0] * pi[1] for pi in p))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP LIST FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
class CardSet(object): def __init__(self, cardValue, cardAmount): self.cardValue = cardValue self.cardAmount = cardAmount T = int(input()) for _ in range(T): N, K = [int(x) for x in input().split()] A = [int(x) for x in input().split()] D = [int(x) for x in input().split()] B = [int(x) for x in input().split()] cardSets = [CardSet(a, d) for a, d in zip(A, D)] cardSets.sort(key=lambda x: x.cardValue) totalCardsLeft = sum(D) chefsMove = True for move, b in enumerate(B): cardsToRemove = totalCardsLeft - b if chefsMove: while cardsToRemove > 0: currentCardSet = cardSets[0] if currentCardSet.cardAmount <= cardsToRemove: cardSets.pop(0) cardsToRemove -= currentCardSet.cardAmount else: cardSets[0].cardAmount -= cardsToRemove cardsToRemove = 0 break chefsMove = not chefsMove totalCardsLeft = b elif not chefsMove: while cardsToRemove > 0: currentCardSet = cardSets[-1] if currentCardSet.cardAmount <= cardsToRemove: cardSets.pop(-1) cardsToRemove -= currentCardSet.cardAmount else: cardSets[-1].cardAmount -= cardsToRemove cardsToRemove = 0 break chefsMove = not chefsMove totalCardsLeft = b totalScore = sum([(cardSet.cardValue * cardSet.cardAmount) for cardSet in cardSets]) print(totalScore)
CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR WHILE VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR WHILE VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
for _ in range(int(input())): n, k = map(int, input().split()) A = list(map(int, input().split())) D = list(map(int, input().split())) B = list(map(int, input().split())) C = [] f1 = 0 for i in range(n): C.append([A[i], D[i]]) f1 += D[i] C.sort() if f1 != n: j = len(C) - 1 l = 0 for i in range(k): f = f1 - B[i] if (i + 1) % 2 == 0: while f > 0: if f - C[j][1] >= 0: f -= C[j][1] j -= 1 else: w = C[j][1] C[j][1] -= f f -= w else: while f > 0: if f - C[l][1] >= 0: f -= C[l][1] l += 1 else: w = C[l][1] C[l][1] -= f f -= w f1 = B[i] sum = 0 if k % 2 == 0: while l <= j and f1 >= 0: if f1 - C[l][1] > 0: sum += C[l][0] * C[l][1] f1 -= C[l][1] l += 1 else: sum += C[l][0] * f1 f1 -= C[l][1] print(sum) else: while l < j and f1 >= 0: if f1 - C[j][1] > 0: sum += C[j][0] * C[j][1] f1 -= C[j][1] j -= 1 else: sum += C[j][0] * f1 f1 -= C[j][1] print(sum) else: A.sort() j = len(A) - 1 l = 0 f1 = n for i in range(k): f = f1 - B[i] if (i + 1) % 2 == 1: A = A[f:] else: A = A[: len(A) - f] f1 = B[i] sum = 0 for j in range(len(A)): sum += A[j] print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
c = int(input()) for _ in range(c): n, k = input().split() n, k = int(n), int(k) a = list(map(int, input().split())) d = list(map(int, input().split())) b = list(map(int, input().split())) arr = [(a[i], d[i]) for i in range(n)] arr.sort() a = [arr[i][0] for i in range(n)] d = [arr[i][1] for i in range(n)] for i in range(k - 1, 0, -1): b[i] = b[i - 1] - b[i] b[0] = sum(d) - b[0] one = 0 two = 0 for i in range(k): if i % 2: one += b[i] else: two += b[i] i = 0 j = n - 1 itill = 0 jtill = 0 while itill + d[i] <= two: itill += d[i] i += 1 while jtill + d[j] <= one: jtill += d[j] j -= 1 x = y = 0 if i == j: print(a[i] * (d[i] - one - two + itill + jtill)) else: x = a[i] * (d[i] - (two - itill)) y = a[j] * (d[j] - (one - jtill)) t = 0 i += 1 j -= 1 for k in range(i, j + 1): t += a[k] * d[k] print(x + y + t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
t = int(input()) for z in range(t): n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] d = [int(x) for x in input().split()] b = [int(x) for x in input().split()] big = [[a[i], d[i]] for i in range(n)] big.sort() left = 0 right = n - 1 num_cards = sum(d) for z in range(k): target = b[z] if z % 2 == 0: idx = right while True: if num_cards >= target + big[left][1]: num_cards -= big[left][1] left += 1 else: big[left][1] -= num_cards - target num_cards = target break else: idx = left while True: if num_cards >= target + big[right][1]: num_cards -= big[right][1] right -= 1 else: big[right][1] -= num_cards - target num_cards = target break ans = 0 for i in range(left, right + 1): ans += big[i][0] * big[i][1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
T = int(input()) for testCase in range(T): N, K = list(map(int, input().split())) A = list(map(int, input().split())) D = list(map(int, input().split())) C = [] n = 0 for i in range(N): C.append([A[i], D[i]]) n += D[i] C.sort() B = list(map(int, input().split())) right = 0 left = 0 for i in range(K): if i % 2 == 0: left += n - B[i] else: right += n - B[i] n = B[i] i = 0 while left > 0: if left <= C[i][1]: C[i][1] -= left left = 0 else: left -= C[i][1] C[i][1] = 0 i += 1 i = N - 1 while right > 0: if right <= C[i][1]: C[i][1] -= right right = 0 else: right -= C[i][1] C[i][1] = 0 i -= 1 ans = 0 for i in range(N): ans += C[i][0] * C[i][1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
t = int(input()) for j in range(t): n, k = map(int, input().split(" ", 2)[:2]) list1 = list(map(int, input().split(" ", n)[:n])) list2 = list(map(int, input().split(" ", n)[:n])) list3 = list(map(int, input().split(" ", k)[:k])) list4 = [] s = sum(list2) for i in range(n): temp = [list1[i], list2[i]] list4.append(temp) qs = 0 sum1 = 0 qe = 2 * n - 1 list4.sort() for i in range(n): sum1 += list4[i][0] * list4[i][1] for i in range(k): if i % 2 == 0: c = s - list3[i] qs += c s = list3[i] elif i % 2 != 0: c = s - list3[i] qe -= c s = list3[i] qe += 1 qe = 2 * n - qe for i in range(n): if qs - list4[i][1] > 0: qs -= list4[i][1] sum1 -= list4[i][0] * list4[i][1] continue else: sum1 -= list4[i][0] * qs break for i in range(n - 1, 0, -1): if qe - list4[i][1] > 0: qe -= list4[i][1] sum1 -= list4[i][0] * list4[i][1] continue else: sum1 -= list4[i][0] * qe break print(sum1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin chinese, Russian and Vietnamese as well. Chef is playing a game with his brother Chefu. Before the game begins, C cards are placed on a table. Each card has a number written on it; since C can be very large, these numbers are described by two sequences A and D. Each of these sequences has length N; for each i (1 ≤ i ≤ N), D_{i} cards, each with number A_{i} written on it, are placed on the table. Therefore, C is equal to the sum of elements of D. Note that the elements of A don't have to be unique. You are also given a sequence B with length K. The game will last for exactly K rounds numbered 1 through K. The players alternate turns — Chef plays on odd-numbered turns and his brother on even-numbered turns. In the i-th round, the current player must select B_{i} cards currently lying on the table, keep these cards on the table and discard all other cards. Chef's goal is to maximize the sum of numbers written on cards that remain on the table after the last round, while Chefu's goal is minimize this sum. What will be the sum of numbers written on cards remaining on the table if both players play optimally? ------ Input ------ The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N and K. The second line contains N space-separated integers A_{1}, A_{2}, ..., A_{N}. The third line contains N space-separated integers D_{1}, D_{2}, ..., D_{N}. The fourth line contains K space-separated integers B_{1}, B_{2}, ..., B_{K}. ------ Output ------ For each test case, print a single line containing one integer — the sum of numbers on cards that remain on the table at the end of the game. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N, K ≤ 100,000$ $1 ≤ sum of N over all test cases ≤ 100,000$ $1 ≤ sum of K over all test cases ≤ 100,000$ $1 ≤ A_{i}, D_{i} ≤ 1,000,000 for each valid i$ $1 ≤ B_{i} ≤ C for each valid i$ $B_{i+1} < B_{i} for each valid i$ ------ Subtasks ------ Subtask #1 (20 points): $D_{i} = 1 for each valid i$ $sum of N over all test cases ≤ 1,000$ $sum of K over all test cases ≤ 1,000$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 1 4 2 1 2 3 4 2 2 2 2 6 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Example case 1: The cards that are on table initially are 1, 1, 2, 2, 3, 3, 4, 4 Chef in his turn should select 6 cards to keep and discard the rest, so he will keep 2, 2, 3, 3, 4, 4 after that Chefu should keep 3 cards and discard the rest, so he will keep 2, 2, 3 after that the game ends and sum of cards remained on the table is 2 + 2 + 3 = 7
for i in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) d = list(map(int, input().split())) b = list(map(int, input().split())) l = list(map(list, zip(a, d))) l = sorted(l, key=lambda x: x[0]) s1 = 0 s2 = 0 s = 0 tot = sum(d) for j in range(k): if j % 2 == 0: s1 += tot - b[j] else: s2 += tot - b[j] tot = b[j] for j in range(n): s += a[j] * d[j] j = 0 sj = 0 while True: sj += l[j][1] if sj <= s1: s -= l[j][0] * l[j][1] elif sj - l[j][1] <= s1: x = s1 - sj + l[j][1] s -= l[j][0] * x break else: break j += 1 j = -1 sj = 0 while True: sj += l[j][1] if sj <= s2: s -= l[j][0] * l[j][1] elif sj - l[j][1] <= s2: x = s2 - sj + l[j][1] s -= l[j][0] * x break else: break j -= 1 print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def find(root, c, p, s): if root != None: if p == c: s += root.data s = find(root.left, c, p - 1, s) s = find(root.right, c, p + 1, s) return s def trav(root, target, c): if root != None: if target == root.data: return root, c elif target < root.data: return trav(root.left, target, c - 1) else: return trav(root.right, target, c + 1) return None, -1 root1, c = trav(root, target, 0) if root1 == None: return c return find(root1, c, c, -root1.data)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NONE IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NONE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NONE RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): level = 0 is_present = False target_ind = 0 while root: if root.data == target: is_present = True break if root.data > target: root = root.left target_ind -= 1 else: root = root.right target_ind += 1 if not is_present: return -1 result = [0] def fun(root, target_ind, ind): if root == None: return if target_ind == ind: result[0] += root.data fun(root.left, target_ind, ind - 1) fun(root.right, target_ind, ind + 1) fun(root, target_ind, target_ind) return result[0] - target
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def find(root): if root == None: return None if root.data == target: return root elif root.data > target: return find(root.left) else: return find(root.right) loc = find(root) if loc == None: return -1 q = [[loc, 0]] s = 0 while q: for i in range(len(q)): ans = q.pop(0) node = ans[0] if ans[1] == 0: s += ans[0].data if node.left: q.append([node.left, ans[1] - 1]) if node.right: q.append([node.right, ans[1] + 1]) return s - target
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
def verticalSumUtil(root, hd, Map): if root == None: return verticalSumUtil(root.left, hd - 1, Map) if hd in Map.keys(): Map[hd] = Map[hd] + root.data else: Map[hd] = root.data verticalSumUtil(root.right, hd + 1, Map) class Solution: def calculate(self, root): Map = {} verticalSumUtil(root, 0, Map) return Map[0] def findNode(self, root, target): if root.data == target: return root elif root.left and target < root.data: return self.findNode(root.left, target) elif root.right and target > root.data: return self.findNode(root.right, target) def verticallyDownBST(self, root, target): nd = self.findNode(root, target) if not nd: return -1 summation = self.calculate(nd) - nd.data return summation
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): return self.dfs(root, target, False, 0, 0) - 1 def dfs(self, r, targetValue, hasFound, targetPos, currPos): if r is None: return 0 s = 0 if hasFound and targetPos == currPos: s += r.data elif r.data == targetValue: hasFound = True targetPos = currPos s += 1 s += self.dfs(r.left, targetValue, hasFound, targetPos, currPos - 1) s += self.dfs(r.right, targetValue, hasFound, targetPos, currPos + 1) return s
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): stack = [root] ans = None while stack: root_val = stack.pop() if root_val is None: continue if root_val.data == target: ans = root_val break stack.append(root_val.left) stack.append(root_val.right) if ans == None: return -1 def findans(root, target): stack = [(root, 0)] dic = {} our_pos = 0 while stack: root_val, pos = stack.pop() if root_val is None: continue if root_val.data == target: our_pos = pos if pos not in dic: dic[pos] = [root_val.data] else: dic[pos].append(root_val.data) stack.append((root_val.left, pos - 1)) stack.append((root_val.right, pos + 1)) ans = 0 add = False for i in dic[our_pos]: if add: ans += i if i == target: add = True return ans return findans(ans, target)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR NONE WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NONE IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NONE IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def sum_below(node, level): while node and node.data != target: if node.data < target: node = node.right level += 1 else: node = node.left level -= 1 if not node: return -1 q, s = [], 0 if node.left: q.append((node.left, level - 1)) if node.right: q.append((node.right, level + 1)) while q: nq = [] for n, l in q: if l == level: s += n.data if n.left: nq.append((n.left, l - 1)) if n.right: nq.append((n.right, l + 1)) q = nq return s return sum_below(root, 0)
CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR LIST NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def __init__(self): self.targetNode = None self.nodeSum = 0 def verticallyDownBST(self, root, target): result = self.findNode(root, target) if result == False: return -1 self.downwardTraversal(self.targetNode, 0) return self.nodeSum - self.targetNode.data def downwardTraversal(self, node, w): if node is None: return if w == 0: self.nodeSum += node.data self.downwardTraversal(node.left, w + -1) self.downwardTraversal(node.right, w + 1) def findNode(self, node, target): if node is None: return False if node.data == target: self.targetNode = node return True elif node.data > target: return self.findNode(node.left, target) elif node.data < target: return self.findNode(node.right, target)
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR ASSIGN VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, tar): def helper(node, data): if not node: return 0 ans = 0 if data == 0: ans += node.data ans += helper(node.left, data - 1) + helper(node.right, data + 1) return ans def find(node): if not node: return if node.data > tar: find(node.left) elif node.data < tar: find(node.right) else: self.node = node return self.node = None find(root) if self.node == None: return -1 return helper(self.node.left, -1) + helper(self.node.right, 1)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: ans = 0 def rootPresent(self, root, target): if root == None: return False if root.data > target: return self.rootPresent(root.left, target) elif root.data < target: return self.rootPresent(root.right, target) else: self.targ = root return True def solve(self, rot, d): if rot == None: return if d == 0: self.ans += rot.data self.solve(rot.left, d - 1) self.solve(rot.right, d + 1) def verticallyDownBST(self, root, target): present = self.rootPresent(root, target) if present == False: return -1 self.solve(self.targ, 0) return self.ans - self.targ.data
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def find(root, target): if root == None: return False if root.data > target: return find(root.left, target) elif root.data < target: return find(root.right, target) else: self.req = root return True present = find(root, target) if present == False: return -1 def solve(root, d): if root == None: return if d == 0: self.ans += root.data solve(root.left, d - 1) solve(root.right, d + 1) self.ans = 0 solve(self.req, 0) return self.ans - self.req.data
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
MAIN = {(0): 0} class Solution: def verticallyDownBST(self, root, target): hs = 0 while root != None: if root.data == target: self.get_sum(root, hs, "o") res = MAIN[0] MAIN[0] = 0 if res: return res return 0 elif target < root.data: root = root.left else: root = root.right return -1 def get_sum(self, root, hs, abcd): if root != None: l = self.get_sum(root.left, hs - 1, "a") if not hs and abcd != "o": MAIN.update({hs: MAIN[hs] + root.data if MAIN[hs] else root.data}) r = self.get_sum(root.right, hs + 1, "a")
ASSIGN VAR DICT NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NONE IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR RETURN VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NUMBER FUNC_DEF IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING IF VAR VAR STRING EXPR FUNC_CALL VAR DICT VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def getsum(self, root, pos): if root is None: return 0 return ( self.getsum(root.left, pos - 1) + self.getsum(root.right, pos + 1) + (root.data if pos == 0 else 0) ) def verticallyDownBST(self, root, target): if root is None: return -1 if root.data == target: return self.getsum(root.left, -1) + self.getsum(root.right, 1) if root.data < target: return self.verticallyDownBST(root.right, target) return self.verticallyDownBST(root.left, target)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): self.foundTarget = False self.target = target self.ans = 0 self.find(root) if not self.foundTarget: return -1 return self.ans - target def verticalTraversal(self, r, val): if r is not None: if self.foundTarget and self.targetLevel == val: self.ans += r.data self.verticalTraversal(r.left, val - 1) self.verticalTraversal(r.right, val + 1) def find(self, r): if r is not None: if not self.foundTarget and r.data == self.target: self.foundTarget = True self.targetLevel = 0 self.verticalTraversal(r, 0) return elif self.foundTarget: return self.find(r.left) self.find(r.right)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NONE IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN IF VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def __init__(self): self.levelFind = None self.colFind = None self.find = False self.summ = 0 self.startNode = None def traverse(self, node, target, level, col): if not node: return -1 if node.data < target: self.traverse(node.right, target, level + 1, col + 1) elif node.data > target: self.traverse(node.left, target, level + 1, col - 1) else: self.levelFind = level self.colFind = col self.find = True self.startNode = node def traverseAfter(self, node, level, col): if not node: return if level > self.levelFind and col == self.colFind: self.summ += node.data self.traverseAfter(node.left, level + 1, col - 1) self.traverseAfter(node.right, level + 1, col + 1) def verticallyDownBST(self, root, target): if not root: return -1 self.traverse(root, target, 0, 0) if self.find: self.traverseAfter(self.startNode, self.levelFind, self.colFind) return self.summ return -1
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF VAR RETURN IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): l = [] def findColumn(root, target, l, v, level): if root == None: return if root.data == target: l.append(v) l.append(level) l.append(root) return findColumn(root.left, target, l, v - 1, level + 1) findColumn(root.right, target, l, v + 1, level + 1) findColumn(root, target, l, 0, 0) if len(l) == 0: return -1 v = l[0] level = l[1] root1 = l[2] def findSum(root, v, column, tlevel, level): if root == None: return 0 if column == v and root.data != target: return ( root.data + findSum(root.left, v, column - 1, tlevel, level + 1) + findSum(root.right, v, column + 1, tlevel, level + 1) ) return findSum(root.left, v, column - 1, tlevel, level + 1) + findSum( root.right, v, column + 1, tlevel, level + 1 ) return findSum(root1, 0, 0, level, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
import sys sys.setrecursionlimit(1000000) class Solution: def __init__(self): self.answer = -1 self.target_position = 0 self.start_node = None self.total = 0 def findTarget(self, root, target, target_position): if root is None: return if root.data == target: self.target_position = target_position self.start_node = root return elif root.data < target: self.findTarget(root.right, target, target_position + 1) else: self.findTarget(root.left, target, target_position - 1) def findVertical(self, start_node, current_position): if start_node is None: return if current_position == self.target_position: self.total += start_node.data self.findVertical(start_node.right, current_position + 1) self.findVertical(start_node.left, current_position - 1) def verticallyDownBST(self, root, target): self.findTarget(root, target, 0) if self.start_node is None: return -1 curr = self.target_position self.findVertical(self.start_node.right, curr + 1) self.findVertical(self.start_node.left, curr - 1) return self.total
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def findVerticalSum(self, root, pos, sum): if root is None: return 0 sum += self.findVerticalSum(root.left, pos - 1, sum) + self.findVerticalSum( root.right, pos + 1, sum ) if pos == 0: sum += root.data return sum def solve(self, root, target): if root is None: return -1 if root.data == target: return self.findVerticalSum(root, 0, 0) - root.data x, y = -1, -1 if root.data < target: x = self.solve(root.right, target) else: y = self.solve(root.left, target) return max(x, y) def verticallyDownBST(self, root, target): return self.solve(root, target)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def findnode(self, root): if not root: return if root.data == self.target: return root return self.findnode(root.left) or self.findnode(root.right) def verticalsum(self, root, parity): if not root: return if parity == 0: self.sum += root.data self.verticalsum(root.left, parity - 1) self.verticalsum(root.right, parity + 1) def verticallyDownBST(self, root, target): self.target = target cur = self.findnode(root) if not cur: return -1 self.sum = 0 self.verticalsum(cur.left, -1) self.verticalsum(cur.right, +1) return self.sum
CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR RETURN IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def __init__(self): self.flag = False self.ans = 0 self.g = 0 self.start = None def dfs(self, root, trt, gg): if root is None: return if root.data == trt and not self.flag: self.g = gg self.start = root return self.dfs(root.left, trt, gg - 1) self.dfs(root.right, trt, gg + 1) if self.flag: if self.g == gg: self.ans += root.data def verticallyDownBST(self, root, target): self.dfs(root, target, 0) self.flag = True if self.start is None: return -1 self.dfs(self.start, target, self.g) return self.ans - self.start.data
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR IF VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def searchTarget(self, root, target): if root == None: return -1 if root.data == target: return root if root.data > target: return self.searchTarget(root.left, target) else: return self.searchTarget(root.right, target) def verticalSum(self, root, temp, sum_): if root.left != None: if temp - 1 == 0: sum_[0] = sum_[0] + root.left.data self.verticalSum(root.left, temp - 1, sum_) if root.right != None: if temp + 1 == 0: sum_[0] = sum_[0] + root.right.data self.verticalSum(root.right, temp + 1, sum_) def verticallyDownBST(self, root, target): node = self.searchTarget(root, target) if node == -1: return -1 sum_ = [0] self.verticalSum(node, 0, sum_) return sum_[0]
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NONE IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): arr = [0, 0] node = [0] found = self.find(root, target, 0, 0, arr, node) if not found: return -1 return self.getsum(node[0], target, arr[0], arr[1], arr) def find(self, root, target, h, v, arr, node): if root == None: return False if root.data == target: arr[0] = h arr[1] = v node[0] = root return True if root.left: ans = self.find(root.left, target, h + 1, v - 1, arr, node) if ans: return True if root.right: ans = self.find(root.right, target, h + 1, v + 1, arr, node) if ans: return True return False def getsum(self, root, target, h, v, arr): if root == None: return 0 if h > arr[0] and v == arr[1]: return ( root.data + self.getsum(root.left, target, h + 1, v - 1, arr) + self.getsum(root.right, target, h + 1, v + 1, arr) ) return self.getsum(root.left, target, h + 1, v - 1, arr) + self.getsum( root.right, target, h + 1, v + 1, arr )
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR RETURN NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR RETURN NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): if not root: return def t_node(root, target): if not root: return if target < root.data: return t_node(root.left, target) elif target > root.data: return t_node(root.right, target) else: return root target_node = t_node(root, target) if not target_node: return -1 custom_queue = [(target_node, 0, 0)] res = 0 while custom_queue: parent, index, level = custom_queue[0] custom_queue = custom_queue[1:] if index == 0 and level != 0: res += parent.data if parent.left: custom_queue.append((parent.left, index - 1, level + 1)) if parent.right: custom_queue.append((parent.right, index + 1, level + 1)) return res
CLASS_DEF FUNC_DEF IF VAR RETURN FUNC_DEF IF VAR RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
def findRecurse(root, target, depth, column): foundNode = None if root: if root.data == target: return root if root: foundNode = findRecurse(root.left, target, depth + 1, column - 1) if foundNode is None: foundNode = findRecurse(root.right, target, depth + 1, column + 1) return foundNode def sumRecurse(root, depth, column): retSum = 0 if root: if depth > 0 and column == 0: retSum += root.data retSum += sumRecurse(root.left, depth + 1, column - 1) retSum += sumRecurse(root.right, depth + 1, column + 1) return retSum class Solution: def __init__(self): self.depth = 0 def verticallyDownBST(self, root, target): nodeStart = findRecurse(root, target, 0, 0) if nodeStart is not None: return sumRecurse(nodeStart, 0, 0) else: return -1
FUNC_DEF ASSIGN VAR NONE IF VAR IF VAR VAR RETURN VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NONE RETURN FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def dfs(self, root, targetValue, hasFound=False, targetPos=0, currPos=0): if root is None: return 0 ans = 0 if hasFound and targetPos == currPos: ans += root.data if not hasFound and root.data == targetValue: hasFound = True targetPos = currPos ans += 1 ans += self.dfs(root.left, targetValue, hasFound, targetPos, currPos - 1) ans += self.dfs(root.right, targetValue, hasFound, targetPos, currPos + 1) return ans def verticallyDownBST(self, root, target): return self.dfs(root, target) - 1
CLASS_DEF FUNC_DEF NUMBER NUMBER NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): target_node = self.find_target(root, target) if target_node is None: return -1 return self.vertical_sum(target_node, 0) - target_node.data def vertical_sum(self, node, column): _sum = 0 if node is not None: if column == 0: _sum += node.data _sum += self.vertical_sum(node.left, column - 1) _sum += self.vertical_sum(node.right, column + 1) return _sum def find_target(self, node, target): if node is not None: if node.data == target: return node if node.data > target: return self.find_target(node.left, target) else: return self.find_target(node.right, target)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def __init__(self): self.res = -1 self.s = 0 def check(self, a, r, k): if a == None: return 0 o = self.check(a.left, r - 1, k) p = self.check(a.right, r + 1, k) if a.data == k: self.res = a return o + p + a.data def io(self, a, r, k): if a == None: return if a.data != k and r == 0: self.s += a.data self.io(a.left, r - 1, k) self.io(a.right, r + 1, k) def verticallyDownBST(self, root, target): self.check(root, 0, target) if self.res == -1: return -1 self.io(self.res, 0, target) return self.s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): tmp = root found = False y = 0 while tmp: if target == tmp.data: found = True break elif target < tmp.data: y -= 1 tmp = tmp.left else: y += 1 tmp = tmp.right if not found: return -1 self.sum = 0 self.getSum(tmp, y, y) return self.sum - target def getSum(self, root, y, dir): if root == None: return if y == dir: self.sum += root.data self.getSum(root.left, y - 1, dir) self.getSum(root.right, y + 1, dir) return
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): sol = False res = 0 targetCol = 0 def dfs(node, solved, col): nonlocal sol, targetCol, res if not node: return if solved and targetCol == col: res += node.data if node.data == target: targetCol = col sol = True dfs(node.left, True, col - 1) dfs(node.right, True, col + 1) else: dfs(node.left, solved, col - 1) dfs(node.right, solved, col + 1) dfs(root, False, 0) return res if sol else -1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): rt = root while True: if rt.data == target: break elif rt.data > target: if rt.left: rt = rt.left else: return -1 elif rt.right: rt = rt.right else: return -1 d = {} d[rt.data] = 0 def inorder(root): if root.left: d[root.left.data] = d[root.data] - 1 inorder(root.left) if root.right: d[root.right.data] = d[root.data] + 1 inorder(root.right) inorder(rt) d1 = {} for i in d: if d[i] in d1: d1[d[i]].append(i) else: d1[d[i]] = [i] return sum(d1[0]) - target
CLASS_DEF FUNC_DEF ASSIGN VAR VAR WHILE NUMBER IF VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR RETURN NUMBER IF VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def __init__(self): self.s = 0 def verticallyDownBST(self, root, target): return self.findsum(root, target, 0, None, False) def findsum(self, root, target, s, pos, takeNode): root = self.getNode(root, target, s, pos) if root == None: return -1 return self.getNodetoadd(root, target, s, 0) def getNode(self, root, target, s, pos): if root == None: return if root.data == target: pos = 0 return root x = self.getNode(root.left, target, s, pos) y = self.getNode(root.right, target, s, pos) if x: return x else: return y def getNodetoadd(self, root, target, s, pos): if root == None: return if pos == 0 and root.data != target: self.s += root.data x = self.getNodetoadd(root.left, target, self.s, pos - 1) y = self.getNodetoadd(root.right, target, self.s, pos + 1) return self.s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER NONE NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN IF VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def search(self, node, target): if self.found: return if node.data == target: self.found = node return if node.left: self.search(node.left, target) if node.right: self.search(node.right, target) def trav(self, node, index): if index == 0: self.sum += node.data if node.left: self.trav(node.left, index - 1) if node.right: self.trav(node.right, index + 1) def verticallyDownBST(self, root, target): self.found = None self.search(root, target) if not self.found: return -1 self.sum = -self.found.data self.trav(self.found, 0) return self.sum
CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): if not root: return def t_node(root, target): if not root: return if target < root.data: return t_node(root.left, target) elif target > root.data: return t_node(root.right, target) else: return root target_node = t_node(root, target) if not target_node: return -1 custom_queue = [(target_node, 0, 0)] res = 0 idx = 0 flag = False v_idx, lvl = 0, 0 while custom_queue: parent, index, level = custom_queue[0] custom_queue = custom_queue[1:] if parent.data == target: flag, v_idx, lvl = True, index, level if flag: if index == v_idx and lvl != level: res += parent.data if parent.left: custom_queue.append((parent.left, index - 1, level + 1)) if parent.right: custom_queue.append((parent.right, index + 1, level + 1)) return res
CLASS_DEF FUNC_DEF IF VAR RETURN FUNC_DEF IF VAR RETURN IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR IF VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def down(node, direction): nonlocal res if node: if direction == 0: res += node.data down(node.left, direction - 1) down(node.right, direction + 1) def find(node, target): nonlocal target_node if target_node == None: if node: if node.data > target: find(node.left, target) elif node.data < target: find(node.right, target) else: target_node = node down(node, 0) res = -target target_node = None find(root, target) if res == -target: return -1 return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NONE IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): def helper(root, x): if root is None: return 0 count = 0 if x == 0: count += root.data count += helper(root.left, x - 1) count += helper(root.right, x + 1) return count if root is None: return -1 if root.data == target: count = helper(root, 0) return count - root.data if target < root.data: return self.verticallyDownBST(root.left, target) else: return self.verticallyDownBST(root.right, target)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR IF VAR NONE RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): dummy = None while root: if root.data == target: dummy = root break if root.data > target: root = root.left else: root = root.right if not dummy: return -1 ans = [0] def fun(position, root, ref): if not root: return fun(position - 1, root.left, ref) fun(position + 1, root.right, ref) if position == 0 and root != ref: ans[0] += root.data fun(0, dummy, dummy) return ans[0]
CLASS_DEF FUNC_DEF ASSIGN VAR NONE WHILE VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST NUMBER FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
def lo1(root, target): if root is not None: lo1(root.left, target) if root.data == target: m[0] = root m[1] = 1 lo1(root.right, target) def lo2(root, p): if root is not None: lo2(root.left, p - 1) if p == 0: l.append(root.data) lo2(root.right, p + 1) class Solution: def verticallyDownBST(self, root, target): global l global m l = [] m = [0, 0] lo1(root, target) if m[1] == 0: return -1 lo2(m[0], 0) if l: return sum(l) - target return 0
FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def verticallyDownBST(self, root, target): searchNode = self.findNode(root, target) if searchNode == None: return -1 add = [0] self.verticalSum(searchNode, searchNode, 0, add) return add[0] def findNode(self, root, target): if root != None: if root.data == target: return root left, right = None, None left = self.findNode(root.left, target) if left != None: return left right = self.findNode(root.right, target) if right != None: return right return None def verticalSum(self, target, node, direction, add): if node != None: self.verticalSum(target, node.left, direction - 1, add) self.verticalSum(target, node.right, direction + 1, add) if target != node and direction == 0: add[0] += node.data
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR NUMBER FUNC_DEF IF VAR NONE IF VAR VAR RETURN VAR ASSIGN VAR VAR NONE NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR RETURN NONE FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
def find(root, target): if root == None: return -1 if root.data == target: return root if root.data > target: return find(root.left, target) return find(root.right, target) class Solution: def verticallyDownBST(self, root, target): node = find(root, target) if node == -1: return -1 queue = [] su = 0 queue.append([node, 0]) while len(queue): k = queue.pop(0) if k[1] == 0: su += k[0].data if k[0].left != None: queue.append([k[0].left, k[1] - 1]) if k[0].right != None: queue.append([k[0].right, k[1] + 1]) return su - node.data
FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
class Solution: def findTarget(self, root, target): temp = root while temp: if temp.data == target: break elif temp.data < target: temp = temp.right else: temp = temp.left if temp is None: return -1 n = 0 total = [] self.POTD(temp, n, total) return sum(total) - temp.data def POTD(self, root, n, ans): if root is None: return if n == 0: ans.append(root.data) if root.left: self.POTD(root.left, n - 1, ans) if root.right: self.POTD(root.right, n + 1, ans) def verticallyDownBST(self, root, target): temp = self.findTarget(root, target) return temp
CLASS_DEF FUNC_DEF ASSIGN VAR VAR WHILE VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NONE RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given a Binary Search Tree with unique node values and a target value. You have to find the node whose data is equal to target and return the sum of all descendant (of target) node's data which are vertically below the target node. Initially, you are at the root node. Note: If target node is not present in bst then return -1. Example 1: Input: Target = 35 Output: 32 Explanation: Vertically below 35 is 32. Example 2: Input: Target = 25 Output: 52 Explanation: Vertically below 25 is 22, 30 and their sum is 52. Your Task: You don't need to read input or print anything. Your task is to complete the function verticallyDownBST() which takes BST(you are given root node of bst ) and target as input, and return an interger value as the sum of vertically down nodes from target. If target is not present in bst then return -1. Expected Time Complexity: O( n ), n is number of nodes. Expected Auxiliary Space: O(Height of the tree). Constraints: 1 <= n < 10^{6} 1 <= target <= n 1 <= node.data < 10^{6}
ping = None class Solution: def fun(self, root, level, ans): if root: if level == 0: ans += root.data ans = self.fun(root.left, level - 1, ans) ans = self.fun(root.right, level + 1, ans) return ans def check(self, root, target): global ping if not root: return if root.data == target: ping = root return self.check(root.left, target) self.check(root.right, target) return def verticallyDownBST(self, root, target): global ping ans = 0 if not root: return -1 self.check(root, target) if ping: ans = self.fun(ping, 0, ans) ans -= ping.data else: return -1 ping = None return ans
ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR NONE RETURN VAR