description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
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,) = map(int, input().split())
dp = [[p * a[0], (p + q) * a[0], (p + q + r) * a[0]] for i in range(n)]
for i in range(1, n):
dp[i][0] = max(dp[i - 1][0], p * a[i])
for i in range(1, n):
dp[i][1] = max(dp[i - 1][1], dp[i][0] + q * a[i])
for i in range(1, n):
dp[i][2] = max(dp[i - 1][2], dp[i][1] + r * a[i])
print(dp[n - 1][2]) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR 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. | n, p, q, r = map(int, input().split())
a = list(map(int, input().split()))
pmax = [None] * n
smax = [None] * n
pmax[0] = a[0] * p
for i in range(1, n):
pmax[i] = max(pmax[i - 1], a[i] * p)
smax[n - 1] = r * a[n - 1]
for i in range(n - 2, -1, -1):
smax[i] = max(smax[i + 1], a[i] * r)
answer = -99999999999999999999999999999999999999999999999999
for i in range(0, n):
x = pmax[i] + q * a[i] + smax[i]
answer = max(x, answer)
print(answer) | 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 NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR 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 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 = map(int, input().split())
mp = mq = mr = -1e20
for i in map(int, input().split()):
mp = max(mp, p * i)
mq = max(mq, mp + q * i)
mr = max(mr, mq + r * i)
print(mr) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 = map(int, input().split())
A = list(map(int, input().split()))
B = [p, q, r]
def solve():
dp = [float("-inf")] * n
dp[0] = B[0] * A[0]
for i in range(1, n):
dp[i] = max(B[0] * A[i], dp[i - 1])
for j in range(1, 3):
new_dp = [float("-inf")] * n
new_dp[0] = A[0] * B[j] + dp[0]
for i in range(1, n):
new_dp[i] = max(new_dp[i - 1], A[i] * B[j] + dp[i])
dp = new_dp
return max(dp)
print(solve()) | 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 VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER 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 VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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. | R = lambda: map(int, input().split())
n, p, q, r = R()
arr = list(R())
ps = [(p * x) for x in arr]
for i in range(1, n):
ps[i] = max(ps[i], ps[i - 1])
pqs = [(ps[i] + arr[i] * q) for i in range(n)]
for i in range(1, n):
pqs[i] = max(pqs[i], pqs[i - 1])
print(max(pqs[i] + r * arr[i] 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 BIN_OP 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 BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR 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())
list1 = list(map(int, input().split()))
pref = [list1[0]]
suf = [list1[-1]]
ans = -3 * 10**18 - 1
if p > 0:
for i in range(1, n):
pref.append(max(list1[i], pref[-1]))
else:
for i in range(1, n):
pref.append(min(list1[i], pref[-1]))
if r > 0:
for i in range(n - 2, -1, -1):
suf.append(max(suf[-1], list1[i]))
else:
for i in range(n - 2, -1, -1):
suf.append(min(suf[-1], list1[i]))
for i in range(n):
ans = max(ans, pref[i] * p + q * list1[i] + suf[n - i - 1] * r)
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 VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR 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 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 BIN_OP BIN_OP 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. | f = lambda: map(int, input().split())
n, p, q, r = f()
a = list(f())
x = [(t * p) for t in a]
y = [(t * q) for t in a]
z = [(t * r) for t in a]
for i in range(n - 1, 0, -1):
z[i - 1] = max(z[i - 1], z[i])
for i in range(1, n):
x[i] = max(x[i - 1], x[i])
print(max(x[i] + y[i] + z[i] 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 BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR 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()))
x, y, z = -float("inf"), -float("inf"), -float("inf")
for i in range(n):
z = max(z, p * a[i])
y = max(y, z + q * a[i])
x = max(x, y + r * a[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 VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING 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 VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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 = list(map(int, input().split()))
a = list(map(int, input().split()))
num1, num2, num3 = -1e20, -1e20, -1e20
for x in a:
num1 = max(num1, p * x)
num2 = max(num2, num1 + q * x)
num3 = max(num3, num2 + r * x)
print(num3) | 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 VAR VAR NUMBER 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. | class CodeforcesTask855BSolution:
def __init__(self):
self.result = ""
self.n_p_q_r = []
self.a = []
def read_input(self):
self.n_p_q_r = [int(x) for x in input().split(" ")]
self.a = [int(x) for x in input().split(" ")]
def process_task(self):
dp = [([0] * self.n_p_q_r[0]) for x in range(3)]
for x in range(3):
dp[x][0] = self.a[0] * self.n_p_q_r[1 + x]
if x > 0:
dp[x][0] += dp[x - 1][0]
for y in range(1, self.n_p_q_r[0]):
if not x:
dp[x][y] = max(dp[x][y - 1], self.a[y] * self.n_p_q_r[1 + x])
else:
dp[x][y] = max(
dp[x][y - 1], self.a[y] * self.n_p_q_r[1 + x] + dp[x - 1][y]
)
self.result = str(dp[2][-1])
def get_result(self):
return self.result
Solution = CodeforcesTask855BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result()) | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR 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. | def maxSum(arr, p, q, r):
premax = -214748364800000000000000000000000000000
postmax = -2147483648000000000000000000000000000000
maxx = -214748364800000000000000000000000000000000000
pre = []
post = []
for i in arr:
premax = max(p * i, premax)
pre.append(premax)
for j in range(len(arr) - 1, -1, -1):
postmax = max(r * arr[j], postmax)
post.append(postmax)
for i in range(0, len(arr)):
summ = 0
summ = q * arr[i] + pre[i] + post[len(arr) - i - 1]
maxx = max(summ, maxx)
return maxx
n, p, q, r = [int(v) for v in input().split()]
arr = [int(v) for v in input().split()][:n]
print(maxSum(arr, p, q, r)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR 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 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. | n, p, q, r = map(int, input().split())
a = list(map(int, input().split()))
u = [0] * n
u[0] = a[0] * p
for i in range(1, n):
u[i] = max(u[i - 1], a[i] * p)
u[0] += a[0] * q
for i in range(1, n):
u[i] = max(u[i] + q * a[i], u[i - 1])
u[0] += a[0] * r
for i in range(1, n):
u[i] = max(u[i] + r * a[i], u[i - 1])
print(u[-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 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 VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR 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. | inf = 10**18
n, p, q, r = map(int, input().split())
a = list(map(int, input().split()))
mx = [-inf] * n
mn = [inf] * n
val2 = [0] * n
mx[0] = a[0]
mn[0] = a[0]
val2[0] = p * a[0] + q * a[0]
for i in range(1, n):
mn[i] = min(mn[i - 1], a[i])
mx[i] = max(mx[i - 1], a[i])
val2[i] = q * a[i] + max(mn[i] * p, mx[i] * p)
val3 = [0] * n
mx2 = [-inf] * n
mx2[0] = val2[0]
val3[0] = val2[0] + r * a[0]
ans = -inf
for i in range(1, n):
mx2[i] = max(mx2[i - 1], val2[i])
val3[i] = r * a[i] + mx2[i]
print(max(val3)) | ASSIGN VAR BIN_OP NUMBER 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 BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR 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 VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR 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 = list(map(int, input().split()))
alist = list(map(int, input().split()))
first = [alist[0]]
if p < 0:
for i in range(1, n):
first.append(min(alist[i], first[i - 1]))
else:
for i in range(1, n):
first.append(max(alist[i], first[i - 1]))
last = [alist[n - 1]]
if r < 0:
for i in range(0, n - 1):
last.insert(0, min(alist[n - 2 - i], last[0]))
else:
for i in range(0, n - 1):
last.insert(0, max(alist[n - 2 - i], last[0]))
ans = -9999999999999999999999999999
itrans = 0
for i in range(0, n):
itrans = p * first[i] + q * alist[i] + r * last[i]
if itrans > ans:
ans = itrans
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 LIST VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN 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())
ai = list(map(int, input().split()))
maxp = ai[-1] * p
maxq = ai[-1] * q
maxr = ai[-1] * r
maxqr = maxq + maxr
ans = maxqr + maxp
for i in range(n - 2, -1, -1):
maxr = max(maxr, ai[i] * r)
maxqr = max(maxqr, ai[i] * q + maxr)
ans = max(ans, maxqr + ai[i] * p)
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 NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP 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 VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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
initial_inp = []
inp = []
n = input().split()
count = int(n[0], 10)
p = int(n[1], 10)
q = int(n[2], 10)
r = int(n[3], 10)
arrString = input().split()
arr = count * [None]
for i in range(0, count):
arr[i] = int(arrString[i], 10)
def maximum(a, b):
if a < b:
return b
else:
return a
pmax = [1] * count
pmax[0] = arr[0] * p
for i in range(1, count):
pmax[i] = maximum(pmax[i - 1], p * arr[i])
smax = [1] * count
smax[count - 1] = arr[count - 1] * r
for i in range(count - 2, -1, -1):
smax[i] = maximum(smax[i + 1], r * arr[i])
ans = -sys.maxsize - sys.maxsize * sys.maxsize * 10
for i in range(0, count):
ans = maximum(pmax[i] + q * arr[i] + smax[i], ans)
print(ans) | IMPORT ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NONE FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR RETURN 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 VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR 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 VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP 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. | def readints():
return [int(item) for item in input().split()]
class Solver:
def main(self):
n, p, q, r = readints()
a = [0] + readints()
dp = [[(0) for _ in range(3)] for _ in range(n + 1)]
dp[1][0] = p * a[1]
dp[1][1] = (p + q) * a[1]
dp[1][2] = (p + q + r) * a[1]
for i in range(2, n + 1):
dp[i][0] = max(dp[i - 1][0], p * a[i])
dp[i][1] = max(p * a[i] + q * a[i], dp[i - 1][0] + q * a[i], dp[i - 1][1])
dp[i][2] = max(
p * a[i] + q * a[i] + r * a[i],
dp[i - 1][0] + q * a[i] + r * a[i],
dp[i - 1][1] + r * a[i],
dp[i - 1][2],
)
print(dp[n][2])
Solver().main() | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR 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 BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL 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 = map(int, input().split())
a = [int(s) for s in input().split()]
dp = []
INF = 1000000000.0 + 5
for i in range(n + 1):
dp.append([-(3 * INF * INF), -(3 * INF * INF), -(3 * INF * INF)])
for i in range(n):
dp[i + 1][0] = max(dp[i][0], a[i] * p)
dp[i + 1][1] = max(dp[i][1], dp[i + 1][0] + q * a[i])
dp[i + 1][2] = max(dp[i][2], dp[i + 1][1] + r * a[i])
print(dp[n][2]) | 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 LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP 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. | n, p, q, r = map(int, input().split())
a = list(map(int, input().split()))
b = a[::-1]
ma = [0] * n
mb = [0] * n
Ma = [0] * n
Mb = [0] * n
ma[0] = Ma[0] = a[0]
mb[0] = Mb[0] = b[0]
for i in range(1, n):
ma[i] = min(ma[i - 1], a[i])
mb[i] = min(mb[i - 1], b[i])
Ma[i] = max(Ma[i - 1], a[i])
Mb[i] = max(Mb[i - 1], b[i])
ans = (p + q + r) * a[0]
for j in range(n):
z = max(ma[j] * p, Ma[j] * p) + q * a[j] + max(mb[n - 1 - j] * r, Mb[n - 1 - j] * r)
ans = max(ans, z)
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 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 VAR NUMBER ASSIGN VAR NUMBER 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 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 BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN 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 BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER 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. | nn, p, q, r = map(int, input().split())
ar = list(map(int, input().split()))
max_l = [ar[0]]
for i in range(1, nn):
max_l.append(max(max_l[-1], ar[i]))
min_l = [ar[0]]
for i in range(1, nn):
min_l.append(min(min_l[-1], ar[i]))
max_r = [0] * nn
max_r[-1] = ar[-1]
for i in range(nn - 2, -1, -1):
max_r[i] = max(max_r[i + 1], ar[i])
min_r = [0] * nn
min_r[-1] = ar[-1]
for i in range(nn - 2, -1, -1):
min_r[i] = min(min_r[i + 1], ar[i])
ans = -3 * 10**19
for i in range(0, nn):
t = q * ar[i]
t += p * (min_l[i] if p < 0 else max_l[i])
t += r * (min_r[i] if r < 0 else max_r[i])
ans = max(ans, t)
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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER 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 BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER 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. | s = input()
inf = 1e30
s = [int(x) for x in s.split()]
n = s[0]
p = s[1]
q = s[2]
r = s[3]
a = input()
a = [int(x) for x in a.split()]
dp = []
for i in range(n + 1):
dp.append(0)
dp[0] = -inf
i = 1
while i <= n:
now = p * a[i - 1]
dp[i] = max(dp[i - 1], now)
i = i + 1
i = 1
while i <= n:
now = q * a[i - 1]
dp[i] = max(dp[i - 1], dp[i] + now)
i = i + 1
i = 1
ans = -inf
while i <= n:
now = r * a[i - 1]
dp[i] = max(dp[i - 1], dp[i] + now)
ans = max(ans, dp[i])
i = i + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP 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())
arr = list(map(int, input().split()))
def select_and_multiply(x, arr):
if x >= 0:
return x * arr[-1]
return x * arr[0]
def solve():
minFromLeft = [None for i in range(n)]
maxFromLeft = [None for i in range(n)]
minFromRight = [None for i in range(n)]
maxFromRight = [None for i in range(n)]
minFromLeft[0] = arr[0]
maxFromLeft[0] = arr[0]
minFromRight[-1] = arr[-1]
maxFromRight[-1] = arr[-1]
for i in range(1, n):
minFromLeft[i] = min(minFromLeft[i - 1], arr[i])
maxFromLeft[i] = max(maxFromLeft[i - 1], arr[i])
for i in range(n - 2, -1, -1):
minFromRight[i] = min(minFromRight[i + 1], arr[i])
maxFromRight[i] = max(maxFromRight[i + 1], arr[i])
result = float("-inf")
for i in range(n):
a = maxFromLeft[i] if p >= 0 else minFromLeft[i]
b = arr[i]
c = maxFromRight[i] if r >= 0 else minFromRight[i]
result = max(result, a * p + b * q + c * r)
return result
print(solve()) | 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 IF VAR NUMBER RETURN BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER 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 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 STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR 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. | def list_input():
return list(map(int, input().split()))
def map_input():
return map(int, input().split())
def map_string():
return input().split()
n, p, q, r = map_input()
a = [p, q, r]
b = list_input()
ans = 0
INF = -100000000000000000000000
dp = [INF] * (n + 5)
dp2 = [INF] * (n + 5)
dp3 = [INF] * (n + 5)
for i in range(n):
dp[i] = p * b[i]
for i in range(1, n):
dp[i] = max(dp[i], dp[i - 1])
for i in range(n):
dp2[i] = q * b[i] + dp[i]
for i in range(1, n):
dp2[i] = max(dp2[i], dp2[i - 1])
for i in range(n):
dp3[i] = r * b[i] + dp2[i]
for i in range(1, n):
dp3[i] = max(dp3[i], dp3[i - 1])
print(dp3[n - 1]) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR 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 VAR ASSIGN VAR VAR BIN_OP BIN_OP 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP 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 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. | tmp = list(map(int, input().split()))
n, p, q, r = tmp[0], tmp[1], tmp[2], tmp[3]
a = list(map(int, input().split()))
d1 = []
d2 = []
d3 = []
d1.append(p * a[0])
for i in range(1, n):
d1.append(max(p * a[i], d1[i - 1]))
d2.append(p * a[0] + q * a[0])
for i in range(1, n):
d2.append(max(d1[i] + q * a[i], d2[i - 1]))
d3.append(p * a[0] + q * a[0] + r * a[0])
for i in range(1, n):
d3.append(max(d2[i] + r * a[i], d3[i - 1]))
print(d3[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER 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 EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER 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(" ")))
maxi = -1000000000000000000000
b = [a[0]]
c = [a[-1]]
if p > 0:
for i in range(1, n):
b.append(max(b[i - 1], a[i]))
else:
for i in range(1, n):
b.append(min(b[i - 1], a[i]))
if r > 0:
for i in range(1, n):
c.append(max(c[i - 1], a[-i - 1]))
else:
for i in range(1, n):
c.append(min(c[i - 1], a[-i - 1]))
for j in range(n):
s = p * b[j] + q * a[j] + r * c[-j - 1]
maxi = max(maxi, s)
print(maxi) | 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 NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER 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
def main():
import sys
input = sys.stdin.readline
n, p, q, r = map(int, input().split())
arr = list(map(int, input().split()))
dp = [([-(10**27)] * 3) for _ in range(n)]
dp[0][0] = p * arr[0]
dp[0][1] = dp[0][0] + q * arr[0]
dp[0][2] = dp[0][1] + r * arr[0]
ans = dp[0][2]
for i in range(1, n):
dp[i][0] = max(dp[i - 1][0], p * arr[i])
dp[i][1] = max(dp[i - 1][1], dp[i][0] + q * arr[i])
dp[i][2] = max(dp[i - 1][2], dp[i][1] + r * arr[i])
ans = max(ans, dp[i][2])
print(ans)
return 0
main() | IMPORT FUNC_DEF 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 LIST BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN 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. | def f(n, p, q, r, a):
dp = [[(0) for i in range(n)] for j in range(3)]
dp[0][0] = p * a[0]
dp[1][0] = dp[0][0] + q * a[0]
dp[2][0] = dp[1][0] + r * a[0]
for i in range(1, n):
dp[0][i] = max(dp[0][i - 1], p * a[i])
dp[1][i] = max(dp[1][i - 1], dp[0][i] + q * a[i])
dp[2][i] = max(dp[2][i - 1], dp[1][i] + r * a[i])
return dp[2][n - 1]
x = input()
x1 = x.split(" ")
x2 = [int(t) for t in x1]
y = input()
y1 = y.split(" ")
y2 = [int(t) for t in y1]
print(f(x2[0], x2[1], x2[2], x2[3], y2)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER 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()))
di = [[(0) for j in range(3)] for i in range(n)]
di[0][0] = p * a[0]
di[0][1] = a[0] * (p + q)
di[0][2] = a[0] * (p + q + r)
for i in range(1, n):
di[i][0] = max(di[i - 1][0], p * a[i])
for i in range(1, n):
di[i][1] = max(di[i - 1][1], di[i][0] + q * a[i])
for i in range(1, n):
di[i][2] = max(di[i - 1][2], di[i][1] + r * a[i])
print(di[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 NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR 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. | read_int = lambda string: list(map(int, string.split()))
def max_until_i(k, arr, v):
x = [None] * len(arr)
x[0] = k * arr[0] + v[0]
for i in range(1, len(arr)):
x[i] = max(x[i - 1], k * arr[i] + v[i])
return x
def main():
n, p, q, r = read_int(input())
arr = read_int(input())
x = max_until_i(p, arr, [0] * len(arr))
y = max_until_i(q, arr, x)
z = max_until_i(r, arr, y)
print(z[-1])
main() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER 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 BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR 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 = map(int, input().split())
arr = list(map(int, input().split()))
l = float("-inf")
Pmax = [0] * N
Pmax[0] = P * arr[0]
for i in range(1, N):
Pmax[i] = max(Pmax[i - 1], P * arr[i])
Smax = [0] * N
Smax[N - 1] = R * arr[N - 1]
for j in range(N - 2, -1, -1):
Smax[j] = max(R * arr[j], Smax[j + 1])
for i in range(0, N):
l = max(l, Pmax[i] + Q * arr[i] + Smax[i])
print(l) | 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 ASSIGN VAR BIN_OP LIST NUMBER VAR 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 ASSIGN VAR BIN_OP LIST NUMBER 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 BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER 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 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(" "))
y1 = list(map(int, input().split()))
dp = [0] * n
for i in range(n):
if i == 0:
dp[i] = x * y1[i]
else:
dp[i] = max(x * y1[i] + dp[i], dp[i - 1])
for i in range(n):
if i == 0:
dp[i] = y * y1[i] + dp[i]
else:
dp[i] = max(dp[i - 1], dp[i] + y1[i] * y)
for i in range(n):
if i == 0:
dp[i] = z * y1[i] + dp[i]
else:
dp[i] = max(dp[i - 1], dp[i] + y1[i] * z)
print(dp[-1]) | 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 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 BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR 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 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())
List = [int(x) for x in input().split()]
MaxPre = []
MaxSuf = []
MinPre = []
MinSuf = []
Max = List[0]
Min = List[0]
for i in range(n):
Max = max(Max, List[i])
Min = min(Min, List[i])
MaxPre.append(Max)
MinPre.append(Min)
Max = List[-1]
Min = List[-1]
for i in range(n - 1, -1, -1):
Max = max(Max, List[i])
Min = min(Min, List[i])
MaxSuf.append(Max)
MinSuf.append(Min)
MaxSuf.reverse()
MinSuf.reverse()
Ans = -1000000000000000000000000000000000000000000000
for i in range(n):
cal = q * List[i]
if p < 0:
cal += p * MinPre[i]
else:
cal += p * MaxPre[i]
if r < 0:
cal += r * MinSuf[i]
else:
cal += r * MaxSuf[i]
Ans = max(Ans, cal)
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 LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR 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. | a = list(map(int, input().split()))
arr = list(map(int, input().split()))
c = a[1:]
pmax = [float("-inf") for i in range(a[0])]
smax = [float("-inf") for i in range(a[0])]
pmax[0] = c[0] * arr[0]
v = float("-inf")
for i in range(a[0]):
pmax[i] = max(pmax[i - 1], c[0] * arr[i])
smax[a[0] - 1] = c[2] * arr[a[0] - 1]
for i in range(a[0] - 2, -1, -1):
smax[i] = max(smax[i + 1], c[2] * arr[i])
for i in range(a[0]):
v = max(v, pmax[i] + c[1] * arr[i] + smax[i])
print(v) | 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 FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER 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
input1 = input().split()
input1 = list(map(int, input1))
n = input1[0]
p = input1[1]
q = input1[2]
r = input1[3]
arr = input().split()
arr = list(map(int, arr))
def max_check(num1, num2):
if num1 > num2:
return num1
else:
return num2
pre_max = []
pre_max.append(p * arr[0])
for i in range(1, n):
val = max_check(pre_max[i - 1], p * arr[i])
pre_max.insert(i, val)
sub_max = [None] * n
sub_max[n - 1] = r * arr[n - 1]
for j in reversed(range(n - 1)):
sub_max[j] = max_check(sub_max[j + 1], r * arr[j])
ans = -999999999999999999999
for k in range(n):
max_val = pre_max[k] + q * arr[k] + sub_max[k]
ans = max_check(max_val, ans)
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR 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. | import sys
def get_list():
return list(map(int, sys.stdin.readline().strip().split()))
def get_int():
return map(int, sys.stdin.readline().strip().split())
n, p, q, r = get_int()
arr = get_list()
p_max = [0] * n
r_max = [0] * n
ans = -1e20
p_max[0] = p * arr[0]
r_max[n - 1] = r * arr[n - 1]
for i in range(1, n):
p_max[i] = max(p * arr[i], p_max[i - 1])
for i in range(n - 2, -1, -1):
r_max[i] = max(r_max[i + 1], r * arr[i])
for i in range(0, n):
ans = max(ans, p_max[i] + q * arr[i] + r_max[i])
print(ans) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER 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 VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP 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 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 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.buffer.readline
def solution():
n, p, q, r = map(int, input().split())
l = list(map(int, input().split()))
np = -2e19
nq = -2e19
nr = -2e19
for i in range(n):
np = max(np, p * l[i])
nq = max(nq, np + q * l[i])
nr = max(nr, nq + r * l[i])
print(nr)
solution() | IMPORT ASSIGN VAR VAR FUNC_DEF 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 ASSIGN 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 VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP 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 = map(int, input().split())
a = [int(i) for i in input().split()]
min_suf = [0] * n
max_suf = [0] * n
min_suf[-1] = [n - 1, a[n - 1]]
max_suf[-1] = min_suf[-1]
pqr = [0] * n
qr = [0] * n
rr = [0] * n
rr[-1] = a[-1] * r
for i in range(n - 2, -1, -1):
if a[i] < min_suf[i + 1][1]:
min_suf[i] = [i, a[i]]
else:
min_suf[i] = min_suf[i + 1]
if a[i] > max_suf[i + 1][1]:
max_suf[i] = [i, a[i]]
else:
max_suf[i] = max_suf[i + 1]
if r < 0:
rr[i] = min_suf[i][1] * r
else:
rr[i] = max_suf[i][1] * r
for i in range(n):
qr[i] = a[i] * q + rr[i]
for i in range(n - 2, -1, -1):
qr[i] = max(qr[i], qr[i + 1])
ans = a[0] * p + qr[0]
for i in range(n):
ans = max(a[i] * p + qr[i], ans)
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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER 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 NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR 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 BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP 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, r2 = list(map(int, input().split()))
a = list(map(int, input().split()))
small = 10**9 + 1
large = -(10**9) - 1
infa = 10**9 + 1
infb = -(10**9) - 1
l = []
r = []
for i in range(0, n):
small = min(small, a[i])
large = max(large, a[i])
l.append((small, large))
small = 10**9 + 1
large = -(10**9) - 1
for i in range(n - 1, -1, -1):
small = min(small, a[i])
large = max(large, a[i])
r.append((small, large))
r = r[-1::-1]
s = 0
x = a[0]
maxs = x * p + x * q + x * r2
for i in range(n):
s = 0
if p > 0:
s = s + p * l[i][1]
else:
s = s + p * l[i][0]
if r2 > 0:
s = s + r2 * r[i][1]
else:
s = s + r2 * r[i][0]
s = s + q * a[i]
maxs = max(s, maxs)
print(maxs) | 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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP 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. | n, p, q, r = [int(i) for i in input().split(" ")]
a = [int(i) for i in input().split(" ")]
m_inf = float("-inf")
dp = []
for i in range(n + 1):
dp.append([m_inf, m_inf, m_inf])
for i in range(1, n + 1):
dp[i][0] = max(dp[i - 1][0], a[i - 1] * p)
dp[i][1] = max(dp[i - 1][1], dp[i][0] + a[i - 1] * q)
dp[i][2] = max(dp[i - 1][2], dp[i][1] + a[i - 1] * r)
print(dp[i][2]) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST 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 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. | max_integer, map1 = max, map
n, p, q, r = map1(int, input().strip().split())
list1 = list(map1(int, input().strip().split()))
prefix_max, suffix_max, result = [0] * n, [0] * n, float("-inf")
prefix_max[0] = p * list1[0]
for i in range(1, n):
prefix_max[i] = max_integer(prefix_max[i - 1], p * list1[i])
suffix_max[n - 1] = r * list1[n - 1]
for i in range(n - 2, -1, -1):
suffix_max[i] = max_integer(suffix_max[i + 1], r * list1[i])
for i in range(n):
result = max_integer(result, prefix_max[i] + q * list1[i] + suffix_max[i])
print(f"{result}") | ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FUNC_CALL VAR STRING 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 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 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 = map(int, input().split())
a = list(map(int, input().split()))
rl = [a[-1] * r]
pl = [a[0] * p]
for i in range(2, n + 1):
rl.append(max(rl[-1], a[-i] * r))
pl.append(max(pl[-1], a[i - 1] * p))
rl.reverse()
ans = pl[0] + a[0] * q + rl[0]
for i in range(1, n):
ans = max(ans, pl[i] + a[i] * q + rl[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 BIN_OP VAR NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER 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 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())
a = list(map(int, input().split()))
pqr = [p, q, r]
mat = [[(0) for j in range(n)] for i in range(3)]
for i in range(3):
for j in range(n):
if i == 0:
if j == 0:
mat[i][j] = pqr[i] * a[j]
else:
mat[i][j] = max(mat[i][j - 1], pqr[i] * a[j])
elif i == 1 or i == 2:
if j == 0:
mat[i][j] = mat[i - 1][j] + pqr[i] * a[j]
else:
mat[i][j] = max(mat[i][j - 1], mat[i - 1][j] + pqr[i] * a[j])
print(mat[2][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 LIST VAR VAR 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 VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR 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 VAR EXPR FUNC_CALL VAR VAR NUMBER 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, a, b, c = map(int, input().split(" "))
l = list(map(int, input().split(" ")))
dp = [([0] * n) for i in range(3)]
dp[0][0] = a * l[0]
dp[1][0] = dp[0][0] + b * l[0]
dp[2][0] = dp[1][0] + c * l[0]
for i in range(1, n):
dp[0][i] = max(dp[0][i - 1], a * l[i])
dp[1][i] = max(dp[1][i - 1], dp[0][i] + b * l[i])
dp[2][i] = max(dp[2][i - 1], dp[1][i] + c * l[i])
print(dp[2][n - 1]) | 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 BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER 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()))
mxLeft = [a[0]]
mnLeft = [a[0]]
for i in range(1, n):
mxLeft.append(max(mxLeft[-1], a[i]))
mnLeft.append(min(mnLeft[-1], a[i]))
mxRight = [a[n - 1]]
mnRight = [a[n - 1]]
for i in range(n - 2, -1, -1):
mxRight.append(max(mxRight[-1], a[i]))
mnRight.append(min(mnRight[-1], a[i]))
ans = []
for i in range(n):
ans.append(q * a[i])
if p < 0:
ans[-1] += mnLeft[i] * p
else:
ans[-1] += mxLeft[i] * p
if r < 0:
ans[-1] += mnRight[n - i - 1] * r
else:
ans[-1] += mxRight[n - i - 1] * r
print(max(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 VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER 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())
li = []
li = list(map(int, input().split()))
for i in range(len(li)):
if i == 0:
x = li[i] * p
y = li[i] * q + x
z = li[i] * r + y
else:
x = max(li[i] * p, x)
y = max(x + li[i] * q, y)
z = max(y + li[i] * r, z)
print(z) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP 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(x) for x in input().split()]
b = [int(y) for y in input().split()]
n = a[0]
p, q, r = a[1], a[2], a[3]
left = []
right = []
for i in range(n):
left.append(b[i] * p)
right.append(b[i] * r)
prefixMax = [0] * n
temp = -10000000000000000000
i = 0
while i < n:
temp = max(temp, left[i])
prefixMax[i] = temp
i += 1
temp = -10000000000000000000
suffixMax = [0] * n
i = n - 1
while i >= 0:
temp = max(temp, right[i])
suffixMax[i] = temp
i -= 1
ans = -10000000000000000000
for j in range(n):
aJ = q * b[j]
lMax = prefixMax[j]
rMax = suffixMax[j]
s = aJ + lMax + rMax
ans = max(ans, s)
print(ans) | 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 NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP 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 maxe(a, b):
if a > b:
return a
elif a < b:
return b
else:
return a
def pmmax(a, p):
x = 0
a[x] = a[x] * p
max = a[0]
y = x + 1
cap = len(a) - 1
while y != cap + 1:
max = maxe(max, p * a[y])
a[y] = max
y = y + 1
return a
def smmax(a, r):
x = len(a) - 1
a[x] = a[x] * r
max = a[x]
y = x - 1
while y != -1:
max = maxe(max, r * a[y])
a[y] = max
y = y - 1
return a
def main():
n, p, q, r = list(map(int, input().strip().split()))[:4]
a = []
a = list(map(int, input().strip().split()))[:n]
b = list(a)
b = pmmax(b, p)
c = list(a)
c = smmax(c, r)
start = 0
finish = len(a) - 1
max = a[0] * p + a[0] * q + a[0] * r
max = maxe(max, a[finish] * p + a[finish] * q + a[finish] * r)
st = 1
i = 0
while start != finish:
max = maxe(max, a[start] * p + a[start] * q + a[start] * r)
max = maxe(max, a[start] * p + a[start] * q + c[start + 1])
if start != 0:
max = maxe(max, b[start - 1] + a[start] * q + a[start] * r)
max = maxe(max, b[start - 1] + a[start] * q + c[start + 1])
start = start + 1
print(max)
main() | FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE 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 ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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. | a = input().split()
n = int(a[0])
p, q, r = int(a[1]), int(a[2]), int(a[3])
ar = [p, q, r]
array = input().split()
fin = []
for i in array:
fin.append(int(i))
dp = [[(0) for x in range(len(array))] for y in range(3)]
i = 0
while i < len(array):
j = 0
while j < 3:
if i == 0:
if j == 0:
dp[j][i] = ar[j] * fin[0]
else:
dp[j][i] = dp[j - 1][i] + ar[j] * fin[0]
elif j == 0:
dp[j][i] = max(ar[j] * fin[i], dp[j][i - 1])
else:
dp[j][i] = max(dp[j][i - 1], dp[j - 1][i] + ar[j] * fin[i])
j += 1
i += 1
print(dp[2][-1]) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER 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 VAR VAR NUMBER VAR NUMBER 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. | inp = input().split()
n = int(inp[0])
p = int(inp[1])
q = int(inp[2])
r = int(inp[3])
del inp
del n
a = input().split()
mas_1 = list()
mas_2 = list()
mas_3 = list()
max = int(a[0]) * p
for i in a:
if int(i) * p > max:
max = int(i) * p
mas_1.append(max)
max = mas_1[0] + int(a[0]) * q
for i in range(len(a)):
if mas_1[i] + int(a[i]) * q > max:
max = mas_1[i] + int(a[i]) * q
mas_2.append(max)
max = mas_2[0] + int(a[0]) * r
for i in range(len(a)):
if mas_2[i] + int(a[i]) * r > max:
max = mas_2[i] + int(a[i]) * r
mas_3.append(max)
print(mas_3[len(mas_3) - 1]) | 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 VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP 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())
a = list(map(int, input().split()))
total = p + q + r
if n == 1:
print(total * a[0])
exit()
if p > 0 and q > 0 and r > 0:
print(total * max(a))
exit()
if p < 0 and q < 0 and r < 0:
print(total * min(a))
exit()
p_vals = [None] * n
p_vals[0] = p * a[0]
for i in range(1, n):
p_vals[i] = max(p_vals[i - 1], p * a[i])
pq_vals = [None] * n
pq_vals[0] = p_vals[0] + q * a[0]
for i in range(1, n):
pq_vals[i] = max(pq_vals[i - 1], p_vals[i] + q * a[i])
ans = pq_vals[0] + r * a[0]
for i in range(1, n):
ans = max(ans, pq_vals[i] + r * a[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 BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR 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 ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER BIN_OP 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 BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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
n, p, q, r = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
def find_pref_sum(array):
maxm = -(2**63)
pref_sum = [0] * n
for i in range(0, n):
pref_sum[i] = max(maxm, array[i] * p)
maxm = max(maxm, array[i] * p)
return pref_sum
def find_suff_sum(array):
maxm = -(2**63)
suff_sum = [0] * n
for j in reversed(range(n)):
suff_sum[j] = max(maxm, array[j] * r)
maxm = max(maxm, array[j] * r)
return suff_sum
pref_sum = find_pref_sum(arr)
suff_sum = find_suff_sum(arr)
def find_max_expression(p, q, r, arr):
max_exp = -(2**63)
for i in range(0, n):
max_exp = max(
max_exp,
max(p * arr[i], pref_sum[i]) + arr[i] * q + max(r * arr[i], suff_sum[i]),
)
return max_exp
max_expression_val = find_max_expression(p, q, r, arr)
print(max_expression_val) | IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN 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. | n, p, q, r = [int(i) for i in input().split(" ")]
inp_arr = [int(i) for i in input().split(" ")]
prefix_max = []
suffix_max = []
maxi = -(2**63)
for i in inp_arr:
if i * p > maxi:
maxi = i * p
prefix_max.append(maxi)
maxi = -(2**63)
for i in range(n - 1, -1, -1):
if inp_arr[i] * r > maxi:
maxi = inp_arr[i] * r
suffix_max.append(maxi)
suffix_max.reverse()
maxi = -(2**63)
for i in range(n):
if prefix_max[i] + suffix_max[i] + inp_arr[i] * q > maxi:
maxi = prefix_max[i] + suffix_max[i] + inp_arr[i] * q
print(maxi) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN 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 = list(map(int, input().split(" ")))
a = list(map(int, input().split(" ")))
max1 = {}
min1 = {}
max2 = {}
min2 = {}
maxval1 = a[0]
minval1 = a[0]
maxval2 = a[n - 1]
minval2 = a[n - 1]
for i in range(n):
j = n - 1 - i
maxval1 = max(maxval1, a[i])
max1[i] = maxval1
minval1 = min(minval1, a[i])
min1[i] = minval1
maxval2 = max(maxval2, a[j])
max2[j] = maxval2
minval2 = min(minval2, a[j])
min2[j] = minval2
x = None
for i in range(n):
y = q * a[i]
if p > 0:
y += p * max1[i]
else:
y += p * min1[i]
if r > 0:
y += r * max2[i]
else:
y += r * min2[i]
if x is None or x < y:
x = y
print(x) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NONE VAR VAR ASSIGN 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. | maxi = ks = -554564764545467545566666634534554
l = list(map(int, input().split()))
l1 = list(map(int, input().split()))
r_list, p_list = [0] * len(l1), [0] * len(l1)
r_list[-1] = l[3] * l1[-1]
p_list[0] = l1[0] * l[1]
for i in range(len(l1) - 2, -1, -1):
r_list[i] = max(r_list[i + 1], l1[i] * l[3])
for i in range(1, len(l1)):
p_list[i] = max(p_list[i - 1], l[1] * l1[i])
for i in range(len(l1)):
ks = r_list[i] + p_list[i] + l[2] * l1[i]
maxi = max(maxi, ks)
print(maxi) | ASSIGN VAR VAR NUMBER 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 BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP 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 BIN_OP VAR NUMBER BIN_OP VAR 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 NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER 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, a, b, c = map(int, input().split())
array = list(map(int, input().split()))
val_max = (10**20 + 1) * -1
array_b = []
for x in range(n):
if a * array[x] > val_max:
val_max = a * array[x]
array_b.append(b * array[x] + val_max)
resp = (10**20 + 1) * -1
val_max = (10**18 + 1) * -1
for y in range(n):
if c * array[n - y - 1] > val_max:
val_max = c * array[n - y - 1]
if array_b[n - y - 1] + val_max > resp:
resp = array_b[n - y - 1] + val_max
print(resp) | 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 NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP 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. | f = lambda: map(int, input().split())
n, p, q, r = f()
a = b = c = -(10**19)
d = list(f())
for i in range(n):
a = max(a, d[i] * p)
b = max(b, a + d[i] * q)
c = max(c, b + d[i] * r)
print(c) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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
n, p, q, r = map(int, input().split())
lst = list(map(int, input().split()))
suffix_max = [(0) for _ in range(n)]
suffix_max[n - 1] = r * lst[n - 1]
for i in range(n - 2, -1, -1):
suffix_max[i] = max(suffix_max[i + 1], lst[i] * r)
left = lst[0] * p
res = -999999999999999999999999999999
for i in range(n):
mid = lst[i] * q
left = max(left, lst[i] * p)
right = suffix_max[i]
res = max(res, left + right + mid)
print(res) | 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 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 BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN 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 VAR BIN_OP 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())
arr = [int(i) for i in input().split()]
parr = [arr[0] * p]
for i in range(1, len(arr)):
val = max(arr[i] * p, parr[i - 1])
parr.append(val)
qarr = [arr[0] * q + p * arr[0]]
for i in range(1, len(arr)):
val = max(qarr[i - 1], arr[i] * q + parr[i])
qarr.append(val)
rarr = [arr[0] * q + p * arr[0] + r * arr[0]]
for i in range(1, len(arr)):
val = max(rarr[i - 1], r * arr[i] + qarr[i])
rarr.append(val)
print(rarr[-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 LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL 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())
A = list(map(int, input().split()))
P = [(p * a) for a in A]
Q = [(q * a) for a in A]
R = [(r * a) for a in A]
ans = -(10**20)
for i in range(1, n):
P[i] = max(P[i], P[i - 1])
PQ = [(0) for i in range(n)]
for i in range(n):
PQ[i] = P[i] + Q[i]
if i:
PQ[i] = max(PQ[i], PQ[i - 1])
for i in range(n):
ans = max(ans, PQ[i] + R[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 VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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())
nums = list(map(int, input().split()))
dp = [[(0) for _ in range(n + 1)] for _ in range(4)]
dp[0][0] = 0
coef = [p, q, r]
for i in range(1, 4):
for j in range(1, n + 1):
if j == 1:
dp[i][j] = dp[i - 1][j] + coef[i - 1] * nums[j - 1]
else:
dp[i][j] = max(dp[i - 1][j] + coef[i - 1] * nums[j - 1], dp[i][j - 1])
print(dp[3][n]) | 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 BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER 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 get_ans(n, p, q, r, a):
maxpre = [-100000000] * n
minpre = [100000000] * n
maxsuf = [-100000000] * n
minsuf = [100000000] * n
curmax = a[0]
curmin = a[0]
for i in range(n):
if a[i] > curmax:
curmax = a[i]
maxpre[i] = curmax
if a[i] < curmin:
curmin = a[i]
minpre[i] = curmin
curmax = a[n - 1]
curmin = a[n - 1]
for i in range(n - 1, -1, -1):
if a[i] > curmax:
curmax = a[i]
maxsuf[i] = curmax
if a[i] < curmin:
curmin = a[i]
minsuf[i] = curmin
ans = []
for i in range(n):
pmas = maxpre if p > 0 else minpre
rmas = maxsuf if r > 0 else minsuf
ans.append(pmas[i] * p + a[i] * q + rmas[i] * r)
return max(ans)
n, p, q, r = tuple(map(int, input().split()))
a = list(map(int, input().split()))
print(get_ans(n, p, q, r, a)) | FUNC_DEF 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 VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR 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 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. | import sys
n, p, q, r = list(map(int, input().split()))
a = list(map(int, input().split()))
minil = [0] * n
minir = [0] * n
maxil = [0] * n
maxir = [0] * n
minil[0] = maxil[0] = a[0]
minir[n - 1] = maxir[n - 1] = a[n - 1]
for i in range(1, n):
minil[i] = min(minil[i - 1], a[i])
maxil[i] = max(maxil[i - 1], a[i])
for i in range(n - 2, -1, -1):
minir[i] = min(minir[i + 1], a[i])
maxir[i] = max(maxir[i + 1], a[i])
ans = 0
tans = -(10**19)
for i in range(n):
ans = q * a[i]
ans += max(p * minil[i], p * maxil[i])
ans += max(r * minir[i], r * maxir[i])
tans = max(tans, ans)
print(tans) | IMPORT 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 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 VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP 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 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 NUMBER ASSIGN 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. | n, p, q, r = map(int, input().split())
a = list(map(int, input().split(" ")))
dpr = [(-(10**18)) for i in range(n)]
dpr[-1] = r * a[-1]
for k in reversed(range(n - 1)):
dpr[k] = max(dpr[k + 1], r * a[k])
dpqr = [(-(10**18)) for i in range(n)]
dpqr[-1] = (q + r) * a[-1]
for k in reversed(range(n - 1)):
dpqr[k] = max(dpqr[k + 1], q * a[k] + dpr[k])
ans = (p + q + r) * a[-1]
for k in reversed(range(n)):
ans = max(p * a[k] + dpqr[k], ans)
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 STRING ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP 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 = [int(i) for i in input().split()]
crmx, crmn = [a[n - 1]] * n, [a[n - 1]] * n
for i in range(n - 2, -1, -1):
crmx[i] = max(crmx[i + 1], a[i])
crmn[i] = min(crmn[i + 1], a[i])
ans = 0 - 10**21
mn, mx = a[0], a[0]
for i in range(n):
cans = q * a[i]
mx = max(mx, a[i])
mn = min(mn, a[i])
cans += max(mx * p, mn * p)
cans += max(crmx[i] * r, crmn[i] * r)
ans = max(ans, cans)
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 VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR 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 BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP 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. | n, p, q, r = input().split(" ")
arr = input().split(" ")
n = int(n)
p = int(p)
q = int(q)
r = int(r)
arr = [int(x) for x in arr]
prefix_max = []
maximum = float("-inf")
for i in range(len(arr)):
prefix_max.append(max(maximum, p * arr[i]))
maximum = prefix_max[-1]
maxi = float("-inf")
suffix_max = [(1**x) for x in range(len(arr))]
for i in range(len(arr) - 1, -1, -1):
suffix_max[i] = max(r * arr[i], maxi)
maxi = suffix_max[i]
max_len = [(1) for x in range(len(arr))]
for j in range(len(arr)):
max_len[j] = prefix_max[j] + q * arr[j] + suffix_max[j]
print(max(max_len)) | ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING 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 VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR 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()))
pre, suf = [p * a[0]], [r * a[n - 1]] * n
for i in range(1, n):
pre.append(max(pre[i - 1], p * a[i]))
suf[n - i - 1] = max(suf[n - i], r * a[n - i - 1])
ans = -float("inf")
for i in range(n):
ans = max(ans, pre[i] + suf[i] + a[i] * q)
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 VAR LIST BIN_OP VAR VAR NUMBER BIN_OP LIST BIN_OP VAR VAR BIN_OP VAR NUMBER VAR 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 BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP 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 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())
a = list(map(int, input().split()))
INF = float("inf")
R = [-INF] * n
R[-1] = a[-1] * r
for i in range(n - 2, -1, -1):
R[i] = max(R[i + 1], a[i] * r)
P = [-INF] * n
P[0] = a[0] * p
for i in range(1, n):
P[i] = max(P[i - 1], a[i] * p)
num = -INF
for i in range(n):
num = max(num, P[i] + a[i] * q + R[i])
print(num) | 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 ASSIGN VAR BIN_OP LIST VAR 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 VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR 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 VAR 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 maxValue(arr, n, p, q, r):
preMap = [0] * n
preMap[0] = p * arr[0]
suffMap = [0] * n
suffMap[n - 1] = r * arr[n - 1]
for i in range(1, n):
if preMap[i - 1] >= p * arr[i]:
preMap[i] = preMap[i - 1]
else:
preMap[i] = p * arr[i]
for i in range(n - 2, -1, -1):
if suffMap[i + 1] >= r * arr[i]:
suffMap[i] = suffMap[i + 1]
else:
suffMap[i] = r * arr[i]
maxm = float("-inf")
for i in range(n):
if suffMap[i] + preMap[i] + q * arr[i] >= maxm:
maxm = suffMap[i] + preMap[i] + q * arr[i]
return maxm
li = list(map(int, input().split()))
arr = list(map(int, input().split()))
print(maxValue(arr, li[0], li[1], li[2], li[3])) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER 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 FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER 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 Dumbledore2(n, p, q, r, arr):
dp = [None] * 100000
dp[0] = p * arr[0]
for i in range(1, n):
dp[i] = max(dp[i - 1], arr[i] * p)
dp1 = [None] * 100000
dp1[0] = dp[0] + q * arr[0]
for i in range(1, n):
dp1[i] = max(dp1[i - 1], arr[i] * q + dp[i])
dp2 = [None] * 100000
dp2[0] = dp1[0] + r * arr[0]
for i in range(1, n):
dp2[i] = max(dp2[i - 1], arr[i] * r + dp1[i])
print(dp2[n - 1])
n, p, q, r = map(int, input().split())
arr = list(map(int, input().split()))
Dumbledore2(n, p, q, r, arr) | FUNC_DEF ASSIGN VAR BIN_OP LIST NONE 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 ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER BIN_OP 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 BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER BIN_OP 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 BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR 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 EXPR 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. | def modified_pre_max(arr, p):
n = len(arr)
arr1 = ["None"] * n
arr1[0] = arr[0] * p
for i in range(1, n):
arr1[i] = max(arr1[i - 1], arr[i] * p)
return arr1
def modified_suf_max(arr, r):
n = len(arr)
arr1 = ["None"] * n
arr1[n - 1] = arr[n - 1] * r
for i in range(n - 2, -1, -1):
arr1[i] = max(arr1[i + 1], arr[i] * r)
return arr1
n, p, q, r = map(int, input().split())
arr = list(map(int, input().split()))
ans = float("-inf")
arr_pre_max = modified_pre_max(arr, p)
arr_suf_max = modified_suf_max(arr, r)
for i in range(0, n):
current_ans = arr_pre_max[i] + arr[i] * q + arr_suf_max[i]
ans = max(ans, current_ans)
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR 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 RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR 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 VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN 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 ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER 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. | def preMax(arr, coeff):
tempArr = [arr[0] * coeff]
for n in range(1, len(arr)):
tempArr.insert(n, max(arr[n] * coeff, tempArr[n - 1]))
return tempArr
def suffMax(arr, coeff):
tempArr = [arr[-1] * coeff]
for n in range(len(arr) - 1, 0, -1):
tempArr.insert(0, max(arr[n - 1] * coeff, tempArr[0]))
return tempArr
coeff = list(map(int, input().split(" ")))
arr = list(map(int, input().split(" ")))
pMax = preMax(arr, coeff[1])
sMax = suffMax(arr, coeff[3])
ans = None
for n in range(0, len(arr)):
tempAns = pMax[n] + arr[n] * coeff[2] + sMax[n]
if ans == None:
ans = tempAns
else:
ans = max(ans, tempAns)
print(ans) | FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER 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. | import sys
input = sys.stdin.readline
n, p, q, r = map(int, input().split())
a = list(map(int, input().split()))
ml = [a[0]]
Ml = [a[0]]
for i in range(1, n):
ml.append(min(ml[-1], a[i]))
Ml.append(max(Ml[-1], a[i]))
mr = [a[-1]]
Mr = [a[-1]]
for i in range(n - 2, -1, -1):
mr.append(min(mr[-1], a[i]))
Mr.append(max(Mr[-1], a[i]))
mr = mr[::-1]
Mr = Mr[::-1]
ans = -float("inf")
for i in range(n):
ans = max(ans, p * ml[i] + q * a[i] + r * mr[i])
ans = max(ans, p * ml[i] + q * a[i] + r * Mr[i])
ans = max(ans, p * Ml[i] + q * a[i] + r * mr[i])
ans = max(ans, p * Ml[i] + q * a[i] + r * Mr[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 LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER 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 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 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
n, p, q, r = [int(x) for x in input().split()]
pmax = [] * n
smax = [] * n
arr = list(map(int, input().split()))[:n]
pmax.append(p * arr[0])
for i in range(1, n):
pmax.append(max(pmax[i - 1], p * arr[i]))
smax.append(r * arr[n - 1])
for i in range(n - 2, -1, -1):
smax.insert(0, max(smax[0], r * arr[i]))
if p == 0 and q == 0 and r == 0:
print(0)
else:
ans = pmax[0] + q * arr[0] + smax[0]
i = 1
while i < n:
ans = max(ans, pmax[i] + q * arr[i] + smax[i])
i += 1
print(ans) | IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR ASSIGN VAR BIN_OP LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR 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 VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR 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. | import sys
n, p, q, r = map(int, input().split())
arr = list(map(int, input().split()))
suffix_max = [0] * n
suffix_max[n - 1] = r * arr[n - 1]
for i in range(n - 2, -1, -1):
suffix_max[i] = max(suffix_max[i + 1], r * arr[i])
max_left = -1 * sys.maxsize * 10**15
ans = -1 * sys.maxsize * 10**15
for j in range(0, n):
max_left = max(max_left, p * arr[j])
max_right = suffix_max[j]
max_value_1 = q * arr[j]
ans = max(ans, max_left + max_right + max_value_1)
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 BIN_OP LIST NUMBER 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 BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP 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 = tuple(int(i) for i in input().split(" "))
data = [int(i) for i in input().split(" ")]
prefix = []
suffix = []
prefix.append(data[0] * p)
suffix.append(data[n - 1] * r)
len_d = len(data)
maxnum = prefix[0]
for i in range(1, len_d):
if maxnum < data[i] * p:
maxnum = data[i] * p
prefix.append(maxnum)
maxnum = suffix[0]
for i in range(len_d - 2, -1, -1):
if maxnum < data[i] * r:
maxnum = data[i] * r
suffix.insert(0, maxnum)
maxnum = prefix[0] + q * data[0] + suffix[0]
for i in range(0, len_d):
tmp = prefix[i] + q * data[i] + suffix[i]
if maxnum < tmp:
maxnum = tmp
print(maxnum) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right.
Each cell of the matrix can be either free or locked.
Let's call a path in the matrix a staircase if it:
* starts and ends in the free cell;
* visits only free cells;
* has one of the two following structures:
1. the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on;
2. the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on.
In particular, a path, consisting of a single cell, is considered to be a staircase.
Here are some examples of staircases:
<image>
Initially all the cells of the matrix are free.
You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free.
Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Input
The first line contains three integers n, m and q (1 ≤ n, m ≤ 1000; 1 ≤ q ≤ 10^4) — the sizes of the matrix and the number of queries.
Each of the next q lines contains two integers x and y (1 ≤ x ≤ n; 1 ≤ y ≤ m) — the description of each query.
Output
Print q integers — the i-th value should be equal to the number of different staircases after i queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Examples
Input
2 2 8
1 1
1 1
1 1
2 2
1 1
1 2
2 1
1 1
Output
5
10
5
2
5
3
1
0
Input
3 4 10
1 4
1 2
2 3
1 2
2 3
3 2
1 3
3 4
1 3
3 1
Output
49
35
24
29
49
39
31
23
29
27
Input
1000 1000 2
239 634
239 634
Output
1332632508
1333333000 | from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n, m, q = list(map(int, input().split()))
matrix = [[[0, 0] for i in range(m)] for j in range(n)]
free = [[(True) for i in range(m)] for j in range(n)]
ans = 0
for i in range(n):
for j in range(m):
if i > 0:
matrix[i][j][1] += matrix[i - 1][j][0]
if j > 0:
matrix[i][j][0] += matrix[i][j - 1][1]
matrix[i][j][0] += 1
matrix[i][j][1] += 1
ans += matrix[i][j][0] + matrix[i][j][1] - 1
for _ in range(q):
x, y = list(map(int, input().split()))
x -= 1
y -= 1
count_right = 0
act_x = x
act_y = y + 1
right = True
while act_x < n and act_y < m and free[act_x][act_y]:
count_right += 1
if right:
act_x += 1
else:
act_y += 1
right = not right
count_down = 0
act_x = x + 1
act_y = y
right = False
while act_x < n and act_y < m and free[act_x][act_y]:
count_down += 1
if right:
act_x += 1
else:
act_y += 1
right = not right
count_left = 0
act_x = x
act_y = y - 1
left = True
while act_x >= 0 and act_y >= 0 and free[act_x][act_y]:
count_left += 1
if left:
act_x -= 1
else:
act_y -= 1
left = not left
count_up = 0
act_x = x - 1
act_y = y
left = False
while act_x >= 0 and act_y >= 0 and free[act_x][act_y]:
count_up += 1
if left:
act_x -= 1
else:
act_y -= 1
left = not left
count_stairs = 0
count_stairs += (count_up + 1) * (count_right + 1)
count_stairs += (count_down + 1) * (count_left + 1)
count_stairs -= 1
if free[x][y]:
ans -= count_stairs
else:
ans += count_stairs
free[x][y] = not free[x][y]
print(str(ans) + "\n") | ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right.
Each cell of the matrix can be either free or locked.
Let's call a path in the matrix a staircase if it:
* starts and ends in the free cell;
* visits only free cells;
* has one of the two following structures:
1. the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on;
2. the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on.
In particular, a path, consisting of a single cell, is considered to be a staircase.
Here are some examples of staircases:
<image>
Initially all the cells of the matrix are free.
You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free.
Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Input
The first line contains three integers n, m and q (1 ≤ n, m ≤ 1000; 1 ≤ q ≤ 10^4) — the sizes of the matrix and the number of queries.
Each of the next q lines contains two integers x and y (1 ≤ x ≤ n; 1 ≤ y ≤ m) — the description of each query.
Output
Print q integers — the i-th value should be equal to the number of different staircases after i queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Examples
Input
2 2 8
1 1
1 1
1 1
2 2
1 1
1 2
2 1
1 1
Output
5
10
5
2
5
3
1
0
Input
3 4 10
1 4
1 2
2 3
1 2
2 3
3 2
1 3
3 4
1 3
3 1
Output
49
35
24
29
49
39
31
23
29
27
Input
1000 1000 2
239 634
239 634
Output
1332632508
1333333000 | def main():
H, W, Q = map(int, input().split())
A = [[] for _ in range(H + W)]
for i in range(H):
for j in range(W):
A[(i + j + 1) // 2 * 2].append(0)
A[(i + j) // 2 * 2 + 1].append(0)
RawAns = H * W
Diff = [0] * (H + W)
for i in range(H + W):
K = len(A[i]) * (len(A[i]) - 1) // 2
Diff[i] = K
RawAns += K
for _ in range(Q):
h, w = map(int, input().split())
h -= 1
w = W - w
i1 = (h + w + 1) // 2 * 2
j1 = h * 2 + (h + w) % 2 if i1 < W else (W - w - 1) * 2 + (h + w + 1) % 2
i2 = (h + w) // 2 * 2 + 1
j2 = h * 2 + (h + w + 1) % 2 if i2 < W else (W - w - 1) * 2 + (h + w) % 2
if A[i1][j1]:
RawAns += 1
else:
RawAns -= 1
A[i1][j1] ^= 1
A[i2][j2] ^= 1
Tmp1 = 0
Count = -1
for a in A[i1]:
if a:
Tmp1 += Count * (Count + 1) // 2
Count = -1
else:
Count += 1
Tmp1 += Count * (Count + 1) // 2
Tmp2 = 0
Count = -1
for a in A[i2]:
if a:
Tmp2 += Count * (Count + 1) // 2
Count = -1
else:
Count += 1
Tmp2 += Count * (Count + 1) // 2
RawAns -= Diff[i1] - Tmp1 + Diff[i2] - Tmp2
Diff[i1] = Tmp1
Diff[i2] = Tmp2
print(RawAns)
main() | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right.
Each cell of the matrix can be either free or locked.
Let's call a path in the matrix a staircase if it:
* starts and ends in the free cell;
* visits only free cells;
* has one of the two following structures:
1. the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on;
2. the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on.
In particular, a path, consisting of a single cell, is considered to be a staircase.
Here are some examples of staircases:
<image>
Initially all the cells of the matrix are free.
You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free.
Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Input
The first line contains three integers n, m and q (1 ≤ n, m ≤ 1000; 1 ≤ q ≤ 10^4) — the sizes of the matrix and the number of queries.
Each of the next q lines contains two integers x and y (1 ≤ x ≤ n; 1 ≤ y ≤ m) — the description of each query.
Output
Print q integers — the i-th value should be equal to the number of different staircases after i queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Examples
Input
2 2 8
1 1
1 1
1 1
2 2
1 1
1 2
2 1
1 1
Output
5
10
5
2
5
3
1
0
Input
3 4 10
1 4
1 2
2 3
1 2
2 3
3 2
1 3
3 4
1 3
3 1
Output
49
35
24
29
49
39
31
23
29
27
Input
1000 1000 2
239 634
239 634
Output
1332632508
1333333000 | class Array2d:
def __init__(self, n, m, arr):
self.n = n
self.m = m
self.arr = arr
@classmethod
def full(cls, n, m, fill_value):
return cls(n, m, [fill_value] * (n * m))
@classmethod
def from_list(cls, lst):
n, m = len(lst), len(lst[0])
arr = [lst[0]] * (n * m)
k = 0
for row in lst:
for v in row:
arr[k] = v
k += 1
return cls(n, m, arr)
def _get_view(self, i, j):
i = tuple(range(0, self.n * self.m, self.m))[i]
j = tuple(range(self.m))[j]
return Array2dView(self.arr, i, j)
def get_ind(self, i, j):
return i * self.m + j
def __getitem__(self, index):
try:
return self.arr[self.get_ind(*index)]
except TypeError:
return self._get_view(*index)
def __setitem__(self, index, value):
try:
self.arr[self.get_ind(*index)] = value
except TypeError:
x = self._get_view(*index)
for i in x.i_indices:
for j in x.j_indices:
self.arr[i + j] = value
def __iter__(self):
return iter(self.arr)
def __reversed__(self):
return reversed(self.arr)
def __str__(self):
m = max(len(str(v)) for v in self)
res = [""] * self.n
row = [""] * (self.m + 2)
for i in range(self.n):
if i == 0:
row[0] = "["
else:
row[0] = " "
if i == self.n - 1:
row[-1] = "]\n"
for j in range(self.m):
row[j + 1] = f"{str(self.arr[i * self.m + j]):>{m + 1}}"
res[i] = "".join(row)
return "\n".join(res)
def __eq__(self, other):
return self.arr == other.arr
def copy(self):
return self.__init__(self.n, self.m, self.arr[:])
@property
def t(self):
arr = [self.arr[0]] * len(self.arr)
x = 0
for i in range(self.n):
for j in range(self.m):
arr[j * self.n + i] = self.arr[x]
x += 1
return self.__init__(self.m, self.n, arr)
def helper(n, m, queries):
grid = Array2d.full(n, m, 0)
cnt = 0
for s in range(1, min(n, m) + 1):
cnt += (n - s) * (m - s) * 2 + (n - s) * (m - s + 1) + (n - s + 1) * (m - s)
cnt += n * m
res = [cnt]
for i, j in queries:
ul = 0
x, y = i, j - 1
f = True
while x >= 0 and y >= 0 and not grid[x, y]:
ul += 1
if f:
x -= 1
else:
y -= 1
f = not f
rd = 0
x, y = i + 1, j
f = True
while x < n and y < m and not grid[x, y]:
rd += 1
if f:
y += 1
else:
x += 1
f = not f
lu = 0
x, y = i - 1, j
f = False
while x >= 0 and y >= 0 and not grid[x, y]:
lu += 1
if f:
x -= 1
else:
y -= 1
f = not f
dr = 0
x, y = i, j + 1
f = False
while x < n and y < m and not grid[x, y]:
dr += 1
if f:
y += 1
else:
x += 1
f = not f
k = (ul + 1) * (rd + 1) + (lu + 1) * (dr + 1) - 1
r = res[-1]
if not grid[i, j]:
res.append(r - k)
grid[i, j] = 1
else:
res.append(r + k)
grid[i, j] = 0
return res[1:]
n, m, q = map(int, input().split())
queries = [tuple(int(c) - 1 for c in input().split()) for _ in range(q)]
print(*helper(n, m, queries), sep="\n") | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL STRING VAR RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR FOR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING |
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right.
Each cell of the matrix can be either free or locked.
Let's call a path in the matrix a staircase if it:
* starts and ends in the free cell;
* visits only free cells;
* has one of the two following structures:
1. the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on;
2. the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on.
In particular, a path, consisting of a single cell, is considered to be a staircase.
Here are some examples of staircases:
<image>
Initially all the cells of the matrix are free.
You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free.
Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Input
The first line contains three integers n, m and q (1 ≤ n, m ≤ 1000; 1 ≤ q ≤ 10^4) — the sizes of the matrix and the number of queries.
Each of the next q lines contains two integers x and y (1 ≤ x ≤ n; 1 ≤ y ≤ m) — the description of each query.
Output
Print q integers — the i-th value should be equal to the number of different staircases after i queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Examples
Input
2 2 8
1 1
1 1
1 1
2 2
1 1
1 2
2 1
1 1
Output
5
10
5
2
5
3
1
0
Input
3 4 10
1 4
1 2
2 3
1 2
2 3
3 2
1 3
3 4
1 3
3 1
Output
49
35
24
29
49
39
31
23
29
27
Input
1000 1000 2
239 634
239 634
Output
1332632508
1333333000 | import sys
from sys import stdin
def calc(l):
assert l >= 0
return l * (l - 1) // 2
n, m, q = map(int, stdin.readline().split())
xy = []
color = [([0] * m) for i in range(n)]
freenum = n * m
ans = 0
maxi = [float("-inf")] * 2002
mini = [float("inf")] * 2002
for i in range(n):
for j in range(m):
diff = i - j
s = i + j
maxi[diff] = max(maxi[diff], s)
mini[diff] = min(mini[diff], s)
diff = i - 1 - j
s = i + j
maxi[diff] = max(maxi[diff], s)
mini[diff] = min(mini[diff], s)
ans = 0
for i in range(2002):
if maxi[i] != float("-inf"):
ans += calc(maxi[i] - mini[i] + 1)
ANS = []
for i in range(q):
x, y = map(int, stdin.readline().split())
x -= 1
y -= 1
xx = x
yy = y - 1
cnt = 0
while xx >= 0 and yy >= 0 and color[xx][yy] == 0:
if cnt == 0:
xx -= 1
else:
yy -= 1
cnt ^= 1
XX = x + 1
YY = y
cnt = 0
while XX < n and YY < m and color[XX][YY] == 0:
if cnt == 0:
YY += 1
else:
XX += 1
cnt ^= 1
sp = xx + yy
sq = XX + YY
sa = x + y
if color[x][y] == 0:
ans -= calc(sq - sp - 1)
ans += calc(sa - sp - 1) + calc(sq - sa - 1)
else:
ans += calc(sq - sp - 1)
ans -= calc(sa - sp - 1) + calc(sq - sa - 1)
xx = x - 1
yy = y
cnt = 0
while xx >= 0 and yy >= 0 and color[xx][yy] == 0:
if cnt == 0:
yy -= 1
else:
xx -= 1
cnt ^= 1
XX = x
YY = y + 1
cnt = 0
while XX < n and YY < m and color[XX][YY] == 0:
if cnt == 0:
XX += 1
else:
YY += 1
cnt ^= 1
sp = xx + yy
sq = XX + YY
sa = x + y
if color[x][y] == 0:
ans -= calc(sq - sp - 1)
ans += calc(sa - sp - 1) + calc(sq - sa - 1)
else:
ans += calc(sq - sp - 1)
ans -= calc(sa - sp - 1) + calc(sq - sa - 1)
if color[x][y] == 0:
freenum -= 1
color[x][y] = 1
else:
freenum += 1
color[x][y] = 0
ANS.append(ans + freenum)
print("\n".join(map(str, ANS))) | IMPORT FUNC_DEF VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right.
Each cell of the matrix can be either free or locked.
Let's call a path in the matrix a staircase if it:
* starts and ends in the free cell;
* visits only free cells;
* has one of the two following structures:
1. the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on;
2. the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on.
In particular, a path, consisting of a single cell, is considered to be a staircase.
Here are some examples of staircases:
<image>
Initially all the cells of the matrix are free.
You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free.
Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Input
The first line contains three integers n, m and q (1 ≤ n, m ≤ 1000; 1 ≤ q ≤ 10^4) — the sizes of the matrix and the number of queries.
Each of the next q lines contains two integers x and y (1 ≤ x ≤ n; 1 ≤ y ≤ m) — the description of each query.
Output
Print q integers — the i-th value should be equal to the number of different staircases after i queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path.
Examples
Input
2 2 8
1 1
1 1
1 1
2 2
1 1
1 2
2 1
1 1
Output
5
10
5
2
5
3
1
0
Input
3 4 10
1 4
1 2
2 3
1 2
2 3
3 2
1 3
3 4
1 3
3 1
Output
49
35
24
29
49
39
31
23
29
27
Input
1000 1000 2
239 634
239 634
Output
1332632508
1333333000 | import sys
input = sys.stdin.readline
output = sys.stdout.write
def solve(R, C, Q, queries):
blocked = [([0] * C) for _ in range(R)]
res = R * C
for c in range(C):
for ln in range(1, R + C + 1):
nr = ln // 2
nc = c + (ln + 1) // 2
if nr >= R or nc >= C:
break
res += ln
for r in range(R):
for ln in range(1, R + C + 1):
nr = r + (ln + 1) // 2
nc = ln // 2
if nr >= R or nc >= C:
break
res += ln
for qr, qc in queries:
change = 1
r, c = qr, qc
up = down = 0
for ln in range(1, R + C + 1):
nr = r + ln // 2
nc = c + (ln + 1) // 2
if nr >= R or nc >= C or blocked[nr][nc]:
break
down += 1
for ln in range(1, R + C + 1):
nr = r - (ln + 1) // 2
nc = c - ln // 2
if nr < 0 or nc < 0 or blocked[nr][nc]:
break
up += 1
change += up + down + up * down
r, c = qr, qc
up = down = 0
for ln in range(1, R + C + 1):
nr = r + (ln + 1) // 2
nc = c + ln // 2
if nr >= R or nc >= C or blocked[nr][nc]:
break
down += 1
for ln in range(1, R + C + 1):
nr = r - ln // 2
nc = c - (ln + 1) // 2
if nr < 0 or nc < 0 or blocked[nr][nc]:
break
up += 1
change += up + down + up * down
res += change if blocked[qr][qc] else -change
blocked[qr][qc] ^= 1
output(str(res) + "\n")
def main():
N, M, Q = list(map(int, input().split()))
queries = []
for _ in range(Q):
x, y = list(map(int, input().split()))
queries.append((x - 1, y - 1))
solve(N, M, Q, queries)
main() | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def betterString(self, s, t):
def countSubSets(s):
c, l, d = 1, [1], {}
for i in range(len(s)):
c *= 2
if s[i] in d:
c -= l[d[s[i]] - 1]
d[s[i]] = i + 1
l.append(c)
return c
c1, c2 = map(countSubSets, (str1, str2))
return str1 if c1 == c2 or c1 > c2 else str2 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER LIST NUMBER DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | def func(S):
last = [-1] * 256
dp = [0] * (len(S) + 1)
dp[0] = 1
for i in range(1, len(S) + 1):
dp[i] = dp[i - 1] * 2
if last[ord(S[i - 1])] != -1:
dp[i] -= dp[last[ord(S[i - 1])]]
last[ord(S[i - 1])] = i - 1
return dp[len(S)] % int(1000000000.0 + 7)
class Solution:
def betterString(self, str1, str2):
a = func(str1)
b = func(str2)
if a >= b:
return str1
return str2 | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def betterString(self, str1, str2):
def funcall(string):
count = 1
dicti = dict()
for char in string:
if char not in dicti:
dicti[char] = count
count *= 2
else:
temp = dicti[char]
dicti[char] = count
count *= 2
count -= temp
return count
arr1 = funcall(str1)
arr2 = funcall(str2)
if arr2 > arr1:
return str2
else:
return str1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def getLen(self, st):
dp = [1]
lo = dict()
for i in range(1, len(st) + 1):
dp.append(2 * dp[i - 1])
ch = st[i - 1]
if ch in lo:
j = lo[ch]
dp[i] = dp[i] - dp[j - 1]
lo[ch] = i
return dp[-1]
def betterString(self, str1, str2):
r1 = self.getLen(str1)
r2 = self.getLen(str2)
if r1 >= r2:
return str1
else:
return str2 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def betterString(self, s1, s2):
l = [0] * len(s1)
l[0] = 2
h, h1 = {}, {}
for i in range(len(s1)):
h[i] = h1.get(s1[i], -1)
h1[s1[i]] = i
for i in range(1, len(s1)):
if h[i] == -1:
l[i] = l[i - 1] * 2
elif h[i] == 0:
l[i] = l[i - 1] * 2 - 1
else:
l[i] = l[i - 1] * 2 - l[h[i] - 1]
l1 = [0] * len(s2)
l1[0] = 2
h3, h4 = {}, {}
for i in range(len(s2)):
h3[i] = h4.get(s2[i], -1)
h4[s2[i]] = i
for i in range(1, len(s2)):
if h3[i] == -1:
l1[i] = l1[i - 1] * 2
elif h3[i] == 0:
l1[i] = l1[i - 1] * 2 - 1
else:
l1[i] = l1[i - 1] * 2 - l1[h3[i] - 1]
if l[-1] >= l1[-1]:
return s1
return s2 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def countSub(self, str):
last = [-1] * 127
n = len(str)
dp = [0] * (n + 1)
dp[0] = 1
for i in range(1, n + 1):
dp[i] = 2 * dp[i - 1]
if last[ord(str[i - 1])] != -1:
dp[i] -= dp[last[ord(str[i - 1])]]
last[ord(str[i - 1])] = i - 1
return dp[n]
def betterString(self, str1, str2):
if self.countSub(str1) < self.countSub(str2):
return str2
return str1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def betterString(self, str1, str2):
def count_subsequences(s):
n = len(s)
dp = [0] * (n + 1)
dp[0] = 1
last = {}
for i in range(1, n + 1):
dp[i] = 2 * dp[i - 1]
if s[i - 1] in last:
dp[i] -= dp[last[s[i - 1]] - 1]
last[s[i - 1]] = i
return dp[n] - 1
count1 = count_subsequences(str1)
count2 = count_subsequences(str2)
if count1 == count2:
return str1
elif count1 > count2:
return str1
else:
return str2 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def subSequence(self, strn):
answer = set()
N = 1 << len(strn)
for i in range(N):
temp = ""
for j in range(len(strn)):
if i >> j & 1 == 1:
temp += strn[j]
answer.add(temp)
return len(answer)
def countSubsequence(self, strn):
last = [-1] * 127
N = len(strn)
dp = [0] * (N + 1)
dp[0] = 1
for i in range(1, N + 1):
dp[i] = 2 * dp[i - 1]
if last[ord(strn[i - 1])] != -1:
dp[i] = dp[i] - dp[last[ord(strn[i - 1])]]
last[ord(strn[i - 1])] = i - 1
return dp[N]
def betterString(self, str1, str2):
if self.countSubsequence(str1) >= self.countSubsequence(str2):
return str1
return str2 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def countSubsequences(self, s):
freq = {}
n = len(s)
distinctCount = [0] * (n + 1)
distinctCount[0] = 1
for i in range(1, n + 1):
if s[i - 1] not in freq:
distinctCount[i] = 2 * distinctCount[i - 1]
else:
distinctCount[i] = (
2 * distinctCount[i - 1] - distinctCount[freq[s[i - 1]]]
)
freq[s[i - 1]] = i - 1
return distinctCount[n]
def betterString(self, str1, str2):
count1 = self.countSubsequences(str1)
count2 = self.countSubsequences(str2)
if count1 >= count2:
return str1
else:
return str2 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
Given a pair of strings, Geek wants to find the better string. The better string is the string having more number of distinct subsequences.
If both the strings have equal count of distinct subsequence then return str1.
Example 1:
Input:
str1 = "gfg", str2 = "ggg"
Output: "gfg"
Explanation: "gfg" have 7 distinct subsequences whereas "ggg" have 4 distinct subsequences.
Example 2:
Input: str1 = "a", str2 = "b"
Output: "a"
Explanation: Both the strings have only 1 distinct subsequence.
Constraints:
1 <= str1.lenght , str2.length <= 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function betterString() which takes str1 and str2 as input parameters and returns the better string.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n) | class Solution:
def rec(self, s):
n = len(s)
dp = [1]
m = {}
for i in range(n):
if s[i] in m:
dp.append(dp[i] * 2 - dp[m[s[i]] - 1])
else:
dp.append(dp[i] * 2)
m[s[i]] = i + 1
return dp[n]
def betterString(self, str1, str2):
a = self.rec(str1)
b = self.rec(str2)
if a >= b:
return str1
return str2 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | t = int(input())
for c in range(t):
s = input()
up_max = down_max = right_max = left_max = 0
(first_up) = last_up = first_down = last_down = first_left = last_left = (
first_right
) = last_right = 0
current_x = current_y = 0
horizontal_count = vertical_count = 0
for i in range(len(s)):
if s[i] == "W":
current_y += 1
vertical_count += 1
if current_y > up_max:
up_max = current_y
first_up = last_up = i + 1
elif current_y == up_max:
last_up = i + 1
elif s[i] == "S":
current_y -= 1
vertical_count += 1
if current_y < down_max:
down_max = current_y
first_down = last_down = i + 1
elif current_y == down_max:
last_down = i + 1
elif s[i] == "D":
current_x += 1
horizontal_count += 1
if current_x > right_max:
right_max = current_x
first_right = last_right = i + 1
elif current_x == right_max:
last_right = i + 1
else:
current_x -= 1
horizontal_count += 1
if current_x < left_max:
left_max = current_x
first_left = last_left = i + 1
elif current_x == left_max:
last_left = i + 1
h = up_max - down_max + 1
w = right_max - left_max + 1
ans = h * w
if vertical_count > 1 and last_up < first_down:
ans = min(ans, (h - 1) * w)
if vertical_count > 1 and last_down < first_up:
ans = min(ans, (h - 1) * w)
if horizontal_count > 1 and last_right < first_left:
ans = min(ans, h * (w - 1))
if horizontal_count > 1 and last_left < first_right:
ans = min(ans, h * (w - 1))
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(input().rstrip()) for _ in range(Q)]
for S in Query:
L = len(S)
T = [(0, 0)]
for s in S:
x, y = T[-1]
if s == "W":
T.append((x, y + 1))
elif s == "S":
T.append((x, y - 1))
elif s == "A":
T.append((x - 1, y))
else:
T.append((x + 1, y))
dp1 = [[0, 0, 0, 0] for _ in range(L + 1)]
for i, (x, y) in enumerate(T):
if i == 0:
continue
dp1[i][0] = max(y, dp1[i - 1][0])
dp1[i][1] = min(y, dp1[i - 1][1])
dp1[i][2] = min(x, dp1[i - 1][2])
dp1[i][3] = max(x, dp1[i - 1][3])
lx, ly = T[-1]
dp2 = [[ly, ly, lx, lx] for _ in range(L + 1)]
for i in reversed(range(L)):
x, y = T[i]
dp2[i][0] = max(y, dp2[i + 1][0])
dp2[i][1] = min(y, dp2[i + 1][1])
dp2[i][2] = min(x, dp2[i + 1][2])
dp2[i][3] = max(x, dp2[i + 1][3])
Y, X = dp1[L][0] - dp1[L][1] + 1, dp1[L][3] - dp1[L][2] + 1
ans = 0
for i in range(L):
if dp1[i][0] < dp2[i][0] and dp1[i][1] < dp2[i][1]:
ans = max(ans, X)
if dp1[i][0] > dp2[i][0] and dp1[i][1] > dp2[i][1]:
ans = max(ans, X)
if dp1[i][2] < dp2[i][2] and dp1[i][3] < dp2[i][3]:
ans = max(ans, Y)
if dp1[i][2] > dp2[i][2] and dp1[i][3] > dp2[i][3]:
ans = max(ans, Y)
print(X * Y - ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | for i in range(int(input())):
s = input()
lm, rm, um, dm = 0, 0, 0, 0
xp, yp = 0, 0
for ch in s:
if ch == "W":
yp += 1
elif ch == "A":
xp -= 1
elif ch == "S":
yp -= 1
else:
xp += 1
lm = min(lm, xp)
rm = max(rm, xp)
um = max(um, yp)
dm = min(dm, yp)
xp, yp = 0, 0
lmfSet, rmfSet, umfSet, dmfSet = 0, 0, 0, 0
if lm == 0:
lml = 0
lmf = 0
lmfSet = 1
if rm == 0:
rml = 0
rmf = 0
rmfSet = 1
if um == 0:
uml = 0
umf = 0
umfSet = 1
if dm == 0:
dml = 0
dmf = 0
dmfSet = 1
for i, ch in zip(list(range(1, len(s) + 1)), s):
if ch == "W":
yp += 1
elif ch == "A":
xp -= 1
elif ch == "S":
yp -= 1
else:
xp += 1
if xp == lm:
lml = i
if not lmfSet:
lmf = i
lmfSet = 1
if xp == rm:
rml = i
if not rmfSet:
rmf = i
rmfSet = 1
if yp == um:
uml = i
if not umfSet:
umf = i
umfSet = 1
if yp == dm:
dml = i
if not dmfSet:
dmf = i
dmfSet = 1
canx, cany = 0, 0
if dml + 1 < umf or uml + 1 < dmf:
cany = 1
if lml + 1 < rmf or rml + 1 < lmf:
canx = 1
if canx:
if cany:
print(min((um - dm) * (rm - lm + 1), (um - dm + 1) * (rm - lm)))
else:
print((rm - lm) * (um - dm + 1))
elif cany:
print((um - dm) * (rm - lm + 1))
else:
print((rm - lm + 1) * (um - dm + 1)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | INF = 100000000000.0
move = {"W": (0, 1), "A": (-1, 0), "S": (0, -1), "D": (1, 0)}
def getExtremes(positions):
minX, minY, maxX, maxY = (
[positions[0][0]],
[positions[0][1]],
[positions[0][0]],
[positions[0][1]],
)
for p in positions[1:]:
minX.append(min(minX[-1], p[0]))
minY.append(min(minY[-1], p[1]))
maxX.append(max(maxX[-1], p[0]))
maxY.append(max(maxY[-1], p[1]))
return minX, minY, maxX, maxY
t = int(input())
while t > 0:
t -= 1
s = input()
x, y = 0, 0
positions = [(0, 0)]
for c in s:
x, y = x + move[c][0], y + move[c][1]
positions.append((x, y))
minXBeg, minYBeg, maxXBeg, maxYBeg = getExtremes(positions)
positions.reverse()
minXEnd, minYEnd, maxXEnd, maxYEnd = getExtremes(positions)
minXEnd.reverse()
minYEnd.reverse()
maxXEnd.reverse()
maxYEnd.reverse()
positions.reverse()
ans = INF
for i in range(len(s)):
for c in move:
minX = min(
minXBeg[i], positions[i][0] + move[c][0], minXEnd[i + 1] + move[c][0]
)
maxX = max(
maxXBeg[i], positions[i][0] + move[c][0], maxXEnd[i + 1] + move[c][0]
)
minY = min(
minYBeg[i], positions[i][1] + move[c][1], minYEnd[i + 1] + move[c][1]
)
maxY = max(
maxYBeg[i], positions[i][1] + move[c][1], maxYEnd[i + 1] + move[c][1]
)
area = (maxX - minX + 1) * (maxY - minY + 1)
ans = min(ans, area)
print(ans) | ASSIGN VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR LIST VAR NUMBER NUMBER LIST VAR NUMBER NUMBER LIST VAR NUMBER NUMBER LIST VAR NUMBER NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | q = int(input())
for _ in range(q):
d = [x for x in list(input())]
x, y = 0, 0
minX, maxX, minY, maxY = 0, 0, 0, 0
allowW, allowS, allowA, allowD = True, True, True, True
for v in d:
if v == "W":
y += 1
if y > maxY:
maxY = y
allowS = True
allowW = False
elif y == maxY:
allowW = False
elif v == "S":
y -= 1
if y < minY:
minY = y
allowW = True
allowS = False
elif y == minY:
allowS = False
elif v == "A":
x -= 1
if x < minX:
minX = x
allowA = False
allowD = True
elif x == minX:
allowA = False
else:
x += 1
if x > maxX:
maxX = x
allowA = True
allowD = False
elif x == maxX:
allowD = False
val = (maxX - minX + 1) * (maxY - minY + 1)
if maxX - minX > 1 and (allowD or allowA):
val = min(val, (maxX - minX) * (maxY - minY + 1))
if maxY - minY > 1 and (allowW or allowS):
val = min(val, (maxX - minX + 1) * (maxY - minY))
print(val) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | def main():
h, v = hv = [0], [0]
f = {"W": (v, -1), "S": (v, 1), "A": (h, -1), "D": (h, 1)}.get
for _ in range(int(input())):
del h[1:], v[1:]
for l, d in map(f, input()):
l.append(l[-1] + d)
x = y = 1
for l in hv:
lh, a, n = (min(l), max(l)), 200001, 0
for b in filter(lh.__contains__, l):
if a != b:
a = b
n += 1
le = lh[1] - lh[0] + 1
x, y = y * le, x * (le - (n < 3 <= le))
print(x if x < y else y)
main() | FUNC_DEF ASSIGN VAR VAR VAR LIST NUMBER LIST NUMBER ASSIGN VAR DICT STRING STRING STRING STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | T = int(input())
for _ in range(T):
cmd = input()
mostL, mostR, mostB, mostT = 0, 0, 0, 0
mostLs, mostRs, mostBs, mostTs = [0], [0], [0], [0]
x, y = 0, 0
i = 0
for c in cmd:
i += 1
if c == "W":
y += 1
if y > mostT:
mostT = y
mostTs = [i]
elif y == mostT:
mostTs.append(i)
elif c == "S":
y -= 1
if y < mostB:
mostB = y
mostBs = [i]
elif y == mostB:
mostBs.append(i)
elif c == "A":
x -= 1
if x < mostL:
mostL = x
mostLs = [i]
elif x == mostL:
mostLs.append(i)
elif c == "D":
x += 1
if x > mostR:
mostR = x
mostRs = [i]
elif x == mostR:
mostRs.append(i)
LR = mostR - mostL + 1
if LR >= 3:
firstL, lastL = mostLs[0], mostLs[-1]
firstR, lastR = mostRs[0], mostRs[-1]
cross = lastR > firstL and lastL > firstR
LR_extra = not cross
else:
LR_extra = False
BT = mostT - mostB + 1
if BT >= 3:
firstB, lastB = mostBs[0], mostBs[-1]
firstT, lastT = mostTs[0], mostTs[-1]
cross = lastB > firstT and lastT > firstB
BT_extra = not cross
else:
BT_extra = False
if LR_extra and BT_extra:
area = min((LR - 1) * BT, LR * (BT - 1))
elif LR_extra:
area = (LR - 1) * BT
elif BT_extra:
area = LR * (BT - 1)
else:
area = LR * BT
print(area) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | import sys
input = sys.stdin.readline
Q = int(input())
for testcases in range(Q):
S = input().strip()
X = Y = 0
MAXX = MINX = MAXY = MINY = 0
for s in S:
if s == "D":
X += 1
MAXX = max(MAXX, X)
elif s == "A":
X -= 1
MINX = min(MINX, X)
elif s == "W":
Y += 1
MAXY = max(MAXY, Y)
else:
Y -= 1
MINY = min(MINY, Y)
MAXXLIST = []
MINXLIST = []
MAXYLIST = []
MINYLIST = []
if MAXX == 0:
MAXXLIST.append(0)
if MAXY == 0:
MAXYLIST.append(0)
if MINX == 0:
MINXLIST.append(0)
if MINY == 0:
MINYLIST.append(0)
X = Y = 0
for i in range(len(S)):
s = S[i]
if s == "D":
X += 1
if X == MAXX:
MAXXLIST.append(i + 1)
elif s == "A":
X -= 1
if X == MINX:
MINXLIST.append(i + 1)
elif s == "W":
Y += 1
if Y == MAXY:
MAXYLIST.append(i + 1)
else:
Y -= 1
if Y == MINY:
MINYLIST.append(i + 1)
ANS = (MAXX - MINX + 1) * (MAXY - MINY + 1)
if MAXX - MINX > 1:
if MAXXLIST[0] > MINXLIST[-1] or MINXLIST[0] > MAXXLIST[-1]:
ANS = min(ANS, (MAXX - MINX) * (MAXY - MINY + 1))
if MAXY - MINY > 1:
if MAXYLIST[0] > MINYLIST[-1] or MINYLIST[0] > MAXYLIST[-1]:
ANS = min(ANS, (MAXX - MINX + 1) * (MAXY - MINY))
print(ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | t = int(input())
for _ in range(0, t):
a = list(input())
nowx = 0
nowy = 0
maxx = 0
minx = 0
maxy = 0
miny = 0
tmaxx = 0
tminx = 0
tmaxy = 0
tminy = 0
highw = 0
highs = 0
widthd = 0
widtha = 0
for i in range(0, len(a)):
if a[i] == "W":
nowy += 1
if nowy >= maxy:
maxy = nowy
tmaxy = i
elif a[i] == "S":
nowy -= 1
if nowy <= miny:
miny = nowy
tminy = i
elif a[i] == "D":
nowx += 1
if nowx >= maxx:
maxx = nowx
tmaxx = i
elif a[i] == "A":
nowx -= 1
if nowx <= minx:
minx = nowx
tminx = i
highw = max(highw, nowy - miny)
highs = max(highs, maxy - nowy)
widthd = max(widthd, nowx - minx)
widtha = max(widtha, maxx - nowx)
y1 = max(highw, highs)
y2 = max(highw != 0 or highs != 0, y1 - (highw != highs))
x1 = max(widthd, widtha)
x2 = max(widthd != 0 or widtha != 0, x1 - (widthd != widtha))
print(min((y1 + 1) * (x2 + 1), (1 + y2) * (x1 + 1))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER |
You have a string $s$ — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: 'W' — move one cell up; 'S' — move one cell down; 'A' — move one cell left; 'D' — move one cell right.
Let $Grid(s)$ be the grid of minimum possible area such that there is a position in the grid where you can place the robot in such a way that it will not fall from the grid while running the sequence of commands $s$. For example, if $s = \text{DSAWWAW}$ then $Grid(s)$ is the $4 \times 3$ grid: you can place the robot in the cell $(3, 2)$; the robot performs the command 'D' and moves to $(3, 3)$; the robot performs the command 'S' and moves to $(4, 3)$; the robot performs the command 'A' and moves to $(4, 2)$; the robot performs the command 'W' and moves to $(3, 2)$; the robot performs the command 'W' and moves to $(2, 2)$; the robot performs the command 'A' and moves to $(2, 1)$; the robot performs the command 'W' and moves to $(1, 1)$. [Image]
You have $4$ extra letters: one 'W', one 'A', one 'S', one 'D'. You'd like to insert at most one of these letters in any position of sequence $s$ to minimize the area of $Grid(s)$.
What is the minimum area of $Grid(s)$ you can achieve?
-----Input-----
The first line contains one integer $T$ ($1 \le T \le 1000$) — the number of queries.
Next $T$ lines contain queries: one per line. This line contains single string $s$ ($1 \le |s| \le 2 \cdot 10^5$, $s_i \in \{\text{W}, \text{A}, \text{S}, \text{D}\}$) — the sequence of commands.
It's guaranteed that the total length of $s$ over all queries doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $T$ integers: one per query. For each query print the minimum area of $Grid(s)$ you can achieve.
-----Example-----
Input
3
DSAWWAW
D
WA
Output
8
2
4
-----Note-----
In the first query you have to get string $\text{DSAWW}\underline{D}\text{AW}$.
In second and third queries you can not decrease the area of $Grid(s)$. | for _ in range(int(input())):
s = input()
ar = ["AD", "WS"]
nr = [[], []]
for e in s:
if e in ar[0]:
nr[0].append(e)
else:
nr[1].append(e)
b, c = [float("inf")] * 2, [float("inf")] * 2
for i in range(2):
sm = [0]
for el in nr[i]:
sm += [sm[-1] + (1 if el == ar[i][0] else -1)]
for _ in range(2):
f = sm.index(min(sm))
l = len(sm) - 1 - sm[::-1].index(max(sm))
bse = sm[l] - sm[f] + 1
bst = bse
if bse >= 3 and l < f:
bst -= 1
b[i] = min(b[i], bse)
c[i] = min(c[i], bst)
for j in range(len(sm)):
sm[j] *= -1
print(min(b[0] * c[1], b[1] * c[0])) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING ASSIGN VAR LIST LIST LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER BIN_OP LIST FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR VAR VAR LIST BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.