description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
for t in range(int(input())): n, k = map(int, input().split()) dp = [([0] * 1013) for i in range(1013)] ans = 1 for i in range(1, k + 1): for j in range(n + 1): dp[i][j] = ( 1 if j == 0 or i == 1 else (dp[i - 1][n - j] + dp[i][j - 1]) % (10**9 + 7) ) print(dp[k][n])
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 BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from sys import stdin mod = pow(10, 9) + 7 t = int(stdin.readline().rstrip()) matrix_l = [([0] * 1001) for _ in range(1000)] matrix_r = [([0] * 1001) for _ in range(1000)] for _ in range(t): n, k = list(map(int, stdin.readline().rstrip().split())) for i in range(k): matrix_l[i][0] = 1 matrix_r[i][0] = 1 for i in range(n + 1): matrix_l[0][i] = 1 matrix_r[0][i] = 1 for i in range(1, k): for j in range(1, n + 1): matrix_l[i][j] = (matrix_l[i][j - 1] + matrix_r[i - 1][n - j]) % mod matrix_r[i][j] = (matrix_l[i - 1][n - j] + matrix_r[i][j - 1]) % mod print(matrix_r[k - 1][n])
ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
e = 1000000007 ma = {} def get(n, k, f): if ma[n][k][f] == -1: if k == 1: ma[n][k][f] = 1 elif k == 2: ma[n][k][f] = f + 1 elif f == 0: ma[n][k][f] = 1 else: res = get(n, k - 1, n - f) + get(n, k, f - 1) if res > e: res %= e ma[n][k][f] = max(0, res) return ma[n][k][f] t = int(input()) for i in range(t): n, k = map(int, input().split()) if n not in ma: ma[n] = [([-1] * (n + 1)) for i in range(k + 1)] else: if len(ma[n][0]) <= n: prevn = ma[n][0] for i in range(len(ma[n])): for j in range(n - prevn): ma[n][i].append(-1) if len(ma[n]) <= k: ma[n] += [([-1] * (n + 1)) for i in range(k - len(ma[n]) + 1)] ans = get(n, k, n) print(ans)
ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR VAR 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 IF VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
T = int(input()) while T: n, k = map(int, input().split()) ps = [([0] * 1007) for i in range(1007)] ans = 1 if k > 1: ans += n for i in range(1, n + 1): ps[k - 1][i] = 1 turn = 0 for i in range(k - 2, 0, -1): if not turn: for j in range(n - 1, 0, -1): ps[i][j] = ps[i][j + 1] + ps[i + 1][j + 1] ps[i][j] %= 1000000007 ans += ps[i][j] ans %= 1000000007 else: for j in range(2, n + 1): ps[i][j] = ps[i][j - 1] + ps[i + 1][j - 1] ps[i][j] %= 1000000007 ans += ps[i][j] ans %= 1000000007 turn = 1 - turn T -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.readline MOD = 1000000007 for _ in range(int(input())): n, k = map(int, input().split()) if k == 1: print(1) continue if n == 1: print(2) continue dp1 = [[(0) for i in range(n + 2)] for j in range(k + 1)] dp2 = [[(0) for i in range(n + 2)] for j in range(k + 1)] s1 = [[(0) for i in range(n + 2)] for j in range(k + 1)] s2 = [[(0) for i in range(n + 2)] for j in range(k + 1)] ans = 1 for j in range(1, n + 1): dp1[k][j] = 1 s1[k][j] = j for i in range(k - 1, 0, -1): for j in range(1, n + 1): dp1[i][j] = (s2[i + 1][1] - s2[i + 1][j] + MOD) % MOD s1[i][j] = (s1[i][j - 1] + dp1[i][j]) % MOD for j in range(n, 0, -1): dp2[i][j] = (s1[i + 1][n] - s1[i + 1][j] + MOD) % MOD s2[i][j] = (s2[i][j + 1] + dp2[i][j]) % MOD ans = (ans + s1[i + 1][n] + s2[i + 1][1]) % MOD print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import time from itertools import accumulate t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) M = 10**9 + 7 if n == 1: print(1 + int(k > 1)) else: crossing = [[None for p in range(n)] for age in range(k + 1)] crossing[k] = [(1) for p in range(n)] crossing[k - 1] = list(accumulate(crossing[k])) for age in reversed(range(1, k - 1)): crossing[age][:-1] = list(accumulate(crossing[age + 1][-2::-1])) crossing[age][-1] = crossing[age][-2] for p in range(n): crossing[age][p] %= M print(sum([crossing[age][-1] for age in range(1, k + 1)]) % M)
IMPORT 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 BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
mod = 10**9 + 7 for t in range(int(input())): n, k = map(int, input().split()) n -= 1 state = [1] * n total = 1 if k == 1 else 2 nextState = [0] * n for _ in range(k - 1): temp = 0 for i in range(n): temp = (temp + state[i]) % mod nextState[n - i - 1] = temp total = (total + temp) % mod state = nextState.copy() print(total)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int, sys.stdin.readline().rstrip().split()) def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int, sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) t = I() mod = 10**9 + 7 for _ in range(t): n, k = MI() if k == 1: ans = 1 elif k == 2: ans = 1 + n else: ans = 1 + n X = [i for i in range(n - 1, 0, -1)] for i in range(k - 2): ans += sum(X) ans %= mod if i % 2 == 0: for j in range(1, n - 1): X[j] += X[j - 1] X[j] %= mod else: for j in range(n - 3, -1, -1): X[j] += X[j + 1] X[j] %= mod print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.readline mod = 10**9 + 7 for _ in range(int(input())): n, k = map(int, input().split()) dp = [([0] * (n + 1)) for _ in range(k + 1)] for i in range(1, k + 1): cur = 1 for j in range(n + 1): dp[i][j] = cur if j < n: cur = (cur + dp[i - 1][n - 1 - j]) % mod print(dp[-1][-1])
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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
def helper(n, k): if k == 1: return 1 mod = 1000000007 res = n + 1 k -= 1 dp = [1] * n flag = -1 while k > 1: ndp = [0] * n if flag == -1: for i in range(n - 2, -1, -1): ndp[i] = (dp[i + 1] + ndp[i + 1]) % mod flag = 1 elif flag == 1: for i in range(1, n): ndp[i] = (dp[i - 1] + ndp[i - 1]) % mod flag = -1 res = (res + sum(ndp)) % mod dp = ndp k -= 1 return res t = int(input()) for i in range(t): n, k = map(int, input().split()) print(helper(n, k))
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MOD = 1000000000.0 + 7 T = int(input()) coldp = [] def getsum(row1, row2, col): global coldp row1 -= 1 if row1 < 0: return coldp[row2][col] else: return coldp[row2][col] - coldp[row1][col] for t in range(T): n, k = [int(i) for i in input().split()] dp = [[(-1) for i in range(k + 1)] for j in range(n + 1)] coldp = [[(0) for i in range(k + 1)] for j in range(n + 1)] dp[0][0] = 0 colsum = 0 newcolsum = 0 coldp[0][0] = 0 mem = dict() for col in range(k + 1): for row in range(n + 1): if col == 0: dp[row][col] = 0 coldp[row][col] = 0 elif row == 0: dp[row][col] = 1 coldp[row][col] = 1 elif col == 1: dp[row][col] = 1 coldp[row][col] = (dp[row][col] + coldp[row - 1][col]) % MOD else: ans = 0 if col < 3: for val in range(n - 1, n - row - 1, -1): ans += dp[val][col - 1] else: if n - row - 1 < 0: rem = 0 else: rem = coldp[n - row - 1][col - 1] ans = coldp[n - 1][col - 1] - rem dp[row][col] = (1 + ans) % MOD coldp[row][col] = (coldp[row - 1][col] + dp[row][col]) % MOD print(int(dp[n][k]))
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF VAR NUMBER IF VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
for i in range(int(input())): n, k = map(int, input().split()) dp = [([0] * n) for j in range(k - 1)] if k > 1: for j in range(n): dp[0][j] = 1 for j in range(1, k - 1): if j % 2 == 1: for l in range(1, n): dp[j][l] = (dp[j][l - 1] + dp[j - 1][l - 1]) % 1000000007 else: for l in range(n - 2, -1, -1): dp[j][l] = (dp[j][l + 1] + dp[j - 1][l + 1]) % 1000000007 sumo = 0 for j in range(k - 1): for l in range(n): sumo = (sumo + dp[j][l]) % 1000000007 print((1 + sumo) % 1000000007)
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 BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
def sol(n, k): m = 1000000007 if k == 1: return 1 elif n == 1: return 2 arr = [[(0) for j in range(n)] for p in range(2)] sum = 0 if k < 3: return (sum + n + 1) % m for i in range(n): sum += i arr[(k - 2) % 2][n - i - 1] = i k = k - 2 buf_sum = sum while k > 1: buf = storage = 0 ind = k % 2 ind1 = (k + 1) % 2 for i in range(n): buf += arr[ind][n - i - 1] buf %= m arr[ind1][i] = buf_sum - buf sum += buf_sum - buf storage += buf_sum - buf storage %= m sum = sum % m k -= 1 buf_sum = storage return sum + n + 1 for t in range(int(input())): data = input().split() n = int(data[0]) k = int(data[1]) print(sol(n, k))
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MODULE = 10**9 + 7 def r(n, k): a = [(1) for _ in range(n + 1)] new_a = [(0) for _ in range(n + 1)] cur_k = 1 while cur_k < k: new_a[0] = 1 for i in range(1, n + 1): new_a[i] = (new_a[i - 1] + a[n - i]) % MODULE cur_k += 1 a, new_a = new_a, a return a[-1] def main(): t = int(input()) for _ in range(t): n, k = map(int, input().split()) print(r(n, k)) main()
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
mod = 10**9 + 7 cas = int(input()) while cas: cas -= 1 n, k = map(int, input().split()) dp = [(1) for i in range(n)] ans = 1 for i in range(k - 1): tmp = 0 for j in range(n): ans = (ans + dp[j]) % mod if i % 2 == 0: for j in range(n - 1, -1, -1): tt = dp[j] dp[j] = tmp tmp = (tmp + tt) % mod else: for j in range(n): tt = dp[j] dp[j] = tmp tmp = (tmp + tt) % mod print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
mod = int(1000000000.0 + 7) def iter_solver(N, K): dp = [[[(-1) for _ in range(2)] for __ in range(K + 1)] for ___ in range(N + 1)] for i in range(1, N + 1): dp[i][1][0] = dp[i][1][1] = 1 for k in range(2, K + 1): for n in range(N, 0, -1): ans = 2 if n < N: ans += dp[n + 1][k][0] - 1 ans %= mod if n > 1: ans += dp[n - 1][k - 1][1] - 1 ans %= mod dp[n][k][0] = ans for n in range(1, N + 1): ans = 2 if n < N: ans += dp[n + 1][k - 1][0] - 1 ans %= mod if n > 1: ans += dp[n - 1][k][1] - 1 ans %= mod dp[n][k][1] = ans return dp[1][K][0] t = int(input()) while t > 0: t -= 1 n, k = list(map(int, input().split())) print(iter_solver(n, k))
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR RETURN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
mod = 10**9 + 7 def solve(n, k): left = [([0] * n) for _ in range(k + 1)] right = [([0] * n) for _ in range(k + 1)] res = 1 for i in range(n): right[k][i] = 1 for a in range(k - 1, 0, -1): for i in range(1, n): right[a][i] = left[a + 1][i - 1] + right[a][i - 1] right[a][i] %= mod res += sum(left[a + 1]) res %= mod for i in range(n - 2, -1, -1): left[a][i] = right[a + 1][i + 1] + left[a][i + 1] left[a][i] %= mod res += sum(right[a + 1]) res %= mod return res for _ in range(int(input())): n, k = map(int, input().split()) print(solve(n, k))
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys from sys import stdin def solve(N, K): res_array = [0] * K if K == 1: return 1 if K == 2: return 1 + N res_array[0] = 1 res_array[1] = N old_array = [1] * (N - 1) for i in range(2, K): new_array = [0] * (N - 1) val = 0 for j in range(N - 1): val += old_array[j] val %= 10**9 + 7 new_array[N - 1 - j - 1] = val old_array = [x for x in new_array] res_array[i] = sum(new_array) % (10**9 + 7) return sum(res_array) % (10**9 + 7) def run(): out = "" T = int(input()) for i in range(T): N, K = [int(x) for x in stdin.readline().split()] print(solve(N, K)) run()
IMPORT FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
t = int(input()) mod = 1000000007 for _ in range(t): n, k = list(map(int, input().split())) if k == 1: print(1) continue if n == 1: print(2) continue a = [1] * n total = 1 total += n k -= 1 r = True if k == 1: print(total) else: for i in range(k - 1): temp = [0] * n if r: for j in range(n - 2, -1, -1): temp[j] = (temp[j + 1] + a[j + 1]) % mod total = (total + temp[j + 1] + a[j + 1]) % mod else: for j in range(1, n): temp[j] = (temp[j - 1] + a[j - 1]) % mod total = (total + temp[j - 1] + a[j - 1]) % mod r = not r a = temp print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from sys import gettrace, stdin if gettrace(): def inputi(): return input() else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() MOD = 1000000007 def solve(): n, k = map(int, input().split()) if k == 1: print(1) return counts = [([0] * (k + 1)) for _ in range(n)] for i in range(n): counts[i][k] = 1 res = 2 for j in range(k - 1, 0, -1): if (k - j) % 2 == 0: for i in range(1, n): counts[i][j] = (counts[i - 1][j] + counts[i - 1][j + 1]) % MOD res = (res + counts[n - 1][j]) % MOD else: for i in range(n - 2, -1, -1): counts[i][j] = (counts[i + 1][j] + counts[i + 1][j + 1]) % MOD res = (res + counts[0][j]) % MOD print(res) def main(): t = int(input()) for _ in range(t): solve() main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) m = 10**9 + 7 dp = [] for i in range(0, 1013): dp.append([0] * 1013) for i in range(1, k + 1): for j in range(0, n + 1): if j == 0 or i == 1: dp[i][j] = 1 else: dp[i][j] = (dp[i - 1][n - j] + dp[i][j - 1]) % m return dp[k][n] for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys get_string = lambda: sys.stdin.readline().strip() get_intmap = lambda: map(int, get_string().split()) for t in range(int(get_string())): n, k = get_intmap() m = int(1000000000.0 + 7) current_decay_age = k total_particles = 1 right = 1 start = 1 if current_decay_age > 1: l = [1] * n total_particles += n current_decay_age -= 1 while current_decay_age > 1: tmp_arr = [0] * n tmp = 0 for ind in range(n - 1): tmp += l[ind + 1] % m tmp_arr[ind] = tmp total_particles = (total_particles + sum(tmp_arr)) % m l = tmp_arr[::-1] current_decay_age -= 1 print(total_particles)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
t = int(input()) mod = 10**9 + 7 for _ in range(t): n, k = map(int, input().split()) ans = 2 if k == 1: ans = 1 elif n == 1: ans = 2 elif k > 1: count = 0 temp = [(1) for i in range(n - 1)] for i in range(k - 1, 0, -1): if count == 0: ans += sum(temp) ans %= mod for j in range(1, n - 1): temp[j] = temp[j - 1] + temp[j] temp[j] %= mod count = 1 elif count == 1: ans += sum(temp) ans %= mod for j in range(n - 3, -1, -1): temp[j] = temp[j + 1] + temp[j] temp[j] %= mod count = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR 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 VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
a is an array of n positive integers, all of which are not greater than n. You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + ap + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations. Input The first line contains one integer n (1 ≤ n ≤ 100000). The second line contains n integers — elements of a (1 ≤ ai ≤ n for each i from 1 to n). The third line containts one integer q (1 ≤ q ≤ 100000). Then q lines follow. Each line contains the values of p and k for corresponding query (1 ≤ p, k ≤ n). Output Print q integers, ith integer must be equal to the answer to ith query. Example Input 3 1 1 1 3 1 1 2 1 3 1 Output 2 1 1 Note Consider first example: In first query after first operation p = 3, after second operation p = 5. In next two queries p is greater than n after the first operation.
import sys n = int(sys.stdin.buffer.readline().decode("utf-8")) a = [0] + list(map(int, sys.stdin.buffer.readline().decode("utf-8").split())) q = int(sys.stdin.buffer.readline().decode("utf-8")) dp_size = 200 dp = [([1] * (n + 1)) for _ in range(dp_size + 1)] for k in range(1, dp_size + 1): for i in range(n, 0, -1): if i + a[i] + k <= n: dp[k][i] = dp[k][i + a[i] + k] + 1 ans = [0] * q for i, (p, k) in enumerate( map(int, line.decode("utf-8").split()) for line in sys.stdin.buffer ): if k <= dp_size: ans[i] = dp[k][p] else: while p <= n: p += a[p] + k ans[i] += 1 sys.stdout.buffer.write("\n".join(map(str, ans)).encode("utf-8"))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
primes = [] for p in range(2, 6000): is_prime = True for p2 in primes: if p2 * p2 > p: break if p % p2 == 0: is_prime = False break if is_prime: primes.append(p) def factor(n): d = {} for p in primes: if p * p > n: break if n % p == 0: c = 0 while n % p == 0: n = n // p c += 1 d[p] = c if n > 1: d[n] = 1 return d def factors(n): d = factor(n) answer = [1] for p in d: a2 = [] for x in answer: for i in range(d[p] + 1): a2.append(x * p**i) answer = a2 return answer def process(n, m): f_dict = {(0): 0, (1): 1, (2): 2} for i in range(3, n + 1): f_dict[i] = 2 * f_dict[i - 1] for x in factors(i): if 2 <= x <= i: f_dict[i] += f_dict[i // x] - f_dict[(i - 1) // x] f_dict[i] = f_dict[i] % m f_dict[i] = f_dict[i] % m return f_dict[n] n, m = [int(x) for x in input().split()] print(process(n, m))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, M = map(int, input().split()) if n == 1: print(1) exit(0) dp = [1] * (n + 1) accu = [0] * (n + 1) accu[1] = 1 accu[2] = 2 for j in range(4, n + 1, 2): dp[j] += dp[2] for i in range(3, n + 1): dp[i] += accu[i - 1] for j in range(2 * i, n + 1, i): dp[j] += dp[i] dp[j] = dp[j] % M accu[i] = (accu[i - 1] + dp[i]) % M print(accu[n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
class BIT: def __init__(self, n: int): q = 1 << (n - 1).bit_length() + 1 self.ceiling = q self.bittree = [0] * (q + 1) def updatebit(self, v: int, n: int): v = v * 2 - 1 self.bittree[v] = n while v < self.ceiling: s = int(v) count = 0 while not s & 1: s >>= 1 count += 1 v += 2**count self.bittree[v] += n def getIntervalSum(self, l: int, r: int): l = l * 2 - 2 r = r * 2 - 1 ret = 0 while r > 0: ret += self.bittree[r] s = int(r) count = 0 while not s & 1: s >>= 1 count += 1 r -= 2**count while l > 0: ret -= self.bittree[l] s = int(l) count = 0 while not s & 1: s >>= 1 count += 1 l -= 2**count return ret n, mod = map(int, input().split()) dp = [(0) for _ in range(n + 1)] bittree = BIT(n) dp[n] = 1 bittree.updatebit(n, 1) sum = 1 for i in range(n - 1, 0, -1): for j in range(2, n + 1): if i * j <= n: dp[i] = ( dp[i] + bittree.getIntervalSum(i * j, min((i + 1) * j - 1, n)) ) % mod else: break dp[i] = (dp[i] + sum) % mod bittree.updatebit(i, dp[i]) sum = (sum + dp[i]) % mod print(dp[1])
CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, m = map(int, input().split()) sumL = [0] * (n + 2) L = [0] * (n + 1) L[-1], sumL[-2] = 1, 1 for i in range(n - 1, 0, -1): L[i] = sumL[i + 1] j = 2 while i * j <= n: L[i] += sumL[i * j] - sumL[min(n, (i + 1) * j - 1) + 1] j += 1 L[i] = L[i] % m sumL[i] = (sumL[i + 1] + L[i]) % m print(L[1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, m = map(int, input().split()) dp = [0] * (n + 2) s = [0] * (n + 2) dp[n] = 1 s[n] = 1 for i in range(n - 1, 0, -1): dp[i] = s[i + 1] for j in range(2, n // i + 1): h = min(n + 1, (i + 1) * j) dp[i] = (dp[i] + s[i * j] - s[h]) % m s[i] = (s[i + 1] + dp[i]) % m print(dp[1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, M = map(int, input().split()) fact = [{} for i in range(n + 1)] for i in range(1, n + 1): for j in range(i, n + 1, i): fact[j][i] = 1 dp = [0] * (n + 1) accu = [0] * (n + 1) dp[1] = 1 dp[2] = 2 extra = 0 for i in range(3, n + 1): dp[i] = 2 * dp[i - 1] % M for ele in fact[i]: if ele == i: continue dp[i] += dp[ele] - dp[ele - 1] dp[i] = dp[i] % M print(dp[n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
N, MOD = map(int, input().split()) DP = [0] * N DP[0] = 1 Cum = [0] * (N + 1) Cum[1] = 1 for i1 in range(1, N): i2 = N - i1 Value = Cum[i1] for j in range(2, N + 1): if i2 * j > N: break Value += Cum[N - i2 * j + 1] - Cum[max(0, N - i2 * j - j + 1)] Value %= MOD Cum[i1 + 1] += Cum[i1] + Value Cum[i1 + 1] %= MOD DP[i1] = Value print(DP[-1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
for u in range(1): n, m = map(int, input().split()) dp = [(0) for i in range(n + 1)] dp[1] = 1 p = [0] * (n + 1) cache = [[] for i in range(n + 1)] a = 0 b = 1 for i in range(2, n + 1): for j in cache[i]: a = (a - dp[p[j]] + m) % m p[j] += 1 a = (a + dp[p[j]]) % m a = (a + dp[1]) % m dp[i] = (a + b) % m b = (b + dp[i]) % m p[i] = 1 j = 2 while i * j <= n: cache[i * j].append(i) j += 1 print(dp[n])
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
def solve(n, mod): a = [0] * (n - 1) + [1] b = [0] * (n - 1) + [1] for i in range(n - 2, -1, -1): a[i], m = b[i + 1], 2 s = (i + 1) * m - 1 while s < n: e = min((i + 2) * m - 2, n - 1) a[i] = (a[i] + b[s] - b[e] + a[e]) % mod m += 1 s = (i + 1) * m - 1 b[i] = (b[i + 1] + a[i]) % mod return a[0] n, m = map(int, input().split()) print(solve(n, m))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
import sys input = sys.stdin.readline n, m = map(int, input().split()) mod = m dp = [0] * (n + 3) dp[n] = 1 sdp = [0] * (n + 3) sdp[n] = 1 for x in range(n - 1, 0, -1): dp[x] += sdp[x + 1] dp[x] %= mod z = 2 while x * z <= n: l = x * z r = (x + 1) * z - 1 if r > n: r = n dp[x] += sdp[l] - sdp[r + 1] dp[x] %= mod z += 1 sdp[x] = sdp[x + 1] + dp[x] sdp[x] %= mod print(dp[1] % mod)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
from sys import stdin input = stdin.readline def add(x, y): return (x % mod + y % mod) % mod def sub(x, y): return (x - y + mod) % mod def answer(): dp = [0] * (n + 1) prefix = [0] * (n + 2) dp[n] = 1 for i in range(n, 0, -1): dp[i] = add(dp[i], prefix[i + 1]) j = 2 while i * j <= n: dp[i] = add(dp[i], prefix[i * j]) if j * i + j <= n: dp[i] = sub(dp[i], prefix[j * i + j]) j += 1 prefix[i] = add(prefix[i + 1], dp[i]) return dp[1] for T in range(1): n, mod = map(int, input().split()) print(answer())
ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
import sys input = sys.stdin.readline def ri(): return [int(i) for i in input().split()] def rs(): return input().split()[0] def main(): t = 1 for _ in range(t): n, m = ri() ans = [0] * (n + 1) sum = [0] * (n + 2) ans[n] = 1 sum[n] = 1 for x in range(n - 1, 0, -1): extra = sum[x + 1] for d in range(2, n // x + 1): from_ = x * d to_ = min(x * d + d - 1, n) extra = (extra + m + sum[from_] - sum[to_ + 1]) % m ans[x] = extra sum[x] = (sum[x + 1] + ans[x]) % m print(ans[1] % m) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, M = map(int, input().split(" ")) D = [[] for _ in range(n + 1)] for d in range(2, n + 1): for dst in range(2 * d, n + 1, d): D[dst].append(d) F = [None] * (n + 1) F[1] = 1 F[2] = 2 for x in range(3, n + 1): F[x] = (2 * F[x - 1] + F[x // (x - 1)]) % M S = 0 for d in D[x]: if 2 <= d <= x - 2: S += F[x // d] - F[(x - 1) // d] F[x] = (F[x] + S) % M print(F[n] % M)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
import sys from sys import path_hooks, stdin, stdout def mapinput(): return map(int, stdin.readline().split()) n, m = mapinput() factors = [[] for i in range(n + 1)] for i in range(1, n + 1): for j in range(i + i, n + 1, i): factors[j].append(i) for test in range(1): def solve(): memo = {(0): 0, (1): 1, (2): 2, (3): 5} for i in range(4, n + 1): ans = memo[i - 1] * 2 for j in factors[i]: ans += memo[j] - memo[j - 1] ans %= m memo[i] = ans return memo[n] print(solve())
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
from sys import gettrace, stdin if gettrace(): def inputi(): return input() else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def main(): n, m = map(int, input().split()) nways = [0, 1, 2] + [0] * (n - 1) divisors = [[] for _ in range(n + 1)] for i in range(2, n + 1): for j in range(i, n + 1, i): divisors[j].append(i) for i in range(3, n + 1): nways[i] = nways[i - 1] * 2 % m for d in divisors[i]: nways[i] = (nways[i] + nways[i // d] - nways[(i - 1) // d]) % m print(nways[n]) main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, mod = map(int, input().split()) if n == 1: print(1) exit() fact = [{} for i in range(n + 1)] for i in range(1, n + 1): for j in range(i, n + 1, i): fact[j][i] = 1 ans = [0] * n ans[0] = 1 ans[1] = 2 for i in range(2, n): ans[i] = 2 * ans[i - 1] % mod for j in fact[i + 1]: if j == i + 1: continue if j == 1: ans[i] = (ans[i] + ans[j - 1]) % mod continue ans[i] = (ans[i] + (ans[j - 1] - ans[j - 2])) % mod print(ans[-1] % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, m = map(int, input().split()) dp = [(0) for i in range(n + 3)] dp2 = [(0) for i in range(n + 3)] dp[n] = 1 dp2[n] = 1 for i in range(n - 1, 0, -1): dp[i] = dp2[i + 1] % m mul = 2 while i * mul <= n: dp[i] = (dp[i] % m + dp2[i * mul] % m - dp2[min((i + 1) * mul, n + 1)] % m) % m mul += 1 dp2[i] = (dp[i] % m + dp2[i + 1] % m) % m print(dp[1] % m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, m = map(int, input().split()) dp = [(0) for i in range(n + 1)] dp[1] = 1 dp[2] = 2 for i in range(2, n + 1): if i > 2: dp[i] = (dp[i] + dp[i - 1] + dp[i - 1] + 1) % m for j in range(i + i, n + 1, i): dp[j] = (dp[j] + dp[i] - dp[i - 1]) % m print((dp[n] + m) % m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, m = map(int, input().split()) c = [0] * n + [1] + [0] * n for i in range(n - 1, 0, -1): c[i] = 2 * c[i + 1] % m for j in range(2, n // i + 1): c[i] = (c[i] + c[i * j] - c[(i + 1) * j]) % m print((c[1] - c[2]) % m)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
import sys input = sys.stdin.readline n, m = map(int, input().split()) dp = [0] * (n + 2) dp[n] = 1 for i in range(n - 1, 0, -1): dpi = dp[i + 1] j = 2 while i * j < n + 2: dpi += dp[i * j] - dp[min(n + 1, (i + 1) * j)] dpi %= m j += 1 dp[i] = dpi + dp[i + 1] dp[i] %= m ans = (dp[1] - dp[2]) % m print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
This version of the problem differs from the next one only in the constraint on n. Note that the memory limit in this problem is lower than in others. You have a vertical strip with n cells, numbered consecutively from 1 to n from top to bottom. You also have a token that is initially placed in cell n. You will move the token up until it arrives at cell 1. Let the token be in cell x > 1 at some moment. One shift of the token can have either of the following kinds: * Subtraction: you choose an integer y between 1 and x-1, inclusive, and move the token from cell x to cell x - y. * Floored division: you choose an integer z between 2 and x, inclusive, and move the token from cell x to cell ⌊ x/z ⌋ (x divided by z rounded down). Find the number of ways to move the token from cell n to cell 1 using one or more shifts, and print it modulo m. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers n and m (2 ≤ n ≤ 2 ⋅ 10^5; 10^8 < m < 10^9; m is a prime number) — the length of the strip and the modulo. Output Print the number of ways to move the token from cell n to cell 1, modulo m. Examples Input 3 998244353 Output 5 Input 5 998244353 Output 25 Input 42 998244353 Output 793019428 Note In the first test, there are three ways to move the token from cell 3 to cell 1 in one shift: using subtraction of y = 2, or using division by z = 2 or z = 3. There are also two ways to move the token from cell 3 to cell 1 via cell 2: first subtract y = 1, and then either subtract y = 1 again or divide by z = 2. Therefore, there are five ways in total.
n, M = map(int, input().split()) dp = [0] * (n + 1) accu = [0] * (n + 1) dp[1] = 1 dp[2] = 1 accu[1] = 1 accu[2] = 2 extra = 0 for i in range(3, n + 1): dp[i] = accu[i - 1] for j in range(1, int(i**0.5) + 1): if i % j == 0: dp[i] += dp[j] dp[i] = dp[i] % M if j > 1 and j * j < i: dp[i] += dp[i // j] dp[i] = dp[i] % M accu[i] = (accu[i - 1] + dp[i]) % M print(accu[n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) ar = list(map(int, input().split())) su = r * ar[-1] suf = [0] * n for i in range(n - 1, -1, -1): su = suf[i] = max(r * ar[i], su) pr = p * ar[0] ans = -1111111111111111111111111111 for i in range(n): pr = max(pr, p * ar[i]) ans = max(ans, pr + q * ar[i] + suf[i]) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = input().split() x = [] x = list(map(int, input().split())) p = int(p) q = int(q) r = int(r) for i in range(int(n)): t = int(x[i]) if i == 0: a = t * p b = a + t * q c = b + t * r else: if a < t * p: a = t * p if b < a + t * q: b = a + t * q if c < b + t * r: c = b + t * r print(c)
ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST 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 VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) z = list(map(int, input().split())) pmini = [z[0]] pmaxa = [z[0]] xa = [z[0]] for i in range(1, len(z)): pmini.append(min(pmini[-1], z[i])) pmaxa.append(max(pmaxa[-1], z[i])) rmini = [(0) for i in range(len(z))] rmaxa = [(0) for i in range(len(z))] rmini[-1] = z[-1] rmaxa[-1] = z[-1] for i in range(len(z) - 2, -1, -1): rmini[i] = min(z[i], rmini[i + 1]) rmaxa[i] = max(z[i], rmaxa[i + 1]) maxa = z[0] * (p + q + r) for i in range(len(z)): if p < 0: if r < 0: maxa = max(maxa, z[i] * q + pmini[i] * p + rmini[i] * r) else: maxa = max(maxa, z[i] * q + pmini[i] * p + rmaxa[i] * r) elif r < 0: maxa = max(maxa, z[i] * q + pmaxa[i] * p + rmini[i] * r) else: maxa = max(maxa, z[i] * q + pmaxa[i] * p + rmaxa[i] * r) print(maxa)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
import sys input = sys.stdin.readline n, p, q, r = map(int, input().split()) l = list(map(int, input().split())) dpleft = [float("-inf") for _ in range(n)] dpright = [(0) for _ in range(n)] leftmax = float("-inf") for i in range(n): leftmax = max(leftmax, p * l[i]) dpleft[i] = leftmax rightmax = float("-inf") for i in range(n - 1, -1, -1): rightmax = max(rightmax, r * l[i]) dpright[i] = rightmax ans = float("-inf") for i in range(n): ans = max(ans, dpleft[i] + q * l[i] + dpright[i]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
import sys input = sys.stdin.readline n, p, q, r = map(int, input().split()) a = list(map(int, input().split())) s1 = [(a[i] * p) for i in range(n)] s2 = [] m = s1[0] for i in range(n): m = max(m, s1[i]) s2.append(m + a[i] * q) s3 = [] m = s2[0] for i in range(n): m = max(m, s2[i]) s3.append(m + a[i] * r) print(max(s3))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) a = list(map(int, input().split())) dpp = [(0) for i in range(n)] dpq = [(0) for i in range(n)] dpr = [(0) for i in range(n)] dpp[0] = a[0] * p dpq[0] = a[0] * q + dpp[0] dpr[0] = a[0] * r + dpq[0] for i in range(1, n): dpp[i] = max(dpp[i - 1], a[i] * p) dpq[i] = max(dpq[i - 1], dpp[i] + a[i] * q) dpr[i] = max(dpr[i - 1], dpq[i] + a[i] * r) print(dpr[n - 1])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = list(map(int, input().split())) arr = list(map(int, input().split())) premax = [] suffmax = [] for i in arr: premax.append(i * p) suffmax.append(i * r) for i in range(1, n): premax[i] = max(premax[i], premax[i - 1]) for i in range(2, n + 1): suffmax[-i] = max(suffmax[-i], suffmax[-i + 1]) maxx = float("-inf") for j in range(0, n): if maxx < premax[j] + q * arr[j] + suffmax[j]: maxx = premax[j] + q * arr[j] + suffmax[j] print(maxx)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, x, y, z = map(int, input().split()) l = [int(x) for x in input().split()] a = -1e21 b = -1e21 c = -1e21 for i in range(n): temp = l[i] a = max(a, x * temp) b = max(b, a + y * temp) c = max(c, b + z * temp) print(c)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n, p, q, r = lst() a = lst() NI = float("-inf") dp1 = [NI] * n dp2 = [NI] * n dp3 = [NI] * n for i in range(n): if i == 0: dp1[i] = a[i] * p else: dp1[i] = max(dp1[i - 1], a[i] * p) for i in range(n): if i == 0: dp2[i] = dp1[i] + a[i] * q dp2[i] = max(dp1[i] + q * a[i], dp2[i - 1]) for i in range(n): if i == 0: dp3[i] = dp2[i] + r * a[0] else: dp3[i] = max(dp3[i - 1], dp2[i] + a[i] * r) print(max(dp3))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) arr = list(map(int, input().split())) dp = [] for i in range(n): dp.append([0, 0, 0]) dp[0][0] = p * arr[0] dp[0][1] = (p + q) * arr[0] dp[0][2] = (p + q + r) * arr[0] for i in range(1, n): dp[i][0] = max(p * arr[i], dp[i - 1][0]) dp[i][1] = max(dp[i][0] + q * arr[i], dp[i - 1][1]) dp[i][2] = max(dp[i][1] + r * arr[i], dp[i - 1][2]) print(dp[n - 1][2])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
s = input() c = s.split(" ") for i in range(4): c[i] = int(c[i]) s = input() a = s.split(" ") for i in range(c[0]): a[i] = int(a[i]) a[i] = [c[1] * a[i], c[2] * a[i], c[3] * a[i]] dp = [[0, 0, 0] for i in range(c[0])] m1 = -1000000000000000001 m2 = -2000000000000000001 m3 = -3000000000000000001 for i in range(c[0]): m1 = max(m1, a[i][0]) m2 = max(m2, m1 + a[i][1]) m3 = max(m3, m2 + a[i][2]) print(m3)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = [int(x) for x in input().split()] a = list(map(int, input().split())) ap = [a[0] * p] * n for i in range(1, n): ap[i] = max(ap[i - 1], a[i] * p) aq = [a[0] * q + ap[0]] * n for i in range(1, n): aq[i] = max(aq[i - 1], a[i] * q + ap[i]) ar = [a[0] * r + aq[0]] * n for i in range(1, n): ar[i] = max(ar[i - 1], a[i] * r + aq[i]) print(ar[n - 1])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) a = list(map(int, input().split())) ans = p * a[0] + q * a[0] + r * a[0] b = [0] * n c = [0] * n d = [0] * n e = [0] * n b[0] = a[0] c[0] = a[0] for i in range(1, n): b[i] = max(b[i - 1], a[i]) c[i] = min(c[i - 1], a[i]) d[n - 1] = a[n - 1] e[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): d[i] = max(d[i + 1], a[i]) e[i] = min(e[i + 1], a[i]) for j in range(n): v = a[j] * q + max(b[j] * p, c[j] * p) + max(d[j] * r, e[j] * r) ans = max(ans, v) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
def main(): n, p, q, r = [int(i) for i in input().split()] L = [int(i) for i in input().split()] LR_big = [L[0]] LR_small = [L[0]] for i in range(1, len(L)): LR_big.append(max(L[i], LR_big[-1])) LR_small.append(min(L[i], LR_small[-1])) RL_big = [L[-1]] RL_small = [L[-1]] for i in range(len(L) - 2, -1, -1): RL_big.append(max(L[i], RL_big[-1])) RL_small.append(min(L[i], RL_small[-1])) RL_big = RL_big[::-1] RL_small = RL_small[::-1] count = -(10**20) for i in range(len(L)): count = max( max(LR_small[i] * p, LR_big[i] * p) + L[i] * q + max(RL_small[i] * r, RL_big[i] * r), count, ) print(count) main()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = input().split() n = int(n) p = int(p) q = int(q) r = int(r) arr = [int(i) for i in input().split()] def ring(arr, p, q, r): max_ = p * arr[0] + q * arr[0] + r * arr[0] maxr = [r * arr[-1]] maxl = [p * arr[0]] for i in range(1, len(arr)): maxl.append(max(p * arr[i], maxl[i - 1])) for i in range(len(arr) - 2, -1, -1): maxr.append(max(maxr[-1], r * arr[i])) maxr.reverse() for j in range(len(arr)): qa = q * arr[j] pa = maxl[j] ra = maxr[j] if pa + qa + ra > max_: max_ = pa + qa + ra print(max_) ring(arr, p, q, r)
ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
P_I = 0 Q_I = 1 R_I = 2 def solve(n, pqr, a): max_p = [(0) for _ in range(n)] max_r = [(0) for _ in range(n)] max_p[0] = pqr[P_I] * a[0] max_r[n - 1] = pqr[R_I] * a[n - 1] for i in range(1, n): max_p[i] = max(max_p[i - 1], pqr[P_I] * a[i]) for k in range(n - 2, -1, -1): max_r[k] = max(max_r[k + 1], pqr[R_I] * a[k]) m = -3000000000000000000000 for j in range(n): prod = pqr[Q_I] * a[j] s = max_p[j] + prod + max_r[j] m = max(s, m) return m line1 = input().split() n = int(line1[0]) pqr = [int(i) for i in line1[1:]] a = [int(i) for i in input().split()] print(solve(n, pqr, a))
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) a = list(map(int, input().split())) max_pa_i = [0] * n max_pa_i[0] = a[0] * p for i in range(1, n): max_pa_i[i] = max(a[i] * p, max_pa_i[i - 1]) max_ra_k = [0] * n max_ra_k[-1] = a[-1] * r for i in range(n - 2, -1, -1): max_ra_k[i] = max(a[i] * r, max_ra_k[i + 1]) x = -(10**19) for i in range(n): x = max(x, max_pa_i[i] + q * a[i] + max_ra_k[i]) print(x)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
def sign(n): return n // abs(n) n, p, q, r = map(int, input().split()) A = list(map(int, input().split())) Q = q P = p R = r if p <= 0 and q <= 0 and r <= 0: print((p + q + r) * min(A)) elif p >= 0 and q >= 0 and r >= 0: print((p + q + r) * max(A)) elif p * r >= 0 and p < 0: b = [] max1 = -9999999999999999999999999999999 for i in range(n): b.append((A[i], i)) b.sort() mini = b[0][1] temp = mini temp2 = A[mini] tempb = 0 while temp >= 0: temp2 = max(A[temp], temp2) while b[tempb][1] > temp: tempb += 1 max1 = max(p * b[tempb][0] + q * temp2 + r * A[mini], max1) temp -= 1 temp2 = A[mini] tempb = 0 temp = mini while temp <= n - 1: temp2 = max(A[temp], temp2) while b[tempb][1] < temp: tempb += 1 max1 = max(r * b[tempb][0] + q * temp2 + p * A[mini], max1) temp += 1 print(max1) elif p * r >= 0 and p >= 0: b = [] max1 = -9999999999999999999999999999999 for i in range(n): b.append((A[i], i)) b.sort() mini = b[n - 1][1] temp = mini temp2 = A[mini] tempb = n - 1 while temp >= 0: temp2 = min(A[temp], temp2) while b[tempb][1] > temp: tempb -= 1 max1 = max(p * b[tempb][0] + q * temp2 + r * A[mini], max1) temp -= 1 temp2 = A[mini] tempb = n - 1 temp = mini while temp <= n - 1: temp2 = min(A[temp], temp2) while b[tempb][1] < temp: tempb -= 1 max1 = max(r * b[tempb][0] + q * temp2 + p * A[mini], max1) temp += 1 print(max1) elif r >= 0: b = [] if q >= 0: r += q else: p += q for i in range(n): b.append((A[i], i)) b.sort() max1 = -99999999999999999999999999999999 temp = 0 temp2 = A[0] tempb = n - 1 while temp <= n - 1: temp2 = min(temp2, A[temp]) while b[tempb][1] < temp: tempb -= 1 max1 = max(max1, p * temp2 + r * b[tempb][0]) temp += 1 print(max1) elif r < 0: b = [] if q <= 0: r += q else: p += q for i in range(n): b.append((A[i], i)) b.sort() max1 = -9999999999999999999999999999 temp = 0 temp2 = A[0] tempb = 0 while temp <= n - 1: temp2 = max(temp2, A[temp]) while b[tempb][1] < temp: tempb += 1 max1 = max(max1, p * temp2 + r * b[tempb][0]) temp += 1 print(max1)
FUNC_DEF RETURN BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, q, s, r = list(map(int, input().split())) a = list(map(int, input().split())) pmax_f = [a[0]] pmin_f = [a[0]] for i in range(n): pmax_f += [max(pmax_f[-1], a[i])] pmin_f += [min(pmin_f[-1], a[i])] pmax_e = [a[-1]] pmin_e = [a[-1]] for i in range(n): i = n - i - 1 pmax_e += [max(pmax_e[-1], a[i])] pmin_e += [min(pmin_e[-1], a[i])] ma = "False" for i in range(n): nat = s * a[i] nat += max(pmax_f[i + 1] * q, pmin_f[i + 1] * q) i = n - i - 1 nat += max(pmax_e[i + 1] * r, pmin_e[i + 1] * r) if ma == "False": ma = nat ma = max(ma, nat) print(ma)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR NUMBER VAR VAR VAR LIST FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR LIST FUNC_CALL VAR VAR NUMBER VAR VAR VAR LIST FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] maxP = [] maxim = float("-inf") for i in range(n): maxP.append(arr[i] * p) maxim = max(maxim, arr[i] * p) maxP[i] = maxim maxim = float("-inf") maxR = [float("-inf") for x in range(n)] for i in range(n - 1, -1, -1): maxR[i] = arr[i] * r maxim = max(maxim, arr[i] * r) maxR[i] = maxim ans = float("-inf") for i in range(n): ans = max(ans, maxP[i] + arr[i] * q + maxR[i]) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
max1 = [] max2 = [] max3 = [] N, A, B, C = map(int, input().split()) p = list(map(int, input().split())) max1.append(-100000000000000000000) max2.append(-100000000000000000000) max3.append(-100000000000000000000) for x in range(1, N + 1): max1.append(max(max1[x - 1], A * p[x - 1])) for x in range(1, N + 1): max2.append(max(max2[x - 1], max1[x] + B * p[x - 1])) for x in range(1, N + 1): max3.append(max(max3[x - 1], max2[x] + C * p[x - 1])) print(max3[-1])
ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
def fun(n, p, q, r, li): sfmax = [(0) for i in range(n)] sfmax[n - 1] = r * li[n - 1] for j in range(n - 2, -1, -1): sfmax[j] = max(sfmax[j + 1], r * li[j]) ans = -float("inf") pmax = [] pmax.append(p * li[0]) for i in range(1, n): pmax.append(max(pmax[j - 1], p * li[i])) for j in range(0, n): ans = max(ans, pmax[j] + li[j] * q + sfmax[j]) return ans n, p, q, r = list(map(lambda x: int(x), input().split())) li = list(map(lambda x: int(x), input().split())) print(fun(n, p, q, r, li))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) x = [int(i) for i in input().split()] x1 = [0] * n x2 = [0] * n x3 = [0] * n x1[0] = p * x[0] x2[0] = x1[0] + q * x[0] x3[0] = x2[0] + r * x[0] for i in range(1, len(x)): x1[i] = max(x1[i - 1], p * x[i]) x2[i] = max(x2[i - 1], q * x[i] + x1[i]) x3[i] = max(x3[i - 1], r * x[i] + x2[i]) print(x3[n - 1])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
def binarySearch(arr, x): l = 0 r = len(arr) - 1 while l <= r: mid = (l + r) // 2 if arr[mid] == x: return mid elif arr[mid] < x: l = mid + 1 else: r = mid - 1 return -1 n, p, q, r = map(int, input().split()) ar = list(map(int, input().split())) dp = [] inf = -9999999999999999999999999999999999999 for i in range(n + 1): dp.append([inf, inf, inf]) for i in range(1, n + 1): dp[i][0] = max(dp[i - 1][0], ar[i - 1] * p) dp[i][1] = max(dp[i - 1][1], dp[i][0] + ar[i - 1] * q) dp[i][2] = max(dp[i - 1][2], dp[i][1] + ar[i - 1] * r) print(dp[-1][-1])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
a = list(map(int, input().split())) arr = list(map(int, input().split())) n = a[0] dp = [[(0) for i in range(4)] for j in range(n + 1)] for i in range(4): dp[0][i] = (-10) ** 27 for i in range(1, n + 1): for j in range(1, 4): x = arr[i - 1] * a[j] + dp[i][j - 1] y = dp[i - 1][j] dp[i][j] = max(x, y) print(dp[n][3])
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 NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
_, x, y, z = map(int, input().split()) a = list(map(int, input().split())) def ans(x, y, z, a): dummy_x, dummy_x_y, dummy_x_y_z = [-1 * float("inf")] * 3 for e in a: dummy_x = max(dummy_x, e * x) dummy_x_y = max(dummy_x_y, dummy_x + e * y) dummy_x_y_z = max(dummy_x_y_z, dummy_x_y + e * z) return dummy_x_y_z print(ans(x, y, z, a))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR BIN_OP LIST BIN_OP NUMBER FUNC_CALL VAR STRING NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
Arr = list(map(int, input().split())) p = Arr[1] q = Arr[2] r = Arr[3] org_arr = list(map(int, input().split())) precal_premax = [-1] * len(org_arr) precal_suffmax = [-1] * len(org_arr) max_ele = p * org_arr[0] for i in range(len(org_arr)): if max_ele < p * org_arr[i]: max_ele = org_arr[i] * p precal_premax[i] = max_ele max_ele = org_arr[-1] * r for i in range(len(org_arr) - 1, -1, -1): if max_ele < org_arr[i] * r: max_ele = org_arr[i] * r precal_suffmax[i] = max_ele max_sum = float("-inf") for i in range(len(org_arr)): max_sum = max(max_sum, precal_premax[i] + precal_suffmax[i] + q * org_arr[i]) print(max_sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) nums = list(map(int, input().split())) dp0 = [(0) for i in range(100000)] dp1 = [(0) for i in range(100000)] dp2 = [(0) for i in range(100000)] dp0[0] = nums[0] * p for i in range(1, n): dp0[i] = max(dp0[i - 1], nums[i] * p) dp1[0] = dp0[0] + nums[0] * q for i in range(1, n): dp1[i] = max(dp1[i - 1], dp0[i] + nums[i] * q) dp2[0] = dp1[0] + nums[0] * r for i in range(1, n): dp2[i] = max(dp2[i - 1], dp1[i] + nums[i] * r) print(dp2[n - 1])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
x = list(map(int, input().split())) n = x[0] x[0] = 0 data = list(map(int, input().split())) ans = [-(10**19)] * 4 ans[0] = 0 for i in data: for j in range(1, 4): ans[j] = max(ans[j], ans[j - 1] + x[j] * i) print(ans[3])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) v = list(map(int, input().split())) d = [0] * n for i in range(n - 1, -1, -1): if i == n - 1: d[i] = r * v[i] elif d[i + 1] > r * v[i]: d[i] = d[i + 1] else: d[i] = r * v[i] now, ans = 0, -3 * 10**18 for i in range(n): if i == 0: now = p * v[i] elif now < p * v[i]: now = p * v[i] ans = max(ans, now + q * v[i] + d[i]) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
a = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] p, q, r = a[1], a[2], a[3] suff = [(0) for i in range(a[0])] suff[len(suff) - 1] = arr[len(arr) - 1] * r for i in range(len(arr) - 2, -1, -1): suff[i] = max(suff[i + 1], arr[i] * r) pre = arr[0] * p pre = max(pre, arr[0] * p) c = pre + arr[0] * q + suff[0] ma = c for i in range(1, len(arr)): pre = max(pre, arr[i] * p) c = pre + arr[i] * q + suff[i] ma = max(ma, c) print(ma)
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 VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = list(map(int, input().split())) a = list(map(int, input().split())) dp1 = [0] * n for i in range(n): if i == 0: dp1[i] = p * a[i] else: dp1[i] = max(dp1[i - 1], p * a[i]) dp2 = [0] * n for i in range(n): if i == 0: dp2[i] = p * a[i] + q * a[i] else: dp2[i] = max(q * a[i] + dp1[i], dp2[i - 1]) ans = -float("inf") for i in range(n): ans = max(ans, r * a[i] + dp2[i]) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) arr = list(map(int, input().split())) n = len(arr) if n == 1: print(arr[0] * (p + q + r)) else: pref = [arr[0]] if p > 0: for i in range(1, n): pref.append(max(pref[-1], arr[i])) else: for i in range(1, n): pref.append(min(pref[-1], arr[i])) suff = [arr[-1]] if r > 0: for i in range(n - 2, -1, -1): suff.append(max(suff[-1], arr[i])) else: for i in range(n - 2, -1, -1): suff.append(min(suff[-1], arr[i])) suff = suff[::-1] ans = float("-inf") for i in range(n): ans = max(ans, p * pref[i] + q * arr[i] + r * suff[i]) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = input().split() n = int(n) p = int(p) q = int(q) r = int(r) ls = list(map(int, input().split())) ans = -9223372036854775807 ls1 = [None] * n ls2 = [None] * n ls3 = [None] * n ls5 = [None] * n ls4 = [([None] * n) for i in range(3)] for i in range(0, n): ls1[i] = p * ls[i] ls2[i] = r * ls[i] ls5[i] = q * ls[i] ls3[n - 1] = n - 1 for i in range(0, n - 1): if ls2[n - i - 2] > ls2[n - i - 2 + 1]: ls3[n - i - 2] = n - i - 2 else: ls3[n - i - 2] = ls3[n - i - 2 + 1] ls4[0][0] = ls1[0] ls4[1][0] = ls5[0] + ls1[0] ls4[2][0] = ls2[ls3[0]] ans = max(ans, ls4[1][0] + ls4[2][0]) for i in range(1, n): ls4[0][i] = max(ls4[0][i - 1], ls1[i]) ls4[1][i] = max(ls4[1][i - 1], ls4[0][i] + ls5[i]) ls4[2][i] = ls2[ls3[i]] ans = max(ans, ls4[1][i] + ls4[2][i]) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
inf = int(1e-12) m = inf n, p, q, r = map(int, input().split(" ")) arr = list(map(int, input().split(" "))) dp = [[inf, inf, inf] for i in range(n + 1)] dp[1][0] = arr[0] * p dp[1][1] = arr[0] * q + dp[1][0] dp[1][2] = arr[0] * r + dp[1][1] for i in range(2, n + 1): dp[i][0] = max(arr[i - 1] * p, dp[i - 1][0]) dp[i][1] = max(dp[i][0] + arr[i - 1] * q, dp[i - 1][1]) dp[i][2] = max(dp[i][1] + arr[i - 1] * r, dp[i - 1][2]) print(dp[n][2])
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) l = list(map(int, input().split())) dp = [[(0) for i in range(n)] for j in range(3)] for i in range(3): for j in range(n): if i == 0: if j == 0: dp[i][j] = l[j] * p else: dp[i][j] = max(dp[i][j - 1], l[j] * p) elif i == 1: if j == 0: dp[i][j] = dp[i - 1][j] + l[j] * q else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j] + l[j] * q) elif j == 0: dp[i][j] = dp[i - 1][j] + l[j] * r else: dp[i][j] = max(dp[i][j - 1], dp[i - 1][j] + l[j] * r) print(dp[-1][-1])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
vals = list(map(int, input().strip().split())) arr = list(map(int, input().strip().split())) dp = [[(0) for i in range(vals[0])] for i in range(3)] n = vals[0] ind = 0 for i in vals[1:]: dp[ind][n - 1] = i * arr[-1] for j in range(n - 2, -1, -1): dp[ind][j] = max(dp[ind][j + 1], i * arr[j]) ind += 1 ans = -float("inf") dp2 = [(0) for i in range(n)] dp2[-1] = arr[-1] * vals[2] + dp[2][-1] for i in range(n - 2, -1, -1): dp2[i] = max(dp2[i + 1], arr[i] * vals[2] + dp[2][i]) for i in range(n): ans = max(ans, dp2[i] + arr[i] * vals[1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) lst = [] for x in input().split(): lst.append(int(x)) a = -(10**20) b = -(10**20) c = -(10**20) for x in lst: a = max(a, x * p) b = max(b, a + x * q) c = max(c, b + x * r) print(c)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
import sys length, p, q, r = [int(x) for x in sys.stdin.readline().strip().split(" ")] inp = [int(x) for x in sys.stdin.readline().strip().split(" ")] my_dict = dict() my_dict["p"] = -1 * float("inf") my_dict["q"] = -1 * float("inf") my_dict["r"] = -1 * float("inf") for x in inp: my_dict["p"] = max(my_dict["p"], p * x) my_dict["q"] = max(my_dict["q"], q * x + my_dict["p"]) my_dict["r"] = max(my_dict["r"], r * x + my_dict["q"]) print(my_dict["r"])
IMPORT ASSIGN VAR VAR 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 ASSIGN VAR STRING BIN_OP NUMBER FUNC_CALL VAR STRING ASSIGN VAR STRING BIN_OP NUMBER FUNC_CALL VAR STRING ASSIGN VAR STRING BIN_OP NUMBER FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR STRING FUNC_CALL VAR VAR STRING BIN_OP VAR VAR ASSIGN VAR STRING FUNC_CALL VAR VAR STRING BIN_OP BIN_OP VAR VAR VAR STRING ASSIGN VAR STRING FUNC_CALL VAR VAR STRING BIN_OP BIN_OP VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
def main(): n, p, q, r = list(map(int, input().split())) a = list(map(int, input().split())) dp = [float("-inf") for _ in range(n)] dp[-1] = r * a[-1] for i in range(n - 2, -1, -1): dp[i] = max(r * a[i], dp[i + 1]) dp2 = [float("-inf") for _ in range(n)] dp2[-1] = (q + r) * a[-1] for i in range(n - 2, -1, -1): dp2[i] = max(q * a[i] + dp[i], dp2[i + 1]) dp3 = [float("-inf") for _ in range(n)] dp3[-1] = (p + q + r) * a[-1] for i in range(n - 2, -1, -1): dp3[i] = max(p * a[i] + dp2[i], dp3[i + 1]) print(dp3[0]) main()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = [int(i) for i in input().split()] aa = [int(i) for i in input().split()] max_from_p = [] m = None for i in range(n): if i == 0: m = aa[i] * p elif aa[i] * p > m: m = aa[i] * p max_from_p.append(m) max_from_pq = [] m = None for i in range(n): if i == 0: m = aa[i] * q + max_from_p[i] elif aa[i] * q + max_from_p[i] > m: m = aa[i] * q + max_from_p[i] max_from_pq.append(m) m = None for i in range(n): if i == 0: m = aa[i] * r + max_from_pq[i] elif aa[i] * r + max_from_pq[i] > m: m = aa[i] * r + max_from_pq[i] print(m)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) x = list(map(int, input().split())) p_max = [] q_max = [] r_max = [] p_max.append(p * x[0]) for i in range(1, n): p_max.append(max(p_max[i - 1], p * x[i])) q_max.append(x[0] * q + p_max[0]) for i in range(1, n): q_max.append(max(q_max[i - 1], p_max[i] + x[i] * q)) ans = -(10**30) for i in range(0, n): ans = max(ans, r * x[i] + q_max[i]) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
import sys def getX(ai, aj, ak): return ai * p + aj * q + ak * r inpt = input().split() n = int(inpt[0]) p = int(inpt[1]) q = int(inpt[2]) r = int(inpt[3]) arr = list(map(int, input().split())) minLeft = [] maxLeft = [] minRight = [] maxRight = [] for i in range(n): if i == 0: minLeft.append(arr[i]) maxLeft.append(arr[i]) else: minLeft.append(min(minLeft[len(minLeft) - 1], arr[i])) maxLeft.append(max(maxLeft[len(maxLeft) - 1], arr[i])) arr.reverse() for i in range(n): if i == 0: minRight.append(arr[i]) maxRight.append(arr[i]) else: minRight.append(min(minRight[len(minRight) - 1], arr[i])) maxRight.append(max(maxRight[len(maxRight) - 1], arr[i])) minRight.reverse() maxRight.reverse() result = -9223372036854775808 arr.reverse() for j in range(n): if p < 0: ai = minLeft[j] else: ai = maxLeft[j] if r < 0: ak = minRight[j] else: ak = maxRight[j] result = max(result, getX(ai, arr[j], ak)) print(result)
IMPORT FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
import sys n, p, q, r = map(int, input().split()) lis = [int(temp) for temp in input().split()] a = lis[0] * p b = a + lis[0] * q c = b + lis[0] * r for x in lis: a = max(a, x * p) b = max(b, a + x * q) c = max(c, b + x * r) print(c)
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
[n, p, q, r] = list(map(int, input().split())) a = list(map(int, input().split())) Inf = int(1000000000.0 + 7) pref_min = [Inf for i in range(n)] pref_max = [(-Inf) for i in range(n)] suf_min = [Inf for i in range(n)] suf_max = [(-Inf) for i in range(n)] pref_min[0] = a[0] pref_max[0] = a[0] for i in range(1, n): pref_min[i] = min(pref_min[i - 1], a[i]) pref_max[i] = max(pref_max[i - 1], a[i]) suf_min[n - 1] = a[n - 1] suf_max[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): suf_min[i] = min(suf_min[i + 1], a[i]) suf_max[i] = max(suf_max[i + 1], a[i]) res = -int(3e18 + 7) for j in range(n): x = q * a[j] x += max(p * pref_max[j], p * pref_min[j]) x += max(r * suf_max[j], r * suf_min[j]) res = max(res, x) print(res)
ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
import sys n, p, q, r = map(int, input().split()) a = list(map(int, input().split())) prefixmax = [(0) for i in range(0, n)] suffixmax = [(0) for i in range(0, n)] suffixmax[n - 1] = r * a[n - 1] prefixmax[0] = p * a[0] for i in range(1, n): prefixmax[i] = max(prefixmax[i - 1], p * a[i]) for i in range(n - 2, -1, -1): suffixmax[i] = max(suffixmax[i + 1], r * a[i]) ans = -1 * sys.maxsize * 10**15 for j in range(0, n): x = 0 x += prefixmax[j] + suffixmax[j] + q * a[j] ans = max(x, ans) print(ans)
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
vals = list(map(int, input().strip().split())) arr = list(map(int, input().strip().split())) dp = [[(0) for i in range(vals[0])] for i in range(3)] n = vals[0] dp[-1][-1] = vals[3] * arr[-1] for i in range(n - 2, -1, -1): dp[-1][i] = max(dp[-1][i + 1], arr[i] * vals[3]) for i in range(1, -1, -1): dp[i][-1] = arr[-1] * vals[i + 1] + dp[i + 1][-1] for j in range(n - 2, -1, -1): dp[i][j] = max(dp[i][j + 1], arr[j] * vals[i + 1] + dp[i + 1][j]) print(dp[0][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = list(map(int, input().split())) a = list(map(int, input().split())) z = -(100000000000000000000000000000**1000) if q >= 0 and p >= 0 and r >= 0: c = -100000000000 for i in range(len(a)): if a[i] > c: c = a[i] print(c * (q + p + r)) elif q <= 0 and p <= 0 and r <= 0: c = 100000000000 for i in range(len(a)): if a[i] < c: c = a[i] print(c * (q + p + r)) else: b2 = a[0] * p b = [b2] * n for i in range(1, len(a)): if a[i] * p > b[i - 1]: b[i] = a[i] * p else: b[i] = b[i - 1] t2 = a[n - 1] * r t = [t2] * n for i in range(n - 2, -1, -1): if a[i] * r > t[i + 1]: t[i] = a[i] * r else: t[i] = t[i + 1] for i in range(n): if a[i] * q + b[i] + t[i] > z: z = a[i] * q + b[i] + t[i] print(z)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = [int(i) for i in input().split(" ")] a = input() b = a.split() for i in range(n): b[i] = int(b[i]) dp = [p * b[0]] for i in range(1, n): dp.append(max(dp[i - 1], p * b[i])) dq = [dp[0] + q * b[0]] for i in range(1, n): dq.append(max(dq[i - 1], dp[i] + q * b[i])) dr = [dq[0] + r * b[0]] for i in range(1, n): dr.append(max(dr[i - 1], dq[i] + r * b[i])) print(dr[-1])
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = map(int, input().split()) arr = [int(i) for i in input().split()] siz = len(arr) pre_max = [0] * siz suf_max = [0] * siz pre_max[0] = p * arr[0] suf_max[n - 1] = r * arr[n - 1] for i in range(1, len(arr)): pre_max[i] = max(pre_max[i - 1], p * arr[i]) for i in range(len(arr) - 2, -1, -1): suf_max[i] = max(suf_max[i + 1], r * arr[i]) ans = float("-inf") for i in range(len(arr)): x = pre_max[i] + q * arr[i] + suf_max[i] ans = max(ans, x) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = list(map(int, input().split())) a = list(map(int, input().split()))[::-1] INF = -(10**30) answer = INF dp = [INF, INF, INF] for i in range(n): dp[0] = max(dp[0], r * a[i]) dp[1] = max(dp[1], q * a[i] + dp[0]) dp[2] = max(dp[2], p * a[i] + dp[1]) answer = max(answer, dp[2]) print(answer)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
t, p, q, r = map(int, input().split()) l = [int(i) for i in input().split()] prex = [(0) for i in range(len(l))] suff = [(0) for i in range(len(l))] prex[0] = l[0] * p n = len(l) for i in range(1, n): prex[i] = max(l[i] * p, prex[i - 1]) suff[n - 1] = l[n - 1] * r for i in range(n - 2, -1, -1): suff[i] = max(l[i] * r, suff[i + 1]) maxx = -3 * 10**20 for i in range(n): maxx = max(maxx, prex[i] + q * l[i] + suff[i]) print(maxx)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = [int(x) for x in input().split()] a = [int(x) for x in input().split()] if len(a) == 0: print(0) pmax = [] pmax.append(p * a[0]) for i in range(1, n): pmax.append(max(pmax[i - 1], a[i] * p)) smax = a[n - 1] * r res = pmax[n - 1] + a[n - 1] * q + smax for i in range(n - 2, -1, -1): smax = max(smax, a[i] * r) res = max(pmax[i] + a[i] * q + smax, res) print(res)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
n, p, q, r = list(map(int, input().split())) a = list(map(int, input().split())) prefix = [None for _ in range(n)] suffix = [None for _ in range(n)] p_compare = max if p > 0 else min r_compare = max if r > 0 else min for i in range(n): if i == 0: prefix[i] = a[i] else: prefix[i] = p_compare(a[i], prefix[i - 1]) for i in range(n - 1, -1, -1): if i == n - 1: suffix[i] = a[i] else: suffix[i] = r_compare(a[i], suffix[i + 1]) optimum = None for j in range(n): s = a[j] * q s += prefix[j] * p s += suffix[j] * r if optimum is None: optimum = s else: optimum = max(optimum, s) print(optimum)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·a_{i} + q·a_{j} + r·a_{k} for given p, q, r and array a_1, a_2, ... a_{n} such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative. -----Input----- First line of input contains 4 integers n, p, q, r ( - 10^9 ≤ p, q, r ≤ 10^9, 1 ≤ n ≤ 10^5). Next line of input contains n space separated integers a_1, a_2, ... a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- Output a single integer the maximum value of p·a_{i} + q·a_{j} + r·a_{k} that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n. -----Examples----- Input 5 1 2 3 1 2 3 4 5 Output 30 Input 5 1 2 -3 -1 -2 -3 -4 -5 Output 12 -----Note----- In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30. In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
R = lambda: map(int, input().split()) n, p, q, r = R() arr = list(R()) lmi, lmx, rmi, rmx = arr[:], arr[:], arr[:], arr[:] for i in range(1, n): lmi[i] = min(lmi[i], lmi[i - 1]) lmx[i] = max(lmx[i], lmx[i - 1]) for i in range(n - 2, -1, -1): rmi[i] = min(rmi[i], rmi[i + 1]) rmx[i] = max(rmx[i], rmx[i + 1]) print( max( max(lmi[i] * p, lmx[i] * p) + arr[i] * q + max(rmi[i] * r, rmx[i] * r) for i in range(n) ) )
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR